// JavaScript Document

/*if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))||(navigator.userAgent.match(/Android/i))) {	return; }
}else{
	var isTouchScreen = 0;
}*/
function stopRKey(evt) {

  var evt = (evt) ? evt : ((event) ? event : null);

  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}

}

document.onkeypress = stopRKey; 

function signUp(){

	hideAllLayers(); $('.registro').fadeIn(); $(this).addClass('on');

}

var _pathSecciones='';

////////// SCROLLL //////
var bgoffset=-5;
var bgmove=false;
function endlessScroll(to){
	if(bgmove){
		if(to==1)			{ $('.scroller').css('background-position', bgoffset+'px 0'); bgoffset--; setTimeout('endlessScroll(1);',1); } //right
		else if(to==2)	{ $('.scroller').css('background-position', bgoffset+'px 0'); bgoffset++; setTimeout('endlessScroll(2);',1); } //left
	}
}

////////FIN SCROLL  /////

function hideAllLayers(){ $('.popLayer').fadeOut(); $('a.on').removeClass('on'); $('.sectionLayer').fadeOut(); $('.sectionLayerTransp').fadeOut(); }

function hideLogin(){ $('div.login, div.forgotPass').fadeOut(); $('a.login').removeClass('on'); }

function goToSection(name,loader){

	hideAllLayers();

	

	$.ajax({type:'GET',url:_pathSecciones+name+'.php?random='+Math.random()+'&pfusr='+window.usr,success:function(msg){

		//alert(msg);

		//alert(name);

		$('#'+name).html(msg).fadeIn();
		$('.scrollPane').jScrollPane();
		
		bgoffset=-5;
		try{

			$('#promo').hide();

		}catch(e){}

		switch(name){

			case 'collection'	:

				//alert("entra");
				if(loader) $('.scroller').css({'margin-left':'-800px', 'background-position':'-5px 0'}); else $('.scroller').animate({marginLeft:'-800px', 'background-position':'-5px 0'},900);
				var fotos=0; var wfotos=300; var cfotos=1;
				$('#collection .image').each(function(){ fotos++; });

				try{
					$('#collection .arrRgt').die( "click" )
				}
				catch(e){
				}
				try{
					$('#collection .arrLft').die( "click" )
				}
				catch(e){
				}
				$('#collection .arrRgt').live('click',function(){ if(cfotos<fotos){ $('#collection .imgScroll').animate({ marginLeft:'-=300px' },900); cfotos++; $('#coll_current').html(cfotos); } });

				$('#collection .arrLft').live('click',function(){ if(cfotos>1){ $('#collection .imgScroll').animate({ marginLeft:'+=300px' },900); cfotos--; $('#coll_current').html(cfotos); } });
				$('a.collection').addClass('on');
				/*

				if(loader) $('.scroller').css({'margin-left':'-800px'}); else $('.scroller').animate({marginLeft:'-800px'},900);

				var fotos=0; var wfotos=300; var cfotos=1;

				$('#collection .image').each(function(){ fotos++; });

				$('#collection .arrRgt').live('click',function(){ if(cfotos<fotos){ $('#collection .imgScroll').animate({ marginLeft:'-=300px' },900); cfotos++; $('#coll_current').html(cfotos); } });

				$('#collection .arrLft').live('click',function(){ if(cfotos>1){ $('#collection .imgScroll').animate({ marginLeft:'+=300px' },900); cfotos--; $('#coll_current').html(cfotos); } });

				

				*/

			break;

			

			case 'shop':

				if(loader) $('.scroller').css({'margin-left':'-1457px'}); else $('.scroller').animate({marginLeft:'-1457px'},900);

				$('a.shop').addClass('on');

				

			case 'shopping-bag':

				if(loader) $('.scroller').css({'margin-left':'-1457px'}); else $('.scroller').animate({marginLeft:'-1457px'},900);

			break;

			

			case 'my-account':


			break;

			case 'blog': $('a.blog').addClass('on'); if(loader) $('.scroller').css({'margin-left':'-163px'}); else $('.scroller').animate({marginLeft:'-163px'},900); break;

			

			case 'contact':  $('a.contact').addClass('on'); if(loader) $('.scroller').css({'margin-left':'-490px'}); else $('.scroller').animate({marginLeft:'-490px'},900); break;

			case 'about':

				$('a.about').addClass('on');

				if(loader) $('.scroller').css({'margin-left':'0px'}); else $('.scroller').animate({marginLeft:'0px'},900); 

			break;

			case 'policies':

				if(loader) $('.scroller').css({'margin-left':'0px'}); else $('.scroller').animate({marginLeft:'0px'},900); 

			break;

			case 'stores':

				if(loader) $('.scroller').css({'margin-left':'-1120px'}); else $('.scroller').animate({marginLeft:'-1120px'},900);

				$('a.stores').addClass('on');

			break;

			case 'press':

				if(loader) $('.scroller').css({'margin-left':'-1850px'}); else $('.scroller').animate({marginLeft:'-1850px'},900);
				
				$('a.press').addClass('on');

			break;

			default: if(loader) $('.scroller').css({'margin-left':'0px'}); else $('.scroller').animate({marginLeft:'0px'},900); 	break;

		}

	}});

}



