function mainNaviHorizontal()
{

	var $el, leftPos, newWidth;
		$mainNav2 = $("#magicline");


	$mainNav2.append("<li id='magic-line-background'></li>");
	var $magicLineTwo = $("#magic-line-background");

	$magicLineTwo
		.width($(".magicline_current").width())
		.height($mainNav2.height())
		.css("left", $(".magicline_current a").position().left)
		.data("origLeft", $(".magicline_current a").position().left)
		.data("origWidth", $magicLineTwo.width())
		.data("origColor", $magicLineTwo.css("color"));

	$("#magicline a").hover(function() {
		$el = $(this);
		leftPos = $el.position().left;
		newWidth = $el.parent().width();
		$(".magicline_current a").css("color", "#173378");
		$magicLineTwo.stop().animate({
		left: leftPos,
		width: newWidth
		}, function() {
			$(".magicline_current a").css("color", "#173378"); // repetition pour ne pas avoir un clip à l'initialisation
			$el.css("color", "#FFFFFF");

		});
	}, function() {
		$magicLineTwo.stop().animate({
		left: $magicLineTwo.data("origLeft"),
		width: $magicLineTwo.data("origWidth")
		}, function() {
			$(".magicline_current a").css("color", "#FFFFFF");
		});
		$el.css("color", "#173378");

	});

	/* Kick IE into gear */
	$(".magicline_current a").mouseenter();


}


function mainNaviVertical()
{
	$('#navigation .navi_produits').click(function() {
		if ($(this).next().css("display")=="block")
		{
			$(this)
				.next().slideUp().end()
				.removeClass("active")
				.blur();
			$('#contenu').removeClass("open");
		} else
		{
			$(this)
				.next().slideDown().end()
				.addClass("active")
				.blur();
			$('#contenu').addClass("open");
		}
		return false;
	});

	$('#navigation .navi_produits').next().find('li').click(function() {
		naviPopupOpen($(this).attr("class").split(" ")[0].split("_")[2]);
	});


}

function naviPopupOpen(cha_id, fam_id)
{

	$.post(
		"ajax/general.ajax.php",
		{
			action: "popup_navi_content",
			cha_id: cha_id
		},
		function(data)
		{
			var tmp = data.split(":::");
			$('#navi_popup .header').css("background-image", "url(design/images/"+tmp[0]+")");
			$('#navi_popup .content')
				.css("background-image", "url(design/images/"+tmp[1]+")")
				.html(tmp[2]);
			$('#navi_popup')
				.show("slow", "easeOutBounce")
				.css({"left": "370px", "top": "170px"})
				.draggable({ handle: ".header"});

			/*.position({
				"my": "center center",
				"at": "center center",
				"of": $("body")
			})*/
			clickOutsidePopup();

			if (typeof fam_id != "undefined")
			{
				var accordion_active = $('#accordion_index_'+fam_id).index('#popup_navi_accordion h3');
			} else
			{
				var accordion_active = false;
			}

			$('#popup_navi_accordion').accordion({
				autoHeight: false,
				clearStyle: true,
				animated: "easeOutElastic",
				collapsible: true,
				active: accordion_active,
				maxHeight: 400
			});
		}
	);


	$('#navi_popup .header .close').hover(
		function()
		{
			$(this).css("background-size", "110% 110%");
		}, function()
		{
			$(this).css("background-size", "100% 100%");
		}
	).click(function() {
		naviPopupClose();
	});



	$('.overlay').addClass("overlay_on");
	return false;

}

function clickOutsidePopup()
{
	$('body').click(function() {
		naviPopupClose();
	});
	$('#navi_popup').click(function(event){
		event.stopPropagation();
	});
}

function naviPopupClose()
{
	$('#navi_popup').draggable("destroy");
	$('#navi_popup').fadeOut("fast");
	$('#popup_navi_accordion').accordion("destroy");
	$('.overlay').removeClass("overlay_on");
	$('#navi_popup .header .close').unbind();
	$('body, #navi_popup').unbind();
}

function formEffects()
{
	$('.formulaire, #dialog, #contenu')
		.undelegate('.input_submit', "mouseenter mouseleave mousedown mouseup")
		.delegate('.input_submit', "mouseenter mouseleave mousedown mouseup",
			function(event) {
				switch(event.type)
				{
					case "mouseenter":
						$(this).addClass("input_hover").removeClass("input_focus");
						break;

					case "mouseleave":
						$(this).removeClass("input_hover");
						break;

					case "mousedown":
						$(this).addClass("input_focus");
						break;

					case "mouseup":
						$(this).removeClass("input_focus");
						break;
				}
			}
		)
		.undelegate('.input_text, .input_password, .textarea', "focusin focusout")
		.delegate('.input_text, .input_password, .textarea', "focusin focusout",
			function(event) {
				switch(event.type)
				{
					case "focusin":
						$(this).addClass("input_focus");
						break;

					case "focusout":
						$(this).removeClass("input_focus");
						break;
				}
			}
		)
		;

}

