/* Scroller Logic */
/*----------------*/
(function($) {
	$.fn.qCarousel = function(obj) {
			obj = $.extend({
				auto: null,
				speed: null,
				visible: null, /* Mandatory */
				slideBy: null, /* Mandatory */
				nextB: null,
				prevB: null,
				iWidth: null,
				height: null,
				align: null
			}, obj || {});
			
		return this.each(function() {
			var sTimer = null;
			var qDIV = $(this), qUL = qDIV.children(':first'), qLI = qUL.children(), qLength = qLI.length;
			var sBy = obj.slideBy;
			var toScroll = obj.visible<qLength;
			
			var vM = parseInt(qLI.css('margin-right').split('px')[0]);
			var qULw = (toScroll)?getVWidth(true):0;
			var vWindow = getVWidth(obj.visible*2);
			var sS = obj.speed?obj.speed:800;
			
			var sHTML = qUL.html();
			
			if(toScroll) qUL.children(':eq(0)').before(sHTML);
			
			qDIV.css({ overflow: "hidden", position: "relative", "z-index": "2", left: "0px", width: vWindow+'px'});
			qUL.css({ overflow: "hidden", margin: "0", padding: "0", position: "relative", left: 0-qULw, width: getVWidth(true)*2, "list-style-type": "none", "z-index": "1" });
			
			qUL.children().width(obj.iWidth);		
			
			qDIV.height(obj.height?obj.height:qUL.height());
			
			switch (obj.align)
			{
				case 'left': 
					qDIV.css({'float':'left',display:'inline',margin: "0 1em 1em 0"});
					break;
					
				case 'right': 
					qDIV.css({'float':'right',display:'inline',margin: "0 0 1em 1em"}); 
					break;
					
				default:
					qDIV.parent().addClass('clearit');
					break;
			}
			
			function getVWidth(num)
			{
				var tW = 0;
				var bW = obj.iWidth+vM;
				
				qLI.each(function()
				{
					tW += ($(this).width()+vM);
				});
				
				return num?(num==true?tW:(bW*(num/2))-vM):(bW*4)-vM;
			}
			
			
			function bindEv()
			{
				var cpos = parseInt(qUL.css('left').split('px')[0]);
				$(obj.prevB).bind('click', function(){
					if(sTimer)clearInterval(sTimer);
					move(cpos,'prev');
				});
				
				$(obj.nextB).bind('click', function(){
					if(sTimer)clearInterval(sTimer);
					move(cpos,'next');
				});
			}
			
			function unbindEv()
			{
				$(obj.prevB).unbind('click');
				$(obj.nextB).unbind('click');
			}
			
			function move(cpos,type)
			{
				if (toScroll)
				{
					var sAmount = (qLI.width()+vM)*sBy;
					var vWindowMargin = vWindow+vM;
					var npos = sBy?(type=='prev'?cpos+sAmount:cpos-sAmount):(type == 'prev'?npos=cpos+vWindowMargin:npos=cpos-vWindowMargin);
					
					unbindEv();
					
					qUL.animate({"left": npos},sS,function(){									   
						if (type=='prev')
						{
							if (((cpos*-1)-vWindowMargin == 0 && !sBy) || ((cpos*-1)-sAmount == 0 && sBy))
							{
								qUL.css('left',(0-qULw)+'px');
							}
						}
						else if (type=='next')
						{
							if (((cpos-vWindowMargin)*-1==qUL.width()-vWindowMargin && !sBy) || ((cpos*-1)==qUL.width()-(vWindowMargin+sAmount) && sBy))
							{
								qUL.css('left',(-qULw)+vWindowMargin+'px');
							}
						}
						bindEv();
					});
				}
			}
			
			if (toScroll)
			{
				bindEv();
				$(obj.prevB).removeClass('disabled');
				$(obj.nextB).removeClass('disabled');
				
				if (obj.auto)
				{
					sTimer = setInterval(function(){
						var cpos = parseInt(qUL.css('left').split('px')[0]);
						move(cpos, 'next')
					}, obj.auto);
				}
			}
			
			
		});
	};
})(jQuery);

