var imageWidth = 'auto';
var imageHeight = '405px';
window.onresize = calculateImageSize; 

function initSlik() {
	calculateImageSize();
}

function calculateImageSize() {
	var windowWidth = 800, windowHeigth = 600;
	if (document.body && document.body.offsetWidth) {
	 windowWidth = document.body.offsetWidth;
	 windowHeigth = document.body.offsetHeight;
	}
	if (document.compatMode=='CSS1Compat' &&
	    document.documentElement &&
	    document.documentElement.offsetWidth ) {
	 windowWidth = document.documentElement.offsetWidth;
	 windowHeigth = document.documentElement.offsetHeight;
	}
	if (window.innerWidth && window.innerHeight) {
	 windowWidth = window.innerWidth;
	 windowHeigth = window.innerHeight;
	}
	if (windowWidth < 660) {
		imageWidth = '100%';
		imageHeight = 'auto';
	} else if (windowWidth < 768) {
		imageWidth = 'auto';
		imageHeight = '240px';
	} else if (windowWidth < 1024) {
		imageWidth = 'auto';
		imageHeight = '330px';
	} else {
		imageWidth = 'auto';
		imageHeight = '405px';
	}
	var aa = $$('#content article.open figure.image img');
	if (aa[0]) { 
		setImageSize(aa[0]);
	}
}

function showNews(inIndex){
	var newsItems = $$('section#news div');
	Effect.SlideUp('news', { duration: 0.5, afterFinish: function() {
			newsItems.each(function(item) {
 				item.hide();
 			});
			newsItems[inIndex].show();
			Effect.SlideDown('news', { duration: 0.5, delay:1 });			
		} 
	});
	newIndex = inIndex+1;
	if (newIndex > (newsItems.length-1)) {newIndex = 0; }
	setTimeout('showNews('+newIndex+')',5000);
}

function showList(){
	var listItems = $$('#content article, footer');
	footerDelay = 0.1*listItems.length;
	Effect.multiple(listItems, Effect.Appear, { speed: 0.1, duration:0.4 });
	$('foot').appear({ duration: 0.4, delay:footerDelay});
}

function toggleInfo(id, link) {	
	var containerID = 'info';	
	var activeContentLinks = $$('#contentNav li a.selected');
	if (link.hasClassName('selected')) {
		closeInfo();
	} else {
		if ($(containerID).hasClassName('open')) {
			new Effect.BlindUp(containerID, { duration: 0.3, queue: {position: 'end', scope: 'infoContainer'}, afterFinish: function(){
					updateInfo(id);
				}
			});	
		} else {
			updateInfo(id);
		}	
		link.addClassName('selected');		
		if (activeContentLinks[0]) {
			activeContentLinks[0].removeClassName('selected');	
		} 
	}
}	

function updateInfo(id) {
	containerID = 'info';
	new Ajax.Updater(containerID,'?ctrl=loadContent&contentId='+id, {
        onComplete: function(){ 
			new Effect.BlindDown(containerID, { duration: 0.15, queue: {position: 'end', scope: 'infoContainer'} });
			$(containerID).addClassName('open');
			Cufon.refresh();
        } 
	});
}

function closeInfo() {
	containerID = 'info';
	if ($(containerID).hasClassName('open')) {
		new Effect.BlindUp(containerID, { duration: 0.3, queue: {position: 'end', scope: 'infoContainer'}});	
		$(containerID).removeClassName('open');
		var activeContentLinks = $$('#contentNav li a.selected');
		if (activeContentLinks[0].hasClassName('selected')) {
			activeContentLinks[0].removeClassName('selected');
		}
	}
}

function toggleArticle(inArticleID){
	if ($(inArticleID).hasClassName('open')) {
		closeArticle(inArticleID);
	} else {
		openArticle(inArticleID);
	}
}

function openArticle(inArticleID) {

	var aa = $$('#content article.open');
	if (aa[0]) { 		
		aa[0].removeClassName('open');
		new Effect.Morph(aa[0], {
  			style: 'close', // CSS Properties
  			duration: 0.5, // Core Effect properties
  			afterFinish: function(){
				new Effect.ScrollTo(inArticleID, { duration:'0.5', offset:-8 });
				new Effect.Morph(inArticleID, {
						style: 'open', // CSS Properties
						duration: 0.3, // Core Effect properties
						afterFinish: function(){
							$(inArticleID).removeClassName('close');
						}
				});
			}
		});
	} else {
		new Effect.Morph(inArticleID, {
				style: 'open', // CSS Properties
				duration: 0.3, // Core Effect properties
				afterFinish: function(){
					$(inArticleID).removeClassName('close');
				}
		});
		new Effect.ScrollTo(inArticleID, { duration:'0.5', offset:-8, delay:0.3 });
	}		
}

function closeArticle(inArticleID) {
	$(inArticleID).removeClassName('open');
	new Effect.Morph(inArticleID, {
		style: 'close', // CSS Properties
		duration: 0.5 // Core Effect properties
	});
}

function setImage(inArticleID,imgArray,captionArray,inImgIndex) {
	fadeDuration = 0.2;
	appearDuration = 0.5;
	imageSrc = imgArray[inImgIndex];
	captionStr = captionArray[inImgIndex];
	bigImage = $('i'+inArticleID);
	captionBox = $('c'+inArticleID);
	
	if (!$('a'+inArticleID).hasClassName('open')) {
		bigImage.hide();
		openArticle('a'+inArticleID);
	}

	bigImage.fade({ duration: fadeDuration, afterFinish: function(){
		bigImage.writeAttribute('src', '');
		bigImage.observe("load", function(){	   		
			setImageSize(bigImage);
	  		bigImage.appear({duration: appearDuration});
		   	bigImage.stopObserving('load');
		   	captionBox.update(captionStr); 
	   });	
	   bigImage.writeAttribute('src', imageSrc); 	   
     } 
	}); 
	window['imgIndex'+inArticleID] = inImgIndex;
}

function setImageSize(element) {
	iaa = element.ancestors();
	imgContainer = iaa[1];
	imgContainer.setStyle({ height: imageHeight });
	element.setStyle({ height: imageHeight, width: imageWidth });
}

function nextImage(inArticleID,imgArray,captionArray,imgIndex) {
	if(imgIndex == imgArray.length-1) {
		setIndex = 0; }
	else {
		setIndex = imgIndex+1; }
	setImage(inArticleID,imgArray,captionArray,setIndex);
	return false;
}

function prevImage(imgArray,inImgIndex) {
	if(imageListIndex == 0) {
		setIndex = imageList.length-1; }
	else {
		setIndex = imageListIndex-1; }
	updateImage(setIndex);
	return false;
}


function toggleClass(inID,inClass) {
	if ($(inID).hasClassName(inClass)) {	
		$(inID).removeClassName(inClass);
	} else {
		$(inID).addClassName(inClass);
	}	
}

function setThumb(inIndex) {
	thumbId = "t"+inIndex;
	var ts = $$('.thumbs a.selected');
	ts[0].removeClassName('selected');
	$(thumbId).addClassName('selected');
}
