﻿/**
 * 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 = {
        $fadingImages: null,
        fadeTime: 1500,
        fadePauseTime: 5000,

        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() {
            $('ul.promos').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;
                }
            );
        },

        bindSearchBoxEvents: function() {
            // Search button
            $('#homesearch').bind("focus",
                function() {
                	if ($('#homesearch').val() == Resource.GetText('FOR_EXAMPLE_SHORT')) {
                		$('#homesearch').val('');
                }
                }
            ).bind("keydown",
                function(evt) {
                    if (evt.keyCode == 13) {
                        priv.inputSubmit();
                    }
                }
            );

            $('#homesearchbutton').bind("click",
                function() {
                    priv.inputSubmit();
                }
            );

            $('#form-search a.btn-blue').bind('click', function() {
                    priv.inputSubmit();
            });

			// autocomplete functionality
			$('#homesearch').autocomplete(Resource.GetText('path_prefix') + "/js/ajax/get_searchsuggestions.ashx", {
				selectFirst: false,
				delay: 10,
				width: 209
			}).result(function(event, item) {
				priv.inputSubmit();
			});
        },

        inputSubmit: function() {
            if ($('#homesearch').val() != null && $('#homesearch').val() != "") {
				location.href = Resource.GetText('searchpage') + "?searchtext=" + encodeURIComponent($('#homesearch').val());
            }
        },

        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
                });
            });
        },

        bindBrochureBlock: function() {
            $('#brochure-block').bind('click', function() {
                location.href = Resource.GetText('path_prefix') + '/brochure';
            }).hover(
                function() {
                    $(this).css('text-decoration', 'underline');
                },
                function() {
                    $(this).css('text-decoration', 'none');
                }
            );
        }
    };

    /** @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();
            priv.bindBrochureBlock();

            priv.$fadingImages = $('div.fading-images img');
            if (priv.$fadingImages.length > 1) {
                for (var k = 0; k < (priv.$fadingImages.length); k++) {
                    if (k != (priv.$fadingImages.length - 2)) {
                        $(priv.$fadingImages[k]).hide();
                    }
                    if (k == (priv.$fadingImages.length - 1)) {
                        setTimeout('HomeMain.FadeImage()', priv.fadePauseTime);
                    }
                }
            }
        },

        FadeImage: function() {
            priv.$fadingImages = $('div.fading-images img');
            $(priv.$fadingImages[priv.$fadingImages.length - 1]).fadeIn(priv.fadeTime, function() {
                $(priv.$fadingImages[priv.$fadingImages.length - 2]).hide();
                $('div.fading-images').append($(priv.$fadingImages[0]).parent());

                setTimeout('HomeMain.FadeImage()', priv.fadePauseTime);
            });
        }
    };
} (jQuery);
