$(document).ready(function(){
	$('a.multiple').click(function(){
		$('div.singleContainer').hide();
		$('div.chooser').css('background-image','url(\'/_inc/img/bgChooserMultiple.png\')');
		$('div.multipleContainer').show();
	});
	$('a.single').click(function(){
		$('div.singleContainer').show();
		$('div.chooser').css('background-image','url(\'/_inc/img/bgChooser.png\')');
		$('div.multipleContainer').hide();
	});

	$('#masterImageFileInvisible').bind('change', function(){
		var val = $(this).val();
		val = val.split('\\');
		val = val[val.length-1];
		$('#masterImageFileVisible').val(val);
	});
	
	var selOption = $('#productFormatSelector option:selected').val();
		
	$('.format').change(function(){
		$('#size5_'+ selOption).attr('checked','checked');
		var val = $(this).val();
		$('#size5_'+ selOption).val(val);
	});
	$('input[name=size]').click(function(){
		if($(this).attr('id') != 'size5'){
			$('#format').val('0');
		}
	});
	$('.multipleContainer img.win').bind('mouseover',function(){
		$(this).attr('src','/_inc/img/winSoft_hover.png');
	});
	$('.multipleContainer img.win').bind('mouseout',function(){
		$(this).attr('src','/_inc/img/winSoft.png');
	});
	$('.multipleContainer img.mac').bind('mouseover',function(){
		$(this).attr('src','/_inc/img/macSoft_hover.png');
	});
	$('.multipleContainer img.mac').bind('mouseout',function(){
		$(this).attr('src','/_inc/img/macSoft.png');
	});
	$('.bigBox img.win').bind('mouseover',function(){
		$(this).attr('src','/_inc/img/winSoftService_hover.gif');
	});
	$('.bigBox img.win').bind('mouseout',function(){
		$(this).attr('src','/_inc/img/winSoftService.gif');
	});
	$('.bigBox img.mac').bind('mouseover',function(){
		$(this).attr('src','/_inc/img/macSoftService_hover.gif');
	});
	$('.bigBox img.mac').bind('mouseout',function(){
		$(this).attr('src','/_inc/img/macSoftService.gif');
	});

	//slider
	$("#chained").scrollable({circular: true, speed: 1000 }).navigator().autoscroll({
		interval: 5000
	});
	//tabs
	$('.right').hide();
	$('.right.first').show();
	$('#tabs li').click(function(){
		$(this).parent().parent().find('li a').removeClass('active');
		$('a',this).addClass('active');
		var i = $(this).index();
		$(this).parent().parent().parent().find('.right').hide();
		$(this).parent().parent().parent().find('.right:eq('+i+')').show();
	});
	
	// show all tables
	$('.allFormats').hide();
	$('.tableSwichterHide').hide();
	
	$('#tableShow a').click(function(){
		$(this).parent().parent().find('#tableShow').hide();
		$(this).parent().parent().find('#tableHide').show();
		$(this).parent().parent().find('.allFormats').show();
	});
	$('#tableHide a').click(function(){
		$(this).parent().parent().find('#tableHide').hide();
		$(this).parent().parent().find('#tableShow').show();
		$(this).parent().parent().find('.allFormats').hide();
	});	
	//selectorTables
	$('.selectorTable').hide();
	

	$('#productOptionTable_'+selOption).show();
	
    $('#productFormatSelector').bind('change',function(){
		selOption = $(this).find('option:selected').val();
		$('.selectorTable').hide();
		$('#productOptionTable_'+$(this).find('option:selected').val()).show();
		  });	

	// productDetailPage
	
	if ($('#changeImage').length == 0)	{
		$('#imageIntroducer').show();
		$('#uploadContainer').show();
	} else {
		$('#uploadContainer').hide();
		$('#imageIntroducer').hide();
	}
	
	$('#changeImage a').click(function(){
		$('#uploadContainer').show();
		$('#imageIntroducer').show();
		$('#imageContainer').hide();	
		$('#changeImage').hide();
		/*
		var id = $(this).attr('rel');
		$.get("/projectpath/system/shopProducts/basketUpdater.html?client_as_bxmediaplusclass=BXMediaPlus&edit_BXMediaPlusMasterImage_OID="+id+"&client_request_mode=&client_request_auswahl=&edit_cmd_commit=true&edit_delete_BXMediaPlusMasterImage=true");
		*/
	});
	
	// shop Basket
	/*$('#shopBasketRemover a').click(function(){
		var id = $(this).attr('rel');
		$.get("/projectpath/system/shopProducts/basketUpdater.html?i_cmd_basket:Basket[]_removeEntry_param_entry=basket:"+id+"&i_cmd_basket:Basket[]_removeEntry=true");
	});*/

	// shop Basket increment
	/*$('#shopBasketIncrease{=cartEntries_actualRowIndex}').bind('click',function(){

	});		*/
	
	// shop Checkout
	if ($('#checkQuickPayment').attr('checked') == true)
		$('#quickPayment').hide();
	$('#checkQuickPayment').change(function(){
		if($('#checkQuickPayment').attr('checked') == true)
		{
			$('#quickPayment').hide();
		} else
		{
			$('#quickPayment').show();
		}
	});

	
});
function doAfterRemove(){
	window.location = nextPage;
}
function dummyCallback()
{}
var nextPage = "";
function removeEntry(id, page){	
	var basketEntry = $('#shopBasketRemover'+id+' a').attr('rel');
	$.get("/public/dummy.html?i_cmd_basket:Basket[]_removeEntry_param_entry=basket:"+basketEntry+"&i_cmd_basket:Basket[]_removeEntry=true");
	nextPage = page;
	$('#tablerowidsmall_'+id).fadeOut(50,dummyCallback);
	$('#tablerowidsmall2_'+id).fadeOut(50,doAfterRemove);
}

