// JavaScript Document
//jQuery.noConflict();
function getFileName(path){
	//Rule for the live server
//	var fn = path.match(/([\w-]+\.\w+)\/([a-z0-9_-]+\/)?([a-z0-9_-]+\.\w+)/i);
	
	//Rule for localhost
	var fn = path.match(/([\w-]+)\/([a-z0-9_-]+\/)?([a-z0-9_-]+\.\w+)/i);

	//alert(fn);
	if(fn == null)
		return '';
	else
	{
		if(fn[2]==null)
			return fn[3];
		else
			return fn[2]+fn[3];
	}

}

function paddedNum(iNum)
{
	iNum = iNum.toString();
	iPaddedLength = 2;
	paddedNum = ""+iNum;
	while (paddedNum.length < iPaddedLength) {
		 paddedNum = "0" + paddedNum;
	}
	return 	paddedNum;	
}

function DocfilenameExtract()
{
	wholeurl = document.location.href;
	x = wholeurl.length;
	while((wholeurl.substring(x,x-1))!= "/"){ x--; } clipstart = x;
	return wholeurl.substring(wholeurl.length,clipstart);
} 

function parse_date(string) {
	var date = new Date();
	var parts = String(string).split(/[- :]/);

	date.setFullYear(parts[0]);
	date.setMonth(parts[1] - 1);
	date.setDate(parts[2]);
	date.setHours(parts[3]);
	date.setMinutes(parts[4]);
	date.setSeconds(parts[5]);
	date.setMilliseconds(0);

	return date;
}

