jQuery(document).ready(function(){
	clearInputs();
	jQuery('div.navigation').each(function(){
		if(jQuery(this).find('a').length == 0) jQuery(this).remove();
	});
	pageAjax();
});
/*--- page ajax ---*/
function pageAjax(){
	var _speed = 400; //in ms
	var _fader = jQuery('<div id="load-fader">Loading...</div>').css({display: 'none', opacity: 0.3}).appendTo('body');
	jQuery('ul.list-thumbnail').each(function(){
		var _a = false;
		var ajax_f = true;
		var _hold = jQuery(this);
		var _list = _hold.children();
		var hold_h = _hold.height();
		_hold.css('height', hold_h);
		
		_list.each(function(){
			var _el = jQuery(this);
			_el.data('t', _el.position().top);
			_el.data('l', _el.position().left);
		}).each(function(){
			var _el = jQuery(this);
			_el.css({position: 'absolute', top: _el.data('t'), left: _el.data('l')});
			_el.find('a').click(function(){
				showPost(_el);
				return false;
			});
		});
		
		function showPost(_btn){
			if(ajax_f){
				ajax_f = false;
				if(_a) _hide(_a);
				if(_btn.data('box')){
					ajax_f = true;
					_show(_btn);
				}
				else{
					_fader.css({'height': initH(), display:'block'});
					jQuery.ajax({
						url: _btn.find('a:eq(0)').attr('href'),
						dataType: 'html',
						type: 'POST',
						data: 'ajax=1',
						success: function(_html){
							_btn.data('box', jQuery(_html));
							_btn.data('box').css({display: 'none', opacity: 0}).appendTo('#main');
							inBox(_btn.data('box'));
							_btn.data('box').find('a.close').data('btn', _btn).click(function(){
								_hide(jQuery(this).data('btn'));
								return false;
							});
							ajax_f = true;
							
							var t_img = _btn.data('box').find('img');
							if(t_img.length == 0 || _btn.data('box').hasClass('large-box')){
								_fader.css('display', 'none');
								_show(_btn);
							}
							else{
								var i_k = t_img.length;
								for(var i = 0; i < t_img.length; i++){
									if(t_img.eq(i).attr('complete')) i_k--;
									else t_img.eq(i).load(function(){
										i_k--;
										if(i_k == 0){
											_fader.css('display', 'none');
											_show(_btn);
										}
									});
								}
								if(i_k == 0){
									_fader.css('display', 'none');
									_show(_btn);
								}
							}
						}
					});
				}
			}
		}
		function _show(_btn){
			_a = _btn;
			window.location.hash = _a.attr('id').split('post-')[1];
			var _box = _btn.data('box');
			var _h = _box.outerHeight();
			var _w = _box.outerWidth();
			var _t = 0;
			var _l = 0;
			if(_box.hasClass('large-box')){
				_l = 0;
				_t = 0;
				_box.css({top: _t, left: _l});
				
				_list.each(function(){
					var _el = jQuery(this);
					_el.animate({top: _el.data('t')+_h+12}, {duration: _speed, queue:false});
				});
				_hold.animate({height: hold_h+_h+12},{duration: _speed, queue:false});
			}
			else if(_box.hasClass('small-box')){
				_t = _btn.data('t');
				if(_btn.data('l') + _w >= _hold.outerWidth()) _l = _btn.data('l')+_btn.outerWidth()-_w;
				else _l = _btn.data('l');
				_box.css({top: _t, left: _l});
				
				_list.filter(function(){
					var _el = jQuery(this);
					if(_el.data('l') >= _l && _el.data('l') <= _l+_w && _el.data('t') > _btn.data('t')) return true;
				}).each(function(){
					var _el = jQuery(this);
					_el.animate({top: _el.data('t')+_h-_btn.outerHeight()}, {duration: _speed, queue:false});
				});
				_hold.animate({height: hold_h+_h-_btn.outerHeight()},{duration: _speed, queue:false});
			}
			if(jQuery.browser.msie) _box.stop().css({opacity: 'auto', display: 'block'});
			else _box.stop().css('display', 'block').animate({opacity: 1}, _speed);
		}
		function _hide(_btn){
			var _box = _btn.data('box');
			_a = false;
			window.location.hash = '0';
			if(jQuery.browser.msie) _box.stop().css({display: 'none'});
			else _box.stop().animate({opacity: 0}, _speed, function(){
				jQuery(this).css('display', 'none');
			});
			_hold.animate({height: hold_h},{duration: _speed, queue:false});
			_list.each(function(){
				var _el = jQuery(this);
				_el.animate({top: _el.data('t')},{queue: false, duration: _speed});
			});
		}
		if(window.location.hash && _list.filter('#post-'+window.location.hash.substr(1)).length){
			showPost(_list.filter('#post-'+window.location.hash.substr(1)));
		}
	});
}
function inBox(_box){
	var _speed = 500; //in ms
	_box.find('div.carousel').each(function(){
		var _hold = jQuery(this);
		var list_hold = _hold.find('div.carousel-box > ul');
		var _list = list_hold.find('> li');
		var btn_prev = _hold.find('a.link-prev');
		var btn_next = _hold.find('a.link-next');
		var _a = (_list.index(_list.filter('.active:eq(0)')) != -1)?(_list.index(_list.filter('.active:eq(0)'))):(0);
		
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		btn_prev.click(function(){
			if(_a > 0) changeEl(_a - 1);
			else changeEl(_list.length - 1);
			return false;
		});
		btn_next.click(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		function changeEl(_ind){
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0},{ queue:false, duration: _speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1},{ queue:false, duration: _speed});
				_a = _ind;
			}
		}
	});
	
}
function initH(){
	var _h = jQuery('div.page:eq(0)').outerHeight();
	if(_h < jQuery(window).height()) _h = jQuery(window).height();
	if(_h < jQuery('body').height()) _h = jQuery('body').height();
	return _h;
}
/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
