var timestamp_search = time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);


/*
	$(document).click(function(event) { 
		if($(event.target).parents().index($('#search_footer_block')) == -1) {
			
			if($('#search_footer_block').css('height') != "0px") {
				$(".search_results").stop();
				$(".search_results").animate({
					height: '0px'
				},400);
			}
		}        
	});
*/


function toolOpen(num,name,height1,height2) {
	var $sub_tool = $('#sub_tool_' + num);
	$sub_tool.animate(
		{marginRight: parseInt($sub_tool.css('marginRight')) == 0 ? -134 : 0},
		{queue:false, duration:800, easing: 'easeOutBounce', complete: function(){
				var $tool = $('#tools_' + name);
				$("#tool_" + num).css({'height': height2});
				$tool.animate({
					height: parseInt($tool.css('height')) == height1 ? 33 : height1
				});
			}
		}
	);
}
function toolClose(num,name,height1) {
	var $tool = $('#tools_' + name);		
	$tool.animate(
		{
			height: parseInt($tool.css('height')) == height1 ? 0 : height1
		},
		{
			complete: function(){
				var $sub_tool = $('#sub_tool_' + num);
				$("#tool_" + num).css({'height': '33px'});
				$sub_tool.animate(
				{
					marginRight: parseInt($sub_tool.css('marginRight')) == 0 ? -134 : 0
				},
				{queue:false, duration:800, easing: 'easeOutBounce'});
			}
		}
	);
}
function prodNav(name){
	$('#nav_' + name).mouseover(function() {
		if (this.timer){
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(function() {
			$('.sub_nav_image').show();
		}, 100); // delay
		$('#sub_nav').show();
		$('#nav_' + name).addClass("active");
		$('#nav_' + name).addClass("imHovered");

		var $sub_nav1 = $('#' + name + '_sub_nav');
		$sub_nav1.slideDown("slow");
	});

	$('#nav_' + name).mouseout(function() {
		var $sub_nav1 = $('#' + name + '_sub_nav');
		
		$('#nav_' + name).removeClass("imHovered");
		
		if (this.timer){
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(function() {
			if (!$('#nav_' + name).hasClass('imHovered') && !$('#' + name + '_sub_nav').hasClass('imHovered')) {
				$sub_nav1.hide();
				$('#nav_' + name).removeClass("active");
			}
		}, 100); // delay
		$('#sub_nav').hide();
	});

	$('#' + name + '_sub_nav').mouseover(function() {
		$('#sub_nav').show();
		$('#nav_' + name).addClass("active");
		$('#' + name + '_sub_nav').addClass("imHovered");
	});

	$('#' + name + '_sub_nav').mouseout(function() {
		var $sub_nav1 = $('#' + name + '_sub_nav');
		
		$('#' + name + '_sub_nav').removeClass("imHovered");
		
		if (this.timer){
			clearTimeout(this.timer);
		}
		this.timer = setTimeout(function(){
			if (!$('#nav_' + name).hasClass('imHovered') && !$('#' + name + '_sub_nav').hasClass('imHovered')) {
				$sub_nav1.hide();
				$('#nav_' + name).removeClass("active");
			}
		}, 100); // delay
		$('#sub_nav').hide();
	});
}

function prodSubNav(name){
	$('#button_' + name).mouseover(function() {
		$('#nav_' + name).show();
		$('#button_' + name).addClass("imHovered");
	});

	$('#button_' + name).mouseout(function() {
		$('#button_' + name).removeClass("imHovered");
		if (!$('#nav_' + name).hasClass('imHovered') && !$('#button_' + name).hasClass('imHovered')) {
			$('#nav_' + name).hide();
			$('#button_' + name).removeClass("active");
		}
	});

	$('#nav_' + name).mouseover(function() {
		$('#nav_' + name).show();
		$('#nav_' + name).addClass("active");
		$('#button_' + name).addClass("imHovered");
	});

	$('#nav_' + name).mouseout(function() {
		$('#button_' + name).removeClass("imHovered");
		if (!$('#button_' + name).hasClass('imHovered') && !$('#nav_' + name).hasClass('imHovered')) {
			$('#nav_' + name).hide();
			$('#nav_' + name).removeClass("active");
		}
		$('#nav_' + name).hide();
	});
}


$(document).ready(function() {
	VideoJS.setupAllWhenReady({
		controlsHiding: false,
		controlsBelow: true,
		defaultVolume: 0.00,
		flashPlayerVersion: 9
	});
	
	$('#bt_print').click(function() {
		window.print();
		return false;
	});
	
	$('#slider').nivoSlider({
		effect:'fade',
		boxCols: 8,
        boxRows: 4,
        animSpeed:500,
        pauseTime:3000,
        startSlide:0,
        directionNav:true,
        directionNavHide:false,
        controlNav:true,
        controlNavThumbs:false,
        keyboardNav:true,
        pauseOnHover:true,
        manualAdvance:false,
        captionOpacity:0.8,
        prevText: 'Prev', 
        nextText: 'Next'
	});
	
	$(".prod_carousel").jCarouselLite({
        btnNext: "#carousel_next",
        btnPrev: "#carousel_prev",
		visible: 8,
		scroll: 8,
		speed: 1000
    });
	
	$('.sub_nav_buttons').mouseover(function() {
		$('.sub_nav_image').hide();
	});
	prodSubNav('about_1');
	prodSubNav('about_2');
	prodSubNav('about_3');
	prodSubNav('about_4');
	prodSubNav('about_5');
	prodSubNav('about_6');
	prodSubNav('about_7');
	
	prodSubNav('innovation_1');
	prodSubNav('innovation_2');
	prodSubNav('innovation_3');
	prodSubNav('innovation_4');
	
	prodSubNav('solutions_1');
	prodSubNav('solutions_2');
	prodSubNav('solutions_3');
	prodSubNav('solutions_4');
	
	prodSubNav('youare_1');
	prodSubNav('youare_2');
	prodSubNav('youare_3');
	
	/******************/
	/* MENU PRINCIPAL */
	/******************/
	
	prodNav('about');
	prodNav('innovation');
	prodNav('solutions');
	prodNav('youare');
	
	/******************/
	/*    OUTILS      */
	/******************/
	
	
	
	$('#sub_tool_toggle_1').click(function() {		
		if($("#tool_1").css('height') == "33px") toolOpen('1','fav','67','105');
		else if($("#tool_1").css('height') == "105px") toolClose('1','fav','67');			
	});
	
	$('#sub_tool_toggle_2').click(function() {		
		if($("#tool_2").css('height') == "33px") toolOpen('2','share','252','290');
		else if($("#tool_2").css('height') == "105px") toolClose('2','share','252');			
	});
	
	$('#sub_tool_toggle_3').click(function() {		
		if($("#tool_3").css('height') == "33px") toolOpen('3','print','27','65');
		else if($("#tool_3").css('height') == "105px") toolClose('3','print','27');			
	});
	
	$('#sub_tool_toggle_4').click(function() {		
		if($("#tool_4").css('height') == "33px") toolOpen('4','rss','105','140');
		else if($("#tool_4").css('height') == "105px") toolClose('4','rss','105');			
	});
	
	$('#sub_tool_toggle_5').click(function() {		
		if($("#tool_5").css('height') == "33px") toolOpen('5','cart','57','95');
		else if($("#tool_5").css('height') == "105px") toolClose('5','cart','57');			
	});
	
	
	/*$('#sub_tool_toggle_1').toggle(function(){toolOpen('1','fav','67','105')},function(){toolClose('1','fav','67')});
	$('#sub_tool_toggle_2').toggle(function(){toolOpen('2','share','252','290')},function(){toolClose('2','share','252')});
	$('#sub_tool_toggle_3').toggle(function(){toolOpen('3','print','27','65')},function(){toolClose('3','print','27')});
	$('#sub_tool_toggle_4').toggle(function(){toolOpen('4','rss','105','140')},function(){toolClose('4','rss','105')});
	$('#sub_tool_toggle_5').toggle(function(){toolOpen('5','cart','57','95')},function(){toolClose('5','cart','57')});
	
	$('#sub_tool_toggle_8').toggle(function(){toolOpen('8','add','0','33')},function(){toolClose('8','add','0')});*/
	
	
	/*$('#sub_tool_1 span.tool_title').click(function(){toolClose('1','fav','100')});
	$('#sub_tool_2 span.tool_title').click(function(){toolClose('2','share','250')});
	$('#sub_tool_3 span.tool_title').click(function(){toolClose('3','print','60')});
	$('#sub_tool_4 span.tool_title').click(function(){toolClose('4','rss','134')});
	$('#sub_tool_5 span.tool_title').click(function(){toolClose('5','cart','90')});
	$('#sub_tool_8 span.tool_title').click(function(){toolClose('8','add','33')});*/
	
	
	
	$('#recommend_form').submit(function() {
		var recommend_post = {}

		recommend_post['share_name'] = $("[name=share_name]").val();
		recommend_post['share_email'] = $("[name=share_email]").val();
		recommend_post['share_dest_email'] = $("[name=share_dest_email]").val();
		recommend_post['share_message'] = $("[name=share_message]").val();
		recommend_post['share_url'] = window.location.href;
		
		$.post(site_url + "/ajax/recommend.php", recommend_post, function loaded(data) {
			$("#recommend_data").html(data);
		});
		
		return false;
	});
	
	$('#rss_press,#rss_photos,#rss_backgrounders').click(function() {
		$("#rss_url").attr("href", $(this).val());
	});
	
	
	
	$(document).click(function(event) { 
		if($(event.target).parents().index($('#tool_1')) == -1 && $(event.target).parents().index($('#tool_2')) == -1 && $(event.target).parents().index($('#tool_3')) == -1 && $(event.target).parents().index($('#tool_4')) == -1) {
						
			if($("#tool_1").css('height') == "105px") {
				toolClose('1','fav','67');
			}
			
			if($("#tool_2").css('height') == "290px") {
				toolClose('2','share','252');
			}
			
			if($("#tool_3").css('height') == "65px") {
				toolClose('3','print','27');
			}
			
			if($("#tool_4").css('height') == "140px") {
				toolClose('4','rss','105');
			}
			
			if($("#tool_5").css('height') == "95px") {
				toolClose('5','cart','57');
			}
			
		}        
	});
	
		
	

	/*****************/
	/* MENU PRODUITS */
	/*****************/
	
	$("#products_nav.navigation ul.subMenu").hide();
	$("#products_nav.navigation ul.capacity_active").show();
    $("#products_nav.navigation ul.subMenu2").hide();
	
	$("li.nav_active").parents().show();
	$("li.nav_active").parents("li.subMenuLevel1 > a span").html("[ - ]");
	
    $("#products_nav.navigation li.toggleSubMenu > a").click( function () { 
		if ($(this).next("ul.subMenu:visible").length != 0) { 
            $(this).next("ul.subMenu").slideUp("normal");
        }
        else {
            $(".navigation ul.subMenu").slideUp("normal"); 
            $(this).next("ul.subMenu").slideDown("normal");	
        }
		
		if ($(this).next("ul.subMenu2:visible").length != 0) { 
            $(this).next("ul.subMenu2").slideUp("normal");
			$("span").html("[ + ]");
        } 
        else {
            $(".navigation ul.subMenu2").slideUp("normal"); 
			$("li.subMenuLevel1 a span").html("[ + ]");			
        } 
        return false; 
    }); 
	
    $("#products_nav.navigation li.subMenuLevel1 > a").click( function () {
        if ($(this).next("ul.subMenu2:visible").length != 0) { 
            $(this).next("ul.subMenu2").slideUp("normal");
			$("span", this).html("[ + ]");
        } 
        else {
            $(".navigation ul.subMenu2").slideUp("normal"); 
            $(this).next("ul.subMenu2").slideDown("normal");
			$("li.subMenuLevel1 a span").html("[ + ]");
			$("span", this).html("[ - ]");
        }
        return false;
    });

	
	/*****************/
	/* MENU  CONTENU */
	/*****************/
	
	$("#content_nav.navigation ul.subMenu").hide();
	$("#content_nav.navigation ul.page_active").show();
    $("#content_nav.navigation ul.subMenu2").hide();
	
	$("li.nav_active").parents().show();
	$("li.nav_active_ns").parents().show();
	$("li.nav_active").parents("li.subMenuLevel1 > a span").html("[ - ]");
	
    $("#content_nav.navigation li.toggleSubMenu > a").mouseover( function () { 
        if($(this).next("ul.subMenu:visible").length == 0) {
            $(".navigation ul.subMenu").slideUp("normal"); 
            $(this).next("ul.subMenu").slideDown("normal");	
        }
		
        /*if ($(this).next("ul.subMenu2:visible").length == 0) { 
            $(".navigation ul.subMenu2").slideUp("normal"); 
			$("li.subMenuLevel1 a span").html("[ + ]");			
        } */
        return false; 
    }); 
	
    /*$("#content_nav.navigation li.subMenuLevel1 > a").click( function () {
        if($(this).next("ul.subMenu2:visible").length == 0) { 
            $(".navigation ul.subMenu2").slideUp("normal"); 
            $(this).next("ul.subMenu2").slideDown("normal");
			$("li.subMenuLevel1 a span").html("[ + ]");
			$("span", this).html("[ - ]");
        }
        return false;
    });*/
	
	/*****************/
	/* MEDIA GALLERY */
	/*****************/
		
	$('#media_search_advanced').click(function() {
		$('#media_nav_search_2').slideDown();
		return false;
	});
	
	$("a#fancybox_trigger_slideshow").fancybox({
		onComplete : function() {
			//$('#video_cvs').get(0).play();
		}
	});
	
	$("a#fancybox_trigger").fancybox();

	/*****************/
	/* SEARCH FOOTER */
	/*****************/
	$(document).click(function(event) { 
		if($(event.target).parents().index($('#search_footer_block')) == -1) {
			
			if($('#search_footer_block').css('height') != "0px") {
				$(".search_results").stop();
				$(".search_results").animate({
					height: '0px'
				},400);
			}
		}        
	});

	$("#footer_search").click(function(){
		if($(this).val() == 'Search...'){
			$(this).val('');
		}
	});
	
	$("#header_search").click(function(){
		if($(this).val() == 'Search...'){
			$(this).val('');
		}
	});
	
	$("#footer_searchform").submit(function () {
		var value = $("#footer_search").val();
		searchInit(value);
		return false;
    });
	
	$("#header_searchform").submit(function () {
		var value = $("#header_search").val();
		searchInit(value);
		return false;
    });
	
	$("#search_footer").submit(function () {
		var value = $("#footer_popup_search").val();
		searchInit(value);
		return false;
    });
	
	$("#footer_search").keyup(function () {
		var value = $(this).val();
		if((value.length >= 3) && (value != 'Search...')){

			searchInit(value);
		}		
    }).keyup();
	
	$("#header_search").keyup(function () {
		var value = $(this).val();
		if((value.length >= 3) && (value != 'Search...')){

			searchInit(value);
		}		
    }).keyup();
	
	$("#footer_popup_search").keyup(function () {
		var value = $(this).val();
		if((value.length >= 3) && (value != 'Search...')){
			
			var search_post = {}

			search_post["search_id"] = timestamp_search;
			search_post["content"] = value;
			
			searchContent(search_post, 'footer');
		}		
    }).keyup();
	
	$("#footer_results_right_content").delegate(":checkbox", "click", function() {		
		
		var thisCheckbox = $(this);
		var search_post = {}
		
		search_post["search_id"] = timestamp_search;
		search_post["checkbox"] = thisCheckbox.val();
		
		
		if (thisCheckbox.is(':checked')) {
			search_post["checkbox_value"] = 1;
		}
		else {
			search_post["checkbox_value"] = 0;		
		}
		
		searchContent(search_post, 'footer');		
	});
	
	
	$("#search_content_footer").delegate(".changeType", "click", function(){
		var search_post = {}

		search_post["search_id"] = timestamp_search;
		search_post["type"] = $(this).attr("rel");
		
		searchContent(search_post, 'footer');
		
		return false;
	});
	
	
	
	$("#footer_results_right_content").delegate(".addCapacity", "click", function() {		
		
		var search_post = {}
		
		search_post["search_id"] = timestamp_search;
		search_post["capacity"] = $(this).attr("rel");
		search_post["action"] = "add";
		
		searchContent(search_post, 'footer');		
	});
	
	$("#search_content_footer").delegate(".removeCapacity", "click", function() {		
		
		var search_post = {}
		
		search_post["search_id"] = timestamp_search;
		search_post["capacity"] = $(this).attr("rel");
		search_post["action"] = "remove";

		searchContent(search_post, 'footer');	
	});
	
	
	
	$("#footer_results_right_content").delegate(".addYear", "click", function() {		
		
		var search_post = {}
		
		search_post["search_id"] = timestamp_search;
		search_post["year"] = $(this).attr("rel");
		search_post["action"] = "add";
		
		searchContent(search_post, 'footer');			
	});
	
	$("#search_content_footer").delegate(".removeYear", "click", function() {		
		
		var search_post = {}
		
		search_post["search_id"] = timestamp_search;
		search_post["year"] = $(this).attr("rel");
		search_post["action"] = "remove";

		searchContent(search_post, 'footer');			
	});
	
	$("#search_results_footer_pages").delegate(".goSearchPage", "click", function() {		
		
		var search_post = {}
		
		search_post["search_id"] = timestamp_search;
		search_post["page"] = $(this).attr("rel");

		searchContentPagination(search_post, 'footer');			
	});
	
	
	function searchInit(value) {
		var search_post = {}

		search_post["search_id"] = timestamp_search;
		search_post["content"] = value;
		
		$("#search_content_footer").html('<div style="font-family: BankGothicLtBTLight, arial, helvetica, sans-serif; text-align: center;"><br /><br /><br /><br /><br /><br /><br /><img src="images/mbda-loader.gif" width="54" height="55" alt="" /><br /><br /><br />Loading</div>');
				
		$.post(site_url + "/ajax/search_results.php", search_post, function loaded(data) {
			
			$("#search_content_footer").html(data);
			
			$(".footer_popup_search").val(value);
			$(".search_results").animate({
				height: '420px'
			},1000);
			//$(".footer_popup_search").focus();
			
			$.post(site_url + "/ajax/search_results_pagination.php", search_post, function loaded(data) {
				$("#search_results_footer_pages").html(data);
			});
		});
	}
	
	function searchContent(search_post, position) {		
		if(position == 'footer') {
			$("#search_content_footer").html('<div style="font-family: BankGothicLtBTLight, arial, helvetica, sans-serif; text-align: center;"><br /><br /><br /><br /><br /><br /><br /><img src="images/mbda-loader.gif" width="54" height="55" alt="" /><br /><br /><br />Loading</div>');
		}
		
		$.post(site_url + "/ajax/search_results.php", search_post, function loaded(data) {				
			$("#search_content_footer").html(data);
			
			$.post(site_url + "/ajax/search_results_pagination.php", search_post, function loaded(data) {
				$("#search_results_footer_pages").html(data);
			});
		});	
	}
	
	
	function searchContentPagination(search_post, position) {		
		if(position == 'footer') {
			$("#search_content_footer").html('<div style="font-family: BankGothicLtBTLight, arial, helvetica, sans-serif; text-align: center;"><br /><br /><br /><br /><br /><br /><br /><img src="images/mbda-loader.gif" width="54" height="55" alt="" /><br /><br /><br />Loading</div>');
		}
		
		$.post(site_url + "/ajax/search_results_pagination.php", search_post, function loaded(data) {
			$("#search_results_footer_pages").html(data);
			
			$.post(site_url + "/ajax/search_results.php", search_post, function loaded(data) {				
				$("#search_content_footer").html(data);
			});	
		});
		
	}
	

	$(".search_results_footer_reduce").click(function() {
		var value = $(".footer_popup_search").val();
		$("#footer_search").val(value);
		$("#footer_search").focus();
		$(".search_results").animate({
				height: '0px'
			},1000);
	});
	
	
	
	/*****************/
	/* SEARCH HEADER */
	/*****************/
	/*$("#header_search").click(function(){
		if($(this).val() == 'Search...'){
			$(this).val('');
		}
	});
	
	$("#header_search").keyup(function () {
		var value = $(this).val();
		if((value.length >= 3) && (value != 'Search...')){
        	$(".header_popup_search").val(value);
			$(".search_results_top").animate({
				height: '420px'
			},1000);
			$(".header_popup_search").focus();
		}
    }).keyup();

	$(".search_results_top_reduce").click(function() {
		var value = $(".header_popup_search").val();
		$("#header_search").val(value);
		$("#header_search").focus();
		$(".search_results_top").animate({
				height: '0px'
			},1000);
	});*/
	
	
	/*****************/
	/* SEARCH HEADER */
	/*****************/	
	$("#prod_media_gallery").delegate("#carousel_photos", "click", function(){
		
		var carousel_post = {}

		carousel_post["missile_id"] = $(this).attr("rel");
		carousel_post["type"] = "photos";
		
		$.post(site_url + "/ajax/missile_carousel.php", carousel_post, function loaded(data) {				
			$("#prod_media_gallery").html(data);
			reloadCarouselMissiles(carousel_post["type"]);
		});	
		
		return false;
	});
	
	$("#prod_media_gallery").delegate("#carousel_videos", "click", function(){
		
		var carousel_post = {}

		carousel_post["missile_id"] = $(this).attr("rel");
		carousel_post["type"] = "videos";
		
		$.post(site_url + "/ajax/missile_carousel.php", carousel_post, function loaded(data) {				
			$("#prod_media_gallery").html(data);
			reloadCarouselMissiles(carousel_post["type"]);
		});	
		
		return false;
	});	
	
	function reloadCarouselMissiles(type) {
		
		var nb_visible = 8;
		
		if(type == 'photos') nb_visible = 8;
		else if(type == 'videos') nb_visible = 5;
		
		$(".prod_carousel").jCarouselLite({
			btnNext: "#carousel_next",
			btnPrev: "#carousel_prev",
			visible: nb_visible,
			scroll: nb_visible,
			speed: 1000
		});
	}
	
});


