jQuery(function(){
	initPlugins();
	initPopups();
});

function initPlugins(){
	//$('input:radio').customRadio();
	//$('input:checkbox').customCheckbox();
	jQuery('select').customSelect();
	jQuery('.gallery').each(function(){
		var holder = jQuery('.holder .frame', jQuery(this)),
			slides = holder.children().each(function(){
				jQuery(this).css({width: jQuery(this).width()});
			});
		holder.css({width: '99999px'});
	});
	jQuery('.gallery').Gallery({
		speed: 800,
		autoRotation: 10000,
		slider: '.holder .frame',
		list: '>div',
		pager: '.switcher-holder ul'
	});
	jQuery('.gallery-office').Gallery({
		speed: 800,
		autoRotation: 10000,
		slider: '.holder'
	});
};

jQuery.fn.Gallery = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		autoRotation: 4000,
		holder: '.holder',
		slider: 'ul.slider',
		list: '>li',
		prev: '.prev, .link-prev, .btn-prev',
		next: '.next, .link-next, .btn-next',
		pager: 'ul.switcher',
		dinamicPagination: true,
		vertical: false,
		infinitive: true,
		stopOnHover: true,
		play: '.play',
		pause: '.pause'
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _autoRotation = _options.autoRotation;
		var _slider = _hold.find(_options.slider);
		var _holder = _slider.parent();
		var _list = _slider.children();
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _play = _hold.find(_options.play);
		var _vert = _options.vertical;
		var _inf = _options.infinitive;
		var _stop = _options.stopOnHover;
		var _f = true;
		var _p = _options.dinamicPagination;
		if (_vert) var _d = _list.eq(0).outerHeight(true);
		else var _d = _list.eq(0).outerWidth(true);
/*--------ADDING SLIDES----------*/
		
		if (_vert) var _vis = Math.ceil(_holder.height()/_d);
		else var _vis = Math.ceil(_holder.width()/_d);
		if (_inf) {
			for	(var i=0; i < _vis; i++){
				_list.eq(i).clone().appendTo(_slider);
			};
		}
/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager);
		if(_p){	_num.empty();
			_list.each(function(i){
				$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
			});	};
		var _thumb = _num.find('a');
/*-------------------------------------*/
		
		var _a = _list.index(_list.filter('.active'));
		if(_a == -1) {_a = 0;
			_thumb.eq(_a).addClass('active');
			_list.eq(_a).addClass('active');
		}
		var _x=0, _new, _t;

		if (_f){ if (_autoRotation) Run(_a);}
		function Run(_a){
			_t = setTimeout(function(){
				if (_inf) {
					if (_a < _list.length){_a++
					}else {	_a = 1;	};
				}else{
					if (_a < _list.length-1){_a++
					}else {	_a = 0;	};
				}
				Slide(_a);
			}, _autoRotation);
		};
		function Slide(_new){
			_x = _new * _d;
			if (_inf) {if (_new == _list.length){_new=0;}}
			_a = _new;
			_list.removeClass('active').eq(_new).addClass('active');
			_thumb.removeClass('active').eq(_new).addClass('active');
			if (_vert){
				_slider.animate({top: -_x}, {queue:false, duration:_speed, easing:'linear', complete:function(){
					if (_inf) {if (_new == 0){_slider.css({top:0});}}
				}});
			}else{
				_slider.animate({left: -_x}, {queue:false, duration:_speed, easing:'linear', complete:function(){
					if (_inf) {if (_new == 0){_slider.css({left:0});}}
				}});
			}
			if(_t) clearTimeout(_t);
			if (_autoRotation) Run(_a);
		};
		if (_stop){
			_holder.mouseenter(function(){		_f = false; if(_t) clearTimeout(_t);})
					.mouseleave(function(){		_f = true; if (_autoRotation) Run(_a);});
		}

		_thumb.click(function(){
			_a = _thumb.index($(this));
			Slide(_a);
			return false;
		});

		_next.click(function(){
			if (_inf) {
				if (_a < _list.length){_a++
				}else {	_a = 1;	};
			}else{
				if (_a < _list.length-1){_a++
				}else {	_a = 0;	};
			}
			Slide(_a);
			return false;
		});
		
		_prev.click(function(){
			if (_a>0){_a--
			}else {
				if (_inf) {
					if (_vert) _slider.css({top:-_list.length*_d});
					else _slider.css({left:-_list.length*_d});
				}
				_a = _list.length - 1;
			};
			Slide(_a);
			return false;
		});
	});
};

