$(function(){
	$(".items").each(function(){
		$(this).css({'display':'none'})
	});$(".items:eq(0)").slideDown(520);
	
	getEvent(0);// event initiallized.
});
var i = 0;
function getEvent(index){
	$("#NewsEvents > .panelDetail").html("<div align='center'><br/><br/><br/><img src='images/loading.gif' alt='loading'></div>");
	 $.ajax({
		type:"GET",
		url:"include/events.php?index="+index,
		dataType:"html",
		success:function(args){
			$("#NewsEvents > .panelDetail").html(args);
			$("#NewsEvents > .panelDetail > .items").fadeIn(1000);
		}
	});
}

/* ==================  Login  ==================*/
$(function(){
	$("#txtLoginPassword").bind("keypress", function(e) {
	  if (e.keyCode == 13){
		 Login(); 
	  }	  
	});
});
function Login(){
	var formlogin = document.getElementById("formlogin");
	
	$("#loginMsg").html('');
	if(!validateEmail2(formlogin.txtloginEmail)){
		$("#loginMsg").html('Please enter valid email address.');		
		return false;
	}else{
		if(!validateString2(formlogin.txtLoginPassword)){
			$("#loginMsg").html('Please enter Password.');		
			return false;	
		}else{ // login 
			$("#loginMsg").html("<img src='images/loading1.gif'>");
			$.ajax({
				type:"GET",
				url:"include/login.php?action=check&email="+ $("#txtloginEmail").val() + "&psw="+ $("#txtLoginPassword").val(),
				dataType:"html",
				success:function(args){
					if(args == "error")
						$("#loginMsg").html('Invalid Login Id / Password, please re-enter.');
					else{
						//formlogin.action = "include/login.php?action=login&url=" + document.location.href;
						formlogin.submit();		
					}
				}
			});
			
	   }
	}
}
// -------------- Tab Control Rss ----------------
/* <![CDATA[ */
$(function(){
	$("#tab1 .tabcontrol > li >a").each(function(index){$(this).click(function(){NavigateTab(this,index);return false})});
	$("#tab1 .Details > div:eq(0)").css({'display':'block'});LoadRssNews($("#tab1 .Details > div:eq(0)"),$("#tab1 .tabcontrol > li > a").attr("title"));

	$("#tab2 .tabcontrol > li >a").each(function(index){$(this).click(function(){NavigateTab(this,index);return false})});
	$("#tab2 .Details > div:eq(0)").css({'display':'block'});LoadRssNews($("#tab2 .Details > div:eq(0)"),$("#tab2 .tabcontrol > li > a").attr("title"));

});
function NavigateTab(o,index){
	$(o).parent().parent().find("a").removeClass("selected");// remove all
	$(o).addClass("selected");
	var obj = $(o).parent().parent().parent().find(".Details > div:eq("+ index +")"); // select preticular tab-container
	obj.css({'display':'block'});
	$(o).parent().parent().parent().find(".Details > div").not(":eq("+index+")").css({'display':'none'});// disable all tab-container
	if(obj.html().length == 0){LoadRssNews(obj,o.title);}
}
function LoadRssNews(obj,title){
	$(obj).html("<div align='center'><br/><br/><br/><img src='images/loading.gif' alt='loading'></div>");
	$.ajax({
		url: "include/rss_feed.php?title="+title,
		method:'get',
		type:'html',
		success:function(args){
			$(obj).html(args);
		}
	});
}	
// ---------  Stock Data ----------
function LoadStockData(_refresh){
	if(!$("#div_stock").length){
		$("body").append("<div id='CoverUpdiv'></div><div id='div_stock'><div class='header'><a href=\"javascript:LoadStockData(true);\">Refresh</a> &nbsp; &nbsp; [<a href=\"javascript:ClosePOPUP('div_stock');\">X</a>]</div><div class='desc'></div></div>");
		ShowPOPUP('div_stock',820);GetLatestStockData();
	}else{
		if(_refresh)
			GetLatestStockData();
		else{
			ShowPOPUP('div_stock',820);	//GetLatestStockData();
		}
	}		
}
function GetLatestStockData(){
	$("#div_stock > .desc").html("<div align='center'><br/><br/><br/><br/><br/><br/>Loading please wait<br/><br/><img src='images/loading.gif' alt='loading'></div>");
	$.ajax({
		url: "include/rss_feed.php?title=KSE Quotes Data" ,
		method:'get',
		type:'html',
		success:function(args){
			$("#div_stock > .desc").html(args);
		}
	});
}
// ------------ poopup --------------
function ShowPOPUP(ID,width){
	$(document).scrollTop(0); 
	PopUpWIDTH = width; // set in popup.css (#PopUp)
	//PopUpHEIGHT = 250; // approximate height of popop
	var PopUp = document.getElementById(ID);
	var CoverUpdiv = document.getElementById("CoverUpdiv");
	PopUp.style.width = PopUpWIDTH + 'px';
	CoverUpdiv.style.width = parseInt($(document).width()) + 'px'; //document.documentElement.clientWidth + 'px';
	CoverUpdiv.style.height = parseInt($(document).height()) + 'px';//(document.documentElement.clientHeight + (document.body.scrollHeight - document.documentElement.clientHeight )) + 'px';
	CoverUpdiv.style.display = 'block';
	PopUp.style.top = '100px' //(parseInt(document.documentElement.clientHeight / 2) - parseInt(PopUpHEIGHT/2)) + 'px';
	PopUp.style.left = (parseInt(parseInt($(document).width())/2) - parseInt(PopUpWIDTH/2)) + 'px';
	PopUp.style.display='block';
}
function ClosePOPUP(o){
	var CoverUpdiv = document.getElementById("CoverUpdiv");
	CoverUpdiv.style.display = 'none';
	$("#"+o).css({'display':'none'});
}

