	var buying = false;
$(document).ready(function() {

	$('.gallerywithscroll').each(function(){
		$(this).jScrollPane({
			scrollbarWidth : 6, dragMaxHeight: 46
		});
	});
	if($('#sendTopaypal').length > 0){
		setTimeout('enviarpago()', 3000);
	}
	
	if($('#contentmenu li').length > 0){
		var tabContainers = $('div.contenttext');
		$('#contentmenu li').css({'cursor':'pointer'});
		tabContainers.hide().filter(':first').show();
		$('#contentmenu li').click(function () {
			tabContainers.hide();
			var hash = $(this).attr('id').replace('content', '');
			tabContainers.filter('#contentext'+hash).fadeIn();
			$('#contentmenu li').removeClass('active');
			$(this).addClass('active');
			return false;
		}).filter(':first').click();
	}
	$('.gallerycontent').each(function(){
		$("a[rel='gallery"+$(this).attr('id').replace('contentext', '')+"']").colorbox();
	});
	
	$('a[rel=TAYLORMADE], a[rel=KIMETZEVENTS], a[rel=LOOKANDLEARN], a[rel=LOOKEDITORIAL]').colorbox();

	$('.news-imgs').each(function(){
		$(this).cycle({
			fx: 'fade', 
			sync: false, 
			delay: -2000,
			next: $('#gnext'),
			prev: $('#gprev')
		});
	})
	$('a.buyproduct').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			if(buying == false){
				buying = true;
				var content = $(this).parent();
				$(content).css({
					'backgroundImage':'url('+kimetzurl+'theme/imagenes/loadingbuy.gif)',
					'backgroundPosition':'43px 5px'
				});
				$.get(kimetzurl+idiom+'/carrito-add/'+$(this).attr('rel'), function(data){
					$(content).css({ 
						'backgroundImage':'url('+kimetzurl+'theme/imagenes/carritob.jpg)',
						'backgroundPosition':'43px 5px'
					});
					var cartcontent = $.evalJSON(data);
					updateCartContent(cartcontent);
					buying = false;
				});
			}
		});
	});
	$('.productcount input').change(function(e){
		e.preventDefault();
		var id_producto = $(this).parent().parent().attr('class').split(' ');
		updateCartItems(id_producto[1], $(this).val());
	}).keyup(function(e){
		e.preventDefault();
		var id_producto = $(this).parent().parent().attr('class').split(' ');
		updateCartItems(id_producto[1], $(this).val());
	});

	$('.gocart').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			window.location = kimetzurl+idiom+'/carrito-lista.html';
		});
	});
	if($('#finish').length > 0){
		$('#finish').click(function(e){
			e.preventDefault();
			window.location = kimetzurl+idiom+'/finalizar.html';
		});
	}
	$('.btndelete').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			var id_producto = $(this).parent().parent().attr('class').split(' ');
			var content = $(this).parent().parent();
			content.css({'background-color':'#9A9A9A'});
			$.get(kimetzurl+idiom+'/carrito-remove/'+id_producto[1], function(data){
				var cartcontent = $.evalJSON(data);
				content.fadeOut('slow', function (){content.remove();});
				updateCartContent(cartcontent);
				updateCartList(cartcontent.total);
			});
		});
	});
	
	
	if($('#clear').length > 0){
		$('#clear').click(function(){
			$('#registerform input[type=text]').each(function(){
				$(this).val('');
			});
		});
	}
	
	$('input[title!=""]').hint();
	updateCart();
	addLogout();
	addLogin();
	
	if($('#registerButtons').length > 0){
		if( ($.browser.msie) && ($.browser.version<=7) ){
			$('#registerButtons').before("<br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/><br style='clear: both; '/>");
		}
	}
	if($('#videocontent').length > 0){
		$('#videocontent').click(function(e){
			e.preventDefault();
			$(this).colorbox({href:kimetzurl + "es/video.html"})
		});
	}
	
	$('.blank').each(function(){
		$(this).attr({'target':'_blank'});
	});
});

function enviarpago(){
	$('#gateway_form').submit();
}

function updateCartItems(id_producto, qt){
	if(buying == false){
		buying = true;
		$.get(kimetzurl+idiom+'/carrito-add/'+parseInt(id_producto)+'/'+parseInt(qt), function(data){
			$(content).css({ 
				'backgroundImage':'url('+kimetzurl+'theme/imagenes/carritob.jpg)',
				'backgroundPosition':'43px 5px'
			});
			var cartcontent = $.evalJSON(data);
			if(cartcontent.resultado == true){
				updateCartContent(cartcontent);
				updateCartList(cartcontent.total);
			}else{
				jAlert(cartcontent.mesanje, 'Error');
			}
			buying = false;
		});
	}
}
function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 }
function addLogout(){
	if($('#btnlogout').length > 0){
		$('#btnlogout').click(function(e){
			e.preventDefault();
			if( buying == false){
				buying = true;								
				$.get(kimetzurl+idiom+'/logout.html', function(data){
					var resultlogout = $.evalJSON(data);
					if(resultlogout.result == true){
						if(idiom == 'es'){
							$('#usermenu').html("<form name='login' id='login' method='post' action='"+kimetzurl+idiom+"/login.html'><fieldset><legend></legend><input type='hidden' name='dologin' value='1' /><input type='text' name='usuario' id='usuario' title='Usuario' value='Usuario' ><input type='password' name='password' id='password' title='Contrase&ntilde;a' value='Contrase&ntilde;a' ><button class='btnwhite' id='btnlogin' title='Ingresar'>Ingresar</button></fieldset></form>");
						}else{
							$('#usermenu').html("<form name='login' id='login' method='post' action='"+kimetzurl+idiom+"/login.html'><fieldset><legend></legend><input type='hidden' name='dologin' value='1' /><input type='text' name='usuario' id='usuario' title='Usuario' value='Erabiltzaile' ><input type='password' name='password' id='password' title='Pasahitza' value='Pasahitza' ><button class='btnwhite' id='btnlogin' title='Sartu'>Sartu</button></fieldset></form>");
						}
					}
					addLogin();

					buying = false;
				});
			}
		});
	}
}
function addLogin(){
	if($('#login').length > 0){
		var newinput = $('<input type="hidden"/>').attr({'name': 'request', 'id': 'request', 'value':'1'});
		$('#login').append(newinput);
		$('#btnlogin').click(function(e){
			e.preventDefault();
			if( buying == false){
				buying = true;	
				$.post($('#login').attr('action'), $('#login').serialize() , function(data){
					var resultlogin = $.evalJSON(data);
					if(resultlogin.result == true){
						$('#login').remove();
						var newcontent = $('<div />').attr({'id':'userlogin'}).html('&nbsp;'+resultlogin.mensaje);
						$('#usermenu').append(newcontent).append($('<button class="btngrey btnlogout"/>').attr({'id':'btnlogout'}));
						addLogout();
					}else{
						jAlert(resultlogin.mensaje, 'Error');
					}
					buying = false;
				});
			}
		});
	}
}
function updateCart(){
	if($('#carro_details').length > 0){
		$.get(kimetzurl+idiom+'/carrito-refresh/', function(data){
			var cartcontent = $.evalJSON(data);
			updateCartContent(cartcontent);
		});
	}
}
function updateCartContent(cartcontent){
	if(cartcontent.resultado == true){
		$('#carro_details').html(cartcontent.cart);
	}else{
		jAlert(cartcontent.mesanje, 'Error');
	}
}
function updateCartList(total){
	$('.cartotalitemprice').each(function(){
		$(this).html(total);
	});
}
