/*
 * jQuery simpleLightbox v1.2.0 
 *
 * Copyright (c) 2008 Taranets Aleksey
 * www: markup-javascript.com
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery.fn.simpleLightbox = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0,
		faderBackground: '#000000',
		closeLink:'a.close',
		href:true,
		ajax:false,
		loadEl:'selector',
		onClick: null
	},_options);

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		_this.lightboxContentBlock = false;
		if (!_options.href) {
			if (!_options.ajax) {
				_this.lightboxContentBlock = _options.lightboxContentBlock;
			} 
		}
		else _this.lightboxContentBlock = _this.attr('href');
		if (_this.lightboxContentBlock.length > 1 || _options.ajax) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = _options.ajax ? false : jQuery(_this.lightboxContentBlock);
			if (!jQuery('div.lightbox-fader').length)
				_fader = jQuery('body').append('<div class="lightbox-fader"></div>');
			
			_fader = jQuery('div.lightbox-fader');
			_fader.css({
				opacity:_this.faderOpacity,
				backgroundColor:_this.faderBackground,
				display:'none',
				position:'absolute',
				top:0,
				left:0,
				zIndex:998,
				textIndent: -9999
			}).text('&nbsp');
			
			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}
				if (_options.ajax && !_lightbox) {
					var _url = jQuery(this).attr('href');
					jQuery(_options.loadEl).show();
					jQuery.ajax({
						url: _url,
						success: function(msg){
							_lightbox = jQuery(msg);
							_lightbox.css('position','absolute');
							jQuery('body').append(_lightbox);
							_lightbox.hide();
							_fader.fadeIn(300, function(){
								_lightbox.fadeIn(400);
								jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
							});
							jQuery(_options.loadEl).hide();
							
							selectForm ();
							
							jQuery(_this.closeLink).click(function(){
								_lightbox.fadeOut(400, function(){
									_fader.fadeOut(300);
								});
								return false;
							});
						}
					});
				} else {
					_fader.fadeIn(300, function(){
						_lightbox.fadeIn(400);
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					});
				}
				return false;
			});
			
			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if (_lbox) {
					var _height = 0;
					var _width = 0;
					_lbox.css('zIndex',999);
					var _minWidth = jQuery('body > div:eq(0)').outerWidth();
					if (window.innerHeight) {
						_height = window.innerHeight;
						_width = window.innerWidth;
					} else {
						_height = document.documentElement.clientHeight;
						_width = document.documentElement.clientWidth;
					}
					var _thisHeight = _lbox.outerHeight();
					var _page = jQuery('body');
					if (_lbox.length) {
						if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
						if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
						if (_height > _thisHeight) {
							if (!window.innerHeight) {
								_lbox.css({
									position:'absolute',
									top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
								});
							} else {
								_lbox.css({
									position:'fixed',
									top: ((_height - _lbox.outerHeight()) / 2)+"px"
								});
							}
						}
						else {
							_lbox.css({
								position:'absolute',
								top: 0
							});
						}
						if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + "px"});
						else _lbox.css({position:'absolute',left: 0});
					}
				}
			}
			
			jQuery(window).resize(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			
			//jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			jQuery(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(400, function(){
						_fader.fadeOut(300);
					});
				}
			});
		}
	});
}

var Jq = jQuery.noConflict();
Jq(document).ready(function(){
	Jq('a.open').simpleLightbox({
		href:false,
		ajax:true	
	});
});
function selectForm () {
	Jq('div.popup div.right-part select').change(function(){
		var _url = Jq(this).val();
		if (_url != 'false') {
			var _formHolder = Jq('div.add-box', 'div.popup');
			var _title = Jq('h2', 'div.popup');
			_formHolder.fadeOut(200, function(){
				_title.html(_url.substr(_url.indexOf('?')+1));
				Jq.ajax({
					url: _url,
					success: function(msg){
						_formHolder.html(msg);
						_formHolder.fadeIn(300);
					}
				});
			});
		}
		return false;
	});	
}



	