// ----------------   Time  ------------------------
var weekdaystxt=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July","August","September","October","November","December"];
function showLocalTime(container, offsetMinutes, displayversion,serverTime){
	if (!document.getElementById || !document.getElementById(container)) return
	this.container=document.getElementById(container)
	this.displayversion=displayversion
	var servertimestring= serverTime;
	this.localtime=this.serverdate=new Date(servertimestring)
	this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
	this.updateTime()
	this.updateContainer()
}
showLocalTime.prototype.updateTime=function(){
	var thisobj=this
	this.localtime.setSeconds(this.localtime.getSeconds()+1)
	setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}
showLocalTime.prototype.updateContainer=function(){
var thisobj=this
if (this.displayversion=="long")
	this.container.innerHTML = this.localtime.toLocaleString()
else{
	var hour=this.localtime.getHours()
	var minutes=this.localtime.getMinutes()
	var seconds=this.localtime.getSeconds()
	var ampm = (hour>=12)? "PM" : "AM"
	var dayofweek=weekdaystxt[this.localtime.getDay()]
	this.container.innerHTML = "<br/>"+ dayofweek + ", "+ this.localtime.getDate() + " " + months[this.localtime.getMonth()] + " " + this.localtime.getFullYear() +"<br/><br/><span>" + formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm + "</span>";
}
	setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second
}
function formatField(num, isHour){
if (typeof isHour!="undefined"){ //if this is the hour field
	var hour=(num>12)? num-12 : num
	return (hour==0)? 12 : hour
}
	return (num<=9)? "0"+num : num//if this is minute or sec field
}

/* ]]> */		
// --------------  pool vote ---------------------

function Vote(){
		var purl = "include/pollvote.php?pid=" + $("#hndpid").val() + "&vote&ids=";
		var cnt = 0;
		$("input[name=poolfield[]]").each(function(){
			if($(this).attr("checked")){
				purl += $(this).val() + ",";
				cnt = 1;
			}
		});
		if(cnt == 0){
			alert("please select one option");
		}else{
			purl = purl.substr(purl, (purl.length - 1));
			$("#pooltbl").fadeOut(300,function(){
				// create cookie for voit .			
				createCookie("poll",$("#hndpid").val(),30);
				$("#_Pool").html("<div align='center'>please wait while loading...</div>");
				$.ajax({
					url: purl,
					method:'get',
					type:'html',
					success:function(args){
						$("#_Pool").html(args);
						$("#pooltbl").fadeIn(300);
						$("#View_result").html("Back"); // change text of view result link
					}
				});
			});
		}
	}
	function viewResult(){
		var purl = "include/pollvote.php?result&pid=" + $("#hndpid").val() ;
		$("#pooltbl").fadeOut(300,function(){
			$("#_Pool").html("<div align='center'>please wait while loading...</div>");
			$.ajax({
					url: purl,
					method:'get',
					type:'html',
					success:function(args){
						$("#_Pool").html(args);
						$("#pooltbl").fadeIn(300);
					}
			});
		});
	}
	function viewFrom(){
	var purl = "include/pollvote.php?form&pid=" + $("#hndpid").val() ;
		$("#pooltbl").fadeOut(300,function(){
			$.ajax({
					url: purl,
					method:'get',
					type:'html',
					success:function(args){
						$("#_Pool").fadeIn(300);
						$("#_Pool").html(args);
					}
			});
		});
	} // change text of view result link
	/*function viewPoolResult(o){
		if(o.innerHTML == "View Results"){
			o.innerHTML = "Back";
			viewResult();
		}else{
			o.innerHTML = "View Results";
			viewFrom();
		}
	}*/
// ======================  Cookie  =====================
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}


