/**
 * @author Administrator
 */

$(document).ready(function() {
	initButtons();
});

function reloadShoppingCart(amount) {

	data = new Object();
	if($('#svShoppingCartPortal').size() != 0){
		data.action = 'getShoppingCart';
		$("#svShoppingCartPortal .cartContainer").load('/plugins/'+pluginID+'/ajax/ajax.cfm .cartContainer > *',data,function(response){		
			$("input[name=shopTotal]").val($("strong#totalPrice").html());
			if(amount == 0){
				$('.address').remove();		
			}
			initButtons();
			Shadowbox.setup($('a.gallery'));
		});
	} else {
		data.action = 'getShoppingCartShort';
		$(".warenkorb").load('/plugins/'+pluginID+'/ajax/ajax.cfm',data,function(response){		
		    
		});
	}
	

}

function initButtons(){
	// "In Warenkorb"-Button
	$('.addToShoppingCart').click(function(){
		articleIDstr = $(this).attr('name');
		articleID = articleIDstr.split("button_")[1];
		data = new Object();
		data.action = 'addToShoppingCart';
		data.articleID = articleID;
		$.ajax({
			  url: '/plugins/'+pluginID+'/ajax/ajax.cfm',
			  type: 'POST',
			  data: data,
			  success: function(amount) {
				$("div#"+articleID).html(amount);
				reloadShoppingCart(trim(amount));
			   // alert(amount);
				/*$('.result').html(data);
			     * 
			    alert('Load was performed.'); */
			  }
			});
		return false;
	});

	$('.removeFromShoppingCart').click(function(){
		articleIDstr = $(this).attr('name');
		articleID = articleIDstr.split("button_")[1];
		data = new Object();
		data.action = 'removeFromShoppingCart';
		data.articleID = articleID;
		$.ajax({
			  url: '/plugins/'+pluginID+'/ajax/ajax.cfm',
			  type: 'POST',
			  data: data,
			  success: function(amount) {
				$("div#"+articleID).html(amount);
				reloadShoppingCart(trim(amount));
			    //alert(amount);
				/*$('.result').html(data);
			     * 
			    alert('Load was performed.'); */
			  }
			});
		return false;
	});	
}

function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}