function goToShopSection(name){

	//alert(_pathSecciones+'categoria.php?id_categoria='+name+'');

	$('a.shop').addClass('on');

	$.ajax({type:'GET',url:_pathSecciones+'categoria.php?id_categoria='+name+'&pfusr='+window.usr+'&random='+Math.random(),success:function(msg){

		$('#sub-shop').html(msg);

		//setImgManiqui('img/maniqui.png','');

	}});

}

function goToProductSection(name){

	//alert(_pathSecciones+'categoria.php?id_categoria='+name+'');
	var partes=name.split('_');
	var id_producto=partes[0];
	if(partes.length>1){
		var id_categoria=partes[1];
	}
	$('a.shop').addClass('on');

	$.ajax({type:'GET',url:_pathSecciones+'product.php?id_producto='+id_producto+'&id_categoria='+id_categoria+'&pfusr='+window.usr+'&random='+Math.random(),success:function(msg){

		$('#sub-shop').html(msg);

	}});

	//alert("entra");

}

function goToPressSection(name){

	//alert(_pathSecciones+'categoria.php?id_categoria='+name+'');

	$('a.press').addClass('on');

	$.ajax({type:'GET',url:_pathSecciones+'press.php?id_prensa='+name+'&pfusr='+window.usr+'&random='+Math.random(),success:function(msg){

		$('#press').html(msg);

	}});

	//alert("entra");

}

function goToProductEditSection(name){


	hideAllLayers();

	$('a.shop').addClass('on');

	$.ajax({type:'GET',url:_pathSecciones+'shop.php?random='+Math.random()+'&pfusr='+window.usr,success:function(msg){

		$('#shop').html(msg).fadeIn();

		$.ajax({type:'GET',url:_pathSecciones+'product.php?id_producto='+name+'&edit=1&pfusr='+window.usr+'&random='+Math.random(),success:function(msg){

			$('#sub-shop').html(msg);

		}});

	}});

	

}

function goToCheckoutSection(){

	goToSection('checkout',true);

}

function goToPaymentOKSection(){

	goToSection('paymentok',true);

}

function goToWholesaleOkSection(){

	goToSection('wholesaleok',true);

}

function goToShippingSection(){

	goToSection('shipping',true);

}

function goToWholesale(){

	goToSection('wholesale',true);

}

function goToHome(){

	 var n=$('.scroller').css('margin-left');

	 if(n!='0px'){

		 $('.scroller').animate({marginLeft:'0px'},1100);

	 }

	 try{

		$('#promo').show();

	}catch(e){}

	 hideAllLayers();

}

