var marquee = function() {
    var leftPos = $(".textMarquee").parent().width() + 10 + "px";
    $(".textMarquee").animate(
	    { left: ($(".textMarquee").width() - $(".textMarquee").parent().width() - $(".textMarquee").parent().width()) },
	    35000, function() { $(".textMarquee").css("left", leftPos); marquee(); }
	 );
}

var showSelectOptions = function() {
    $(".selectOptions").hide();
    $(".selectOptions").css("visibility", "visible");
    $(".selectOptions").each(function() {
        if (!$(this).hasClass("styledSelectContent")) {
            $(this).find("li").eq($(this).attr("rel")).addClass("liSelected");
            /*var idInput = $(this).attr("id").replace("Options", "");
            $("#" + idInput).val($(this).find("li").eq($(this).attr("rel")).text());*/
        }
    });
    $(".showSelectOptions").click(function() {
        var optionsId = "#" + $(this).attr("id") + "Options";
        if ($(optionsId).hasClass("opened")) {
            $(optionsId).slideUp();
            $(optionsId).removeClass("opened");
        }
        else {
            $(optionsId).slideDown();
            $(optionsId).addClass("opened");
        }
        return false;
    });
    $(".selectOptions li").click(function() {
        $(this).parents(".selectOptions").slideUp();
        $(this).parents(".selectOptions").prev("input.showSelectOptions").val($(this).text());
        $(this).parents(".selectOptions").removeClass("opened");
        $(this).parents(".selectOptions").find(".liSelected").removeClass("liSelected");
        $(this).addClass("liSelected");
        if ($(this).parents(".selectOptions").hasClass("styledSelectContent")) {
            $(this).parents(".selectOptions").prev("input.showSelectOptions").attr("rel", $(this).attr('rel'));
            /*SwitchIataDep($(this).attr('rel'));*/
        }
        return false;
    });
    $(".selectOptions li").hover(function() {
        $(this).css("background-color", "#f4fb8a");
        $(this).css("color", "#735f42");
    }, function() {
        $(this).css("background-color", "#fff");
        $(this).css("color", "#777");
    });
}

var emptyField = function() {
    $(".fieldToEmpty").focus(function() {
        if ($(this).val() == $(this).attr("rel")) {
            $(this).val("");
        }
    });
}



var initPopins = function() {
    if ($(".showPopin").is("a") || $("#bookingConfirm").is("input") || $(".showPopinInsurances").is("input")) {
        var theMask = '<div id="mask">&nbsp;</div>';
        if (!$("#mask").is("div")) {
            $("body").append(theMask);
        }
       if($(".showPopin").is("a")) initShowPopin();
		 if($(".showPopinInsurances").is("input")) initShowPopinInsurances();
    }
}

var initClosePopin = function() {
    $(".closePopin").click(function() {
        $(".window-container").fadeOut("slow");
		  $(".window-container").children(".window-content").html("");
        $("#mask").hide();
		  showActiveObjects();
		  var ie6 = ($.browser.msie && parseInt(jQuery.browser.version.substr(0,1))<=6);
		  if(!ie6){
	  			$(".window-container").css("position", "fixed");
		  }
        return false;
    });
     $("#mask").click(function() {
        $(".window-container").fadeOut("slow");
		  $(".window-container").children(".window-content").html("");
        $("#mask").hide();
		  showActiveObjects();
		  var ie6 = ($.browser.msie && parseInt(jQuery.browser.version.substr(0,1))<=6);
		  if(!ie6){
	  			$(".window-container").css("position", "fixed");
		  }
        return false;
     });
}

var hideActiveObjects = function() {
	if($.browser.msie){
		// virer les selects pour IE
		$("select").css("visibility","hidden");
		$(".window-container select").css("visibility","visible");
		$("legend").css("visibility","hidden");
		$(".window-container legend").css("visibility","visible");
	 }
	 if((navigator.userAgent.toLowerCase().indexOf("mac")!=-1)?true:false){
		// on masque les Flashs sur Mac
		$("embed").css("visibility","hidden");
	 }
}

var showActiveObjects = function() {
	if($.browser.msie){
		$("select").css("visibility","visible");
		$("legend").css("visibility","visible");
	}
	if((navigator.userAgent.toLowerCase().indexOf("mac")!=-1)?true:false){
		$("embed").css("visibility","visible");		
	}
}

var distinctCss = new Array();

var initShowPopin = function() {
    $(".showPopin").each(function() {		 
		 var theRel = $(this).attr("rel");
		 var cssPopin = theRel.split("|")[0];
		 var w =  theRel.split("|")[1];
		 var absolute = theRel.split("|")[2];
	 	 var isNew = true;
		 var nb = distinctCss.length;
		 for(var i=0; i<nb; i++){
			if(distinctCss[i]==cssPopin){
				isNew = false;
				break;
			}
		 }
		 if(isNew){
			 var p = $(".GO_header");
		    var position = p.position();
		    var left = position.left + 470 - (w/2);
		    var url = $(this).attr("href");
			 $("."+cssPopin).newWindow({
		        windowTitle: "",
		        minimizeIcon: "",
		        maximizeIcon: "",
		        closeIcon: "",
		        ajaxURL: url,
		        posx: left,
		        posy: 100,
		        width: w,
		        height: 2000,
		        onAjaxContentLoaded: function() {
		            $("#mask").show();
		            $("#mask").animate({ opacity: 0.6 }, 1000);
		            initClosePopin();
						hideActiveObjects();
						if(absolute=="scroll") $(".window-container").css("position", "absolute");
		        }
		    });
			 distinctCss[nb] = cssPopin;
		 }
	});
}