$(document).ready(function(){
	//var path = DocfilenameExtract();
	var path = getFileName(document.location.href);
	//alert(path);
	if(path!="" || path!="main/index.html"|| path!="/main.html")
	{
		 $("#news").load("/news/headlines/10/0/0/0/0",{},function(){
			$(this).innerfade({ 
			animationtype: "slide", 
			speed: 850, 
			timeout: 3000, 
			type: "random", 
			containerheight: "1em" 
			}); 
		}); 
	}
	$("#image_rotate1").innerfade({ 
			animationtype: "fade",
			speed: "slow", 
			timeout: "3000", 
			type: "sequence", 
			containerheight: "220px"
		});

    $("#smoothmenu1 li").each(function(){
		//alert($(this).find("a").attr("href"));	
		
		var check = false;
		var rel = $(this).find("a").attr("rel").split(",");
		for(i=0;i<rel.length;i++)
		{
			//alert(rel[i]+' '+path);
			if(rel[i]==path)
			{
				check = true;
				break;
			}
		}
		//alert(check);
		if(!check){
		$(this).hover(function(){
			$(this).css({"background-image":"url(/my/frontend/images/Menu-BG-O.jpg)"});
			$(this).children("a").css("color","#015C19");
		}, function () {
			$(this).removeAttr("style");
			$(this).css({"background-image":"url(/my/frontend/images/Menu-BG-N.jpg)"}).find("a").css("color","#D5E1D5");
		});
		$(this).click(function(e){
			if($(e.target).is("li"))
			{
				$(e.target).hide("explode",{ pieces: 25 });
				$(e.target).show("explode",{ pieces: 25 });
			}	
			else
			{
				$(e.target).parent().hide("explode",{ pieces: 25 });
				$(e.target).parent().show("explode",{ pieces: 25 });
			//$(e.target).is("a").show("explode",{ pieces: 25 });
			}
			//document.location.href=href;
		});
		} else {
			$(this).css({"background-image":"url(/my/frontend/images/Menu-BG-O.jpg)"}).find("a").css("color","#015C19");
//			alert($(this).find('a').text());
//			$(this).css({"background-image":"url(/my/frontend/images/Menu-BG-O.jpg)"}).find("a:contains('"+$(this).text()+"')").css("color","#015C19");
			
			$(this).click(function(){
				$(this).show("highlight",{backgroundColor: "#D5E1D5"},800);
				//return false;
			});
		}	
		
		
	});


// Datepicker
$("#datepicker").datepicker({inline:true, changeMonth: true, changeYear: true, 
onChangeMonthYear: function(yr, mon) { 
	$.getJSON("/events/getEvents/"+mon+"/"+yr, function(data){
		var cal = new Array;
		$.each(data, function(i,item){
					item.Date = parse_date(item.Date); 	
					//var day = item.Date.getUTCDay();
					if(cal[item.Date.getDate()+1])
						cal[item.Date.getDate()+1] += '<br>'+item.Title1;
					else
						cal[item.Date.getDate()+1] = item.Title1;
					
				 });
		$.each(cal, function(i,item){
					//alert(i+' '+item);
					if(item)
					{
					$("#datepicker").find("a").eq(i).addClass('ui-state-active').parent().aqLayer({closeBtn:true, attach:'s', offsetY:10,offsetX:-180}).click(function(e){
							   $('*').aqLayer('hide');
							  	$(e.target).parent().aqLayer(item);
								$("a[rel*='external']").click(function(e){
									window.open($(e.target).attr("href"));
									return false;
								});
								return false;
						   });
					}
			 });

		 });
  } });

//right image rotator
	var stackright = []; 
	 var imgsright = [];
	//alert(paddedNum(1));
	
	// preload images into an array; we will preload beach3.jpg - beach8.jpg
	var imgsright = new Array("/my/shared/images/img_rotator1/01b367d2123d8c54b1318e1522f35867.jpg","/my/shared/images/img_rotator1/b107257e0fbccc50f6bbcedaaef6fd82.jpg","/my/shared/images/img_rotator1/6ac7ed4ee2120dc24f9f217d4e013064.jpg","/my/shared/images/img_rotator1/22c4331e2a8261521bbe09c9b4bb0e76.jpg","/my/shared/images/img_rotator1/98e681d1222e26a36c4efa781a15a34b.jpg","/my/shared/images/img_rotator1/6653c56ce469e2e98e14001c3b2ebe19.jpg","/my/shared/images/img_rotator1/cb3bc206393d73e3e2a882f43e0d3168.jpg","/my/shared/images/img_rotator1/f9527cffe9bccb611097f806492eef54.jpg","/my/shared/images/img_rotator1/233e9a6a0dc9900b082d4c9f93e909ce.jpg","/my/shared/images/img_rotator1/72b2795104e3bf8113c9edcffee49915.jpg","/my/shared/images/img_rotator1/2ddc7141a44591f0bf427095896d6e93.jpg","/my/shared/images/img_rotator1/96ecfb4429f4d3bf3990b35f5c9430a9.jpg","/my/shared/images/img_rotator1/2ea654dac04d2247b64b2fce450fd6d1.jpg","/my/shared/images/img_rotator1/fb7c0b46680995fdd77568c10a2f9b97.jpg","/my/shared/images/img_rotator1/8b0de4755721ab33eb78d5ae4ee62a8a.jpg","/my/shared/images/img_rotator1/44c542e97ccb18cb3619a56d95a94157.jpg","/my/shared/images/img_rotator1/b22ec661a2cd8278da548d38926e3e01.jpg","/my/shared/images/img_rotator1/b0f0a98e0656c3c9d31d21bc247c6918.jpg");
	//alert(imgsright.length);
	for (var i = 3; i <= imgsright.length; i++) { 
		var imgsright1 = new Image(); 
		imgsright1.src = imgsright[i]; 
		imgsright1.alt = ""; 
		
		$(imgsright1).bind("load", function() { 
			//alert(this.src);
			stackright.push(this); 
		}); 
	}  
	// start slideshow 
	$("#slideshowright").cycle({ 
		fx: "growX,growY,wipe",
		before:   onBefore 
	}); 
	
	// add images to slideshow 
	function onBefore(curr, next, opts) { 
		if (opts.addSlide) 
			while(stackright.length) 
				opts.addSlide(stackright.pop());  
	}; 
			



});