
jQuery(document).ready(function() {

    jQuery("#accordion").accordion();
 
    jQuery('#accordion > a').click(function(e) {
        jQuery("#selected_destination").text(jQuery(this).attr("rel"));
        jQuery.historyLoad(DSR.city_id()+"||");
        return false;
    });

    jQuery("#stars").change(function() {
        jQuery.historyLoad(DSR.city_id()+"|"+DSR.stars_id()+"|");
    });

    jQuery("#hotels").change(function() {
        jQuery.historyLoad(DSR.history_hash());
    });

    jQuery.historyInit(pageload);
});


    function pageload(hash) {

		if(hash) {
			// restore ajax loaded state
			var params = hash.split('|');
			
            // select destination
            if (params.length > 0){
                jQuery("#selected_destination").text(params[0]);
			    var destination_link = jQuery("a[rel='"+params[0]+"']");
			    jQuery("#accordion").accordion("activate", destination_link.parent().children("a").index(destination_link));
			}
			
			// select stars
			if (params.length > 1){
			    jQuery("#stars :selected").attr("selected","");
			    jQuery("#stars option[value='"+params[1]+"']").attr("selected","selected");
			}
			
			// load hotels
	        if (params.length > 2 &&  params[2] != ""){
			    DSR.refreshHotels(params[2]);
			}
			else{
			    DSR.refreshHotels("");
			}
		}
	}

var DSR = {
    refreshHotels: function(hotel_to_select) {
        DSRService.GetCityHotels(DSR.city_id(), DSR.stars_id() == "" ? 0 : DSR.stars_id(),
        function (result){DSR.onHotelsResult(result, hotel_to_select);});
    },

    onHotelsResult: function(hotels, hotel_to_select) {
        jQuery("#hotels").html("").append("<option value=''>alle</option>");
        for (var i = 0; i < hotels.length; i++) {
            var option = jQuery("<option value='" + hotels[i].Id + "'>" + hotels[i].Name + "</option>");
            $("#hotels").append(option);
        }
        if (hotel_to_select !=""){
            jQuery("#hotels :selected").attr("selected","");
			jQuery("#hotels option[value='"+hotel_to_select+"']").attr("selected","selected");
		    DSRService.GetOffers([{Id:hotel_to_select,Name:"",Stars:0}], DSR.onOffersResult);	
        }
        else
            DSRService.GetOffers(hotels, DSR.onOffersResult);
    },

    onOffersResult: function(offers) {
        jQuery("#offers").html("");       
        for (var i = 0; i < offers.length; i++) {
            var photo = (offers[i].Photo != "") ?  "<div class='photo' style='background-image:url(\"" + offers[i].Photo + "?w=175\")'><a href='"+offers[i].OfferLink+"'></a></div>" : "";
            var offer = jQuery("<div class='offer p'><h2>" + offers[i].Hotel + " </h2><h3 class='title'>" + offers[i].Title + " <span class='price'> " + offers[i].Price + "</span> </h3><div class='content'>"+photo + offers[i].Content + "</div><a class='btns hotel_link' href='" + offers[i].HotelLink + "'><span>mere om hotellet</span></a> <a class='btns offer_link' href='" + offers[i].OfferLink + "'><span>v\u00E6lg</span></a><div class='clear'></div></div>");
            jQuery("#offers").append(offer);
        }
    },
    
    city_id:  function(){ return jQuery("#selected_destination").text() },
    stars_id: function(){ return jQuery("#stars :selected").attr("value"); },
    hotel_id: function(){ return jQuery("#hotels :selected").attr("value"); },
    history_hash:  function(){ return this.city_id() + "|" + this.stars_id() + "|" + this.hotel_id(); }
}
