jQuery(document).ready(function(){
	pageRefresh();
	initExternalLinks();
	initTabs();
	initImagesBorder();
});

function pageRefresh(){
	if(window.location=='http://thedirty.com/'){
		window.setTimeout('window.location.reload()',600000);
	}
}

function initTabs(){
	jQuery('.tabset').each(function(index, tabset){
		var _tabs = [];
		jQuery('a.tab', tabset).each(function(i, tabLink){
			_tabs[i] =  jQuery(jQuery(tabLink).attr('href'));
			
			if (!jQuery(tabLink).is('.active')) _tabs[i].hide();
			
			jQuery(tabLink).click(function(){
				jQuery(this).parents('.tabset').find('a').removeClass('active');
				jQuery(this).addClass('active');
				
				for (k in _tabs) {
					var old_height = jQuery(this).parents('.tabs').find('.tab-content').height();
					jQuery(this).parents('.tabs').find('.tab-content').height(old_height);
					_tabs[k].hide();
				}
				
				//Calculating next block height
				jQuery(jQuery(this).attr('href')).width(242);
				var new_height = jQuery(jQuery(this).attr('href')).height() + 20;

				jQuery(this).parents('.tabs').find('.tab-content').animate({height: new_height + "px"}, 1000);
				jQuery(jQuery(this).attr('href')).show();
				
				return false;
			});
		});
	});	
}

function initExternalLinks(){
	jQuery('a[rel*="external"]').attr('target','_blank');
	
	jQuery('a[rel*="popup"]').click(function(link) {
		window.open(jQuery(this).attr('href'),'_blank','resizable=yes,titlebar=no,location=no,status=no,menubar=no,toolbar=no');
		return false;
	});
}

function initImagesBorder(){
	jQuery('#content .post p img').each(function(){
		if ( jQuery(this).parent().is('p') ) {
			jQuery(this).wrap(document.createElement("div"));
			jQuery(this).parent('div').width(8 + jQuery(this).width());
			jQuery(this).parent('div').addClass('img-wrapper');
		} else {
			jQuery(this).parent('a').wrap(document.createElement("div"));
			jQuery(this).parent('a').parent('div').width(8 + jQuery(this).width());
			jQuery(this).parent('a').parent('div').addClass('img-wrapper');
		};
	});
	jQuery("#content .post").each(function(){
		if ( parseInt( jQuery(this).find('p:first .img-wrapper img').attr('width') ) > 533 ) {
			jQuery(this).find('p:first .img-wrapper img').attr('width', '533');
			jQuery(this).find('p:first .img-wrapper').css('width', '541px');
		}
	});
}

/*
if (window.addEventListener)
	window.addEventListener("load", initTabs, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initTabs);
*/