// JavaScript Document
function preloadImages () {
	var d = document;
	if (d.images) {
		if (!d.p) d.p = new Array ();
		var i, j = d.p.length, a = preloadImages.arguments;
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf ("#") != 0) {
				d.p[j] = new Image;
				d.p[j++].src = a[i];
			}
		}
	}
}

// create bg slideshow
var minPos = 1;
var maxPos = 12;
var pos = Math.ceil (Math.random () * maxPos);
var timeOut = 5000;

function changeBG (element, urlPrefix, removeText) {
	pos = (pos % maxPos) + 1;
	if (removeText == 0) {
		var h = document.getElementById ('home')
		h.style.backgroundImage = 'none';
		// remove text from ie6
		try { h.style.setAttribute ('filter', ''); } catch (ex) {}
	}
	element.style.backgroundImage = 'url(' + urlPrefix + 'img/bg/img_' + pos + '.jpg)';
	removeText--;
	setTimeout (function () { changeBG (element, urlPrefix, removeText); }, timeOut);
}

// history
var historyCurrentIndex = 0;
var historyIsScrolling = false;
var historyStepCount = 20;
var historyContentWidth = 819;

function moveHistory (destinationIndex, source) {
	// only move if not already scrolling
	if (!historyIsScrolling) {
		// on arrow, translate the destination to exact position instead of -1/+1
		if (source == 'arrow') {
			// count available items
			var itemCount = historyTimelinePositions.length;
			destinationIndex = (historyCurrentIndex + destinationIndex) % itemCount;
			if (destinationIndex < 0) {
				destinationIndex = itemCount - 1;
			}
		}
		// only move if not already there
		if (destinationIndex != historyCurrentIndex) {
			historyIsScrolling = true;
			scrollHistory (destinationIndex, 0);
		}
	}
}

function scrollHistory (destinationIndex, step) {
	// move content
	var contentDiv = document.getElementById ('his_con_wrap');
	contentDiv.style.marginLeft = (((historyCurrentIndex * historyContentWidth) + (step * (destinationIndex - historyCurrentIndex) * historyContentWidth) / historyStepCount) * -1) + 'px';
	// move timeline indicator
	var timelineDiv = document.getElementById ("his_nav_ruler");
	timelineDiv.style.marginLeft = ((historyTimelinePositions[historyCurrentIndex]) + (step * (historyTimelinePositions[destinationIndex] - historyTimelinePositions[historyCurrentIndex])) / historyStepCount) + 'px';
	// continue?
	if (step < historyStepCount) {
		window.setTimeout ('scrollHistory (' + destinationIndex + ',  ' + (step + 1) + ')', 1);
	}
	// stop!
	else {
		historyCurrentIndex = destinationIndex;
		historyIsScrolling = false;
	}
}

//Galery Popup

function showGaleryPopup (gallery)
	{
		galeryPop = window.open(document.fumanHttpRoot+'../site/gallery/popup.php?id=' + gallery, "galleryName", "width=600, height=480, menubar=no, scrollbars=no, toolbar=no");	
		galeryPop.focus();
	}


//Get Style from extern css file
/*
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
*/

//Gallery Popup Thumbnail scroller

var galleryItem = 1;

function gallery(handler,item){
	var arrowL = document.getElementById('gal_pop_left');
	var arrowR = document.getElementById('gal_pop_right');
	
	arrowL.className = 'active_left';
	arrowR.className = 'active_right';
	if (handler == 'arrow') {
		item = galleryItem + item;
		if (item <= 0)
			item = 0;
		if (item >= images.length-1)
			item = images.length - 1;
	}
	
	if (item <= 0)
		arrowL.className = 'inactive_left';
	if (item >= images.length-1)
		arrowR.className = 'inactive_right'
	
if (item != galleryItem) {
	galleryImage(item);
	galleryMove(item);
	galleryItem = item;
	}
}
//Detail View for Images

function galleryImage(item){
	var screen = document.getElementById('gal_pop_det');
	var img = document.main_image;
	screen.style.visibility = 'hidden';
	img.src = img.src.substring(0, img.src.lastIndexOf ('id=') + 3) + images[item]['id']
	img.style.marginLeft = (580 - images[item]['width'])/2 + 'px'
	img.style.marginTop = (400 - images[item]['height'])/2 + 'px'
	screen.style.visibility = 'visible';
}


var position = 0;
var border = 4;
var margin = 13;

function galleryMove(item){
	position = 0;
	for(i=0;i<item;i++) {
	position = position + (parseInt(images[i]['thumb']) + border + margin);
	}
	document.getElementById('gal_pop_thumbs_con').style.marginLeft = -position + 219  + 'px';
	document.getElementById('img_'+item).className = 'gal_pop_img img_selected';
	document.getElementById('img_'+galleryItem).className = 'gal_pop_img ';
}
	
	
	
	
	
	
	
	
	
