/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

		
function getRandomInt(min,max)
{ 
	return Math.round(min + Math.random()*(max-min));
}

function getAllBetween(firstEl,lastEl) {
    var firstElement = $(firstEl); // First Element
    var lastElement = $(lastEl); // Last Element
    var collection = new Array(); // Collection of Elements
    collection.push(firstElement); // Add First Element to Collection
    $(firstEl).nextAll().each(function(){ // Traverse all siblings
        var siblingID  = $(this).attr("id"); // Get Sibling ID
        if (siblingID != $(lastElement).attr("id")) { // If Sib is not LastElement
                collection.push($(this)); // Add Sibling to Collection
        } else { // Else, if Sib is LastElement
                return false; // Break Loop
        }
    });         
    return collection; // Return Collection
}


$(function() {
	// highlight both the headings links and the p links when mousing over one of them
	$('#content.home .two-col .col a').not($('.add-to-basket a, .col-foot a, h2 a')).hover(function() {
		$(this).parent().parent().find('a').addClass('hover');
	}, function() {
		$(this).parent().parent().find('a').removeClass('hover');
	});
});

$(function() {
	// even up the news and top courses elements
	if($('#content').hasClass('home'))
	{
		var cc = $('.two-col .col-courses');
		var cn = $('.two-col .col-news');
		
		var diff = cc.height() - cn.height();
		
		if(diff > 0)
		{   
			var f = cn.find('.col-foot');
			var fmt = parseInt(f.css('margin-top'));
			f.css({marginTop:(fmt+diff)+'px'});
		}
		else if(diff < 0)
		{
			var f = cc.find('.col-foot');
			var fmt = parseInt(f.css('margin-top'));
			f.css({marginTop:(fmt+(diff*-1))+'px'});
		}
	}
});

// rotate the header banner text
$(document).ready(function() {
	$('#header ul.rotate li').not(':first-child').hide();
});
$(function() {
	if($('#header ul.rotate li').length>1)
	{
		setTimeout("rotateHeader();", 7000);
	}
});
function rotateHeader()
{
	//$('#header ul.rotate li:visible').fadeOut()

	$('#header ul.rotate li')
		.not(':hidden')
		.fadeOut(200, 
			function() {
				if($(this).next().length)
				{
					$(this).next()
					.fadeIn(400);
				}
				else
				{
					$(this).parent()
					.find('li:first')
					.fadeIn(300);
				}
			});
		
		
	setTimeout("rotateHeader();", 7000);
}

// add product page tabs
$(function() {
	var container = $('#course_details .tabs');
	var tabs = container.find('.tab');
	
	// add the js specific css
	container.addClass('is_js');
	
	// hide the tab divs
	tabs.not(':first-child').hide();
	
	// add the tab bar
	var tab_bar = $('<div id="tab_bar"><ul></ul></div>').prependTo(container);
	
	// take each of the headings and create a tab button from it
	tabs.each(function() {
		var el = $(this).find('h2:first-child');
		el.remove();
		var text = el.text();
		
		$('<li><a href="#"><span>'+text+'</span></a></li>').appendTo(tab_bar.find('ul'));	
	});
	
	tab_bar.find('a').click(function(e) {
		e.preventDefault();
		
		var i = tab_bar.find('a').index(this);
		tabs.hide();
		
		// remove active class on all elements
		tab_bar.find('li').removeClass('active');
		$(this).parent().addClass('active');
		
		tabs.eq(i).show();
	});
	
	// click the first tab element
	tab_bar.find('a:first').click();
	
	// remove the line on the first of the h3's
	tabs.each(function() {
		$(this).find('h3:first').addClass('noline');
	});
	
	// remove line on last related products list item
	$('.related_courses').find('li:last').css({borderBottom:0})
});

// add active category nav marker
$(function() {
	var ele = $('.orange-box ul').find('li.active');
	
	ele.append('<span class="marker"></span>');	
});

// auto submit the a-z dropdown onchange and hide the button
$(function() {
	if($('#a-z_courses.right-col.right-col').length > 0)
	{
		$('form#filter input[type=submit]').hide();
		$('select#l').change(function() {
			$('form#filter').submit();
		});
	}
});

