document.domain = 'todobebe.com';

jQuery.noConflict();

// Put all jQuery code inside this function
jQuery(document).ready(function($){
  
  //quick registration cookies
  if($.cookie("quickreg_donotshow") != '1'){ 
    if (parseInt($.cookie("quickreg_click")) > 1){
      Boxy.load("/quick_reg", {title: "¡No te pierdas nuestras novedades!", closeText: "X"});
    }else{
      if($.cookie("quickreg_click") == null){
        $.cookie("quickreg_click", "1", { path: '/' })
      }else{
        $.cookie("quickreg_click", parseInt($.cookie("quickreg_click")) + 1, { path: '/' });
      }
    }
  }

  
  //flash messages
  function refreshFlashMessages(){
    $("#flash_messages").load("/flash_messages");
  }
  refreshFlashMessages();
  
  // Login box (reset)
  function setLoginBoxBehavior(){
    $("#header_login_username").click(function() {
        if (this.value == 'Nombre de Usuario') {
            $("#header_login_username").attr("value", "");
        }
    }); 
    $("#header_login_username").blur(function() {
        if (this.value == '') {
            $("#header_login_username").attr("value", "Nombre de Usuario");
        }
    }); 
    $("#header_login_password").click(function() {
        if (this.value == '**********') {
            $("#header_login_password").attr("value", "");
        }
    }); 
    $("#header_login_password").blur(function() {
        if (this.value == '') {
            $("#header_login_password").attr("value", "**********");
        }
    });    
  }

  // Login box (cache)
  function refreshLoginStatus(){
    $("#login_status").load("/sesiones/status", function() {
         setLoginBoxBehavior();
    });
  }
  refreshLoginStatus();
  
  //current poll
  function refreshCurrentPoll(){
    $("#current_poll").load("/encuesta/current");
  }
  refreshCurrentPoll();
  
  //ad variables bar
  function refreshAdVariablesBar(){
    $("#ad_variables_bar").load("/ad_variables");
  }
  refreshAdVariablesBar();
  

    $("#s1").cycle({ 
        fx:    'fade', 
        speed: 3000,
        ramdom: 1,
        pause: 1
    });
    
    $("a.article_alert").click(function() {
        $("#article_warning").slideToggle();
    });

    // This will handle the left expand/collapse navigation menu
    $("#expando > ul > li > a.toggle").click(function() {
        if ($(this).hasClass("expando_collapse")) { 
            $(this).parent().next().slideUp();
            $(this).parent().parent().find("ul").slideUp();
            $(this).removeClass("expando_collapse");
            $(this).addClass("expando_expand");
        }
        else {
            $(this).parent().next().slideDown();
            $(this).parent().parent().find("ul").slideDown();
            $(this).addClass("expando_collapse");
            $(this).removeClass("expando_expand");
        }
    });
    
    function hideMenu(the_link) {
        the_link.parent().next().slideUp();
        the_link.parent().parent().find("ul").slideUp();
        the_link.removeClass("expando_collapse");
        the_link.addClass("expando_expand");
    }
    
    function getLinkFor(the_class) {
        return $("#expando > ul > ." + the_class).next().children("a.toggle");
    }
    
    function permalinkInURL(permalink, url) {
        var re = new RegExp("^\/" + permalink + "\/?");
        return url.match(re);
    }
    
    function hideIfNotCurrent(permalink) {
        current_url = window.location.pathname;
        if (permalinkInURL(permalink, current_url)) {
            return false;
        }
        the_link = getLinkFor(permalink);
        hideMenu(the_link);
    }
    
    hideIfNotCurrent("herramientas");
    hideIfNotCurrent("infertilidad");
    hideIfNotCurrent("planeando");
    hideIfNotCurrent("esperando");
    hideIfNotCurrent("criando");
    hideIfNotCurrent("para-papas");
    hideIfNotCurrent("salud-familiar");
    hideIfNotCurrent("recetas");
    hideIfNotCurrent("productos");
    
    
    
  // $(function(){
  //  $(".popupwindow").popupwindow(profiles);
  // });
    
    $(".submit_btn").click(function() {
        $(".submit_btn").fadeOut();
    });
    

    $(".submit_btn2").click(function formatUpload() {
        
        var isValid = true;

        if ($("#name").val().length == 0) isValid = false;
        if ($("#email").val().length == 0) isValid = false;
        if ($("#city").val().length == 0) isValid = false;   
        if ($("#File1").val().length == 0) isValid = false;      
        if ($("input[@name=contest]:checked").length == 0)  isValid = false;

        if ($("input[@name=terms_accepted]:checked").length == 0){
          alert("Necesitas aceptar los términos y las condiciones antes de enviar el formulario.");
          return false;
        }
        else if (isValid == true)
        {
            var detailsText = "";
            detailsText += "contest: " + $("input[@name=contest]:checked").val() + "\n"
            detailsText += "name: " + $("#name").val() + "\n";
            detailsText += "email: " + $("#email").val() + "\n";
            detailsText += "phone: " + $("#phone").val() + "\n";
            detailsText += "city: " + $("#city").val() + "\n";
            detailsText += "terms: accepted \n";              
            detailsText += "message: " + $("#message").val();

          $("#details").val(detailsText);
          $("#submit_button").hide("slow")

          startUploadProgress();
          return true;
        }
        else
        {
            alert("Por favor rellena todos los campos requeridos del formulario.");
            return false;
        }
    });

    // new_bebe_sonriente form

    $(".submit_btn3").click(function formatUpload() {
        
        var isValid = true;

        if ($("#name").val().length == 0) isValid = false;
        if ($("#email").val().length == 0) isValid = false;
        if ($("#city").val().length == 0) isValid = false;   
        if ($("#File1").val().length == 0) isValid = false;      

        if ($("input[@name=terms_accepted]:checked").length == 0){
          alert("Necesitas aceptar los términos y las condiciones antes de enviar el formulario.");
          return false;
        }
        else if (isValid == true)
        {
            var detailsText = "";
            detailsText += "name: " + $("#name").val() + "\n";
            detailsText += "email: " + $("#email").val() + "\n";
            detailsText += "phone: " + $("#phone").val() + "\n";
            detailsText += "city: " + $("#city").val() + "\n";
            detailsText += "terms: accepted \n";              
            detailsText += "message: " + $("#message").val();

            $("#details").val(detailsText);
            $("#submit_button").hide("slow")

            startUploadProgress();
            return true;
        }
        else
        {
          alert("Por favor rellena todos los campos requeridos del formulario.");
          return false;
        }
    });

});


