var numli;
var ordenli = 1;
var numeroli;
var secuenciali = 1;
function analisis(clas,obj){
	$('.results').addClass('hidden');
	$('.'+ clas).removeClass('hidden');
	
	$('#searchs li').removeClass('active');
	$(obj).parent().addClass('active');
}

function olvide(){
	var div = $('#efect');
	div.fadeIn('slow');
	return false;
}

function mostrarerror(){
	var div = $('#efecto');
	div.fadeIn('slow');
	return false;
}


//function vermas(){
function vermas(id,TypeID,CatID,where,q){
	
	/*AGREGADO EL 18/10/2011
	var id 		= $id;
	var TypeID 	= $TypeID;
	var CatID 	= $CatID;
	var where 	= $where;
	var q 		= $q;
	*/
	
	var div = $('#product_dos');
	var page = $('#page').val();
	var items = $('#items').val();
	var SubID = $('#SubID').val();

	var hidden_TypeID = $('#hidden_TypeID').val();
	var hidden_CatID = $('#hidden_CatID').val();
	var hidden_SubID = $('#hidden_SubID').val();
	
	page=parseInt(page)+1;
	$('#page').val(page);
	var div = $('#product_dos');
	
	//$.getJSON('mod/ajax.php',{'page':page, 'items':items, 'SubID':SubID, 'hidden_TypeID':hidden_TypeID,'hidden_CatID':hidden_CatID, 'hidden_SubID':hidden_SubID} , 
	$.getJSON('mod/ajax.php',{'page':page, 'items':items, 'SubID':SubID, 'hidden_TypeID':hidden_TypeID,'hidden_CatID':hidden_CatID, 'hidden_SubID':hidden_SubID,'id':id, 'TypeID':TypeID, 'CatID':CatID, 'where':where,'q':q} , 	
		function(data) {
			
			if(data.cantidad==0) { 
				$('#vermas').hide(); 				
			}
			div.append(data.results);
		});
		
	return false;
}

function vermasbus(){
	var div = $('#product_dos');
	var page = $('#page').val();
	var items = $('#items').val();
	/*
	NOSE SE UTILIZA
	var SubID = $('#SubID').val();
	*/
	var bus = $('#bus').val();

	page=parseInt(page)+1;
	$('#page').val(page);
	var div = $('#product_dos');
	
	$.getJSON('mod/ajaxbus.php',{'page':page, 'items':items, 'bus':bus} , 
		function(data) {
			if(data.cantidad==0) {$('#vermas').hide();}
			div.append(data.results);
		});
	return false;
}

var ScriptManya = {
	popUp : function(optionManya){
		var src = optionManya.src;
		
		var alto = (optionManya.height == '') ? 0 : optionManya.height;
		var ancho = (optionManya.width == '') ? 0 : optionManya.width;
		
		if(optionManya.type == 'image'){
			var foto = new Image();
			foto.src = src;
			ancho = foto.width;
			alto = foto.height;
			var num = 6;
		}else{
			var num = 0;
		}
		
		var top = parseInt((alto + num)/2);
		var left = parseInt((ancho + num)/2);
		
		var background = $(document.createElement('div')).css({'position':'fixed',
														'top': '0px',
														'left': '0px',
														'width': '100%',
														'height': '100%',
														'z-index' : '99999'})
													.addClass('popUp-Background');
		
		var box = $(document.createElement('div')).css({'position':'fixed',
														'top': '0',
														'left': '50%',
														'width': (ancho + num) +'px',
														'height': (alto + num) +'px',
														'margin-left': '-'+ left +'px',
														'z-index' : '100000'}).addClass('popUp-Box');

		if(optionManya.type == 'iframe'){
			var cbox = $(document.createElement('div')).addClass('popUp-Content');
			var cont = $(document.createElement('iframe')).css({'width': ancho +'px',
																'height': alto +'px',
																'background': 'transparent',
																'border': '0'})
															.attr({
																'frameborder':0,
																'src': src
															})
															.addClass('popUp-Body');
		}else{
			var cbox = $(document.createElement('div')).addClass('popUp-Content none');
			var cont = $(document.createElement('div')).css({'width': ancho +'px','height': alto +'px'}).addClass('popUp-Body').html('<img src="'+ foto.src +'" />');
		}
		
		var close = $(document.createElement('div')).css({'float':'right'})
													.addClass('popUp-Close')
													.html('<a href="Javascript:void(0)"><div class="icon"></div><div class="flotante"></div></a>')
													.click(function(){
														$('.popUp-Background').remove();
														$(this).parents('.popUp-Box').remove();
														$('.btnLogin span span').html('Sign in');
														$('.btnLogin').attr('disabled',false);
														$('.btnLogin').removeClass('disabled');
													});
		
		cbox.append(close);
		cbox.append(cont);
		box.append(cbox);
		$('body').append(background);
		$('body').append(box);
	}
};

