var fadeImages = function(max){
	var current=0;
	
	var timer = 6;
	var periodical; 
	 
	var refresh = (function() {
		$('fade'+current).setStyle('z-index', '10');
		$('fade'+current).fade(1, 0);		
		
		current=(current==max ? -1 : current);
		current++;
		
		$('fade'+current).setStyle('z-index', '20');
		$('fade'+current).fade(0, 1);
	});

	periodical = refresh.periodical(timer * 1000, this);
};

var calculatePrice = function(specials, freedevqty){
	var special_subtotal=0;
	var special_qty=0;
	
	var subtotal=0;
	var shippingcost;
	var total=0;
	var qty=0;
	var discount=.9;
	var specialtext='';
		
	var wines = $$("select.wineselect");
	wines.each(function(wine, i) {
		bottleqty=$(wine.id+'type').value; // 1 or 12		
		bottlevalue=$(wine.id+'price').value;	
		av_bottles=$('av_bottles'+wine.id).value;
		wineqty=(wine.value*bottleqty);
		
		special_item=$('special'+wine.id).value;
		if(special_item==1){
			special_subtotal=special_subtotal+(wineqty*bottlevalue);
			special_qty=special_qty+(wineqty*av_bottles);
		}else{
			subtotal=subtotal+(wineqty*bottlevalue);
			qty=qty+(wineqty*av_bottles);
		}	
	});
		
	shipping=$('shipping').value;
	
	switch (shipping) {
		case 'si':
			shippingcost=8;
			break;
		case 'ni':
			shippingcost=12;
			break;			
		case 'int':
			shippingcost=0;
			break;
	}
	
	if(specials==1 && qty>=12){
		subtotal=(subtotal*discount);
		specialtext='<br /><span class="star">(10% discount included)</span>';
	}
	
	subtotal=(subtotal+special_subtotal);
	
	qty=(qty+special_qty);
	
	if(shipping=='int'){
		shippingcost='TBA';
		total='TBA';
	}else{
		if(qty>=freedevqty){
			shippingcost='FREE';
			total=subtotal;
		}else{
			total=(subtotal+shippingcost);
			shippingcost='$'+shippingcost.toFixed(2);
		}

		total='$'+total.toFixed(2);		
	}
	
	subtotal='$'+subtotal.toFixed(2);
	
	$('shippingcost').set('html', shippingcost);
	$('subtotal').set('html', subtotal+specialtext);
	$('qty').set('html', qty);
	$('total').set('html', total);
};

function website(url){
	popupWindow = window.open(url, '_blank','');
	popupWindow.focus();
}

function popup(url){
	popupWindow = window.open(url, '_blank', 'location=no,menubar=no,width=600,height=520,toolbar=no,scrollbars=yes,resizable=no');
	popupWindow.focus();
}
function popupclose(){
	window.close();
}

var external = function(){
	var anchors = $$("a");
	anchors.each(function(anchor, i) {
		var relAttribute = String(anchor.getAttribute('rel'));
		if (anchor.id=='print'){
			anchor.onclick = function() {
				print();
				return false;
			};
		}else if (anchor.getAttribute('href')){
			if(relAttribute=='external'){
				anchor.onclick = function() {
					website(this);
					return false;
				};
			}else if(relAttribute=='popup'){
				anchor.onclick = function() {
					popup(this);
					return false;
				};
			}
		}
	});
};

window.addEvent('domready', function(){
	new external();
});