/*---- popups ---*/
function initPopups(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _fader = $('#fader');
	_fader.hide();
	
	var _popup = false;
	
	$('a.with-popup').each(function(){
		var _el = this;
		if(_el.hash && _el.hash.length > 1){
			_el._popup = $(_el.hash);
			if(_el._popup.length){
				_el.onclick = function(){
					if(_el._popup){
						_popup = _el._popup;
						showPopup();
					}
					return false;
				}
				_el._popup.find('a.close').click(function(){
					closePopup();
					return false;
				});
			}
		}
	});
	_fader.click(function(){
		closePopup();
		return false;
	});
	$(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27) closePopup();
	});
	/*--- close popup ---*/
	function closePopup(){
		if(_popup){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(100);
				_popup = false;
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = false;
				});
			}
		}
	}
	
	/*--- show popup ---*/
	function showPopup(){
		
		_fader.css({
			opacity: 0,
			height: initH(),
			display:'block'
		});
		_popup.css({left:(((document.getElementById("inner-page").offsetWidth) - _popup.outerWidth()) / 2 + 10) + "px"});
		_fader.fadeTo(400, 0.5, function(){
			if(_popup){
				if($.browser.msie) _popup.show();
				else _popup.fadeIn(200, function(){
					if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				});
				_popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top)
				
			}
		});
		$(window).resize(function(){
		var _height = 0;
		var _width = 0;
		if (window.innerHeight) {
			_height = window.innerHeight;
			_width = document.getElementById("inner-page").offsetWidth;
		}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.getElementById("inner-page").offsetWidth;
		}
			
			_popup.css({top: ((_height - _popup.outerHeight())/2) + $(window).scrollTop()});
			_popup.css({left:((_width - _popup.outerWidth()) / 2 + 10) + "px"});
		});
		
		$(window).scroll(function(){
			var _height = 0;
			var _width = 0;
			if (window.innerHeight) {
				_height = window.innerHeight;
				_width = document.getElementById("inner-page").offsetWidth;
			}
			else {
				_height = document.documentElement.clientHeight;
				_width = document.getElementById("inner-page").offsetWidth;
			}
			
			_popup.css({top: ((_height - _popup.outerHeight())/2) + $(window).scrollTop()});
			_popup.css({left:((_width - _popup.outerWidth()) / 2 + 10) + "px"});
		});
		
	}
	/*--- get height ---*/
	function initH(){
		var _h = $('#content').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('body').height()) _h = $('body').height();
		return _h;
	}
}
/*--- gallery function ---*/
function initGall(){
	var change_speed = 500; //in ms
	$('div.lightbox div.gallery-hold').each(function(){
		var _hold = $(this);
		var img_hold = _hold.find('div.images-view');
		var _btn = _hold.find('ul.gallery-list a');
		var _a = (_btn.index(_btn.filter('.active:eq(0)')) != -1)?(_btn.index(_btn.filter('.active:eq(0)'))):(0);
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.eq(_a).data('img', $('<img src="'+_btn.eq(_a).attr('href')+'" alt="'+_btn.eq(_a).attr('rel')+'"/>'));
		_btn.eq(_a).data('img').addClass('active').css('opacity', 1).appendTo(img_hold);
		
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		var fade_f = true;
		function changeEl(_ind){
			if(fade_f && _ind != _a){
				fade_f = false;
				img_hold.stop().height(img_hold.height());
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_btn.eq(_a).data('img').removeClass('active').animate({opacity: 0}, {queue:false, duration: change_speed});
				if(_btn.eq(_ind).data('img')){
					_btn.eq(_ind).data('img').addClass('active').animate({opacity: 1}, {queue:false, duration: change_speed});
					img_hold.animate({height: _btn.eq(_ind).data('img').height()}, change_speed/2, function(){ $(this).height('auto');});
					_a = _ind;
					fade_f = true;
				}
				else{
					_btn.eq(_ind).data('img', $('<img />'));
					_btn.eq(_ind).data('img').load(function(){
						_btn.eq(_ind).data('img').addClass('active').animate({opacity: 1}, {queue:false, duration: change_speed});
						img_hold.animate({height: _btn.eq(_ind).data('img').height()}, change_speed/2, function(){ $(this).height('auto');});
						_a = _ind;
						fade_f = true;
					});
					_btn.eq(_ind).data('img').css('opacity', 0);
					img_hold.append(_btn.eq(_ind).data('img'));
					_btn.eq(_ind).data('img').attr('src', _btn.eq(_ind).attr('href')).attr('alt', _btn.eq(_ind).attr('rel'));
				}
			}
		}
	});
}
$(document).ready(function(){
	initPopups();
	initGall();
});
