// Ensure we're working with a relatively standards compliant user agent
function isBrowserCompliant()
{
	return document.getElementById && document.createElement && document.createTextNode;
}


function initFields()
{
	var items = document.getElementsByTagName("input");
	for(var i = 0; i < items.length; i++)
	{
		if(items[i].value && (items[i].type == "text" || items[i].type == "password"))
		{
			items[i].v = items[i].value;
			items[i].onfocus = function(){ if(this.value == this.v) this.value = ""; }
			items[i].onblur = function(){ if(this.value == "") this.value = this.v; }
		}
	}
	
	var items = document.getElementsByTagName("textarea");
	for(var i = 0; i < items.length; i++)
	{
		items[i].v = items[i].innerHTML;
		items[i].onfocus = function(){ if(this.innerHTML == this.v) this.innerHTML = ""; }
		items[i].onblur = function(){ if(this.innerHTML == "") this.innerHTML = this.v; }
	}
}


contents = Array();
links = Array();
function initTabs( items, holder )
{
	if ( document.getElementById(holder) == null )
	{
		return false;
	}

	var holders = document.getElementById(holder).getElementsByTagName("div");
	for(var i = 0; i < holders.length; i++)
	{
		if(holders[i].className == "home")
		{
			contents.push(holders[i]);
			if( i > 0 ) holders[i].style.display = "none";
		}
	}
	
	if( contents.length > 1 )
	{
		var items = document.getElementById(items).getElementsByTagName("a");
		for(var i = 0; i < items.length; i++)
		{
			links.push(items[i]);
			items[i].value = i;
			items[i].onclick = function()
			{
				for(var j = 0; j < links.length; j++)
				{
					links[j].className = "";
					if( j == this.value )
					{
						links[j].className = "active";
						document.getElementById('availabletitle').innerHTML = "We've got every film '" + links[j].innerHTML + "' available in the UK&hellip;</h2>";
					}
					contents[j].style.display = "none";
					if( j == this.value ) contents[j].style.display = "block";
				}
				return false;
			}
		}
		items[0].className = "active";
	}
}

function popupWin(url , allowScroll )
{
	var allowScroll =  ( allowScroll != null || allowScroll );
	window.open (url, "window","location=0,status=0,scrollbars="+ (allowScroll?"1":"0") +",resizable=1,width=700,height=640");
}

function popupWin2(url , allowScroll )
{
	var allowScroll =  ( allowScroll != null || allowScroll );
	window.open (url, "window","location=0,status=0,scrollbars="+ (allowScroll?"1":"0") +",resizable=1,width=1100,height=640");
}

/**
 * Reposition the Adverts on the site to their place holders 
 * @param AdID
 * @return
 */
function repositionAd( AdID )
{
	if( $('#' + AdID ) )
	{
		//var adContainer = $('#' + AdID ).offset();
		//var adContainerTop = adContainer.top;
		//var adContainerLeft = adContainer.left;
		var loader = document.getElementById(AdID + '_loader');
		if( document.getElementById( AdID ) )
		{
			document.getElementById(AdID).appendChild(loader);
			$('#' + AdID + '_loader').show();
		}
		//$('#' + AdID + '_loader').css({ position: 'absolute', top: adContainerTop, left: adContainerLeft, display: 'block', zIndex: 1000});
	}
}

function setInputFieldTextOnEvent(el, text)
{	
	el.focus(function(){
		if($(this).attr('value') == text)
		{
			$(this).attr('value', '');
		}
	});
	el.blur(function(){
		if($(this).attr('value') == '')
		{
			$(this).attr('value', text);
		}
	});
}

function showUserBlock(name, my_stuff_url, want_to_see, available_are)
{
	if( want_to_see == 0 )
	{
		$('#logindatablock-left').html(
                    '<a href="'
                    + my_stuff_url
                    + '">Add films to your wish list and we\'ll alert you when they become available.</a>'
                );
	}
	else
	{
		$('#logindatablock-left').html(
                    '<a href="'
                    + my_stuff_url
                    + '">Welcome back '
                    + name
                    + '. You want to see '
                    + want_to_see
                    + ' films of which '
                    + available_are 
                    +' are available</a>');
	}
	$('#logindatablock-right').html('<a href="#">'+name+'</a> | <a href="/core/auth/logout">Logout</a>');
}

function pickFormat()
{
	  $('a.format_picker').click(function(){
		  var li = $(this).closest('li');
		  var input = $(this).closest('li').find('input');
		  
		  if(li.hasClass('selected'))
		  {
			  li.removeClass('selected');
			  input.attr('value', '0');
		  }
		  else
		  {
			  li.addClass('selected');
			  input.attr('value', '1');
		  }
		  return false;
	  });
}

function pickAllFormats(num_formats_available)
{
	  $('a.format_picker_all').click(function(){
		  if(($('li.selected').length != num_formats_available))
		  {
			  $('li.selected').removeClass('selected');
			  $('input.formats_holder').attr('value', '0');  
		  }
		  $('a.format_picker').trigger('click');
		  return false;
	  });
}