function initGalleryDif(){
	var _speed = 400;
	$('.main-gallery').each(function(){
		var _this = $(this);
		var list = _this.find('.main-gallery-holder ul li'),
			slider = _this.find('.main-gallery-holder ul'),
			prev = _this.find('a.link-prev'),
			next = _this.find('a.link-next'),
			_max = list.length,
			w = _this.find('.main-gallery-holder').width(),
			_t,
			_margin = parseFloat(list.eq(_max-1).css('marginRight')),
			a = 0;
		var _w = [];
		list.each(function(){
			_w.push($(this).outerWidth(true));
		});
		var _vis = 0;
		var _visEl = 0;
		for (var i = _max-1; i > 0; i--){
			if (_vis + _w[i] - _margin <= w) {_visEl ++; _vis += _w[i]}
		}
		var _a = 0;
		list.removeClass('active').eq(_a).addClass('active');
		var _x=0, _new, _t;
		
		function Slide(_new){
			_x = 0;
			for (var i = 0; i < _new; i++){
				_x += _w[i];
			}
			_a = _new;
			list.removeClass('active').eq(_new).addClass('active');
			slider.animate({marginLeft: -_x}, {queue:false, duration:_speed, easing:'linear'});
		};

		next.click(function(){
			if (_a < _max - _visEl)_a++
			else _a = 0;
			Slide(_a);
			return false;
		});
		
		prev.click(function(){
			if (_a>0) _a--
			else _a = _max - _visEl;
			Slide(_a);
			return false;
		});
	})
}

