/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
;
(function ($) { $(document).ready(function(){


  //Search box click
  $("#edit-search-block-form--2").focus(function(){
    if($(this).val() == "Search") {
      $(this).val("");
    };
  })
  .blur(function(){
    if($(this).val() == "") {
      $(this).val("Search");
    };
  });


  $("#login-link").click(function(e){
    e.preventDefault();
    $(".region-header-menu #block-user-login").toggle();
  });
  
  
  $(".not-logged-in #block-menu-menu-super-menu li:first a[href*=\"forum\"]").click(function(e){
    e.preventDefault();
    $("#toboggan-login").slideToggle(200);
  });


  //Show login popup if an error
  //$("#toboggan-login input.error").parents("#toboggan-login").show();

  $('#toboggan-login input, #user-login input').keypress(function(e) {
    if(e.which == 13) {
      $("input.form-submit").mousedown();
    }
  });
  
  
  //user registration terms and conditions
  $(".form-item-legal-accept a").colorbox({ href: '/legal #content', innerWidth: '800px', innerHeight: '480px', onOpen: function(e){
    //alert($(this).attr("href")+" .region-content");
  }});
  
  
  
  
  //video popups
  $("a.colorboxvideo").colorbox({ innerWidth: '800px', innerHeight: '480px', iframe: true});


  //image map map of Australia
  $("map area").mouseover(function(){   
    $("#image-map").attr("src", ($("#image-map").attr("src")).substr(0, ($("#image-map").attr("src")).lastIndexOf("/")) +  "/map-" + $(this).attr("id") + ".png");
    
  })
  .mouseout(function(){
    $("#image-map").attr("src", ($("#image-map").attr("src")).substr(0, ($("#image-map").attr("src")).lastIndexOf("/")) +  "/map.png");
  });



  //IVF process map
  $("map#process area").click(function(e){
    e.preventDefault();
    var step = $(this).attr("id");
    $("." + step).each(function(){
      $.colorbox({width: "520px", opacity: "0.75", html: '<div class="prose">' + $("." + step).html() + '</div>' });
    });
  });
  






  //disable mobile css if cookie
  if($.cookie("nomobile") == 1) {
    $("head style:contains('mobile.css')").remove();
    $('head link[media*="640px"]').remove();
  }
  
  $("a#view-full-site").click(function(e){
    e.preventDefault(); 
    $.noMobile();
  });
  
  //Video popups
  $("a.video").each(function(){
    var video = $(this).parents(".node").find(".video-popup").html();
    $(this).colorbox({ html : video });
  });
  
  
  
  //contact form dynamics
  $(".salesforce-form.contact-form #preferred-location").change(function(){
    if($(this).attr("checked")){
      $(".preferred-location").show();
    } else {
      $(".preferred-location").hide();
    }
  })
  .change();
  
  $(".salesforce-form.contact-form #preferred-specialist").change(function(){
    if($(this).attr("checked")){
      $(".preferred-specialist").show();
    } else {
      $(".preferred-specialist").hide();
    }
  })
  .change();
  
  
  $(".salesforce-form.contact-form #00N900000027RyR").change(function(){
    if($(this).find("option:selected").text() == 'Donor Program'){
      $(".donor-program").show();
      $("#00N900000027Ryv").val("Donor");
      $(".surrogacy-checkbox").show();
      $(".how-long-trying").hide();
    } else {
      $(".donor-program").hide();
      $("#00N900000027Ryv").val("");
      $(".surrogacy-checkbox").hide();
      $(".how-long-trying").show();
    }
    
    if($(this).val() == 'Booking a Consultation'){
      $(".preferred-specialist-checkbox").show();
      $(".preferred-location-checkbox").show();
    } else {
      $("#preferred-location").attr("checked", false).change();
      $("#preferred-specialist").attr("checked", false).change();
      $(".preferred-specialist-checkbox").hide();
      $(".preferred-location-checkbox").hide();
    }
    
    if($(this).val() == 'International Patient'){
      $(".country").show();
      $(".medicare-card").show();
    } else {
      $(".country").hide();
      $(".medicare-card").hide()
    }
    
    
  })
  .change();
  
  //seminar form dynamics
    $(".salesforce-form.seminar-form #preferred-specialist, .salesforce-form.seminar-form #00N900000027YQk").change(function(){
    if($(this).attr("checked") || $("#00N900000027YQk").attr("checked")){
      $(".preferred-specialist").show();
    } else {
      $(".preferred-specialist").hide();
    }
  })
  .change();

  
  //Standalone register events form
  $(".salesforce-form.register-form .treatment select").change(function(){
    if($(this).val() == 'other'){
      $(".treatment-other").show();
      $(".treatment-other .form-text").val("");
    } else {
      $(".treatment-other").hide();
      $(".treatment-other .form-text").val( $(this).val() );
    }
  })
  .change();
  
    $(".salesforce-form.register-form .stage select").change(function(){
    if($(this).val() == 'other'){
      $(".stage-other").show();
      $(".stage-other .form-text").val("");
    } else {
      $(".stage-other").hide();
      $(".stage-other .form-text").val( $(this).val() );
    }
  })
  .change();
  
  
  
  
  
  
  //Salesforce form validation
  $("form.salesforce-form").submit(function(e){
    var msg = "";
    $(this).find(".form-item.required:visible").each(function(){
      if($(this).find("input.form-text, select").val() == ""){
        msg += ($(this).find("label").text()).replace(/\*?\:/gi, "") + " is required.\n";
      }
    });
    
    if(msg != ''){
      e.preventDefault();
      alert(msg);
    }
  });
  
  
  
  



}); 


  //Mobile site cookies
  $.noMobile = function(){
    $.cookie("nomobile", 1);
    $("head style:contains('mobile.css')").remove();
    $('head link[media*="640px"]').remove();
  }
  
  $.goMobile = function() {
    $.cookie("nomobile", null);
    location.reload(true);
  }



})(jQuery);;

