
/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <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(at)cherne(dot)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 ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){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.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);

$(document).ready(function(){

	$('#tab a').click(function(){
		$('#tab a.current').removeClass('current');
		var tab = $(this).attr('rel');
		$(this).addClass('current');
		$('#tabs > div').css('opacity','0');
		$('#tabs > div').css('height','0');
		$('#tab-'+tab).css('opacity','1');
		$('#tab-'+tab).css('height','85px');
		return false;
	});	

	if($('#carro1').length){
		$('#carro1').jcarousel({
	        auto: 1,
	        wrap: 'circular',
	        scroll: 1
	    });
	}

	if($('#carro2').length){
	    $('#carro2').jcarousel({
	        auto: 1,
	        wrap: 'circular',
	        scroll: 1
	    });
	}

	if($('#carro3').length){
	    $('#carro3').jcarousel({
	        auto: 1,
	        wrap: 'circular',
	        scroll: 1
	    });
	}

$(window).bind('load', function(){
	

	if($('.gal_img').width() > 0){
		$('.gal_img').fancybox();
	}


$('#homeslider').nivoSlider({
        effect:'fade',
		beforeChange: function(){
			$('.tlcurrent').removeClass('tlcurrent').addClass('tboff');
			$('.tlnext').removeClass('tlnext').addClass('tbcurrent');
			$('.tloff').removeClass('tloff').addClass('tbnext');
			
			$('.tboff').removeClass('tboff').addClass('tloff');
			$('.tbcurrent').removeClass('tbcurrent').addClass('tlcurrent');
			$('.tbnext').removeClass('tbnext').addClass('tlnext');
		} // Triggers after a slide transition
    });

});


$('#baf_email').focus(function(){
	if(!$(this).attr('rel')) {
		$(this).attr('rel',$(this).val()).val('');
	} else {
		if($(this).val()==$(this).attr('rel')) $(this).val('');
	}}).blur(function(){
		if($(this).val()=="") $(this).val($(this).attr('rel'));
	});
	
	/*
	CHKEML
	*/
	(function(a){a.fn.checkEmail=function(){var b;this.each(function(){var c=a(this).val();b=c.indexOf("@")==-1||c.lastIndexOf(".")==-1?false:true});return b}})(jQuery);
	
	//BAF
	$('#baf-form').submit(function(){
		$('#baf_text p').fadeOut();
		chkeml = $('#baf_email').checkEmail();
		if(chkeml)
		{
			$.ajax({
				type: "POST",
				url:"skin/scripts/php/process.php",
				data: "emailadd=" + $('#baf_email').val(),
				success: function(data){
					$('#baf_text p').html(data);
					$('#baf_email').val('');
					$('#baf_text p').fadeIn();
				}
			});
		}
		else
		{
			$('#baf_text p').html("<span class='error'>Please enter a valid email address.</span>");
			$('#baf_text p').fadeIn();
		}
		return false;
	});



		$('.productBox').children('a').children('.prodImage1').animate({'opacity': '100'},250);
		$('.productBox').children('a').children('.prodImage2').animate({'opacity': '0'},250);
		$('.productBox').children('a').children('.viewproduct').animate({'width': '29'},250);
$('.productBox').hoverIntent(
	function(){
		$(this).children('a').children('.prodImage1').animate({'opacity': '0'},250);
		$(this).children('a').children('.prodImage2').animate({'opacity': '100'},250);
		$(this).children('a').children('.viewproduct').animate({'width': '149'},250);
	},
	function(){
		$(this).children('a').children('.prodImage1').animate({'opacity': '100'},250);
		$(this).children('a').children('.prodImage2').animate({'opacity': '0'},250);
		$(this).children('a').children('.viewproduct').animate({'width': '29'},250);
	}
);

//TIPSY
(function(a){a.fn.tipsy=function(e){e=a.extend({},a.fn.tipsy.defaults,e);return this.each(function(){var d=a.fn.tipsy.elementOptions(this,e);a(this).hover(function(){a.data(this,"cancel.tipsy",true);var c=a.data(this,"active.tipsy");if(!c){c=a('<div class="tipsy"><div class="tipsy-inner"/></div>');c.css({position:"absolute",zIndex:1E5});a.data(this,"active.tipsy",c)}if(a(this).attr("title")||typeof a(this).attr("original-title")!="string")a(this).attr("original-title",a(this).attr("title")||"").removeAttr("title"); var b;if(typeof d.title=="string")b=a(this).attr(d.title=="title"?"original-title":d.title);else if(typeof d.title=="function")b=d.title.call(this);c.find(".tipsy-inner")[d.html?"html":"text"](b||d.fallback);b=a.extend({},a(this).offset(),{width:this.offsetWidth,height:this.offsetHeight});c.get(0).className="tipsy";c.remove().css({top:0,left:0,visibility:"hidden",display:"block"}).appendTo(document.body);var f=c[0].offsetWidth,g=c[0].offsetHeight;switch((typeof d.gravity=="function"?d.gravity.call(this): d.gravity).charAt(0)){case "n":c.css({top:b.top+b.height,left:b.left+b.width/2-f/2}).addClass("tipsy-north");break;case "s":c.css({top:b.top-g,left:b.left+b.width/2-f/2}).addClass("tipsy-south");break;case "e":c.css({top:b.top+b.height/2-g/2,left:b.left-f}).addClass("tipsy-east");break;case "w":c.css({top:b.top+b.height/2-g/2,left:b.left+b.width}).addClass("tipsy-west")}d.fade?c.css({opacity:0,display:"block",visibility:"visible"}).animate({opacity:0.8}):c.css({visibility:"visible"})},function(){a.data(this, "cancel.tipsy",false);var c=this;setTimeout(function(){if(!a.data(this,"cancel.tipsy")){var b=a.data(c,"active.tipsy");d.fade?b.stop().fadeOut(function(){a(this).remove()}):b.remove()}},100)})})};a.fn.tipsy.elementOptions=function(e,d){return a.metadata?a.extend({},d,a(e).metadata()):d};a.fn.tipsy.defaults={fade:false,fallback:"",gravity:"n",html:false,title:"title"};a.fn.tipsy.autoNS=function(){return a(this).offset().top>a(document).scrollTop()+a(window).height()/2?"s":"n"};a.fn.tipsy.autoWE=function(){return a(this).offset().left> a(document).scrollLeft()+a(window).width()/2?"e":"w"}})(jQuery);
//where am i using it
$("a,img,h1,h2,h3,input,span,div,em,li").each(function(){$(this).attr("title").length>0&&$(this).tipsy({opacity:1,html:true,gravity:$.fn.tipsy.autoNS})});

$(function() {
	//find all form with class jqtransform and apply the plugin
	$("form.jqtransform").jqTransform().animate({height:40},{complete: function(){
		$(this).css({overflow:'visible'});
		
	}});
});


//**** CARLS STUFFS
//Load the enquiry cart -
$('#enquiry').load('/cart/show');
$('#top-enquiry').load('/cart/summary');

changeMinQuant();

//If anything is done on the products page. Calculate the new price using the calcPrice function -
$('#size ul a, #size ul a.selected, #quant ul a, #quant ul a.selected, #upgrade-check .jqTransformCheckbox').click(function(){
	calcPrice();
	return false;
});

$('#size ul a, #size ul a.selected').click(function(){
	changeMinQuant();
	return false;
});

//Lit and colour have thier own function as they need to disable the upgade if unlit/non-white is selected.
//This uses the function canUpgrade to do this and calcPrice for the calction of the new price - 
$('#lit ul a, #lit ul a.selected, #colour ul a, #colour ul a.selected').click(function(){
	canUpgrade();		
	calcPrice();
	return false;
});

	
//When the jqtransform form is submitted add the current setup, to the cart.
//Using the addtoCart function - 	
$('.jqtransform').submit(function(){
	//addtoCart($(this).attr('rel'));
	id = $(this).attr('rel');
	 
	 gquantity = parseFloat($('#quant ul a.selected').attr('index')) + 1;
	 qty =  $('#quantr option:nth-child('+gquantity+')').attr('rel');
	 
	 gsize = parseFloat($('#size ul a.selected').attr('index')) + 1;
	 var size = $('#sizer option:nth-child('+gsize+')').val();
	 
	 price = calcCart();
	
	 gcolour = parseFloat($('#colour ul a.selected').attr('index')) + 1;
	 var colour =  $('#colourr option:nth-child('+gcolour+')').attr('rel'); 
	
	upgrade = 0;
	if($('#upgrade-check .jqTransformCheckbox').hasClass('jqTransformChecked')){
		 upgrade = 1;
	}
	
		
	
	 isthislit = parseFloat($('#lit ul a.selected').attr('index')) + 1; 
     if($('#litr option:nth-child('+isthislit+')').val() == 'lit'){
		var lit = 1;
	 } else {
		var lit = 0;
	 }
	
	$.ajax({
		type: 'POST',
		url: '../../cart/add/'+id+'/'+qty+'/'+price+'/'+colour+'/'+size+'/'+lit+'/'+upgrade+'/',
		success: function(cart){
			$('#cart').html(cart);
		}
	});
	
	return false;
});
	
//When the closeupgrade anchor is clicked, hide the upgrade popup -
$('#closeupgrade').click(function(){
	$('#upgrade-option').hide();
	return false;
});

//Whats the minimum order?
function changeMinQuant(){

	if(typeof(prodarray) !== typeof(undefined)) {
	index = $('#size ul a.selected').text();
	if(index == 'X Large'){
		index = 'Xlarge';
	}
	
		minorder = prodarray['min_'+index];	
	
	$('#quant div div span').text(minorder);
	$('#quant ul li a.selected').removeClass('selected');
	$('#quant ul li').each(function(){
		if(parseInt($(this).text()) < parseInt(minorder)){
			$(this).css('display', 'none');
		} else {
			$(this).css('display', 'block');
		}
		
		if(parseInt($(this).text()) == parseInt(minorder)){
			$(this).children('a').addClass('selected');
		}
	});
	//alert(minorder);
	}
}
	
//Discover if the upgrade function needs to be present.
function canUpgrade()
{
	islit = parseFloat($('#lit ul a.selected').attr('index')) + 1;
	var colour = parseFloat($('#colour ul a.selected').attr('index')) + 1;
		
	if($('#litr option:nth-child('+islit+')').val() == 'unlit' || $('#colourr option:nth-child('+colour+')').attr('rel') != 'White'){
		$('#upgrade-option').hide();
		$('#upgrade-check').hide();
		if($('#upgrade-check .jqTransformCheckbox').hasClass('jqTransformChecked')){
			$('#upgrade-check .jqTransformCheckbox').toggleClass('jqTransformChecked');
		} 
	} else {
		if($('#colourr option:nth-child('+colour+')').attr('rel') == 'White'){
			$('#upgrade-option').show();
			$('#upgrade-check').show();
		}
	}
}

//Calculate the current price
function calcPrice()
{

	index = parseFloat($('#size ul a.selected').attr('index')) + 1;
	quantity = parseFloat($('#quant ul a.selected').attr('index')) + 1;
	islit = parseFloat($('#lit ul a.selected').attr('index')) + 1;
	
	price = $('#sizer option:nth-child('+index+')').attr('rel') * $('#quantr option:nth-child('+quantity+')').attr('rel');
	each = parseFloat($('#sizer option:nth-child('+index+')').attr('rel'));
	
	if($('#litr option:nth-child('+islit+')').val() == 'unlit'){
		price = ($('#sizer option:nth-child('+index+')').attr('rel') * $('#quantr option:nth-child('+quantity+')').attr('rel')) - ($('#litr option:nth-child('+islit+')').attr('rel') * $('#quantr option:nth-child('+quantity+')').attr('rel'));
		each = $('#sizer option:nth-child('+index+')').attr('rel') - $('#litr option:nth-child('+islit+')').attr('rel');
			if($('#colourr option:first-child').attr('title') == 1){
				$('#colour ul li').each(function(){
					$(this).css({'display': 'block'});
				});

				//$('#colour li:first-child').css({'display': 'block'});
			}
	}  else {
	if($('#colourr option:first-child').attr('title') == 1){
		$('#colour ul li').each(function(){
			$(this).css({'display': 'none'});
		});
		$('#colour div span').text('White');
		$('#colour li:first-child').css({'display': 'block'});
		$('#colour ul li a.selected').removeClass('selected');
		$('#colour li:first-child a').addClass('selected');
	}
	}
	if($('#upgrade-check .jqTransformCheckbox').hasClass('jqTransformChecked')){
		price = price + (40 * $('#quantr option:nth-child('+quantity+')').attr('rel'));
		each = each + 40;
	} 
	
	price = Math.round(price*100)/100;
	price = String(price);
	
	/*var checker = /(?<=\.)[0-9]{2}/;  
	if(!(checker.test(price))){
		price = price+'0';
	}*/
	
	$('#totalwrap strong').html('&pound;'+price);
	$('#totalwrap em').html('&pound;'+each);
}

//Calulate the cart each price
function calcCart()
{
	index = parseFloat($('#size ul a.selected').attr('index')) + 1;
	quantity = parseFloat($('#quant ul a.selected').attr('index')) + 1;
	islit = parseFloat($('#lit ul a.selected').attr('index')) + 1;
	
	each = parseFloat($('#sizer option:nth-child('+index+')').attr('rel'));
	
	if($('#litr option:nth-child('+islit+')').val() == 'unlit'){
		each = $('#sizer option:nth-child('+index+')').attr('rel') - $('#litr option:nth-child('+islit+')').attr('rel');
	}
	
	if($('#upgrade-check .jqTransformCheckbox').hasClass('jqTransformChecked')){
		each = each + 30;
	} 
	
	return each;
}

//Add the current setup to the cart.
function addtoCart(id)
{	
	
}

if($('#colourr option:first-child').attr('title') == 1){
	$('#colour ul li').each(function(){
		$(this).css({'display': 'none'});
	});
	
	$('#colour li:first-child').css({'display': 'block'});
	
	
	$('#colour ul').css({'height': 'auto'});
}

canUpgrade();
	calcPrice();
});
