/**
 * Login javascript for video channel.
 *
 * Some of this script was partially ported from the Charter login script.
 */

/**
 * AVS overridable login api URL
 *
 * @var string
 */
var vpjs_login_api_url = 'video_login.php';

var vpjs_disable_login = false;


/**
 * Retrieve width of the viewport.
 *
 * @return int
 */
function vpjs_viewport_width()
{
	return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}



/**
 * Retrieve height of the viewport.
 *
 * @return int
 */
function vpjs_viewport_height()
{
	return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
}



/**
 * Retrieve the complete height of the page.
 *
 * @return int
 */
function vpjs_scrollsize_height()
{
	return Math.max(document.body.scrollHeight, document.body.offsetHeight);
}



/**
 * Retrieve the complete width of the page.
 *
 * @return int
 */
function vpjs_scrollsize_width()
{
	return self.scrollWidth || (document.documentElement.scrollWidth || document.body.scrollWidth);
}



/**
 * Calculate how for the page has scrolled down.
 *
 * @return int
 */
function vpjs_my_scroll_top()
{
	return self.scrollTop || (document.documentElement.scrollTop || document.body.scrollTop);
}



/**
 * Retrieve an object containing the width and height of the viewport.
 *
 * @return object
 */
function vpjs_viewport_size()
{
	return { width: vpjs_viewport_width(), height: vpjs_viewport_height() };
}



/**
 * Hide windows media player.
 *
 */
function vpjs_hide_naughty_players()
{
	pldv_hide_immediate();
	var players = new Array('wmp_vid_player', 'pldv_player');

	for (var i = 0; i < players.length; i++)
	{
		var oplayers = document.getElementById(players[i]);
		if (oplayers)
		{
			oplayers.style.visibility='hidden';
		}
	}
}



/**
 * Show windows media player.
 *
 */
function vpjs_reveal_naughty_players()
{
	pldv_show_immediate();
	var players = new Array('wmp_vid_player', 'pldv_player');
		
	for (var i = 0; i < players.length; i++)
	{
		var oplayers = document.getElementById(players[i]);
		if (oplayers)
		{
			oplayers.style.visibility='visible';
		}
	}
}



/**
 * Hide embed tags.
 *
 */
function vpjs_hide_naughty_tags()
{
	var types = ['embed'];

	for (var i=0;i<types.length;i++)
	{
		var tags = document.getElementsByTagName(types[i]);
		for (var j=0;j<tags.length;j++)
		{
			var tag = tags[j];
			if (tag.style.visibility == 'visible')
			{
				tag.style.visibility = 'hidden';
				tag.setAttribute('forced_hidden',true);
			}
		}
	}
}



/**
 * Reveal embed tags.
 *
 */
function vpjs_reveal_naughty_tags()
{
	var types = ['embed'];

	for (var i=0;i<types.length;i++)
	{
		var tags = document.getElementsByTagName(types[i]);
		for (var j=0;j<tags.length;j++)
		{
			var tag = tags[j];
			if (tag.getAttribute('forced_hidden'))
			{
				tag.style.visibility = 'visible';
				tag.setAttribute('forced_hidden',false);
			}
		}
	}
}



/**
 * Hide the log in window
 *
 */
function vpjs_hide_login()
{
	var loginCont = document.getElementById('vich_login');
	var modalCover = document.getElementById('vich_login_cover');

	vpjs_reveal_naughty_players();
	vpjs_reveal_naughty_tags();

	loginCont.style.display  = 'none';
	modalCover.style.display = 'none';

	if (!flpl_username)
	{
		flpl_login_status = "failed";
	}
}



/**
 * Show the login window.
 *
 */
