/*
 * ArtoonCart - E-Commerce system
 * Copyright © 2011 ARTOON STUDIO (http://artoon.hu) ALL RIGHTS RESERVED.
 * @author Gabor Sar - http://gaborsar.artoon.hu
 * @version 1.0
 * @date October 28, 2011
 */

$(document).ready(function(){
	$('.customize_popup_show').click(function(){
		if ($('.ingredient_description_popup',$(this).parent()).length>0){
			$('.fader').show();
			$('.fader_closer').show();
			$('.ingredient_description_popup',$(this).parent()).show();
		}
	});
	$('.fader, .fader_closer').click(function(){
		$('.ingredient_description_popup').hide();
		$('.fader').hide();
		$('.fader_closer').hide();
	});
	// Stop empty quick search
	$('#quick_search').submit(function(){
		if ($('#quick_search .text').val()=="") return false;
	});
	$('#nav a').each(function(){
		if (navigator.appVersion.indexOf('MSIE 7.')==-1)
		var additionalHtml='<span class="hover"><span class="left"></span><span class="center"></span><span class="right"></span></span>';
		else additionalHtml='';
		$(this).html(additionalHtml+'<span class="left"></span><span class="center">'+$(this).text()+'</span><span class="right"></span>');
		$(this).css('padding','0px');
		if (navigator.appVersion.indexOf('MSIE 7.')==-1){
			$('> .hover .center',this).text($(this).text());
			$('> .hover',this).css('opacity',0);
			$(this).hover(function(){
				$('> .hover',this).stop().fadeTo(500,1);
				}, function(){
				$('> .hover',this).stop().fadeTo(500,0);
			});
		}
	});
	// SET AJAX
	if(navigator.appName=="Microsoft Internet Explorer"){
		http=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		http=new XMLHttpRequest();
	}
	$('.myorders_table tr.hidden').hide();
	$('.myorders_table a.opener').click(function(){
		var id=$(this).attr("href");
		$('#hidden_'+id).toggle("slow");
		return false;
	});
	// COSUTMIZE
	$('.build_table input:checkbox').change(function(event){
		var ingredient=$(this).attr("name");
		var textname=ingredient.replace("checkbox","text");
		if ($(this).is(':checked')){
			var value="1";
			$("#"+textname).val("1");
		}
		else{
			var value="0";
			$("#"+textname).val("");
		}
		ingredient+='_'+value;
		var product=$('#product_information_url').val();
		http.open("GET", site_url+"/product_customize_price.php?ingredient="+ingredient+'&product='+product);
		http.onreadystatechange=function(){
			if (http.readyState==4){
				var prices=http.responseText.split("|");
				$(".product_information_price .amount").text(parseFloat(prices[0]).toFixed(2));
				$(".product_information_price_additional .amount").text(parseFloat(prices[1]).toFixed(2));
			}
		}
		http.send(null);
	});
	$('.build_table input:text').keyup(function(event){
		var ingredient=$(this).attr("name");
		var checkboxname=ingredient.replace("text","checkbox");
		var value=parseInt($(this).attr("value"));
		if (value<1) $("#"+checkboxname).attr('checked',false);
		else $("#"+checkboxname).attr('checked',true);
		ingredient+='_'+value;
		var product=$('#product_information_url').val();
		http.open("GET", site_url+"/product_customize_price.php?ingredient="+ingredient+'&product='+product);
		http.onreadystatechange=function(){
			if (http.readyState==4){
				var prices=http.responseText.split("|");
				$(".product_information_price .amount").text(parseFloat(prices[0])).toFixed(2);
				$(".product_information_price_additional .amount").text(parseFloat(prices[1]).toFixed(2));
			}
		}
		http.send(null);
	});
	$('.build_table input:radio').change(function(event){
		var ingredient=$(this).attr("name");
		var value=$(this).attr("value");
		ingredient+='_'+value;
		var product=$('#product_information_url').val();
		http.open("GET", site_url+"/product_customize_price.php?ingredient="+ingredient+'&product='+product);
		http.onreadystatechange=function(){
			if (http.readyState==4){
				var prices=http.responseText.split("|");
				$(".product_information_price .amount").text(parseFloat(prices[0]).toFixed(2));
				$(".product_information_price_additional .amount").text(parseFloat(prices[1]).toFixed(2));
				$(".customize_price").each(function(){
					$('span.value',this).text($('input:hidden',this).val());
					if ($('input:hidden',this).val()!='0') $('span.method',this).text('Add');
					else $('span.method',this).text('');
				});
				if (prices.length>2)
				for (var i=2; i<prices.length; i++){
					if (i==prices.length-1) $('#description_dynamic').html(prices[i]);
					else{
						var price=prices[i].split("=");
						$(".customize_price_"+price[0]).each(function(){
							value=parseInt($('input:hidden',this).val()-price[1])
							if (value<0) $('span.method',this).text('Subtract');
							if (value>0) $('span.method',this).text('Add');
							if (value==0) $('span.method',this).text('');
							if (value<0) value=-value;
							$('span.value',this).text(parseFloat(value).toFixed(2));
						});
					}
				}
			}
		}
		http.send(null);
	});





	// Change gallery.
	$('.build_table input:radio.gallery').change(function(event){
		var id=$(this).val();
		$('.image_gallery').hide();
		$('#gallery_'+id).show();		
	});




	// SET LIGHTBOX
	$('.image_gallery').each(function(){
		$(this).slideIt({
			//width:752,
			width:188,
			height:148,
			tag:'a',
			steps:3,
			imgResize:false,
			auto:false,
			nav:false,
			bullets:false,
			nextButton:'slideit-next-button',
			prevButton:'slideit-prev-button'
		});
	});
	$('#slideshow_1').each(function(){
		$(this).slideIt({
			width:1000,
			height:340
		});
	});
	$('a.image_link').lightBox();
	$('.product_table a.zoom').each(function(){
		$(this).lightBox();
	});	
	$('.product_information .image_gallery').each(function(){
		$('a',this).lightBox();
	});
	$('.float_filter').keyup(function(event){
		if (event.keyCode!=37 && event.keyCode!=38 && event.keyCode!=39 && event.keyCode!=40){
			var num=$(this).val();
			var num2="";
			var n_d=0;
			for (var i=0; i<num.length; i++){
				switch (num[i]){
					case '.':
						if (n_d==0){
							if (num2=="") num2=0;
							num2+=".";
						}
						n_d+=1;
						break;
					case ',':
						if (n_d==0){
							if (num2=="") num2=0;
							num2+=".";
						}
						n_d+=1;
						break;
					case '0': if (num2!='0') num2+='0'; break;
					case '1': num2+='1'; break;
					case '2': num2+='2'; break;
					case '3': num2+='3'; break;
					case '4': num2+='4'; break;
					case '5': num2+='5'; break;
					case '6': num2+='6'; break;
					case '7': num2+='7'; break;
					case '8': num2+='8'; break;
					case '9': num2+='9'; break;
				}
			}
			if ($(this).val()!=num2) $(this).val(num2);
		}
	});
	$('.int_filter').keyup(function(event){
		if (event.keyCode!=37 && event.keyCode!=38 && event.keyCode!=39 && event.keyCode!=40){
			var num=$(this).val();
			var num2="";
			for (var i=0; i<num.length; i++){
				switch (num[i]){
					case '0': if (num2!='0') num2+='0'; break;
					case '1': num2+='1'; break;
					case '2': num2+='2'; break;
					case '3': num2+='3'; break;
					case '4': num2+='4'; break;
					case '5': num2+='5'; break;
					case '6': num2+='6'; break;
					case '7': num2+='7'; break;
					case '8': num2+='8'; break;
					case '9': num2+='9'; break;
				}
			}
			if (num2.length>1 && num2[0]=='0') num2=num2.substring(1);
			if ($(this).val()!=num2) $(this).val(num2);
		}
	});
	$("#address_shipping").change(function(){
		if (!$(this).is(':checked')){
			$('.shippingline .text').val('');
			$('.shippingline .select').val('');
			$('.shippingline').hide();
		}
		else $('.shippingline').show();
	});
});