function closePopUp(){
	$('.popUp-Background').remove();
	$('.popUp-Box').remove();
}

function popUp(anc,alt,href){
	ScriptManya.popUp({
		src:href,
		type:'iframe',
		width:anc,
		height:alt
	});
}

function transicion(){
	$('.slide_'+ ordenli).fadeOut(1000);// 2
	ordenli++;// 3
	if(ordenli > numli){
		ordenli = 1;
	}
	$('.slide_'+ ordenli).fadeIn(2000);// 1
}

function efecto(){
	$('.bs_'+ secuenciali).fadeOut(1000);// 2
	secuenciali++;// 3
	if(secuenciali > numeroli){

	secuenciali = 1;
	}
	$('.bs_'+ secuenciali).fadeIn(2000);// 1

}

function addItem(id, unit_id, cant){
/*
	alert(  "id="+		id+"\n"+
			"unit_id="+	unit_id+"\n"+
			"cant="+	cant+"\n");
*/			
	$.ajax({
	  type: "POST",
	  url: "mod/addItem.php",
	  data: "id="+id+
			"&unit_id="+unit_id+
			"&cant="+cant,
	  success: function(data){
					//alert( data);
					//location.href='milista.php';

					$.fancybox.close();
					$('#anadir'+id).effect("transfer", { to: $(".nro_items") }  , 1500);
					$.getJSON('mod/showItemsQuantity.php',{} , 
							function(data) {
								$('.nro_items').html(data.nro_items);
							});
					$("#cantdet"+id).val('');
					/*
					setTimeout( function(){
					
						$('#anadir'+id).effect("transfer", {
								to: $(".nro_items")
							}, 1500);	
						
					setTimeout( function(){
							$.getJSON('mod/showItemsQuantity.php',{} , 
							function(data) {
							$('.nro_items').html(data.nro_items);
					});
						}, 1500 );
						$("#cantdet"+id).val('');
					}, 500 );
					*/
				  }		
	  
	});
  
 /* 			
	$.post("mod/addItem.php",{
		'id': id ,'unit_id': unit_id ,'cant': cant
		},function(data){
				//alert( data);
				//location.href='milista.php';
				$.fancybox.close();
				setTimeout( function(){
				
					$('#anadir'+id).effect("transfer", {
							to: $(".nro_items")
						}, 1500);	
					
				setTimeout( function(){
						$.getJSON('mod/showItemsQuantity.php',{} , 
						function(data) {
						$('.nro_items').html(data.nro_items);
				});
					}, 1500 );
					$("#cantdet"+id).val('');
				}, 500 );
			
		});
*/	
	}
	
	
	//Web
	function validateCar(){
		var nro_items = $('.nro_items').html();
		//alert(nro_items);
		if(nro_items==0){
			$('#mensaje_car').css('display','block');
		}

		var color_a="#999999";
		var color_b="#FF0000";
		$(".campo").css("border-color",color_a);
		var nom_list=$('#nom_list').attr('value');
		var tel_list=$('#tel_list').attr('value');
		var suc_list=$('#suc_list').attr('value');
		var emp_list=$('#emp_list').attr('value');
		var ruc_list=$('#ruc_list').attr('value');
		var tel_list=$('#tel_list').attr('value');
		var com_list=$('#com_list').attr('value');
		var error = false;
		
		var mai_list=$('#mail_list');
		
		if(nom_list==''){$('#nom_list').css("border-color",color_b); error=true;}
		if(tel_list==''){$('#tel_list').css("border-color",color_b); error=true;}
		if(suc_list==''){$('#suc_list').css("border-color",color_b); error=true;}
		
		if(mai_list.val() == '' || validarEmail(mai_list.val()) === false){mai_list.css("border-color",color_b); error=true;}

		if(error) return false;
		
		$.getJSON('mod/SendList.php',{
			'nom_list' : nom_list,
			'emp_list' : emp_list,
			'ruc_list' : ruc_list,
			'tel_list' : tel_list,
			'suc_list' : suc_list,
			'com_list' : com_list,
			'mai_list' : mai_list.val()
		},function(data) {
			$('#car').html(data.car);
			$('.nro_items').html(data.nro_items);
			$('.campo').attr('value','');
			//mostrarerror();
			$.fancybox({
				'href': 'pop_compra.php'
			});
		});
		return false;
}