function vpjs_show_login()
{
	if (vpjs_disable_login)
	{
		return;
	}

	flpl_login_status = null;
	var vW = vpjs_viewport_width();
	var vH = vpjs_viewport_height();
	var vS = vpjs_viewport_size();
	var sH = vpjs_scrollsize_height();
	var sW = vpjs_scrollsize_width();
	var sT = vpjs_my_scroll_top();
	var loginCont = document.getElementById('vich_login');
	var modalCover = document.getElementById('vich_login_cover');
	var closeLogin = document.getElementById('vich_close_login');
	closeLogin.onclick = vpjs_hide_login;

	window.onscroll = window.onresize = function (e)
	{
		var Client =
		{
			myScrollTop: function()
			{
				return self.scrollTop || (document.documentElement.scrollTop || document.body.scrollTop);
			}
		};

		var sT = Client.myScrollTop();
		loginCont.style.top = ((vH - 295)/2) + sT + 'px';
	};

	vpjs_hide_naughty_players();
	vpjs_hide_naughty_tags();

	document.getElementById('vich_login_error').innerHTML = '';
	document.getElementById('vich_login_error').style.display = 'none';
	document.getElementById('vich_login_password').value = '';

	loginCont.style.clear = 'all';
	loginCont.style.left = (vW - 227) / 2 + 'px';
	loginCont.style.top = (vH - 295) / 2 + 'px';
	loginCont.style.display = 'block';

	if (sH < vH)
	{
		modalCover.style.height = vH + 'px';
	}
	else
	{
		modalCover.style.height = sH + 'px';
	}

	// -----------------------------------------------------------------------------------

	/** 
	 * Returns array with page width, height and window width, height
	 * Core code from - quirksmode.org
	 * Edit for Firefox by pHaez
	 * @return array
	 */
	function vpjs_get_page_size()
	{
		var xScroll, yScroll;
		var pageWidth, pageHeight;
		
		if (window.innerHeight && window.scrollMaxY)
		{
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight)
		{ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else
		{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight)
		{ // all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) 
		{ // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body)
		{ // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if (yScroll < windowHeight)
		{
			pageHeight = windowHeight;
		}
		else
		{
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if (xScroll < windowWidth)
		{
			pageWidth = windowWidth;
		}
		else
		{
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}

	// -----------------------------------------------------------------------------------

	var arrayPageSize = vpjs_get_page_size();
	
	if (navigator.userAgent.indexOf("MSIE 6") > 0)
	{
		modalCover.style.width = arrayPageSize[0] + 'px';
	}
	else
	{
		modalCover.style.width = 100 + '%';
		modalCover.style.minWidth = 968 + 'px';
	}

	modalCover.style.position = 'absolute';
	modalCover.style.top = '0px';
	modalCover.style.left = '0px';
	modalCover.style.display = 'block';
	modalCover.style.overflow = 'hidden';

	loginCont.style.display = 'block';
	document.getElementById('vich_login_username').focus();
}



/**
 * Attempt to log in.
 *
 * @return bool
 */
function vpjs_try_to_login()
{
	flpl_username = null;
	document.getElementById('vich_login_error').style.display = 'none';
	var username = document.getElementById('vich_login_username').value;
	var password = document.getElementById('vich_login_password').value;

	if (username.length == 0 || password.length == 0)
	{
		vpjs_login_error('Please enter both your username and password');
		return false;
	}
	
    new SynAjax().request(
    	vpjs_login_api_url,
		{
			method: 'post',
			onComplete: vpjs_login_complete,
			parameters: 'username=' + escape(username) + '&password=' + escape(password)
		} 
	);

	return true;
}



/**
 * Handle the status of the login attempt.
 *
 * @param resource req
 */
function vpjs_login_complete(req)
{
	var responseText = new String(req.responseText);
	if (responseText == 0)
	{
		flpl_username = null;
		document.getElementById('vich_login_password').value = '';
		vpjs_login_error('Incorrect username and/or password');
	}
	else if (responseText.match(/^1:\b(?!\.)[\w!#$%&\'*+\-\/=?^_`{|}~.]{2,}[\w!#$%&\'*+\-\/=?^_`{|}~](?:@(?!\.)[\w!#$%&\'*+\-\/=?^_`{|}~.]{2,}[\w!#$%&\'*+\-\/=?^_`{|}~])?$/i)) 
	{
		flpl_username = responseText.substr(2);
		vpjs_hide_login();
		flpl_login_status = "success";
	}
	else
	{
		flpl_username = null;
		document.getElementById('vich_login_password').value = '';
		vpjs_login_error('Error: Please try again later');
	}
}



/**
 * Display an error in the log in div.
 *
 * @param string error_str
 */
function vpjs_login_error(error_str)
{
	document.getElementById('vich_login_error').style.display = 'block';
	document.getElementById('vich_login_error').innerHTML = error_str;
}
