/**
 * @author sarca
 */

$(document).ready(function(){
	
	//$("#box_black").height($("#content").height());
	
	
	//VIDEO 
	$("#video_prom").colorbox({width:"700",height:"500",href:"videos/video.php",title:'Equipos Gleason',iframe:true});
	
	
	$('a img').css('border','none');
	
	//menu activo
	$(".submenuActive").parent().prev().children().addClass('active_menu');
	if ($("#menu_buttons a img.active_menu").size()>0) {
		var newsrc = $("#menu_buttons a img.active_menu").attr('src');
		newsrc = newsrc.replace('.jpg', '_active.jpg');
		$("#menu_buttons a img.active_menu").attr('src', newsrc);
	}
	
	
	//Efecto hover de los menú
	$("#menu_buttons a img").hover(function(){
        if(!$(this).hasClass('active_menu')){
			var newsrc = $(this).attr('src');
	        newsrc = newsrc.replace('.jpg', '_over.jpg');
	        $(this).attr('src', newsrc);	
		}
    }, function(){
		 if (!$(this).hasClass('active_menu')) {
			var newsrc = $(this).attr('src');
	        newsrc = newsrc.replace('_over.jpg', '.jpg');
    	    $(this).attr('src', newsrc); 
		 }
    });

	
	//dialogo video
	$('#videoGleason').dialog({
		autoOpen:false,
		modal:true,
		width:600,
		height:380
	});
	
	//Efecto hover video
	if($("#video_prom").size()>0){
		
		$("#video_prom").hover(
			function(){
				$("#video_prom img").attr({src:'images/banners_gleason/amd_over_07.png'})	
			},
			function(){
				$("#video_prom img").attr({src:'images/banners_gleason/amd_07.png'})	
			}
		);
	}
	
	/*open active onload*/
		$(".submenuActive").parent().slideToggle(200,"easeOutQuad")
	
	
	
	/*show submenus*/
	$("img.has_sub").click(function(){
		var submenuId=$(this).parent().next().attr('id');
		$(".submenu:visible:not(#"+submenuId+")").slideToggle(200,"easeOutQuad");
		$("#"+submenuId).slideToggle(200,"easeOutQuad");
	});
	
	if($.browser.msie()){
		if( $.browser.version.number() <= 6 ){
			png_to_gif();
		}
	}
	//hover botones
	$(".btnHover").hover(
		function(){
			//consultando la ruta de la imagen
			var newsrc = $(this).children().attr('src');
			//consultando la extención
			var mime=newsrc.slice(newsrc.length-4);
			
	        newsrc = newsrc.replace(mime, '_over'+mime);
	        $(this).children().attr('src', newsrc);	
		},
		function(){
			var newsrc = $(this).children().attr('src');
			var mime=newsrc.slice(newsrc.length-4);
	        newsrc = newsrc.replace('_over'+mime, mime);
    	    $(this).children().attr('src', newsrc); 
		}
	);
	
	
	
	//LOGIN
	$("#login_username").focus(function(){
		if($(this).val()=='Usuario'){
			$(this).val('');
		}
	});	
	
	$("#login_username").blur(function(){
		if($(this).val()==''){
			$(this).val('Usuario');
		}
	});	
	
	$("#login_passwd").focus(function(){
		if($(this).val()=='Contrseña'){
			$(this).val('');
		}
	});	
	
	$("#login_passwd").blur(function(){
		if($(this).val()==''){
			$(this).val('Contrseña');
		}
	});	
	
	
	
	
	
	
	//VALIDANDO LOGIN
	var v=jQuery("#frm_login").validate({
		debug:true,
		errorLabelContainer:"#login_error",
		rules:{
		   	login_username:{required:true},
			login_passwd:{required:true}
		},
		submitHandler: function(form){
			jQuery(form).ajaxSubmit({
				url:"services/service_login.php",
				data:{login_user:"1"},
				ajaxStart:function(){
						$("#div_form").hide('fast',function(){
						$("#frm_loader").show('fast');	
					})
				},
				success: function showResponse(responseText, statusText){
					switch(responseText){
						case '1': $("#login_error").text('Usuario Invalido');
						break;
						
						case '2': $("#login_error").text('Contraseñ Invalida');
						break;
						
						default: window.location.href='index.php';
						break;
					}
				}
			});
		}
	});
	
	
	//mostrando primer bloque de contenido estatico
	
	/*var dinLink=$("#sub_menu a.db").length;
	if(parseInt(dinLink) < 1 ){
		var contlink=$(".program:first").length;
		if(contlink >= 1){
			var idlink=$(".program:first").attr('id');
			$("#info_container").html($("#"+idlink).html());	
		}
	}*/
	
	var dinLink=$("#sub_menu a.db").length;
	//alert(dinLink);
	if(parseInt(dinLink) < 1 ){
		var contlink=$(".program:first").length;
		if(contlink >= 1){
			$("#info_container").hide();
			$(".program:first").show();
				
		}
	}
	
});

