﻿/**
 * static object that handles page logic
 * @class 
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var HomeMain = function($){

    /**
     * @namespace Private methods and variables
     */
    var priv = {
        
        initializeBannerpool: function () {
            try {
                var ulBanner = new BannerPool({"container" : $("ul.banner").get(0), "debuglogger" : function(msg){Log.Debug(msg);}});
                //make the elements clickable
                $("ul.banner li").bind("click", 
                    function(evt){
                        var newloc = $(this).find("a").attr("href");
                        
                        //deterimine if we need to open a new window
                        if(newloc && $(this).find("a").attr("target") != ""){
                           return;
                        }
                        else if(newloc){
                            document.location.href = newloc;
                        }
                    }
                );
            }
            catch(e){
                Log.Debug("HomeMain : error initializing the bannerpool: " + e.toString());
            }
        },
        
        bindPromoEvents: function () {
            $('#homepagepromo ul.promotion-blocks').children("li").hover(
                function(){$(this).addClass("hover");},
                function(){$(this).removeClass("hover");}
            ).bind("click",
                function(){
                    //redirect to the correct page
                    var url = $(this).find(".promo-acco-name a").attr("href");
                    
                    document.location.href = url;
                    //prevent event bubbling
                    return false;
                }
            );
        },
        
        bindSearchBoxEvents: function () {
            // Search button
            $('#homesearch').bind("focus",
                function(){
                    this.value = '';
                }
            ).bind("keydown",
                function(evt){
                    if(evt.keyCode == 13){
                        priv.inputSubmit();
                    }
                }
            );
                
            $('#homesearchbutton').bind("click",
                function(){
                   priv.inputSubmit();
                }
            );
        },
        
        inputSubmit: function(){
            if($('#homesearch').val() != null && $('#homesearch').val() != ""){
                $('#homesearch').val($('#homesearch').val().replace(Resource.GetText('for_example_short') + ': ',''));
                $('#form-search').get(0).submit();
            }
        },
        
        top10Tooltip: function () {
            //enable tooltips top 10 destinations            
            $('#popularLocations a.plus0').each(function(){
                $(this).jHelperTip({
                    trigger: "hover", 
                    source: "container", 
                    dC: "#tip" + $(this).attr('rel'),
                    autoClose: true,
                    topOff: -20,
                    leftOff: 30    
                });
            });
        }
    };
    
    /** @scope Main */
    return {

        /**
         * Initializes the logic for the current page
         * to be called on $(document).ready
         */
        OnReady: function(){
            priv.initializeBannerpool();
            priv.bindPromoEvents();
            priv.bindSearchBoxEvents();
            priv.top10Tooltip();
        }
    };
}(jQuery);