/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function updatePx($obj) {

	// Check for Options. If there are none, return.
	if ($($obj).length == 0) { return; }

	$.ajax({
		type: 'post',
		url: 'index.php?route=product/option_price_update/updatePrice',
		//dataType: 'html',
		dataType: 'json',
		//data: $($obj +' input[type=\'text\'], '+$obj+' input[type=\'hidden\'], '+$obj+' input[type=\'radio\']:checked, '+$obj+' input[type=\'checkbox\']:checked, '+$obj+' select, '+$obj+' textarea'),
		//data: $($obj +' input[type=\'radio\']:checked, '+$obj+' input[type=\'hidden\'], '+$obj+' input[type=\'checkbox\']:checked, '+$obj+' select, '+$obj+' input[name=\'quantity\']'),
		data: $($obj +' input[type=\'radio\']:checked, '+$obj+' input[type=\'hidden\'], '+$obj+' input[type=\'checkbox\']:checked, '+$obj+' select'),
		
                beforeSend: function() {
			//$('#opu_price').html('<img style="margin-left:5px;" src="catalog/view/theme/default/image/opu_ajax_load.gif" alt="" />');
			//$('#opu_special').html('<img style="margin-left:5px;" src="catalog/view/theme/default/image/opu_ajax_load.gif" alt="" />');
			//$('#opu_tax').html('<img style="margin-left:5px;" src="catalog/view/theme/default/image/opu_ajax_load.gif" alt="" />').delay(400);
		},
		success: function (mydata) {
			// Update the main price with the new price.

			// SLIDE EFFECTS DON'T WORK ON ALL BROWSERS PROPERLY. USE FADE INSTEAD.
			//$('#opu_price').slideUp('100').queue(function(n){$(this).html(mydata.price); n();}).slideDown('100');
			//$('#opu_special').slideUp('100').delay(100).queue(function(n){$(this).html(mydata.special); n();}).slideDown('100');
			//$('#opu_tax').slideUp('100').delay(200).queue(function(n){$(this).html(mydata.tax); n();}).slideDown('100');

			$('#opu_price').fadeOut('100').queue(function(n){$(this).html(mydata.price); n();}).fadeIn('100');
			$('#opu_special').fadeOut('100').delay(100).queue(function(n){$(this).html(mydata.special); n();}).fadeIn('100');
			$('#opu_tax').fadeOut('100').delay(200).queue(function(n){$(this).html(mydata.tax); n();}).fadeIn('100');
		},
		error: function (mydata) {
			alert('error in price update:' + mydata.price);
		}
	});
}

$(document).ready(function () {

	// Check if 1.4.x or 1.5.x
	$ocver = false;
	if ($('.product-info select').length || $('.product-info :input[type=radio]').length || $('.product-info :input[type=checkbox]').length) {
		$ocver = '15x'; // 1.5.x
		$obj = '.product-info';
	} else if ($('#product select').length) {
		$ocver = '14x'; // assume 1.4.x
		$obj = '#product';
	}
	//alert($ocver);


	// If ocver is false, then there are no options on the page
	if ($ocver) {

		// Update on initial page load for 1.4.x only
		if ($ocver == '14x') {
			updatePx($obj);
		}

		// Update whenever the triggerable page inputs are changed
		$($obj).change(function(){
			updatePx($obj);
		});
	}

});


