// JavaScript Document
/*              WINDOWS                 */
function addWindow(title){
    if($("#UI_window").length > 0){
        $("#UI_window").remove();
    }
    $("#UI").after('<div id="UI_window"><div class="window"><div class="top"></div><div class="middle"><h2>'+title+'</h2><span class="close"><a href="javascript:removeWindow()">Fermer</a></span><div class="area"></div></div><div class="bottom"></div></div></div>');
    $("#UI_window .window").hide();
    
    $('#UI_window .close').click(function(){
        removeWindow();
	});
    
}
function removeWindow(){
    $("#UI_window").remove();
}
function setWindow(target){

	if(!target){
	    var target = 'window';
	}

	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 ."+target).width()/2;
	var newY = $(window).height()/2 - $("#UI_window ."+target).height()/2 + scrOfY;
	$("#UI_window ."+target).css({left: newX, top: newY});
	$("#UI_window").width($(document).width());
	$("#UI_window").height($(document).height());

    $("#UI_window ."+target).show();

}

/*              COMPONENTS                 */
function createTabs(){

	$("#UI_area div.tabs").each(function(){
		$(this).children("div.panel:eq(0)").show();
		$(this).children("div.panel:eq(0)").nextAll().hide();
	});

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

	$("#UI_area 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();

		if(n == 0){
		    $("#UI_mea").slideDown(300);
		}else{
		    $("#UI_mea").slideUp(300);
		}
	});
}
function selectTab(n){
	$("#UI_area div.tabs ul.tab li").removeClass("active");
	$("#UI_area div.tabs div.panel").hide();

    $("#UI_area div.tabs ul.tab li").eq(n).addClass("active");
	$("#UI_area div.tabs div.panel").eq(n).show();
	
	if(n == 0){
	    $("#UI_mea").slideDown(300);
	}else{
	    $("#UI_mea").slideUp(300);
	}
}

/*              POP-UPS                 */
function mentions(url, title){
    window.scroll(0,0);
    addWindow(title);
    
    $("#UI_window .area").load(url, function(){
        setWindow();
    });
}
function credits(url, title){
    window.scroll(0,0);
    addWindow(title);
    $("#UI_window .area").load(url, function(){
        setWindow();
    });
}

function engine(url){
    window.scroll(0,0);
    addWindow("Demande de devis");
    $("#UI_window .area").load(url, function(){
        //$("#UI_window .area").height(bowHeight)
        setWindow();
        init_engineForm();
    });
}

function zoomGmaps(title){
    window.scroll(0,0);
    addWindow(title);
    $("#UI_window .window").addClass('gmaps');
    $("#UI_window .gmaps").removeClass('window');
    var id = "UI_gmaps_zoom";
    $("#UI_window .area").html('<div id="'+id+'"></div>');
    setWindow('gmaps');
    loadGmaps_zoom(id);
}

function send_engineForm() {
	var queryString = "";
	inputtexts = $('#engineForm input:text');
	for(i=0 ; i < inputtexts.length ; i++){
        queryString += "&" + inputtexts[i].name + "=" + inputtexts[i].value;
	}
	selects = $('#engineForm select');
	for(i=0 ; i < selects.length ; i++){
        queryString += "&" + selects[i].name + "=" + selects[i].value;
	}
    var dupli = $('#engineForm input:checked').val();
    queryString += "&dupli=" + dupli;
    var comment = $('#engineForm textarea').val();
    queryString += "&comment=" + comment;
    var action = $('#engineForm input:hidden').val();
    queryString += "&action=" + action;
    /*
    */
	$.ajax({
		type: "POST",
		url: "engine.php",
		data: queryString,
		success: function(msg){
    		//var msg = 'Message envoyé';
    		//$('#UI_window .area').hide('fast');
    		//$('#UI_window .area').show('fast');
    		
    		$('#UI_window .area').html(msg);

            init_engineForm();
		}
	})

	return true;
}

function init_engineForm(){
    //$("#UI_window .errorForm").hide();
    
    $("#livraison").datepicker({
		inline: true,
		numberOfMonths: 1,
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		dateFormat: 'dd/mm/yy',
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
		firstDay: 1
    });
    /*
    $("#engineForm").validate(){
		var errors = validator.numberOfInvalids();
		if (errors) {
			var message = 'Veuillez remplir les champs signalés !';
			$("#engineForm .errorForm").html(message);
			$("#engineForm .errorForm").show();
		} else {
			$("#engineForm .errorForm").hide();
		}
		submitHandler: function(form) {
    		send_engineForm();
            //$("#engineForm").submit();
		}
	});
	$("#UI_window .prompt").html("Initialisation du formulaire !");
	*/
}

function redirectTo(url){
	window.location = url;
}

/*              ONREADY                 */
$(document).ready(function(){
    var urlMentions = $("#mentions").attr("href");
    var urlCredits = $("#credits").attr("href");
    var titleMentions = $("#mentions").html();
    var titleCredits = $("#credits").html();

    $("#mentions").attr("href", "javascript:mentions('"+urlMentions+"', '"+titleMentions+"')");
    $("#credits").attr("href", "javascript:credits('"+urlCredits+"', '"+titleCredits+"')");
    
    var link = new Array();
	var elementArr = $('a.link');
    for(var i = 0 ; i < elementArr.length ; i++){
        link[i] = new Array();
		link[i]['label'] = $('a.link').eq(i).html();
		link[i]['url'] = $('a.link').eq(i).attr('href');
        $('a.link').eq(i).after('<button class="customButton" type="button" onclick="redirectTo(\''+link[i]['url']+'\')"><div><span>'+link[i]['label']+'</span></div></button>');
	}
	$('a.link').hide();
	//alert(elementArr.length);
	
	/* -------------- */
	
	$("#JS_tabs dl").eq(0).addClass("current");

	$("#JS_tabs dl").click(function(i){
		var url = $(this).attr('target');
		$(this).parent().children("dl").removeClass("current");
		$(this).addClass("current");
		$("#UI_fbengine").attr("src", url+lng);

	});

});