jQuery(function(){
	hideFormText();
	//ajaxPages();
	initAfterAjax();
	if(jQuery('#blogdiv').length){
		jQuery('#main-sidebar').hide();
		jQuery('#blog-sidebar').show();
	}else{
		jQuery('#main-sidebar').show();
		jQuery('#blog-sidebar').hide();
	}
});

function initAfterAjax(){
	jQuery('a.open-fancy').fancybox({
		padding : 10,
		titleShow : true,
		titlePosition : 'over',
		overlayShow : true,
		titleFromAlt: true,
		overlayOpacity : 0.7,
		overlayColor : '#fff'
	});
	jQuery('.post-content a:has("img")').fancybox({
		padding : 10,
		overlayShow : true,
		titleShow : true,
		titleFromAlt: true,
		titlePosition : 'over',
		overlayOpacity : 0.7,
		overlayColor : '#fff'
	});
	jQuery('a.open-fancy-vimeo').each(function(){
		jQuery(this).fancybox({
			padding : 10,
			overlayShow : true,
			overlayOpacity : 0.7,
			overlayColor : '#fff',
			'titleShow'     : false,
			'width'			: 400,
			'height'		: 265,
			'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
			'type'			: 'swf'
		});
	})
	jQuery('a.open-fancy-youtube').each(function(){
		jQuery(this).fancybox({
			padding : 10,
			overlayShow : true,
			overlayOpacity : 0.7,
			overlayColor : '#fff',
			'width'			: 400,
			'height'		: 265,
			'titleShow'     : false,
			'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'      : 'swf',
			'swf'       : {'wmode':'transparent','allowfullscreen':'true'}
		});
	});
}

function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];

	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				_inputs[i].onfocus = function(){
					if (this.value == this.defaultValue) this.value = '';
				};
				_inputs[i].onblur = function(){
					if (this.value == '') this.value = this.defaultValue;
				};
			};
		};
	};
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].onfocus = function(){
				if (this.value == this.defaultValue) this.value = '';
			};
			_txt[i].onblur = function(){
				if (this.value == '') this.value = this.defaultValue;
			};
		};
	};
};

