// JavaScript Document

// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July","August","September","October","November","December");

var now = new Date();

//Write today's date
$(document).ready(function() {
	document.getElementById('date').innerHTML = dayNames[now.getDay()] + ", " + monthNames[now.getMonth()] + " " + now.getDate() + ", " + now.getFullYear();
 });


var total_divs=0; //do not change this. it will be calculated automatically
var className='fadeThis'; //name of your class assigned to the divs you would like to rotate. Need to change it

$(document).ready(function(){
	$("."+className).attr("id", function (arr) {
		total_divs++;
		return "fade" + (arr+1);
	})
	fadeEngine(0);
});

function fadeEngine(x) {
	var y=x;
	if(x==total_divs) y=1; else y++;
	$("#fade"+x).css("display","none");
	$("#fade"+y).fadeIn("slow");
	setTimeout('fadeEngine('+y+')',8000);
}



function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn("fast");
		},function(){
		$(this).find('ul:first').fadeOut("fast");
		});
}
 
$(document).ready(function(){					
	mainmenu();
});


$(document).ready(function() {
	
	$.get("/nav.xml", null, function(result) {
									   
		function makeInFunc(submenu) { return function() { submenu.css("display","block"); } };
		function makeOutFunc(submenu) { return function() { submenu.css("display","none"); } };

		function appendToList(xmlItems, list)
		{
			for(var i = 0; i < xmlItems.length; i++)
			{
				var xmlItem = $(xmlItems[i]);
				var xmlItemChildren = xmlItem.children();
				
				if(xmlItem.attr('link')){
					if(xmlItemChildren.length > 0){
						var listItem = $("<li><a href=\"" + xmlItem.attr("link") + "\" class=\"sub\">" + xmlItem.attr("text") + "</a></li>").appendTo(list);
					} else {
						var listItem = $("<li><a href=\"" + xmlItem.attr("link") + "\">" + xmlItem.attr("text") + "</a></li>").appendTo(list);
					}
					
					var mypath = window.location.pathname;
					//mypath = mypath.replace("", "");
					if(xmlItem.attr('link') == mypath) {

						myParent = xmlItem.parent();
						var myParentChildren = myParent.children();
						
						var foundIt = myParent.attr("text");
						
						$("#leftNav.open").prepend('<h2>'+foundIt+'</h2>');
						
						for(var j = 0; j < myParentChildren.length; j++) {
							var childItem = $(myParentChildren[j]);
							if(childItem.attr('link') == mypath) {
								$("#leftNav.open").append("<a href=\"#\" class=\"active\">" + childItem.attr("text") + "</a>");
							} else {
								$("#leftNav.open").append("<a href=\"" + childItem.attr("link") + "\">" + childItem.attr("text") + "</a>");
							}
						}
						$("#leftNav.open").removeClass("open");
						
					}
				} else {
					var listItem = $("<li><a href=\"#\" class=\"top\">" + xmlItem.attr("text") + "</a></li>").appendTo(list);
				}
				
				
				if(xmlItemChildren.length > 0)
				{
					var submenu = $("<ul class=\"submenu\"></ul>").prependTo(listItem);
					listItem.hover(makeInFunc(submenu),makeOutFunc(submenu));
					appendToList(xmlItemChildren, submenu);
				}
			}
		}
		appendToList($("menu", result).children(), $("#nav"));
		var c1h = $("#col1").height();
		var c2h = $("#col2").height();
		var wnh = $("#whatsNew").height();
		if(c2h > c1h){
			var new_wnh = c2h - c1h;
			new_wnh = new_wnh + wnh;
			$("#whatsNew").height(new_wnh);
		}
		if(c1h > c2h){
			var new_wnh = c1h - c2h;
			new_wnh = new_wnh + c2h;
			$("#col2").height(new_wnh);
		}
	});
});

$(document).ready(function(){				   
	$("#quicklinks a").each(function(){
		if($(this).height() < 20){
			$(this).addClass("singleLine");
			//alert($(this).height());
		} else {
			$(this).addClass("doubleLine");
		}
	});
});

$(document).ready(function(){
	$("#toggleTabs a").each(function(){
		$(this).bind("click", function(){
			var myPane = this.id;
			myPane = myPane.replace("pane", "");
			var myelement = document.getElementById(myPane); 
			//alert(myPane);
			$("#toggleTabs a").removeClass("active");
			$(this).addClass("active");
			$("#weather").removeClass("active");
			$("#news").removeClass("active");
			$(myelement).addClass("active");
		});
	});
});

$(document).ready(function(){					
	$.ajax({
		type: "GET",
		url: "google-weather.php?where=92399",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('city').each(function(){
				var id_text = $(this).attr('data');
				//alert(id_text);
			});
			$(xml).find('current_conditions').each(function(){
				$("#weather").append('<h3>Current Conditions:</h3>');
				var condition = $(this).children('condition').attr('data');
				var temp_f = $(this).children('temp_f').attr('data');
				var temp_c = $(this).children('temp_c').attr('data');
				var humidity = $(this).children('humidity').attr('data');
				var wind_condition = $(this).children('wind_condition').attr('data');
				var icon = $(this).children('icon').attr('data');
				$("#weather").append('<img src="http://www.google.com'+icon+'" />');
				$("#weather").append('<p><b>'+condition+': </b>'+temp_f+'&ordm;F'+'</p>');
				$("#weather").append('<p><b>'+humidity+': </b></p>');
				$("#weather").append('<p><b>'+wind_condition+'</b></p>');
			});
			var i= 0;
			$(xml).find('forecast_conditions').each(function(){
				if(i < 3){
					var now = new Date();
					var long_date = now.setDate(now.getDate()+i);
					//var day_of_week = $(this).children('day_of_week').attr('data');
					$("#weather").append('<h3>'+dayNames[now.getDay()]+", " + monthNames[now.getMonth()] +" " + now.getDate() +'</h3>');
					var condition = $(this).children('condition').attr('data');
					var high = $(this).children('high').attr('data');
					var low = $(this).children('low').attr('data');
					var icon = $(this).children('icon').attr('data');
					$("#weather").append('<img src="http://www.google.com'+icon+'" />');
					$("#weather").append('<p><b>'+condition+'</b></p>');
					$("#weather").append('<p><b>High: </b>'+high+'&ordm;F'+'</p>');
					$("#weather").append('<p><b>Low: </b>'+low+'&ordm;F'+'</p>');
				}
				i = i + 1;
			});
		}
	}); //close $.ajax(
});

$(document).ready(function() {
    $('.rotate').after('<div id="ronav">').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		pager:  '#ronav',
		fit: 1,
		timeoutFn: calculateTimeout
	});
});

function calculateTimeout(currElement, nextElement, opts, isForward) { 
    // here we set even number slides to have a 2 second timeout; 
    // by returning false for odd number slides we let those slides 
    // inherit the default timeout value (4 sec) 
    var index = opts.currSlide;
    var dsp_time = $(currElement).attr("class") * 1000;
    //alert(dsp_time);
    return dsp_time; 
} 

