/* 
 * GLOBAL TOOLS AND UTILITIES
 * Place all custom scripts here.
 *
*/
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\\

   
$(function () {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
    var time = 100;
    var hideDelay = 200;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -100,
          left: -33,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

( function($) {
	$.fn.ClearText = function() {
		return this.each( function() {
			var defaultValue = $( this ).val();
			$( this ).focus( function() {
				if ( $( this ).val() === defaultValue ) {
					$( this ).val( "" );
				}
			});
			$( this ).blur( function() {
				if ( $( this ).val() === "" ) {
					$( this ).val( defaultValue );
				}
			});
		});
	};
	
})(jQuery);

/* MAKE IT SAFE TO USE console.log ALWAYS */
/*window.log = function() {
	log.history = log.history || [];
	log.history.push( arguments );
	arguments.callee = arguments.callee.caller;
	if ( this.console ) console.log( Array.prototype.slice.call( arguments ) );
};

( function(b) {
	function c() {}
	for ( var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)
		b[a]=b[a] || c;
});
(window.console = window.console || {});

//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-//

var engine = { 
    utils : { 
	   function1 : function() { 
	   }
	}
}*/
/* Initialize and/or execute jQuery Scripts here */
jQuery(document).ready( function() {
	
	 var popOut = "#popout"; // Name of the popout container.
   var adBox = "#adbox"; // Name of the animated bit of the ad.
   var adWidth = $(adBox).width() + $("#cap").width(); // Width of the ad container.
   

   function openAd() {
      $(popOut).width(adWidth+"px");
      $(adBox).stop().animate({marginLeft: "0"},1200)
     
   }
   
   function closeAd() {
      $(adBox).stop().animate({marginLeft: "-"+adWidth+"px"},1200,"linear",
         function(){ $(popOut).width($("#cap").width() + "px"); }
      );
     
   }

   $("#popout").mouseenter(function() {
             openAd();
    
      return false;
   });
   $("#popout").mouseleave(function() {
      closeAd();
      return false;
   });   
   
    function fixBreadcrumbs() {
		var html = $('.breadcrumbs').html();
			
		var newHTML = html.replace(/You are:/g,'You are: <a href="/index.htm">HOME</a> > <a href="/latest-news.htm">LATEST NEWS</a> > ');
			
		$('.breadcrumbs').html(newHTML);
			//$('<span>Test ></span>').prependTo('.bread-style');
			
	};
 
	// store url for current page as global variable
	current_page = document.location.href
 
	// apply selected states depending on current page
	if (current_page.match(/index/)) {
		$("ul#siteInfoSelected li:eq(0)").addClass('selected');
	} else if (current_page.match(/about-us/)) {
		$("ul#siteInfoSelected li:eq(1)").addClass('selected');
	} else if (current_page.match(/latest-news/)) {
		$("ul#siteInfoSelected li:eq(2)").addClass('selected');
	} else if (current_page.match(/testimonials/)) {
		$("ul#siteInfoSelected li:eq(3)").addClass('selected');
	} else if (current_page.match(/our-fleet/)) {
		$("ul#siteInfoSelected li:eq(4)").addClass('selected');
	} else if (current_page.match(/faq/)) {
		$("ul#siteInfoSelected li:eq(5)").addClass('selected');		
	} else if (current_page.match(/contact-us/)) {
		$("ul#siteInfoSelected li:eq(6)").addClass('selected');
	} 
	else { // don't mark any nav links as selected
		$("ul#siteInfoSelected li").removeClass('selected');
	};
	
	if (current_page.match(/business-travel/)) {
		$("ul#ourServicesSelected li:eq(0)").addClass('selected');
	} else if (current_page.match(/corporate-hospitality/)) {
		$("ul#ourServicesSelected li:eq(1)").addClass('selected');
	} else if (current_page.match(/private-hire/)) {
		$("ul#ourServicesSelected li:eq(2)").addClass('selected');
	} else if (current_page.match(/sporting-events/)) {
		$("ul#ourServicesSelected li:eq(3)").addClass('selected');
	} else if (current_page.match(/outdoor-events/)) {
		$("ul#ourServicesSelected li:eq(4)").addClass('selected');
	} else if (current_page.match(/helicopter-management/)) {
		$("ul#ourServicesSelected li:eq(5)").addClass('selected');
	} else if (current_page.match(/special-occasions/)) {
		$("ul#ourServicesSelected li:eq(6)").addClass('selected');
	} else if (current_page.match(/aerial-filming/)) {
		$("ul#ourServicesSelected li:eq(7)").addClass('selected');
	} else { // don't mark any nav links as selected
		$("ul#ourServicesSelected li").removeClass('selected');
	};

	if (current_page.match(/latest-news\//)) {
		fixBreadcrumbs();
	};
	
	$("input:text").ClearText();
	var getMenu = $('ul#nav_1215078 li.selected ul').html();
	$('#put-sidemenu').append(getMenu);
	//$('.bread-style a:eq(1), .bread-style a:eq(2), .bread-style a:eq(3)').wrap('<span></span>');
	//$('.bread-style label').contents().filter(function(){ return this.nodeType != 1; }).wrap('<span></span>');
	//$('.bread-style span').each(function(i) {
      // var getSpan =  $.trim($('.bread-style span:eq('+i+')').text());
       //if( getSpan == '' ){ $('.bread-style span:eq('+i+')').remove() }
    //});
	//$('.midwrap h2#menu-name').html($('#nav_1215078 li.selected a:first').text());
	//if( $('.bread-style a').length > 2 ) {
	  // var f = $('.bread-style label');
       //f.html(f.html().replace(/:/g, ""));   	
	//}
	
	if($('.breadcrumbs').text() != '')
	{
	
	var html = $('.breadcrumbs').html();
			
			var newHTML = html.replace(/&gt;/g,'<img src="/images/breadcrumbs-current-bg.png" alt=">" style="vertical-align:middle; margin: -3px 8px 0 6px">');
			
			$('.breadcrumbs').html(newHTML);	
	}
 

	
	 //$('#put-sidemenu > li').mouseover(function() {
		// if($("#put-sidemenu > li ul").queue()==0){
        	//if ($(this).attr("class") != "selected active"){
            	//if ($(this).attr("class") != "active"){
                	//$("#put-sidemenu > li ul").slideUp(300);
                	//$(this).children("ul").slideToggle(1200);
                	//$("#put-sidemenu > li").removeClass("active");
                	//$(this).addClass("active");
            //}
        //}
		 //}
    //})


});

jQuery(window).load( function() {
	
	
if($('.latest-news').text() != '')
{
	var o1 = $('.latest-news').offset();
	var o2 = $('#footer').offset();
	var dy = Math.abs(o1.top - o2.top) - $('.latest-news').height();
	
	var numOfTweets = Math.floor((dy-60)/90);
	
	if(dy >= 165){
		
		var html = '<div class="twitter" style="margin-top:12px;"><div class="top"></div><div class="mid"><div class="midwrap"><h2><a href="/latest-news.htm">LATEST TWEETS</a></h2><div class="tweets"></div></div></div><div class="bot"></div><div></div>';
		
		$('.latest-news').append(html);
		
		  // Declare variables to hold twitter API url and user name
	  	var twitter_api_url = 'http://search.twitter.com/search.json';
	 	var twitter_user    = 'atlashelis';
	 
	  	// Enable caching
	  	$.ajaxSetup({ cache: true });
	 
	  	// Send JSON request
	  	// The returned JSON object will have a property called "results" where we find
	  	// a list of the tweets matching our request query
	  	$.getJSON(
		twitter_api_url + '?callback=?&rpp='+numOfTweets+'&q=from:' + twitter_user,
		function(data) {			
	
		  $.each(data.results, function(i, tweet) {
			// Uncomment line below to show tweet data in Fire Bug console
			// Very helpful to find out what is available in the tweet objects
			//console.log(tweet);
			// Before we continue we check that we got data
				if(tweet.text !== undefined) {		  
			  	// Calculate how many hours ago was the tweet posted
			  	var date_tweet = new Date(tweet.created_at);
			  	var date_now   = new Date();
			  	var date_diff  = date_now - date_tweet;
			  
			  	if(Math.round(date_diff/(1000*60*60) >= 24))
			  	{
				  hours =	Math.round(date_diff/(1000*60*60*24));
				  if(hours == 1)
				  {
					  hours += " day ago";
				  }
				  else
				  {
					  hours += " days ago";
				  }
			  	}
			  	else
			  	{
				  if(Math.round(date_diff/(1000*60*60) < 1))
				  {
					  hours = Math.round(date_diff/(1000*60));
					  
					  if(hours == 1)
					  {
						  hours += " minute ago";
					  }
					  else
					  {
						  hours += " minutes ago";
					  }
				  }
				  else
				  {
					  hours = Math.round(date_diff/(1000*60*60));
					  
					  if(hours == 1)
					  {
						  hours += " hour ago";
					  }
					  else
					  {
						  hours += " hours ago";
					  }
				  }
			  	}
	 
			  	// Build the html string for the current tweet
			  	var tweet_html = '<div class="tweet_text" >';
			  	tweet_html    += '<a style="color:#2B7BB9;" href="http://www.twitter.com/';
			  	tweet_html    += twitter_user + '/status/' + tweet.id_str + '" target="_blank">';
			  	tweet_html    += tweet.text + '<\/a><\/div>';
			  	tweet_html    += '<div class="tweet_hours" style="border-bottom:1px dashed #c5ddeb; margin-bottom:8px; padding-bottom:8px;">' + hours;
			  	tweet_html    += ' <\/div>';
	 
			  	// Append html string to tweet_container div
			  	$('.tweets').append(tweet_html);
			}
			else{ var tweet_html = '<div class="tweet_text">';
			  tweet_html    += 'No Tweets Found</div>';
			  $('.tweets').append(tweet_html);}
		  });
		
		}
		
		
	 );
		
	}
	
	}
	
	
})


     
        

