// JavaScript Document


function maincarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        if(carousel.options.auto < 0) {
            carousel.startAuto(-carousel.options.auto);
        }
    });
    carousel.buttonPrev.bind('click', function() {
        if(carousel.options.auto > 0) {
            carousel.startAuto(-carousel.options.auto);
        }
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function newprodscarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        if(carousel.options.auto < 0) {
            carousel.startAuto(-carousel.options.auto);
        }
    });
    carousel.buttonPrev.bind('click', function() {
        if(carousel.options.auto > 0) {
            carousel.startAuto(-carousel.options.auto);
        }
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

var lngCode;
var curDivState = 1; 
var currencyShow = function (curDivState, obj, trig){
	$(trig).unbind('click') ;
	$(obj).animate({
		
		height: $(obj).height()+95*curDivState
	  }, 500, function() {
		  curDivState = -1 * curDivState;
		  $(trig).click(function(){currencyShow(curDivState, obj, trig);}) ;
		  
	  });
}

var paymentMethodInfo = function(){
	$('.payment-method-info').children().hide();
	$('.payment-method-info' + $(this).val()).show();
	
}

var toggleAddressBlock = function(elem){
	if ($(elem).is(':checked')){
		$(".addresses-block").hide();
		$("#shop_address").attr('noname', $("#shop_address").attr('name')) ;
		$("#shop_address").attr('name','') ;
	} else {
		$(".addresses-block").show();
		$("#shop_address").attr('name', $("#shop_address").attr('noname')) ;
		$("#shop_address").attr('noname','') ;
	}
}

$(document).ready(function() {
	
	$(".user-type-radio").click(function(){
		if($(this).attr('id') == 'radio_corporate'){
			$('.corporate_fields').show();
		} else {
			
			$('.corporate_fields').hide() ;
		}
		
		if($(this).attr('id') == 'radio_dealer'){
			$('.dealer_fields').show();
		} else {
			
			$('.dealer_fields').hide() ;
		}
		
	}) ;
	$(".super_goods_item").each(function(){
		
		$(this).find('.list-product-price-container-for-grid-and-list').html($(this).find('.list-product-price-container').html()) ;
	}) ;
	

	$(".compare-toggle-area").click(function(){
		if (parseInt($(".compare-count").html())>0)
		$(this).siblings(".compare-block").slideToggle();
	});
	
	$(".first-cur").click(function(){
		
		
		currencyShow(curDivState, $(".currency-div"), this) ;
		
		
		
	});
	
	
	
	lngCode = $("#lngcode").val();
	
	$(".product-more-options").click(function(){
		$(".product-options").slideToggle();
	});
	
	$("#currency_select").change(function(){
		document.forms['currency_form'].submit() ;
	});
	
	$("#maincarouseldiv").mouseenter( function(){
		$(this).find(".jcarousel-next").fadeIn();
		$(this).find(".jcarousel-prev").fadeIn();
	} ) ;
	
	$("#maincarouseldiv").mouseleave( function(){
		$(this).find(".jcarousel-next").hide();
		$(this).find(".jcarousel-prev").hide();
	} ) ;
	$("#hurrycarouseldiv").mouseenter( function(){
		$(this).find(".jcarousel-next").fadeIn();
		$(this).find(".jcarousel-prev").fadeIn();
	} ) ;
	
	$("#hurrycarouseldiv").mouseleave( function(){
		$(this).find(".jcarousel-next").hide();
		$(this).find(".jcarousel-prev").hide();
	} ) ;
	$("#newprodscarouseldiv").mouseenter( function(){ 
		$(this).find(".jcarousel-next").fadeIn();
		$(this).find(".jcarousel-prev").fadeIn();
	} ) ;
	
	$("#newprodscarouseldiv").mouseleave( function(){
		$(this).find(".jcarousel-next").hide();
		$(this).find(".jcarousel-prev").hide();
	} ) ;
	
	$(".auctiondiv").mouseenter( function(){ 
		$(this).find(".jcarousel-next").fadeIn();
		$(this).find(".jcarousel-prev").fadeIn();
	} ) ;
	
	$(".auctiondiv").mouseleave( function(){
		$(this).find(".jcarousel-next").hide();
		$(this).find(".jcarousel-prev").hide();
	} ) ;
	
	
	
	$(".optRadio").click(function() {
	    var previousValue = $(this).attr('previousValue');
	    $(".optRadio").attr( 'previousValue', 'false' ) ;
	    if(previousValue == 'true'){
	        $(this).attr('checked', false)
	    }
	    
	    $(this).attr('previousValue', $(this).attr('checked'));
	});
		
		$(".optRemove").click(
						function(){
								
							$(this).parent().remove();
							optionsCollection = optionsCollection.replace($(this).parent().attr("id")+";", "");
							
						});
	
	
	$(".super_search_link").click(function(){
		
		
		
		if ($("#advsearch").is(":hidden")) 
			$('#search').append('<input type="hidden" value="do_adv" id="do_adv" name="do_adv" />'); 
		else 
			$("#do_adv").remove(); 
		$("#advsearch").slideToggle(); 
		return false;
	});
	
	$(".filter_opts_link").click(function(){
		
		
		
		if ($("#filter_opts_div").is(":hidden")) 
			$('#filter_form').append('<input type="hidden" value="do_adv" id="do_adv" name="filter[do_adv]" />'); 
		else 
			$("#do_adv").remove(); 
		$("#filter_opts_div").slideToggle();
		return false;
	});
	
    var $jc = $.jcarousel;
    $jc.fn.extend({
        startAuto: function(s) {

            if (s !== undefined) {
                this.options.auto = s;
            }

            if (this.options.auto === 0) {
                return this.stopAuto();
            }

            if (this.timer !== null) {
                return;
            }

            this.autoStopped = false;

            var self = this;

            //console.log(this.options.auto);
            if (this.options.auto < 0) {
                this.timer = window.setTimeout(function() {
                    self.prev();
                }, -this.options.auto * 1000);
            } else {
                this.timer = window.setTimeout(function() {
                    self.next();
                }, this.options.auto * 1000);
            }
        }
    });
    
    
    
	
	
	$(".super_price").mouseenter(function(){
		$(this).children('.currencies').fadeIn(100);
		
	});

	$(".super_price").mouseleave(function(){
		$(this).children('.currencies').fadeOut(100);
		
	});
	
	$(".prices").mouseenter(function(){
		$(this).children('.currencies').fadeIn(100);
		
	});

	$(".prices").mouseleave(function(){
		$(this).children('.currencies').fadeOut(100);
		
	});
	
	$(".pricer").mouseenter(function(){
		$(this).children('.currencies').fadeIn(100);
		
	});

	$(".pricer").mouseleave(function(){
		$(this).children('.currencies').fadeOut(100);
		
	});
	
	$('#opts').find('div').hide();	
	$('#opts li').find('span').click(function(){
		$(this).parent().find('div').slideToggle(100);
	});
	
	$('#filter_opts').find('div').hide();	
	$('#filter_opts li').find('span').click(function(){
		$(this).parent().find('div').slideToggle(100);
	});

	//DD_roundies.addRule('.rounds5', '5px');
	
		/**CATALOGUE*/
		
		$(".send-to-friend").click(function(){
			$(this).children().slideDown();
		});
	
		$('.menusub').hide();
		$('.menusubsub').hide();
		$('.menuparent').click(function() {
			$('.menusub').removeClass('db');
			_art = '#sub_' + this.id;
			$(_art).slideToggle(200);
			$(_art).toggleClass('inview');
			/*if($(_art).hasClass('inview'))
			{
				
				$(_art).removeClass('inview');
				$(_art).slideUp(200);
			}
			else
			{
				$('.menusub').removeClass('inview');
				$('.menusub').hide();

				$(_art).addClass('inview');
				$(_art).slideDown(200);
			}*/

			//return false;
			});
		
		

		$('.menusubparent').click(function() {

			$('.menusubsub').removeClass('db');
			_art = '#subsub_' + this.id;

			if ($(_art).hasClass('inview')) {
				$(_art).removeClass('inview');
				$(_art).slideUp(200);
			} else {
				$('.menusubsub').removeClass('inview');
				$('.menusubsub').hide();

				$(_art).addClass('inview');
				$(_art).slideDown(200);
			}

			//return false;
			});

		/**PRODUCERS**/
		_getShopingCart();
		$('.prodsub').hide();
		$('.prodparent').click(function() {
			$('.prodsub').removeClass('db');
			_art = '#sub_' + this.id;

			if ($(_art).hasClass('inview')) {
				$(_art).removeClass('inview');
				$(_art).slideUp(200);
			} else {
				$('.prodsub').removeClass('inview');
				$('.prodsub').hide();

				$(_art).addClass('inview');
				$(_art).slideDown(200);
			}

			//return false;
			});
		$('.selproducers').trigger('click');

		_selcat = $('.selcat');

		$('.selcat').trigger('click');
		if (_selcat.length < 1)
			$('.menuparent:first').trigger('click');

		$('.overlay').colorbox();
		//$('.add-to-cart').colorbox({ width:'60%', height:'80%',onComplete:addToCartLaoded,overlayClose:false});
		$(".add-to-cart").click(			
				
				function ()    { 
					if ( $(this).attr("href") )
					{
									
									var _addToCardDialog = document.createElement('div');
									$(_addToCardDialog).attr("id","dialog");
									var _loaderImage = new Image();
									_loaderImage.src = "/images/loading.gif";
									$(_addToCardDialog).html(_loaderImage);
								var ahref=$(this).attr("href");
								//var tit=$(this).attr("title");
					        $(_addToCardDialog).dialog({
					            modal: true,
					            open: function ()
					            {
								
					                $(this).load(ahref);
									
					            },    
					           
					            position:'center',
					            width: 650,
					            //title: tit,
								close: function(e, i) { $(this).remove(); }
					        });
					        $(".ui-dialog-titlebar").hide();
					        return false;
			}
}

);
		

		
	});

var newsletterSubscribe = function (){
	
	var url = "/users" ;

	$.ajax( {
		url : url,
		type: "POST",
		dataType: 'json',
		data : "captcha="+ $("#captcha").val() +"&newsletter[name]=" + $("#newsletter-name").val() + "&newsletter[email]=" + $("#newsletter-email").val() +"&action=newslettersubscribe",
		error:function()
		{
			$('.newsletter-err').css('color','red') ;
			$('.newsletter-err').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			if (data.statusFlag == 1) 		
				$('.newsletter-err').css('color','green') ;
			else 
				$('.newsletter-err').css('color','red') ;
			$('.newsletter-err').html(data.statusStr) ;
			
			
		}
	});
	
	
}


var showNewsletterDialog = function (elem){
	
	
				if ( $(elem).attr("href") )
				{
								
								var _newsletterDialog = document.createElement('div');
								$(_newsletterDialog).attr("id","dialog");
								var _loaderImage = new Image();
								_loaderImage.src = "/images/loading.gif";
								$(_newsletterDialog).html(_loaderImage);
							var ahref=$(elem).attr("href");
							//var tit=$(this).attr("title");
				        $(_newsletterDialog).dialog({
				            modal: true,
				            open: function ()
				            {
							
				                $(this).load(ahref);
								
				            },    
				           
				            position:'center',
				            width: 350,
				            //title: tit,
							close: function(e, i) {$(_newsletterDialog).remove()}
				        });
				        $(".ui-dialog-titlebar").hide();
				        return false;

				}

	
}



var setProductMark = function (productid, mark, elem){
	var url = "/products" ;
	 
	var obj = $(elem).parent() ; 
	
	obj.children('div').attr('disabled', 'disabled');

	obj.next().html('<img style=" bottom:0px; width:20px;" src="/images/loading.gif" />');
	$.ajax( {
		url : url,
		type: "POST",
		dataType: 'json',
		data : "mark[productid]=" + productid + "&mark[mark]=" + mark +"&action=setProductMark",
		error:function()
		{
			$('.markInps').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			
			obj.children('div').removeClass('rate-yellow');
			obj.children('div').removeClass('rate-users-mark');
			obj.children('div').addClass('rate-grey');
			obj.children('div').each(function(){
	
				if ( Math.round(parseFloat(data.currentMark)) >= parseInt($(this).attr("val")) ){
					
					$(this).removeClass('rate-grey');
					$(this).removeClass('rate-users-mark');
					$(this).addClass('rate-yellow');
				}
				if ( parseInt(mark) == parseInt($(this).attr("val")) ){
					
					$(this).removeClass('rate-grey');
					$(this).addClass('rate-users-mark');
				}
			});
			
			
			
			
			obj.children('div').removeAttr('disabled');
			obj.next().html( data.currentMark );
			
		}
	});
}

var compareClearAll = function(){
	var url = "/products" ;
	$.ajax( {
		url : url,
		type: "POST",
		data : "action=compareclearall",
		error:function()
		{
			$('.compare-list').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			
				$('.compare-list').html(data);
			
					$(".compare-button").removeAttr('href') ;
				
					
					$(".compare-count").html(0);
					$(".compare-button-clear-all").hide() ;
					$(".compare-block").slideUp();
					$(".is-in-compare").removeClass('is-in-compare') ;
					
		}
	});
	return false;
}

var setViewMode = function(mode){
	var url = "/products" ;
	
	
	$.ajax( {
		url : url,
		type: "POST",
		data : "action=setviewmode&view_mode="+mode,
		error:function()
		{
			//$('#compare-list').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
					
				
					$(".super_goods_item").removeClass('list') ;
					$(".super_goods_item").removeClass('classic') ;
					$(".super_goods_item").removeClass('grid') ;
					
					
					
					$(".mode-link").removeClass('blue') ;
					$(".mode-link").removeClass('grey') ;
					$(".mode-link").addClass('grey') ;
					$(".mode-link-"+mode).removeClass('grey') ;
					$(".mode-link-"+mode).addClass('blue') ;
					
					$(".super_goods_item").addClass(mode) ;
					
		}
	});
	return false;
}

var setOrder = function (field, direction){
	var url = "/home" ;
	dir = parseInt(direction) * (-1) ; 

	
	$.ajax( {
		url : url,
		type: "POST",
		
		data : "order[field]=" + field + "&order[direction]="+dir+"&action=setorder",
		error:function()
		{
			
			//$('#compare-list').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			location.reload(true);
			
		}
	}) ;
}

var addremoveCompareProduct = function (productid, elemId) {
	var url = "/products" ;
	
	if ($("#" + elemId).hasClass('is-in-compare') == false ) { 
	$.ajax( {
		url : url,
		type: "POST",
		dataType: 'json',
		data : "compare[productid]=" + productid + "&action=addcompareproduct",
		error:function()
		{
			$('.compare-list').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			
			if ( data.prodId )
			{
				$("#" + elemId).addClass('is-in-compare') ;
			$('.compare-list').append('<input checked type="checkbox" id="compare_'+data.prodId+'" class="compare_'+data.prodId+'" onclick="removeCompareProduct('+data.prodId+')" /><label class="fs12 font-white compare_label_'+data.prodId+'"  for="compare_'+data.prodId+'">'+data.prodName+'<br /><br /></label>');
			
			$(".compare-count").html(data.prodsNum );
			if (data.prodsNum > 1){
				$(".compare-button").attr('href', $(".compare-button").attr('url')) ;
				
			}
			if (data.prodsNum > 0)
				$(".compare-button-clear-all").show() ;
			}
			
		}
	}) ;
	} else {
		$.ajax( {
			url : url,
			type: "POST",
			dataType: 'json',
			data : "compare[productid]=" + productid + "&action=removecompareproduct",
			error:function()
			{
				$('.compare-list').html( 'Internal Server Error' );	
			},
			success : function(data) 
			{
				
				if ( data.prodId ){
					$("#" + elemId).removeClass('is-in-compare') ;
					
				$('.compare_'+data.prodId).remove();
				$(".compare_label_"+data.prodId).remove();
				
				$(".compare-count").html(data.prodsNum);
				if (data.prodsNum < 2){
					$(".compare-button").removeAttr('href') ;
				}
				if (data.prodsNum < 1){
					$(".is-in-compare").removeClass('is-in-compare') ;
					$(".compare-button-clear-all").hide() ;
					$(".compare-block").slideUp() ;
				 }
				}
				
			}
		});
	}
	return false;
}

var removeCompareProduct = function (productid) {
	var url = "/products" ;
	
	$.ajax( {
		url : url,
		type: "POST",
		dataType: 'json',
		data : "compare[productid]=" + productid + "&action=removecompareproduct",
		error:function()
		{
			$('.compare-list').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			if ( data.prodId ){
				$('.compare_'+data.prodId).remove();
				$(".compare_label_"+data.prodId).remove();
				
				$("#compare_add_rm_"+productid).removeClass('is-in-compare') ;
				
				if (data.prodsNum < 2){
					$(".compare-button").removeAttr('href') ;
				}
			
				$(".compare-count").html(data.prodsNum);
				if (parseInt(data.prodsNum) < 1){
					$(".compare-button-clear-all").hide() ;
					$(".compare-block").slideUp() ;
				}
				
				}
			
			
			
		}
	});
}

var drawTime =  function ( startTime, elementsPrefix, elementsPostfix )         
	{
	//alert(startTime) ;
	startTime--;
	hours = Math.floor( startTime / 3600 ) ;
	mins = Math.floor( (startTime - hours * 3600) / 60 ) ;
	secs = startTime - mins * 60 - hours * 3600 ; 
	var zsecs = '';
	var zmins = '';
	var zhours = '';
	if (secs < 10)
		zsecs = "0";
	if (mins < 10)
		zmins = "0" ;
	if (hours < 10)
		zhours = "0" ;
		
	
	$('#' +elementsPrefix+ 'timer_sek' + elementsPostfix).html( zsecs  ) ;
	$('#' +elementsPrefix+ 'timer_sek' + elementsPostfix).append ( secs ) ;
	$('#' +elementsPrefix+ 'timer_min' + elementsPostfix).html( zmins  ) ;
	$('#' +elementsPrefix+ 'timer_min' + elementsPostfix).append(  mins ) ;
	$('#' +elementsPrefix+ 'timer_hour' + elementsPostfix).html( zhours ) ;
	$('#' +elementsPrefix+ 'timer_hour' + elementsPostfix).append( hours ) ;
	
	
	if (startTime > 0)
	{
		setTimeout( "drawTime("+startTime+", '"+ elementsPrefix +"', '"+ elementsPostfix +"')" , 1000 );
	}
	else {
		$('.timer_div' + elementsPostfix).html("<center><br /><span style='font-size:18px!important;'>"+$("." + elementsPrefix + "finish_text")+"<span></center>") ;
		$("#" +elementsPrefix+ "inputs" + elementsPostfix).remove();
		}
}

var placeBid = function (productid, url, amount_elementid, currency, key)
{	
	
	var bidamount = $("#"+ amount_elementid).val() ; 
	$('#bidstat_'+key).html('<img style="position:absolute; bottom:-20px; width:20px;" src="/images/loading.gif" />');
	
	$.ajax( {
		url : url,
		type: "POST",
		dataType: 'json',
		data : "bid[productid]=" + productid + "&bid[bidamount]=" + bidamount +"&action=placebid&bid[currency]=" + currency,
		error:function()
		{
			$('#auction').html( 'Internal Server Error' );	
		},
		success : function(data) 
		{
			
			
			$('#bidsnum_' + key).html( data.bidscount );
			if(data.currentbid != '')
			$('#currentbid_' + key).html( data.currentbid );
			if(data.status == 'ok')
			$('#bidstat_' + key).css( 'color', 'green' );
			else
				$('#bidstat_' + key).css( 'color', 'red' );
			$('#bidstat_' + key).html( data.result );
			
			
			$(".prices").mouseenter(function(){
				$(this).children('.currencies').fadeIn(100);
				
			});

			$(".prices").mouseleave(function(){
				$(this).children('.currencies').fadeOut(100);
				
			});
			
		}
	});
}

var refreshAuc = function (productid, url, currency, key)
{	
	
	
	$('#bidstat_'+key).html('<img style="position:absolute; bottom:-20px; width:20px;" src="/images/loading.gif" />');
	$(".bids-refresh-"+key).hide() ;
	$.ajax( {
		url : url,
		type: "POST",
		dataType: 'json',
		data : "bid[productid]=" + productid + "&action=refreshauc&bid[currency]=" + currency,
		error:function()
		{
			//$('#auction').html( 'Internal Server Error' );
		
		setTimeout(function(){$(".bids-refresh-"+key).show() ;}, 1000);
		
		},
		success : function(data) 
		{
			
			
			$('#bidsnum_' + key).html( data.bidscount );
			if(data.currentbid != '')
			$('#currentbid_' + key).html( data.currentbid );
			$('#bidstat_' + key).html( '' );
			setTimeout(function(){$(".bids-refresh-"+key).show() ;}, 1000);
			
			}
		});
			
	//setTimeout("refreshAuc("+productid+", '"+url+"', '"+currency+"', "+key+")", 10000) ;
	
}

var showBidsHistory = function (productid,url, key, currency, userId)
{	
	

	$('.bids-number-'+key).append('<div class="bids-number-history"><center><img src="/images/loading.gif" /></center></div>');
	$('.bids-number-'+key).hide() ;
	$.ajax( {
		url : url,
		type: "POST",
		data : "auc[productid]=" + productid + "&auc[userid]=" + userId +"&action=viewhistory&auc[currency]=" + currency,
		error:function()
		{
			$('#auction').html( 'Internal Server Error' );	
		},
		success : function(data) {
			$('.bids-number-'+key).children('div').remove();
			$('.bids-number-'+key).append('<div class="bids-number-history">' + data + '</div>') ;
			setTimeout(function(){$('.bids-number-'+key).show() ;}, 1000);
			
		}
	});
}


var loadAuctionBlock = function (productid, url, elementid)
{	
	$('#auction').html('<div style="padding-top:100px; width:100%; height:100%;"><center><img src="/images/loading.gif" /></center></div>');
	
	$.ajax( {
		url : url,
		type: "POST",
		data : "bid[productid]=" + productid + "&action=loadAuctionBlock",
		error:function()
		{
			$('#auction').html( 'Internal Server Error' );
			
			
		},
		success : function(data) 
		{

			$('#auction').html( data );
			
			$(".prices").mouseenter(function(){
				$(this).children('.currencies').fadeIn(100);
				
			});

			$(".prices").mouseleave(function(){
				$(this).children('.currencies').fadeOut(100);
				
			});
			
			//$(this).html(data);
			//$(this).dialog("options",{  beforeClose: function(event, ui) { return true; }});

		}
	});
	
	
}


var sendToFriend = function (productalias) {
	var _loaderImage = new Image();
	_loaderImage.src = "/images/loading.gif";
	$('.status').html(_loaderImage);
	$.ajax( {
		url : this,
		type: "POST",
		data : "friendemail=" + $("#friendemail").val() + "&productalias=" + productalias,
		error:function()
		{
			$('.status').html( 'Internal Server Error' );
			
			
		},
		success : function(data) 
		{
			$('.status').html( data );
			//$(this).html(data);
			//$(this).dialog("options",{  beforeClose: function(event, ui) { return true; }});

		}
	});
}

var initAddTocard = function() {
	$('.add-to-cart').click(function() {
		var _addToCardDialog = document.createElement('div');
		var _loaderImage = new Image();
		_loaderImage.src = "/images/loading.gif";
		
		$(_addToCardDialog).html(_loaderImage);
		
		$(_addToCardDialog).dialog( {
			modal : true,
			 beforeClose: function(event, ui) { alert('Loading'); return false; },
			close : function() {
				$(this).remove();
			}
		});
		alert(this);
		$.ajax( {
			url : this,
			context:_addToCardDialog,
			error:function()
			{
				alert("Internal Server Error");
				$(this).dialog("options",{  beforeClose: function(event, ui) { return true; }});
				$(this).dialog('close');
			},
			success : function(data) 
			{
			
				$(this).html(data);
				$(this).dialog("options",{  beforeClose: function(event, ui) { return true; }});

			}
		});

	});
	
	return false;
}

function addToCartLaoded() {

	$('#qty').animate( {
		width : 20
	}, 50);
	$('#qty').keyup(function() {
		_qty(this)
	});
	$('#qty').change(function() {
		_qty(this)
	});

}
function _qty(_this) {
	var _v = _this.value;
	_v = parseInt(_v);
	if (isNaN(_v)) {
		_v = 1;
	}

	if (_v < 1)
		_v = 1;
	_this.value = _v;
}

function _addTocardSubmit(form, submiter) {
	_url = $(form).attr('action');
	
	_callFuntionName = submiter.name;
	
	_data = $(form).serialize();
	
	 

	$.post(_url, _data, function(result) {
		window[_callFuntionName](result);
	});
	_closeDialog();
	return false;

}

function _closeDialog()
{
	$("#dialog").dialog("close");
}

var _getShopingCart = function() {
	
	$.get("/" + lngCode + '/products/shoping-cart-counter', function(data) {
		$('#shoping_cart_counter').html(data);
	});
}
var addToCart = function(result) {

	_getShopingCart();
	//$('#colorbox').hide(250);
	//$('#cboxOverlay').hide(250);

}



var addAndGoToCart = function(result) {
	_getShopingCart();
	window.location.href = result;
}

var initShopingCartForm = function(){
	$(".remover").click( function()
								  {
									  
									var _data = $("#shoping_cart_sheet").serialize();
									var _url = $("#shoping_cart_sheet").attr('action');
									
									$("#shoping_cart_sheet").css({opacity:0.5});
									
									$.post(_url,_data,function(data){ $("#shoping_cart_sheet").replaceWith(data); initShopingCartForm(); _getShopingCart();})
								  })
								  
	$(".cart-remove-all").click(function(){
		$(".remover").attr('checked','checked') ;
		var _data = $("#shoping_cart_sheet").serialize();
		var _url = $("#shoping_cart_sheet").attr('action');
		
		$("#shoping_cart_sheet").css({opacity:0.5});
		
		$.post(_url,_data,function(data){ $("#shoping_cart_sheet").replaceWith(data); initShopingCartForm(); _getShopingCart();})
	
	});
	
	$(".pqty").change( function(){
									var _data = $("#shoping_cart_sheet").serialize();
									var _url = $("#shoping_cart_sheet").attr('action');
									
									$("#shoping_cart_sheet").css({opacity:0.5});
									val = this.value;
									   val = parseInt(val);
									   if(isNaN(val))
									   val =1;
									   if(val < 1)
									   val=Math.abs(val);
									   this.value = val;
									$.post(_url,_data,function(data){ $("#shoping_cart_sheet").replaceWith(data); initShopingCartForm(); _getShopingCart();})
								})
	return false;
}




function setImageSrc(imgId, val)
{	
	$("#"+imgId).attr({"src": val});
}

function reloadCaptcha(path)
{
	$("#captImage").attr('src','/kcaptcha/index.php?chk='+Math.random(150000));
}


