/*ajax.js*/
var _AjaxRtnVal = ""; /*
 * Global variable to return response text from the
 * server
 */
/**
* sURL : Remote page URL. sData : URL parameters["name=John&location=Boston"].
*	sErrorMsg		:	Error message to be displayed on failure.
*/
function callAjax(sURL, sData, sErrorMsg) {
//alert("DOM Ready!");
if (typeof sURL == "undefined" || sURL == null || sURL == "") {
alert("Please specify the remote page URL");
return;
}
if (typeof sData == "undefined" || sData == null || sData == "") {
sData="";
}		

$.ajax({
type: "POST",
url: sURL,
data: sData,
async:false,
error : function() {
	alert(sErrorMsg);
},
success: function(sMsg){
	_AjaxRtnVal=sMsg;
}
});
}
function callVAjax(sURL, sData, sErrorMsg, sContrID, sBusyIconID) {
var contrID = document.getElementById(sContrID);
var busyIconID=document.getElementById(sBusyIconID);
if (contrID != null) {

if (typeof sURL == "undefined" || sURL == null || sURL == "") {
alert("Please specify the remote page URL");
return;
}
if (typeof sData == "undefined" || sData == null || sData == "") {
sData = "";
}
//busyIconID.style.display="block";
showBusyIcon();
$.ajax( {
type : "GET",
url : sURL,
data : sData,
async : true,
cache: false,
dataType:"html",
error : function() {
alert(sErrorMsg);
},
success : function(sMsg) {
//console.log(sMsg);
contrID.innerHTML=sMsg;
//busyIconID.style.display="none";
hideBusyIcon();
}
});
}
}
function callAsyncAjax(sURL, sData, sErrorMsg, sContrID) {
var contrID = document.getElementById(sContrID);
if (contrID != null) {

if (typeof sURL == "undefined" || sURL == null || sURL == "") {
alert("Please specify the remote page URL");
return;
}
if (typeof sData == "undefined" || sData == null || sData == "") {
sData = "";
}
//busyIconID.style.display="block";
showBusyIcon();
$.ajax( {
type : "GET",
url : sURL,
data : sData,
async : true,
cache: false,
dataType:"html",
error : function() {
alert(sErrorMsg);
},
success : function(sMsg) {
contrID.innerHTML=sMsg;
hideBusyIcon();
}
});
}
}
function showBusyIcon()
{
//setOpacity(2,"show");	
load();
if(document.getElementById('busyIcon')!=null)
{
document.getElementById('busyIcon').style.display="block";
}
}
function hideBusyIcon()
{
if(document.getElementById('busyIcon')!=null)
{
document.getElementById('busyIcon').style.display="none";
}
//setOpacity(10);
}
function load()
{
$("#busyIcon").remove();	
var div=document.createElement("div");
div.setAttribute("id","busyIcon");
div.style.cssText="position:absolute;top:85%;left:32%;";
div.innerHTML="<img id='img' src='./images/ajax-loader.gif' border='0'/>";
document.body.appendChild(div);			 
}
function setOpacity( value,type ) {
document.body.style.opacity = value / 10;
document.body.style.filter = 'alpha(opacity=' + value * 10 + ')';
if(type==null)
{
$("#busyIcon").remove();
}
else
{
$("#busyIcon").remove();
var div=document.createElement("div");
div.setAttribute("id","busyIcon");
div.style.cssText="position:absolute;top:55%;left:40%;opacity:1.0;filter:alpha(opacity=100);";
div.innerHTML="<img id='img' src='./images/ajax-loader.gif' border='0' style='z-index:100001;'/>";
document.body.appendChild(div);		 
}

}
var start,end;
function start()
{
start = new Date().getTime();
}
function end()
{
end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);	
}
/*end ajax.js*/
/*asynctabloading.js*/
var flagC=false;
var flagL=false;
var flagM=false;
var paginationStr="";

function tabClick(tabChar,context)
{
	var sURL="";
	var sData="";
	var sErrorMsg="";
	var sBusyIconID="busyIcon";
	var sContrID="";
	
	if(tabChar=='F')
	{
		sURL="";
		sData="";
		sErrorMsg="";
		sContrID="fragment-index1";		
		toggleTab(sContrID);
	}
	else if(tabChar=='C')
	{
		if(!flagC)
		{
			sURL=context+"/Home.htm";
			sData="TAB_NAME=C";
			sErrorMsg="Error! getting comming soon category of videos.";
			sContrID="fragment-index2";	
			toggleTab(sContrID);
			flagC=true;
			paginationStr="trailer";
		}
		else
		{
			sContrID="fragment-index2";	
			toggleTab(sContrID);			
			return;
		}
	}
	else if(tabChar=='L')
	{
		if(!flagL)
		{
			sURL=context+"/Home.htm";
			sData="TAB_NAME=L";
			sErrorMsg="Error! getting Latest videos.";
			sContrID="fragment-index3";	
			toggleTab(sContrID);
			flagL=true;
			paginationStr="popClips";
		}
		else
		{
			sContrID="fragment-index3";	
			toggleTab(sContrID);			
			return;
		}
		
	}
	else if(tabChar=='M')
	{
		if(!flagM)
		{
			sURL=context+"/Home.htm";
			sData="TAB_NAME=M";
			sErrorMsg="Error! getting Music videos.";
			sContrID="fragment-index4";	
			toggleTab(sContrID);
			flagM=true;
			paginationStr="popSong";
		}
		else
		{
			sContrID="fragment-index4";	
			toggleTab(sContrID);			
			return;
		}

	}	
	if(tabChar!='F')
	{

		callVAjax(sURL, sData, sErrorMsg, sContrID, sBusyIconID);
	}
}

