﻿/**
 * Script Julio Acuña
 **/

$(document).ready(function() {
    startGeneralBahavior();
	SliderContacto();
	Form("ingreso");
	change();
	addwrapper();
	switchMenu();
	styleMouse();
	showHeight();
	styleInformacion();
    Xwidth();
	WrapperSpan();
//	seerchSlider();
//    $("#ulSlider").easySlider({
//		auto: true,
//		continuous: true,
//		vertical: true,
//        controlsShow: false,
//		speed: 800
//	});	
});


function startGeneralBahavior() {
    setInputCellStyle();
    setClassForCRIRInputs();
    setHover();
    addMorePags();
    addMoreMB();
}

function setInputCellStyle() {
    $("table.blue_grid td:has(:input)").addClass("with_control");
}

function setClassForCRIRInputs() {
    var arrInputRadio = $("table#rbl_periodo input:radio, table#rbl_medio_pago input:radio");
    jQuery.each(arrInputRadio, function() {
        $(this).addClass("crirHidden");
    });
}

function setClassForCRIRInputsChk() {
    var arrInputRadio = $("table#list_pagos input:checkbox");
    jQuery.each(arrInputRadio, function() {
        $(this).addClass("crirHidden");
    });
}

function setHover() {
    $("#btnFinalizar").bind("mouseover", function() {
        $(this).attr("src", "../images/templates/bt_finalizar_hover.png");
    });
    $("#btnFinalizar").bind("mouseout", function() {
        $(this).attr("src", "../images/templates/bt_finalizar.png");
    });
}

function addMorePags() {
    $("select#ddl_pags").change(function() {
        var input = $("<input>").attr({ id: "txt_more_pags", name: "more_pags", type: "text", maxlength: "4" }).addClass("short");
        if ($(this).val() == "more") {
            input.insertAfter($(this));
            $("span#pags_info em").text("Páginas, debe ser múltiplo de 16");
        } else {
            if ($("input#txt_more_pags")) {
                $("input#txt_more_pags").remove();
                $("span#pags_info em").text("Páginas");
            }
        }
    });
}

function addMoreMB() {
    $("select#ddl_mb").change(function() {
        var input = $("<input>").attr({ id: "txt_more_mb", name: "more_mb", type: "text", maxlength: "4" }).addClass("short");
        if ($(this).val() == "more") {
            input.insertAfter($(this));
            $("span#mb_info em").text("MB, debe ser múltiplo de 5");
        } else {
            if ($("input#txt_more_mb")) {
                $("input#txt_more_mb").remove();
                $("span#mb_info em").text("MB");
            }
        }
    });
}

function Form(objId) {
	$("#" + objId).validate({
		rules:{
			nombre: "required",
			email: "required",
			mensaje: "required",
			categoria: "ddl_select",
			subcategoria: "ddl_select",
			nombre_producto: "required",
			descripcion: {
				required: true,
				maxWords: (0,255)
				},
		    cobertura: "lettersonly",
			codigo:  "number",
			imagen: "accept",
			precio_pesos_chileno: "number",
			precio_oferta: "number",
			nombre_sitio_web: { 
			    required:true,
				nowhitespace:true
			   }
		    }
		});
	
}

jQuery.validator.addMethod(
	"ddl_select", function(value, element) {
		if(element.value == "none") {
			return false;
		} else {
			return true;
		}
	},
	"<< Por favor, seleccione una opci&oacute;n."
);

function change() {
	$("table.grilla_green tbody tr").each(function() {
		 $(this).mouseover(function () {$(this).addClass("yelowtr")});
		 $(this).mouseout(function () {$(this).removeClass("yelowtr")});
		});
	
	$("table#grilla tfoot tr td:last").each(function() {
		 $(this).addClass("with_margin");
		});
	
	$("table#grilla thead tr th a.decoration").each(function() {
		 $(this).mouseover(function() { $(this).addClass("not_decoration")});
		 $(this).mouseout(function () {$(this).removeClass("not_decoration")});
 		 });		

	$("table#grilla tbody tr td a.light_blue").each(function() {
		 $(this).mouseover(function() { $(this).css("color","#0061ff")});
		 $(this).mouseout(function () {$(this).attr("style","")});
		});	
}

function switchMenu() {

	$("ul.contdetails li a").each(function() {
		var id = $(this).attr("id");
	   $(this).mouseover(function() {
		  $(this).addClass("blue_light");
	   });
	   $(this).mouseout(function() {
		 $(this).removeClass("blue_light");
	   });		
		$(this).click(function() {
		  $(this).delay(500,function(){
			attrValue(id);
	       });
		});
	});
}

