/* c0849e */

$(function(){

// ff hack
	if($.browser.mozilla){
		$('.button,.addone').css({height:'18px',verticalAlign:'top'});
		a=$('#sub li').length;
		$('#sub').css({width:a*90+'px'});
	}

	var os = navigator.userAgent;

//  win webkit
	if(os.indexOf('Mac OS X')==-1){
		if($.browser.webkit){
			$('.button,.addone').css({height:'17px'});
			$('#search').css({paddingTop:'2px'});
		}
		
	}

// ie	
	if($.browser.msie){
		a=$('#sub li').length;
		$('#sub').css({width:a*90+'px'});
		$('#coll').css({top:'5px'});
		$('.button,.addone').css({height:'18px',verticalAlign:'top'});
		if(parseInt($.browser.version,10)<8){
			$('#left').css({width:'525px'});
			$('#main').css({width:'913px'});
 			$('#allcont').css({width:'905px',height:'auto'});
 			$('#basketail').css({marginTop:'-65px'});
 			$('.itemtotal').css({marginTop:'-18px'});
 			$('.itemtotal').prev().css({marginTop:'-18px'});
 			$('.icont').css({margin:'3px'});
 			$('.subcart div').css({marginTop:'-17px'});
 			
		}
	}

// settings & start
	w=($('body').width()-300)/2;
	$('#msgbox').css('left',w+'px');
	$('.opa').css('background','url(images/opa.png)');
	$('.opa').css('background','#000');

	$('.opa2').css('background','url(images/opa2.png)');
	$('.opa2').css('background','#cd009e');

	$('.buying').css('opacity','0.75');

/* 	$('#slideshow').cycle({fx: 'fade', random: 1}); */
	$('#slideshow').jqFancyTransitions({ width: 376, height: 190, effect:'curtain',direction:'left',position:'top' });

	$('.fancy').fancybox({
		'titlePosition':'over',
		'overlayOpacity':'0',
		'transitionIn':'elastic'
	});

	getCart();
	
// hovers etc
	
	$('.button').hover(function(){$(this).css({color:'#000'});},function(){$(this).css({color:'#fff'});});
	$('.butadd').hover(function(){$(this).css({color:'#fff'});},function(){$(this).css({color:'#000'});});
	$('.delbut,.addone,.button2').hover(function(){$(this).css({color:'#cd009e'});},function(){$(this).css({color:'#fff'});});

	$('#nav li').hover(function(){
		a=$(this).find('a:eq(0)');
		a.css({background:'#cd009e',color:'#000'});
		$(this).find('ul').clearQueue().stop().show();
	},function(){
		a.css({background:'transparent',color:'#fff'});
		$('#sub a').css({background:'transparent',color:'#fff'});
		if(a.hasClass('selected')){a.css('color','#000');}
		z=$(this);
		z.find('ul').delay(2000).hide(0,function(){
			if(z.parent('div#sub')){
		 		p=$('#nav li:eq(1)').find('a:eq(0)');
				p.css({background:'transparent',color:'#fff'});
				if(p.hasClass('selected')){p.css('color','#000');}
			}
		});
	});

// place order wholesale
	$('#gord').click(function(){
		go=true;
		$('#orderform input[type=text]').removeClass('warn');
		$('#orderform input[type=text]').each(function(e){
			if($(this).val()==''||($(this).attr('id')=='email'&&!validMail($(this).val()))){
				$(this).addClass('warn');
				go=false;
			}
		});
		if(!go){
			$('.warn:eq(0)').focus();
		}else{
			showmsg('<img src="images/buyloader.gif" alt="" />');
			$('#orderform').submit();
		}
	});

// place order retail
	$('#orderform #country').change(function(){
		if($(this).val()!=''){
			t=0;
			a=0;
			v=0;
			t=$(this).val().split('|');
			t=parseFloat(t[1].replace(/,/g,''));
			$('.basliend:eq(1) span:eq(1)').html(t.toFixed(2));
			v=$('.basliend:eq(0) span:eq(1)').text();
			v=parseFloat(v.replace(/,/g,''));
			$('.basliend:eq(2) span:eq(1)').html((v+t).toFixed(2));
		}else{
			$('.basliend:eq(1) span:eq(1)').html('0.00');
			$('.basliend:eq(2) span:eq(1)').html($('.basliend:eq(0) span:eq(1)').text());
		}
	});

	$('#gordret').click(function(){
		go=true;
		$('#orderform input[type=text]').removeClass('warn');
		$('#orderform input[type=text]').each(function(e){
			if($(this).val()==''){
				$(this).addClass('warn');
				go=false;
			}
		});
		if($('#country').val()==''){
			$(this).addClass('warn');
			go=false;
		}
		if(!go){
			$('.warn:eq(0)').focus();
		}else{
			showmsg('<img src="images/buyloader.gif" alt="" />');
			ctry=$('#country').val();
			ctry=ctry.split('|');
			name=$('#name').val();
			city=$('#city').val();
			county=$('#county').val();
			add=$('#address2').val();
			pc=$('#postcode').val();
			com=$('#comment').val();
			$.post('calls/setorder.php',{ctry:ctry[0],name:name,city:city,county:county,add:add,pc:pc,com:com},function(d){
				d=jQuery.trim(d);
				if(d.length==6){
					$('#buyform input[name=cartId]').val(d);
					v=$('.basliend:eq(2) span:eq(1)').text();
					v=parseFloat(v.replace(/,/g,''));
					$('#buyform input[name=amount]').val(v);
					$('#buyform').submit();
				}else{
					showmsg('Something went wrong<br/>please try again later!');
				}
			});
			
		}
	});



// cart related
	$('.clearcart').click(function(e){
		t=$(this);
		e.preventDefault();
		if(confirm('Are you sure you want to empty the cart?')){
			$.post('calls/updatecart.php',{act:'del',id:'',pcs:''},function(d){
				if(t.hasClass('bass')){
					location.reload();
				}else{
					getCart();
				}
			});
		}
	});

	$('.butadd').click(function(){
		t=$(this);
		id=t.attr('id');
		tt=t.prev().val();
		if(tt!='0'&&!isNaN(tt)){
			z=t.parents('.icont').find('.buying');
			z.fadeIn(200);
			$.post('calls/updatecart.php',{act:'upd',id:id,pcs:tt},function(d){
				getCart();
				t.prev().val('0');
				z.fadeOut(100);
			});
		}else{
			t.prev().val('0').focus().select();
		}
	});
		
	$('.delbut').click(function(){
		a=$(this).attr('id').split('|');
		a=a[1];
		if(confirm('Are you sure you want to remove this item ' + a + ' from the cart?')){
			par=$(this).parents('div.buyline');
			par.find('.deloa').show();
			$.post('calls/updatecart.php',{act:'delone',id:a,pcs:''},function(d){
				par.remove();
				if(d=='0'){
					location.reload();
				}else{
					getCart();
					setTotal();
				}
			});
		}
	});

	$('.addone').click(function(){
		t=$(this);
		id=t.attr('id');
		a=t.parent();
		tt=a.find('input:eq(1)').val();
		par=t.parents('div.buyline');
		pp=par.find('span.itemprice').text();
		if(tt!='0'&&!isNaN(tt)){
			par.find('.deloa').show();
			$.post('calls/updatecart.php',{act:'addone',id:id,pcs:tt},function(d){
				par.find('.deloa').hide();
				n=(parseFloat(pp.replace(/,/g,''))*parseFloat(tt.replace(/,/g,''))).toFixed(2);
				par.find('.itemtotal').html(n);
				getCart();
				setTotal();
			});
		}else{
			a.find('input:eq(1)').val('0').focus();
		}
	});

// cart goes
	$('#procheck').click(function(){document.location='order.php';});
	$('#procheck_retail').click(function(){document.location='order_retail.php';});

	$('#goback').click(function(){document.location='index.php?p=collection';});


// search/login-out submits
	$('#logtext').keydown(function(e){
		if(e.keyCode==13){$('#golog').trigger('click');}
	});

	$('#golog').click(function(){
		if($('#logtext').val()==''){
			$('#logtext').focus();
		}else{
			$('#logname').val($('#logtext').val());
			$('#logform').submit();
		}
	});

	$('#srcbut').click(function(){
		if($('#sstr').val()==''){
			$('#sstr').focus();
		}else{
			$('#srcform').submit();
		}
	});

	$('#gologout').click(function(){document.location='logout.php';});

	$('#msglayer,#msgclose').click(function(){
		$('body').css('overflow','auto');
		$('#msglayer,#msgbox').fadeOut(200);
	});

});