var delay = (function(){
	var timer = 0;
	return function(callback, ms){
	clearTimeout (timer);
		timer = setTimeout(callback, ms);
	};
})();

function form_input_is_int(input){
	return !isNaN(input)&&parseInt(input)==input;
}

/* FUNCTION TYPEWRITER POUR BANNIERE */
$.fn.typewriter = function(opt) {
  var typeone = function(self, text, content) {
    if (text.length > 0) {
      var next = text.match(/(\s*(<[^>]*>)?)*(&.*?;|.?)/)[0];
      text = text.substr(next.length);
      $(self).html(content+next);
      setTimeout(function(){
        typeone(self, text, content+next);
      }, opt['delay']);
    }
  }
  this.each(function() {
    opt = opt || { 'delay': 100 };
    $(this).height($(this).height());
    $(this).width($(this).width());
    typeone(this, $(this).html(), '');
  });
}


/* FUNCTION DELAY */
var delay = (function(){
	var timer = 0;
	return function(callback, ms){
	clearTimeout (timer);
		timer = setTimeout(callback, ms);
	};
})();



/* FUNCTION SEARCH */

function searchModule()
{
	var wrapper = $('#search_div2');
	var input = $('#search_input');
	var reset_wrapper = $('#search_submit');
	var results_wrapper = $('#search_results');

	function clickOutside()
	{
		$('body')
			.bind(
				"click.outside_search",
				function()
				{
					input.val("");
					hideResults();
				}
			);
		results_wrapper.click(function(event){
			event.stopPropagation();
		});
	}


	function initResults(results)
	{
		function show()
		{
			results_wrapper
				.html(results)
				.show();
			actions();
		}

		function actions()
		{
			results_wrapper
				.undelegate('ul.chapitres a, ul.familles a', "click")
				.delegate('ul.chapitres a, ul.familles a', "click",
					function(event)
					{
						var cha_id = $(this).attr("class").split("_")[2];
						if ($(this).closest('ul').hasClass("chapitres") )
						{
							naviPopupOpen(cha_id)
						} else
						{
							var fam_id = $(this).attr("class").split("_")[5];
							naviPopupOpen(cha_id, fam_id)
						}
						hideResults();
						return false;
					}
				);
		}
		show();
	}

	function hideResults()
	{
		results_wrapper
			.unbind()
			.undelegate('ul.chapitres a, ul.familles a', "click")
			.empty()
			.slideUp("fast");

		$('body').unbind("click.outside_search");
	}

	function waiting()
	{
		results_wrapper
			.html("<p>Recherche en cours... <img src='../design/images/loader.gif' alt='loading..' /></p>")
			.slideDown("fast");
	}

	function launcher()
	{
		if (input.val().length >= 2)
		{
			delay(function() {
				waiting();
				$.post(
					"ajax/recherche.ajax.php",
					{
						action: "search",
						string: input.val()
					},
				       function(data)
				       {
						initResults(data);
				       }
				);
				clickOutside();
			}, 750);
		} else
		{
			delay(function() {
				hideResults();
			}, 750);
		}
	}

	function resetButton()
	{
		reset_wrapper
			.hover(
				function() {
					$(this).addClass("active");
				}, function() {
					$(this).removeClass("active");
				}
			)
			.click(
				function()
				{
					input.val("");
					hideResults();
					return false;
				}
			);
	}

	function init()
	{
		resetButton();
		input
			.val("")
			.unbind()
			.keyup(
				function() {
					launcher();
				}
			);
	}

	init();
}

jQuery(document).ready(function() {

	mainNaviHorizontal();
	mainNaviVertical();
	formEffects();

	$('#identification').hover(function() {
		$(this).animate({ top: "5px" });
	}, function() {
		$(this).animate({ top: "0px" });
	});

	$('#mini_panier').hover(function() {
		$(this).animate({ top: "5px" });
	}, function() {
		$(this).animate({ top: "0px" });
	});


	searchModule();


	$.redmine.settings.ajax_url = "./ajax/redmine.ajax.php";
	$.redmine();
});
