function clear_field(object,value) {
	if (object.value==value)
		object.value='';
}
function clear_reset(object,value) {
	if (object.value=='')
		object.value=value;
}
function runEffect(item, where){
	var options = {};
	options = { to: ".shoppingCart", className: 'ui-effects-transfer' };
	$("#" + where + "_" + item).effect("transfer", options, 500, callback);
	options = {};
	$(".shoppingCart").effect("bounce", options, 300, callback);
}
function callback(){
	setTimeout(function(){
		$("#effect:hidden").removeAttr('style').hide().fadeIn();
	}, 1000);
}

/*Basket actions*/
function toBasket(item, where) {
	$.post("/_ajax/toBasket/id/" + item, {}, function(data){
		$('.shoppingCart').addClass('inCart');
		runEffect(item, where);
		items = $(".nrIncart").text();
		if (items == '') items = 0;
		items = parseInt(items) + 1;
		$(".nrIncart").text(items);
		$("#cartPrice").text(data + '.-');
	});
}
function removeBasket(item) {
	$.post("/_ajax/removeBasket/id/" + item, {}, function(data){
		runEffect();
		$(".nrIncart").text(data);
		$('#' + item).remove();
		old_total = parseInt($('#sum_number_' + item).html());
		$('#total_number').html(parseInt($('#total_number').html())-old_total);
	});
}
function changeBasket(item) {
	quantity = parseInt($('#quantity_' + item).val());
	$.post("/_ajax/changeBasket/id/" + item + "&quantity="+quantity, {}, function(data){
		runEffect();
		$(".nrIncart").text(data);
	});
	old_total = parseInt($('#sum_number_' + item).html());
	new_total = parseInt($('#price_number_' + item).html())*quantity;
	$('#sum_number_' + item).html(new_total);
	$('#total_number').html(parseInt($('#total_number').html())-old_total+new_total);
}
function changeTransport(val) {
	$.post("/_ajax/changeTransport=1&item=" + val, {}, function(data){
		old = parseInt($('#transport_number').html());
		$('#transport_number').html(val);
		$('#total_number').html(parseInt($('#total_number').html())-old+parseInt(val));
	});
}
function swapProducts(which){
	$('.artistTabs>li').each(function(i){
		$(this).removeClass('active');
	});
	$('#'+which+'Tab').addClass('active');
	$('.artistList:visible').fadeOut(300, function(){
		$('#'+which+'Artists').fadeIn(300);
	});
	
}
function getHeight() {
	height = $(document).height();
	$('.curtain').css('height', height);
	return false;
};
function sendToFriend() {
	$('#name').removeClass('cError');
	$('#email').removeClass('cError');
	$('#friend').removeClass('cError');
	error = 0;
	if ($('#name').val() == '') {
		error = 1;
		$('#name').addClass('cError');
	}
	if ($('#email').val() == '' && $('#name:contains("@")')) {
		error = 2;
		$('#email').addClass('cError');
	}
	if ($('#friend').val() == '' && $('#friend:contains("@")')) {
		error = 3;
		$('#friend').addClass('#friend');
	}
	if (error > 0) {
		//Alert smthng
	} else {
		$.ajax({
			type: 'POST',
			url: '/_ajax/sendToFriend',
			cache: false,
			data: {
				name: $('#name').val(), 
				email: $('#email').val(), 
				heading: $('#friend').val()
			},
			dataType: 'text',
			timeout: 2000,
			success: function(data) {
				$('.overLayer').hide();
				$('.curtain').hide();
			}
		});
	}
}
$(function() {
	$("#jquery_jplayer").each(function(i){
		$(this).jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", "http://pedrobeat.aimedia.ee/userfiles/images/toote_pildid/Miks%20m6ni%20asi%20on%20nii%20hea%20%28radio%20master%29.mp3");
			},
			volume: 50
		});
	});
	
	$('.wide').find('img').height($('.catList').find('img').height()/2);
	$('.wide').find('img').width($('.catList').find('img').width()/2);
	

	$('#coda-slider-1').codaSlider({
		dynamicArrows: false,
		dynamicTabs: false,
		autoSlide: true,
		firstPanelToLoad: 1,
		autoSlideInterval: 8000,
		slideDirection: "horizontal"
	});
	$("a[rel='light']").lightBox();
	$("a.lightbox").lightBox();
	function log(message) {
		$("<div/>").text(message).prependTo("#log");
		$("#log").attr("scrollTop", 0);
	}
	$("#search").autocomplete({
		source: function(request, response) {
			$.ajax({
				url: '/_ajax/search',
				dataType: "json",
				type: "POST",
				data: {
					maxRows: 12,
					name_startsWith: request.term
				},
				success: function(data) {
					response($.map(data, function(item) {
						return {
							label: item.name,
							value: item.name + ', ' + item.artist,
							url: item.url
						}
					}))
				}
			})
		},
		minLength: 2,
		select: function(event, ui) {
			//log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
			//alert('Siia mine:' + ui.item.value);
			document.location.href = '/et/tooted/' + ui.item.url;
		},
		open: function() {
			$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
		},
		close: function() {
			$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
		}
	});
});
