//global variables to hold from and to number to get videos from cached list


var fromNum=0;
//index so 0-4 means 5 videos
var toNum=9;
var timer;
var autoReload="true";
var  intervalId=0 ;

$(document).ready(function() 
		{		
			if ($.cookie('showCurrentWatch')=="yes")
				showcw();	
			else		
				hidecw();
			
			
		});
		
		
		function showcw()
		{	
				//alert("sfd");
if(intervalId>0)
	clearInterval( intervalId );
				
				autoReload="true";
				
				$("div.watching").show("slow");
callCurWatchOnce(fromNum,toNum);
				//set cookie which expires in 120 days				
				$("a.hideClass").empty().append("Hide");	
				$("a.showClass").empty();
				
				 $.cookie('showCurrentWatch', 'yes', { expires: 120 }); 

				

intervalId=setInterval("getCurrentlyWatching(fromNum,toNum)",120000);
				
				
			
		}
		function hidecw()
		{
			if(intervalId>0)
				clearInterval ( intervalId );
autoReload="false";
			$("div.watching").hide("slow");	
			$("a.showClass").empty().append("Show me Currently being Watched videos");	
			$("a.hideClass").empty();	
			
			//$("a.hideClass").html.empty();
			//set cookie value to no and store it for next 120 days
			$.cookie('showCurrentWatch', 'no', { expires: 120 }); 		
		}
		


function showOrigImage(origImg,imgId)
{
	
	document.getElementById(imgId).src=origImg;
	

}


function callCurWatchOnce(fromNum,toNum)
{
//this function created to get data and show when ever user clicked show,otherwise we need to wait until timer ticks
$("div#currentlyWatching").empty().append("Loading...");
$.ajax({
			url:"../ajax/currentlyWatching.aspx",
			data:{fromNum:fromNum,toNum:toNum},
			success:function(data){
				$("div#currentlyWatching").empty().append(data);
if (document.getElementById('username') != null)
				{
					
					//document.getElementById('username').focus();			

	
					//$("input#username").focus();
					
				}
		}
		});
}


function getCurrentlyWatching(fromNum,toNum)
{
	if (autoReload=="true")
	{
		$("div#currentlyWatching").empty().append("Loading...");
		$.ajax({
			url:"../ajax/currentlyWatching.aspx",
			data:{fromNum:fromNum,toNum:toNum},
			success:function(data){
				if (data!="")
				{
					
					$("div#currentlyWatching").empty().append(data);
if (document.getElementById('username') != null)
				{
					
					//document.getElementById('username').focus();	
					//$("input#username").focus();
					
				}					
				}
				else
					$("div#currentlyWatching").empty().append("Sorry..Please visit after few minutes.");
					
		}
		});
	}
}

function logout()
{
	$.ajax({
			url:"../logout.aspx",
			data:{dummy:'dummy'},
			success:function(data){
			hidecw();	
					
		}
		});
}


function validateLogin()
{
//function to validdate login details submitted from currently watcing box 

//if validation successful then  show currently watching

//set cookie --later you can delete when deleting cookies functionality
//alert(toNum);
//$.cookie('showCurrentWatch', 'yes', { expires: 120 }); 

var un=document.getElementById("username").value;
var ps=document.getElementById("password").value;

$.ajax({
			url:"../youtube/login.aspx",
			data:{username:un,password:ps},
			success:function(data){
					//getCurrentlyWatching(fromNum,toNum);
					if (data=="success")
					{
						callCurWatchOnce(fromNum,toNum);
						getCurrentlyWatching(fromNum,toNum);
					}
					else
					{
						//dont show
						//$("input#btnSubmit").append("<br><b>Sorry. Please try again.</b>");
						
						$("div.msgError").empty().append("<div class=\"bottomSmall\"><b style=\"color:red;\">Sorry. Wrong Username or Password or not activated.</b></div>");
					}
					
										
		}
		});

}

function dontAcceptEnter()
{
	if (window.event.keyCode == 13) 
	{
		event.returnValue=false; 
		event.cancel = true;
	}
}

/*
$(document).ready(function() 
		{		
			//first check if cookie exists
			if ($.cookie('showCurrentWatch')!=null)
			{
				//cookie exists
				///now see value is yes or no
				if ($.cookie('showCurrentWatch')=="yes")
				{
					showcw();					
				} 
				else if ($.cookie('showCurrentWatch')=="no")
				{					
					hidecw();
				}
			}
			else
			{
				//no cookie at all, so we need to show
				showcw();
			}
			
		});
*/








function getVideo(videoId)
{
	//$("div#youtube").empty().append("Loading..");
	$.ajax({
		url:"http://www.saradaga.com/ajax/video.aspx",
		data:{vid:videoId},
		success:function(data){
			$("div#youtube").empty().append(data);
	}
	});	
	
}


function rotate(thumb)
{
	var imageurl = thumb.src;
//alert(thumb.src);
	thumb.src = imageurl.replace("/2.jpg", "/1.jpg");

	setTimeout(function() {
			thumb.src = imageurl.replace("/2.jpg", "/3.jpg");
	}, 1000);

}
