﻿/**
 * @class Location logic
 * depends on jQuery, jQuery-cookie, and sunweb static "logic" object
 */
var locDetails = function($) {

    /**
    * @namespace Private methods and properties
    */
    var priv = {

        /**
        * String id of a location to show on the map. If null, do not bind map logic.
        * @private
        */
        mapid: null,

        /**
        * @private
        */
        countryId: null,

        /**
        * @private
        */
        regionId: null,

        /**
        * @private
        */
        randomBoxId: null,

        /**
        * @private
        */
        locationType: 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');
            });
        },

        /**
        * Prepare everything related to google map popup.
        * @private
        */
        initMap: function() {
            if (null === priv.mapid) {
                return;
            }

            var $container = $('#mapcontainer').hide();

            $('#showmap').click(function() {
                // Open Google maps with acco map in lightbox in iframe
                $container.find('.content').html("<iframe frameborder=\"0\" style=\"width:575px;height:536px;border:0px;\" src=\"" + Resource.GetText('path_prefix') + "/_html/popups/googlemappopup.aspx?curid=" + $('#mapid').val() + "\" />");
                var lightbox = new Lightbox({
                    width: '650px',
                    container: $container.show(),
                    clone: false
                });
                lightbox.Show();
            });
        }
    };

    /**
    * @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);
        },

        showMapPopup: function() {
            $('#location-map').show();
            priv.openLightbox('location-map-lightbox', {
                width: "623px",
                container: document.getElementById('location-map-lightbox')
            });
        },

        /**
        * should be triggered on page ready
        */
        OnReady: function() {
            priv.countryId = $("#countryId").val();

            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());

            priv.mapid = $('input#mapid').val();
            priv.initMap();
        }
    };
} (jQuery);