function toggleTab(control_to_show)
{
	var map={
		'featuredv': 'fragment-index1',
		'latestdv' : 'fragment-index3',
		'mostpopdv' : 'fragment-index4',
		'comingdv' : 'fragment-index2'
	};
	$.each(map, function(key, value) { 
		if(value==control_to_show)
		{
			//alert("control_to_show:"+control_to_show+",Key:"+key+",value:"+value);			
			$("#"+control_to_show).css("display","block");
			$("#"+key).css("background-image","url('./images/cs-tab-selected.jpg')");
				$("#"+key).find("a").css("color","white");
		}
		else
		{
			$("#"+value).css("display","none");
			if(key=='comingdv')
			{
				$("#"+key).css("background-image","url('./images/tab-selected.jpg')");		
				$("#"+key).find("a").css("color","red");
			}
			else
			{
				$("#"+key).css("background-image","url('./images/tab-selected.jpg')");	
				$("#"+key).find("a").css("color","black");
			}
		}
	});
}
function toggleCompetitorTab(control_to_show)
{
	var map={
		'featuredv': 'fragment-video1',
		'latestdv' : 'fragment-video2',
		'mostpopdv' : 'fragment-video3'
	};
	$.each(map, function(key, value) { 
		if(value==control_to_show)
		{
		
			$("#"+control_to_show).css("display","block");
			$("#"+key).css("background-image","url('./images/cs-tab-selected-grey.jpg')");
		}
		else
		{
			$("#"+value).css("display","none");
			$("#"+key).css("background-image","url('./images/tab-selected.jpg')");				
		}
	});
}
/*end asynctabloading.js*/

/*login_page.js*/
function login0()
{
	document.getElementById('login_table1').style.display = 'none';
	document.getElementById('login_table2').style.display = '';
	return;
}
function cancel0()
{
	disable_loginpwd();
	document.getElementById('login_table2').style.display = 'none';
	document.getElementById('login_table100').style.display = 'none';
	document.getElementById('login_table101').style.display = '';
	document.getElementById('login_table1').style.display = '';
	return;
}
function enable_login()
{
	enable_login_pwd();
	document.getElementById('j_username').focus();
	return;
}
function enable_pwd()
{
	enable_login_pwd();
	document.getElementById('j_password').focus();
	return;
}
function enable_login_pwd()
{
	document.getElementById('username').style.display = '';
	document.getElementById('password').style.display = '';
	document.getElementById('dummy_username').style.display = 'none';
	document.getElementById('dummy_password').style.display = 'none';
	document.getElementById('j_username').value = '';
	document.getElementById('j_password').value = '';
}
function disable_loginpwd()
{
	document.getElementById('username').style.display = 'none';
	document.getElementById('password').style.display = 'none';
	document.getElementById('dummy_username').style.display = '';
	document.getElementById('dummy_password').style.display = '';
	return;
}
/*end login_page.js*/
/*submitFormEntr.js*/
function entsub(myform) 
{
  if (window.event && window.event.keyCode == 13)
    myform.submit();
  else
    return true;
}

function checkEnter(e, obj)
{ 
	//e is event object passed from function invocation
	var characterCode; 
	//literal character code will be stored in this variable
	if(e && e.which)
	{ 
		//if which property of event object is supported (NN4)
		e = e
		characterCode = e.which 
		//character code is contained in NN4's which property
	}
	else
	{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)
	{
		//if generated character code is equal to ascii 13 (if enter key)
		obj.submit() 
		//submit the form
		return false
	}
	else{
		return true
	}

}
/*submitFormEntr.js*/

/*favourites.js*/

