// JavaScript Document
function CreateDate(container) {

	var d_names = new Array("Sunday", "Monday", "Tuesday",
	"Wednesday", "Thursday", "Friday", "Saturday");
	
	var m_names = new Array("January", "February", "March", 
	"April", "May", "June", "July", "August", "September", 
	"October", "November", "December");
	
	var d = new Date();
	var curr_day = d.getDay();
	var curr_date = d.getDate();
	var sup = "";
	if (curr_date == 1 || curr_date == 21 || curr_date ==31) { sup = "st";
	} else if (curr_date == 2 || curr_date == 22) { sup = "nd";
	} else if (curr_date == 3 || curr_date == 23) { sup = "rd";
	} else { sup = "th"; }
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	
	$(container).html(d_names[curr_day]+", "+curr_date+"<SUP>"+sup+"</SUP> "+m_names[curr_month]+", "+curr_year);

}

function styleSwitcher(styleName) {
    $('link[rel*=style]').attr("disabled",true).siblings('link[title='+styleName+']').attr("disabled",false);
}

function getQueryVariable(variable) { 
	var query = window.location.search.substring(1); var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); var ret = ""; if (pair[0] == variable) return pair[1]; } 
	return false;  
} 

function LoadMenu() { $.ajax({ url: "/upload/menu.html", success: function(data) { $(".TopLine").after(data); ActivateMenu(); } }); }

