/*
Mioo LePopup v1.2
2011 © scripts.mioo.sk
*/
;(function (jQuery, window, document, undefined) {
	"use strict";	// Strict mode
	$ = jQuery.noConflict();
	var pluginName = 'LePopup',
	defaults = {
		modal	: true,
		autoShow	: false,
		hourInterval	: 0,	// If autoShow is true, this will show popup according to interval in Hours (1 will show popup automatically every hour to each user)
		showDelay	: 500,
		closeDelay	: 80000,
		animSpeed	: 100,
		skin	: "default"
	},
	globals = {
		lePopup : {
			overlay : "<div id='lepopup-overlay' />",
			wrap : "<div id='lepopup-wrap' />",
			outer : "<div id='lepopup-outer' />",
			inner : "<div id='lepopup-inner' />",
			close : "<a href='#' id='lepopup-close' />"
		},
		loading : "loading",
		visible : "visible",
		cookieName : "lepopup_interval"
	};
	
	// The actual plugin constructor
	function LeMiooPlugin(element, options) {
		this.element = element;
		this.$element = $(this.element);
		this.options = $.extend({}, defaults, options);
		this._defaults = defaults;
		this._name = pluginName;
		
		this.$popup = null;
		this.$overlay = null;
		this.$outer = null;
		this.$content = null;
		this.timeout = null;
		this.dynamic = null;
		this.ajax = null;
		this.cookieName = globals.cookieName + "_" + this.$element.LeCompatibleAttr("id");
		
		this.init();
	}
	
	// Plugin private methods
	LeMiooPlugin.prototype = {
		
		// Plugin initialization
		init : function () {
			this.setPopup();
			this.setAutoShow();
			
			if (!this.$element.is("a[href$='.php']")) {
				this.dynamic = false;
				this.$element.css('display','none');
				
			}else{
				this.dynamic = true;
				
			}
			this.bindLinks();
		},
		
		setPopup : function () {
			this.$popup = $("#" + $(globals.lePopup.wrap).LeCompatibleAttr("id"));
			if (!this.$popup.length) {
				var popup = $(globals.lePopup.wrap);
				popup.append($($(globals.lePopup.outer).addClass(this.options.skin).append(globals.lePopup.inner + globals.lePopup.close)));
				this.$popup = $(popup);
				this.$close = this.$popup.find("#lepopup-close");
				var self = this;
				this.$close.bind("click", function(e){
					self.toggle(false);
					e.preventDefault();
					return false;
				});
				$("body").append(this.$popup).append(globals.lePopup.overlay);
			}
			this.$content = this.$popup.find("#lepopup-inner");
			this.$outer = this.$popup.find("#lepopup-outer");
			
			this.$overlay = $("#" + $(globals.lePopup.overlay).LeCompatibleAttr("id"));
			this.bindClosures();
			
		},
		
		bindClosures : function () {
			var self = this;
			this.$overlay.bind("click", function () {
				self.toggle(false);
			});
			
			$(document).bind("keyup", function (e) {
				if (self.$popup.is(":visible") && e.keyCode === 27) {
					self.toggle(false);
				}
			});
		},
		
		bindLinks : function () {
			var self = this;
			this.$links = $("a[href=#" + this.$element.LeCompatibleAttr("id") + "]");
			if (this.dynamic){$.extend(this.$links, this.$element);}
			this.$links.bind("click", function (e) {
				self.toggle(true);
				e.preventDefault();
				return false;
			});
		},
		
		bindResize : function () {
			var self = this;
			$(window).bind("resize scroll", function () {
				self.setPosition();
			});
		},
		
		unbindResize : function () {
			$(window).unbind("resize scroll");
		},
		
		setAutoShow : function () {
			if (this.options.autoShow) {
				var cookieValue = parseFloat(this.getCookie(this.cookieName), 10) || null,
					hourInt = parseFloat(this.options.hourInterval, 10),
					self = this;
				
				if (cookieValue !== null && (hourInt === 0 || cookieValue !== hourInt)) {
					this.deleteCookie(this.cookieName);
					cookieValue = parseFloat(this.getCookie(this.cookieName), 10) || null;
				}

				if (cookieValue === null || !hourInt) {
					$(document).bind("ready", function () {
						if (self.options.showDelay > 0) {
							self.timeout = setTimeout(function () {
								self.toggle(true);
								self.setAutoClose();

								if (hourInt)
									self.setCookie(self.cookieName, hourInt, hourInt);

							}, self.options.showDelay);
						}else{
							self.toggle(true);
							self.setAutoClose();
						}
						
					});
				}
			}
		},
		
		setAutoClose : function () {
			var self = this;
			if (this.options.closeDelay > this.options.animSpeed) {
				this.timeout = setTimeout(function() {
					self.toggle(false);
				}, this.options.closeDelay);
			}
		},
		
		toggle : function (show, el) {
			var self = this,
			url;
			
			if(!this.$popup.is(":visible"))
				this.unbindResize();
			
			clearTimeout(this.timeout);
			
			if (this.dynamic && typeof el === "undefined" && show) {
				url = this.$element.attr("href");
				this.$content.addClass(globals.loading);
				this.ajax = 
					$.get(url)
						.done( function(data) {
							self.toggle(true, $(data));
						}).always( function() {
							self.$content.removeClass(globals.loading);
							return; 
						});
				this.doToggle(show);
				
			}else{
				if(typeof el === "undefined") el = this.$element.clone().css('display','block');
				
				if (show && (this.$content.children().get(0) !== el.get(0))) {
					this.$content.empty().append(el);
				}
				this.doToggle(show);
			}
			
		},
		
		doToggle : function (show) {
			this.toggleOverlay(show);
			
			
			this.setToggle(this.$popup, show);
			this.$popup.css(this.getPosition());
		},
		
		toggleOverlay : function (show) {
			if (this.options.modal) {
				this.setToggle(this.$overlay, show);
			}
		},
		
		setPosition : function () {
			if (this.$popup.is(":visible")) {
				this.$popup.stop(true,false).animate(this.getPosition(), this.options.animSpeed);
			}
		},
		
		getPosition : function () {
			var pos = {
				left : ($(window).width()/2) - (this.$popup.outerWidth()/2) + $(document).scrollLeft(),
				top : ($(window).height()/2) - (this.$popup.outerHeight()/2) + $(document).scrollTop()
			};
			return pos;
		},
		
		setToggle : function (ele, show) {
			if(typeof ele === "undefined" || ele.length === 0) return false;
			var self = this;
			
			if (show) {
				if (!ele.is(":visible")) {
					this.setSkin();
					ele.stop(true,false).fadeIn(this.options.animSpeed, function(){self.bindResize();});
				}
			} else {
				if (ele.is(":visible"))
					ele.stop(true,false).fadeOut(this.options.animSpeed, function(){self.emptyContent();});
			}
		},
		
		emptyContent : function () {
			if(this.ajax) {
				this.ajax.abort();
				return false;
			}
			
			this.$content.empty();
		},
		
		setSkin : function () {
			this.$outer.removeClass().addClass(this.options.skin);
		},

		getCookie : function (check_name) {
			var a_all_cookies = document.cookie.split( ';' );
			var a_temp_cookie = '';
			var cookie_name = '';
			var cookie_value = '';
			var b_cookie_found = false; 
			var i = '';
			
			for ( i = 0; i < a_all_cookies.length; i++ )
			{
				a_temp_cookie = a_all_cookies[i].split( '=' );
				
				cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
				if ( cookie_name == check_name )
				{
					b_cookie_found = true;
					if ( a_temp_cookie.length > 1 )
					{
						cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
					}
					return cookie_value;
					break;
				}
				a_temp_cookie = null;
				cookie_name = '';
			}
			if ( !b_cookie_found ) 
			{
				return null;
			}
		},

		setCookie : function (name, value, expires, path, domain, secure) {
			// set time, it's in milliseconds
			var today = new Date();
			today.setTime( today.getTime() );
			if ( expires )
			{
				expires = expires * 1000 * 60 * 60;
			}

			var expires_date = new Date( today.getTime() + (expires) );

			document.cookie = name + "=" +escape( value ) +
				( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
				( ( path ) ? ";path=" + path : "" ) + 
				( ( domain ) ? ";domain=" + domain : "" ) +
				( ( secure ) ? ";secure" : "" );
		},

		// this deletes the cookie when called
		deleteCookie : function (name, path, domain) {
			if ( this.getCookie( name ) ) document.cookie = name + "=" +
					( ( path ) ? ";path=" + path : "") +
					( ( domain ) ? ";domain=" + domain : "" ) +
					";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		},
		
		method : function () {}
		
	};
	
	// jQuery external plugin definition
	$.fn[pluginName] = function (options) {
		return this.each(function () {
			if (!$.data(this, 'plugin_' + pluginName)) {
				$.data(this, 'plugin_' + pluginName, new LeMiooPlugin(this, options));
			}
		});
	};
	
	$.fn.LeCompatibleAttr = function () {
		try {
			return this.prop("id");
		} catch (err) {
			return this.attr("id");
		}
	};
	
})(jQuery, window, document);

