function change_product_image(src){

	$('product-detail-image').src = src;
}

function special_inputs_selected(special_input_values, special_input_description){

	$('special_input_values').innerHTML = special_input_values;
	$('special_input_description').innerHTML = special_input_description;
	$('button_options').style.display = 'none';
	$('button_add').style.display = '';
	
	attribute_panel.hide();	
}

function attributes_selected(attribute_ids, attribute_description){

	$('attribute_ids').innerHTML = attribute_ids;
	$('attribute_description').innerHTML = attribute_description;
	attribute_panel.hide();

	$('button_options').style.display = 'none';
	$('button_add').style.display = '';
	$('link_options').style.display = '';
}

(function() {

function gift_certificate_change(e){

	if($('amount').value == 'other'){
		$('other_wrapper').style.display = '';
	}
	else{
		$('other_wrapper').style.display = 'none';	
	}
}

function add_to_cart(t){

	if($('gift_cert')){
	
		var message = [];
	
		var amount_reg = new RegExp(/\d/);
		var amount = $('amount').value;
		if(amount == 'other'){
			amount = $('amount_other').value;

			if(amount < $('minimum_amount').value)
				message[message.length] = 'Minimum Amount is $'+$('minimum_amount').value;
		}
	
		if(!amount.length)
			message[message.length] = 'Amount is required.';
		else if(!amount_reg.test(amount))
			message[message.length] = 'Invalid Amount.';
		if(!$('recipient_name').value.length)
			message[message.length] = 'Recipient Name is required.';
		if(!$('sender_name').value.length)
			message[message.length] = 'Sender Name is required.';
		if(!$('recipient_email').value.length)
			message[message.length] = 'Recipient Email is required.';
		else if($('recipient_email').value != $('confirm_recipient_email').value)
			message[message.length] = 'Recipient Email confirmation does not match Recipient Email.';
	
		if(message.length){
	
			error_panel.setBody(message.join('<br />'));
			error_panel.show();
			return;
		}
	
		$('cart_loader').style.display = '';
	
		var params = 'action=add'
		+'&product_id=gc'
		+'&amount='+amount
		+'&recipient_name='+$('recipient_name').value
		+'&sender_name='+$('sender_name').value
		+'&recipient_email='+$('recipient_email').value
		+'&gift_message='+$('gift_message').value;
		var callback = {success:cart_update_callback};
		//alert('happens4');
		YAHOO.util.Connect.asyncRequest('POST', 'cart_update.php', callback, params);
	}
	else{
	
		var message = [];
	
		var qty_reg = new RegExp(/\d/);
		var qty = $('qty').value;
	
		if(!qty.length)
			message[message.length] = 'Quantity is required.';
		else if(!qty_reg.test(qty))
			message[message.length] = 'Invalid Quantity.';
	
		var special_input_values = '';
		var attribute_ids = '';
		if($('has_attributes').innerHTML == 1){
	
			special_input_values = $('special_input_values').innerHTML;
			attribute_ids = $('attribute_ids').innerHTML;
			if(!attribute_ids.length)
				message[message.length] = 'Please Select Product Attributes.';
		}		
		if($('special_input_values').innerHTML != null){			
			special_input_values = $('special_input_values').innerHTML;
		}
		
	
		if(message.length){
	
			error_panel.setBody(message.join('<br />'));
			error_panel.show();
			return;
		}
	
		var product_id = t.href.substring(t.href.indexOf('#')+1);
		
		$('cart_loader').style.display = '';
		
		
		var params = 'action=add&product_id='+product_id+'&qty='+qty+'&attribute_ids='+attribute_ids+'&special_input_values='+special_input_values;		
		//alert('happens3');
		var callback = {success:cart_update_callback};		
		YAHOO.util.Connect.asyncRequest('POST', 'cart_update.php', callback, params);
	}
}


function product_details_bubble(e){

	var t = YAHOO.util.Event.getTarget(e);

	if(t.nodeName.toLowerCase() === 'input'){

		if(YAHOO.util.Dom.hasClass(t, 'saved-toggle')){

			$('saved_products_loader').style.display = '';

			var product_id = t.value;
			var action = t.checked?'add':'remove';
			var params = 'action='+action+'&product_id='+product_id;
			var callback = {success:saved_products_bubble_callback};
			YAHOO.util.Connect.asyncRequest('POST', 'saved_products_ajax.php', callback, params);
		}
	}
	else if(t.nodeName.toLowerCase() === 'img'){

		t = t.parentNode;

		if(YAHOO.util.Dom.hasClass(t, 'cart-add')){			
			YAHOO.util.Event.preventDefault(e);
			add_to_cart(t);			
		}
		else if(YAHOO.util.Dom.hasClass(t, 'cart-options')){			
			YAHOO.util.Event.preventDefault(e);

			$('attribute_panel_iframe').src = 'product_attributes.php?product_id='+$('product_id').innerHTML;
			$('attribute_panel_loader').style.display = '';
			attribute_panel.show();
		}
		else if(YAHOO.util.Dom.hasClass(t, 'cart-special-inputs')){
			YAHOO.util.Event.preventDefault(e);
			$('attribute_panel_iframe').src = 'special_inputs.php?product_id='+$('product_id').innerHTML;
			$('attribute_panel_loader').style.display = '';
			attribute_panel.show();
		}
	}
	else if(t.nodeName.toLowerCase() === 'a'){

		if(YAHOO.util.Dom.hasClass(t, 'help-save')){

			YAHOO.util.Event.preventDefault(e);

			help('template.help_save.html');
		}
		else if(YAHOO.util.Dom.hasClass(t, 'cart-options-edit')){

			YAHOO.util.Event.preventDefault(e);

			$('attribute_panel_iframe').src = 'product_attributes.php?product_id='+$('product_id').innerHTML;
			$('attribute_panel_loader').style.display = '';
			attribute_panel.show();
		}
	}
}



YAHOO.util.Event.on(window,'load',function(){

	YAHOO.util.Event.on('suggested-items','click',product_details_bubble);
	YAHOO.util.Event.on('shoppers-bought','click',product_details_bubble);
	YAHOO.util.Event.on('product-details','click',product_details_bubble);
	YAHOO.util.Event.on('cart-add','click',product_details_bubble);
	if($('amount_other'))
		YAHOO.util.Event.on('amount','change',gift_certificate_change);
});

})();