function ajaxPages(){
	var _d = 300;
	var _aClass = 'current-menu-item current_page_item';
	jQuery('.ajax-pages').each(function(){
		var hold = jQuery(this);
		var nav = jQuery('#nav a, #bottom-menu a, .nav-pager a, ul.posts ul.sub-menu > li > a');
		var _curPage = hold.children();
		var _f = false;
		_curPage.attr('rel', window.location.href);
		nav.live('click',function(){
			if(!_f){
				if(hold.children().filter('[rel='+this.href+']').length && _curPage.get(0) == hold.children().filter('[rel='+this.href+']').get(0)){
					return false;
				}else{
					nav.parent().removeClass(_aClass);
					jQuery(this).parent().addClass(_aClass);
					jQuery('.nav-pager span').removeClass(_aClass);
					_f = true;
					getNewPage(this.href);
					//window.location.hash = '#page-'+this.title;
				};
			}
			return false;
		});
		jQuery('a.content-pointer').live('click',function(){
			if(!_f){
				if(hold.children().filter('[rel='+this.href+']').length && _curPage.get(0) == hold.children().filter('[rel='+this.href+']').get(0)){
					return false;
				}else{
					nav.parent().removeClass(_aClass);
					jQuery('[href='+this.href+']').not('.content-pointer').parent().addClass(_aClass);
					jQuery('.nav-pager span').removeClass(_aClass);
					_f = true;
					getNewPage(this.href);
					//window.location.hash = '#page-'+this.title;
				};
			};
			return false;
		});
/* ajax search */
		jQuery('#searchform').submit(function(){
			var _url = this.action+'/';
			var _form = this;
			try{
				if(hold.children().filter('[rel='+_url+'?ajax=page&'+jQuery(_form).serialize()+']').length  && _curPage.get(0) == hold.children().filter('[rel='+_url+'?ajax=page&'+jQuery(_form).serialize()+']').get(0)){
					return false;
				}else{
					if(hold.children().filter('[rel='+_url+'?ajax=page&'+jQuery(_form).serialize()+']').length){
						animateChange(hold.children().filter('[rel='+_url+'?ajax=page&'+jQuery(_form).serialize()+']'))
					}else{
						jQuery('body').append('<div id="fader"></div>');
						jQuery('#fader').css({
							'backgroundColor' : '#fff',
							'opacity': '0',
							'width': ((jQuery('#wrapper').width() > jQuery(window).width()) ? jQuery('#wrapper').width() : jQuery(window).width()),
							'height': ((jQuery('#wrapper').height() > jQuery(window).height()) ? jQuery('#wrapper').height() : jQuery(window).height()),
							'position': 'absolute',
							'top' : 0,
							'left' : 0,
							'zIndex' : 1000
						}).fadeTo(300, 0.7 ,function(){
							jQuery.ajax({
								url: _url,
								type:"GET",
								data: 'ajax=page&'+jQuery(_form).serialize(),
								success: function(html){
									var _content = jQuery(html).css({position: 'absolute',top: hold.outerHeight(true)});
									_content.attr('rel', _url+'?ajax=page&'+jQuery(_form).serialize());
									hold.append(_content);
									initCufon();
									setTimeout(function(){
										jQuery('#fader').fadeOut(300,function(){
											jQuery(this).remove();
											animateChange(_content);
											initAfterAjax();
										});
									},400);
								},
								error: function(){
									alert('Ajax error! File not found!');
								}
							})
						});
					}
				}
			}catch(err){};
			return false;
		})


		function getNewPage(_url){
			if(hold.children().filter('[rel='+_url+']').length){
				animateChange(hold.children().filter('[rel='+_url+']'))
			}else{
				jQuery('body').append('<div id="fader"></div>');
				jQuery('#fader').css({
					'backgroundColor' : '#fff',
					'opacity': '0',
					'width': ((jQuery('#wrapper').width() > jQuery(window).width()) ? jQuery('#wrapper').width() : jQuery(window).width()),
					'height': ((jQuery('#wrapper').height() > jQuery(window).height()) ? jQuery('#wrapper').height() : jQuery(window).height()),
					'position': 'absolute',
					'top' : 0,
					'left' : 0,
					'zIndex' : 1000
				}).fadeTo(300, 0.7 ,function(){
					jQuery.ajax({
						url: _url,
						type: 'POST',
						data: 'ajax=page',
						success: function(html){
							var _content = jQuery(html).css({position: 'absolute',top: hold.outerHeight(true)});
							_content.attr('rel', _url);
							hold.append(_content);
							initCufon();
							setTimeout(function(){
								jQuery('#fader').fadeOut(300,function(){
									jQuery(this).remove();
									animateChange(_content);
									initAfterAjax();
								});
							},200);
						},
						error: function(){
							alert('Ajax error! File not found!');
						}
					});
				});
			}
		};
		function animateChange(_content){
			jQuery('body, html').animate({scrollTop:0},{duration: _d});
			if(_content.find('#blogdiv').length){
				jQuery('#main-sidebar').fadeOut(300,function(){
					jQuery('#blog-sidebar').fadeIn(300,function(){});
				})
			}else{
				jQuery('#blog-sidebar').fadeOut(300,function(){
					jQuery('#main-sidebar').fadeIn(300,function(){});
				})
			}
			var _h = _curPage.outerHeight(true);
			var _newH = _content.children(':eq(0)').outerHeight(true);
			hold.css('height', _h);
			hold.animate({height: _newH},{duration: _d, easing: 'linear', complete: function(){/*complete*/}});
			_content.css({
				position: 'absolute',
				top: - hold.outerHeight(true)
			}).animate({top: 0},{duration: _d, easing: 'linear', complete: function(){
				_f = false;
				jQuery(this).css({position: 'relative'});
				hold.css('height','auto');
				_curPage = jQuery(this);
				cufonRefresh();
			}});
			_curPage.css({
				position: 'absolute',
				top:0
			}).animate({top: -_h},{duration: _d, easing: 'linear', complete: function(){/*complete*/}});
		};
	});
}
