
window.addEvent('domready', function(){

	totPlylst(-1);

});

function shwPlylst(){
	cookieData  =unescape(readCookie('yourPls'));
	if (cookieData!='null') {
		myObject = eval('(' + cookieData + ')');
		if(myObject.length>0){
			for (i=0;i<myObject.length;i++)
			{
				add_li(myObject[i]['id'],myObject[i]['name'])
			}
		}
	}
}
function add_li(id,name) {
if ( jQuery('#'+id).length ) {


} else {
	var list = jQuery('#curList');
	var li = document.createElement("li");
	var adel = document.createElement("a");
	var fname = name.replace(/\+/g, ' ');


	var t = jQuery("<li id='"+id+"'><a href='#' class='delete' onclick='rmpl("+id+")'>X</a><p><a href='http://zapaporn.com/videos/watch/"+escape(id)+"/' target='_blank'>"+fname+"</a></p></li>");
	list.append(t);
	//Delete icons on Alert Panel
	jQuery("#chatpanel li").hover(function() {
		jQuery(this).find("a.delete").css({'visibility': 'visible'}); //Show delete icon on hover
	},function() {
		jQuery(this).find("a.delete").css({'visibility': 'hidden'}); //Hide delete icon on hover out
	});
} 
}
function rmpl(id){
	jQuery('#'+id).remove();

	cookieData  =unescape(readCookie('yourPls'));
	var myObject = eval('(' + cookieData + ')');

var removeItem = id; 

	for(var i=0;i < myObject.length;i++) {
		if (myObject[i]['id'] == removeItem)
		{
			//alert(myObject[i]['id']+'->'+ removeItem)
			myObject.splice(i,1)
				
		}
	}
	out = myObject.toJSON();

Delete_Cookie('yourPls','/','.zapaporn.com')
Set_Cookie( 'yourPls', out, 30, '/', '.zapaporn.com', '' );

totPlylst(-1);

}

function totPlylst(req){

		cookieData  =unescape(readCookie('yourPls'));

		var myObject = eval('(' + cookieData + ')');
		if (myObject) {
			
		 tot = myObject.length;
		 }
		 else {
				
		 tot = 0;
		 }
		jQuery('#plLink').html(tot);
		shwPlylst();

}
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 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 eraseCookie(name) {
	alert(name)
	createCookie('yourPls',"",-1);
}
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function addpl(id,name,e){ 
	/* ajax replace element text */
		//make the ajax call, replace text
var myRequest = new Request({
	url: 'http://www.zapaporn.com/videos/addFav',
	onComplete: function(response) { totPlylst(response) }  
});

myRequest.send({
    method: 'post',
    data: 'id='+id+'&name='+name,
	
});

	}
jQuery(document).ready(function(){

	//Adjust panel height
	jQuery.fn.adjustPanel = function(){ 
		jQuery(this).find("ul, .subpanel").css({ 'height' : 'auto'}); //Reset subpanel and ul height
		
		var windowHeight = jQuery(window).height(); //Get the height of the browser viewport
		var panelsub = jQuery(this).find(".subpanel").height(); //Get the height of subpanel	
		var panelAdjust = windowHeight - 100; //Viewport height - 100px (Sets max height of subpanel)
		var ulAdjust =  panelAdjust - 25; //Calculate ul size after adjusting sub-panel (27px is the height of the base panel)
		
		if ( panelsub >= panelAdjust ) {	 //If subpanel is taller than max height...
			jQuery(this).find(".subpanel").css({ 'height' : panelAdjust }); //Adjust subpanel to max height
			jQuery(this).find("ul").css({ 'height' : ulAdjust}); //Adjust subpanel ul to new size
		}
		else if ( panelsub < panelAdjust ) { //If subpanel is smaller than max height...
			jQuery(this).find("ul").css({ 'height' : 'auto'}); //Set subpanel ul to auto (default size)
		}
	};
	
	//Execute function on load
	jQuery("#chatpanel").adjustPanel(); //Run the adjustPanel function on #chatpanel
	jQuery("#alertpanel").adjustPanel(); //Run the adjustPanel function on #alertpanel
	
	//Each time the viewport is adjusted/resized, execute the function
	jQuery(window).resize(function () { 
		jQuery("#chatpanel").adjustPanel();
		jQuery("#alertpanel").adjustPanel();
	});
	
	//Click event on Chat Panel + Alert Panel	
	jQuery("#chatpanel a:first, #alertpanel a:first").click(function() { //If clicked on the first link of #chatpanel and #alertpanel...
		if(jQuery(this).next(".subpanel").is(':visible')){ //If subpanel is already active...
			jQuery(this).next(".subpanel").hide(); //Hide active subpanel
			jQuery("#footpanel li a").removeClass('active'); //Remove active class on the subpanel trigger
		}
		else { //if subpanel is not active...
			jQuery(".subpanel").hide(); //Hide all subpanels
			shwPlylst();
			jQuery(this).next(".subpanel").toggle(); //Toggle the subpanel to make active
			jQuery("#footpanel li a").removeClass('active'); //Remove active class on all subpanel trigger
			jQuery(this).toggleClass('active'); //Toggle the active class on the subpanel trigger
		}
		return false; //Prevent browser jump to link anchor
	});
	
	//Click event outside of subpanel
	jQuery(document).click(function() { //Click anywhere and...
		jQuery(".subpanel").hide(); //hide subpanel
		jQuery("#footpanel li a").removeClass('active'); //remove active class on subpanel trigger
	});
	jQuery('.subpanel ul').click(function(e) { 
		e.stopPropagation(); //Prevents the subpanel ul from closing on click
	});
	
	//Delete icons on Alert Panel
	jQuery("#chatpanel li").hover(function() {
		jQuery(this).find("a.delete").css({'visibility': 'visible'}); //Show delete icon on hover
	},function() {
		jQuery(this).find("a.delete").css({'visibility': 'hidden'}); //Hide delete icon on hover out
	});





	
});
