/**
 * @author sarca
 */

$(document).ready(function(){
	
	var v=jQuery("#frmLogin").validate({
		debug:true,
		errorPlacement: function(error, element) {
			error.appendTo(element);
		},
		rules:{
		   	logUsername:{required:true},
			logPasswd:{required:true}
		},
		msg:{
			logUsername:{required:""},
			logPasswd:{required:""}
		},
		submitHandler: function(form){
			jQuery(form).ajaxSubmit({
			type:'post',
			url:"services/service_login.php",
			dataType:'text',
			data:{adminLogin:'1'},
			beforeSend:function(){
				$("#loginLoader").fadeIn();	
			},
			success: function (data, status){
				$("#loginLoader").fadeOut();	
				switch(data){
					case '1':	jAlert("Username does not exist!");
								$("#logUsername").focus().select();		
							
					break;
					
					case '2': 	jAlert("Wrong password!");
								$("#logPasswd").focus().select();
					break;
					
					case '3':	window.location.reload();
					break;
					
					case '4': 	jAlert("You can not access until administrative verification");
					break;

				}
			},
			error: function (data, status, e){
					jAlert("Ajax Error: "+e);
			}
		});
		}
	});
	
	$('#prodBanner').cycle({ 
	    fx:     'fade',
		speed: '600', 
	    timeout: 2000
		
		
	});
	
	
	$('#mainBanner').cycle({ 
	    fx:     'fade',
		speed: '600', 
	    timeout: 4000, 
	    pager:  '#mainBannerNav' 
	});
	
	//barra navegación panel
	var p = $("#mainColumn");
	var offset = p.offset();
	var Navtop=offset.top+360;
	var Navleft=offset.left+6;
	//jAlert("left: " + offset.left + ", top: " + offset.top );
	$("#mainBannerNav").css({
		position:'absolute',
		left:Navleft,
		top:Navtop
	});
	
	$("#mainBannerNav a").wrap('<div class="divNav"></div>');
	var marginLeft=735-(31*($("div.divNav").length));
	
	$("#mainBannerNav div.divNav:first").css({
		'margin-left':marginLeft
	});
	
	
	$(".btnHover").hover(
		function(){
			var current_path=$(this).attr('src');
			var hover_path=str_replace('.png','_over.png',current_path);
			$(this).attr({src:hover_path});
		},
		function(){
			var current_path=$(this).attr('src');
			var hover_path=str_replace('_over.png','.png',current_path);
			$(this).attr({src:hover_path});
		}
	);
	
	$("button.btnHover").hover(
		function(){
			var current_path=$("#"+$(this).attr('id')+" img").attr('src');
			var hover_path=str_replace('.png','_over.png',current_path);
			$("#"+$(this).attr('id')+" img").attr({src:hover_path});
		},
		function(){
			var current_path=$("#"+$(this).attr('id')+" img").attr('src');
			var hover_path=str_replace('_over.png','.png',current_path);
			$("#"+$(this).attr('id')+" img").attr({src:hover_path});
		}
	);
	
	$(".prodAcord").css({
		cursor:'pointer'
	});
	$(".prodAcord").click(function(){
		
		switch($(this).attr('id')){
			case 'cat1-link':	$("#cat2-cont").slideUp(500);
								$("#cat1-cont").slideDown(500);
			break;
			
			case 'cat2-link':	$("#cat1-cont").slideUp(500);
								$("#cat2-cont").slideDown(500);
			break;
			
		}
	});
});

function loadProduct(idprod){
	var page=explode("/",location.href);
	page=page[page.length-1];
	var pageParam=strripos(page, 'php?');
	//jAlert("Paginaddd: "+page+"<br />Valor:"+pageParam);return;
	if(page != 'product.php' && pageParam ===false){
		//jAlert("Paginasss: "+page+"<br />Valor:"+pageParam);
		window.location.href='product.php?idproduct='+idprod;
	}
	else{
		$.ajax({
			type:'POST',
			url:'services/service_producto.php',
			data:{loadProd:1,loadProduct:idprod},
			dataType:'text',
			beforeSend:function(){
				var loader='<div style="margin:30% 50%;"><img src="images/ajax-loader.gif" /></div>';
				$("#ContProd").html(loader);	
			},
			success:function(data, textStatus){
				$("#ContProd").html(data);
				
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				if(textStatus!='' || textStatus != null){
					jAlert("Error: "+textStatus);
				}
				else{
					jAlert("Error: "+errorThrown);
				}
			}
		});	
	}
}


function logout(){
	$.ajax({
			type:'POST',
			url:'services/service_login.php',
			data:{logout:1},
			dataType:'text',
			beforeSend:function(){
				$("#loginLoader").fadeIn();	
			},
			success:function(data, textStatus){
				$("#loginLoader").fadeOut();
				window.location.reload();	
				
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				if(textStatus!='' || textStatus != null){
					jAlert("Error: "+textStatus);
				}
				else{
					jAlert("Error: "+errorThrown);
				}
			}
		});	
}

