/*******************************
 *
 *	Misc Class - Handy Stuff
 *
 *  Version: 1.1
 *
 *	Author: 
 *	The Roundhouse
 *
 *  © The Roundhouse 2007 -
 * 	ALL RIGHTS RESERVED
 */
  
if(!TheRoundhouse) 								var TheRoundhouse 							= new Object();
if(!TheRoundhouse.Frameworks) 					TheRoundhouse.Frameworks	 				= new Object();
if(!TheRoundhouse.Frameworks.Misc) 				TheRoundhouse.Frameworks.Misc				= function(){};

TheRoundhouse.Frameworks.__available			= function()
{
	return (typeof $ == "function");
}

TheRoundhouse.Frameworks.__validateOptions  	= function(objOptions, arrExpectedOptions)
{
	for(var i in objOptions)
		for(var j = 0; j < arrExpectedOptions.length; j++)
			if(i == arrExpectedOptions[j])
				arrExpectedOptions.splice(j,1);
				
	if(arrExpectedOptions.length)
		alert("Error: Missing options: "+arrExpectedOptions.join(", "));
	
	return (arrExpectedOptions.length == 0);
}


TheRoundhouse.Frameworks.Misc.prototype 	= {
			
	$get :function(key, url)
	{
		// Function by Jens Anders Bakke, webfreak.no
		// Returns URL query string information - use the swfobject API if available
		// NOTE: expect the same results as PHP $get
		
		if(arguments.length < 2) 					url 		= location.href;
		if(arguments.length > 0 && key != "")
		{
			if(key == "#")							var regex	= new RegExp("[#]([^$]*)");
			else if(key == "?") 					var regex 	= new RegExp("[?]([^#$]*)");
			else 									var regex 	= new RegExp("[?&]"+key+"=([^&#]*)");
			
			var results 							= regex.exec(url);
			return (results == null )? "" : results[1];
		} else 
		{
			url 									= url.split("?");
			var results 							= {};
			if(url.length > 1)
			{
				url 								= url[1].split("#");
				if(url.length > 1) results["hash"] 	= url[1];
				url[0].split("&").each(function(item,index)
				{
				item 								= item.split("=");
				results[item[0]] 					= item[1];
				});
		}
		return results;
		}
	},
	
	showHideElement :function(objCtrl, objMisc)
	{
		
		var objCollapsible								= objCtrl.objCollapsible;
		var objBtn										= objCtrl.objBtn;
		var bHorizontal									= (objMisc && objMisc.bHorizontal)? objMisc.bHorizontal : false;
		var iSlideDuration								= (objMisc && objMisc.iSlideDuration)? objMisc.iSlideDuration : 450;
		var fSlideType									= (objMisc && objMisc.fSlideType)? objMisc.fSlideType : Fx.Transitions.Quad.easeInOut;
		var bOpenClose									= (Cookie.read('bOpenClose')) ? Number(Cookie.read('bOpenClose')):0;
				
		var cSlide 										= new Fx.Slide(objCollapsible, 
		{
			duration: 		iSlideDuration,
			transition: 	fSlideType
		});
		
		// Set the intial collapsible state the first time through
		if (!bHorizontal && !bOpenClose)				cSlide.hide();
		else if (bHorizontal)							cSlide.hide('horizontal');
		
		objBtn.addEvent('click', function(event)
		{
			event 										= new Event(event).stop();
			// Toggle the Reveal of the collapsible element
			if (!bHorizontal)							cSlide.toggle();
			else 										cSlide.toggle('horizontal');
			// Set a cookie to remember what we've done 
			if (!bOpenClose)							Cookie.write('bOpenClose', 1, {duration: 1, domain: document.domain, path:'/'});
			else										Cookie.write('bOpenClose', 0, {duration: 1, domain: document.domain, path:'/'});
			

		});		
	}
};

function addTargetBlanks()
{
	var arrLinks = $$('a.targetblank');
	if(arrLinks)
	{
		for(var i = 0; i < arrLinks.length; i++)	
		{
			arrLinks[i].set('target', '_blank');
		}
	}
}

window.addEvent('domready', function()
							{
								if($('frm_register'))
								{
									$('dob_day').addEvent('change', function()
									{
										// deal with age restrictions
										//checkAge();
									});	
									
									$('dob_month').addEvent('change', function()
									{
										// when month changes check days
										updateCalendar('dob')
										
										// deal with age restrictions
										//checkAge();
									});
									
									$('dob_year').addEvent('change', function()
									{
										// when year changes check days
										updateCalendar('dob')
										
										// deal with age restrictions
										//checkAge();
									});	
									
									//checkAge();
								}
								
								if($('frm_entry'))
								{
									$('category').addEvent('change',function()
									{
										checkFilm();
									});
									checkFilm();
								}

								addTargetBlanks();
								initOverLabels();
							});
function checkAge()
{
	if(isUnder16())
	{
		$('li_parentname').removeClass('hidden');
		$('li_parentphone').removeClass('hidden');											
	}
	else
	{
		$('li_parentname').addClass('hidden');
		$('li_parentphone').addClass('hidden');																						
	}	
}

