

/**
 * static object that handles page logic
 */
var Logic = function($){
	/**
	 * private methods and variables
	 */
	var priv = {
		debug	: (resources.debugmode == 'true'),		//enables/disables the error logging
		debugSeverity : 10,	//level of error logging
		console	: $("<ul>").addClass("debug-console"),
				
		initConsole	: function(){
			//if the console has not yet been added to the document, do so now
			if(!priv.console.parentNode){
				$("body").append(priv.console);
			} 
		},
		
		bindEvents : function(){
		    // make whole price blocks clickable
		   
		   
            
        }		
	};
	
	/**
	 * public methods
	 */
	return {		
		/**
		 * initializes the page logic
		 * to be called on $(document).ready
		 */
		OnReady	: function(){
		    $.browser.msie6 = ($.browser.msie && typeof(XMLHttpRequest) == "undefined" && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent) && !/MSIE 8\.0/i.test(window.navigator.userAgent)); 
		    Logic.writeDebug("IE6? : " + $.browser.msie6);
			//add trim method to the string object
			String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
			String.prototype.stripTags = function() {return this.replace(/<(.|\n)*?>/g, "");}
			String.format = function()
            {
                if( arguments.length == 0 )
                    return null;

                var str = arguments[0];
                for(var i=1;i<arguments.length;i++)
                {
                    var re = new RegExp('\\{' + (i-1) + '\\}','gm');
                    str = str.replace(re, arguments[i]);
                }

                return str;
            }            
            
//            if(typeof(Home) != "undefined"){
//                Home.OnReady();
//            }
			
           
            if(typeof(Accommodation) != "undefined"){
                Accommodation.OnReady();
			}			
			
			
//			if(typeof(personalItems) != "undefined"){
//			    personalItems.Load('alreadyviewed');
//			    personalItems.Load('favorites');
//				personalItems.ShowItemLinks();
//			}
			
			priv.bindEvents();
			
			//firefox select all "issue"
            if($.browser.mozilla){
                $("body").bind("mousedown", function(evt){
                    if(evt.target.tagName == "BODY"){
                        return false;
                    }
                });
            }
			
			if(!priv.debug){
			    $("span.error").hide();
			}
		},
				
		OnResize	: function(){
		    //Lightbox.OnResize();
		},
		
		
        		
		/**
		 * handles the writing of debug messages
		 * @param {String} msg
		 * @param {Int} severity: 0 info, 1 warning, 2 error, 3 critical
		 */
		writeDebug	: function(msg, severity){
			if(typeof(severity) == "undefined"){
				severity = 0;
			}
			
			if(priv.debug && severity <= priv.debugSeverity){
				//check whether debug console exists
				if(typeof(console) != "undefined" && console.log){
					console.log(msg);
				}
				//if no debug console exists: create debug div
				else {
					priv.initConsole();
					$(priv.console).append(
						$("<li>").html(msg)
					);
				}
			}
		}

	}
}(jQuery);

var Timer = function(){
    var priv = {
        startTime : new Date(),
        endTime   : null
    };
        
    return {
        Start : function(){
            priv.startTime = new Date();
        },
        
        Stop : function(){
            priv.endTime = new Date();
            return (priv.endTime - priv.startTime);
        },
        
        ShowIntermediate : function(){
            var intermediateTime = new Date();
            return (intermediateTime - priv.startTime); 
        },
        
        GetStartTime : function(){
            return priv.startTime;
        }
    };
};

/**
 * Initiate onload methods and functions
 */
$(document).ready(
	function(){
	    var TotalOnreadyTime = new Timer();
		Logic.OnReady();
		Logic.writeDebug("script time was: " + (TotalOnreadyTime.Stop()) + "ms ", -1);
        
        $('.marker span.lnk').bind('click', function() {
            location.replace($(this).attr('lnk'));
        });
        
        $('.marker label').bind('click', function() {
            location.replace($(this).attr('lnk'));
        });
        
        $('#inp-search').bind('keypress', function(e) {
            if(e.keyCode == 13) {
                Search.doTextSearch()
            }
        });
        
	}
);

/**
 * Bind resize eventes
 */
$(window).bind('resize', 
	function(){
		Logic.OnResize();
});
