var stamp_nl   = 0.50;
var stamp_eu = 0.85;
var stamp_non_eu = 0.95;
var postage_nl = {"0":"0","1":"4","2":"4","3":"5","4":"5","5":"5","6":"6"};
var postage_eu = {"0":"0","1":"4","2":"4","3":"8","4":"8","5":"8","6":"11"};
var postage_non_eu = {"0":"0","1":"6","2":"6","3":"11","4":"11","5":"11",	"6":"17"};
var envelope = 0.40;

$(document).ready(function() {
  $('.hidden').css('display','none');
  $('.toggle').css('visibility','visible');
	
	prepareToggle();
	if(document.getElementById('counter')) prepareCounter();
	preparelinks();        
	
	if ($('#store').length) {
		$('.release a').has('img').fancybox({
			'titleShow' 		: false
			
		});
	}
});



function prepareToggle() {
	$('.toggle').click(function(){
		$(this).parent().parent().next('.hidden').toggle();
		return false;
	});
}
		 
function prepareCounter() {
		document.getElementById('counter').style.display = 'block';
		 document.getElementById('spchart').style.display = 'none';
				dest_class = document.getElementById('dest_class');
				
				dest_class.onchange = cashier;
				
				shipping = document.getElementById('shipping');
				total = document.getElementById('total');
				cashier();
}

function CurrencyFormatted(amount)
{
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = '\u20AC'+minus + s;
		return s;
}

function cashier() {
			
			var qty = parseInt(document.getElementById('qty').firstChild.nodeValue);
			var twelveinch = 0;
			var seveninch = 0;
			
			/* vinyl hack */
			if ($("input[value*='vinyl']").length > 0) {
				
				$("input[value*='vinyl']").each(function() {
					var pizza = $(this).attr('value').split('^');
					if ($(this).attr('value').indexOf('7"') > 0) {
						seveninch = parseInt(pizza[1]) + seveninch;
					} else {
						twelveinch = parseInt(pizza[1]) + twelveinch;

					}
					
				});
				
				if (qty >= (twelveinch + seveninch)) {
					qty = qty - twelveinch - seveninch;
				} else {
					qty = twelveinch + seveninch;
				}
			}
			/* vinyl hack */
			
			
			
			/* shirt exception */
			if ($("input[value*='shirt']").length > 0) {
				
				var pancake = $("input[value*='shirt']").attr('value').split('^');
				var shirt_qty = pancake[1];
				
				
				if (qty >= shirt_qty) {
					qty = qty - shirt_qty;
				} else {
					qty = shirt_qty;
				}
			}
			/* /shirt exception */
			
			var sbttl = parseInt(document.getElementById('subtotal').firstChild.nodeValue);
			var dc = dest_class.options[dest_class.selectedIndex].value;
			
			var least_full = qty % 6;					
			if (qty < 7) least_full = qty;
			var full_packages = (qty - least_full) / 6;
			
			var stamp = eval('stamp_'+dc);
			
			var nrofstamps = (full_packages * parseInt(eval('postage_'+dc)[6])) + parseInt(eval('postage_'+dc)[least_full]);
			
			var nrofpackages = full_packages;
			if (least_full > 0) nrofpackages = nrofpackages + 1;
			
			shppng = nrofstamps * stamp + (nrofpackages * envelope); 
			
			/* vinyl hack */
			var vinyl_shppng = 0;
			if (twelveinch > 0) {
				if (dc == 'nl' || dc == 'eu') {
				 vinyl_shppng += twelveinch * 8.69;
				} else if (dc == 'non_eu') {
					vinyl_shppng += twelveinch * 17.10;
				}
			}
			if (seveninch > 0) {
				if (dc == 'nl' || dc == 'eu') {
				 vinyl_shppng += seveninch * 2.90;
				} else if (dc == 'non_eu') {
					vinyl_shppng += seveninch * 10.45;
				}
			}

			shppng = shppng + vinyl_shppng;
			/* /vinyl hack */
			
			/* shirt exception */
			if (shirt_qty > 0) shppng = shppng + (shirt_qty * 3.50);
			/* /shirt exception */
			
			ttl = sbttl + shppng;
			
			if ($('#paypal_check:checked').length) {
				ttl = ttl * 1.10;
			}
						
			$('#paypal_check').change(function() {
				if ($('#paypal_check:checked').length) {
					ttl = sbttl + shppng * 1.10;
				} else {
					ttl = sbttl + shppng;
				}
				total.value = CurrencyFormatted(ttl);
			});
			
			//if ($('#paypal_check:checked').length) {
			//	ttl = ttl * 1.10
			//}
			
			shipping.value = CurrencyFormatted(shppng);
			total.value = CurrencyFormatted(ttl);
}

function preparelinks() {
  if (!document.getElementsByTagName) return false;
  var lnks = document.getElementsByTagName("a");
  for (var i=0; i<lnks.length; i++) {
if ((lnks[i].className == "newwindow") || (lnks[i].href.indexOf('snowstar.nl') == -1))  {
      lnks[i].onclick = function() {
        popUp(this.getAttribute("href"));
        return false;
      }
    }
  }
}
	

	
function popUp(href) {
    window.open(href);
}
