var arrColors = ['orange', 'yellow', 'red', 'green', 'blue'];
var iSlideShowFadeTime = 200;
var iSlideShowSlideTime = switchTime;
var oTimerInstance;

$(document).ready(function(){
	LoadCufon();
	LoadSlideshows();
	LoadBalloons();
	CorrectColumnHeights();
	
	$('.slide-show .slide .inner').hide();
	$('.slide-show .slide .inner h2').hide();
	$('.slide-show .orange .inner').show();
	$('.slide-show .orange .inner h2').show();
	
	$('#main-menu li').mouseenter(function(){
		var szColor = $(this).GetColorClass();
		if (szColor.length > 0) {
			var oColorSlide = $('.slide-show .slide.'+szColor).first();
			if (oColorSlide.length > 0) {
				if (!oColorSlide.is(':visible')) {
					$('.slide-show .slide').hide();
					$('.slide-show .slide .inner').hide();
					window.setTimeout(function(){ $('.slide-show .slide').hide(); }, 80);
					window.setTimeout(function(){ $('.slide-show .slide .inner').hide(); }, 80);
					window.setTimeout(function(){ oColorSlide.fadeIn(iSlideShowFadeTime); }, 100);
					window.setTimeout(function(){ $('.slide-show .slide .inner').show(); }, 100);
					window.setTimeout(function(){ $('.slide-show .slide .inner h2').show(); }, 100);
					//oColorSlide.fadeIn(iSlideShowFadeTime);
				}
				window.clearTimeout(oTimerInstance);
			}
		}
	});
	
	$('#search-text').focus(function(){ if($(this).val() == $(this).attr('rel-defval')) $(this).val(''); });
	$('#search-text').blur(function(){ if($(this).val().length == 0) $(this).val($(this).attr('rel-defval')); });
	$('#search-text').keyup(function(event){ if (event.keyCode == '13') $('#search-submit').click(); });
	$('#search-submit').click(function(){ document.location='/?id=59&action=search&q=' + $('#search-text').val(); });
});

function CorrectColumnHeights() {
	var iMaxColumnHeight = $('#left-column').height();
	if ($('#main-column').height() > iMaxColumnHeight)
		iMaxColumnHeight = $('#main-column').height();
	
	var iBlockHeightCorrection = 0;
	var iLastBlockHeight = 0;
	if ($('#left-column').children().length > 1) {
		$('#left-column').children().each(function(){
			iLastBlockHeight = $(this).height() + 24;
			iBlockHeightCorrection += iLastBlockHeight;
		});
		if (iBlockHeightCorrection > iLastBlockHeight)
			iBlockHeightCorrection -= iLastBlockHeight;
	}
	
	var iTotalHeightLeftBlocks = $('#left-column').height() - $('#left-column').children().last().height();
	$('#left-column').children().last().height(iMaxColumnHeight - iBlockHeightCorrection - 24);
	
	if ($('#main-column .block').length > 1) {
		$('#main-column .block').each(function(){
			$(this).height(iMaxColumnHeight - $('#main-column .moodbar').height() - 24);
		});
	}
}

function LoadCufon() {
	Cufon.replace('#main-menu li a', {
		hover:true,
		fontFamily:'vinceHand II', 
		fontSize:'18px', 
		onAfterReplace:function(el){
			$(el).css('padding-top','5px');
			$(el).parent().parent().css('margin-right', '6px');
		}
	});
	Cufon.replace('ul.sub-sub-menu li a', { hover:true, fontFamily:'Myriad Pro' });
	Cufon.replace('ul.sub-menu li a', { hover:true, fontFamily:'Myriad Pro' });
	Cufon.replace('h1', {fontFamily:'Myriad Pro', fontSize:'24px'});
	Cufon.replace('h2', {fontFamily:'vinceHand II', fontSize:'28px'});
	Cufon.replace('h3', {fontFamily:'Myriad Pro', fontSize:'16px'});
}

function LoadBalloons() {
	$('.balloon').each(function(){
		var szSize = '';
		var szColor = $(this).GetColorClass();
		
		if ($(this).text().length >= 16)
			szSize = 'large';
		else if ($(this).text().length >= 10)
			szSize = 'medium';
		else
			szSize = 'small';
		
		var szClassName = 'balloon-' + szColor + '-' + szSize;
		$(this).addClass(szClassName);
	});
}

function LoadSlideshows() {
	$('.slide-show').each(function(){
		var oSlideShow = $(this);
		if (oSlideShow.find('.slide').length > 1) {
			oSlideShow.find('.slide').hide();
			SlideShow(oSlideShow, oSlideShow.find('.slide').last());
		}
	});
}

function SlideShow(oSlideShow, oCurrentSlide) {
	$('.slide-show .slide .inner').show();
	$('.slide-show .slide .inner h2').show();
	if (oCurrentSlide == null)
		oCurrentSlide = oSlideShow.find('.slide').first();
	var oNextSlide = oCurrentSlide.next();
	if ((oNextSlide == null) || (oNextSlide.length == 0))
		oNextSlide = oSlideShow.find('.slide').first();
	oCurrentSlide.fadeOut(iSlideShowFadeTime, function(){
		oNextSlide.fadeIn(iSlideShowFadeTime, function(){
			oTimerInstance = window.setTimeout(function(){ SlideShow(oSlideShow, oNextSlide); }, iSlideShowSlideTime);
		});
	});
}

jQuery.fn.GetColorClass = function() {
	for (var i = 0; i < arrColors.length; i++)
		if ($(this).hasClass(arrColors[i]))
			return arrColors[i];
	return '';
}
