jQuery.noConflict();

jQuery(document).ready(function() {
	jQuery(".palyginimas").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var id = split[0];
		var dataString = 'id=' + id + '&r=' + Math.random();
		jQuery.ajax({
			type: "POST",
			url: "ajax/setCompare.php",
			data: dataString,
			success: function(response)
			{
				jQuery('.bold3').html('<u>'+response+'</u>');
				jQuery('.bold3').fadeTo("fast", 0.0);
				jQuery('.bold3').fadeTo("slow", 1);
			},
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});
		return false;
	});
	jQuery(".delpalyginimas").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var id = split[0];
		var dataString = 'id=' + id + '&r=' + Math.random();
		jQuery.ajax({
			type: "POST",
			url: "ajax/unsetCompare.php",
			data: dataString,
			success: function(response)
			{
				window.location.reload(true);
			},
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});
		return false;
	});
	jQuery(".krepselis").click(function() {
		if(jQuery("#con-spalva-dydis").val() == '1')
		{
			if(jQuery("#spalva-dydis").val() == '')
			{
				alert('Pasirinkite spalvą ir dydį.');
				return false;
			}
		}
		var split = jQuery(this).attr('class').split(' ');
		var id = split[0];
		var count = jQuery('#kiekis-'+id).val();
		var productX 		= jQuery(".im"+id).offset().left;
		var productY 		= jQuery(".im"+id).offset().top;
		var basketX 		= jQuery(".bold2").offset().left;
		var basketY 		= jQuery(".bold2").offset().top;
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
		var img = jQuery("#im"+id).html();
		
		jQuery(".im"+id)
			.clone()
			.prependTo(jQuery("#im"+id))
			.css({'position' : 'absolute'})
			.animate({opacity: 1}, 100 )
			.animate({opacity: 0, marginLeft: gotoX, marginTop: gotoY}, 1200,function() {
				var dataString = 'id=' + id +'&count=' + count + '&r=' + Math.random();
				jQuery.ajax({
					type: "POST",
					url: "ajax/setBasket.php",
					data: dataString,
					success: function(atsakymas)
					{
						var response = atsakymas;
						jQuery(".im"+id).animate({ opacity: 0 }, 500);
						jQuery("#im"+id+ " img").remove();
						jQuery(".im"+id).animate({ opacity: 0 }, 500);
						jQuery(".im"+id).animate({ opacity: 1 }, 500);
						jQuery("#im"+id).html(img);
						var s = response.split('_');
						jQuery('.bold1').html(s[0]);
						jQuery('.bold2').html(s[1]);
						jQuery('.bold1').fadeTo("fast", 0.0);
						jQuery('.bold1').fadeTo("slow", 1);
						jQuery('.bold2').fadeTo("fast", 0.0);
						jQuery('.bold2').fadeTo("slow", 1);
						jQuery('.k_place2'+id).show();
						jQuery('.k_place1'+id).hide();
					},
				    failure: function( response ) 
				    {
						alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
				    }
				});
				return false;
			});
	});
	jQuery(".krepselisubl").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var id = split[0];
		var count = 1;

		var dataString = 'id=' + id +'&count=' + count + '&r=' + Math.random();
		jQuery.ajax({
			type: "POST",
			url: "ajax/setBasket.php",
			data: dataString,
			success: function(atsakymas)
			{
				var response = atsakymas;
				var s = response.split('_');
				jQuery('.bold1').html(s[0]);
				jQuery('.bold2').html(s[1]);
				jQuery('.bold1').fadeTo("fast", 0.0);
				jQuery('.bold1').fadeTo("slow", 1);
				jQuery('.bold2').fadeTo("fast", 0.0);
				jQuery('.bold2').fadeTo("slow", 1);
			},
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});
	});
	jQuery(".iskrepselis").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var id = split[0];
		var img_url = jQuery("#img_url").val();
		var dataString = 'id=' + id + '&r=' + Math.random();
		jQuery.ajax({
			type: "POST",
			url: "ajax/unsetBasket.php",
			data: dataString,
			success: function(response)
			{
				var s = response.split('_');
				jQuery('.bold1').html(s[0]);
				jQuery('.bold2').html(s[1]);
				jQuery('.bold1').fadeTo("fast", 0.0);
				jQuery('.bold1').fadeTo("slow", 1);
				jQuery('.bold2').fadeTo("fast", 0.0);
				jQuery('.bold2').fadeTo("slow", 1);
				jQuery('.k_place1'+id).show();
				jQuery('.k_place2'+id).hide();
			},
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});
		return false;
	});
	jQuery("#filtras .item").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		if(jQuery("#filtravimas"+split[1]).css('display') == 'none')
		{
			jQuery("#filtras .item").each(function (i) {	
				jQuery(this).removeClass("active");
				jQuery("#filtravimas"+i).hide();
			});
			
			jQuery(this).addClass("active");
			jQuery("#filtravimas"+split[1]).show();
		}
		else
		{
			jQuery(this).removeClass("active");
			jQuery("#filtravimas"+split[1]).hide();
		}
	});
	jQuery(".filtruoti").click(function() {
		jQuery("#filtras .item").each(function (i) {	
			jQuery(this).removeClass("active");
			jQuery("#filtravimas"+i).hide();
		});
	});
	
	jQuery(".maza").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var id = split[0];
		var site_url = jQuery("#site_url").val();
		jQuery('.foto').html('<script type="text/javascript">jQuery(function() {jQuery(".foto a.lightbox").lightBox();});</script><a href="' + site_url + 'img/uploads/images/' + id + '" class="lightbox"><img src="'+site_url+'i.php?i='+id+'&amp;w=220&amp;h=220&amp;type=2" alt=""/></a>');
		return false;
	});
	jQuery(".tipas").click(function() {
		var val = jQuery(this).val();
		if(val==0)
		{
			jQuery('.juridinis').fadeTo("slow", 0.0);
			jQuery('.juridinis').hide('slow');
		}
		else
		{
			jQuery('.juridinis').show('fast');
			jQuery('.juridinis').fadeTo("slow", 1.0);
		}
	});
	jQuery(".l1").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	}); 
	jQuery(".l2").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	}); 
	jQuery(function() {
		jQuery(".kiekis").numeric();
		jQuery(".kaina").numeric_kainai();
	});
	jQuery(".palyginti").click(function() {
		window.open ("http://www.perkupats.lt/palyginimas.php",  "mywindow","location=1,status=1,scrollbars=1,resizable=1,width=780,height=500");
		return false;
	}); 
	jQuery(".klausk").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var windowWidth = 780;
		var windowHeight = 500;
		var myleft=(screen.width)?(screen.width-windowWidth)/2:100;
		var mytop=(screen.height)?(screen.height-windowHeight)/2:100;
		window.open ("http://www.perkupats.lt/klausk.php?id="+split[0],  "mywindow","location=1,status=1,scrollbars=1,resizable=1,width=" + windowWidth + ",height="+windowHeight+",top="+mytop+",left="+myleft+"");
		return false;
	}); 
	jQuery(".uncheck").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var tipas = split[1];
		
		jQuery("."+tipas).each(function(){
            this.checked = false;
        });
		jQuery('.mygtukas'+tipas+'1').hide();
		jQuery('.mygtukas'+tipas+'2').show();
		return false;
	});
	jQuery(".check").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var tipas = split[1];
		
		jQuery("."+tipas).each(function(){
            this.checked = true;
        });
		jQuery('.mygtukas'+tipas+'2').hide();
		jQuery('.mygtukas'+tipas+'1').show();
		return false;
	});
	jQuery(".spalva_dydis").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var dydis = jQuery(this).val();
		var id = split[1];
		var dataString = 'id=' + id + '&value=' + dydis + '&r=' + Math.random();
		jQuery.ajax({
			type: "POST",
			url: "ajax/setSpalvaDydis.php",
			data: dataString,
			success: function(response)
			{
				if(jQuery("#con-spalva-dydis").val() == '1')
				{
					jQuery("#spalva-dydis").val(id);
				}
			},
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});
	});
}); 