// pull in the carousel images
var timeout;
$(function() {
		 $.ajax({
			type: "POST",
			url: "/",
			data: "ajax=yes",
			success: function(html){
			   $('#carousel').find('img').remove();
			   //$('#carousel').height($('#carousel').height()+'px');
			   $('#carousel').prepend(html);
			   if($('#carousel img').length>1)
				{
				   $('#carousel img').not(':first').hide();
				
				   timeout=setTimeout("carousel_rotate();", 7000);
					
					// add controls
					//$('<div id="car-controls"><div><ul><li class="pro">promotions</li></ul></div></div>').appendTo($('#carousel'));
					//var cc = $('#car-controls');
					
					//for(var i=1;$('#carousel img').length>=i;i++)
					//{
					//	cc.find('ul').append('<li class="cc'+i+'"><a href="#">'+i+'</a></li>')
					//}
					//cc.find('li:eq(1)').addClass('active');
					
					//cc.find('a').click(function(e) {
					//	e.preventDefault();
						
					//	clearTimeout(timeout);
						
					//	var _t = $(this).parent().attr('class');
					//		_t = _t.replace(/cc/g, '');
							
					//	$('#car-controls li').removeClass('active');
					//	$(this).parent().addClass('active');
						
					//	$('#carousel img:visible').stop().fadeOut(500, function() {
					//		var z = parseInt(_t)-1;
					//		$(this).css({opacity:1}).hide();
					//		$('#carousel img').eq(z).fadeIn(800, function() {		
					//		});
					//		timeout=setTimeout("carousel_rotate();", 7800);
					
					//	});
					//	});
					

				}
			}
		 });
});

function carousel_rotate() {
		var ci = $('#carousel img');
		
	   $('#carousel img')
		.not(':hidden')
		.fadeOut(200, 
			function() {
				if($(this).next().length && !$(this).next().is('div'))
				{        
					$(this).next().fadeIn(500, function() {
						var n = $('#carousel img').index(this);
						$('#car-controls li').removeClass('active');
						$('#car-controls li').eq(n+1).addClass('active');
					});
				}
				else
				{                   
					$(this).parent().find('img:first').fadeIn(800, function() {
						var n = $('#carousel img').index(this);
						$('#car-controls li').removeClass('active');
						$('#car-controls li').eq(n+1).addClass('active');
					});
				}
			});
			
			timeout = setTimeout("carousel_rotate();", 7000);
}

// remove the shipping fields from checkout if tickbox is ticked
$(function() {
	shippingTickBox();
	$('#sameas').change(shippingTickBox);
	
	// adding ability to toggle buttons on and off
	if($('#sameas').length > 0 && !$('#sameas').is(':checked'))
	{
		if(!$('.fields').hasClass('delivery'))
			$('#register input[type=image]:first').hide();
	}
});

function shippingTickBox()
{
	if($('#sameas').length > 0 && !$('#sameas').is(':checked'))
	{
		$('#shipping_fields').show();
		if(!$('.fields').hasClass('delivery'))
			$('#register input[type=image]:first').hide();
		
	}
	else
	{
		$('#shipping_fields').hide();
		if(!$('.fields').hasClass('delivery'))
			$('#register input[type=image]:first').show();
	}
}

$(function() {
	$('#search_bar').prepend($('<div class="opa"></div>'));
	$('#search_bar div.opa').css({height: 37+'px', width: '100%', backgroundColor: '#fff', opacity: '0.8', position: 'absolute', top: 0, left: 0});
});

/*$(function() {
	var h = $('#about.right-col h2');
	
	h.each(function() {
		$(this).nextAll('*:not(\'div\')').not($('#about.right-col h2')).wrap('<div></div>');
	});

});*/
$(function() {
	// affiliate links
	var el = $('#content div.left-col .image-stack li');
	el.css({height:'85px'});
	el.parent().css({height:'130px'});
	el.not(':first').hide();
	
	setTimeout("aff_rotate();", 4000);
});

function aff_rotate() {
	   $('#content div.left-col .image-stack li')
		.not(':hidden')
		.fadeOut(200, 
			function() {
				if($(this).next().length && !$(this).next().is('div'))
				{
					$(this).next().fadeIn(300);
				}
				else
				{
					$(this).parent().find('li:first').fadeIn(400);
				}
			});
			
			setTimeout("aff_rotate();", 4000);
}

// load the minibasket
$(function() {
	if(!($.browser.msie && (parseInt($.browser.version)<8)))
	{
		$.ajax({
			type: "POST",
			url: "/basket/",
			data: "ajax=yes",
			success: function(html){
				$('#mb_basket a').addClass('mb_anchor');
				$(html).prependTo($('#mb_basket')).hide();
				
				$('a.mb_anchor').parent().hover(function() {
					$('#minibasket').show();
					$('a.mb_anchor').addClass('hovered');
				},
				function() {
					$('#minibasket').hide();
					$('a.mb_anchor').removeClass('hovered');
				});
			}
		});
	}
});

$(function() {
	// remove first nav bullets, as we can't use the negative margins and hidden overflow with the minibasket
	$('#header .navigation ul#nav-primary li:first, #header .navigation ul#nav-secondary li:first').css({background:'none'});
});