function ShowIndicator(ob, name) {
	ob.children().wrapAll('<div id="indicator-transparent" />');
	jQuery('#indicator-transparent').animate({opacity: '0.3'}, 100);
	ob.append('<div id="indicator-text">'+name+'</div>');
	var indMargin = parseInt(jQuery('#indicator-text', ob).width())/2;
	jQuery('#indicator-text', ob).css('margin-left', '-'+indMargin+'px');
}

function RemoveIndicator(ob) {
	jQuery('#indicator-text', ob).remove();
	jQuery('#indicator-transparent', ob).animate({opacity: '1'}, 100, function() {
		jQuery(this).remove();
	});
}

function Add2Cart(itemId, itemQuantity) {
	var target = jQuery('#basket_result');
	
	jQuery.post('/catalog/add.php', { mode : 'ajax' , id : itemId , quantity : itemQuantity }, function(result){
		jQuery('#basket_button').click();
		ShowIndicator(target, 'Загрузка...');
		
		jQuery.post('/inc/template/basket_result.php', { mode : 'ajax' }, function(data){
			jQuery('#basket_add_result').html('<div class="basket_message">'+result+'</div>');
			RemoveIndicator(target);
			target.html(data);
		
	        /*Scroll for Basket*/
	        ShowScrollBasket();

              });
	});
     
}

function DeleteFromCart(cartId) {
	var target = jQuery('#basket_result');
	
	jQuery.post('/catalog/delete.php', { mode : 'ajax' , cartid : cartId }, function(result){
		ShowIndicator(target, 'Загрузка...');
		
		jQuery.post('/inc/template/basket_result.php', { mode : 'ajax' }, function(data){
			jQuery('#basket_add_result').html('<div class="basket_message">'+result+'</div>');
			RemoveIndicator(target);
			target.html(data);

	        /*Scroll for Basket*/
	        ShowScrollBasket();

		});
	});
}

function RecalculateOrder() {
	var delivery = jQuery('#delivery-val');
	var deliveryVal = parseFloat(delivery.text());
	var totalPrice = jQuery('#total-price');
	
	var priceSumm = 0;
	
	jQuery('div.checkout div.checkout_right span.price-val').each(function() {
		var id = jQuery(this).attr('id');
		var productId = id.substr(10);
		var priceVal = parseFloat(jQuery(this).text());
		var quantity = jQuery('#quantity-val-'+productId);
		var quantityVal = parseInt(quantity.text());
		
		priceSumm += priceVal*quantityVal;
	});
	
	priceSumm = priceSumm + deliveryVal;
	priceSumm = priceSumm.toFixed(2);
	
	totalPrice.text(priceSumm);
}


function ShowScrollBasket() {
	var iScrollHeight = document.getElementById('basket_result');
	if (iScrollHeight.offsetHeight > 400)
		{
			jQuery('#scroll_area').css({'height':'315px'});
			jQuery('#scroll_area').jScrollPane({showArrows:true, scrollbarWidth:19, dragMaxHeight:43});
					
			var popMargTop = ($('.popup_window').height() + 290) / 2;
		
			//Apply Margin to Popup
			$('.popup_window').css({ 
				'margin-top' : -popMargTop,
			});	
		}
	};




function ShowPopWindow(id) {
	//Get the screen height and width
	var maskHeight = jQuery(document).height();
	var maskWidth = jQuery(window).width();
	
	//Set heigth and width to mask to fill up the whole screen
	jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect		
	jQuery('#mask').fadeIn(1000).fadeTo("slow",0.8);


	//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
	var popMargTop = ($(id).height()) / 2;
		
	//Apply Margin to Popup
	$(id).css({ 
	   'margin-top' : -popMargTop,
	});

	//transition effect
	jQuery(id).fadeIn(2000);	
	

	/*Scroll for Basket*/
	ShowScrollBasket();

}

pLogin = false;

jQuery(document).ready(function() {
	jQuery('#option-sort-select').change(function() {
		jQuery('#option-sort').submit();
	});
	jQuery('#option-page-select').change(function() {
		jQuery('#option-page').submit();
	});
	
	jQuery('#sort-order div').click(function() {
		var link = jQuery(this);
		if(!link.hasClass('order-select'))
			return false;
		
		jQuery('#option-order-val').val(link.attr('rel'));
		jQuery('#option-order').submit();
	});
	
	//select all the a tag with name equal to modal
	jQuery('.popup_window').prependTo('body');
	jQuery('#mask').prependTo('body');
	
	jQuery('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		var link = jQuery(this);
		
		//Get the A tag
		var id = link.attr('modal');	
		
		ShowPopWindow(id);
	});
	
	//if close button is clicked
	jQuery('.popup_window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		jQuery('#mask').hide();
		jQuery('.popup_window').hide();
	});
	
	//if mask is clicked
	jQuery('#mask').click(function () {
		jQuery(this).hide();
		jQuery('.popup_window').hide();
	});


/*******************INPUT TYPE="FILE"***************************/
	$('.data-table input[type="file"]').wrap('<div class="f_imulation_wrap"><div class="im_button"></div></div>').addClass('imulated');
	$('.im_button').prepend('<span class="inptext">Обзор</span>');
	$('.f_imulation_wrap').prepend('<div class="im_input"><input type="text"></div>');

	$('.imulated').mouseover(function(){
		$('.im_button').addClass('act');
		$(this).css('cursor','pointer');
	});
	$('.imulated').mouseout(function(){
		$('.im_button').removeClass('act');
	});
		$('.imulated').change(function(){
		$('.im_input input').val($(this).val());
	});
		$('.im_input input').click(function(){
		$('.imulated').trigger('click');
	});				

	// new menu
	$('.bigmenu a.trig').parent('li').addClass('close');
	$('.bigmenu a.trig').click(function() {
		$(this).parent('li').children('ul').slideToggle('normal', function() {
			$(this).parent('li').toggleClass('opened');
		});
		return false;
	});
	$('.bigmenu .first').prepend('<img src="/img/first-top.jpg" />').append('<img src="/img/first-bottom.jpg" />');
	$('.bigmenu .second').prepend('<img src="/img/second-top.jpg" />').append('<img src="/img/second-bottom.jpg" />');
	$('.bigmenu .third').prepend('<img src="/img/third-top.jpg" />').append('<img src="/img/third-bottom.jpg" />');
	$('.bigmenu').append('<img src="/img/block-menu-bottom.jpg" />');
	/*$('.bigmenu li:last').css('border','none');*/

});
