var Global = {
	init:false,
	direction:1,
	colors:
	{
		main_menu:{over:"#dc005e", out:"#58585a"},
		sub_menu:{over:"#645745", out:"#9c9387"}
	},
	ease:
	{
		realisations:{"in":Circ.easeOut, out:Circ.easeIn}
	}
};

Event.observe(window, "load", initMain);

function initMain()
{
    setupMenu("sub_menu");
	setupMenu("main_menu");
	/*if (Navigation)
	{
		Navigation.register("#main_menu a", {html:true}).onStart(globalLoadingStart).onComplete(globalLoadingComplete);
	}*/
}

function globalLoadingStart()
{
	console.log("start");
}

function globalLoadingComplete(pResponse)
{
	console.log("complete");
	Navigation.transitions.fade(document.getElementById("content"), .3, pResponse.html).onComplete(function()
	{
		setupMenu("sub_menu");
	});
	$$("#main_menu a.menuOn").each(function(t){
        t.removeClassName("menuOn");
        Event.simulate(t, "mouseout");
    });
	$$("#main_menu a").each(function(a)
	{
		if(a.href==Navigation.base+""+Navigation.path)
		{
			a.addClassName("menuOn");
			Event.simulate(a, "mouseover");
		}
	});
	setupMenu("main_menu");
}

//function onSuccessRealisations(pResponse)
//{
//	var text = {};
//	var diapo = {};
//	switch(Global.direction)
//	{
//		case 1:
//			text = {
//				"old":{to:"-158px", delay:0, time:.5},
//				"new":{from:"242px", to:"30px", delay:.5, time:.4}
//			};
//			diapo = {
//				"old":{to:"154px", delay:0, time:.5},
//				"new":{from:"554px", to:"354px", delay:.5, time:.4}
//			};
//            /*diapo = {
//				"old":{to:"154px", delay:.3, time:.3},
//				"new":{from:"554px", to:"354px", delay:.3, time:.7}
//			};*/
//		break;
//		case -1:
//			text = {
//				"old":{to:"242px", delay:0, time:.5},
//				"new":{from:"-158px", to:"30px", delay:.5, time:.4}
//			};
//			diapo = {
//				"old":{to:"554px", delay:0, time:.5},
//				"new":{from:"154px", to:"354px", delay:.5, time:.4}
//			};
//            /*diapo = {
//				"old":{to:"554px", delay:0, time:.5},
//				"new":{from:"154px", to:"354px", delay:.5, time:.4}
//			};*/
//		break;
//	}
//}

function setupMenu(pId)
{
	var d = document.getElementById(pId+"_cursor"), e = true;
	if(!$$("#"+pId+" li a").length)
	{
		if(d)
			M4Tween.to(d, .3, {opacity:0}).onComplete(function(){window.document.body.removeChild(d);})
		return;
	}
	if(!d)
	{
		e = false;
		d = document.createElement("div");
		d.setAttribute("id", pId+"_cursor");
		window.document.body.appendChild(d);
	}
	M4Tween.to(d, .3, {opacity:1});
	var current = $$("#"+pId+" li a.menuOn")[0];
    if(!current)
        return;
	current.setStyle({textDecoration:"none"});
	var o = current.cumulativeOffset();
	if(e)
		M4Tween.to($(pId+"_cursor"), .3, {"left":o.left+"px", width:current.getStyle("width")});
	else
		$(pId+"_cursor").setStyle({"left":o.left+"px", width:current.getStyle("width")});
	$$("#"+pId+" li a").each(function(a)
	{
		Event.stopObserving(a, "mouseover");
		Event.stopObserving(a, "mouseout");
		Event.observe(a, "mouseover", function(e)
		{
			$$("#"+pId+" li a").each(function(a)
			{
				console.log("over");
				if(a.hasClassName("menuOn"))
					return;
				M4Tween.to(a, .3, {color:Global.colors[pId].out});
			});
			M4Tween.to(e.target, .3, {color:Global.colors[pId].over});
		});
		Event.observe(a, "mouseout", function(e)
		{
			if($(e.target).hasClassName("menuOn"))
				return;
			M4Tween.to(e.target, .3, {color:Global.colors[pId].out});
		});
	});
}

var Diaporama= {
	imgs:null,
	currentImg:-1,
	timeout:null,
	stop:function(pKeep)
	{
		if(!pKeep)
			Diaporama.currentImg = -1;
		window.clearTimeout(Diaporama.timeout);
		Diaporama.timeout = null;
	}
};
function tickDiapo()
{
	Diaporama.stop(true);
	Diaporama.currentImg++;
	Diaporama.imgs = $$("#main_container .diapo_real img");
	if(Diaporama.currentImg>=Diaporama.imgs.length)
		Diaporama.currentImg = 0;
	var i = Diaporama.imgs[Diaporama.currentImg];
	Diaporama.imgs.each(function(img)
	{
		var to = img == i?1:0;
		var z = img == i?2:1;
		var t = img == i?.2:.5;
		img.setStyle({zIndex:z});
		M4Tween.to(img, t, {opacity:to});
	});
	Diaporama.timeout = setTimeout(tickDiapo, 2000);
}

function createNewElement(pNode, pProperties)
{
	var e = document.createElement(pNode);
	for(var i in pProperties)
	{
		switch(i)
		{
			case "innerHTML":
				e.innerHTML = pProperties[i];
			break;
			case "text":
				e.appendChild(document.createTextNode(pProperties[i]));
			break;
			default:
				e.setAttribute(i, pProperties[i]);
			break;
		}
	}
	return e;
}

/**
 * @link https://github.com/kangax/protolicious/blob/5b56fdafcd7d7662c9d648534225039b2e78e371/event.simulate.js
 * Event.simulate(@element, eventName[, options]) -> Element
 *
 * - @element: element to fire event on
 * - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
 * - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
 *
 *    $('foo').simulate('click'); // => fires "click" event on an element with id=foo
 *
 **/
(function(){

  var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
  }
  var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
  }

  Event.simulate = function(element, eventName) {
    var options = Object.extend(defaultOptions, arguments[2] || { });
    var oEvent, eventType = null;

    element = $(element);

    for (var name in eventMatchers) {
      if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
      throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent) {
      oEvent = document.createEvent(eventType);
      if (eventType == 'HTMLEvents') {
        oEvent.initEvent(eventName, options.bubbles, options.cancelable);
      }
      else {
        oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
          options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
          options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
      }
      element.dispatchEvent(oEvent);
    }
    else {
      options.clientX = options.pointerX;
      options.clientY = options.pointerY;
      oEvent = Object.extend(document.createEventObject(), options);
      element.fireEvent('on' + eventName, oEvent);
    }
    return element;
  }

  Element.addMethods({ simulate: Event.simulate });
})();
