// JavaScript Document
/*              DEBUGG                 */
function trace(str){
	var input = $('#UI_debugg .output').html();
	var output = input + '<p>' + str + '</p>';
	
	$('#UI_debugg .output').html(output);
	$('#UI_debugg').show();
}


/*              WINDOWS                 */
function addWindow(title){
    if($("#UI_window").length > 0){
        $("#UI_window").remove();
    }
    $("#UI").after('<div id="UI_window"><div class="back"></div><div class="window"><h2>'+title+'</h2><div class="close"><span>Fermer</span></div><div class="area"></div></div></div>');
    $("#UI_window .close").hover(
        function(){
            $(this).addClass("hover");
        },
        function(){
            $(this).removeClass("hover");
        }
    );
    $("#UI_window .close").click(function(){
        removeWindow();
    });
}
function removeWindow(){
	var newY = $(window).height() + 500;
    $("#UI_window .window").animate({
		top: newY},
		800,
		function(){
		    $("#UI_window").remove();
		}
	);
}
function setWindow(){
	
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	var newX = $(window).width()/2 - $("#UI_window .window").width()/2;
	var newY = $(window).height()/2 - $("#UI_window .window").height()/2 + scrOfY;
	$("#UI_window .window").animate({top: newY}, 600);
	$("#UI_window .window").css({left: newX, top: newY});
	$("#UI_window").width($(document).width());
	$("#UI_window").height($(document).height());
	
}

/*              COMPONENTS                 */
function setOgone(){
    $('#paymentForm').submit();
}
function setPayment3x(){
    $('#payment3xForm').submit();
}
function payment3x_check(){
	var n = $("#payment3x_accept input:checked").length;
	if(n == 2){
		$("#payment3x_conditions").hide();
		$("#payment3x_form").show();
	}else{
		$("#payment3x_conditions .alert").show();
	}
}
function homeRedirect(){
	window.location = "http://www2.mondial.com.fr/FR/reserver-un-vol/";
}
function urlRedirect(){
	window.location = "http://www2.mondial.com.fr/FR/reserver-un-vol/";
}

function createTabs(target){
    if(target == undefined ){
        target = '';
    }else{
		target = '#'+target+' ';
    }
	$(target+"div.tabs").each(function(){
		$(this).children("div.panel:eq(0)").show();
		$(this).children("div.panel:eq(0)").nextAll().hide();
	});

	$(target+"div.tabs ul.tab").children("li:eq(0)").addClass("active");

	$(target+"div.tabs ul.tab > li").click(function(i){

		$(this).parent().children("li").removeClass("active");
		$(this).addClass("active");
		$(this).parent().parent().children("div.panel").hide();

		var n = $(this).parent().children("li").index(this);

		$(this).parent().parent().children("div.panel").eq(n).show();

	});
}
function selectTab(n){
	$("#UI_content div.tabs ul.tab li").removeClass("active");
	$("#UI_content div.tabs div.panel").hide();

    $("#UI_content div.tabs ul.tab li").eq(n).addClass("active");
	$("#UI_content div.tabs div.panel").eq(n).show();
}

/*              POP-UPS                 */
function signin(){
    addWindow("Identification");
    $("#UI_window .area").load(root+"popup_signin.php", function(){
        setWindow();
    });
}
function signout(){
    addWindow("Déconnexion");
    $("#UI_window .area").load(root+"popup_signout.php", function(){
        setWindow();
    });
}
function forgot(){
    addWindow("Mot de passe oublié");
    $("#UI_window .area").load(root+"popup_forgot.php", function(){
        setWindow();
    });
}
function detail(type, id){
	var popup_title,popup_toload;
	switch(type){
	    case 'hotel':
			popup_title = "Détail de votre hôtel ("+id+")";
			popup_toload = root+"popup_hotel.php";
	    break;
	    case 'flight':
			popup_title = "Détail de votre vol";// ("+id+")";
			popup_toload = root+"popup_flight.php";
	    break;
	    default:
	}
    addWindow(popup_title);
    
	$.ajax({
		type: "GET",
		url: popup_toload,
		data: {IdFlight:id},
		beforeSend: function(){
			$.modal(_waiting,{containerCss:{
				backgroundColor:"#ef8200",
				borderColor:"#ef8200",
				height:100,
				padding:10,
				width:230
			}});
		},
		complete: function(){
	        $.modal.close();
		},
		success: function(msg){
			$("#UI_window .area").html(msg);
			
			createTabs('UI_window');
			
			
	        setWindow();
		    $("#UI_window .back").click(function(){
		        removeWindow();
		    });
		}
	});
}
function showPromo(target){
	$.modal(_waiting,{onShow:function(){
		$('#'+target).submit();
	}, containerCss:{
		backgroundColor:"#ef8200",
		borderColor:"#ef8200",
		height:100,
		padding:10,
		width:230
	}});
	
}

/*              MODULES                 */

function trim (myString) 
{ 
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'') 
}

