﻿/**
 * Location logic
 * depends on jQuery, jQuery-cookie, and sunweb static "logic" object
 */
var locDetails = function($){
    var priv = {
        countryId  : null,
        regionId  : null,
        randomBoxId : null,
        locationType    : null,
        
        /*
        * Determines what set of functions to run, depending on the locationType
        * @param {string} functionName, name of the function to run
        */
        run : function(functionName){
            logic.writeDebug("evaluating: " + priv.locationType + "." + functionName);
            eval(priv.locationType + "." + functionName);
        }
    };
    
    var country = {
        //sets the image for the current country
        setImage : function(){            
            //wait till we have actually loaded the image
            //this way if there is no image, the default image shows
            var image = new Image();
            image.onload = function(){
                $("#location-ribbon").css("background-image", "url(" + image.src + ")");
            }
            image.src = resources.skin_path + "images/countries/result-properties-bg-" + priv.countryId + ".jpg";
            logic.writeDebug("setting country Image: " + image.src);
            
            /*var imageRandom = new Image();
            imageRandom.onload = function(){
                $("#randombox-content").css("background-image", "url(" + imageRandom.src + ")");
            }
            imageRandom.src = resources.skin_path + "images/region/block-bg-" + priv.randomBoxId + ".jpg";*/
        }
    };
    
    var city = {
        setImage : function(){
            logic.writeDebug("setting region Image");
            
            //wait till we have actually loaded the image
            //this way if there is no image, the default image shows
            var image = new Image();
            image.onload = function(){
                $("#location-ribbon").css("background-image", "url(" + image.src + ")");
            }
            image.src = resources.skin_path + "images/countries/result-properties-bg-" + priv.regionId + ".jpg";
        }
    };
    
    var region = {
        setImage : function(){
            logic.writeDebug("setting region Image");
            
            //wait till we have actually loaded the image
            //this way if there is no image, the default image shows
            var image = new Image();
            image.onload = function(){
                $("#location-ribbon").css("background-image", "url(" + image.src + ")");
            }
            
            image.src = resources.skin_path + "images/countries/result-properties-bg-" + priv.regionId + ".jpg";
        }
    };
    
    return {
        
        showLocationReadMore : function () {
	        $('#location-read-more').show();
	        logic.resizePopup();
	        logic.showPopup('location-read-more-lightbox', true, 623, 0);
	    },
	    
	    showPracticalInformation : function (countryId) {        
	        if($('#pPracticalInfo').html() == ''){
	            $('#pPracticalInfo').load(resources.path_prefix + '/html/popups/practicalpopup.aspx?cId='+countryId+' #popup', null, function(){
                    locDetails.showPracticalInformation(countryId);
                });
            } else {
	            logic.showPopup('pPracticalInfo', true, 623, 0);
	            logic.resizePopup();
	        }
	    },
	    
	    showDestinationsPopup : function (countryId, regionId, s) {	        
	        if($('#pDestinations').html() == '' || $('#pDestinations .hidden').text() != ''+countryId){
	            var url = resources.path_prefix + '/html/popups/result-destination.aspx?id='+countryId;   
	         if( regionId )
	            url += '&rid='+ regionId;
	         if( s )
	            url += '&s=1'; 
	            
                $('#pDestinations').load( url +' #popup', null, function(){
                    locDetails.showDestinationsPopup(countryId);
                    $('#pDestinations').removeClass('initialized');
                });
            } else {
	            logic.showPopup('pDestinations', true, 250, 0);
	            logic.resizePopup();
	        }
	    },
	    
	    //should be triggered on page ready
        OnReady : function(){
            //get the locationType and Id
            priv.countryId = $("#countryId").attr("name");
            
            /*
            * location page OnReady functions
            */
            if(priv.countryId != null){
                priv.regionId = $("#regionId").attr("name");
                priv.randomBoxId = $("#randomBoxId").attr("name");
                priv.locationType = $("#locationType").attr("name").toLowerCase();
                
                $('div.bestreviews ul, div.lastminute ul').children("li").hover(
                    function(){$(this).addClass("hover");},
		            function(){$(this).removeClass("hover");}
                ).bind("click",
                    function(){
                        //redirect to the correct page
                        var url = $(this).find('a').attr("href");
                        
                        document.location.href = url;
                        //prevent event bubbling
                        return false;
                    }
                );
                
                //set the locationImage
                priv.run("setImage()");
            } 
            /*
            * destinations page OnReady functions
            */
            else {                
                $("div.destination-block h2").bind("click", function(e){
                  location.href = $('a.image-block', $(this).parent()).attr('href');
                });
                
               
                $('div.destination-block h2').hover(function() {
                    $(this).addClass('destination-hover');
                }, function() {
                    $(this).removeClass('destination-hover');
                });
            }
            
            $('#location-ribbon div.pricetag span.price').bind("click", function(){
			    location.href = $('span.lmonth', $(this).parent()).text();
            });

        }
        
    };
}(jQuery);