function DeleteItem(id){
	$.getJSON('mod/deleteItem.php',{ 'id': id } , 
	function(data) {
		$('.fila_car_'+id).remove();
		$.getJSON('mod/showItemsQuantity.php',{} , 
		function(data) {
			$('.nro_items').html(data.nro_items);
			if(data.nro_items==0){
			$('#car').html('<center class="noreg"><b>No hay productos en lista</b><br /><div id="mensaje_car" style="color:#F40000;display:none">Agregue productos a la lista para continuar</div></center>');
			}
		});
	});
}

function UpdateQuantity(cant, id){
	$.getJSON('mod/editItem.php',{ 'cant': cant, 'id': id} , 
	function(data) {
		$.getJSON('mod/showItemsQuantity.php',{} , 
		function(data) {
			$('.nro_items').html(data.nro_items);
		});
	});
}



function validarEmail(valor) {
	re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor)){
        return false;
    }else{
        return true;
    }
}

function soloNumeros(evt){
	if(window.event){
		keynum = evt.keyCode;
	}else{
		keynum = evt.which;
	}
	if ((keynum>47 && keynum<58)  || (keynum==8)  || (keynum==0)){
		return true;
	}else{
		return false;
	}
}

function soloNumerosEnter(evt){
	if(window.event){
		keynum = evt.keyCode;
	}else{
		keynum = evt.which;
	}
	if ((keynum>47 && keynum<58)  || (keynum==8)  || (keynum==0) || (keynum==13) ){
		if(keynum==13){
		}else{
			return true;
		}
	}else{
		return false;
	}
}

function validateCV(){
	var form='#form_cv';
	var nam=$(form+' #nam').val();
	var age=$(form+' #age').val();
	var tel=$(form+' #tel').val();
	var com=$(form+' #com').val();
	var ema=$(form+' #ema').val();
	var jobs=$(form+' #jobs').val();
	var file=$(form+' #file').val();
	
	var color_a="#999999";
	var color_b="#FF0000";
	$(form+" #nam").css("border-color",color_a);
	$(form+" #age").css("border-color",color_a);
	$(form+" #tel").css("border-color",color_a);
	$(form+" #com").css("border-color",color_a);
	$(form+" #ema").css("border-color",color_a);
	$(form+" #jobs").css("border-color",color_a);
	$(form+" #file").css("border-color",color_a);
	
	if(nam=='') {$(form+" #nam").css("border-color",color_b);}
	if(age=='') {$(form+" #age").css("border-color",color_b);}
	if(tel=='') {$(form+" #tel").css("border-color",color_b);}
	if(com=='') {$(form+" #com").css("border-color",color_b);}
	if(ema=='') {$(form+" #ema").css("border-color",color_b);}
	if(jobs=='') {$(form+" #jobs").css("border-color",color_b);}
	if(file=='') {$(form+" #file").css("border-color",color_b);}
	
	if(nam=='' || age=='' || tel=='' || com=='' || ema=='' || file=='' || jobs=='') return false;
	
	if (!validarEmail(ema)) {
		$(form+" #ema").css("border-color",color_b);
		$(form+" #ema").focus();
		return false;
	}
	return true;
}
// fin de web

function viewAbout(id){
	$(".abouts").css('display','none');
	$('#about'+id).css('display','inline');
}