function attrValue(idObj) {
   var $li = $("li."+idObj);
   $("div[wrapper]").each(function() {
         var value = $(this).attr("title");
		 if(idObj == value){
			 $("div[title='"+value+"']").slideDown("slow", function() {
  		     $li.css("background","none");
   		     $(this).css("position","relative");
			 $("a#"+value).addClass("listar_green");
			 $("a#"+value).removeClass("listar");
		   });
			 
		 }else{
			 $(this).slideUp(function() {
			 $("a#"+value).addClass("listar");			 
			 $("a#"+value).removeClass("listar_green");
			});
		 $("ul.contdetails li[class]").each(function() {
	          if($(this) != idObj){
			    $(this).attr("style","");
			  };
			});
		  };
   });							  
}

function addwrapper() {
   $("ul.contdetails div[wrapper]").each(function() {
		   if($(this).attr("wrapper") == "wrapper"){
			  $(this).hide();
		   }
   });
}

function styleMouse() {
   $("ul.subitems li a").each(function() {
		   $(this).mouseover(function() {
			  $(this).addClass("blue_light");
		   });
		   $(this).mouseout(function() {
		     $(this).removeClass("blue_light");
		   });
   });
}

function showHeight() {
   var maxWidth = 0;
   $("table.empresas tbody tr td:first").each(function() {
		if(maxWidth < $(this).width()){
		   maxWidth = $(this).width();
		}
	});
  $("table.empresas thead tr th div.cont_inputs").css("width",maxWidth);
  $("table.empresas thead tr th div.cont_inputs").next().css("float","left");
  
  $("table.empresas tbody tr td div a").each(function() {
		 $(this).mouseover(function() { $(this).addClass("light_blue").removeClass("blue");});
		 $(this).mouseout(function () {$(this).removeClass("light_blue").addClass("blue")});
  });  
}

function styleInformacion() {
   $("ul.ulInformacion li").each(function() {
	     var url = $(this).attr("wrapper");
		 var result = $("#box_result");
		 var title = $(this).attr("title");
	
		   $(this).mouseover(function() {
		   if( $(this).attr("class") != "current" ){
			  $(this).attr("id","highlight");
		      };
		   });
		   
		   $(this).mouseout(function() {
		     $(this).attr("id","");
		   });
		   
		 $(this).click(function() {		
			 result.load("cont/"+url+".aspx").fadeIn("slow");
			 removeClassP(url);
			 $(this).attr({'class':"current",'id':""});			 
		 });
   });
}

function removeClassP(idObj) {
   $("ul.ulInformacion li p").each(function() {
	     var id = $(this).attr('id');
		 if(idObj == id){
		 $(this).addClass("active");
	   }else 
	     $(this).removeClass("active");
		 $("ul.ulInformacion li[wrapper!="+id+"]").removeClass("current");
   });
}

function SliderContacto() {
   var ficha_contacto = $("#ficha_contacto").hide();
   var boton_contacto = $("#cont_btncontacto");
   
   $("#ver_ficha_contacto").bind("click", function() { ficha_contacto.slideDown("slow");boton_contacto.slideUp("slow");});
   $("#cancelar_contacto").bind("click", function() { ficha_contacto.slideUp("slow");boton_contacto.slideDown("slow");});
   
}

function WrapperSpan() {
	var div = "";
		
	$("div.box_datos").each(function() { 
			div = $(this).attr("id");
		 });
		
	$("ul.ulDatos li span.wrappenSpan").each(function() {
			 var maxSpan = $(this).width() + 110;
			 var x = 596 - maxSpan;
			 $(this).css('margin-right',x);		 
			});  
   
    $("ul.ulDatos li span.text13white").each(function() {
		    var rut = $(this).attr("title");
				$(this).click(function() {
				   $("div#" + rut).slideToggle("slow");
				   $("ul.ulDatos li[title=" + rut +"]").addClass("light_On");
				   $(this).text("Ocultar Detalles");
				});
     });
}

function Xwidth() {  
	   $("ul.ulDatos li div.box_datos").each(function() {
			 $(this).hide()
		});
}

function overlay() {
	 $("div.productos a").each( function() {
			 $(this).click(function() {
				var scrol = (($(window).height() - $('#boxLight').height())/2) - 100;
				  $("#overlay_capa").css({display:'block',width: $(document).width(),height:$(document).height()}).fadeIn("slow");
				  $("#boxLight").css({
		             display:'block',
		             left:( $(window).width() - $('#boxLight').width() )/2,
		             top:( $(window).height() - $('#boxLight').height() )/2 - 250,
		             position:'absolute'}).fadeIn("slow").load("temp/temp.aspx");
				})					   
			})
}

function closer_light() {
	 $("#closer_light").click(function() {
		  $("#boxLight").fadeOut("slow");
		 })
}

function start_winner() {
 var arrayPageSize = getPageSize();
 Element.setHeight('overlay', arrayPageSize[1]);
 new Effect.Appear('overlay', { duration: 0.5, from: 0.0, to: 0.6, afterFinish: function() { $('boxLight').style.display = 'block'; } });
}

function seerchSlider() {
  var start = $(".avanced");
  var box = $("#slider");
  var finish = $("#finish");
  
  start.click(function() {
	   box.slideDown("slow");
	 })
  finish.click(function() {
	   box.slideUp("slow");
	 })
}