function getTimestamp()
{
	return new Date().getTime();
}
function isAllowedToAdd()
{
	if(USER_SOCIAL_SESSION!="F" && USER_SOCIAL_SESSION!="T" && USER_SOCIAL_SESSION!="N" && USER_SOCIAL_SESSION!="M")
	{
		return true;
	}
	return false;
}
function submitFavForm(sURL,sVID)
	{
		if(USER_POJO_SESSION=="null")
		{
			alert(ADD_TO_FAV_LOGIN_WARNING_MSG);
			return false;
		}
		else
		{
			showBusyIcon();
			var sURL=context+"/AddVideoToPlaylist";
			var sData="uriReqqq="+sURL+"&v="+sVID;
			var sErrorMsg="Error! Can not add video to favourite list.";	
			var callType=(isAllowedToAdd()==true)?false:true;
			$.ajax( {
				type : "GET",
				url : sURL,
				data : sData,
				async : callType,
				cache: false,
				error : function() {
					alert(sErrorMsg);
				},
				success : function(sMsg) {
					hideBusyIcon();
				}
			});				
			hideBusyIcon();
			if(isAllowedToAdd()==true)
			{
				showBusyIcon();
				 $.ajax({
					   type: "GET",
					   url: context+"/ReloadVideosAjax",
					   data: "rand="+getTimestamp(),
					   async:true,
					   error: function()
					   {
					   		alert("Error! Could not retrieve favourite list.");
					   },
					   success: function(data){
						document.getElementById('mnucntnr').innerHTML="";
						document.getElementById('mnucntnr').innerHTML=data;
						loaded();loadInfoHover();star_loadStars1();showRightSideRatings();adjust_rightside_height();
						hideBusyIcon();
					   }
				});					
			}
			if(isAllowedToAdd()==false)
			{
				alert("Congratulations! Video added to your favourite list.");					
			}
			return false;				
		}
		}
function submitFavFormV(sURL,sVID)
{
	if(USER_POJO_SESSION=="null")
	{
		alert(ADD_TO_FAV_LOGIN_WARNING_MSG);
		return false;
	}
	showBusyIcon();
	var sURL=context+"/AddVideoToPlaylist";
	var sData="uriReqqq="+sURL+"&v="+sVID; 
	var sErrorMsg="Error! Can not add video to favourite list.";	
	callAjax(sURL,sData,sErrorMsg);	
	hideBusyIcon();
	
	if(isAllowedToAdd()==true)
	{
		refreshFavDiv();			
	}

	if(isAllowedToAdd()==false)
	{
		alert("Congratulations! Video added to your favourite list.");					
	}
	return false;
}

function refreshFavDiv()
{
	 showBusyIcon();
	 $.ajax({
		   type: "GET",
		   url: context+"/ReloadVideosAjax",
		   data: "rand="+getTimestamp(),
		   async:true,
		   error: function()
		   {
		   		alert("Error! Could not retrieve favourite list.");
		   },
		   success: function(data){
			document.getElementById('mnucntnr').innerHTML="";
			document.getElementById('mnucntnr').innerHTML=data;
			var fav=document.getElementById('favorite');
			var tRows=fav.getElementsByTagName("tr");
			
			for(var k=0;k<20;k++)
			{
				var cssVal=tRows[k].style.display;
				if(cssVal!=null && typeof cssVal!="undefined")
				{
					tRows[k].style.display="table-row";
				}
			}		
			var fav=document.getElementById('recent_videos');
			var tRows=fav.getElementsByTagName("tr");
			
			for(var k=0;k<15;k++)
			{
				var cssVal=tRows[k].style.display;
				if(cssVal!=null && typeof cssVal!="undefined")
				{
					tRows[k].style.display="table-row";
				}
			}			
			document.getElementById('favbusy').style.display="none";
			loadInfoHover();star_loadStars1();showRightSideRatings();adjust_rightside_height();
			hideBusyIcon();
		   }
	});			
}				

function postComment()
{
	var commentTextValue=$.trim(document.getElementById("commentText").value);
	if(commentTextValue!="")
	{
		var rewIdValue=document.getElementById("rewId").value;
		
        $.ajax({
    		url: context+"/PostUserVideoComment",
            cache: false,
            data: "commentText="+commentTextValue+"&rewId="+rewIdValue+"&uriReqq="+window.location,
            dataType: "json",
            type: "post",
            success: function( returnData, textStatus )
            {	if(textStatus=="success")
				{
					var postedDate=returnData.Posted;
					var user=returnData.User;
					
					var html="<strong class='hdng1'>Posted On: "+postedDate+"</strong><br />";
	            	html+=commentTextValue+"<br />";
					html+="<b>Posted By: </b><span class='box_sub_headline1'><u>"+user+"</u></span><br /><br />";
					var dvComments=document.getElementById('dvComments');
					if(dvComments!=null)
					{
						$("#dvComments").append(html);						
					}
					else
					{
						$("#spnNotCommented").remove();
						var dvComments=document.createElement("div");
						dvComments.setAttribute("id","dvComments");
						dvComments.innerHTML=html;
						//document.getElementById('fragment-video2').appendChild(dvComments);
						$("#fragment-video2").prepend(dvComments);
					}
					document.getElementById("commentText").value='';
					alert("Congratulation! Comment posted.");
				}
            },
            error: function( returnData, textStatus, ex )
            {
                alert("Error posting comment! Root Cause :"+textStatus);
            }
        });			
	}
	else
	{
		alert("Please enter your comment.");
	}
    return false;
}
function getNextPage(sURL)
{
    callAjax(sURL,"","");
    document.getElementById("fragment-video3").innerHTML="";
    document.getElementById("fragment-video3").innerHTML=$(_AjaxRtnVal).find("#fragment-video3").html();
    loaded();
}

/*end favourites.js*/