var main = null;
var footer = null;
var content = null;
var image = null;
var image_slide = null;
var flash = null;
var contentHeight = 0;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function load()
{
    if(document.getElementById('master') && document.getElementById('footer'))
    {
        document.getElementById('master').style.top = '-500px';
        document.getElementById('footer').style.height = '0px';
        init();
    }
    //else setTimeout(load, 100);
}

load();

function dofadeOn(element, onColor, bound, reset, timer)
{
    if(element && onColor > bound)
    {
        onColor -= 10;
        element.style.color = 'rgb(' + onColor + ', ' + onColor + ', ' + onColor + ')';
        timer = setTimeout(function() { dofadeOn(element, onColor, bound, reset, timer); }, 20); 
    }
    else
    {
        clearTimeout(timer);
    }
}

function dofadeOut(element, outColor, bound, reset, timer)
{
    if(element && outColor < bound)
    {
        outColor += 10;
        element.style.color = 'rgb(' + outColor + ', ' + outColor + ', ' + outColor + ')';
        timer = setTimeout(function() { dofadeOut(element, outColor, bound, reset, timer); }, 20); 
    }
    else
    {
        clearTimeout(timer);
    }
}

function slideDownMain()
{
    var top = parseInt(main.style.top);
    if(top < 0)
    {
        var delay = 50;
        var step = (0 - top) / 10;
        main.style.top = (top + Math.ceil(step)) + 'px';
        setTimeout(slideDownMain, delay);
    }
}

function slideUpBottom()
{
    var height = parseInt(footer.style.height);
    if(height < 32)
    {
        var delay = 50;
        var step = (32 - height) / 10;
        footer.style.height = (height + Math.ceil(step)) + 'px';
        setTimeout(slideUpBottom, delay);
    }
}

function slideDownContent()
{
    var height = parseInt(content.style.height);
    if(height < contentHeight)
    {
        var delay = 50;
        var step = (contentHeight - height) / 20;
        content.style.height = (height + Math.ceil(step)) + 'px';
        content.scrollTop = content.scrollHeight
       
        setTimeout(slideDownContent, delay);
    }
    else
    {
        placeFocus();
    }
}

function slideDownImage(image_url)
{
    if(image)
    {
        document.getElementById('Show_Image').style.backgroundImage = "url('" + image_url + "')";
        image.style.visibility = 'visible';
        image.style.cursor = 'pointer';
        document.getElementById('Show_Image').style.height = clientHeight() + 'px';
        if(document.getElementById('Flash'))
        {
            flash = document.getElementById('Flash').innerHTML;
            document.getElementById('Flash').innerHTML = "";
        }
        if(image_slide) clearTimeout(image_slide);
        doSlideDownImage();
    }
    else
    {
        var newWindow = window.open(image_url, '_blank');
        newWindow.focus();
    }
}

function doSlideDownImage()
{
    var height = parseInt(image.offsetHeight);
//    alert("SHOWING IMAGE! height = " + height);
    if(height < clientHeight())
    {
        var delay = 25;
        if(height > (clientHeight() / 2)) delay = 0;
        var step = (clientHeight() - height) / 10;
        image.style.height = (height + Math.ceil(step)) + 'px';
        image.scrollTop = image.scrollHeight
        showRate(height, clientHeight());
        
        image_slide = setTimeout(doSlideDownImage, delay);
    }
    else
    {
        document.getElementById('Show_Image_Close').focus();        
    }
}

function slideUpImage()
{
    image.style.cursor = 'default';
    if(document.getElementById('Flash'))
    {
        document.getElementById('Flash').innerHTML = flash;
    }
    if(image_slide) clearTimeout(image_slide);
    doSlideUpImage();
}

function doSlideUpImage()
{
    var height = parseInt(image.offsetHeight);
//    alert("HIDING IMAGE! height = " + height);
    if(height > 1)
    {
        var delay = 25;
        if(height < (clientHeight() / 2)) delay = 0;
        var step = height / 20;
        image.style.height = (height - Math.ceil(step)) + 'px';
        image.scrollTop = image.scrollHeight

        showRate(height, clientHeight());
        image_slide = setTimeout(doSlideUpImage, delay);
    }
    else
    {
        image.style.visibility = 'hidden';
    }
}