/* Loads all the effects for promotions */
/* ------------------------------------ */

function initPromotions()
{
	setTimeout('renderPromotions()',500);
}

function renderPromotions()
{
	$j('div').filter('.promotion').children('object').each(function(i){							   
		var iHeight = $j(this).height();
		var iWidth = $j(this).width();
		var fromTop = ((iHeight-9)-$j(this).parent().find('span.title').height());
		
		promoEffects(i, iHeight, iWidth, fromTop, $j('div').filter('.promotion').children('object'));
	});
	
	var promoImage = $j('div').filter('.promotion').children('img');
	var promoLength = promoImage.length;
	var promoCount = 0;
	
	promoImage.each(function(i){
		if (promoImage[i].complete )
		{
			var iHeight = $j(this).height();
			var iWidth = $j(this).width();
			var fromTop = ((iHeight-9)-$j(this).parent().find('span.title').height());
	
			promoEffects(i, iHeight, iWidth, fromTop, promoImage);
			
			promoCount++;
			
			if (promoCount==promoLength) displayPromotions();
		}
		else
		{
			promoImage.load(
				function(){
					var iHeight = $j(this).height();
					var iWidth = $j(this).width();
					var fromTop = ((iHeight-9)-$j(this).parent().find('span.title').height());
					
					promoEffects(i, iHeight, iWidth, fromTop, promoImage);
					
					promoCount++;
					
					if (promoCount==promoLength) displayPromotions();
				}
			);
		}
	});
}

function promoEffects(index, iheight, iwidth, itop, obj)
{
	$j(obj[index]).prev('a').children('span').filter('.promoContent').css({'top':itop+'px','opacity':'0.9','width':iwidth});
	$j(obj[index]).prev('a').hover(
		function()
		{
			$j(this).children('span').filter('.promoContent').animate({"top": itop-($j(this).children('span').filter('.promoContent').children('.content').height()+6)});
		},
		function()
		{
			$j(this).children('span').filter('.promoContent').animate({"top": itop});
		}
	);
	
	$j(obj[index]).parent().width(iwidth).height(iheight);
}

function displayPromotions()
{
	$j('.promotionsBody').attr('style','position: static; left: auto;');
}
/* ------------------------------------ */

function qInit()
{	
	layoutTweaks();	
	/* Bind javascript methods to classes */
	$j('.gotoDes').click(gotoDes);
	
	$j('input[type=checkbox]').filter('.mandatoryCheck').each(loadMandatoryCheck);
	$j('input[type=checkbox]').filter('.mandatoryCheck').click(mandatoryCheck);
}

function mandatoryCheck()
{
	if($j(this).attr('checked'))
	{
		$j(this).prev().filter('.mandatoryNote').fadeOut();
	}
	else
	{
		$j(this).prev().filter('.mandatoryNote').fadeIn();
	}
}

function loadMandatoryCheck()
{
	$j(this).attr('checked',false);
	$j(this).before('<div class="mandatoryNote"><span class="arr_notice"></span><div class="mandatoryNoteTxt">'+$j(this).attr('title')+'</div></div>');
}

/* Handles Select Box Toggle that loads new window or new url */
/* ---------------------------------------------------------- */
function gotoDes()
{
	if ($j(this).prev().children('option:selected').val() != '' &&
		$j(this).prev().children('option:selected').val() != 'undefined' &&
		(($j(this).prev('select').children('option:selected').val().indexOf('--') == -1) ||
		($j(this).prev().children('option:selected').val().indexOf('-') == -1 && $j(this).prev().children('option:selected').val().indexOf('select') == -1)))
	{
		window.location.href = $j(this).prev().children('option:selected').val();
	}
}
/* ---------------------------------------------------------- */

/* Handles the inclusion external stylesheets into the head */
/* -------------------------------------------------------- */
function includeCSS(file)
{
	$j('head').append('<link href="'+file+'" type="text/css" rel="stylesheet" media="screen" />');
}
/* -------------------------------------------------------- */