$(window).resize(function(){
	var posicion=$("#menu_wrapper").position();
	$("#menu").css({
		left:posicion.left+5
	}).fadeIn(3000);
});


function png_to_gif(){
	//background images
	$("#menu_buttons").css({background:'url(../images/base/menu_02.gif)'})
	
	//images
	$("#img_menu_01").css({display:'none'}).attr({src:'../images/base/menu_01.gif'}).fadeIn(3000);
	$("#img_menu_11").css({display:'none'}).attr({src:'../images/base/menu_11.gif'}).fadeIn(3000);
	$(".img_leer_mas_black").css({display:'none'}).attr({src:'../images/home/leer_mas_black.gif'}).fadeIn(3000);
	$(".img_leer_mas").css({display:'none'}).attr({src:'../images/home/leer_mas.gif'}).fadeIn(3000);
	$(".name_footer").css({display:'none'}).attr({src:'../images/base/name_footer.gif'}).fadeIn(3000);
	$(".img_at").css({display:'none'}).attr({src:'../images/base/at.gif'}).fadeIn(3000);
	
}


function show_content(menuName){
var strURI=document.URL;
var data=strURI.split('/');
if(data[data.length - 1] != 'empresa.php' && data[data.length - 1] != "empresa.php?section="+menuName){
	 window.location.href="empresa.php?section="+menuName;
}
else{
	$.ajax({
		type:"POST",
		url:"services/service_empresa.php",
		data:"show_content=1&menuName="+menuName,
		beforeSend:function(){
			$('#content_text').html("<img src='../images/base/load.gif' />");
		},
		success: function(msg){
			$('#content_text').html(msg);
			$('#hm_'+menuName).addClass('mh_active');
			$('.empresa_hm:not(#hm_'+menuName+')').removeClass('mh_active');
			
			$('#vm_'+menuName).addClass('submenuActive');
			$('.empresa_vm:not(#vm_'+menuName+')').removeClass('submenuActive');
		}
	});	
}

}


function register_user(){
	
}//end create_bckuser

function show_register(){
	$("#div_formRegister").slideToggle('normal',"easeInCubic");
}


function switchContent(id){
	//cambiar a link activo
	if($("#"+id).hasClass('active')){
		return false;
	}
	else{
		$(".active").removeClass('active');
		$("#"+id).addClass('active');
		$.ajax({
			type:"POST",
			url:"services/service_general.php",
			data:{switchContent:1,idcontent:id},
			beforeSend:function(){
				$("#loaderContent").show();
			},
			success: function(data){
				$("#loaderContent").hide();
				if($(".program:visible").length >=1){
					$(".program").hide();
					$("#info_container").show();
				}
				else{
					var alto=$("#info_container").height();
					var nuevo_alto=alto-20;
				
				$("#info_container").html(data).append("<div style='clear:both;'></div>");
				//$("#info_container").animate({height: nuevo_alto}).html(data).append("<div style='clear:both;'></div>").animate({height: alto});
				}
			}
		});	
	}
}



function loadDiv(id,idlink){
	var links=$("#sub_menu a").length;
	if(links > 1){
		if($("#"+idlink).hasClass('active')){
			return false;
		}
		else{
			$(".active").removeClass('active');
			$("#"+idlink).addClass('active');
		}	
	}
	
	/*
	if($(".currentCont").length>0){
		$(".currentCont").html($("#info_container").html());
		$(".currentCont").removeClass('currentCont');
	}
	*/
	
	if($("#sub_menu a.db").length >=1){
		$("#info_container").hide();
	}
	$(".program").hide();
	
	var alto=$("#"+id).height();
	var nuevo_alto=alto-20;
	$("#"+id).animate({height: nuevo_alto},600).animate({height:alto},600);
	
	/*
	$("#info_container").html($("#"+id).html());
	$("#"+id).addClass('currentCont').html('');
	*/
	
}

function loader(estado,id){
	if(estado==1){
		$(id).show();	
	}
	else{
		$(id).hide();
	}
	
}


function showSpiner(contenedor){
	$(contenedor).html("<img src='../images/base/load.gif' />");
}


