// these items have to be in the global scope because they're referenced by a child iframe
var shipping_panel = null;
var error_panel = null;
var help_panel = null;
var attribute_panel = null;

function get_radio_value(radioElement){

	if(radioElement.length){
	
		for (var i = 0; i < radioElement.length; i++){
			if (radioElement[i].checked)
				return radioElement[i].value;
		}
	}
	else if (radioElement.checked)
		return radioElement.value;

	return '';
}

function escape(value){

	if(!YAHOO.lang.isValue(value)){

		value = '';
	}
	else if(YAHOO.lang.isString(value)){

		value = YAHOO.lang.trim(value);
		value = value.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
		value = encodeURIComponent(value);
	}

	return value;
}

function shipping_selected(){

	window.location = 'checkout.php';
}

function shopping_cart_totals_change(){

	shipping_panel.hide();
	$('cart_loader').style.display = '';

	var callback = {success:shopping_cart_totals_callback};
	YAHOO.util.Connect.asyncRequest('GET', 'shopping_cart_totals_ajax.php', callback);
}
function shopping_cart_totals_callback(o){

	$('cart_totals').innerHTML = o.responseText;
	$('cart_loader').style.display = 'none';
	new Effect.Highlight('shopping_header', {startcolor:'#729F2A', endcolor:'#E6E6E6'});
}

function cart_contents_change(e){

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

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

		if(YAHOO.util.Dom.hasClass(t, 'cart-qty')){

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

			var data = t.name

			var product_id = data.substring(0,data.indexOf('-'));
			var attribute_ids = data.substring(data.indexOf('-')+1);

			var params = 'action=set&product_id='+product_id+'&qty='+t.value+'&attribute_ids='+attribute_ids;
			var callback = {success:cart_update_callback};
			YAHOO.util.Connect.asyncRequest('POST', 'cart_update.php', callback, params);
		}
	}
}

function cart_update_callback(o){

	var json = YAHOO.lang.JSON.parse(o.responseText)

	if(json.error.length){

		error_panel.setBody(json.error);
		error_panel.show();
	}

	var callback = {success:shopping_cart_callback};
	YAHOO.util.Connect.asyncRequest('GET', 'shopping_cart_ajax.php', callback);
}

function shopping_cart_callback(o){

	if(o.responseText.length){

		$('shopping_cart').style.display = '';
		$('cart_contents').innerHTML = o.responseText;

		var inputs = $('cart_contents').getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++) {

			YAHOO.util.Event.on(inputs[i],'change',cart_contents_change);
		}

		shopping_cart_totals_change();
	}
	else{

		$('shopping_cart').style.display = 'none';
	}
}

function saved_products_bubble_callback(o){

	if(o.responseText.length){

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

		$('saved_products').innerHTML = o.responseText;
		$('saved_products_loader').style.display = 'none';
	}
	else{

		$('saved').style.display = 'none';
	}
}

function help(template){

	$('help_panel_iframe').src = template;
	$('help_panel_loader').style.display = '';
	help_panel.show();
}

(function() {


function saved_products_bubble(e){

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

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

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

			YAHOO.util.Event.preventDefault(e);

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

			var product_id = t.href.substring(t.href.indexOf('#')+1);

			var params = 'action=remove&product_id='+product_id;
			var callback = {success:saved_products_bubble_callback};
			YAHOO.util.Connect.asyncRequest('POST', 'saved_products_ajax.php', callback, params);
		}
	}
}



function cart_contents_click(e){

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

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

		if(YAHOO.util.Dom.hasClass(t, 'cart-remove')){

			YAHOO.util.Event.preventDefault(e);

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

			var data = t.href.substring(t.href.indexOf('#')+1);

			var product_id = data.substring(0,data.indexOf('-'));
			var attribute_ids = data.substring(data.indexOf('-')+1);

			var params = 'action=remove&product_id='+product_id+'&attribute_ids='+attribute_ids;
			var callback = {success:cart_update_callback};
			YAHOO.util.Connect.asyncRequest('POST', 'cart_update.php', callback, params);
		}
	}
}



function cart_totals_click(e){

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

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

		if(YAHOO.util.Dom.hasClass(t, 'cart-estimate-shipping')){

			cart_estimate_shipping();
		}
	}
	else if(t.nodeName.toLowerCase() === 'a'){

		if(YAHOO.util.Dom.hasClass(t, 'cart-edit-shipping')){

			YAHOO.util.Event.preventDefault(e);

			cart_estimate_shipping();
		}
	}
}

