jQuery(document).ready(function() {

	$('.hidden').hide();
	
	$('.category-list-image').imgr({size:"1px",color:"66bb00",radius:"10px"});
	$('.subcategory3-list-image').imgr({size:"1px", color: "ffffff", radius:"10px"});
	$('.subcategory2-list-image').imgr({size:"1px", color: "ffffff", radius:"10px"});
	
	$('img.category-list-image, img.subcategory3-list-image, img.subcategory2-list-image').hover(
			function() {
				var id = $(this).attr('id');
				var current_src_1 = $(this).attr('src').split('=');
				var current_src = current_src_1[1].split('&');
				//$('#'+id).attr('src','i.php?i='+current_src[0]+'&cat');
				$('#'+id).parent().css('background-image','url(i.php?i='+current_src[0]+'&cat)');
			},
			function() {
				var id = $(this).attr('id');
				var current_src_1 = $(this).attr('src').split('=');
				var current_src = current_src_1[1].split('&');				
				//$('#'+id).attr('src','i.php?i='+current_src[0]+'&bw');
				$('#'+id).parent().css('background-image','url(i.php?i='+current_src[0]+'&bw=1)');
			}
	)
	
	$("ul.dropdown li").hover(function(){
	    
	    $(this).addClass("hover");
	    $('ul:first',this).css('visibility', 'visible');

	}, function(){

	    $(this).removeClass("hover");
	    $('ul:first',this).css('visibility', 'hidden');

	});
	
	var width = jQuery('body').width();
	var height = jQuery('body').height();
	var width_div = (width - 1010)/2 + 'px';
	var height_div = (height - 188 + 42) + 'px';
	$('#wrapper-left').css('width',width_div);
	$('#wrapper-right').css('width',width_div);
	$('#catalog-wrapper-left').css('width',width_div);
	$('#catalog-wrapper-right').css('width',width_div);
	$('#catalog-wrapper-right2').css('width',width_div);
	$('#catalog3-wrapper-left').css('width',width_div);
	$('#catalog3-wrapper-right').css('width',width_div);
	$('#catalog3-wrapper-right').css('height',height_div);
	$('#catalog3-wrapper-right2').css('width',width_div);
	$('#catalog2-wrapper-left').css('width',width_div);
	$('#catalog2-wrapper-right').css('width',width_div);
	$('#catalog2-wrapper-right').css('height',height_div);
	$('#catalog2-wrapper-right2').css('width',width_div);
	$('#catalog2-wrapper-right3').css('height',height_div);
	$('#footer-left').css('width',width_div);
	$('#footer-right').css('width',width_div);
	
	jQuery('.maxlength').maxlength();
	jQuery(function() {
		jQuery(".iamount").numeric();
		jQuery(".ic").attr("autocomplete","off");
		jQuery(".kaina").numeric_kainai();
	});
	
	jQuery(".cart").click(function() {
		var split = jQuery(this).attr('class').split(' ');
		var id = split[1];
			var dataString = 'id=' + id;
			jQuery.ajax({
				type: "POST",
				url: "ajax/setBasket.php",
				data: dataString,
				success: function(atsakymas)
				{
					var response = atsakymas;
					var cc = $("span.cart-count").html();
					var new_cc = parseInt(cc) + 1;
					$("span.cart-count").html(new_cc);
					$("img."+id).attr('src','img/sim/incart.png');
				},
			    failure: function(response) 
			    {
					alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
			    }
			});
		return false;
	});
	
	jQuery(".top_search").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	$('.c2-product').hover(
		function() {
			var id = $(this).attr('id');
			var dataString = 'get=thumb&id=' + id;
			jQuery.ajax({ type: "POST", url: "ajax/getImg.php", async: false, data: dataString, 
				success: function(data) {
					$('#product-thumb img').attr('src', data.img);
				}
			});
		},
		function() { return false; }
	);
	
	$('.c2-product').click(function() {
		var id = $(this).attr('id');
		var dataString = 'get=data&id=' + id;
		$('.c2-product').attr('class', 'c2-product');
		$(this).addClass('active');
		jQuery.ajax({ type: "POST", url: "ajax/getImg.php", async: false, data: dataString, 
			success: function(data) {
				$('#c2-right #product-image img').attr('src', data.img);
				$('#c2-right #product-title').html(data.title);
				$('#c2-right #product-more a').attr('href', data.more);
				$('#c2-right #product-order a').attr('href', data.order);
				if (data.link) { $('#c2-right #product-link').html('<a href="http://'+data.link+'" target="_blank">'+data.link+'</a>'); }
				else { $('#c2-right #product-link').html(''); }
			}
		});
	});
	
	jQuery('#mycarousel').jcarousel({ vertical: true, scroll: 10 });
	
	$('.blink').each(function() {
		var blink_e		= $(this).attr('id');
		var blink_id	= $(this).attr('id').replace('menu-item-', '');
		var blink_c		= $('#bColor-' + blink_id).val();
		
		blinkFont(blink_e, blink_c);
	});
	
	var type = $("input[name='type']:checked").val();
	if (type == 'private') {
		$('input[name="type"]:not("#p")').attr('checked', '');
		$('#company').hide(); 
		$('#private').show(); 
	}
	else { 
		$('input[name="type"]:not("#c")').attr('checked', '');
		$('#c').attr('checked', 'checked');
		$('#private').hide(); 
		$('#company').show(); 
	}
		
	
});