// custom forms plugin
(function(jQuery){
	// custom checkboxes module
	jQuery.fn.customCheckbox = function(_options){
		var _options = jQuery.extend({
			checkboxStructure: '<div></div>',
			checkboxDisabled: 'disabled',
			checkboxDefault: 'checkboxArea',
			checkboxChecked: 'checkboxAreaChecked'
		}, _options);
		return this.each(function(){
			var checkbox = jQuery(this);
			if(!checkbox.hasClass('outtaHere') && checkbox.is(':checkbox')){
				var replaced = jQuery(_options.checkboxStructure);
				this._replaced = replaced;
				if(checkbox.is(':disabled')) replaced.addClass(_options.checkboxDisabled);
				else if(checkbox.is(':checked')) replaced.addClass(_options.checkboxChecked);
				else replaced.addClass(_options.checkboxDefault);

				replaced.click(function(){
					if(checkbox.is(':checked')) checkbox.removeAttr('checked');
					else checkbox.attr('checked', 'checked');
					changeCheckbox(checkbox);
				});
				checkbox.click(function(){
					changeCheckbox(checkbox);
				});
				replaced.insertBefore(checkbox);
				checkbox.addClass('outtaHere');
			}
		});
		function changeCheckbox(_this){
			_this.change();
			if(_this.is(':checked')) _this.get(0)._replaced.removeClass().addClass(_options.checkboxChecked);
			else _this.get(0)._replaced.removeClass().addClass(_options.checkboxDefault);
		}
	}

	// custom radios module
	jQuery.fn.customRadio = function(_options){
		var _options = jQuery.extend({
			radioStructure: '<div></div>',
			radioDisabled: 'disabled',
			radioDefault: 'radioArea',
			radioChecked: 'radioAreaChecked'
		}, _options);
		return this.each(function(){
			var radio = jQuery(this);
			if(!radio.hasClass('outtaHere') && radio.is(':radio')){
				var replaced = jQuery(_options.radioStructure);
				this._replaced = replaced;
				if(radio.is(':disabled')) replaced.addClass(_options.radioDisabled);
				else if(radio.is(':checked')) replaced.addClass(_options.radioChecked);
				else replaced.addClass(_options.radioDefault);
				replaced.click(function(){
					if($(this).hasClass(_options.radioDefault)){
						radio.attr('checked', 'checked');
						changeRadio(radio.get(0));
					}
				});
				radio.click(function(){
					changeRadio(this);
				});
				replaced.insertBefore(radio);
				radio.addClass('outtaHere');
			}
		});
		function changeRadio(_this){
			$(_this).change();
			$('input:radio[name='+$(_this).attr("name")+']').not(_this).each(function(){
				if(this._replaced && !$(this).is(':disabled')) this._replaced.removeClass().addClass(_options.radioDefault);
			});
			_this._replaced.removeClass().addClass(_options.radioChecked);
		}
	}

	// custom selects module
	jQuery.fn.customSelect = function(_options) {
		var _options = jQuery.extend({
			selectStructure: '<div class="selectArea"><span class="left"></span><span class="center"></span><a href="#" class="selectButton"></a><div class="disabled"></div></div>',
			hideOnMouseOut: false,
			copyClass: true,
			selectText: '.center',
			selectBtn: '.selectButton',
			selectDisabled: '.disabled',
			optStructure: '<div class="optionsDivVisible"><div class="select-top"></div><div class="select-center"><ul></ul><div class="select-bottom"></div></div>',
			optList: 'ul'
		}, _options);
		return this.each(function() {
			var select = jQuery(this);
			var _title = false;
			if(!select.hasClass('outtaHere')) {
				if(select.is(':visible')) {
					var hideOnMouseOut = _options.hideOnMouseOut;
					var copyClass = _options.copyClass;
					var replaced = jQuery(_options.selectStructure);
					var selectText = replaced.find(_options.selectText);
					var selectBtn = replaced.find(_options.selectBtn);
					var selectDisabled = replaced.find(_options.selectDisabled).hide();
					var optHolder = jQuery(_options.optStructure);
					var optList = optHolder.find(_options.optList);
					if(copyClass) optHolder.addClass('drop-'+select.attr('class'));

					if(select.attr('disabled')) selectDisabled.show();
					select.find('option').each(function(_ind){
						var selOpt = $(this);
						var _itemTitle = selOpt.attr('rel') || false;
						var _selectTitle = select.attr('title');
						var _opt = jQuery('<li'+(selOpt.hasClass('empty-option') ? ' class="empty-option" ' : '')+'>'+(_itemTitle ? '<strong class="subtitle">'+_itemTitle+'</strong>' : '')+'<a href="#">' + selOpt.html() + '</a></li>');
						if(selOpt.attr('selected')){
							selectText.html(selOpt.html());
							_title = true;
							if(selOpt.attr('selected') && _ind == 0){
								_title = false;
							}
							_opt.addClass('selected');
						}
						if(select.attr('title') && !_title){
							selectText.html(_selectTitle);
						}
						_opt.children('a').click(function(){
							optList.find('li').removeClass('selected');
							select.find('option').removeAttr('selected');
							$(this).parent().addClass('selected');
							selOpt.attr('selected', 'selected');
							selectText.html(selOpt.html());
							select.change();
							optHolder.hide();
							return false;
						});
						optList.append(_opt);
					});
					replaced.width(select.outerWidth());
					replaced.insertBefore(select);
					optHolder.css({
						width: select.outerWidth(),
						display: 'none',
						position: 'absolute'
					});
					jQuery(document.body).append(optHolder);

					var optTimer;
					replaced.hover(function() {
						if(optTimer) clearTimeout(optTimer);
					}, function() {
						if(hideOnMouseOut) {
							optTimer = setTimeout(function() {
								optHolder.hide();
							}, 200);
						}
					});
					optHolder.hover(function(){
						if(optTimer) clearTimeout(optTimer);
					}, function() {
						if(hideOnMouseOut) {
							optTimer = setTimeout(function() {
								optHolder.hide();
							}, 200);
						}
					});
					selectBtn.click(function() {
						if(optHolder.is(':visible')) {
							optHolder.hide();
						}
						else{
							if(_activeDrop) _activeDrop.hide();
							optHolder.children('ul').css({height:'auto', overflow:'hidden'});
							optHolder.css({
								top: replaced.offset().top + replaced.outerHeight(),
								left: replaced.offset().left,
								display: 'block'
							});
							if(optHolder.children('ul').height() > 200) optHolder.children('ul').css({height:200, overflow:'auto'});
							_activeDrop = optHolder;
						}
						return false;
					});
					select.addClass('outtaHere');
				}
			}
		});
	}

	// event handler on DOM ready
	var _activeDrop;
	jQuery(function(){
		jQuery('body').click(hideOptionsClick)
		jQuery(window).resize(hideOptions)
	});
	function hideOptions() {
		if(_activeDrop && _activeDrop.length) {
			_activeDrop.hide();
			_activeDrop = null;
		}
	}
	function hideOptionsClick(e) {
		if(_activeDrop && _activeDrop.length) {
			var f = false;
			$(e.target).parents().each(function(){
				if(this == _activeDrop) f=true;
			});
			if(!f) {
				_activeDrop.hide();
				_activeDrop = null;
			}
		}
	}
})(jQuery);

