﻿/**
* @class Location logic
* depends on jQuery, jQuery-cookie, and sunweb static "logic" object
*/
var locDetails = function($) {

    /**
    * @namespace Private methods and properties
    */
    var priv = {

        /**
        * @private
        */
        countryId: null,

        /**
        * @private
        */
        regionId: null,

        /**
        * @private
        */
        randomBoxId: null,

        /**
        * @private
        */
        locationType: null,

        /**
        * @private
        */
        regionCode: null,

        /**
        * @private
        */
        countryCode: null,

        openLightbox: function(id, options) {
            var lightbox = Lightbox.CreateCached(id, options);
            lightbox.Show();
        },

        locationPageReady: function() {
            priv.regionId = $("#regionId").val();
            priv.randomBoxId = $("#randomBoxId").attr("name");
            priv.locationType = $("#locationType").val().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;
            });

            $("#result-list>li").hover(function() {
                $(this).addClass('hover');
            }, function() {
                $(this).removeClass('hover');
            });
        },

        destinationsPageReady: function() {
            $("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');
            });
        },

        run: function() {
            location.SetImage();
        },

        bindRegionLocations: function() {
            $("li.region").hover(
                function() {
                    var $countryPopup = $("div.country-popup", this);
                    var $borderHider = $("div.hide-border", this);

                    var $liLink = $(this);
                    $liLink.addClass("country-selected");

                    var $locationBlock = $("#location-block", this);
                    $locationBlock.css({ 'height': 'auto', 'border': 'none', 'padding-top': '20px' })
                    var $randombox = $("div.randombox", this); ;
                    $randombox.css({ 'height': 'auto' });

                    $countryPopup.show();
                    $borderHider.show();

                    if (!$countryPopup.hasClass('initialized')) {
                        $countryPopup.load(Resource.GetText('path_prefix') + '/_html/location-block.aspx?theme=region&id=' + $(this).find('a').attr('rel') + ' #location-block', null, function() {
                            var $locationBlock = $("#location-block", this);
                            $locationBlock.css({ 'border': 'none', 'padding-top': '20px' });
                            $locationBlock.height($locationBlock.get(0).offsetHeight + 20);
                            var $removeButton = $('a.remove');
                            $removeButton.css({ 'display': 'block' });
                            $("a.remove").click(
                                    function() {
                                        $liLink.removeClass('country-selected');
                                        $countryPopup.hide();
                                        $borderHider.hide();
                                        return false;
                                    }
                                )
                            $countryPopup.addClass('initialized');

                        });
                    }
                    return false;
                },
                function() {
                    var $countryPopup = $("div.country-popup", this);
                    var $borderHider = $("div.hide-border", this);
                    $(this).removeClass("country-selected");
                    $countryPopup.hide();
                    $borderHider.hide();

                    return false;
                }
            )
        }
    };

    var location = {
        SetImage: function() {
            Log.Info("setting " + priv.locationType + " 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 + ")");
            }

            var source = Resource.GetText('path_prefix') + "/images/";

            if (priv.locationType == "country")
                source += "countries/result-properties-bg-" + priv.countryCode + ".jpg";
            else
                source += "region/block-bg-" + priv.regionCode + ".jpg";

            image.src = source;

            Log.Info("setting " + priv.locationType + " Image: " + image.src);
        }
    }

    /**
    * @scope locDetails
    */
    return {

        showLocationReadMore: function() {
            $('#location-read-more').show();
            priv.openLightbox('location-read-more-lightbox', {
                width: "623px",
                container: document.getElementById('location-read-more-lightbox')
            });
        },

        showPracticalInformation: function(url) {
            var id = 'pPracticalInfo';
            var config = null;
            if ($('#' + id).html() == '') {
                config = {
                    container: document.getElementById(id),
                    width: '623px',
                    contentUrl: url + " #practical-information-popup"
                };
            }
            priv.openLightbox(id, config);
        },

        showDestinationsPopup: function(url) {
            var id = 'pDestinations';
            var config = {
                contentUrl: url + " #result-destination-popup",
                container: document.getElementById(id),
                width: '250px'
            };
            priv.openLightbox(url, config);
        },

        /**
        * should be triggered on page ready
        */
        OnReady: function() {
            //get the locationType and Id
            priv.countryId = $("#countryId").attr("name");
            priv.countryCode = $("#countryCode").attr("name");
            priv.bindRegionLocations();

            /*
            * location page OnReady functions
            */
            if (priv.countryId != null) {
                priv.regionId = $("#regionId").attr("name");
                priv.regionCode = $("#regionCode").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();
            }
            /*
            * 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();
            });

            switch ($('body').attr('class')) {
                case 'destination-location':
                    priv.locationPageReady();
                    break;
                case 'destination-overview':
                    priv.destinationsPageReady();
                    break;
            }

            $('#location-ribbon div.pricetag span.price').bind("click", function() {
                location.href = $('span.lmonth', $(this).parent()).text();
            });

            //copy the ups-texts to the lightbox list
            $("#usp-texts-lightbox").html($("#usp-texts").html());
        }
    };
} (jQuery);