function checkFilm()
{
	var pitch = $('pitch').getParent();
	var browse = $('browse').getParent();
	
	//Reflect Film category ID
	if($('category').value == 11)
	{
		if(!browse.hasClass("hidden"))
			browse.addClass("hidden");
		if(pitch.hasClass("hidden"))
			pitch.removeClass("hidden");
	}
	else
	{
		if(!pitch.hasClass("hidden"))
			pitch.addClass("hidden");
		if(browse.hasClass("hidden"))
			browse.removeClass("hidden");
	}
}

function isUnder16()
{
	var bUnder16	= false;
	
	var dToday 		= new Date();
	var iThisDay	= dToday.getDate();
	var iThisMonth	= dToday.getMonth() + 1;
	var iThisYear	= dToday.getFullYear();	

	var iDateYear	= ($('dob_year') && $('dob_year').value	!= "year"		? parseInt($('dob_year').value)		: -1);
	var iDateMonth	= ($('dob_month') && $('dob_month').value != "month"	? parseInt($('dob_month').value)	: -1);
	var iDateDay	= ($('dob_day') && $('dob_day').value != "day"			? parseInt($('dob_day').value)		: -1);	
	
	if(iDateYear > -1 && iDateMonth > -1 && iDateDay > -1)
	{
		if((iDateYear + 17) < iThisYear)
		{
			bUnder16	= false;	
		}
		else if((iDateYear + 17) > iThisYear)
		{
			bUnder16	= true;	
		}
		else
		{
			// year is the same, so more checking is needed
			if(iDateMonth < iThisMonth)
			{
				bUnder16	= false;	
			}
			else if(iDateMonth > iThisMonth)
			{
				bUnder16	= true;	 		
			}
			else
			{
				// month is the same, so more checking is needed
				if(iDateDay <= iThisDay)
				{
					bUnder16	= false;	
				}
				else
				{
					bUnder16	= true;	 		
				}
			}
		}	
	}

	return bUnder16;
	/*{
		// force them into reflect youth
		$('cat_reflectyouth').selected = true;
		$('category').disabled = true;
	}
	else
	{
		if($('category').disabled == true)
		{
			$('category_title').selected = true;		
			$('category').disabled = '';
		}
	}*/
}

function updateCalendar(strCalendarID)
{
	if($(strCalendarID+'_day') &&
	   $(strCalendarID+'_month') &&
	   $(strCalendarID+'_year'))
	{
		var iDaySel     = $(strCalendarID+'_day').selectedIndex;
		var iMonthSel   = $(strCalendarID+'_month').selectedIndex;
		var iYearSel    = $(strCalendarID+'_year').selectedIndex;

		var iMonthVal   = $(strCalendarID+'_month').get('value');
		var iYearVal    = $(strCalendarID+'_year').get('value');
		var iDayMax     = 31;

		switch(iMonthVal)
		{
			case "9":
			case "4":
			case "6":
			case "11":
						iDayMax = 30;
						break;
			case "2":  iDayMax = (iYearVal % 4 == 0 ? 29 : 28);
						break;
		}

		// get all the calendar child nodes
		var arrDays = $(strCalendarID+'_day').getChildren();

		// remove them
		for(var i = 1; i < arrDays.length; i++)
			arrDays[i].dispose();

		// limit
		iDaySel = Math.min(iDaySel, iDayMax);

		// now create new ones
		for(var i = 1; i <= iDayMax; i++)
		{
			var optNew = new Element('option');
			optNew.set('html', (i<10?"0"+i:i));
			optNew.set('value', i);			
			$(strCalendarID+'_day').adopt(optNew);
		}

		$(strCalendarID+'_day').selectedIndex = iDaySel;
	}
}

function initOverLabels () {
	  if (!document.getElementById) return;
	  if(Cookie.read("TextOnly") == "Yes") return;

	  var labels, id, field;

	  // Set focus and blur handlers to hide and show 
	  // LABELs with 'overlabel' class names.
	  labels = document.getElementsByTagName('label');
	  for (var i = 0; i < labels.length; i++) {
		
	    if (labels[i].className == 'overlabel') {

	      // Skip labels that do not have a named association
	      // with another field.
	      id = labels[i].htmlFor || labels[i].getAttribute('for');
	      if (!id || !(field = document.getElementById(id))) {
	        continue;
	      }

	      // Change the applied class to hover the label 
	      // over the form field.
	      labels[i].className = 'overlabel-apply';

	      // Hide any fields having an initial value.
	      if (field.value !== '') {
	        hideLabel(field.getAttribute('id'), true);
	      }

	      // Set handlers to show and hide labels.
	      field.onfocus = function () {
	        hideLabel(this.getAttribute('id'), true);
	      };
	      field.onblur = function () {
	        if (this.value === '') {
	          hideLabel(this.getAttribute('id'), false);
	        }
	      };

	      // Handle clicks to LABEL elements (for Safari).
	      labels[i].onclick = function () {
	        var id, field;
	        id = this.getAttribute('for');
	        if (id && (field = document.getElementById(id))) {
	          field.focus();
	        }
	      };

	    }
	  }
	};

	function hideLabel (field_id, hide) {
	  var field_for;
	  var labels = document.getElementsByTagName('label');
	  for (var i = 0; i < labels.length; i++) {
	    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
	    if (field_for == field_id) {
	      labels[i].style.textIndent = (hide) ? '-6000px' : '0px';
		  labels[i].style.visibility = (hide) ? 'hidden' : 'visible';
	      return true;
	    }
	  }
	}