/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

// 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").stop().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 = vPath+"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", vPath+"images/"+page+".jpg");
			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();
		}
	});
}

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 == "")) 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 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");
		}
	});
}