function trace(msg){
	$('#UI_debugg .output').append('<p>'+msg+'</p>');
	if(!$.browser.msie){
		//console.log(msg);
	}else{
		if(parseInt($.browser.version) > 7){
			//console.log(msg);
		}		
	}
}
function init(){
	$("#UI_context_blog h2").click(function(){
		$("#UI_context_blog div.area").slideUp('fast');
		if($(this).hasClass("open")){
	  		$("#UI_context_blog h2").removeClass("open");
    		$(this).removeClass("open");
	   		$(this).next().slideUp('fast');
    	}else{
	  		$("#UI_context_blog h2").removeClass("open");
    		$(this).addClass("open");
	   		$(this).next().slideDown('fast');
        }
	});
	$("#UI_context_nav h2").click(function(){
		$("#UI_context_nav div.area").slideUp('fast');
		if($(this).next().css("display") == "none"){
 	  		$("#UI_context_nav h2").removeClass("open");
   			$(this).removeClass("open");
	   		$(this).next().slideUp('fast');
    	}else{
 	  		$("#UI_context_nav h2").removeClass("open");
    		$(this).addClass("open");
 	   		$(this).next().slideDown('fast');
		}
	});
	
	$('#UI_catalog .list .item h3 a').each(function(){
	    $(this).attr('href', 'javascript:void(0)');
	});
	

	$('#UI .field_select dl, #UI .field_select-error dl, #UI .field_select_sexe dl, #UI .field_select_sexe-error dl, #UI .field_select_pax dl, #UI .field_select_pax-error dl, #UI .field_select_phone dl, #UI .field_select_phone-error dl').hide();
	$('#UI .field_select input.custom, #UI .field_select-error input.custom, #UI .field_select_sexe input.custom, #UI .field_select_sexe-error input.custom, #UI .field_select_pax input.custom, #UI .field_select_pax-error input.custom, #UI .field_select_phone input.custom, #UI .field_select_phone-error input.custom').click(function(){
		var field = $(this);
		$(this).next().fadeIn();
		$(this).next().children('dt').click(function(){
			var value = $(this).text();
			field.prev().val(value);
			field.val(value);
			$(this).parent().fadeOut();
		});
		$('.field_select, .field_select-error, .field_select_sexe, .field_select_sexe-error, .field_select_pax, .field_select_pax-error, .field_select_phone, .field_select_phone-error').bind("mouseleave",function(){
			$(this).children('dl').fadeOut();
		});
	});
	$('#UI .field_select input.custom, #UI .field_select-error input.custom, #UI .field_select_sexe input.custom, #UI .field_select_sexe-error input.custom, #UI .field_select_pax input.custom, #UI .field_select_pax-error input.custom, #UI .field_select_phone input.custom, #UI .field_select_phone-error input.custom').focus(function(){
		var field = $(this);
		$(this).next().fadeIn();
		$(this).next().children('dt').click(function(){
			var value = $(this).text();
			field.prev().val(value);
			field.val(value);
			$(this).parent().fadeOut();
		});
		$('.field_select, .field_select-error, .field_select_sexe, .field_select_sexe-error, .field_select_pax, .field_select_pax-error, .field_select_phone, .field_select_phone-error').bind("mouseleave",function(){
			$(this).children('dl').fadeOut();
		});
	});
	$('#UI .field_select input.custom, #UI .field_select-error input.custom, #UI .field_select_sexe input.custom, #UI .field_select_sexe-error input.custom, #UI .field_select_pax input.custom, #UI .field_select_pax-error input.custom, #UI .field_select_phone input.custom, #UI .field_select_phone-error input.custom').blur(function(){
		$(this).next('dl').fadeOut();
		trace('BLUR sur '+$(this).attr('id'));
	});

	$('#UI .field_select dt, #UI .field_select-error dt, #UI .field_select_sexe dt, #UI .field_select_sexe-error dt, #UI .field_select_pax dt, #UI .field_select_pax-error dt, #UI .field_select_phone dt, #UI .field_select_phone-error dt').hover(function(){
	    $(this).addClass('current');
	}, function(){
	    $(this).removeClass('current');
	});
}

/*              ONREADY                 */
$(document).ready(function(){
    init();
    /*
	$("#UI_mypage h2").click(function(){
		if($(this).next().css("display") == "none"){
    		$(this).removeClass("open");
    	}else{
    		$(this).addClass("open");
        }
   		$(this).next().toggle('fast');

	});
	*/

	$("#UI_language_command").click(function(){
        $("#UI_language_choice").toggle('slow');
    });
    $("#UI_language_choice").hide();
    
    $("#UI_nav .area").hide();
    
    
    $("#UI_context_blog .calendar td.event").hover(
        function(){
            $(this).addClass("hover");
        },
        function(){
            $(this).removeClass("hover");
        }
    );
    $("#UI_nav .title").hover(
        function(){
            $(this).parent().children('div.area').css('min-width', $(this).parent().width());
            $(this).parent().children('div.area').show();
        },
        function(){
            $(this).parent().children('div.area').hide();
        }
    );
    $("#UI_nav li").hover(
        function(){
            $(this).addClass("over");
        },
        function(){
            $(this).removeClass("over");
        }
    );
    $("#UI_offers .element").hover(
        function(){
            $(this).addClass("over");
        },
        function(){
            $(this).removeClass("over");
        }
    );
    createTabs();
    
    $('.autofill').bind("focus", function(){
        $(this).data('default', $(this).val());
        $(this).val('');
	});
    $('.autofill').bind("focusout", function(){
        if($(this).val() != "" && $(this).val() != $(this).data('default')){
		}else{
	        $(this).val($(this).data('default'));
		}
	});
	$("div#moteurFlight a#link1").click(function() {
		$("#engineBloc").hide();
		$("div#errors").hide();
		$("#engineWait").show();
		$("div#moteurFlight dl > dt.current").removeClass("current");
		$(this).parents("dt").addClass("current");
		$('#TypeVol').val('1');
		updateEngine(1);
	});

	$("div#moteurFlight a#link2").click(function() {
		$("#engineBloc").hide();
		$("div#errors").hide();
		$("#engineWait").show();
		$("div#moteurFlight dl > dt.current").removeClass("current");
		$(this).parents("dt").addClass("current");
		$('#TypeVol').val('0');
		updateEngine(0);
	});

	$("#searchMoteur button").click(function(){
		showWaiting("Nous recherchons les offres");
		$("#searchMoteur").submit();
	});
		
});
