/**
 * @author plo
 */

var objectMenu = null;
var objectTab = null;

function checkHoverMenu() {
	if (objectMenu) {
		objectMenu.find('ul').slideUp(200,function(){
			$('.hoverStatus').removeClass('hoverStatus');
			$('.noDotted').removeClass('noDotted');
		});
	} 
}

// function checkHoverTabs() {
// 	if (objectTab) {
// 		/*
// 		if(objectTab.find('div.description').is(':animated')){
// 			objectTab.find('div.description').stop().fadeIn("100", function(){
// 				objectTab.find('div.description').fadeOut("5");
// 			});
// 		}else{
// 		
// 			objectTab.find('div.description').fadeOut("100");
// 		}
// 		objectTab.find('div.description').fadeOut("100");
// 		*/
// 		objectTab.find('div.description').css('visibility','hidden');
// 	} 
// }

function initNavigation(){
	var myFirstLink = $('#nav li:first a');
	var myCurrentLink = $('#nav li a.subNav').parents('li');
	
	myCurrentLink.hover(
		function(){
			myFirstLink.addClass('noDotted');
			$(this).children('a:first').addClass('hoverStatus');
			$(this).children('ul').slideDown(200);
		}, 
		function(){
			objectMenu = $(this);
			setTimeout("checkHoverMenu()", 100);
		}
	);
}

function initTargetLink(){
	$('a.new-window').click(function(){
        window.open(this.href);
        return false;
    });
}
/*Car Description : tabs at the bottom of the page*/
function initCarTabs(){
	$('.navCarDescription li').not($(".content ul li")).toggle(
		function(){
			maskAllDescription();
			var myLiParent = $(this);
			var myLeftPosition = myLiParent.position().left + 'px';
			var myDivDescription = $(this).children('div.description');
				myDivDescription.css('left', myLeftPosition);
				myDivDescription.css('visibility', 'visible');
			if($(this).hasClass('lastTab')){
				myLeftPosition = (myLiParent.position().left)-(340 - myLiParent.width()) + 'px';
				myDivDescription.css('left', myLeftPosition);
			}
			
			if($(this).hasClass('active')) $(this).removeClass("active");
			else $(this).addClass("active");
			$(this).find('div.description').click(function(){ // add by nlenaerts
				window.location = $(this).parents("li").find('a.tabs').attr("href");
			});
			
			return false;
		},
		function(){
			$(this).find('div.description').css('visibility','hidden');
			if($(this).hasClass('active')) $(this).removeClass("active");
			else $(this).addClass("active");
			//objectTab = $(this);
			//setTimeout("checkHoverTabs()", 0);
			
			
			
		}
	);
	
	/*$('.navCarDescription li .description').mouseout(function() { // comment by nlenaerts
		console.log("out");
		$(this).css('visibility','hidden');
	});*/
}

function maskAllDescription(){
	$('div.description').css('visibility','hidden');
	$('#carDescription ul.navCarDescription li').removeClass("active");
}

function initTarifsOptionsTabs(){
	$('li.withListing').not($(".brochures #tarifsOptions ul li.withListing")).click(

		function () {
			if($(this).hasClass('selected')) {
				$(this).removeClass('selected');
				$(this).children('ul').slideUp("slow");
			}
			else {
				$('li.withListing').removeClass('selected');
				$('li.withListing').children('ul').slideUp("slow");
				$(this).addClass('selected');
				$(this).children('ul').slideDown("slow");
			}
		}
	);
}

// open link in new window
function openExtLink() {
	$("a#ext, a.ext").click(function(){
		window.open(this.href);
		return false;
	});
}

// launch new page with printed style
function launchPrint() {
	$(".printButton").click(function() {
		var url = document.location.href;
	 	window.open(url);
		return false;
	});
}


// create a new stylesheet for the print preview page
var doPrintPreview = function()
{ 
	var oLink = document.createElement('link');
	oLink.href = "../_css/print_preview.css";
	oLink.rel = "stylesheet";
	oLink.type = "text/css";
	document.getElementsByTagName('head')[0].appendChild(oLink);
}

if (self.opener && self.opener.doPrintPreview) {
	doPrintPreview();
	// to delay the function
	//	$(window).bind('load', function(){ doPrintPreview(); } );
} 

$(function(){
	initTargetLink();
    initNavigation();
	initCarTabs();
	initTarifsOptionsTabs();
	launchPrint();
	openExtLink();
});