function blinkFont(blink_e, blink_c) {
	document.getElementById(blink_e).style.color="" + blink_c + "";
	setTimeout("setblinkFont('" + blink_e + "', '" + blink_c + "')", 1000);
}

function setblinkFont(blink_e, blink_c) {
	document.getElementById(blink_e).style.color="";
	setTimeout("blinkFont('" + blink_e + "', '" + blink_c + "')", 1000);
}

jQuery(document).change(function(){
	var type = $("input[name='type']:checked").val();
	if(type == 'company') {
		$("table#company").show();
		$("table#private").hide();
		$("#c").attr('checked', true);
	}
	
	if(type == 'private') {
		$("table#company").hide();
		$("table#private").show();
		$("#p").attr('checked', true);	
	}
});


// QTYs and Notes to _SESSION
$(function() {
	$('.qty').change(function() {
		var qty = $(this).val();
		var id_split = $(this).attr('name').split('kiekis');
		var id = id_split[1];
		var dataString = 'pid=' + id + '&new_kiekis=' + qty;
		jQuery.ajax({
			type: "POST",
			url: "ajax/setBasket.php",
			data: dataString,
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});	
	})
	
	$('.note').change(function() {
		var note = $(this).val();
		var id_split = $(this).attr('name').split('pastabos');
		var id = id_split[1];
		var dataString = 'nid=' + id + '&new_note=' + note;
		jQuery.ajax({
			type: "POST",
			url: "ajax/setBasket.php",
			data: dataString,
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});	
	})
});


// Quick Order 
$(function(){
jQuery("input[name=kodas1]").change(function () {
	var kodas = $("input[name=kodas1]").val();
	var dataString = 'new_kodas=' + kodas;
	jQuery.ajax({
		type: "POST",
		url: "ajax/setBasket.php",
		data: dataString,
		success: function(atsakymas)
		{
			if (atsakymas == 'Neteisingas prekės kodas.') {
				alert('Neteisingas prekės kodas.');
				$("input[name=kodas1]").val('');
			}
			
			else {
				var ats_split = atsakymas.split(';');
				var title = ats_split[0];
				$("span#pavadinimas1").html(title);
				var cc = $("span.cart-count").html();
				var new_cc = parseInt(cc) + 1;
				$("span.cart-count").html(new_cc);
				window.location = "/krepselis";
			}
		},
	    failure: function( response ) 
	    {
			alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
	    }
	});		
}
);
});

function popup(id) {
	window.open('http://www.brasa.lt/modules/products/item.php?id='+id,'Item', 'height=600, width=900, scrollbars=1, resizable=yes, menubar=0, toolbar=0, location=0, status=0');
}

