/*

$(document).ready(function () {
	$('#add_to_cart').removeAttr('onclick');

	$('#add_to_cart').click(function () {
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'html',
			data: $('#product :input'),
			success: function (html) {
				$('#module_cart .middle').html(html);
			},	
			complete: function () {
				var image = $('#image').offset();
				var cart  = $('#module_cart').offset();
	
				$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
	
				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),  
					height : $('#module_cart').height()
				};		
	
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});		
			}			
		});			
	});			
});

*/


var moduleCartTimeout ;
	
function addToModuleCart(pProductForm,pImage) {
	
	var vImage = '#' + pImage ;
	var vProduct = '#' + pProductForm ;

	clearTimeout(moduleCartTimeout);
	$('#module_cart').show() ;

	$.ajax({
		type: 'post',
		url: 'index.php?route=module/cart/callback',
		dataType: 'html',
		data: $(vProduct+' :input'),
		success: function (html) {
			$('#module_cart .middle').html(html);
		},	
		complete: function () {
			var image = $(vImage).offset();
			var cart  = $('#module_cart').offset();

			$('body').after('<img src="' + $(vImage).attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');


			params = {
				top : cart.top + 'px',
				left : cart.left + 'px',
				opacity : 0.0,
				width : $('#module_cart').width(),  
				height : $('#module_cart').height()
			};		

			$('#temp').animate(params, 'slow', false, function () {
				$('#temp').remove();
			});		
		}			
	});	
	
	moduleCartTimeout = setTimeout('$(\'#module_cart\').hide()',3000);
	
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace
    //
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

var selection_history = new Array();

function showStockStatus(product_id, cell_pid, stock_checkout) {
    
   $('#stock_status_' + cell_pid).html( $('#product_form_' + product_id + ' input[name=stock_status]').val() );

   if( $('#product_form_' + product_id + ' input[name=stock_qty]').val() > 0) {
       $('#stock_status_' + cell_pid).removeClass('red').addClass('green');
       $('#buybtn_' + cell_pid).show();
   } else {
       $('#stock_status_' + cell_pid).removeClass('green').addClass('red');

       // if backorders allowed show the buy button anyway
       if(stock_checkout=='1') {
	   $('#buybtn_' + cell_pid).show();
       } else {
	   $('#buybtn_' + cell_pid).hide();
       }
   }

   /*
    * replace the cells href to product selected in drop down
    * could not get .replace to work so imported str_replace()
   */
   var img_href = $('#product_image_'+cell_pid).parent().attr('href');

    str_from = "/"+cell_pid+"/";
    str_to = "/"+product_id+"/";

    if(typeof(selection_history[cell_pid]) != "undefined" && img_href !== null) {
	str_from = '/'+selection_history[cell_pid]+'/';
    }

   if(typeof(img_href) != "undefined" && img_href !== null) {	
	img_href = str_replace(str_from, str_to, img_href);
	$('#product_image_'+cell_pid).parent().attr('href', img_href);
   }

   var title_href = $('#product_title_'+cell_pid).attr('href');

   if(typeof(title_href) != "undefined" && title_href !== null) {
	title_href = str_replace(str_from, str_to, title_href);
	$('#product_title_'+cell_pid).attr('href', title_href);
   }

   /*
    * so we know what it has been changed to for the
    * next replace
   */
   selection_history[cell_pid] = product_id;

}

// To add multi attribute products to cart
// 21/01/2010 - Huseyin
function addToCart(product_id) {
	var param = "#proweight_" + product_id + " option:selected";
	attr_pro_id = $(param).attr("id");
	addToModuleCart('product_form_' + attr_pro_id,'product_image_' + product_id);
	
}

function addToCartProduct(product_id, var_qty) {
	if (var_qty == 1) {		
		qty = $("#pro_qty option:selected").attr("id");
		prm = "#qty_" + product_id;
		$(prm).val(qty);		
		addToModuleCart('product_form','product_image');
	}
	else if (var_qty > 1) {
		qty = $("#pro_qty option:selected").attr("id");

		var param = "#proweight_" + product_id + " option:selected";
		attr_pro_id = $(param).attr("id");
		prm = "#qty_" + attr_pro_id;
		$(prm).val(qty);
		//alert("attr_pro_id: " + attr_pro_id + " qty: " + qty);
		addToModuleCart('product_form_' + attr_pro_id,'product_image');
	}
	/*
	var param = "#proweight_" + product_id + " option:selected";
	attr_pro_id = $(param).attr("id");
	addToModuleCart('product_form_' + attr_pro_id,'product_image_' + product_id);
	*/
}



function addToWishList(product_id) {
	$.ajax({
		url: 'shop/lush/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			//$('.success, .warning, .attention, .information').remove();

			if (json['success']) {
				//$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');

				//$('.attention').fadeIn('slow');
				$('#button_addwishlist').html('<span>' + json['success']).fadeIn('slow') + '</span>';
				$('#wishlist_total').html(json['total']);

				//$('html, body').animate({ scrollTop: 0 }, 'slow');
			}
		}
	});
}

 function wishlistSearch() {
 	var val = $('#findlist').val();
	if(val=="") return false;
	location = '/shop/account/search&key='+$('#findlist').val();
 }
