// JavaScript Document
$(document).ready(function(){

	//$(".propertyalerts:first").addClass("active");
	//$(".propertyalertstxt:not(:first)").hide();
	
	$(".propertyalertstxt").hide();
	$(".propertyalertstxt2").show();

	$(".propertyalerts").click(function(){

	  $(this).next(".propertyalertstxt").slideToggle("slow")
	  .siblings(".propertyalertstxt:visible").slideUp("slow");
	  $(this).toggleClass("active");
	  $(this).siblings(".propertyalerts").removeClass("active");

	});
	
	$(".propertyalerts2").click(function(){
	  $(this).next(".propertyalertstxt2").slideToggle("slow")
	  //.siblings(".propertyalertstxt:visible").slideUp("slow");
	  //$(this).toggleClass("active");
	  //$(this).siblings(".propertyalerts").removeClass("active");
	});
				
				
	function markActiveLink() {

    //Look through all the links in the sidebar
   $("div#navigationbar a").filter(function() {

      //Take the current URL and split it into chunks at each slash
      var currentURL = window.location.toString().split("/");

      //return true if the bit after the last slash is the current page name
      return $(this).attr("href") == currentURL[currentURL.length-1];

    //when the filter function is done, you're left with the links that match.
    }).addClass("active");

   //Afterwards, look back through the links. If none of them were marked,
   //mark your default one.
   if($("div#navigationbar a").hasClass("active") == false) {
      $("div#navigationbar li:nth-child(1) a").addClass("active");
    }
 }

markActiveLink();

$(".tooltips").hover(
	        function() { $(".tooltip-popup").stop().fadeIn('slow');},
			
			function() { $(".tooltip-popup").stop().fadeOut('slow', function(){
				$(this).contents("span:last-child").css({display: "none"});	
			});
});

var tip;
	$(".tip_trigger").hover(function(){

		//Caching the tooltip and removing it from container; then appending it to the body
		tip = $(this).find('.tip').remove();
		$('body').append(tip);

		tip.fadeIn('fast'); //Show tooltip

	}, function() {

		tip.hide().remove(); //Hide and remove tooltip appended to the body
		$(this).append(tip); //Return the tooltip to its original position

	}).mousemove(function(e) {
	//console.log(e.pageX)
		  var mousex = e.pageX + 10; //Get X coodrinates
		  var mousey = e.pageY + 10; //Get Y coordinates
		  var tipWidth = tip.width(); //Find width of tooltip
		  var tipHeight = tip.height(); //Find height of tooltip

		 //Distance of element from the right edge of viewport
		  var tipVisX = $(window).width() - (mousex + tipWidth);
		  var tipVisY = $(window).height() - (mousey + tipHeight);

		if ( tipVisX < 10 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 10;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} if ( tipVisY < 10 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 10;
			tip.css({  top: mousey, left: mousex });
		} else {
			tip.css({  top: mousey, left: mousex });
		}
	});
//$("a#newsimg").fancybox({
		//'titleShow'		: false
//});
});