function ActivateMenu() {

	//Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*) 
	$("ul.subnav").prev("a").wrap("<div></div>").after("<span></span>"); 
	
	//Following events are applied to the trigger (Hover events for the trigger)  
	$("ul.subnav span").hover(
		function() { $(this).addClass("subhover"); }, //On hover over, add class "subhover"  
		function(){ $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	});

	$("ul.topnav li").hover(
		function() { //When menu item is hovered ...  
  
			 if ( !$(this).children("ul.subnav").is(':animated') ) { 
				$(this).addClass('hover'); 
				$('ul.topnav .hover').find('.off').fadeOut("fast");
				
				//Following events are applied to the subnav itself (moving subnav up and down)  
				$(this).children("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
			 }
		  
    	},
		function() { 
				$(this).children("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
				$('ul.topnav .hover').find('.off').fadeIn("fast"); $(this).removeClass('hover');
		}
	);

}

function LoadModules(vPath) {

	var modules = getQueryVariable("m");
	if (modules == "") {

		$.ajax({
			url: vPath+"/modules.xml",
			success: function(xml) {
	
				$(xml).find("Module").each(function (i, val) {
					$.ajax({
						aynsc: false,
						url: "upload/modules/"+$(val).text()+".html",
						success: function(data) { 
							$(".SideBar").append("<div class='module "+$(val).text()+"'>"+data+"</div>"); 
						}
					});
				});
			}
		});
	
	} else {
	
		var m = modules.split(",");
		$(m).each(function (i, val) {
			$.ajax({
				url: "upload/modules/"+val+".html",
				success: function(data) { 
					$(".SideBar").append("<div class='module "+val+"'>"+data+"</div>"); 
				}
			});
		});
	
	}

}

function LoadPage(vPath, page) {
	$.ajax({
		url: vPath+page+".html",
		success: function(data) {
			var vImage = "upload/images/" + $(data).siblings("#Image").html();
			if (vImage == "") vImage = "upload/images/"+page+".jpg";
			if (vPath == "upload/static/") vImage = "upload/static/images/"+page+".jpg";
			if (vPath == "upload/countries/") vImage = "upload/countries/images/"+page+".jpg";
			var vTitle = $(data).siblings("#Title").html();
			var vIntro = $(data).siblings("#Intro").html();
			var vMain = $(data).siblings("#Main").html();
			var vMore = $.trim($(data).siblings("#More").html());
			if (vPath == "upload/stories/") $("#StoryTitle h1").html(vTitle+":");
			$("#StoryTitle p").html(vIntro);
			if (vPath == "upload/stories/") $("#StoryImage h1").html("Read Story: "+vTitle); 
			else $("#StoryImage h1").html(vTitle);
			$("#StoryImage .ImgClip img").attr("src", vImage);
			if ($.browser.msie) {
				if (vPath == "upload/countries/") $(".ReadStory .intro h1").html("Samaritan's Purse in "+vTitle);
				else $(".ReadStory .intro h1").html(vTitle);
			} else {
				if (vPath == "upload/countries/") $(".ReadStory>.intro h1").html("Samaritan's Purse in "+vTitle);
				else $(".ReadStory>.intro h1").html(vTitle);
			}
			$(".ReadStory div.countryBody").after(vMain);
			if (vMore != "") $(".ReadStory>.personal").html("<h1 class='StoryHeader'>Samaritan's Purse - Touching Lives</h1>"+vMore); else $(".ReadStory>.personal").hide();
			
			$(".thickbox").click(function(event) {
				event.preventDefault();
				var h = $(this).attr("href");
				tb_show("Show Video",h);
			})
		}
	});
}

function LoadStory(i, path, story) {

	var storyClass = "Story"; var imgSize = 63;
	if (i == 0) { storyClass = "MainStory active"; imgSize = 100; }
	
	$.ajax({
		url: path+story+".html",
		aynsc: false,
		success: function(data) { 
			var vImage = "upload/images/"+story+".jpg";
			var tImage = "upload/images/"+story+"_s.jpg";
			if ($(data).siblings("#Image").html() != "") {
				tImage = vImage = "upload/images/"+$(data).siblings("#Image").html();
				tImage = tImage.replace(".","_s.");
			}
			var vTitle = $(data).siblings("#Title").html();
			var vIntro = $(data).siblings("#Intro").html();
			var vLink = $(data).siblings("#Link").html();
			if (vLink == undefined) vLink = "story.html?p="+story;
			$(".navi").append("<a class='"+storyClass+"' href='"+vLink+"'><div class='ImageContainer'><img src='"+tImage+"' alt='Main Story' height='"+imgSize+"' title='Click here to read this story.'></div><h1>"+vTitle+": </h1><p>"+vIntro+"</p></a>");
			$(".scrollable .items").append("<div><h1 class='RedHeader'>Read Story: "+vTitle+"</h1><div class='ImgClip'><img src='"+vImage+"' title='Hovering on this image pauses the carousel! It will restart shortly after you move the cursor off this image.'/></div></div>");
			if (($.browser.msie) && (storyClass=="MainStory active")) {
				$(".navi .MainStory .ImageContainer img").click(function () { window.location=vLink; });
			}
		}
	});
	if ($.browser.msie) {
		var date = new Date(); var curDate = null;
		do { curDate = new Date(); } while(curDate-date < 200);
	}


}

function LoadDOPs(target) {
	
//	var vPath = "/admin/"; //"upload/static/";
	var vPath = "/upload/"; //"upload/static/";
	var page = "DropOffPoints.xml";
	
	$.ajax({
		url: vPath+page,
		success: function(xml) {
			$(target).html("<TABLE></TABLE>");
			$(target+" TABLE").append("<TR><TH>Country</TH><TH>Town</TH><TH>Name</TH><TH>Address</TH><TH>Phone</TH></TR>");
			$("DROPOFFPOINT", xml).each(function (i, val) {
				var vCounty = $(val).find("COUNTY").text();
				var vTown = $(val).find("TOWN").text();
				var vName = $(val).find("NAME").text();
				var vAddress = $(val).find("ADDRESS").text();
				var vPhone = $(val).find("PHONE").text();
				$(target+" TABLE").append("<TR><TD>"+vCounty+"</TD><TD>"+vTown+"</TD><TD>"+vName+
										  "</TD><TD>"+vAddress+"</TD><TD>"+vPhone+"</TD></TR>");

			});
			$(target+" TABLE TR:even").addClass("even");
		}
	});
}