function c2popup(id) {
	window.open('http://www.brasa.lt/modules/cat2_products/item.php?id='+id,'Item', 'height=600, width=900, scrollbars=1, resizable=yes, menubar=0, toolbar=0, location=0, status=0');
}

function c3order(id) {
	var isUser = $('#user_login').html();
	var returnLink = $('#return_link').html();
	if (isUser == '1') { window.open('http://www.brasa.lt/modules/cat3_products/order.php?id='+id,'Order', 'height=600, width=900, scrollbars=1, resizable=yes, menubar=0, toolbar=0, location=0, status=0'); }
	else {
		window.location.href  = 'http://www.brasa.lt/registracija/?returnLink='+returnLink;
	}
}


function backToCatalog() {
	window.location = 'katalogas';
}

function cartRemove(id, dub) {
	var dataString = 'remove_id=' + id + '&dublicate=' + dub;
	jQuery.ajax({
		type: "POST",
		url: "ajax/setBasket.php",
		data: dataString,
		success: function(atsakymas)
		{
			if (dub) { var item = id+','+dub; }
			else { var item = id; } 
			$("#cart-item-"+item).fadeOut('slow');
			var cc = $("span.cart-count").html();
			var new_cc = parseInt(cc) - 1;
			$("span.cart-count").html(new_cc);
		},
	    failure: function(response) 
	    {
			alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
	    }
	});	
}

function newCartItem() {
	var split = $("#insert").prev("tr").attr('id').split('-');
	var current_id = split[2];
	var id = parseInt(current_id)+1;
    var newTr = '<tr id=\"cart-item-'+id+'\">';
		newTr += '<td class=\"eil_nr\"></td>';
		newTr += '<td class=\"kodas\"><input type=\"text\" name=\"kodas'+id+'\" style=\"width: 100px;\" /></td>';
		newTr += '<td class=\"pavadinimas\" style=\"text-align: left;\"><span id=\"pavadinimas'+id+'\"></span></td>';
		newTr += '<td class=\"kiekis\"><input type=\"text\" name=\"kiekis'+id+'\" style=\"width: 25px;\" value="1" /></td>';
		newTr += '<td class=\"pastabos\"><input type=\"text\" name=\"notes'+id+'\" style=\"width: 100px;\" /></td>';
		newTr += '<td class=\"delete\"><a class=\"cart_delete\" title=\"Delete\" style=\"cursor:pointer\"><img src=\"img/cart_delete.png\" /></a></td>';
	newTr += '</tr>';
	$("tr.cart-row:last").after(newTr);
    //$("tr#cart-item-"+current_id).after(newTr);
    var current_id = id-1;

    jQuery(document).change(function () {
    	var kodas = $("input[name='kodas"+id+"']").val();
		var dataString = 'new_kodas=' + kodas;
		jQuery.ajax({
			type: "POST",
			url: "ajax/setBasket.php",
			data: dataString,
			success: function(atsakymas)
			{
				if (atsakymas == 'exist') {
					alert('Tokia prekė jau yra Jūsų pirkinių krepšelyje.');
				    $("tr#cart-item-"+id).remove();					
				}
				
				if (atsakymas == 'wrong') {
					alert('Neteisingas įvestas prekės kodas.');
				    $("tr#cart-item-"+id).remove();					
				}

				else {
					var title = atsakymas;
					$("span#pavadinimas"+id).html(title);
					var cc = $("span.cart-count").html();
					var new_cc = parseInt(cc) + 1;
					$("span.cart-count").html(new_cc);
					window.location = "/krepselis";
				}

			},
		    failure: function( response ) 
		    {
				alert('Nutrūko ryšys su serveriu. Bandykite dar karta.');
		    }
		});		
	}
	);
}

function showOrderDetails(id) {
	$(".order-details-"+id).each(function (i) {
		if($(this).css("display") == 'none')
			$(this).show();
		else 
			$(this).hide();		
    });
}

function rodyti(id) {
	$('#text-'+id).slideDown("fast");
	$('a#'+id).attr('onClick','slepti('+id+')');
}

function slepti(id) {
	$('#text-'+id).slideUp("fast");
	$('a#'+id).attr('onClick','rodyti('+id+')');
}