function cart_estimate_shipping(){

	$('shipping_panel_iframe').src = 'shipping_panel.php';
	$('shipping_panel_loader').style.display = '';
	shipping_panel.show();
}
function shipping_panel_loaded(){

	$('shipping_panel_iframe').style.display = '';
	$('shipping_panel_loader').style.display = 'none';
}
function shipping_panel_clear(){

	$('shipping_panel_iframe').src = 'about:blank';
}

function help_panel_loaded(){

	$('help_panel_iframe').style.display = '';
	$('help_panel_loader').style.display = 'none';
}
function help_panel_clear(){

	$('help_panel_iframe').src = 'about:blank';
}


function cart_checkout_click(e){

	if(!$('cart_edit_shipping')){

		$('shipping_panel_iframe').src = 'shipping_panel.php?checkout=1';
		$('shipping_panel_loader').style.display = '';
		shipping_panel.show();
	}
	else{

		window.location = 'checkout.php';
	}
}

function attribute_panel_loaded(){

	$('attribute_panel_iframe').style.display = '';
	$('attribute_panel_loader').style.display = 'none';
}
function attribute_panel_clear(){

	$('attribute_panel_iframe').src = 'about:blank';
}


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

	YAHOO.util.Event.on('saved_products','click',saved_products_bubble);
	YAHOO.util.Event.on('cart_contents','click',cart_contents_click);
	YAHOO.util.Event.on('cart_totals','click',cart_totals_click);
	YAHOO.util.Event.on('cart_checkout','click',cart_checkout_click);

	// accordions
	if($('accordion-wrap')){

		var uls = $('accordion-wrap').getElementsByTagName('ul');
		for(var i = 0; i < uls.length; i++){

			var element = YAHOO.util.Dom.getElementsByClassName('element', 'div', uls[i])[0];
			var toggler = YAHOO.util.Dom.getElementsByClassName('toggler', 'li', uls[i])[0];

			YAHOO.util.Event.on(toggler, 'click', function(){Effect.toggle(this, 'blind', {duration:.5})}, element, true);
		}
	}

	if($('cart_contents')){

		var inputs = $('cart_contents').getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++) {

			YAHOO.util.Event.on(inputs[i],'change',cart_contents_change);
		}
	}

	if($('shipping_panel')){

		shipping_panel = new YAHOO.widget.Panel(
			'shipping_panel',
			{
				visible:false,
				draggable:true,
				modal:true,
				fixedcenter:true,
				constraintoviewport:true,
				iframe:true,
				width:'620px',
				height:'550px'
			}
		);
		shipping_panel.render();
		YAHOO.util.Event.on('shipping_panel_iframe', 'load', shipping_panel_loaded);
		shipping_panel.hideEvent.subscribe(shipping_panel_clear,shipping_panel,true);
	}

	if($('error_panel')){

		error_panel = new YAHOO.widget.Panel('error_panel', {
			width:'300px',
			draggable:false,
			modal:true,
			fixedcenter:true,
			constraintoviewport:true,
			visible:false
		});
		error_panel.render();
		YAHOO.util.Event.on("error_panel_close", "click", error_panel.hide, error_panel, true);
	}

	if($('help_panel')){

		help_panel = new YAHOO.widget.Panel(
			'help_panel',
			{
				visible:false,
				draggable:true,
				modal:true,
				fixedcenter:true,
				constraintoviewport:true,
				iframe:true,
				width:'700px',
				height:'600px'
			}
		);
		help_panel.render();
		YAHOO.util.Event.on('help_panel_iframe', 'load', help_panel_loaded);
		help_panel.hideEvent.subscribe(help_panel_clear,help_panel,true);
		YAHOO.util.Event.on("help_panel_close", "click", help_panel.hide, help_panel, true);
	}

	if($('attribute_panel')){

		attribute_panel = new YAHOO.widget.Panel(
			'attribute_panel',
			{
				visible:false,
				draggable:true,
				modal:true,
				fixedcenter:true,
				constraintoviewport:true,
				iframe:true,
				width:'620px',
				height:'350px'
			}
		);
		attribute_panel.render();
		YAHOO.util.Event.on('attribute_panel_iframe', 'load', attribute_panel_loaded);
		attribute_panel.hideEvent.subscribe(attribute_panel_clear,attribute_panel,true);
	}

});

})();