var basketAmount = new Array(100);
function increaseBasketAmount(id){
	var amount = $('#smallBasketAmountStepper'+id).val();
	$('#smallBasketAmountStepper'+id).val(parseInt(amount) + 1);
	var basketEntry = $('#smallBasketAmountStepper'+id).attr('rel');
	$.get("/projectpath/system/shopProducts/basketUpdater.html?i_cmd_"+basketEntry+"_updateQuantity=true&i_cmd_"+basketEntry+"_updateQuantity_param_mode=INCREASE&i_cmd_"+basketEntry+"_updateQuantity_param_amount=1",
		function(data){
			updatePrice(data);
		});
	basketAmount[id] = $('#smallBasketAmountStepper'+id).val();
	if (basketAmount[id] > 1)
	{
		$('#shopBasketDecrease'+id).show();
	}
}
function decreaseBasketAmount(id){
	var amount = $('#smallBasketAmountStepper'+id).val();
	var basketEntry = $('#smallBasketAmountStepper'+id).attr('rel');
	if (amount > 1)
	{
		$('#smallBasketAmountStepper'+id).val(parseInt(amount) - 1);
		$.get("/projectpath/system/shopProducts/basketUpdater.html?i_cmd_"+basketEntry+"_updateQuantity=true&i_cmd_"+basketEntry+"_updateQuantity_param_mode=DECREASE&i_cmd_"+basketEntry+"_updateQuantity_param_amount=1",
			function(data){
				updatePrice(data);
			});
		basketAmount[id] = $('#smallBasketAmountStepper'+id).val();
	}
	if (basketAmount[id] == 1) 
	{
		$('#shopBasketDecrease'+id).hide();
	}
}

function changeBasketAmount(id,value)
{
	var amount = value;
	var basketEntry = $('#smallBasketAmountStepper'+id).attr('rel');
	var keyWord = "";
	if (amount > basketAmount[id])
	{
		changeAmount = amount - basketAmount[id];
		basketAmount[id] = parseInt(basketAmount[id]) + parseInt(changeAmount);
		keyWord = "INCREASE";
	} else
	{
		keyWord = "DECREASE";
		changeAmount = basketAmount[id] - amount;
		basketAmount[id] = parseInt(basketAmount[id]) - parseInt(changeAmount);
	}
	$.get("/projectpath/system/shopProducts/basketUpdater.html?i_cmd_"+basketEntry+"_updateQuantity=true&i_cmd_"+basketEntry+"_updateQuantity_param_mode="+keyWord+"&i_cmd_"+basketEntry+"_updateQuantity_param_amount="+ changeAmount,
		function(data){
			updatePrice(data);
		});	
}
function amountFocus(id, value){
	basketAmount[id] = value;
}

function updatePrice(data){
	data = data.replace(/^\s+/g, '').replace(/\s+$/g, '')
	var parts = data.split(';');	
	for (var k=0; parts.length > k; k++)
	{
		parts[k] = parts[k].replace(/^\s+/g, '').replace(/\s+$/g, '')
		if (!parts[k].search('ID'))
		{
			var id = (parts[k]).split(':');
			id = id[1];
			k++;
			var price = (parts[k]).split(':');
			price = price[1];
			k++;
			var singlePrice = (parts[k]).split(':');
			singlePrice = singlePrice[1];	
			$('#basketPrice'+id).html(price);
			$('#smallBasketPrice'+id).html(price);
			$('#smallBasketAmountStepper'+id).val(basketAmount[id]);
			$('#basketAmountStepper'+id).val(basketAmount[id]);
			$('#basketSinglePrice'+id).html(singlePrice);
			
		}
		if (!parts[k].search('total'))
		{
			var priceTotal = (parts[k]).split(':');
			priceTotal = priceTotal[1];
			k++;
			var vat = (parts[k]).split(':');
			vat = vat[1];
			k++;
			var codeTotal = (parts[k]).split(':');
			codeTotal = codeTotal[1];	
			$('#basketTotalPrice').html(priceTotal);
			$('#smallBasketTotalPrice').html(priceTotal);
			$('#basketTotalVAT').html(vat);
		}
	}
}
