// -------- start of jQuery document.ready() ---------- //
SetupNotifications = function () {
  $("#msgclose").click(function (e) {
    e.preventDefault();
    CloseMsg();
  });
};
CloseMsg = function () {
    $('div.errormessages').slideUp();
    $("#msgtext").val("");
};
DisplayMsg = function (sMsg) {
  if (sMsg!="") {
    $("#msgtext").append(sMsg);
    $('div.errormessages').slideDown();
  }
};
ClearMsg = function () {
	$("#msgtext").html("");
}

$(document).ready(function() {
	
  $(".jbutton").button();

  //create rollover effect for logo
  if ($("#logo_image").length>0){
	$("#logo_image").hover(
		function(){
			this.src = this.src.replace("-off","-on");
		},function(){
			this.src = this.src.replace("-on","-off");
		});
  }
  
   
  //adding ID menu for compatibility
  $("menu").attr({id: "menu"});
  
  //on click remove value from search input
    $("#header fieldset input:text").click(function() {
    $("#header fieldset input:text").attr({value: ""});
    });
    $(".subscription fieldset input:text").click(function() {
    $(".subscription fieldset input:text").attr({value: ""});
    });
  
  //special graphics

  var dwidth = $(document.body).innerWidth()-300;
  var dheight = $(document.body).innerHeight();
  var inmotion = false;

  // land attachments
  $("#header .inside").append('<span class="balloon"></span> <span class="opera-eifel"></span>');
  $("#main .inside").append('<div id="special"><span class="pisa-jesus"></span> <span class="pyramids"></span> <span class="sfinx"></span> <span class="safari"></span> <span class="elephants"></span></div>');
  
  // sea attachments
  /*
  function animateSeaElement(element, h, w) {
	hstr = h+"px";
	wstr = w+"px";
	time_to_move = Math.round(Math.random()*10000);
	$("span."+element).animate({ 
		left: wstr,
		top: hstr
	}, 25000+time_to_move, function(){
		if (element=="boat") {
			goto_height = 435 + Math.round(Math.random()*30);
		} else {
			goto_height = (Math.round((Math.random()*(($(document).height())-$("."+element).height()))+1));
			if (goto_height<435) {
				goto_height = 435;
			}
		}
		goto_width = (Math.round((Math.random()*($(window).width()+$("."+element).width()))+1));
		goto_width = goto_width - $("."+element).width();
		animateSeaElement (element, goto_height, goto_width);
	});
  };
  var aSeaElements = ["boat", "diver1", "diver2", "diver3", "divers", "hammerhead", "humpback", "manta", "turtle", "whaleshark"];
  var i=0;
  for (i=0;i<=5;i++)
  {
	  random_sea_element = Math.floor(Math.random()*aSeaElements.length);
	  $("body").append("<span class='"+aSeaElements[random_sea_element]+" hidden_surprise'></span>");
	  if (aSeaElements[random_sea_element]=="boat") {
			start_height = 435 + Math.round(Math.random()*30);
	  } else {
	  	start_height = (Math.round((Math.random()*($(document).height()-$("."+aSeaElements[random_sea_element]).height()))+1));
		  if (start_height<435) {
			start_height = 435;
		  }
	  }
	  start_width = (Math.round((Math.random()*($(window).width()+$("."+aSeaElements[random_sea_element]).width()))+1));
	  start_width = start_width - $("."+aSeaElements[random_sea_element]).width();
	  $("span."+aSeaElements[random_sea_element]).css("top", start_height+"px");
	  $("span."+aSeaElements[random_sea_element]).css("left", start_width+"px");
	  $("span."+aSeaElements[random_sea_element]).show();
	  if (aSeaElements[random_sea_element]=="boat") {
			goto_height = 435 + Math.round(Math.random()*30);
	  } else {
		  goto_height = (Math.round((Math.random()*($(document).height()-$("."+aSeaElements[random_sea_element]).height()))+1));
		  if (goto_height<435) {
			goto_height = 435;
		  }
	  }
	  goto_width = (Math.round((Math.random()*($(window).width()+$("."+aSeaElements[random_sea_element]).width()))+1));
	  goto_width = goto_width - $("."+aSeaElements[random_sea_element]).width();
	  animateSeaElement(aSeaElements[random_sea_element],goto_height,goto_width);
  }
  
*/

  //find all holders of a.more link and add class right to them
  $("p:has('a.more')").addClass("right");
  
  //finding &raquo; inside of a.more links and wrapping it inside <span></span>
  $("a.more").append(' <span>&raquo;</span>');
  
  //removing background from the last li in footer
  $("#footer li:last").css({background: 'none', paddingRight: '0'});  
  
  //var mouseX = mouseY = 0; 
  //$().mousemove(function(e) { mouseX = e.pageX; mouseY = e.pageY; });

  function animateballoon (h,w) {
	hstr = h+"px";
	wstr = w+"px";
	$("#header span.balloon").animate({ 
	        left: wstr,
	        top: hstr
	      }, 15000, function(){
			goto_height = (Math.round((Math.random()*dheight)+1));
			goto_width = (Math.round((Math.random()*dwidth)+1));
			//goto_width=mouseX;
			//goto_height=mouseY;
			//alert ("move to:"+goto_width+","+goto_height);
			animateballoon (goto_height,goto_width);
			});
  }

  $("#header span.balloon").click(function(){
	if (inmotion==false){
	
	goto_height = (Math.round((Math.random()*dheight)+1));
	goto_width = (Math.round((Math.random()*dwidth)+1));
	animateballoon (goto_height,goto_width);
			
	inmotion=true;
	}
	});
	
});
// -------- end of jQuery document.ready() ---------- //


// -------- start search form support --------------- //

function searchsubmit() {
	if (document.forms[0].searchtype.checked==true) {
		strsearchtype = "users";
	}
    else {
      	strsearchtype = "editorial";
    }

	if (document.forms[0].search_term.value!=null && document.forms[0].search_term.value!=""){
		window.location = "/search/"+strsearchtype+"/"+escape(document.forms[0].search_term.value.replace(/ /g,"+"))+"/1.html";
	}
	else {
		window.location = "/latest-experiences";
	}
}

// -------- end search form support ---------------- //

function doLogin() {
	FB.login(function(response) {
		if (response) { // ignore response.perms
			var uid = response.authResponse.userID;
			var accessToken = response.authResponse.accessToken;
	  	  	FB.api({
				method: 'fql.query',
				query: 'SELECT name FROM profile WHERE id=' + uid
			}, function(response) {
				window.location = "/users/link_user_accounts?id="+uid+"&name="+response[0].name;
			});
		} else {
		  // user is not logged in
		  DisplayMsg("Sorry, your login has failed.");
		}
	}, {});
}
