/**
 * Banner Hand Left
 * 
 * Script works by using CSS attributes
 * 	- Make sure to properly configure the prepare function's CSS
 * The close option sets a cookie to prevent the banner from being displayed
 * 	- jquery.cookie is used for setting the cookie so it is required unless you remove the cookie checks
 * 
 * @author Jeremy Simkins son9ne [at] gmail [dot] com
 */
jQuery.fn.positionLeftBanner = function () {
	var tmpTop = $(window).scrollTop();
	var tmpTop2 = tmpTop + this.height();
	if ($(window).height() > tmpTop2) {
		if ($(window).height() > (tmpTop2 + 300)) {
			tmpTop +=300;
		}
	}				
	
	this.css('top', tmpTop + 'px');
    return this;
};
jQuery.fn.prepareBannerHandLeft = function () {
    this.css('display','none');
    this.css('height','156px');
    this.css('width','598px');
    this.css('background','url("http://www.ehrscope.com/img/banners/banner-hand-left.png") no-repeat scroll 0 0 transparent');
    this.css('position','absolute');
    this.css('z-index','999');
    this.css('left', - this.width()+'px');
    this.positionLeftBanner();
    return this;
};
$(document).ready(function(){
	// Append the banner
	$('body').append(
						'<div id="banner-hand-left">'+
							'<a href="#" id="banner-hand-left-close" title="Close" style="position:absolute; top:40px; left:130px; width:10px; height:10px;"></a>'+
							'<a href="http://www.emrconsultant.com/?ref=hbl" title="Doctors - Click here now!" style="position:absolute; top:34px; left:164px; width:416px; height:92px;"></a>'+
							'<a href="http://www.emrconsultant.com/?ref=hbl" title="Doctors - Click here now!" style="position:absolute; top:112px; left:212px; width:198px; height:30px;"></a>'+
						'</div>'
					);	
	// Fetch element
	var bannerHandLeft = $('#banner-hand-left');
	// Center the banner
	bannerHandLeft.prepareBannerHandLeft();
	// Define flags
	var bannerHandLeftActive=false;
	var bannerHandLeftPosition=0;
	// Check for banner cookie
	var bannerHandLeftCookie =  $.cookie('disableBannerHandLeft');
	if (!bannerHandLeftCookie) {
		// Show the banner after a delay
		setTimeout(function() {
			bannerHandLeft.fadeIn('slow', function() {
				var tmpLeftPosition = 0;
	    		$(this).animate({'left': tmpLeftPosition+'px'},500);
	    		bannerHandLeftActive=true;
	    		bannerHandLeftPosition=tmpLeftPosition;
				return false;
			});
		}, 4000);
	}
	// Close the banner
	$('#banner-hand-left-close').click(function() {
		// Force banner off page then hide
		bannerHandLeft.stop().animate({'left': - bannerHandLeft.width() + 'px'},500,function(){
		  		$(this).fadeOut();
		  });
		  $.cookie('disableBannerHandLeft', '1'); // Set banner cookie to prevent bothering the user
		  bannerHandLeftCookie = true; // Force current session to use cookie value
		  return false; // Prevent page from scrolling when animating the element to off the page
	});
	// Make banner scroll with page
	$(window).scroll(function(){
		if (bannerHandLeftActive && !bannerHandLeftCookie) {
//			var tmpTop = ($(window).scrollTop() + 300);

			var tmpTop = $(window).scrollTop();
			var tmpTop2 = tmpTop + bannerHandLeft.height();
			if ($(window).height() > tmpTop2) {
				if ($(window).height() > (tmpTop2 + 300)) {
					tmpTop +=300;
				}
			}			
			bannerHandLeft.stop().animate({'top':tmpTop+'px'},600);
			bannerHandLeftPosition = tmpTop;
		}
	});
	// Make banner position update with new window size
	$(window).resize(function(){
		if (bannerHandLeftActive && !bannerHandLeftCookie) {
			bannerHandLeft.stop().positionLeftBanner();
		}
	});		
});