$(document).ready(function(){
	$(".inpt2").filestyle({ 
		image: "img/choose-file.gif",
		imageheight : 27,
		imagewidth : 37,
		width : 202
	});				   
	//web
	$("#slider").easySlider({
		auto: true,
		controlsShow: true,
		continuous: false
	});

	//$("#accordion h3:first").addClass("active");
	//$("#accordion div:not(:first)").hide();
	$("#accordion div").hide();
	
	$("#accordion h3").click(function(){
		$(this).next("div").slideToggle("slow").siblings("div:visible").slideUp("slow");
		$(this).toggleClass("active");
		$('.titleh3').removeClass("active");
		$(this).addClass("active");
		$(this).siblings("h3").removeClass("active");
	});	
	
	$(".service").click( function(){
		var id=$(this).attr('value');
		var name=$(this).attr('name');
		var html=$("#service"+id).html();
		$(".spantitle").html(name);
		$("#showService").html(html);
		$('.services').show();
		$("#service"+id).hide();
	});
	
	$(".sucursals").click( function(){
		var id=$(this).attr('value');
		$('#content_right').html('');
		$.post("mod/showStores.php",{
			'type': id
		},function(data){
			$('#content_right').html(data);
		});
	});
	
	$(".store").click( function(){
		var id=$(this).attr('value');
		$.post("mod/showStore.php",{
		'id': id
		},function(data){
		$('.text').html(data);
		});
	});
	
	$(".job").click( function(){
		var id=$(this).attr('value');
		$(".jobs").removeClass('active');
		$("#job"+id).addClass('active');
		$.getJSON('mod/showJob.php',{'id': id } , 
		function(data) {
			$('#content_right').html(data.content);
		});
	});
	
	$(".mycontent").click( function(){
		var id=$(this).attr('value');
		$.getJSON('mod/showContentJob.php',{'id': id } , 
		function(data) {
			$('#content_right').html(data.content);
		});
	});
	
	$(".mycontent_cot").click( function(){
		var id=$(this).attr('value');
		$.getJSON('mod/showContentCot.php',{'id': id } , 
		function(data) {
			$('#content_right').html(data.content);
		});
	});
	
	$(".mycontent_rec").click( function(){
		var id=$(this).attr('value');
		$.getJSON('mod/showContentRec.php',{'id': id } , 
		function(data) {
			$('#content_right').html(data.content);
		});
	});
	
	$("#sendEmail").click( function(){
		var email=$('#email_prom').attr('value');
		
		var color_a="#FFFFFF";
		var color_b="#FFB5B5";
		$(".campo").css("background-color",color_a);
		
		if(email==''){$('#email_prom').css("background-color",color_b);return false;}
		
		if (!validarEmail(email)) {
		$('#email_prom').css("background-color",color_b);
		$('#email_prom').focus();
		return false;
	}
		
		$.getJSON('mod/validateEmail.php',{'email': email } , 
		function(data) {
			$('#email_prom').attr('value','');
			$('#result_email').html(data.res);
		});
	});
	
	$("#email_prom").keypress( function(e){

		if(e.which == 13) { 

		var email=$('#email_prom').attr('value');
		
		var color_a="#FFFFFF";
		var color_b="#FFB5B5";
		$(".campo").css("background-color",color_a);
		
		if(email==''){$('#email_prom').css("background-color",color_b);return false;}
		
		if (!validarEmail(email)) {
		$('#email_prom').css("background-color",color_b);
		$('#email_prom').focus();
		return false;
	}
		
		$.getJSON('mod/validateEmail.php',{'email': email } , 
		function(data) {
			$('#email_prom').attr('value','');
			$('#result_email').html(data.res);
		});
		}
	});
	
	$("#btn_bus").click( function(){
		var bus=$('#bus').attr('value');
		var color_a="#999999";
		var color_b="#FF0000";
		$("#bus").css("border-color",color_a);
		if(bus==''){$('#bus').css("border-color",color_b);return false;}
		
		$.getJSON('mod/showSearch.php',{'bus': bus } , 
		function(data) {
			$('#content_right').html(data.catalog);
		});
	});
	
	$("#bus").keypress( function(e) {
        if(e.which == 13) {         
		var bus=$('#bus').attr('value');
		
		var color_a="#999999";
		var color_b="#FF0000";
		$("#bus").css("border-color",color_a);
		if(bus==''){$('#bus').css("border-color",color_b);return false;}
		
		$.getJSON('mod/showSearch.php',{'bus': bus } , 
		function(data) {
			$('#content_right').html(data.catalog);
		});
        }
    });

	

	$("#saveRec").live("click", function(){
		var fec_rec = $('#fec_rec').attr('value');
		var suc_rec = $('#suc_rec').attr('value');
		var per_rec = $('#per_rec').attr('value');
		var rec_rec = $('#rec_rec').attr('value');
		
		var color_a="#999999";
		var color_b="#FF0000";
		$(".campo").css("border-color",color_a);
		
		if(suc_rec=='')$('#suc_rec').css("border-color",color_b);
		if(rec_rec=='')$('#rec_rec').css("border-color",color_b);
	
		if(suc_rec=='' || rec_rec=='')return false;
		$.getJSON('mod/saveRec.php',{'fec_rec': fec_rec, 'suc_rec': suc_rec, 'per_rec': per_rec, 'rec_rec': rec_rec } , 
		function(data) {
			$(".campo").attr('value','');
			$(".campo").css("border-color",color_a);
			$("#results").html('Se ha enviado correctamente');
		});
	});
	
	$(".type").live("click", function(){
		var id=$(this).attr('value');
		var type='1';

		$.ajax({
		   type: "GET",
		   url: "mod/showCategory.php",
		   data: {'id': id, 'type': type },
		   success: function(data){
			 	//alert(data);
				$('#content_right').html(data);
		   }
		 });
	});

	$(".category").live("click", function(){
		var id=$(this).attr('value');
		var TypeID=$(this).attr('name');
		var type='2';
		/*$.getJSON('mod/showSubcategory.php',{'id': id, 'type': type, 'TypeID': TypeID } , 
		function(data) {
			alert(data.catalog);
			$('#content_right').html(data.catalog);
		});
		*/
		$.ajax({
		   type: "GET",
		   url: "mod/showSubcategory.php",
		   data: {'id': id, 'type': type, 'TypeID': TypeID },
		   success: function(data){
			 	//alert(data);
				$('#content_right').html(data);
		   }
		 });
	});
	
	$(".subcategory").live("click", function(){
		var id=$(this).attr('value');
		var TypeID=$(this).attr('name');
		var CatID=$(this).attr('CatID');
		
		var temp=$(this).attr('temp');
		if(temp == undefined || temp == null){
			temp = 0;
		}
		
		var type='3';
		/*
		$.getJSON('mod/showProducts.php',{'id': id, 'type': type, 'TypeID': TypeID, 'CatID': CatID ,'temp':temp} , 
		function(data) {
			$('#content_right').html(data.catalog);
		});
		*/
		$.ajax({
		   type: "GET",
		   url: "mod/showProducts.php",
		   data: {'id': id, 'type': type, 'TypeID': TypeID, 'CatID': CatID ,'temp':temp},
		   success: function(data){
			 	//alert(data);
				$('#content_right').html(data);
		   }
		 });
	});

	function validarInt(valor){
		var flag = false;
		var re = /^(-)?[0-9]*$/;
		if(!re.test(valor)){
			flag = false;
		}else{
			flag = true;
		}
		return flag;
 	}
	
	$(".addcart").live("click", function(){
		var value=$(this).attr('value');
		var color_a="#999999";
		var color_b="#FF0000";
		var error = "";
		$(".cantdet").css("border-color",color_a);
		$(".unidet").css("border-color",color_a);
		var cant=$('#cantdet'+value).attr('value');
		if((cant=='' || cant==0) || !(validarInt(cant))){
			$("#cantdet"+value).css("border-color",color_b);
			error = 1;
		}
		var uni=$('#unidet'+value).val();
		//alert(uni);
		if(uni=='' || uni==0){
			$("#unidet"+value).css("border-color",color_b);
			error = 1;
		}
		
		if(error == 1) return false;
		
		var url=$(this).attr('url')+'?id='+value+'&cant='+cant+'&unidad='+ uni;
		$.fancybox({'href': url});
	});
	
	$("#addList").live("click", function(){
		var value=$(this).attr('value');
		var color_a="#999999";
		var color_b="#FF0000";
	});
	
	$(".cerrar").live("click", function(){
		var id_prod=$(this).attr("value");
	 	var url=$(this).attr('url')+'?id='+id_prod;
		popUp(448,250,url);
	 });
		 
	$('.cant').live('keypress', function(e) {
        if(e.which == 13) {
            var cant = $(this).attr('value');
			var id = $(this).attr('myid');
			if(cant>0 || cant!=''){ UpdateQuantity(cant, id);}
        }
    });

	$('.cant').live("blur", function(e) {
		var color_a="#999999";
		var color_b="#FF0000";
	 	var cant = $(this).attr('value');
		var id = $(this).attr('myid');
		$(this).css("border-color",color_a);
		if((cant=='' || cant==0) || !(validarInt(cant))) {
			$(this).css("border-color",color_b);
			return false;
		}
		if(cant>0 || cant!=''){ UpdateQuantity(cant, id); }
    });

	$(document).pngFix(); 
	//fin de web
						   
	// Detectar id=Slider ul
	// find() // lo que quieres buscar
	numli = $('#slider ul').find('li').length;// 2
	setInterval(transicion,4000);
	
	numeroli = $('#banner_slider ul').find('li').length;// 2
	setInterval(efecto,5000);
	
	var index = 99;
	for(var i=1; i<=numeroli; i++){
		$('#banner_slider ul .bs_'+ i).css({
			'z-index' : index
		});
		index--;
	}
	
	$(".type_oscar").live("click", function(){
		var id=$(this).attr('value');
		var type='1';

		$.ajax({
		   type: "GET",
		   url: "mod/showCategory_final.php",
		   data: {'id': id},
		   success: function(data){
			 	//alert(data);
				$('#content_right').html(data);
		   }
		 });
		 
	});
});
