/*  #####################################################
**  Domain:    www.tourismus.saarland.de
**  Datei:     offersearch.js
**  Funktion:  exklusiver JavaScriptCode für Angebotssuchseite

**  Erstellt:  06.07.2009

**  Autor:     Axel Güldner
**  copyright: infomax websolutions GmbH
##################################################### */

$(document).ready(function(){

    //Auf- und Zufahren der Formularbereiche
    $(".offerSearch").find("h3.legend").click(function(){    	
        $(this).children("span").toggleClass("active");        
        $(this).parent().children("span.fieldset").slideToggle();
    });


    //Datepicker für Quartiersuche
    Date.format = 'dd.mm.yyyy';
    $("#offerSearch1").datePicker({saarlandstyle:imxSaarlandstyle});
    $("#offerSearch1").dpSetStartDate(currentCalenderDate);
    $("#offerSearch1").dpSetSelected(currentOfferDate[1]);
    $("#offerSearch2").datePicker({saarlandstyle:imxSaarlandstyle});
    $("#offerSearch2").dpSetStartDate(currentOfferDate[0]);
    $("#offerSearch2").dpSetSelected(currentOfferDate[2]);
    
    $("#offerBooking5").datePicker({saarlandstyle:imxSaarlandstyle, horizontalOffset:"-150px"});
    $("#offerBooking5").dpSetStartDate(currentCalenderDate);
    $("#offerBooking5").dpSetSelected(currentOfferDate[1]);
    $("#offerBooking6").datePicker({saarlandstyle:imxSaarlandstyle, horizontalOffset:"-150px"});
    $("#offerBooking6").dpSetStartDate(currentOfferDate[0]);
    $("#offerBooking6").dpSetSelected(currentOfferDate[2]);

    //Sicherstellen das für Abreise nur ein Datum höher als Anreise gewählt werden kann
    $(".kontextBlockLeft").find("#offerSearch1").change(function(){
        $(this).compareDates("#offerSearch2");
        //Werte aus linkem Kontextformular ins rechte übernehmen
        $(".kontextBlock").find("#offerBooking5").val($(this).val());
        $(this).compareDates("#offerBooking6");
    });
    $(".kontextBlockLeft").find("#offerSearch2").change(function(){
        //Werte aus linkem Kontextformular ins rechte übernehmen
        $(".kontextBlock").find("#offerBooking6").val($(this).val());
    });
    $(".kontextBlock").find("#offerBooking5").live("change", function(){
        $(this).compareDates("#offerBooking6");
    });


    $("form.offerSearch").find("input.checkbox.main").click(function(){
        if($(this).attr("checked")){
            $(this).parent().find("input.checkbox:not(.main)").attr("checked","checked");
        }
    });

    $("form.offerSearch").find("input.checkbox:not(.main)").click(function(){
        if(!$(this).attr("checked")){
            $(this).parent().parent().parent().find("input.checkbox.main").attr("checked",false);
        }
    });
    
    //AjaxRequest für Buchungsrelevante Abfragen
    function offerBookAjaxRequest(params){		
		var requestURL = globalLink+"/"+lang+"/index.html?action=ajax_imxTools_angebotssuche";
		$.ajax({
			type: "POST",
			url: requestURL,
			dataType: "html",
			data: params,
			error: function (XMLHttpRequest, textStatus, errorThrown){
			}
		});		
	}

    //Einblenden und anhackeln der Checkboxen im Bestellformular nach Klick auf Listenelemente
    //Außerdem wird ein AjaxRequest abgesetzt um die Liste aktuell gewählter Angebote in die Session zu schreiben
    $("div.hauptbild .offerCheckBox input:checkbox").live("click", function(){
    	location="#anchorContentarea";
    	
        var offerID = $(this).attr("id").substr("10");
        
        var params;
		params = "formAction=checkOffers";
		params += "&offerID="+offerID;
		
        if($(this).attr("checked")){
             $(this).parent().addClass("active");
             $(".offerBook").find(".offerChecks").find("input#offer"+offerID).attr("checked","checked").parent().slideDown();
             params += "&state=true";
        }else{
             $(this).parent().removeClass("active");
             $(".offerBook").find(".offerChecks").find("input#offer"+offerID).attr("checked",false).parent().slideUp();
             params += "&state=false";
        }
        if($(".offerBook").find("input:checked").length > 0)$(".offerBook").find(".offerChecks").children("li.text").slideUp();
        else $(".offerBook").find(".offerChecks").children("li.text").slideDown();
        
        //Ajax-Request
        offerBookAjaxRequest(params);        
    });
        
    //Ajax-Request um ausgewähltes Angebot, nach Klick auf Button in Detailansicht, in Liste aktuell gewählter Angebote in der Session zu schreiben
    $(".aufmacherTop a.formbutton").live("click", function(){
    	location="#anchorContentarea";
    	
        var offerID = $(this).attr("rel");
        $(".offerBook").find(".offerChecks").find("input#offer"+offerID).attr("checked","checked").parent().slideDown();
        $(".offerBook").find(".offerChecks").children("li.text").slideUp();
        
        var params;
		params = "formAction=checkOffers";
		params += "&offerID="+offerID;
		params += "&state=true";        
        
        //Ajax-Request
        offerBookAjaxRequest(params);
    });

    //Einblenden und anhackeln der Listenelementen nach Klick auf Checkboxen im Bestellformular
    //Außerdem wird ein AjaxRequest abgesetzt um die Liste aktuell gewählter Angebote in die Session zu schreiben
    $(".offerBook").find(".offerChecks").find("input:checkbox").live("click", function(){
        var offerID = $(this).attr("id").substr("5");
        
        var params;
		params = "formAction=checkOffers";
		params += "&offerID="+offerID;
		
        if($(this).attr("checked")){
            $("div.hauptbild .offerCheckBox input#offerCheck"+offerID).parent().addClass("active");
            $("div.hauptbild .offerCheckBox input#offerCheck"+offerID).attr("checked","checked");
            params += "&state=true";
        }else{
            $("div.hauptbild .offerCheckBox input#offerCheck"+offerID).parent().removeClass("active");
            $("div.hauptbild .offerCheckBox input#offerCheck"+offerID).attr("checked",false);
            $(this).parent().slideUp();
            params += "&state=false";
        }
        if($(".offerBook").find("input:checked").length > 0)$(".offerBook").find(".offerChecks").children("li.text").slideUp();
        else $(".offerBook").find(".offerChecks").children("li.text").slideDown();
        
        //Ajax-Request
        offerBookAjaxRequest(params);
    });    
});

$(window).load(function(){

});

