﻿/* *******************************************
custom scrollbar (works on divs with class "scroller")
******************************************* */
addLoadEvent(scrollArea);
function scrollArea()	{
	var scrollAreaDivs= getElementsByClassName('div','scroll');
	for(var i=0;i<scrollAreaDivs.length;i++)
		{
		scrollAreaDivs[i].style.overflow='hidden';
		if(scrollAreaDivs[i].clientHeight < scrollAreaDivs[i].scrollHeight)
			{
			addNeatScrollBar(scrollAreaDivs[i]);
			}
		}
	InitDragDrop();
	}
function addNeatScrollBar(e)	{
	var contentDiv = document.getElementById('content');
	contentDiv.setAttribute('class','nodot');
	contentDiv.className='nodot';
	var scrollbar = document.createElement("div");
	scrollbar.setAttribute("class","scrollbar");
	
	scrollbar.style.height = e.clientHeight+'px';
	
	scrollbar.className="scrollbar";
	var img = document.createElement('div');
	img.setAttribute('class','drag');
	img.className='drag';
	scrollbar.appendChild(img);
	e.parentNode.insertBefore(scrollbar,e);
	}
	
var _startX = 0; // mouse starting positions 
var _startY = 0; 
var _offsetX = 0; // current element offset 
var _offsetY = 0; 
var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove 
var _oldZIndex = 0; // we temporarily increase the z-index during drag 

function InitDragDrop() { 
	document.onmousedown = OnMouseDown; 
	document.onmouseup = OnMouseUp; 
	}
function OnMouseDown(e)
{
    // IE is retarded and doesn't pass the event object
    if (e == null) 
        e = window.event; 
    
    // IE uses srcElement, others use target
    var target = e.target != null ? e.target : e.srcElement;
    // for IE, left click == 1
    // for Firefox, left click == 0
    if ((e.button == 1 && window.event != null || 
        e.button == 0) && 
        target.className == 'drag')
    {
        // grab the mouse position
        _startX = e.clientX;
        _startY = e.clientY;
        
        // grab the clicked element's position
        _offsetX = ExtractNumber(target.style.left);
        _offsetY = ExtractNumber(target.style.top);
        
        // bring the clicked element to the front while it is being dragged
        _oldZIndex = target.style.zIndex;
        target.style.zIndex = 10000;
        
        // we need to access the element in OnMouseMove
        _dragElement = target;

        // tell our code to start moving the element with the mouse
        document.onmousemove = OnMouseMove;
        
        // cancel out any text selections
        document.body.focus();

        // prevent text selection in IE
        document.onselectstart = function () { return false; };
        
        // prevent text selection (except IE)
        return false;
    }
}
function OnMouseMove(e)
{
    if (e == null) 
        var e = window.event; 

    // this is the actual "drag code"
   //_dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
   var newpos = (_offsetY + e.clientY - _startY);
   if(newpos>=0 && newpos<=_dragElement.parentNode.clientHeight)
	{
    _dragElement.style.top = newpos + 'px';
	setScrollPercent(_dragElement.parentNode.nextSibling,parseInt(newpos/_dragElement.parentNode.clientHeight*100));
	} else if(newpos<0){
	setScrollPercent(_dragElement.parentNode.nextSibling,0);
	} else	{
	setScrollPercent(_dragElement.parentNode.nextSibling,100);
	}
	
    
   
}
function OnMouseUp(e)
{
    if (_dragElement != null)
    {
        _dragElement.style.zIndex = _oldZIndex;

        // we're done with these events until the next OnMouseDown
        document.onmousemove = null;
        document.onselectstart = null;

        // this is how we know we're not dragging      
        _dragElement = null;
        
  
    }
}

function ExtractNumber(value)
{
    var n = parseInt(value);
	
    return n == null || isNaN(n) ? 0 : n;
}

// this is simply a shortcut for the eyes and fingers
function $(id)
{
    return document.getElementById(id);
}
function setScrollPercent(e,pc)
	{
	var scrollElementHeight = e.clientHeight;
	var scrollContentHeight = e.scrollHeight;
	
	var overflowDistance = parseInt(e.scrollHeight-e.clientHeight);
	//adjust overflow
	e.scrollTop=parseInt(overflowDistance*(pc/100));
	}

/* ********************************************
slideshow (works on images with class="slideshow")
********************************************* */

function slideshow()
	{
	var slideContainers = getElementsByClassName('img','slideshow');
	if(slideContainers.length>0)
		{
		var basetimeout = 2500;
		var timerdelay = basetimeout;
		var timeout = basetimeout*slideContainers.length;
		
		for(var i =0;i<slideContainers.length;i++)
			{
			switchimage(slideContainers[i],timerdelay,timeout);
			timerdelay+=basetimeout;
			}
		/* preload images */
		var preload = new Array();
		/*var path = slideContainers[0].src.split('/');
		path.pop(path[path.length-1]);*/
		for(var i = 0;i<slideImages.length;i++)
			{
			preload.push(new Image);
			preload[i].src=root+slideImages[i];
			}
		}
	}
function switchimage(e,wait,interval)
	{
	window.setTimeout(function(){startTimer(e,interval)},wait);
	}
function startTimer(e,interval){
	window.setInterval(function(){replaceImage(e)},interval);
	}
function replaceImage(e)
	{
	var newImage = slideImages[Math.floor(Math.random()*slideImages.length)];
	/* check if newImage isn't in any other slideThing yet */
	while(checkDupes(newImage))
		{
		newImage = slideImages[Math.floor(Math.random()*slideImages.length)];
		}
	/*var path = e.src.split('/');
	path.pop(path[path.length-1])*/
	e.src=root+newImage;
	}
function checkDupes(url){
	var slideContainers = getElementsByClassName('img','slideshow');
	for(var i = 0;i<slideContainers.length;i++)
		{
		var origFilename = slideContainers[i].src.split('/');
		var newFilename = url.split('/');
		if(origFilename[origFilename.length-1]==newFilename[newFilename.length-1])
			{
			return true;
			}
		}
	return false;
	}
/* ********************************************
 common stuff 
 ******************************************** */

 function addLoadEvent(func)
	{	
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
	    	window.onload = func;
		} else {
			window.onload = function(){
			oldonload();
			func();
			}
		}

	}
//as long as browsers don't support getElementsByClassName, let's just write our own!
//extra functionality browsers won't support ever: 
//-values can be added by appending them in brackets. example: required(*@*.*),, popup(500,400), ...
function getElementsByClassName(tag, classname)	{
	var xyzzy=document.getElementsByTagName(tag);
	var elements = new Array;
	var regulexp= new RegExp("(^|\\s)"+classname+"(\\s|$|\\(.*\\))");
	for(i=0;i<xyzzy.length;i++)
		{
		if(regulexp.test(xyzzy[i].className))
			{
			elements[elements.length]=xyzzy[i];
			}
		}
	return elements;
	}
//extract the variables passed between {}'s
function getClassVariables(e,classname)	{
	vars = new Array;
	varstring = e.className;
	//var regulexp= new RegExp("(^|\\s)"+classname+"(\\s|$|{.*})");
	var regulexp= new RegExp(classname+"\\((.*)\\)");
	varr=varstring.match(regulexp);
	if(varr!=undefined){
		varstring = varr[1];
		vars=varstring.split(',');
		}else{
			vars=new Array(undefined,undefined);
		}
	return vars;
	}