function showRate(rate, max)
{
    if(max - rate < 3) rate = max + 1;
    if(rate < 3) rate = 0;
    var newrate = (rate / max) * (clientWidth() / 2);
    if(document.getElementById('Show_Rate_Left') && document.getElementById('Show_Rate_Right'))
    {
        document.getElementById('Show_Rate_Left').style.width = newrate + 'px';
        document.getElementById('Show_Rate_Right').style.width = newrate + 'px';
    }
}

function switchCheck(element)
{
    if(document.getElementById(element + '_check'))
    {
        var checkbox = document.getElementById(element + '_check');
        var box = document.getElementById(element + '_box');
        if(checkbox.checked)
        {
            checkbox.checked = '';            
            box.style.background = '#000000';
            box.style.color = '#ffffff';
        }
        else
        {
            checkbox.checked = 'checked';
            box.style.background = '#cccccc';
            box.style.color = '#000000';            
        }
    }
}

function placeFocus()
{
    if (document.forms.length > 0)
    {
        var field = document.forms[0];
        for (i = 0; i < field.length; i++)
        {
            if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea")
            || (field.elements[i].type.toString().charAt(0) == "s"))
            {
                document.forms[0].elements[i].focus();
                break;
            }
        }
    }
}

function setLinks()
{
    var links = document.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++)
    {
        if(links[i].className == 'button' || links[i].className == 'sublink')
        {
        
            links[i].onmouseover = function() { dofadeOn(this, 255, 80, 255); }
            links[i].onmouseout = function() { dofadeOut(this, 80, 255, 80); }
        //    links[i].onmouseout = function() { this.style.color = 'White'; }
        }
        else if(links[i].className == 'button-located' || links[i].className == 'sublink-located')
        {
        }
        else if(links[i].className == 'mastering_button')
        {
            if(dofadeOut) links[i].onmouseover = function() { dofadeOut(this, 0, 255, 0); }
            if(dofadeOn)links[i].onmouseout = function() { dofadeOn(this, 255, 0, 255); }
        }
        else
        {
            if(dofadeOn) links[i].onmouseover = function() { dofadeOn(this, 255, 120, 255); }
            if(dofadeOut) links[i].onmouseout = function() { dofadeOut(this, 120, 255, 150); }
        }
        //links[i].onfocus = blur;
    }
}

function alertSize()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function clientWidth()
{
var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function clientHeight()
{
var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function init()
{
    if(readCookie('effects') != 'disabled')
    {
        document.getElementById('effects_enabled').style.visibility = "visible";
        document.getElementById('effects_disabled').style.visibility = "hidden";
        //alert('MAIN: ' + document.getElementById('main').offsetHeight + ' CLIENT: ' + clientHeight());
        //alert(f_clientWidth() + 'x' + f_clientHeight());
        setLinks();
        main = document.getElementById('master');
        footer = document.getElementById('footer');
        image = document.getElementById('Show_Image_Container');

        var showFooter = false;
        if(main)
        {
            //alert(f_clientHeight() + ' >= ' + (140 + parseInt(document.getElementById('master').offsetHeight)));
            //if(f_clientHeight() >= 460) showFooter = true;
            if(clientHeight() >= (32 + main.offsetHeight))
                showFooter = true;
            //document.getElementById('content').onfocus = blur;
        }

    //    alert('REFFERED BY: ' + document.referrer);
        if(footer && !showFooter) footer.style.display = 'none';
        if(document.referrer.indexOf('wdwedemeyer') == -1)
        {
            main.style.top = '-' + main.offsetHeight  + 'px';
            slideDownMain();
            if(showFooter)
            {
                footer.style.height = '0px';
                slideUpBottom();
            }
            else
            {
                footer.style.height = '32px';
            }
        }
        else
        {
            if(document.getElementById('content_scroll') && document.getElementById('content'))
            {
                content = document.getElementById('content_scroll');
               contentHeight = parseInt(document.getElementById('content').style.height);
                if(clientHeight() > 334)
{
testHeight = parseInt(document.getElementById('content').style.height);
contentHeight = clientHeight() - 234;
//document.getElementById('content').style.top = testHeight + 'px';
document.getElementById('content').style.height = contentHeight + 'px';
}
                content.style.height = '0px';
                slideDownContent();
            }        
            main.style.top = '0px';      
            if(showFooter) footer.style.height = '32px';    
        }
    }
}

window.onload = init;