$(document).ready(function(){

	var url=window.location.href;

	if(url.search("#")>-1){ var p=url.split('#/'); goToSection(p[1],true); }

	

	

	$('a.login').click(function(){ if($('div.login').css('display')=='none'){ hideAllLayers(); $('div.login').fadeIn(); $(this).addClass('on'); $('#id_tipo_login').attr('value','u'); $('#aviso_whole').hide();}else hideLogin(); });

	$('a.whole').click(function(){ if($('div.login').css('display')=='none'){ hideAllLayers(); $('div.login').fadeIn(); $(this).addClass('on'); $('#id_tipo_login').attr('value','w'); $('#aviso_whole').show(); }else hideLogin(); });

	$('.forgot').click(function(){ hideLogin(); $('.forgotPass').fadeIn(); });

	$('a.tryagain').click(function(){ $('.forgotPass .error').fadeOut(function(){ $('.forgotPass .request').fadeIn(); }); });

	$('.signup').click(function(){ signUp(); });

	$('.subscribe').click(function(){ hideAllLayers(); $('.newsletter').fadeIn(); $('a.subscribe').addClass('on');});

	$('a.collection').click(function(){ goToSection('collection',false); });

	

	$('a[rel^="async"]').live('click',function(){ var url=$(this).attr('href'); if(url.search("#")>-1){ var p=url.split('#/'); goToSection(p[1],false); } });

	$('a[rel^="shop"]').live('click',function(){ var url=$(this).attr('href'); if(url.search("#")>-1){ var p=url.split('#/'); goToShopSection(p[1],false); } });

	$('a[rel^="product"]').live('click',function(){ var url=$(this).attr('href'); if(url.search("#")>-1){ var p=url.split('#/'); goToProductSection(p[1],false); } });

	$('a[rel^="product-edit"]').live('click',function(){ var url=$(this).attr('href'); if(url.search("#")>-1){ var p=url.split('#/'); goToProductEditSection(p[1],true); } });

	$('a.pressAsync').live('click',function(){	var url=$(this).attr('href'); $.ajax({type:'GET',url:url,success:function(msg){ $('#press').html(msg); }}); return false; });

	$('a.storesAsync').live('click',function(){ var url=$(this).attr('href'); $.ajax({type:'GET',url:url,success:function(msg){ $('#stores').html(msg); }}); return false; });

	$('#storesZip').live('focus',function(){ if($(this).val()=='Enter country or zipcode') $(this).val(''); });

	$('#storesZip').live('blur',function(){ if($(this).val()=='') $(this).val('Enter country or zipcode'); });

	

	//cierre de layers

	$('.popLayer .close').live('click',function(){ $(this).parents().find('.popLayer').fadeOut(); });



	//hardcoded por ahora

	

	//submiteo de formularios

	$('form.formAsync').live('submit',function(){

											   

		var action=$(this).attr('action');

		var method=$(this).attr('method');

		var params=$(this).serialize()+'&pfusr='+window.usr;

		$.ajax({type:method,url:action,data:params,success:function(msg){ } });

		return false;

	});

	

	$('form.formAsync2').live('submit',function(){

		var action=$(this).attr('action'), method=$(this).attr('method'), params=$(this).serialize()+'&pfusr='+window.usr, destin=$(this).find('#dest').val();

		$.ajax({type:method,url:action,data:params,success:function(msg){$('#'+destin).html(msg); /*alert(msg);*/ }}); return false;

	});

	

	//submiteo de formularios

	$('form.formAsyncAddProduct').live('submit',function(){

											   

		var action=$(this).attr('action');

		var method=$(this).attr('method');

		var params=$(this).serialize()+'&pfusr='+window.usr;

		$.ajax({type:method,url:action,data:params,success:function(msg){ 

			if(msg!='OK'){

				objConfirmacion.showError(msg);

				return;

			}else{

				objConfirmacion.showMsg('This item was added to the shopping bag');

				$('#btn_checkout').show();

			}

			//goToSection('shopping-bag',true); 

		} });

		return false;

	});

	

	//submiteo de formularios

	$('form.formAsyncRemvProduct').live('submit',function(){

											   

		var action=$(this).attr('action');

		var method=$(this).attr('method');

		var params=$(this).serialize()+'&pfusr='+window.usr;

		$.ajax({type:method,url:action,data:params,success:function(msg){ 

			if(msg!='OK'){

				objConfirmacion.showError(msg);

				return;

			}else{

				objConfirmacion.showMsg('This item was removed from your shopping bag');

			}

			goToSection('shopping-bag',true); 

		} });

		return false;

	});

	

	$('form.formAsyncSetDiscount').live('submit',function(){

											   

		var action=$(this).attr('action');

		var method=$(this).attr('method');

		var params=$(this).serialize()+'&pfusr='+window.usr;

		$.ajax({type:method,url:action,data:params,success:function(msg){ 

			if(msg!='OK'){

				objConfirmacion.showError(msg);

				return;

			}else{

			}

			goToSection('shopping-bag',true); 

		} });

		return false;

	});
	
	
	$('.scroller').append('<div id="scrollLeft" style="left:0;"></div><div id="scrollRight" style="right:0"></div>');
	$('#scrollLeft, #scrollRight').css({'width':'200px','height':'100%','position':'absolute','z-index':1});
	$('#scrollRight').hover(function(){ bgmove=true; endlessScroll(1); }, function(){ bgmove=false; });
	$('#scrollLeft').hover(function(){ bgmove=true; endlessScroll(2); }, function(){ bgmove=false; });


});




