/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2001 Robert Penner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */

/**
 * jQuery custom event "outerClick".
 * @author David Brockman Smoliansky http://littleroom.se/
 * @license GNU Lesser General Public License: http://creativecommons.org/licenses/LGPL/2.1/
 * @version 1.1
 * 2009/02/27
 *
 * The outerClick event is fired when an element outside of the target element is clicked.
 *
 * Usage:
 * $(selector).bind("outerClick", fn);   // Bind the function fn to the outerClick event on each of the matched elements.
 * $(selector).outerClick(fn);           // Bind the function fn to the outerClick event on each of the matched elements.
 * $(selector).trigger("outerClick");    // Trigger the outerClick event on each of the matched elements.
 * $(selector).outerClick();             // Trigger the outerClick event on each of the matched elements.
 * $(selector).unbind("outerClick", fn); // Unbind the function fn from the outerClick event on each of the matched elements.
 * $(selector).unbind("outerClick");     // Unbind all outerClick events from each of the matched elements.
 */

/*global jQuery */
(function ($, elements, OUTER_CLICK) {

	/**
	 * Check if the event should be fired.
	 * @param {Object} event  The click event.
	 * @private
	 */
	function check(event) {
		for (var i = 0, l = elements.length, target = event.target, el; i < l; i++) {
			el = elements[i];
			if (el !== target && !(el.contains ? el.contains(target) : el.compareDocumentPosition ? el.compareDocumentPosition(target) & 16 : 1)) {
				$.event.trigger(OUTER_CLICK, event, el);
			}
		}
	}


	$.event.special[OUTER_CLICK] = {

		setup: function () {
			var i = elements.length;
			if (!i) {
				$.event.add(document, 'mousedown', check);
			}
			if ($.inArray(this, elements) < 0) {
				elements[i] = this;
			}
		},

		teardown: function () {
			var i = $.inArray(this, elements);
			if (i >= 0) {
				elements.splice(i, 1);
				if (!elements.length) {
					$.event.remove(document, 'mousedown', check);
				}
			}
		}

	};


	/**
	 * Event helper outerClick
	 *
	 * @param  {Function} [fn]  A function to bind to the outerClick event on each of the matched elements.
	 *                          If fn is omitted the event is triggered.
	 * @return {jQuery}         Returns the jQuery object.
	 */
	$.fn[OUTER_CLICK] = function (fn) {
		return fn ? this.bind(OUTER_CLICK, fn) : this.trigger(OUTER_CLICK);
	};

})(jQuery, [], 'outerClick');