function getCart(){
	$.post('calls/getcart.php',{act:'read'},function(d){
		if(d=='0'){
			$('.basli:eq(0)').html('basket is empty');
			$('#basket span').html('no items');
		}else{
			d=d.split('\|');
			$('.basli:eq(0)').html(d[0]+' item(s) in basket');
			$('#basket span').html(d[0]+' item(s)');
		}
	});
}

function setTotal(){
	v=(vat/100);
	v2=(vat/100)+1;
//	ppc=parseInt(ppc,2);
	ppc=ppc*1;
	a=0;
	$('.itemtotal').each(function(e){
		a+=parseFloat($(this).text().replace(/,/g,''));
	});
	$('.subtotal').html(a.toFixed(2));
	if($('.vat').hasClass('retail')){
//		$('.vat').html(ppc.toFixed(2));
//		$('.total').html((a+ppc).toFixed(2));
		$('.vat').html('n/a');
		$('.total').html(a.toFixed(2));
	}else{
		$('.vat').html((a*v).toFixed(2));
		$('.total').html((a*v2).toFixed(2));
	}
}

function showmsg(m){
	if(m!=''){
/*
		window.scrollTo(0,0);
		$('body').css('overflow','hidden');
*/
		// replace width document height!!
		$('#msgit').html(m);
		$('#msglayer,#msgbox').fadeIn(200);
	}
}

function validMail(str) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(str) == false) {
		return false;
	}else{
		return true;
	}
}