var initSubmitPopin = function() {
	var p = $(".GO_header");
   var position = p.position();
   var left = position.left + 470 - 310;
	$("#bookingConfirm").newWindow({
        windowTitle: "",
        minimizeIcon: "",
        maximizeIcon: "",
        closeIcon: "",
        ajaxURL: "popin-confirmation.html",
        posx: left,
        posy: 100,
        width: 620,
        height: 580,
        onAjaxContentLoaded: function() {
            $("#mask").show();
            $("#mask").animate({ opacity: 0.6 }, 1000);
            initClosePopin();
				hideActiveObjects();
				initPopinInProgress();
        }
    });
}

var initPopinInProgress = function() {
	var p = $(".GO_header");
   var position = p.position();
   var left = position.left + 470 - 260;
	$("#popinInProgress").newWindow({
        windowTitle: "",
        minimizeIcon: "",
        maximizeIcon: "",
        closeIcon: "",
        ajaxURL: "attente-reservation.html",
        posx: left,
        posy: 100,
        width: 520,
        height: 280,
        onAjaxContentLoaded: function() {
            $("#popinInProgress").parents(".window-container").fadeOut();
        }
    });
}

var initShowPopinInsurances = function() {
	var p = $(".GO_header");
   var position = p.position();
   var left = position.left + 470 - 330;
	$(".showPopinInsurances").newWindow({
        windowTitle: "",
        minimizeIcon: "",
        maximizeIcon: "",
        closeIcon: "",
        ajaxURL: "popin-comparatif-assurances.html",
        posx: 250,
        posy: 50,
        width: 660,
        height: 2000,
        onAjaxContentLoaded: function() {
            $("#mask").show();
            $("#mask").animate({ opacity: 0.6 }, 1000);
            initClosePopin();
				$(".showPopinInsurances").attr("checked", "checked");
				hideActiveObjects();
				$(".window-container").css("position", "absolute");
        }
    });
}


var showHideLayer = function(id, alink, labelMore, labelLess) {
    $("#" + id).toggle("slow");
    $(alink).toggleClass("lessInfos");
    $(alink).toggleClass("moreInfos");
    if ($(alink).hasClass("moreInfos")) {   
		  $(alink).text(labelMore);
    }
    else {
        $(alink).text(labelLess);
    }
}


var initSelectStation = function(){
	$(".listStations input").each(function(){
		if($(this).is(":checked")) $(this).parent("li").addClass("stationActive");
	});
	$(".listStations input").click(function(){
		$(this).parents("ul").find(".stationActive").removeClass("stationActive");
		$(this).parent("li").addClass("stationActive");
	});
}

var initShowOtherChoice = function() {
	$(".showOtherChoice").click(function() {
		var infos = $(this).attr("rel");
		var id = infos.split("|")[0];
		if($(id).size()>0){
			if($(id).hasClass("divOpened")){
				$(id).slideUp();
				$(id).removeClass("divOpened");
				$(this).text(infos.split("|")[1]);
				var css = infos.split("|")[3];
				if(css!=undefined){$(this).removeClass(css);}
			}
			else{
				$(id).slideDown();
				$(id).addClass("divOpened");
				$(this).text(infos.split("|")[2]);
				var css = infos.split("|")[3];
				if(css!=undefined){$(this).addClass(css);}
			}
		}
		return false;
   });
}

var initShowMoreFlights = function() {
	$(".showMoreFlights").click(function() {
		var parent = $(this).parents(".resultItem");
      if(parent.find(".moreFlights").size()>0){
		  	if(parent.find(".moreFlights").hasClass("divOpened")){
				parent.find(".moreFlights").hide();
				parent.find(".moreFlights").removeClass("divOpened");
				parent.find(".showMoreFlights").text("Plus d’horaires au même prix");
				parent.find(".tableFlight").fadeIn("slow");
			}
			else{
				parent.find(".moreFlights").fadeIn("slow");
				parent.find(".moreFlights").addClass("divOpened");
				parent.find(".showMoreFlights").text("Réduire les horaires au même prix");
				parent.find(".tableFlight").hide();
			}
	  }
     return false;
   });
}

var highlightNav = function(id) {
    $("#" + id).addClass("currentNav");
}

var highlightTab = function(id) {
    $("#" + id).addClass("tabItemActive");
}

var highlightButton = function(id) {
    $("#" + id).addClass("btActive");
}

var moveto = function(obj1, chars, obj2){
	if($(obj1).val().length == chars)
	  $(obj2).focus();
}

var initCardNumber = function(){
	$("#tb_cardNb1").keypress(function(){
		moveto(this, 4, $("#tb_cardNb2"));
	});
	$("#tb_cardNb2").keypress(function(){
		moveto(this, 4, $("#tb_cardNb3"));
	});
	$("#tb_cardNb3").keypress(function(){
		moveto(this, 4, $("#tb_cardNb4"));
	});
}

var initAll = function() {
	if ($(".contentLayer").is("div")) {
        $(".contentLayer").hide();
        $(".contentLayer").css("visibility", "visible");
    }
    if ($(".marquee").is("div")) {
        $(".textMarquee").css("left", $(".textMarquee").parent().width() + 10 + "px");
        marquee();
    }
    if ($(".showSelectOptions").is("input")) showSelectOptions();
    if ($(".fieldToEmpty").is("input")) emptyField();
    initPopins();
	 if ($(".showMoreFlights").is("a")) initShowMoreFlights();
	 if ($("#tb_cardNb1").is("input")) initCardNumber();
	 if ($(".showOtherChoice").is("a")) initShowOtherChoice();
	if ($(".listStations input").is("input")) initSelectStation();
}

$(document).ready(function() {
   initAll();
});