﻿
if (typeof DrKoch == 'undefined') { DrKoch = new Object; }
if (typeof DrKoch.Web == 'undefined') { DrKoch.Web = new Object; }

window.onload = function () {

    if (!document.getElementById) return; 
    var colElements = document.getElementsByTagName('*');
    var i = 0; var objElement;
    while (objElement = colElements[i++]) {
        if (objElement.className.match(/Slideshow/)) new DrKoch.Web.Slideshow(objElement);
        if (objElement.className.match(/Ticker/)) new DrKoch.Web.Ticker(objElement);
        if (objElement.className.match(/Zoom/)) new DrKoch.Web.Zoom(objElement);
        if (objElement.className.match(/SwpSwitch/)) new DrKoch.Web.SwpSwitch(objElement);
        if (objElement.className.match(/Fader/)) new DrKoch.Web.Fader(objElement);
    }
}

DrKoch.Web.Fader = function (objElement) {

    var me = this;
    this.objElement = objElement;
}

DrKoch.Web.Ticker = function (objElement)
{
	function Daemon()
	{
		me.intTimer -= 50;
		if(me.intTimer <= 0)
		{
			me.intTimer = me.intInterval;

			var objElement = DrKoch.Web.FindVisibleElement(me.objElement.getElementsByTagName('div'))
			objElement.style.display = 'none';
			objElement = DrKoch.Web.NextElement(objElement) ? DrKoch.Web.NextElement(objElement) : me.objElement.getElementsByTagName('div')[0];
			objElement.style.display = 'block';
		}				
		window.setTimeout(Daemon, 50);
	}	
	var me = this;
	this.objElement = objElement;
	this.intInterval	= 5000;
	this.intTimer		= this.intInterval;
	Daemon();
}

DrKoch.Web.Slideshow = function (objElement)
{
	this.PopUp = function(e) {
	    var objWindow = window.open(me.objElement.getAttribute('href'), me.strName, me.strFeatures);
		if(objWindow != null)
		{
			objWindow.focus();
			objWindow.resizeTo(screen.availWidth, screen.availHeight);
			return false;
		}
	}	
	var me = this;
	var strName = 'Slideshow';
	var strFeatures = 'top=0, left=0, resizable=yes, titlebar=no, width=360, height=410, alwaysRaised=yes, scrollbars=no, toolbar=no, menubar=no, directories=no, status=no';
	this.objElement = objElement;
	this.objElement.onclick = this.PopUp;
}

DrKoch.Web.Zoom = function (objElement)
{
	this.Hide = function()
	{
		DrKoch.Web.objZoomImage.style.display = 'none';
	}
	this.Move = function(e)
	{
		e = window.event || e; 
		me.iMouseX = e.pageX || (e.clientX + document.documentElement.scrollLeft); 
		me.iMouseY = e.pageY || (e.clientY + document.documentElement.scrollTop);
		me.Position();
	}
	this.Show = function(e)
	{
		var ref = me.objElement.getAttribute('src');
		ref = ref.split('?')[0];
		ref = ref + '?dim=500';
		DrKoch.Web.objZoomImage.src = ref;
	}
	this.Position = function()
	{
	    var intPosX = ((me.intMouseX + me.intOffsetX + DrKoch.Web.objZoomImage.offsetWidth) > DrKoch.Web.GetTotalWidth()) ? DrKoch.Web.GetTotalWidth() - DrKoch.Web.objZoomImage.offsetWidth : me.intMouseX + me.intOffsetX;
	    var intPosY = ((me.intMouseY + me.intOffsetY + DrKoch.Web.objZoomImage.offsetHeight) > DrKoch.Web.GetTotalHeight()) ? me.intMouseY - DrKoch.Web.objZoomImage.offsetHeight - 10 : me.intMouseY + me.intOffsetY;
		
		intPosX -= document.body.offsetLeft;
		intPosX -= 170;
		intPosY -= document.body.offsetTop;
		intPosY -= 103;
		DrKoch.Web.objZoomImage.style.left = intPosX + 'px';
		DrKoch.Web.objZoomImageage.style.top = intPosY + 'px';
		DrKoch.Web.objZoomImage.style.display = 'block';
	}	
	var me = this;
	this.objElement = objElement;
	this.intOffsetX = 8;
	this.intOffsetY = 19;
	this.intMouseX = 0;
	this.intMouseY = 0;
	if(DrKoch.Web.objZoomImage == null)
	{
	    DrKoch.Web.oZoomImage = document.createElement('img');
	    DrKoch.Web.oZoomImage.style.border = 'solid 1px #fff';
	    DrKoch.Web.oZoomImage.style.position = 'absolute';
	    DrKoch.Web.oZoomImage.style.zIndex = 9999;
	    document.body.appendChild(DrKoch.Web.oZoomImage);
	} 
	this.objElement.onmousemove	= this.Move;
	this.objElement.onmouseover	= this.Show;
	this.objElement.onmouseout	= this.Hide;
}

DrKoch.Web.SwpSwitch = function (objElement)
{
	this.MouseOver = function()
	{
	    DrKoch.Web.NextElement(me.objElement).style.display = 'block';
	}

	this.MouseOut = function()
	{
	    DrKoch.Web.NextElement(me.objElement).style.display = 'none';
	}
	var me = this;
	this.objElement = objElement;
	this.objElement.onmouseover = this.MouseOver;
	this.objElement.onmouseout = this.MouseOut;
}

/* Helper functions */

DrKoch.Web.NextElement = function (objNode) {
    while (objNode = objNode.nextSibling) {
        if (objNode.nodeType == 1) break;
    }
    return objNode;
}

DrKoch.Web.FindVisibleElement = function (objNodeList) {
    var objNode = null;
    var i = 0;

    while (objNode = objNodeList[i]) {
        var display = (objNode.currentStyle) ? objNode.currentStyle.display : document.defaultView.getComputedStyle(objNode, null).getPropertyValue('display');
        if (display != 'none') break;
        i++;
    }
    return objNode;
}

DrKoch.Web.GetTotalWidth = function () {
    return (document.documentElement.clientWidth || window.innerWidth) + (window.pageXOffset || document.documentElement.scrollLeft);
}
DrKoch.Web.GetTotalHeight = function () {
    return (document.documentElement.clientHeight || window.innerHeight) + (window.pageYOffset || document.documentElement.scrollTop);
}



