


$(function()
{
	SubnavMenu.initialize();
	NavMenu.initialize();

	if (document.getElementById("w-desc") != undefined){
		$.ajax({
         type: "GET",
         url: DESIGN_FOLDER + "javascript/xml_repeater.php",
         dataType: "xml",
         success: function(xml) {
             var childNodes = $(xml).find('item')[0].childNodes;
             for (x=0; x< childNodes.length; x++){
             	if (childNodes[x].nodeName == "yweather:condition"){
             		$("#w-desc").html($(childNodes[x]).attr("text"));
             		$("#temp").html($(childNodes[x]).attr("temp"));
             		
             		$(".left-column #weather .weather-design").css({
             		"background": "transparent url("+DESIGN_FOLDER+"images/weather/"+$(childNodes[x]).attr("code")+".png) bottom right no-repeat"
             		});

             		break;
             	}
             }

             
	        } //close success
	    }); //close $.ajax

	
	
	}
	if (images_in_slideshow.length){
		if (NODE_ID == 2) { var slideshow_height = "361"}
		else { var slideshow_height = "249" }
		imageStyle = "display:block;";
		for (x=0; x< images_in_slideshow.length; x++ ){
			$("#slideshow-design").append("<div style='background: transparent url("+images_in_slideshow[x]+") no-repeat center center; width: 882px; height: "+slideshow_height+"px;"+imageStyle+"'></div>")
			imageStyle = "display:none;";
		}
		if (NODE_ID == 2) {
			$("#slideshow-design").cycle({ fx:    'fade', 
			    speed:  2500 });
		}

		
		
	}
	
	
	$("input#EMAIL").bind("focus", function() {
		if ($(this).val() == "Enter email for latest news") {
			$(this).val("");
		}
	}).bind("blur", function() {
		if ($(this).val() == "") {
			$(this).val("Enter email for latest news");
		}
	});
});

// nav menu behaviors

NavMenu = {
	initialize: function() {
		//alert($("ul#nav > li > a").length);
		$("ul#nav > li").each(function() {
			// copy behavior methods to DOM object
			for (property in NavMenu) {
		    	this[property] = NavMenu[property];
		  	}
		if (this.hasSubnav()){
			if ($(this.getMySubnavName()).get(0).shouldNormallyBeHidden()) {
				$(this).mouseenter(this.mouseEnterHandler).mouseleave(this.mouseLeaveHandler);
				
			}
		}
		
		});
		
	},
	mouseEnterHandler: function() {
	    //hide everyone
		$("ul.subnav").each(function() { 
			
			 this.hideMe();
		 });
		$(this.getMySubnavName()).each(function(){this.showMe();});
	},
	mouseLeaveHandler: function() {	
		$("ul.subnav").each(function() {
			if (this.shouldNormallyBeHidden()){
				this.hideMe()
			} else if (this.shouldNormallyBeVisible()) {
				$(this).css({"display": "none"})
				this.showMe();
			}
		 });
		if ($(this.getMySubnavName()).size() == 0){
			if ($("#nav li.active").size()){
				$("#nav li.active a").get(0).showMySubnav();
			}
		}
	},
	getMySubnavName: function() {
		var aLink = $("a", this).get(0);
		return "#" + aLink.id.replace("btn_", "subnav_");
	},
	showMySubnav: function() {
		$(this.getMySubnavName()).each(function() { this.showMe();});
	},
	isActive: function() {
		return $(this).hasClass("active");
	},
	hasSubnav: function() {
		//alert($(".subnav li", this.parentNode).size());
		return $(".subnav li", this).size();
	}
	
}

// subnav behaviors

SubnavMenu = {
	
	initialize: function() {
		$("ul.subnav").each(function() {
			// copy behavior methods to DOM object
			for (property in SubnavMenu) {
		    this[property] = SubnavMenu[property];
		    }
		    $(this).mouseenter(SubnavMenu.mouseEnterHandler);
			if (this.shouldNormallyBeHidden()) {
				$(this).mouseleave(SubnavMenu.mouseLeaveHandler);
				this.hideMe()
			} else if (this.shouldNormallyBeVisible()) {
				$(this).css({"display": "none"});
				this.showMe();
			}
		});

	},
	mouseEnterHandler: function() {
		//this.showMe();
	},
	mouseLeaveHandler: function() {


	},
	showMe: function() {
		if (this.isCurrentlyHidden()){
			$(this).show(200, function() { 
				$(this).animate({ top: "19px" }, 200, "swing");
			 });
		}
	},
	hideMe: function() {
		if (this.isCurrentlyVisible()){
			$(this).animate({ top: "0px" }, 200, "swing", function() { $(this).hide(); });
		}
	},
	shouldNormallyBeHidden: function() {
		if ( $( $( this.getMyNavName() ).get(0).parentNode ).hasClass("active") ) {
			return false;
		} else {
			return true;
		}
		
	},
	shouldNormallyBeVisible: function() {
		return $( $( this.getMyNavName() ).get(0).parentNode ).hasClass("active");
	},
	getMyNavName: function() {
		return "#"+this.id.replace("subnav_", "btn_");
	},
	isCurrentlyHidden: function() {
		return ($(this).css("display").toLowerCase() == "none");
	},
	isCurrentlyVisible: function() {
		return ($(this).css("display").toLowerCase() == "block");
	}
	
	
}

function print_r(OObj, recurse, prependingSpace) {
	if(typeof OObj == 'object') {
		var treeDisplay = '';
		for(var key in OObj) {
			treeDisplay += prependingSpace+'['+key+'] => \''+OObj[key]+'\' ('+typeof OObj[key]+')\n';
			if(recurse && typeof OObj[key] == 'object') {
				treeDisplay += print_r(OObj[key], recurse, prependingSpace+'\t');
			}
		}
		return treeDisplay;
	} else {
		return 'not an object!';
	}
}