// popups function with cool ajax
function initPopups() {
	var _zIndex = 1000;
	var _fadeSpeed = 350;
	var _faderOpacity = 0.75;
	var _faderBackground = '#424242';
	var _faderId = 'lightbox-overlay';
	var _closeLink = 'a.btn-close, a.close, a.cancel';
	var _fader;
	var _lightbox = null;
	var _ajaxClass = 'ajax';
	var _openers = jQuery('a.has-popup');
	var _page = jQuery(document);
	var _minWidth = jQuery('body > div:eq(0)').outerWidth();
	var _scroll = false;

	// init popup fader
	_fader = jQuery('#'+_faderId);
	if(!_fader.length) {
		_fader = jQuery('<div />');
		_fader.attr('id',_faderId);
		jQuery('body').append(_fader);
	}
	_fader.css({
		opacity:_faderOpacity,
		backgroundColor:_faderBackground,
		position:'absolute',
		overflow:'hidden',
		display:'none',
		top:0,
		left:0,
		zIndex:_zIndex
	});

	// IE6 iframe fix
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		if(!_fader.children().length) {
			var _frame = jQuery('<iframe src="javascript:false" frameborder="0" scrolling="no" />');
			_frame.css({
				opacity:0,
				width:'100%',
				height:'100%'
			});
			var _frameOverlay = jQuery('<div>');
			_frameOverlay.css({
				top:0,
				left:0,
				zIndex:1,
				opacity:0,
				background:'#000',
				position:'absolute',
				width:'100%',
				height:'100%'
			});
			_fader.empty().append(_frame).append(_frameOverlay);
		}
	}

	// lightbox positioning function
	function positionLightbox() {
		if(_lightbox) {
			var _windowHeight = jQuery(window).height();
			var _windowWidth = jQuery(window).width();
			var _lightboxWidth = _lightbox.outerWidth();
			var _lightboxHeight = _lightbox.outerHeight();
			var _pageHeight = _page.height();

			if (_windowWidth < _minWidth) _fader.css('width',_minWidth);
				else _fader.css('width','100%');
			if (_windowHeight < _pageHeight) _fader.css('height',_pageHeight);
				else _fader.css('height',_windowHeight);

			_lightbox.css({
				position:'absolute',
				zIndex:(_zIndex+1)
			});

			// vertical position
			if (_windowHeight > _lightboxHeight) {
				if (jQuery.browser.msie && jQuery.browser.version < 7) {
					_lightbox.css({
						position:'absolute',
						top: parseInt(jQuery(window).scrollTop()) + (_windowHeight - _lightboxHeight) / 2
					});
				} else {
					_lightbox.css({
						position:'fixed',
						top: (_windowHeight - _lightboxHeight) / 2
					});
				}
			} else {
				var _faderHeight = _fader.height();
				if(_faderHeight < _lightboxHeight) _fader.css('height',_lightboxHeight);
				if (!_scroll) {
					if (_faderHeight - _lightboxHeight > parseInt(jQuery(window).scrollTop())) {
						_faderHeight = parseInt(jQuery(window).scrollTop())
						_scroll = _faderHeight;
					} else {
						_scroll = _faderHeight - _lightboxHeight;
					}
				}
				_lightbox.css({
					position:'absolute',
					top: _scroll
				});
			}

			// horizontal position
			if (jQuery(window).width() > _lightbox.outerWidth()) _lightbox.css({left:(jQuery(window).width() - _lightbox.outerWidth()) / 2, marginLeft: 0});
			else _lightbox.css({left: _minWidth / 2 - _lightbox.outerWidth() / 2, marginLeft: -jQuery(window).scrollLeft()});
		}
	}

	// show/hide lightbox
	function toggleState(_state) {
		if(!_lightbox) return;
		if(_state) {
			_fader.fadeIn(_fadeSpeed,function(){
				_lightbox.fadeIn(_fadeSpeed);
			});
			_scroll = false;
			positionLightbox();
		} else {
			_lightbox.fadeOut(_fadeSpeed,function(){
				_fader.fadeOut(_fadeSpeed);
				_scroll = false;
			});
		}
	}

	// popup actions
	function initPopupActions(_obj) {
		if(!_obj.get(0).jsInit) {
			_obj.get(0).jsInit = true;
			// close link
			_obj.find(_closeLink).click(function(){
				_lightbox = _obj;
				toggleState(false);
				return false;
			});
		}
	}

	// lightbox openers
	_openers.each(function(){
		var _opener = jQuery(this);
		var _target = _opener.attr('rel');
		var _source = _opener.attr('href');
		if(_source == '#'){_source = false;}

		// popup load type - ajax or static
		if(_opener.hasClass(_ajaxClass)) {
			_opener.click(function(){
				// ajax load
				_lightbox = jQuery(_target);
				
				getContent();
				
				initPopupActions(_lightbox);
				
				function getContent(_url, _type){
					var _path = !_url ? _source : _url;
					if(_path == '#'){return;}
					
					if(jQuery('div[rel*="'+_path+'"]').length == 0){
						jQuery.ajax({
							url: _path,
							type: 'post',
							dataType:'html',
							success: function(respand){
								// append loaded popup
								_lightbox.find('div.text').find('div.block').hide();
								var _block = jQuery('<div class="block" rel="'+_path+'"></div>').html(respand).appendTo(_lightbox.find('div.holder'));
								// show lightbox
								toggleState(true);
							},
							error: function(msg){
								alert('AJAX error!');
								return false;
							}
						});
					} else {
						_lightbox.find('div.text').find('div.block').hide();
						if(_path){
							_lightbox.find('div.text').find('div.block[rel*="'+_path+'"]').show();
						}else{
							_lightbox.find('div.text').find('div.block[!rel]').show();
						}
						toggleState(true);
					}
				}
				
				return false;
			});
		} else {
			if(jQuery(_target).length){
				// init actions for popup
				var _popup = jQuery(_target);
				initPopupActions(_popup);
					// open popup
					_opener.click(function(){
					_popup.find('div.text').find('div.block').show().filter('[rel]').hide();
					if(_lightbox){
						_lightbox.fadeOut(_fadeSpeed,function(){
							_lightbox = _popup.hide();
							toggleState(true);
						});
					} else{
						_lightbox = _popup.hide();
						toggleState(true);
					}
					return false;
				});
			}
		}
	});

	// event handlers
	jQuery(window).resize(positionLightbox);
	jQuery(window).scroll(positionLightbox);
	jQuery(document).keydown(function (e) {
		if (!e) evt = window.event;
		if (e.keyCode == 27) {
			toggleState(false);
		}
	})
	_fader.click(function(){
		if(!_fader.is(':animated')) toggleState(false);
		return false;
	})
}

