/*
jQuery Flying Popup, by Damian Szewczyk (netkevin)
damian.szewczyk@gmail.com
Version 1.0
Flying Popup plugin for jQuery. Allows to show floating HTML popups that follow page scroll/resize (also static HTML popups can be shown when desired). Popups can fly in from outside of the screen (from every direction)

See example usage in readme.txt or run index.html for useful creator

Settings:
name	 popup
type	 jQuery
param	 options                  hash                    object containing config options
param	 options[starttime]       int                     when to show popup (in seconds) 0 to start immediately after firing $("ELEMENT_ID").popup() plugin 
param	 options[selfclose]       int					  after what time self close popup. 0 - disable feature
param	 options[popup_div]		  string				  id of popup DIV
param	 options[overlay_div]	  string				  id of overlay DIV
param	 options[close_id]	  	  string				  HTML id of element used to close popup
param	 options[overlay]		  bool					  turn on/off overlay (true/false)
param	 options[opacity_level]	  float					  opacity level of overlay (from 0 to 1)
param	 options[overlay_cc]	  bool					  close popup on overlay click (true/false)
param	 options[centered]		  bool					  center popup vertically
param	 options[top]		  	  int					  distance from top to show popup (in px) - works only for centered=false
param	 options[setcookie]		  bool					  use cookie to prevent showing popup multiple times (true / false)
param	 options[cookie_name]	  string				  name of cookie, that is used 
param	 options[cookie_timeout]  int					  how long should cookie be stored (0 = current session)
param	 options[floating]		  bool					  turn on/off floating popup (true/false)
param	 options[floating_reaction]int				  	  floating reaction in miliseconds (how quickly start floating after scrolling/resizing the page)
param	 options[floating_speed]  int					  smaller value = higher speed (by default = 12)
param	 options[fly_in]		  bool					  turn on/off fly-in ads popup (true/false) - works only with floating popups
param	 options[fly_from]	 	  string				  if fly_in = true, can be 'top-left', 'top-right', 'left', 'right', 'top', 'bottom', 'bottom-left', 'bottom-right' ('top' by default)
param	 options[popup_appear]    string				  how popup appears "show" (default), "fadeIn", "slideDown" (has sense when fly_in = false)
param	 options[popup_appear_time]string/int			  time of appearing (may be "slow", "fast" or number in miliseconds) 0 or empty to show immediately
*/

 (function($) {
 
   $.fn.popup = function(settings) {
     var config = {
     	 starttime      	: 0,
     	 selfclose			: 0,				
		 popup_div			: 'popup',  		
		 overlay_div		: 'overlay', 		
		 close_id			: 'baner_close', 	
		 overlay			: false,			
		 opacity_level  	: 0.7,				
		 overlay_cc			: true,				
		 centered			: true,
		 top				: 130,				
		 setcookie 			: false,
		 cookie_name		: 'popup',
		 cookie_timeout 	: 0,				
		 floating			: true,
		 floating_reaction  : 700,			
		 floating_speed 	: 12,				
		 fly_in				: true,				
		 fly_from 			: 'top', 		
		 popup_appear 		: 'show',
		 popup_appear_time  : ''
	};

	if (settings) $.extend(config, settings);
	
	//variables used for floating
	var timer;					//timer to fire movePopup()
	var timer_anim = 0;			//timer to fire animatePopup()
	var goal;					//variable to set what is the vertical (top) goal to reach by floating popup
	var goal_left;				//variable to set what is the horizontal (left) goal to reach by floating popup
	var current_position;		//current 'top' position
	var current_position_left;  //current 'left' position
	var last_position;			//previous 'top' position
	var last_position_left;		//previous 'left' position
    var popup_content = '';		//content of popup 
	var popup_object;			//jQuery object
	var $popup = $('#'+config.popup_div);		//popup DIV
	var $overlay = $('#'+config.overlay_div);//overlay DIV
    this.each(function() {
	   popup_object = $(this);
	   $(this).hide();
     });

 	if (config.starttime == 0) { //start immediately after firing $("ELEMENT_ID").popup()
 		setTimeout(show_popup, 250);
 	} else { //show popup with configured amount of time
 		setTimeout(show_popup, (config.starttime * 1000));
 	}
	
	return this;
	
	//main function that shows the popup
 	function show_popup() {
		hidePopup();
		popup_content = popup_object.html();
 		if(config.setcookie) { //check for cookie if you set "setcookie" to true
			//don't show popup if cookie is set
 			if(getCookie(config.cookie_name)) return false;
 		}
		//show overlay DIV - please set CSS for it
		if (config.overlay) {
			$("body").prepend('<div id="'+config.overlay_div+'"></div>');
			$overlay = $('#'+config.overlay_div);
			$overlay.css({'opacity': config.opacity_level }); //set opacity
			if(config.overlay_cc){
				//click on overlay = close popup
				$overlay.click(function(){
					hidePopup();
				});
			}
			//special treatment for dying IE6
			isIE = $.browser.msie && !$.support.opacity;
			isIE6 = isIE && $.browser.version < 7;
			if (isIE6) {
				$(window).bind('resize scroll', function () {
					$overlay.css({width: $(window).width(), height: $(window).height(), top: $(window).scrollTop(), left: $(window).scrollLeft()});
				}).trigger('scroll');
			}
		}
		
		//show popup DIV - please set CSS for it
		$("body").prepend('<div id="'+config.popup_div+'">'+popup_content+'</div>');
		$popup = $('#'+config.popup_div);
		//centering popup vertically
		if(config.centered ) config.top = ( $(window).height() - $popup.outerHeight() ) / 2;
		config.top = Math.round(config.top);
		var scrollTop = $(window).scrollTop();
		var width = ( $(window).width() - $popup.outerWidth() ) / 2;
		//Fly-in popup only for floating popups 
		if (config.fly_in && config.floating) { //fly-in popup - by default from top
			switch(config.fly_from) {
				case 'top-left':
					$popup.css("top",  (- $popup.outerHeight() + scrollTop)+"px");
					$popup.css("left", - $popup.outerWidth());
					break;
				case 'top-right':
					$popup.css("top", (- $popup.outerHeight() + scrollTop)+"px");
					$popup.css("right", - $popup.outerWidth());
					break;
				case 'left':
					$popup.css("top", (config.top + scrollTop)+"px");
					$popup.css("left", - $popup.outerWidth());
					break;
				case 'right':
					$popup.css("top", (config.top + scrollTop)+"px");
					$popup.css("right", - $popup.outerWidth());
					break;
				case 'bottom':
					$popup.css("top", ($(window).height()+ $(document).scrollTop())+"px");
					$popup.css({'left': Math.round(width)});
					break;
				case 'bottom-left':
					$popup.css("top", ($(window).height()+ scrollTop)+"px");
					$popup.css("left", - $popup.outerWidth());
					break;
				case 'bottom-right':
					$popup.css("top", ($(window).height()+ scrollTop)+"px");
					$popup.css("right", - $popup.outerWidth());
					break;
				default:
					$popup.css("top", (- $popup.outerHeight() + scrollTop)+"px");
					$popup.css({'left': Math.round(width)});
			}
			
		} else { //not fly-in popup
			$popup.css({'left': Math.round(width)});
			$popup.css("top", (config.top  + scrollTop)+"px");
		}
		
		
		//close popup afer click on "close_id" element
		$('#'+config.close_id).click(
			function() {
				hidePopup();
				return false;
			}
		);
		
		//set floating popup
		if (config.floating) {
			setFloating();
		}
		
		//how popup should appear
		switch(config.popup_appear) {
			case 'fadeIn':
				$popup.fadeIn(config.popup_appear_time);
				break;					
			case 'slideDown':
				$popup.slideDown(config.popup_appear_time);
				break;	
			default: //'show' by default
				$popup.show(config.popup_appear_time);
				break;					
		}
		
		//fix position on resize
		$(window).resize(
			function() {
				if (!config.floating) {
					var width = ( $(window).width() - $popup.width() ) / 2;
					$popup.css({'left': width});
					if (config.centered) {
						var scrollTop = $(window).scrollTop();
						var height = Math.round(( $(window).height() - $popup.outerHeight() ) / 2);
						$popup.css({'top': (height + scrollTop)});
						config.top = height;
					}
				} else {
					if (config.centered) {
						var height = Math.round(( $(window).height() - $popup.outerHeight() ) / 2);
						config.top = height;
					}
				}
		 }); 
		
		//enable selfclose
		if(config.selfclose != 0) {
			setTimeout(hidePopup, (config.selfclose * 1000));
		}
		
		//set cookie that will prevent from showing popup again
		if(config.setcookie) {
			setCookie(config.cookie_name, '1', 365);
		}
 	};
	
	//hide popup function
	function hidePopup() {
		$popup.remove();
		if (config.overlay) {
			$overlay.remove();
		}
		if (config.floating) {
			clearInterval(timer);
			clearTimeout(timer_anim);
		}
	}
		
 	//initiate floating
 	function setFloating(){
		setTimeout(movePopup, 10);
 		timer = setInterval(movePopup, config.floating_reaction);
 	}
 	
 	//to float popup with page scrolling/resizing
 	function movePopup() {
 		goal = Math.round($(window).scrollTop() + config.top);
 		goal_left = Math.round(( $(window).width() - $popup.outerWidth() ) / 2);
 		offset = $popup.offset();
 		current_position = offset.top;
 		current_position_left = offset.left;
		if((current_position != goal || current_position_left != goal_left) && timer_anim == 0) animatePopup();
 	}
 	
	//animating popup - changing horizontal and vertical position
 	function animatePopup() {
 		current_position = Math.round((config.floating_speed * current_position + goal) / (config.floating_speed+1));
		previous_position_left = current_position_left;
 		current_position_left = Math.round((config.floating_speed * current_position_left + goal_left) / (config.floating_speed+1));
 		if (current_position != last_position || current_position_left != last_position_left) {
 			$popup.css("top", Math.round(current_position)+"px");
			$popup.css("left", Math.round(current_position_left)+"px");
			
			timer_anim = setTimeout(animatePopup, 20);
			last_position = current_position;
			last_position_left = current_position_left;
		}else if(current_position != goal || current_position_left != goal_left){
			last_position = current_position;
			last_position_left = current_position_left;
			if(current_position > goal) {
				current_position = current_position - 1;
			} else if(current_position < goal) {
				current_position = current_position + 1;
			}
			if(current_position_left > goal_left) {
				current_position_left = current_position_left - 1;
			} else if(current_position_left < goal_left) {
				current_position_left = current_position_left + 1;
			}
			timer_anim = setTimeout(animatePopup, 20);
		} else { 
			last_position = current_position = goal;
			last_position_left = current_position_left = goal_left;
			$popup.css("top", Math.round(goal)+"px");
			$popup.css("left", Math.round(goal_left)+"px");
			timer_anim = 0;	
		}
 	}
 	//set cookie
	function setCookie(c_name, value, expiredays) {
		var exdate=new Date();
		if(expiredays == 0) expiredays = null;
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	};
	//get cookie 
	function getCookie(c_name){
		if (document.cookie.length>0){
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1) {
		    c_start=c_start + c_name.length+1;
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    return unescape(document.cookie.substring(c_start,c_end));
		  }
		}
		return "";
	}
 	
   };

 })(jQuery);