/* Used to remove the margin-top of the first child in a parent container */
/* ---------------------------------------------------------------------- */
function resetMargin()
{
	$j($j(this).children()[0]).css('margin-top','0');
	if (($j($j(this).children()[0]).hasClass('left')) || ($j($j(this).children()[0]).hasClass('right')))
	{		
		$j($j(this).children()[1]).css('margin-top','0');
	}
}
/* ---------------------------------------------------------------------- */

/* Used to handle the even distribution of widths across sibling elements */
/* ---------------------------------------------------------------------- */
function setItemWidth()
{
	if ($j(this).siblings().length > 0 )
	{
		var setWidth = Math.floor(100/($j(this).siblings().length+1));
		$j(this).width(setWidth+'%')
	}
}
/* ---------------------------------------------------------------------- */


/* Used to handle match class elements to handle the matching of heights */
/* --------------------------------------------------------------------- */
var noOfMatchHeightTypes = [];

function initResizeHeight()
{
	$j("*[@class^='matchH']").each(setNoMatchGroups);
	$j.each(noOfMatchHeightTypes, function()
	{
		var tallest = 0;
		$j('.matchH'+this).each(function(i)
		{
			if ($j(this).height() > tallest) tallest = $j(this).height();
		});
		
		$j('.matchH'+this).height(tallest);
	});
}

function setNoMatchGroups()
{
	var num = $j(this).attr('class').split('matchH')[1];
	if ( noOfMatchHeightTypes.toString().match(num) == null)
		noOfMatchHeightTypes.push(num);
}
/* -------------------------------------------------------------------------------- */

/* Used to add correct margin to float left and right elements */
/* ----------------------------------------------------------- */
function addMargin()
{
	if ($j(this).hasClass('left')) $j(this).addClass('mediaLeft');
	if ($j(this).hasClass('right'))	$j(this).addClass('mediaRight');
}
/* ----------------------------------------------------------- */


function layoutTweaks()
{	
	/* Index Page Tweak every third panels margin */
	$j('.index div:nth-child(3n)').addClass('three');

	/* Ensure that first element withing padded containers do not contain a margin top */
	$j('.content').each(resetMargin);
	$j('.form fieldset').each(resetMargin);
	$j('.panel').each(resetMargin);
	$j('.panelsTable').each(resetMargin);
	$j('.contentPanel').each(resetMargin);
	$j('.important').each(resetMargin);
	
	/* Ensure that the first panel within a panel group does not contain a border-top */
	$j('.panels').each(function(i)
	{
		$j($j(this).find('.panel')[0]).css('border-top','0px none');
	});
	
	/* Adjust Link Collections width in accordance to the column numbers */
	$j('.linkCollection').children().each(setItemWidth);
	
	/* IE6/7 has issues with ABBR so this is the work around */
	if($j.browser.version.substr(0,3) <= '7.0' && $j.browser.msie)
	{
		$j('span').each(function(i)
		{
			if (($j(this).html().indexOf('*') > -1) && ($j(this).html().length < 2))
			{
				$j(this).css({'font-weight':'normal','color':'#c10000','font-size':'11px'});
			}
		});
	}
	
	/* Insert Error style */
	$j('.inputError').wrapInner('<span></span>');
	
	/* Matches the tallest height of a collection of items that require a matched height */
	initResizeHeight();
		
	/* Images */
	$j('.image').each(addMargin);
	$j('.flash').each(addMargin);

	/* Button Hover for IE6 */
	if($j.browser.version.substr(0,3) == '6.0' && $j.browser.msie)
	{
		$j('button').each(function(i){
			$j(this).mouseover(function(){
				$j(this).addClass('hover');
			});

			$j(this).mouseout(function(){
				$j(this).removeClass('hover');
			});
		});
	}

	/* Align Right Help Links */
	$j('#menu-b-10').children().addClass('right');
}

$j(document).ready(qInit);