$(document).ready(function () {
  // block elements
  $("body>div:nth-child(2)").attr("id", "container");
  //$container.first("div").attr("id","header");
  $("div#container>div:nth-child(1)").attr("id","header");
  $("div#container>div:nth-child(2)").attr("id","main");
  $("div#container>div:nth-child(3)").attr("id","footer");
  //$("div#main>div:nth-child(1)").attr("id","sidenav");
  //$("div#main>div:nth-child(2)").attr("id","sidebar");
  //$("div#main>div:nth-child(3)").attr("id","content");
  $("div#main>div:eq(0)").attr("id","sidenav");
  $("div#main>div:eq(1)").attr("id","sidebar");
  $("div#main>div:eq(2)").attr("id","content");
  // components
  $("div#header>ul:first-child").attr("id","user");
  $("div#header>ul:nth-child(3)").attr("id","navbar");
  $("div#footer>ul:first-child").attr("id","subnav");
  // classes - bits and bobs
  $("div#sidebar>div").addClass("box");
  $("body>p:first-of-type").addClass("hide");
  //$("body>div:last-child").addClass("modal");
  //$("div#container").after('<div id="overlay">&nbsp;</div>');
  $("div#content>img[src$='image-unit']:first-of-type").addClass("hide");
  $("a[href^='/documents']").parent('p').addClass("download");
  $.each($("ul.legend").find("li"), function(i, element) {
     $(element).addClass("el-" + i);
  });
  // set body id and class
  var id = window.location.pathname.split("/")[1];
  var pathname = window.location.pathname;
  //var bodyClass = pathname.replace(/\//g, " ");
  var bodyClass = window.location.pathname.split("/")[2];
  var selectedURI = window.location.pathname;
  // set id
  $("body").attr("id",id);
  if ( $("body").attr("id") == "") {
      $("body").attr("id","home");
  }
  // set classes
  $("body").addClass(bodyClass);
  if ( $("body").attr("class") == "") {
      $("body").addClass("home");
  }
  // set lang
	var langClass = getCookie('lang');
  if (getCookie('lang')) {
    $("body").addClass(langClass + "_CA");
  }
  // set selected elements
  $('li a[href$="/' + id +'"]').parent('li').addClass('selected');
  $('li a[href="' + selectedURI +'"]').parent('li').addClass('selected');
  // smooth scroll to top of page
  $("div#content p a[href='#']").click(function() {  
    $('html, body').animate({scrollTop:0},200);
        return false;
  });
  // style back to top links
  $("div#content p a[href='#']").addClass('top');
  // draggable modal window
  $("div.modal").draggable();
  $("div.modal h4 a[href='#']").click(function() {
    $("#overlay").fadeOut('fast');
    $(this).parents("div.modal").hide("highlight",{},250);
  });
  // modal buttons
  $("div.modal button.cancel").click(function() {
    $(this).parents("div.modal").fadeOut('fast');
    $("#overlay").fadeOut('fast');
  });
  $("div.modal").prepend('<span>&nbsp;</span>');
  $("div.modal span").click(function() {
    $("div.modal").fadeOut('fast');
    $("#overlay").fadeOut('fast');
  });
  // entry screen
  $("div.modal.accept_terms a[href^='http']").click(function() {
    $this.parents("div.modal").fadeOut('fast');//break bubble
  });
  $("div.modal.accept_terms a[href='#']").click(function() {
    $(this).parents("div.modal").fadeOut('slow');
    $("div#overlay").fadeOut('fast');
  });
  // callouts
  $("body:not(#home) blockquote").prepend('<span>&nbsp;</span>');
  $("body:not(#home) blockquote").click(function() {
    $(this).children("*:last-child").toggle('fast');
    $(this).toggleClass("open");
  });
  // page mailer
	$("a[href*='pageMailer']").click(function() {
      $("div[id='overlay']").fadeIn('fast');
			$("div[id='page_mailer']").fadeIn('fast');
		  $("div[id='page_mailer'] form").removeClass('hide');
   });
  //$("div#page_mailer form").validate();
  $("div#page_mailer form").validate({
    submitHandler: function(form) {
		$("div#page_mailer form").addClass('hide');
		//$("div[id='page_mailer'] fieldset").toggleClass('hide');
    $("div[id='page_mailer'] p.confirmation")
      .fadeIn('slow')
      .animate({opacity: 1.0}, 2500)
      .fadeOut('slow', function() {
          $(this).parents("div.modal").fadeOut('fast');
          $("div[id='overlay']").fadeOut('fast');
       });
      $.post("/ajax/page_mailer.php", $("#page_mailer form").serialize());
      $("input[id='your_friends_email_address']").each(function() {
        $(this).val("");
      });
     }
  });
  // external links
	$("a[href^='http']:not(a[href*='bayer'])").click(function(){
		var cookie = getCookie('extLink');
		if(!cookie) {
      //$("#overlay").fadeIn('slow');
      //$("#overlay").show('scale','center','fast');
      $("div[id='overlay']").fadeIn('fast');
			$("[class*='confirm_link']").fadeIn('fast');
			var externalUrl = this.href;
			$("[class*='confirm_link'][button[class='continue']").click(function(){
				window.open(externalUrl);
			});
			return false;
		}
		return true;
	});
  // phone formatting
	var globalFns = {};
	globalFns.formatPhone = function() {
		var $element = $(this);
		var input = $element.val().replace(/[^0-9]/g, "");
		var output = "";
		for (var i=0; i<input.length; i++) {
			if (0==i) output+='(';
			output+=input.charAt(i);
			if (2==i) output+=') ';
			if (5==i) output+='-';
		}
		output = output.replace(/[^0-9]*$/, '');
		if (output.length >= 14) {
			output = output.substring(0,14);
		}
		$element.val(output);
	};
	$(".phone").keyup(globalFns.formatPhone);
  
  // custom function to allow toggle/fade
  jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
  }; 
});// end jQuery

// cookie setter
function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
// cookie getter
function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function pageMailer() {
  //return false;
}
