$(document).ready(function () {
  // 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");
  // }
  // $("div#main").append('<br clear="both" />');
  // set selected elements
  // $('li a[href$="/' + id +'"]').parent('li').addClass('selected');
  // $('li a[href="' + selectedURI +'"]').parent('li').addClass('selected');
  // 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);
	};
  // 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');
  // modal popups
  $("div.modal").draggable();
  // draggable modal window
  $("div.modal").draggable();
  $("div.modal h4 a").click(function() {
   // $(this).parents("div.modal").hide("highlight",{},250);
   $(this).parents("div.modal").css( "background-color", "#FFFF99" );
$(this).parents("div.modal").fadeOut('highlight');
  $("#overlay").fadeOut('fast');
  });
  // modal buttons
  $("div.modal button.cancel").click(function() {
    $(this).parents("div.modal").fadeOut('fast');
    $("#overlay").fadeOut('fast');
  });
  // page mailer
	$("a[href*='pageMailer']").click(function() {
      $("div[id='overlay']").fadeIn('fast');
			$("div[id='page_mailer']").fadeIn('fast');
		  $("div[id='page_mailer'] fieldset").removeClass('hide');
   });
  $("div#page_mailer h2").click(function() {
    $("div#page_mailer").fadeOut('fast');
    $("#overlay").fadeOut('fast');
  });
  //$("div#page_mailer form").validate();
  $("div#page_mailer form").validate({
    submitHandler: function(form) {
		$("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("/scripts/components/ajax/page_mailer.php", $("#page_mailer form").serialize());
      $("input[id='your_friends_email_address']").each(function() {
        $(this).val("");
      });
     }
  });

	$("a[href^='http']:not(a[href*='bayer'])").click(function(){
		var cookie = getCookie('extLink');
		if(!cookie) {
      //$("#overlay").fadeIn('slow');
      //$("#overlay").show('scale','center','fast');
	$("#confirm_link").css( "background-color", "#FFF" );
          $(this).parents("div.modal").show();
      $("#overlay").show();
			$("[class*='confirm_link']").fadeIn('fast');
			var externalUrl = this.href;
			$("[class*='confirm_link'][button[class='continue']").click(function(){
				window.open(externalUrl);
			});
			return false;
		}
		return true;
	});
	
  // Open external links in a window and cancel the click action
  //$("a[href^='http']").click(function(){window.open(this.href); return false;});
  // Add target=_blank to external links
  //$("a[href^='http']").attr('target','_blank');
	//var redirectMessage = "Warning: Leaving frankly.net\n\nYou are leaving frankly.net and going to an external website which is not affiliated with frankly.net or Bayer. Bayer provides these links as a service and does not endorse or accept responsibility or liability for any information presented on external websites.\n";
	//$("#container").find("a[href^='http://']:not(a[href*='.livingwithhemophilia.com'])").click(function() {
		//return confirm(redirectMessage);
	//});
	$(".phone").keyup(globalFns.formatPhone);
	
  // hide show pollster
  $("a.poll_picker").click(function() {
    $("div.last_poll").hide('fast');
    $("div.poll").hide('fast');
   });
  $("a.poll_picker.travel").click(function() {
    $("div.poll.travel").show('fast');
  });
  $("a.poll_picker.health_and_fitness").click(function() {
    $("div.poll.health_and_fitness").show('fast');
  });
  $("a.poll_picker.entertainment_and_lifestyle").click(function() {
    $("div.poll.entertainment_and_lifestyle").show('fast');
  });
  $("a.poll_picker.sex_and_relationships").click(function() {
    $("div.poll.sex_and_relationships").show('fast');
  });
	// pollbox in sidebar
	$("#poll").find("button[type='submit']").click(function(){
		var theAnswer 		= $("input[name='poll']:checked").val();
		// var thePollHeader 	= $("input[name='pollHeader']").val();
		var theLang 		= $("input[name='lang']").val();
		var thePollId 		= $("input[name='pollId']").val();
		if(theAnswer) {
			$("#poll").find("button[type='submit']").fadeOut(500);
			var pollHeight = $("#poll").height();
			$("#poll").css('height', pollHeight);
			$("#poll_form").fadeOut(500, function() {
				
				// call save
				$.getJSON("/scripts/components/poll/savepoll.php",
					{ lang:theLang, pollId:thePollId, answer:theAnswer },
					function(data) {
						if(data.success) {
							// redisplay poll
							$.get("/scripts/components/poll/pollresults.php", 
								// { lang:"en_US", pollId:thePollId, pollHeader:thePollHeader },
								{ lang:"en_US", pollId:thePollId }, 
								function(data) {
									$("#poll_form").html(data);
									$("#poll_form").fadeIn(500);
								}
							);
						}
					}
				);
				
			});
		}
	}) // end pollbox
	
	$("div.poll.questions").find("form").submit(function(){return false;});
	
	// poll page
	$("div.poll.questions").find("button[type='submit']").click(function(){
		
		var theAnswer	= $(this).parent("form").find("input[name='poll']:checked").val();
		// var thePollHeader 	= $("input[name='pollHeader']").val();
		var theLang	= $(this).parent("form").find("input[name='lang']").val();
		var thePollId	= $(this).parent("form").find("input[name='pollId']").val();

		if(theAnswer) {
			var $element = $(this).parent("form");
			$(this).parent("form").hide();
			$(this).hide("fast");
			$.getJSON("/scripts/components/poll/savepoll.php",
				{ lang:theLang, pollId:thePollId, answer:theAnswer },
				function(data) {
					if(data.success) {
						// redisplay poll
						$.get("/scripts/components/poll/pollresults.php", 
							// { lang:"en_US", pollId:thePollId, pollHeader:thePollHeader },
							{ lang:"en_US", pollId:thePollId, type:"polls" }, 
							function(data) {
								$element.parent("div").removeClass("questions").addClass("resultset");									
								$element.html(data);
								$element.fadeIn(500);
							}
						);
					}
				}
			);
			//var pollHeight = $("div.poll.questions").height();
			//$("div.poll.questions").css('height', pollHeight);
		}
	}) // end poll page

});

// 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;
}

