// HELPER FUNCTIONS

function changeClass(array, e, active, inactive, level) {
	if(active == '')
	{
		var active = 'active';
	}
	if(inactive == '')
	{
		var inactive = 'inactive';
	}
	
	for( var i = 0; i < array.length; i++ )
	{
		if(level == '' || level == 0)	// Element at same level ...
		{
			var elem = array[i];	
		}
		else if(level > 0)				// Element at given level (above) ...
		{
			var elem = array[i].up(level-1);
		}
		

		Element.extend(elem);
		
		if(array[i] == e)				// Set the active one ...
		{
			if($(elem).hasClassName(inactive))
			{
				$(elem).removeClassName(inactive);
			}
			$(elem).addClassName(active);
		}
		else							// Set all the others inactive ...
		{
			if($(elem).hasClassName(active))
			{
				$(elem).removeClassName(active);
			}
			$(elem).addClassName(inactive);
		}
	}
}

// TEXT RESIZING IN ARTICLES

if($('text-size'))
{
	var textSizeTriggers = $$('#text-size a');
	textSizeTriggers.each(function(trigger){
		Event.observe(trigger, 'click', function(){
			switch(this.id)
			{
				case 'text-size-small':
					$('article').setStyle({fontSize: '0.8em'});
					break;
				case 'text-size-medium':
				default:
					$('article').setStyle({fontSize: '1em'});
					break;
				case 'text-size-large':
					$('article').setStyle({fontSize: '1.2em'});
					break;
			}
			changeClass(textSizeTriggers, this, '', '', 0);
		});
	});
}



// SHARE BOX

if($('share'))
{
	// Set increasing z-index...
	var zIndexInc = 99990;
	var actionItems = $('actions').childElements();
	actionItems.each(function(actionItem){
		actionItem.setStyle({
			zIndex: zIndexInc
		});
		zIndexInc++;
	});
	
	// Determine lists height
	var actionList = $('actions');
	var actionListHeight = actionList.offsetHeight - 29;
	actionList.setStyle({
		height: actionListHeight + 'px'
	});
	
	// Variablize some elements...
	var shareWrap = $('share');
	var shareTrigger = $$('#share a')[0];
	var nextFunction = $('comments-link');
	var thisWidth = $(shareWrap).offsetWidth;
	var shareLinksList = $$('#share ul')[0];
	
	// Set blank targets...
	var shareLinks = $$('#share ul li a');
	shareLinks.each(function(shareLink){
		shareLink.setAttribute('target', '_blank');
	});
	
	Event.observe(shareWrap, 'mouseover', function(){
		$(shareWrap).absolutize();
		
		$(shareWrap).setStyle({
			height: '106px',
			width: '231px'
		});
		$(nextFunction).setStyle({
			left: thisWidth + 'px',
			marginRight: thisWidth + 'px'
		});
		$(shareTrigger).setStyle({
			background: 'none'
		});
		$(shareLinksList).setStyle({
			display: 'block'
		});
	});
	
	Event.observe(shareWrap, 'mouseout', function(){
		$(shareLinksList).setStyle({
			display: 'none'
		});
		$(shareWrap).setStyle({
			position: 'relative',
			height: 'auto',
			width: 'auto',
			top: '-6px',
			left: '0',
			paddingTop: '0'
		});
		$(nextFunction).setStyle({
			left: '0',
			marginRight: '0'
		});
		$(shareTrigger).setStyle({
			//background: '#FFF url(/sitefiles/1/gfx/article-functions/share.png) 9px 7px no-repeat'
			background: 'url(/sitefiles/1/gfx/share-mask.gif) 0 0 no-repeat'
		});
	});
}



// AUTO ADJUSTING BOX

document.observe('dom:loaded', function(){
	if($('auto-adjusting-box'))
	{
		var mainColHeight = $('main-col').offsetHeight;
		var sideColHeight = $('side-col').offsetHeight;
		var boxHeight = $('taste-life').offsetHeight;
		if(mainColHeight > sideColHeight)
		{
			var diff = mainColHeight - sideColHeight;
			var newHeight = (diff + boxHeight) + 10;
			if($$('#article.recipe')[0])
			{
				newHeight -= 7;
			}
			else if(!$($$('#article.recipe')[0]) && !$($$('#article.normal')[0]))
			{
				newHeight -= 2;
			}
			else 
			{
				newHeight -= 9;
			}
		}
		
		$('taste-life').setStyle({
			height: newHeight + 'px'
		});
		
		if(newHeight > 280)
		{
			$($$('#taste-life .deco')[0]).setStyle({
				bottom: '0',
				top: 'auto'
			});
		}
		
	}
});



// CHANGE BACKGROUND IMAGE

function changeBackground(image){
	$($$('body')[0]).setStyle({
		backgroundImage: 'url('+image+')'
	});	
}



// FIX SEARCH LABEL BUTTON IN LESS COMPITABLE BROWSERS

Event.observe(window, 'load', function(){

	if($('global-search'))
	{
		/*
		var labelSubmit = $($$('#global-search label.search-submit')[0]);
		console.log(labelSubmit);
		var theForm = $$('#global-search form')[0];
		Event.observe(labelSubmit, 'click', function(){
			$(theForm).submit();
		});
		*/
	}
});


function __activatePrint(sheets, sheet)
{
	var myHead = $($$('head')[0]);
	if( sheets.length > 0 )
	{
		for( var i = 0; i < sheets.length; i++ )
		{
			var printSheet = sheets[i];
			myHead.removeChild(printSheet);
		}
	}
	myHead.appendChild(sheet);
}

// ARTICLE PRINT

function printArticle()
{
	// Creating the new print sheet...
	var printSheet = document.createElement('link');
	printSheet.setAttribute('rel', 'stylesheet');
	printSheet.setAttribute('type', 'text/css');
	printSheet.setAttribute('media', 'print');
	printSheet.setAttribute('href', '/sitefiles/1/css/article-print.css');
	
	// Getting all the link elements...
	var linkElems = $$('link');
	var printSheets = [];
	var printsheetCounter = 0;
	
	// Extracting all the print stylesheets, ignoring eg. favicons...
	linkElems.each(function(linkElem){
		if( $(linkElem).getAttribute('rel') == 'stylesheet' && $(linkElem).getAttribute('type') == 'text/css' && ( $(linkElem).getAttribute('media') == 'print' || $(linkElem).getAttribute('media') == 'all' ) )
		{
			printSheets[printsheetCounter] = linkElem;
		}
		printsheetCounter++;
	});
		
	// Switching...
	__activatePrint(printSheets, printSheet);
	
	// Printing
	window.print();
}
