//namespace
var ha = {
	ajax: {}, map: {}, geocode: {}, reviews: {}, page: {}, settings: {}, site: {}, strings: {}, ui: {},
	
    util: {
    	isInt: function(c){ return((c>="0")&&(c<="9")) },
    	
    	initToggles: function(){
			$j(".expand .content").addClass("hidden");
			$j(".expand .show").removeClass("hidden");
			$j(".expand .action").bind("click", function(){
				var el = $j(this).parent("div.container").children("div.content");
				if(el.hasClass("hidden")){
					el.removeClass("hidden");
					$j(this).addClass("open");
				}
				else{
					el.addClass("hidden");
					$j(this).removeClass("open");
				}
			});
		}
    }
    
};

/* 
 * Straightforward/simple Observer pattern implementation
 * Example useage:
 * var publisher = new Observer
 * publisher.subscribe(function(msg){
 *    alert(msg);
 * });
 * publisher.fire("Event fired!");
 */
function Observer() {
	this.fns = [];
}

Observer.prototype = {
	subscribe : function(fn) {
		this.fns.push(fn);
	},
	unsubscribe : function(fn) {
		this.fns = this.fns.filter(
			function(el) {
				if (el !== fn) {
					return el;
				}
			}
		);
	},
	fire : function(o, thisObj) {
		var scope = thisObj || window;
		this.fns.forEach(
			function(el) {
				el.call(scope, o);
			}
		);
	}
};
// Add some sugar to the Array prototype
Array.prototype.forEach = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        fn.call(scope, this[i], i, this);
    }
};
Array.prototype.filter = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            continue;
        }
        a.push(this[i]);
    }
    return a;
};

//TO-DO: deprecate these
var jsEnabled = function(s){ this.s=s;}

/*
 * GLOBAL
 * advancedSearchForm, homeSearchForm, keywordSearchForm, refineSearchForm (aka sidebar)
 * noResults, 404error, error, secondary pages, etc.
 */
var searchErrorObserver = new Observer;



/*
 * HOME PAGE
 */
 	var fadeCount = 0;
	var layerCount = 1;
 
ha.page.home = {
	init: function(){
		ha.util.initToggles();
	},
		
	fade: function (init){
		var timeout, duration;
		var fadeTimer = 1500;
		var layer = $j("#layer" + layerCount);
			
		if (init || ha.page.home.faded()) {
			$j(layer).fadeIn(fadeTimer);
			fadeCount++;
			duration = 8000; // 5 seconds
		} else {
			$j(layer).fadeOut(fadeTimer);
				
			layerCount++;
			if (layerCount == $j(".fade-layer").length + 1) layerCount = 1;
			fadeCount = 0;
				
			duration = 10; // about half of fadeTimer/fadeIn
		}
			
		timeout = setTimeout(ha.page.home.fade, duration);
	},
		
	faded: function(){
		return (fadeCount % 2) ? false : true;
	}
};





ha.page.property = {
    init: function(){
		ha.ui.modalbox.init();
		ha.page.property.initNavBar();
		ha.ajax.inquiry.init();
		ha.page.property.initInquired();
		if (ha.settings.similarPropertiesOn){
			ha.util.initToggles();
		}
		
		if(window.ie6) { 
        	// get the image tag
        	image = $j('#regionMapImage');
        	if(image) {
        		imageSourceUrl = image.srcsure
        		image.src="";
        		image.src=imageSourceUrl;
        	}
        }
	},
	
	initNavBar: function(){
		var anchors = new Array('photos','location','rates','amenities');
		for (var i=0; i< anchors.length; i++){
			if ($j('#'+anchors[i]+'-bar').length == 0 || $j('#'+anchors[i]+'-bar').css("display") == "none"){
				$j('.'+anchors[i]+'-link').css("display","none");
			}
		}
	},
	
	initInquired: function() {
		if ($j('a#inquired').length > 0){
        	$j('a#inquired').hover(function(e) {
        		var offset = $j("#inq-icon").offset();
            	var isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);
        		var topPos = offset.top-338;
        		if (isSafari) topPos = topPos - 15;
        		$j('#inquired-msg').css({top: topPos, left: 703}).fadeIn(200);
        	},
        	function() { // out function
        		$j('#inquired-msg').fadeOut(100);
        	});
        }
	}
};

$j(document).ready(function(){
	//global
	if ($j("body").length > 0) ha.page.home.init();

});

