/**
* @author Jerome Conde <j.conde@impactbbdo.ae>
*/

$(function(){
	//global vars
	var locale = "en_gb", $pageWrap = $("div.PageWrap"), $page = $("div.Page");
	if(window.location.href.indexOf("ar_ae") > -1) locale = "ar_ae";

	var isIE = $.browser.msie;
	var isIE6 = isIE && ($.browser.version == 6.0);
	
	var swfEnabled = false;
	swfEnabled = typeof swfobject == "object" && swfobject.hasFlashPlayerVersion("8.0");
	
	if(typeof assetPath == "undefined") assetPath = "";
	
	//add 'FirstChild' and sfhover class for ie6 */
	var firstChild = function(){
		$("li:first-child").addClass("FirstChild");
	}
	var lastChild = function(){
		$("li:last-child").addClass("LastChild");
	}
	if($.browser.msie && $.browser.version == "6.0") {
		firstChild();
	}
	// add LastChild class to IE
	if($.browser.msie){
		lastChild();
	}
	
	//auto text handler
	$("div.Header input.Textbox,div.FAQSearch input.Textbox")
	.each(function(){
		var autoText = "";
		var defaultText = $(this).val();
		$(this).bind('focus',function(){
		autoText = $(this).val();
		if(autoText == defaultText) $(this).val("");
		})
		.bind('blur',function(){
			if($(this).val() == "")
			$(this).val(defaultText);
		});
	});
	
	//external links
	$("a[rel=external],a[rel=External]").click(function(){
		$(this).attr('target','_blank');
	});
	
	$('.ArielPanel,.DownyPanel').each(function(){
		var $me = $(this);
		var opt1 = {left:0}, opt2 = {left:-530};
		if($me.is('.DownyPanel')){
			opt1 = {right:0}, opt2 = {right:-530};
		}
		$me.find('span.ShowHide').toggle(
			function(){
				var $t = $(this);
				if($me.is('.DownyPanel') && $('.ArielPanel').is(':animated')) $('.ArielPanel span.ShowHide').click();
				if($me.is('.ArielPanel') && $('.DownyPanel').is(':animated')) $('.DownyPanel span.ShowHide').click();
				$me.animate(opt1,800,'easeOutCubic',function(){
					$t.addClass('ShowHideExpanded');
				});
				if($me.is('.ArielPanel') && $('.DownyPanel span.ShowHideExpanded').length){
					$('.DownyPanel span.ShowHideExpanded').click();
				}
				if($me.is('.DownyPanel') && $('.ArielPanel span.ShowHideExpanded').length){
					$('.ArielPanel span.ShowHideExpanded').click();
				}
				$me.find('a.GetSample').fadeOut(300);
				//$('div.Intro').css({zIndex:0});
			},
			function(){
				var $t = $(this);
				$me.animate(opt2,600,'easeOutCubic',function(){
					$t.removeClass('ShowHideExpanded');
				});
				$me.find('a.GetSample').fadeIn(300);
				//$('div.Intro').css({zIndex:11});
			}
		);
		$me.find('.Banner1, a.GetSample').click(function(){
			$me.find('.ShowHide').click();
			return false;
		});
	});
	
	// Form handler
	$('div.Form1 fieldset.YesNo').each(function(){
		var $me = $(this), $panel = $me.next('.OnDemand');
		var $radio = $me.find('input:radio');
		if($radio.is(':checked[value=Yes]')) $panel.show();
		$radio.click(function(){
			if($(this).val() == 'Yes') $panel.slideDown();
			else $panel.slideUp();
		});
	});
	$('div.Form1 .RadioGroup input:radio').bind('change',function(){
		$(this).parents('.Error').removeClass('Error');
	});
	$('div.Form1 select').change(function(){
		var $t = $(this);
		if($t.val() != '') $t.parents('fieldset').removeClass('Error');
		if($t.val() == 'Other'){
			$t.parent().next('.Other').slideDown();
		}
		else $t.parent().next('.Other').slideUp();
	}).each(function(){
		var $t = $(this);
		if($t.val() == 'Other') $t.parent().next('.Other').show();
	});
	$('div.Form1 input:text').blur(function(){
		if($(this).parents('fieldset').hasClass('Error') && $(this).val() != '') $(this).parents('fieldset').removeClass('Error');
		//if($(this).val() == '') $(this).parents('fieldset').addClass('Error');
		//else $(this).parents('fieldset').removeClass('Error');
	});
	var validateForm = function(){
		$('.Error').removeClass('Error');
		$('div.Form1 input:visible[type=text][value=""]:not(.NotRequired)').each(function(){
			$(this).parents('fieldset').addClass('Error');
		});
		$('div.Form1 select:visible option:selected:first-child').each(function(){
			$(this).parents('fieldset').addClass('Error');
		});
		$('div.Form1 .RadioGroup').each(function(){
			if(!$(this).find(':radio:checked').length) $(this).parent().addClass('Error');
		});
		$('div.Form1 p.Terms input:checkbox').each(function(){
			if(!$(this).is(':checked')) $(this).parents('p.Terms').addClass('Error');
		}).click(function(){
			if(!$(this).is(':checked')) $(this).parents('p.Terms').addClass('Error');
			else $(this).parents('p.Terms').removeClass('Error');
		});
		$('div.Form1 .Date').each(function(){
			var $f = $(this), $m = $('select.Month',$f), $d = $('select.Day',$f), $y = $('select.Year',$f);
			if(!validDate($m.val(),$d.val(),$y.val())) $f.addClass('Error');
		});
		$('div.Form1 input.Phone').each(function(){
			var $t = $(this);
			if(!isPhone($t.val())) $t.parents('fieldset').addClass('Error');
		});
		$('div.Form1 input.Email').each(function(){
			var $t = $(this);
			if(!isEmail($t.val())) $t.parents('fieldset').addClass('Error');
		});
		// check for invalid data/input
		if($('.Error:visible').length) {
			$('p.Info').addClass('Error');
			return false;
		}
		else return true;
		//else{
		//	showOverlay();
		//	return false;
		//};
	}
	
	function isLeapYear(year){return ((year%4 == 0) && (year%100 != 0 || year%400 == 0));}
	function readDaysInMonth(month,year){
		if (month == 1 && isLeapYear(year) == true) return 29;
		else return [31,28,31,30,31,30,31,31,30,31,30,31][month];
	}
	function validDate(m,d,y){return ((new Date(y,m,d)).getDate() == d)}

	$('fieldset.Date').each(function(){
		var $f = $(this), daysOpt = $('select.Day',$f).html();
		$('select.Month, select.Year, select.Day',$f).change(function(){
			var $m = $('select.Month',$f), $d = $('select.Day',$f), $y = $('select.Year',$f);
			var sel = $d.find('option:selected').val();
			var $o = $d.empty().append(daysOpt).find('option');
			var d = readDaysInMonth($m.val(),$y.val());
			if(sel > d) sel = d;
			$o.filter(':gt('+d+')').remove();
			$o.eq(sel).attr('selected','selected');
		});
	});
	
	$('fieldset.Action input.Submit').click(function(){
		return validateForm();
	});
	$('fieldset.Action input.Reset').click(function(){
		window.location = window.location.href;
		return false;
	});
	
	
	// fix for IE6 table
	if(isIE6){
		$('div.DownyPanel div.Comparison table tr:gt(0)').each(function(){
			$(this).find('td:eq(1)').wrapInner('<div />').find('div').css({width:100});
		});
	}
	
	// Overlay implementation
	var showOverlay = function(){
		var $overlay = $('#Overlay');
		if(isIE6) $('select').css({display:'none'});
		$('<div id="OverlayMask" />').css({opacity:.9}).insertBefore($overlay);
		$overlay.fadeIn(500);
	}
	var hideOverlay = function(){
		var $overlay = $('#Overlay');
		$overlay.fadeOut(500,function(){
			$('#OverlayMask').remove();
			if(isIE6) $('select').css({display:'inline'});
		});
	}
	$('#Overlay').find('a.Submit').click(function(){
		hideOverlay();
		return false;
	})
	.end().find('a.Cancel, span.Close').click(function(){
		hideOverlay();
		return false;
	});
	
	// T & C popup
	$('a[rel=popup_terms]').click(function(){
		window.open($(this).attr('href'),'Popup','width=520,height=800,scrollbars=1');
		return false;
	});
	
	globalShowOverlay = showOverlay;
});

//:: GLOBAL functions

var globalShowOverlay = null;
var ShowOverlay = function(){
	setTimeout(function(){globalShowOverlay()},1000);
};

// email validation
var isEmail = function(sEmail){
	return sEmail.match(/(^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)/gi);
}
var isPhone = function(pNumber){
	return pNumber.match(/(^(\+*\d{7,13})|(00\d{7,13})|(\+\d{2,3}\s*\d{1,2}\s*\d{5,7})|(\d{2,3}\-*\s*\d{7})$)/gi);
}