// popups function
function initPopups() {
	var _zIndex = 1000;
	var _fadeSpeed = 350;
	var _faderOpacity = 0.65;
	var _faderBackground = '#000';
	var _faderId = 'lightbox-overlay';
	var _closeLink = 'a.btn-close, a.close, a.cancel';
	var _fader;
	var _lightbox = null;
	var _ajaxClass = 'ajax';
	var _openers = jQuery('a.has-popup');
	var _page = jQuery(document);
	var _minWidth = jQuery('body').outerWidth();
	var _scroll = false;
	var _ie = true;

	// init popup fader
	_fader = jQuery('#'+_faderId);
	if(!_fader.length) {
		_fader = jQuery('<div />');
		_fader.attr('id',_faderId);
		jQuery('body').append(_fader);
	}
	_fader.css({
		opacity:_faderOpacity,
		backgroundColor:_faderBackground,
		position:'absolute',
		overflow:'hidden',
		display:'none',
		top:0,
		left:0,
		zIndex:_zIndex
	});

	// IE6 iframe fix
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		if(!_fader.children().length) {
			var _frame = jQuery('<iframe src="javascript:false" frameborder="0" scrolling="no" />');
			_frame.css({
				opacity:0,
				width:'100%',
				height:'100%'
			});
			var _frameOverlay = jQuery('<div>');
			_frameOverlay.css({
				top:0,
				left:0,
				zIndex:1,
				opacity:0,
				background:'#000',
				position:'absolute',
				width:'100%',
				height:'100%'
			});
			_fader.empty().append(_frame).append(_frameOverlay);
		}
	}

	// lightbox positioning function
	function positionLightbox() {
		if(_lightbox) {
			_minWidth = jQuery('body').outerWidth();
			var _windowHeight = jQuery(window).height();
			var _windowWidth = jQuery(window).width();
			var _lightboxWidth = _lightbox.outerWidth();
			var _lightboxHeight = _lightbox.outerHeight();
			var _pageHeight = _page.height();

			if (_windowWidth < _minWidth) _fader.css('width',_minWidth);
				else _fader.css('width','100%');
			if (_windowHeight < _pageHeight) _fader.css('height',_pageHeight);
				else _fader.css('height',_windowHeight);

			_lightbox.css({
				position:'absolute',
				zIndex:(_zIndex+1)
			});

			// vertical position
			if (_windowHeight > _lightboxHeight) {
				if (jQuery.browser.msie && jQuery.browser.version < 7) {
					_lightbox.css({
						position:'absolute',
						top: parseInt(jQuery(window).scrollTop()) + (_windowHeight - _lightboxHeight) / 2
					});
				} else {
					_lightbox.css({
						position:'fixed',
						top: (_windowHeight - _lightboxHeight) / 2
					});
				}
			} else {
				var _faderHeight = _fader.height();
				if(_faderHeight < _lightboxHeight) _fader.css('height',_lightboxHeight);
				if (!_scroll) {
					if (_faderHeight - _lightboxHeight > parseInt(jQuery(window).scrollTop())) {
						_faderHeight = parseInt(jQuery(window).scrollTop())
						_scroll = _faderHeight;
					} else {
						_scroll = _faderHeight - _lightboxHeight;
					}
				}
				_lightbox.css({
					position:'absolute',
					top: _scroll
				});
			}

			// horizontal position
			if (jQuery(window).width() > _lightbox.outerWidth()) _lightbox.css({left:(jQuery(window).width() - _lightbox.outerWidth()) / 2, marginLeft: 0});
			else _lightbox.css({left: _minWidth / 2 - _lightbox.outerWidth() / 2, marginLeft: -jQuery(window).scrollLeft()});
		}
	}

	// show/hide lightbox
	function toggleState(_state) {
		if(!_lightbox) return;
		if(_state) {
			if(_ie && jQuery.browser.msie){
				_fader.show();
				_lightbox.show()
			}else{
				_fader.fadeIn(_fadeSpeed,function(){
					_lightbox.fadeIn(_fadeSpeed);
				});
			}
			_scroll = false;
			positionLightbox();
		} else {
			if(_ie && jQuery.browser.msie){
				_lightbox.hide();
				_fader.hide();
			}else{
				_lightbox.fadeOut(_fadeSpeed,function(){
					_fader.fadeOut(_fadeSpeed);
					_scroll = false;
				});
			}
		}
	}

	// popup actions
	function initPopupActions(_obj) {
		if(!_obj.get(0).jsInit) {
			_obj.get(0).jsInit = true;
			// close link
			_obj.find(_closeLink).click(function(){
				_lightbox = _obj;
				toggleState(false);
				return false;
			});
		}
	}

	// lightbox openers
	_openers.each(function(){
		var _opener = jQuery(this);
		var _target = _opener.attr('href');

		// popup load type - ajax or static
		if(_opener.hasClass(_ajaxClass)) {
			_opener.click(function(){
				// ajax load
				if(jQuery('div[rel*="'+_target+'"]').length == 0) {
					jQuery.ajax({
						url: _target,
						type: "POST",
						dataType: "html",
						success: function(msg){
							// append loaded popup
							_lightbox = jQuery(msg);
							_lightbox.find('img').load(positionLightbox)
							_lightbox.attr('rel',_target).hide().css({
								position:'absolute',
								zIndex:(_zIndex+1),
								top: -9999,
								left: -9999
							});
							jQuery('body').append(_lightbox);

							// init js for lightbox
							initPopupActions(_lightbox);

							// show lightbox
							toggleState(true);
						},
						error: function(msg){
							alert('AJAX error!');
							return false;
						}
					});
				} else {
					_lightbox = jQuery('div[rel*="'+_target+'"]');
					toggleState(true);
				}
				return false;
			});
		} else {
			if(jQuery(_target).length) {
				// init actions for popup
				var _popup = jQuery(_target);
				initPopupActions(_popup);
					// open popup
					_opener.click(function(){
					if(_lightbox) {
						_lightbox.fadeOut(_fadeSpeed,function(){
							_lightbox = _popup.hide();
							toggleState(true);
						})
					} else {
						_lightbox = _popup.hide();
						toggleState(true);
					}
					return false;
				});
			}
		}
	});

	// event handlers
	jQuery(window).resize(positionLightbox);
	jQuery(window).scroll(positionLightbox);
	jQuery(document).keydown(function (e) {
		if (!e) evt = window.event;
		if (e.keyCode == 27) {
			toggleState(false);
		}
	})
	_fader.click(function(){
		if(!_fader.is(':animated')) toggleState(false);
		return false;
	})
}