(function($) {
		  
$.widget("pp.popup", $.extend({}, $.ui.dialog.prototype, {
							  
	_init: function()
	{
		var self = this,
			options = this.options,
			closeTimeout = null,
			IE = this.IE = /*@cc_on!@*/false,
			IE6 = this.IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/,
			backgroundIsColor = true;
			
			
		// handle percent sign in width/height
		if (String(options.width).match(/%$/))
			options.width = $(window).width() / 100 * parseInt(options.width);
		else
			options.width = parseInt(options.width) || 'auto';
		
		
		if (String(options.height).match(/%$/))
			options.height = $(window).height() / 100 * parseInt(options.height);
		else
			options.height = parseInt(options.height) || 'auto';
				
				
		$.ui.dialog.prototype._init.call(this);	
		
		// clean default UI Dialog
		this.element
			.add(this.uiDialog)
				.removeClass('ui-widget-content')
				.removeClass('ui-corner-all')
				.css('overflow', 'hidden');
		this.uiDialogTitlebar
			.removeClass('ui-widget-header')
			.removeClass('ui-corner-all')
			.find('.ui-dialog-titlebar-close')
				.remove();
			
					
		
		var framePaddingBottom = 7;
		if (options.closeBtn)
			framePaddingBottom += 22;
		if (options.affiliate)
			framePaddingBottom += 12;		
				
		this.element
			.css({
				padding: '7px',
				paddingBottom: framePaddingBottom + 'px',
				'text-align': 'center'
			})
			.wrapInner('<div class="pp-content-frame" />')
			.find('.pp-content-frame')
				.css({
					padding: parseInt(options.contentPadding),
					overflow: 'auto',
					'text-align': 'left'
				});
	
		
		// set background
		if (options.background)
		{
			if (!options.background.match(/^#\w{3,6}$/))
			{					
				if (this.IE6 && options.background.indexOf(".png") != -1)
				{
					this.element
						.css('background-image', 'none')
						.get(0)
						.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,src='" + options.background + "',sizingMethod='image')";
				}
				backgroundIsColor = false;
				options.background = 'url('+options.background+') no-repeat center';
			}
			
			if (!this.IE6 || backgroundIsColor)
				$(this.element) 
					.css('background', options.background);  
			
		}
		
		
		// some css for title bar
		this.uiDialogTitlebar.css({
			textAlign: 'left',
			padding: '5px 10px',
			fontSize: '14px',
			fontWeight: 'bold',
			'line-height': 'normal',
			color: options.titleTextColor,
			background: options.titleColor
		});
		
		
		if (options.noTitle) // handle no title case
		{	
			this.uiDialogTitlebar
				.css('background', (backgroundIsColor && options.background ? options.background : 'none'))
				.remove(); 
		}
		
		this._setFrame();
		
		this._setTransparency();
		
		this._setShadow();
		
		this._createButtons(options.buttons);	


		// close triggers
		(options.closeOnOuterClick && 
		 	this.uiDialog.outerClick(function(e) {
				if (closeTimeout != null)
					clearTimeout(closeTimeout);
				self.close(e);
				return false;
			}
		));		
		

		this.element.bind('popupopen', function(e, ui) 
		{										
			if (options.closeTimer)
			{									
				var closePopup = function() { self.close(); };	
				setTimeout(closePopup, options.closeTimer);
			}

			// if added before popup is open cause weird distortion on FF (wtf?!)
			self._addAffiliate();
			self._addCloseBtn();
			
			// set the proper height for the frame, possible only on established dimensions
			self.element
				.find('.pp-content-frame')
					.height(self.element.height() - parseInt(self.options.contentPadding)*2);  
						
			
			// hide that ugly outline for all links
			self.element
				.find('a')
					.css('outline', 'none')
					.end()
				.find('p')
					.css({
						'padding-top': 0,
						'margin-top': 0
					});				
		});
		
		
		this.element.bind('popupeffectcomplete', function(e, ui) 
		{			
			// position fixed, this should be done after popup is open andd effects complete, 
			// otherwise absolute > fixed positioning conflict comes into play
			if (options.sticky && !options.draggable)
			{			
				// IE6 doesn't support position:fixed, so a workaround here,
				// and same fix for all IEs which hang on scroll when they got shadow + position:fixed applied
				if (self.IE6 || self.IE && options.shadow)
				{
					var top = parseInt(self.uiDialog.css('top'));
					self.uiDialog.css('position', 'absolute');
					$(window).scroll(function(e) {
						self.uiDialog.css({
							top: top + $(document).scrollTop()
						}); 						  
					});
				}
				else 
					self.uiDialog.css({
						position: 'fixed',
						top: parseInt(self.uiDialog.css('top')) - $(document).scrollTop()
					});
			}
		});
		
		
		this.element.bind('popupclose', function()
		{
			if (options.removeOnClose)
				self.element.remove();
		});
		
		
		
		// open with delay or not
		if (options.delay)
		{
			var openPopup = function() { self.triggerOpen(); };	
			setTimeout(openPopup, options.delay);	
		}
		else
			self.triggerOpen();
		
	},
	
	destroy: function() {
		(this.overlay && this.overlay.destroy());
		this.uiDialog.hide();
		this.element
			.unbind('.dialog')
			.removeData('dialog')
			.removeClass('ui-dialog-content ui-widget-content')
			.hide().appendTo('body');
		this.uiDialog.remove();

		(this.originalTitle && this.element.attr('title', this.originalTitle));
		
	},
	
	triggerOpen: function()
	{
		var self = this;
		
		if (this.options.openOnUnload)
		{
			self.prevY = null;
			
			$(document).mousemove(function(e) {
				if (!self.isOpen() && e.pageY < $(document).scrollTop() + 40 && e.pageY < self.prevY)
					self.open();
				else
					self.prevY = e.pageY;
				
			});
		}
		else 
			this.open();
	},
	
	open: function() {
		if (this._isOpen) { return; }

		var self = this,
			options = this.options,
			uiDialog = this.uiDialog;

		if (options.modal)
		{
			this.overlay = new $.ui.dialog.overlay(this);
			
			// manually add all styles
			this.overlay.$el.css({
				position: 'fixed',
				top: 0,
				left: 0, 
				width: '100%',
				height: '100%',
				background: '#aaaaaa url(../css/smothness/images/ui-bg_flat_0_aaaaaa_40x100.png) 50% 50% repeat-x',
				opacity: .30
			});
			
			if (!options.overlay)
				this.overlay.$el.css('background', 'none');
			
			if (this.IE)
				this.overlay.$el
					.css({
						position: 'absolute',
						height: $(window).height()
					})
					.get(0)
						.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=30)';
						
		
			if (this.IE6) // take care of absent position:fixed
				$(window).scroll(function(e) {
					self.overlay.$el.css({
						top: $(document).scrollTop()
					});    
				});
		}
		
		(uiDialog.next().length && uiDialog.appendTo('body'));
		
		
		this._size();
		
		this._positionPopup(); 
		
		
		switch (options.effect)
		{
			case 'fade':
				uiDialog.fadeIn(options.effectSpeedn, function() { self._trigger('effectcomplete'); });
				break;
				
			case 'fly':
				var top = parseInt(uiDialog.css('top')),
					left = parseInt(uiDialog.css('left')),
					offsetWidth = self.uiDialog.width(),
					offsetHeight = self.uiDialog.height(),
					pTop = $(document).scrollTop(),
					speeds = {
						slow: 2000, 
						medium: 1000,
						fast: 500
					};
					
				switch (options.effectDirection) {
					
					case 'top':
						uiDialog
							.css('top', pTop)
							.show()
							.animate({ top: top }, speeds[options.effectSpeed], 'easeOutQuad', function() { self._trigger('effectcomplete'); });
						break;
						
					case 'bottom':
						uiDialog
							.css('top', pTop + $(window).height() - offsetHeight)  
							.show()
							.animate({ top: top }, speeds[options.effectSpeed], 'easeOutQuad', function() { self._trigger('effectcomplete'); });														
						break;
						
					case 'left':
						uiDialog
							.css('left', -(offsetWidth))
							.show()
							.animate({ left: left }, speeds[options.effectSpeed], 'easeOutQuad', function() { self._trigger('effectcomplete'); });
						break;
						
					case 'right':
						uiDialog
							.css('left', $(window).width() - offsetWidth - offsetWidth/4)
							.show()
							.animate({ left: left },speeds[options.effectSpeed], 'easeOutQuad', function() { self._trigger('effectcomplete'); });
						break;
				
				}
				break;
			
			default:
				uiDialog.show(options.show);
				self._trigger('effectcomplete');
		}
		
		this.moveToTop(true);

		// prevent tabbing out of modal dialogs
		(options.modal && uiDialog.bind('keypress.ui-dialog', function(event) {
			if (event.keyCode != $.ui.keyCode.TAB) {
				return;
			}

			var tabbables = $(':tabbable', this),
				first = tabbables.filter(':first')[0],
				last  = tabbables.filter(':last')[0];

			if (event.target == last && !event.shiftKey) {
				setTimeout(function() {
					first.focus();
				}, 1);
			} else if (event.target == first && event.shiftKey) {
				setTimeout(function() {
					last.focus();
				}, 1);
			}
		}));

		// set focus to the first tabbable element in the content area or the first button
		// if there are no tabbable elements, set focus on the dialog itself
		$([])
			.add(uiDialog.find('.ui-dialog-content :tabbable:first'))
			.add(uiDialog.find('.ui-dialog-buttonpane :tabbable:first'))
			.add(uiDialog)
			.filter(':first')
			.focus();

		this._trigger('open');
		this._isOpen = true;
		
	},
	
	
	_setTransparency: function()
	{
		var self = this,
			options = this.options;
		
		// transparency
		if (options.transparency)
		{
			if (this.IE)
				this.uiDialog[0].style.filter += ' progid:DXImageTransform.Microsoft.Alpha(opacity='+options.transparency+') '; 
			else 
			{
				options.transparency /= 100; 
				this.uiDialog.attr('style', this.uiDialog.attr('style')
					+ '-khtml-opacity:'+options.transparency+';'
					+ '-moz-opacity:'+options.transparency+';'
					+ 'opacity:'+options.transparency+';'
				);	
			}
		}
			
	},
	
	
	_setShadow: function()
	{
		var self = this,
			options = this.options;
			
			// box shadow
		if (options.shadow)
		{
			if (!this.IE)
				this.uiDialog.attr('style', this.uiDialog.attr('style')+'-moz-box-shadow: 0 0 2em 0 rgba(0, 0, 0, .5);-webkit-box-shadow: 0 0 2em rgba(0, 0, 0, .5);');
			else
				this.uiDialog[0].style.filter += " progid:DXImageTransform.Microsoft.Shadow(color=#666666,direction=180,strength=10)"
											  +  " progid:DXImageTransform.Microsoft.Shadow(color=#666666,direction=90,strength=10)"
											  +  " progid:DXImageTransform.Microsoft.Shadow(color=#666666,direction=270,strength=10)"
											  +  " progid:DXImageTransform.Microsoft.Shadow(color=#666666,direction=0,strength=10) ";
		}
	},
	
	_setFrame: function()
	{
		var self = this,
			options = this.options;
		
		// frame settings - size, color, radius
		if (options.frameSize && options.frameColor) 
		{
			$([])
				.add(this.uiDialogTitlebar)
				.add(this.element)
				.css({
					'border-width': options.frameSize + 'px',
					'border-style': 'solid',
					'border-color': options.frameColor
				});
		
			this.uiDialogTitlebar
				.css({
					borderBottom: 'none'
			});
			if (!options.noTitle)
				this.element
					.css({
						borderTop: 'none'
					});
		}
		
		
		if (options.borderRadius && !this.IE)
		{
			this.uiDialog
				.attr('style', this.uiDialog.attr('style')
					+ 'border-radius:'+options.borderRadius+'px;' 
					+ '-moz-border-radius:'+options.borderRadius+'px;'
					+ '-webkit-border-radius:'+options.borderRadius+'px;'
				); // for box-shadow sake
								
			this.element
				.attr('style', this.element.attr('style')
					+ '-moz-border-radius-bottomleft:'+options.borderRadius+'px;'
					+ '-webkit-border-bottom-left-radius:'+options.borderRadius+'px;'
					+ 'border-radius-bottomleft:'+options.borderRadius+'px;'
					+ '-moz-border-radius-bottomright:'+options.borderRadius+'px;'
					+ '-webkit-border-bottom-right-radius:'+options.borderRadius+'px;'
					+ 'border-radius-bottomright:'+options.borderRadius+'px;'
				);
				
			var topBorderElement = options.noTitle ? this.element : this.uiDialogTitlebar;
			
			topBorderElement
				.attr('style', topBorderElement.attr('style') 
					+ '-moz-border-radius-topleft:'+options.borderRadius+'px;'
					+ '-webkit-border-top-left-radius:'+options.borderRadius+'px;'
					+ 'border-radius-topleft:'+options.borderRadius+'px;'
					+ '-moz-border-radius-topright:'+options.borderRadius+'px;'
					+ '-webkit-border-top-right-radius:'+options.borderRadius+'px;'
					+ 'border-radius-topright:'+options.borderRadius+'px;'
				); 
								
		}
	},
	
	
	_positionPopup: function()
	{
		var self = this,
			options = this.options;
		
		// position popup
		if (options.overElement !== -1)
		{
			var x, y,
				overElement = window,
				offset = { top: 0, left: 0 },
				w = self.uiDialog.width(), 
				h = self.uiDialog.height(),
				pTop = $(document).scrollTop(),
				pLeft = $(document).scrollLeft();
				
			if ('' != $.trim(options.overElement))
			{
				var id = '#' + options.overElement.replace(/^#/, '');	
				if  ($(id).length)
				{
					overElement = id; 
					offset = $(overElement).offset();
				}
			}
			
			distanceH = parseInt(options.distanceH);
			distanceV = parseInt(options.distanceV);
			
			
			if ('top' == options.distanceTopBottom)
				y = offset.top + distanceV ;
			else
				y = offset.top + $(overElement).height() - h - distanceV;
				
				
			if ('left' == options.distanceRightLeft)
				x = offset.left + distanceH;
			else
				x = offset.left + $(overElement).width() - w - distanceH ;
				
			this.element.closest('.ui-dialog').css({
				top: y,
				left: x
			});
		}	
		else
			this._position(options.position);	
	},
	
	
	_addAffiliate: function()
	{
		var self = this,
			options = this.options;
		
		// and add affiliate link too
			if (options.affiliate && !this.element.find('.pp-affiliate').length)
			{
				try {
					var affiliate = $(options.affiliate)
						.appendTo(this.element);
				} 
				catch (e) {}
				
				if (affiliate.length)
				{
					affiliate.css({
						display: 'block',
						margin: '2px auto 0',						
						outline: 'none',
						'line-height': 'normal',
						fontSize: '11px',
						fontFamily: 'Verdana'
					});
				}
			}			
	},
	
	
	_addCloseBtn: function()
	{
		var self = this,
			options = this.options;
		
		// add close button, requires exact dialog width, that's why it is called from here
		if (options.closeBtn && !self.element.find('.pp-button-close').length)
		{				
			$('<button class="pp-button-close" type="button">Close</button>')
				.css({
					background: '#ddd',
					border: '1px solid ' + options.frameColor,
					margin: '2px auto 0',
					cursor: 'pointer',
					width: 50,
					fontSize: '11px',
					fontFamily: 'Verdana',
					padding: 2					
				})
				.click(
					function(e) { 
						self.close(e);
					}
				)
				.hover(
					function() {
						$(this)
							.css('background', '#fff')
							.addClass('ui-state-hover');
					},
					function() {
						$(this)
							.css('background', '#eee')
							.removeClass('ui-state-hover');
					}
				)
				.focus(function() {
					$(this).addClass('ui-state-focus');
				})
				.blur(function() {
					$(this).removeClass('ui-state-focus');
				})
				.appendTo(self.element);	
		}		
	}
      

}));

$.extend($.pp.popup, {
	version: "0.1",
	defaults: {
		autoOpen: false,
		bgiframe: false,
		buttons: {},
		closeOnEscape: true,
		closeText: 'close',
		dialogClass: '',
		draggable: false,
		hide: 'swing',
		height: 'auto',
		maxHeight: false,
		maxWidth: false,
		minHeight: 10,
		minWidth: 10,
		modal: false,
		position: 'center',
		resizable: false,
		show: null,
		stack: true,
		title: '',
		width: 300,
		zIndex: 1000,
		
		noTitle: false,
		background: null,
		frameSize: 0,
		frameColor: '#aaaaaa',
		borderRadius: 0,
		titleColor: '#000000',
		titleTextColor: '#aaaaaa',
		sticky: false,
		closeOnOuterClick: true,
		closeTimer: 0,
		closeBtn: false,
		removeOnClose: true,
		openOnUnload: false,
		delay: 0,
		effect: null,
		effectSpeed: 'fast',
		effectDirection: 'top',
		shadow: false,
		overlay: false,
		transparency: 0,
		contentPadding: 0,
		overElement: -1,
		distanceTopBottom: 'top',
		distanceV: 0,
		distanceRightLeft: 'left',
		distanceH: 0,
		affiliate: null
	},

	getter: 'isOpen',

	uuid: 0,
	maxZ: 0,

	getTitleId: function($el) {
		return 'ui-dialog-title-' + ($el.attr('id') || ++this.uuid);
	},

	overlay: function(dialog) {
		this.$el = $.ui.dialog.overlay.create(dialog);
	}
});


$('.pp-onclick').click(function()
{
	var me = jQuery(this),
		id = me.attr('id').replace(/^pp-link-/, ''),
		p = me.data('options'),
		w = $('#ppopup-' + id);
				
	if (w.length)
		w.popup('open');
	else
    	$(p.content)
    		.appendTo(document.body)
        	.popup(p.options);
		
});


$('.pp-onmouseover').bind('mouseenter mouseleave', function(e)  
{
	var me = jQuery(this),
		id = me.attr('id').replace(/^pp-link-/, ''),
		p = me.data('options'),
		w = $('#ppopup-' + id);
		
	if (p.options.overElement == 1)
		p.options.overElement = me.attr('id');	
	
	if ('mouseenter' == e.type)
	{
		if (w.length)
			w.popup('open');
		else
			$(p.content)
				.appendTo(document.body)
				.popup(p.options);
	}
	else
	{
		if (p.options.closeOnMouseLeave)
			w.popup('close');
	}
});
	


})(jQuery);