/*
PopUp, by Damian Szewczyk
Version 1.0
PopUp plugin for jQuery

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[overlay_div]	  string				  id of overlay DIV
param	 options[overlay]		  bool					  turn on/off overlay (true/false)
param	 options[opacity]		  float					  opacity level of overlay (from 0 to 1)
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	 callback                 function                
*/
 (function($) {
 
   $.fn.popup = function(settings, callback) {
     var config = {
     	 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
		 overlay		: false,		//show overlay
		 opacity_level  : 0.7,
		 setcookie 		: false,
		 cookie_name	: 'popup',
		 cookie_timeout : null,			//in days
		 floating		: true,
		 floating_reaction  : 700,		//in miliseconds
		 floating_speed : 12,		//smaller value = higher speed
		 top			: 130		//in px
		 };
	//variables used for floating
	var timer;
	var timer_anim = 0;
	var timer_selfclose = 0;
	var goal;
	var current_position;
	var last_position;
	if (settings) $.extend(config, settings);
	var callback = callback || function() { };
    
     this.each(function() {
       // element-specific code here
       //show_popup();
     });

 	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;
 		}
 		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()}); });
 		}
 		
 		$("body").prepend('<div id="'+config.popup_div+'">'+$('#popup_content').html()+'</div>');
 		$("#"+config.popup_div).css("top", config.top+"px");
 		var popup_div = $('#'+config.popup_div);
		//console.log($(window).width()+" "+popup_div.outerWidth());
		var width = ( $(window).width() - popup_div.outerWidth() ) / 2;
		popup_div.css({'left': Math.round(width)});
		$('#baner_close').click(
			function(){
				popup_div.remove();
				if(config.overlay)
					overlay_div.remove();
				if(config.floating){
					clearInterval(timer);
					clearTimeout(timer_anim);
				}
			}
		);
		
		if(config.setcookie) {
			setCookie(config.cookie_name, '1', config.cookie_timeout);
		}
		
		if(config.floating){
			setFloating();
		}
		if(config.selfclose != 0){
			$("input[name=email]").focus(function(){ clearTimeout(timer_selfclose);  });
			timer_selfclose = setTimeout(function() {$('#baner_close').click()}, (config.selfclose * 1000));
		}
 	};
 	
 	function setFloating(){
 		timer = setInterval(movePopup, config.floating_reaction);
 	}
 	
 	//to float popup with page
 	function movePopup(){
 		goal = $(document).scrollTop() + config.top;
 		offset = $("#"+config.popup_div).offset();
 		current_position = offset.top;
		if(current_position != goal && timer_anim == 0) animatePopup();
 	}
 	
 	function animatePopup(){
 		current_position = Math.round((config.floating_speed * current_position + goal)/(config.floating_speed+1));
 		if(current_position != last_position) {
 			$("#"+config.popup_div).css("top", Math.round(current_position)+"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);
