/*
PopUp, by Damian Szewczyk
Version 1.0
PopUp plugin for jQuery. Can show different types of popups. Normal popup (als floating), sticky popup in the 8 positions, and "catfish" in two positions.

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
param	 options[selfclose]       int					  after what time self close popup. 0 - disbale feature
param	 options[popup_div]		  string				  id of popup DIV
param	 options[popup_appear]    string				  how popup appears "show" (default), "fadeIn", "slideDown"
param	 options[popup_appear_time]string/int			  time of appearing (may be "slow", "fast", "def" or number of miliseconds)
param	 options[overlay_div]	  string				  id of overlay DIV
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[centered]		  bool					  center popup vertically
param	 options[cookie_name]	  string				  name of cookie
param	 options[cooke_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
param	 options[floating_spped]  int					  smaller value, higher speed
param	 options[top]		  	  int					  distance from top to show popup (in px)

param	 options[sticky_div]	  string				  id of sticky popup DIV
param	 options[sticky_position] string				  8 positions top left top, top right... etc. - abbreiated: tl, t, tr, l, r, bl, b, br
param	 options[sticky_hide]	  int				  	  how much hide popup (in px)


param	 options[catfish_div]	  string				  id of catfish popup DIV
param	 options[catfish_position]string				  2 positions top or botoom
 
param	 callback                 function                
*/
 (function($) {
 
   $.fn.popup = function(settings, callback) {
     var config = {
     	 popup_type		: 'normal',  //popup type: 'normal', 'sticky', 'catfish'
     	 starttime      : 0, 		//in seconds, 0 = start immediately after load
     	 selfclose		: 0,		//in seconds, 0 = disable selfclose
		 popup_div		: 'popup',  //id of popup DIV
		 overlay_div	: 'overlay', //id of overlay DIV
		 close_id		: 'baner_close', //HTML id of something used to close popup
		 overlay		: false,		//show overlay
		 opacity_level  : 0.7,
		 overlay_cc		: true,		//close popup on overlay click
		 centered		: true,
		 setcookie 		: false,
		 cookie_name	: 'popup',
		 cookie_timeout : null,			//in days
		 floating		: true,
		 floating_reaction  : 700,		//in miliseconds
		 floating_speed : 42,		//smaller value = higher speed
		 top			: 130,		//in px
		 sticky_div		: 'sticky',
		 sticky_position: 't',
		 sticky_hide	: 0, //number of pixels to hide sticky popup
		 catfish_div	: 'catfish',
		 catfish_position: 'bottom',
		 popup_appear : 'show',
		 popup_appear_time : '',
		 fly_from : 'right'
		 };
	//variables used for floating
	var timer;
	var timer_anim = 0;
	var timer_selfclose = 0;
	var goal;
	var goal_left;
	var current_position;
	var current_position_left;
	var last_position;
	if (settings) $.extend(config, settings);
	var callback = callback || function() { };
    var popup_content = '';
     this.each(function() {
       popup_content = $(this).html();
       $(this).html('');
     });

 	if(config.starttime == 0){ //start immediately after load
 		setTimeout(show_popup, 250);
 	} else {
 		setTimeout(show_popup, (config.starttime * 1000));
 	}
	return this;
	
 	function show_popup() {
 		if(config.setcookie) { //check for cookie
 			if(getCookie(config.cookie_name)) return false;
 		}
 		
 		//STICKY POPUP
 		switch(config.popup_type){
 		case 'sticky':
 			var sh = config.sticky_hide;
 		  $("body").prepend('<div id="'+config.sticky_div+'">'+popup_content+'</div>');
 		  var sticky_div = $("#"+config.sticky_div);
 		  switch(config.sticky_position) {
		    case 't':
		      var szerokosc = ( $(window).width() - sticky_div.width() ) / 2;
		      sticky_div.css('top', '-'+sh+'px');
		      sticky_div.css({'left':szerokosc});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('top', '0px');
				      }, 
				      function () {
				        $(this).css('top', '-'+sh+'px');
				      }
				    );
				}

		      break;
		    case 'tr':
		      sticky_div.css('right', '-'+sh+'px');
		      sticky_div.css({'top':'0px'});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('right', '0px');
				      }, 
				      function () {
				        $(this).css('right', '-'+sh+'px');
				      }
				    );
			  }
		      break;
		    case 'tl':
		      sticky_div.css('top', '0px');
		      sticky_div.css({'left':'-'+sh+'px'});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('left', '0px');
				      }, 
				      function () {
				        $(this).css('left', '-'+sh+'px');
				      }
				    );
			  }
		      break;
		    case 'b':
		      var szerokosc = ( $(window).width() - sticky_div.width() ) / 2;
		      sticky_div.css('bottom', '-'+sh+'px');
		      sticky_div.css({'left':szerokosc});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('bottom', '0px');
				      }, 
				      function () {
				        $(this).css('bottom', '-'+sh+'px');
				      }
				    );
			  }
			   $(window).scroll(function () { 
			      sticky_div.css('bottom', '-'+sh+'px');
			    });
		      break;
		    case 'bl':
		      sticky_div.css('left', '-'+sh+'px');
		      sticky_div.css({'bottom':'0px'});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('left', '0px');
				      }, 
				      function () {
				        $(this).css('left', '-'+sh+'px');
				      }
				    );
			  }
			   $(window).scroll(function () { 
			      sticky_div.css('bottom', '0px');
			    });
		      break;
		   	case 'br':
		      sticky_div.css('right', '-'+sh+'px');
		      sticky_div.css({'bottom':'0px'});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('right', '0px');
				      }, 
				      function () {
				        $(this).css('right', '-'+sh+'px');
				      }
				    );
			  }
			   $(window).scroll(function () { 
			      sticky_div.css('bottom', '0px');
			    });
		      break;
		     case 'l':
		      var wysokosc = ( $(window).height() - sticky_div.height() ) / 2;
		      sticky_div.css('left', '-'+sh+'px');
		      sticky_div.css({'top':wysokosc});
		      if(sh){
			      sticky_div.hover(
				      function () {
				        $(this).css('left', '0px');
				      }, 
				      function () {
				        $(this).css('left', '-10px');
				      }
				    );
			  }
		      break;
		     case 'r':
		      var wysokosc = ( $(window).height() - sticky_div.height() ) / 2;
		      sticky_div.css('right', '-'+sh+'px');
		      sticky_div.css({'top':wysokosc});
		      sticky_div.hover(
			      function () {
			        $(this).css('right', '0px');
			      }, 
			      function () {
			        $(this).css('right', '-'+sh+'px');
			      }
			    );
		      break;
		  }		
		//FIX POSITION ON RESIZE
		$(window).resize(
			function(){
			if(config.sticky_position == 't' || config.sticky_position == 'b') {
			  var width = ( $(window).width() - sticky_div.width() ) / 2;
			  sticky_div.css({'left': width});
			} else if(config.sticky_position == 'l' || config.sticky_position == 'r') {
			  var height = ( $(window).height() - sticky_div.height() ) / 2;
			  sticky_div.css({'top': height});
			}
		 });  
		  
	  	$('#'+config.close_id).click(
			function(){
				sticky_div.remove();
			}
		);
			switch(config.popup_appear){
				case 'show':
					sticky_div.show(config.popup_appear_time);
					break;
				case 'fadeIn':
					sticky_div.fadeIn(config.popup_appear_time);
					break;					
				case 'slideDown':
					sticky_div.slideDown(config.popup_appear_time);
					break;	
			}
		break;
		
		//CATFISH STYLE (AT THE BOTTOM OR AT THE TOP)
		case 'catfish':
			//CSS AND EXTRA DIVS FOR IE
			if(!jQuery.support.hrefNormalized){
				$("body").wrapInner('<div id="jquery-catfish-zip"><div id="jquery-catfish-masthead"></div></div>');
				$("#jquery-catfish-zip").css(
				{
					'width': '100%',
					'padding': '0',
					'margin': '0',
					'height': '100%',
					'overflow': 'auto',
					'position': 'relative'
				}
				);
				$("#jquery-catfish-masthead").css(
				{
					'width': 'auto'
				}
				);
				$("html, body").css(
				{
					'height': '100%',
					'overflow': 'hidden',
					'width': 'auto'
				}
				);
			}
			//CATFISH AT THE BOTTOM
			if(config.catfish_position == 'bottom'){
				$("body").append('<div id="'+config.catfish_div+'">'+popup_content+'</div>');
				var catfish_div = $("#"+config.catfish_div);
				var catfish_height = catfish_div.height();
				$("html").css("padding-bottom", catfish_height);
				catfish_div.css("bottom", "0");
			//CATFISH ON TOP	
			} else {
				$("body").prepend('<div id="'+config.catfish_div+'">'+popup_content+'</div>');
				var catfish_div = $("#"+config.catfish_div);
				var catfish_height = catfish_div.height();
				$("html").css("padding-top", catfish_height);
				catfish_div.css("top", "0");
			}
			
		    //CSS FOR IE
			if(!jQuery.support.hrefNormalized){
				catfish_div.css(
				{
					'position': 'absolute',
					'overflow': 'hidden',
					'z-index': '1000'
				}
				);
			}
			switch(config.popup_appear){
				case 'show':
					catfish_div.show(config.popup_appear_time);
					break;
				case 'fadeIn':
					catfish_div.fadeIn(config.popup_appear_time);
					break;					
				case 'slideDown':
					catfish_div.slideDown(config.popup_appear_time);
					break;	
			}
			$("#"+config.close_id).click(
				function(){
					catfish_div.remove();
					$("html").css("padding", "0");
				}
			);
			
			break;  
		default:
 		//NORMAL POPUP
 		
	 		if(config.overlay) {
	 			$("body").prepend('<div id="'+config.overlay_div+'"></div>');
	 			var overlay_div = $('#'+config.overlay_div);
	 			overlay_div.css({'height':$(document).height(), 'opacity': config.opacity_level });
	 			$(window).resize(function() { overlay_div.css({'height':$(document).height()}); });
	 			if(config.overlay_cc){
					overlay_div.click(function(){
						$('#'+config.close_id).click();
					});
				}
	 		}
	 		
	 		$("body").prepend('<div id="'+config.popup_div+'">'+popup_content+'</div>');
	 		var popup_div = $('#'+config.popup_div);
	 		if(config.centered ) config.top = ( $(window).height() - popup_div.height() ) / 2;
	 		config.top = Math.round(config.top);
	 		//popup_div.css("top", config.top+"px");
	 		
			//console.log($(window).width()+" "+popup_div.outerWidth());
			var width = ( $(window).width() - popup_div.outerWidth() ) / 2;
			//popup_div.css({'left': Math.round(width)});
			$('#'+config.close_id).click(
				function(){
					popup_div.remove();
					if(config.overlay)
						overlay_div.remove();
					if(config.floating){
						clearInterval(timer);
						clearTimeout(timer_anim);
					}
				}
			);
			
			$("#"+config.popup_div).css(config.fly_from, 0);

			
			//FLOATING POPUP
			if(config.floating){
				setFloating();
			}
			switch(config.popup_appear){
				case 'show':
					popup_div.show(config.popup_appear_time);
					break;
				case 'fadeIn':
					popup_div.fadeIn(config.popup_appear_time);
					break;					
				case 'slideDown':
					popup_div.slideDown(config.popup_appear_time);
					break;	
			}
			
			//FIX POSITION ON RESIZE
			$(window).resize(
				function(){
				  var width = ( $(window).width() - popup_div.width() ) / 2;
				  popup_div.css({'left': width});
				  if(config.centered ){
				  	var height = Math.round(( $(window).height() - popup_div.height() ) / 2);
				  	popup_div.css({'top': height});
				  	config.top = height;
				  }
			 }); 
		}
		
		if(config.selfclose != 0){
			$("input[name=email]").focus(function(){ clearTimeout(timer_selfclose);  });
			timer_selfclose = setTimeout(function() {$('#'+config.close_id).click()}, (config.selfclose * 1000));
		}
		
		if(config.setcookie) {
			setCookie(config.cookie_name, '1', config.cookie_timeout);
		}
 	};
 	
 	function setFloating(){
 		timer = setInterval(movePopup, config.floating_reaction);
 	}
 	
 	//to float popup with page
 	function movePopup(){
 		goal = $(document).scrollTop() + config.top;
 		goal_left = ( $(window).width() - $('#'+config.popup_div).outerWidth() ) / 2;
 		offset = $("#"+config.popup_div).offset();
 		current_position = offset.top;
 		current_position_left = offset.left;
		if(current_position != goal && timer_anim == 0) animatePopup();
 	}
 	
 	function animatePopup(){
 		current_position = Math.round((config.floating_speed * current_position + goal)/(config.floating_speed+1));
 		current_position_left = Math.round((config.floating_speed * current_position_left + goal_left)/(config.floating_speed+1));
 		if(current_position != last_position) {
 			$("#"+config.popup_div).css("top", Math.round(current_position)+"px");
 			if(config.fly_from == 'left'){
 				if(current_position_left < goal_left)
 					$("#"+config.popup_div).css("left", Math.round(current_position_left)+"px");
 			} else {
 				 if(current_position_left > goal_left)
 					$("#"+config.popup_div).css("left", Math.round(current_position_left)+"px");
 			}
 			
			timer_anim = setTimeout(animatePopup, 20);
			last_position = current_position;
		}else if(current_position != goal){
			last_position = current_position;
			if(current_position > goal)
				current_position = current_position - 1;
			else
				current_position = current_position + 1
			timer_anim = setTimeout(animatePopup, 20);
		} else { 
			last_position = current_position = goal;
			$("#"+config.popup_div).css("top", Math.round(goal)+"px");
			timer_anim = 0;
		}
 	}
 	
	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());
	};
	  
	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 "";
	}
 	
 	function errorcheck() {

	};
   };
   
   
 
 })(jQuery);
