jQuery.noConflict();
jQuery(document).ready(SiteInit);
function SiteInit(){
	SetArticlStile();
	ShopInit();
	BasketInit();
	jQuery('div.moduletable-video h3').click(function(){
		jQuery('div.module-video').toggle('slow');
	});
}

function SetArticlStile(){
	jQuery('div.items-row:even').addClass('items-row-brown');
}

function ShopInit(){
	jQuery('div.windowclose').click(function(){
		jQuery(this).parent().hide();
	});
	jQuery('.buttonbasket').hover(
		function(){
			jQuery(this).animate({
				'opacity':'0.5'
			}, 200);
		},
		function(){
			jQuery(this).animate({
				'opacity':'1'
			}, 200);
		}
		);
	jQuery('div.rightadd').click(function(){
		var value = parseInt(jQuery('input.countitem').val());
		if(value < 999) jQuery('input.countitem').val(value+1);
	});
	jQuery('div.leftadd').click(function(){
		var value = parseInt(jQuery('input.countitem').val());
		if(value > 1) jQuery('input.countitem').val(value-1);
	});
	jQuery('div.pane-info-pay').click(function(){
		jQuery('div.windowadd').show();
		jQuery('#itemid').val(jQuery(this).attr('title'));
		jQuery('input.addtocart').attr('disabled', '');		
	});
	jQuery('input.addtocart').click(function(){
		jQuery(this).attr('disabled', 'disabled');
		jQuery.ajax({
			type: 'POST',
			cache: false,
			dataType: 'json',
			url: jQuery('#BasketForm').attr('action'),
			data: jQuery('#BasketForm').serializeArray(),
			success: complateAddToBasket
		});
	});
	
}
function complateAddToBasket(data){
	if(data.result){
		jQuery('div.windowadd').hide();
		jQuery('div.windowresult').show();
	}
	
	jQuery('input.addtocart').attr('disabled', '');
}

/*---------------------- baskert ------------------*/
function BasketInit(){
	jQuery('input.decrcnt').click(function(){
		var input = jQuery(this).parent().find('input.cnt');
		var value = parseInt(jQuery(input).val());
		if(value > 1){
			jQuery(input).val(value - 1);		
			RecalcBasket();
		}
	});
	jQuery('input.addcnt').click(function(){
		var input = jQuery(this).parent().find('input.cnt');
		var value = parseInt(jQuery(input).val());
		if(value < 999){
			jQuery(input).val(value + 1);
			RecalcBasket();
		}
	});
	jQuery('div.deletethis').animate({
		'opacity':'0.5'
	}, 0);
	jQuery('div.deletethis').hover(
		function(){
			jQuery(this).animate({
				'opacity':'1'
			}, 200);
		},
		function(){
			jQuery(this).animate({
				'opacity':'0.5'
			}, 200);
		}
		);
	jQuery('div.deletethis').click(function(){
		jQuery(this).parent().parent().find('input.cnt').val(0);
		jQuery(this).parent().parent().hide();
		RecalcBasket();
	});
	jQuery('input.basket-save').click(function(){
		jQuery(this).attr('disabled', 'disabled');
		jQuery(this).animate({
			'opacity':'0.5'
		}, 200);
		jQuery.ajax({
			type: 'POST',
			cache: false,
			dataType: 'json',
			url: jQuery('#adminForm').attr('action'),
			data: jQuery('#adminForm').serializeArray(),
			success: complateSaveBasket
		});
	});
}
function complateSaveBasket(){
	jQuery('input.basket-save').attr('disabled', '');
	jQuery('input.basket-save').animate({
		'opacity':'1'
	}, 200);
	jQuery('input.basket-save').hide();
	jQuery('.basket-bay').show();
}

function RecalcBasket(){
	jQuery('.basket-bay').hide();
	jQuery('input.basket-save').show();
	jQuery('input.basket-save').css('display', 'block');
	var fullsum = 0;
	jQuery('input.cnt').each(function(){
		var price = parseFloat(jQuery(this).parent().parent().find('td.price span').text());
		var value = parseFloat(jQuery(this).val());
		var sum = number_format((price * value), 2, '.', '');
		jQuery(this).parent().parent().find('td.sum span').text(sum);
		fullsum += (price * value);
	});
	jQuery('div.fullsum').text(number_format(fullsum, 2, '.', '')+"$");
}

function number_format( number, decimals, dec_point, thousands_sep ) {	
	var i, j, kw, kd, km;

	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
	return km + kw + kd;
}
