﻿/**
 * static object that handles page logic
 * @class
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var LocationMain = function($) {

    /**
    * @namespace Private methods and variables
    */
    var priv = {
        bindExtraTabsEvents: function() {
            $('.tab-area .tabset li a').click(function() { priv.selectTab($(this).attr('tabindex')) });
            priv.selectTab(0);
        },

        selectTab: function(id) {
            $('.tab-area .tabset li').removeClass('active');
            $('.tab-area .tab-content').hide();

            $($('.tab-area .tabset li').get(id)).addClass('active')
            $($('.tab-area .tab-content').get(id)).show();
            
            priv.syncTabcontentHeight();
        },

        syncHeight: function() {//resize blockquotes next to USP list
		   if ($(".usps").length && $(".blockquote-area").length) { 
			   var th = $(".usps").height()-9;
			   $(".blockquote-area .holder").height(th);
		   }
		   if ($(".visual-block").length && $(".information-box").length) { 
			   var th = $(".information-box").height()+13;
			   $(".visual-block").height(th);
		   }
		},

        syncTabcontentHeight: function() {   
		   var maxHeight = 0;
		   $('.tab-content article.column').each(function() {
				$(this).height('auto');
				if ($(this).height() > maxHeight)
					maxHeight = $(this).height();
		   });
		   $('.tab-content article.column').each(function() {
				$(this).height(maxHeight);
		   });
        },

        initFlashBanner: function() {
            //test for flash content
            var $flashObject = $("section.area-block .visual-block object, section.area-block .visual-block embed");
			var $imageObject = $("section.area-block .visual-block img");
            //test player version
            if (swfobject.hasFlashPlayerVersion("9.0.0") && $flashObject.get(0)) {
                $flashObject.css("display", "block");
                $("section.area-block .visual-block img").hide();
            }
			else {
				$imageObject.fadeIn(200);
			}
			$(".visual-block .social-holder").show();
        },
		
		minColumnHeight : 0,
		loadLock : false,
		loadExamAccoList : function(locationId){
			if(!$("#twocolumns.search").get(0) || priv.loadLock){
				return;
			}
			priv.loadLock = true;
			
			priv.minColumnHeight = Math.max(priv.minColumnHeight, $("#twocolumns.search").height());
			
			$("#twocolumns.search").animate({"opacity" : 0}, 200,
				//not using the .load() method, jQuery 1.4 requires the innerShiv to work with IE7/8
				function(){
					$.ajax({
						url : Resource.GetText("path_prefix") + "/js/ajax/exam_accolist.aspx?locId=" + locationId,
						dataType : "html",
						cache : true,
						success : function(data){
							Log.Debug("locationmain: Ready loading accommodations");
							
							$("#twocolumns.search").empty().append(data);
							
							$("#twocolumns.search").animate({"opacity" : 1}, 200);
							
							//rebind the events from for saving/comparing
							Main.BindSearchEvents();
							
							priv.minColumnHeight = Math.max(priv.minColumnHeight, $("#twocolumns.search").height());
							$("#twocolumns.search").css({"min-height" : priv.minColumnHeight});
							
							priv.loadLock = false;
						},
						error : function(){
							
						}
					});
				}
			);			
		}
    };

    /** @scope Main */
    return {

        /**
        * Initializes the logic for the current page
        * to be called on $(document).ready
        */
        OnReady: function() {
            priv.bindExtraTabsEvents();
            priv.syncHeight();
            priv.initFlashBanner();
			
			if($("#twocolumns.search").get(0)) {
				$("div.block-columns nav a").bind("click", 
					function(evt){
						evt.preventDefault();
						
						priv.loadExamAccoList($(this).attr("data-id"));
					}
				);
			}

            //initialize the countrymap
            if (typeof (MapWrapper) != "undefined" && MapWrapper) {
                var mapWrapper = new MapWrapper({ "mapElement": $("#map").get(0) });
            }
        }
    };
} (jQuery);

