// gccWebImageTools
// version: 0.12
// Date: 2009-04-06
// 
// written by Gian Carlo Cervone
// (c) 2007, 2008, 2009 Gian Carlo Cervone
// All Rights Reserved - this software may not be modified, distributed, or copied
// without written permission
// giancarlo@cervone.net
//     
// Thanks to Davide P.Cervone for help on the SetTimeout replacement
// 

//////////
//
//  function - gccWIT_setTimeout
//
//////////
//////////

// Work around bug in IE where setTimeout doesn't handle the extra
// parameters to be passed to the delayed routine.  Here, we create a
// temporary function that simply acts as a holding area for the arguments
// and that then calls the appropriate function with the given arguements
// when the time is right.

function gccWIT_setTimeout (f,delay) {
//    var args = [];
//    for (var i = 2; i < arguments.length; i++) {args[i-2] = arguments[i]};
    var fn = new Function("arguments.callee.f.apply(arguments.callee.f,arguments.callee.args)");
    fn.f = f; fn.args = [].slice.call(arguments, 2); // remove first two arguments
    gccWIT_oldSetTimeout(fn,delay);
}

// before doing anything else, we check to see whether we are running in
// MSIE, and if so, create the replacement routine

function gccWIT_initialise() {
    gccWIT_oldSetTimeout = window.setTimeout;
    if (navigator.userAgent.indexOf("MSIE")>-1) {
        window.setTimeout = gccWIT_setTimeout;
    }
}


//////////
//
//  function - gccWIT_isMSIE
//  
//////////
//////////

function gccWIT_isMSIE() {
    if(navigator.userAgent.indexOf("MSIE")>-1) {
        return true;
    } else {
	return false;
	
    }
}


//////////
//
//  function - gccWIT_nudge
//  
//////////
//////////

function gccWIT_nudge() {
    window.resizeBy(-1, -1);
}

//////////
//
//  function - gccWIT_resize
//  
//////////

function gccWIT_resize(imageArray, widthCutoff, heightCutoff) {
    if(browser == 'nice') {
	width = window.outerWidth;
	height = window.outerHeight;
//	width = window.innerWidth;
//	height = window.innerHeight;
    } else {
	width = document.documentElement.clientWidth;
	height = document.documentElement.clientHeight;
//	width = document.body.clientWidth;
//	height = document.body.clientHeight;
    }
    if (height == 0) {
	width = document.body.clientWidth;
	height = document.body.clientHeight;
    }
            
//alert(height);

    if (width < widthCutoff)  {
	wReduction = width / widthCutoff;
    } else {
	wReduction = 1;
    }
    if (height < heightCutoff) {
        hReduction = height / heightCutoff;
    } else {
	hReduction = 1;
    }
    if (wReduction < hReduction) {
        reduction = wReduction;
    } else if (hReduction < wReduction) {
	reduction = hReduction;
    } else {
	reduction = hReduction;
    }    
    
    if (reduction < 1) {
	document.body.style.fontSize = Math.floor(reduction * 100) + '%';
	for (counter = 0; counter < imageArray.length; counter += 1) {
	    if (document.getElementById) {
		theElement = document.getElementById(imageArray[counter][0]);
		if (theElement == null) {
		    theElement = document.getElementsByName(imageArray[counter][0])[0];
		}
		if (theElement != null) {
		    theElement.width = Math.floor(imageArray[counter][1] * reduction);
		    if (imageArray[counter][2] != null) {
		    theElement.height = Math.floor(imageArray[counter][2] * reduction);
		    }
		}
	    }
	}
    } else {
	document.body.style.fontSize = '100%';
	for (counter = 0; counter < imageArray.length; counter += 1) {
	    if (document.getElementById) {
		theElement = document.getElementById(imageArray[counter][0]);
		if (theElement == null) {
		    theElement = document.getElementsByName(imageArray[counter][0])[0];
		}
		if (theElement != null) {
		    theElement.width = imageArray[counter][1];
		    if (imageArray[counter][2] != null) {
		    theElement.height = imageArray[counter][2];
		    }
		}
	    }     
	}
    }
}


//////////
//
//  main function - gccWIT_doEffect
//  
//////////

function gccWIT_doEffect(theItem) {
    if ((typeof(theItem.delay) !== 'undefined') && (theItem.delay > 0)) {

// initial delay
	var theDelay = theItem.delay
	if (theItem.effect == 'fadeSwitch') theItem.switchDelay = theDelay;
	theItem.delay = 0;
	theItem.timeOut = setTimeout(gccWIT_doEffect, theDelay, theItem);
    } else {
	    // setup defaults
	if (theItem.status == null) theItem.status = 0;
	if (theItem.pause == null) theItem.pause = 0;
	if (theItem.effect == null) theItem.effect = 'fadeIn';
	if (theItem.steps == null || theItem.steps == 0) theItem.steps = 100;
	if (theItem.minOpacity == null) theItem.minOpacity = 0;
	if (theItem.maxOpacity == null) theItem.maxOpacity = 1;
	if (document.getElementById && theItem.item == null) {
	    theItem.item = document.getElementById(theItem.itemID);
	}
// add error check here for item
        if (theItem.newClass != null) {
            theItem.item.className = theItem.newClass;
	    theItem.newClass = null;
        }
	
	if (theItem.status == 0) {
	    // process effect
	    switch(theItem.effect) {
		case 'fadeIn': {
		    if (theItem.oStep == null || theItem.oStep == 0) {
			// initialise fadeIn
			theItem.oStep = (theItem.maxOpacity - theItem.minOpacity) / theItem.steps;
			theItem.opacity = theItem.minOpacity;
			switch(browser) {
			    case 'nice': {
			        theItem.item.style.opacity = theItem.opacity;
				break;
			    }
			    case 'ie7': {
				theItem.item.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity = "+theItem.opacity*100 + ")";
				break;
			    }
			    default: {
				theItem.item.style.filter="alpha(opacity = "+theItem.opacity*100 + ")";
				break;
			    }
			}
		    }
		    // do fadeIn
		    theItem.opacity += theItem.oStep;
		    if (theItem.opacity > theItem.maxOpacity) theItem.opacity = theItem.maxOpacity;
		    if (theItem.opacity == theItem.maxOpacity) {
			if (theItem.cycle == 1) {
			    theItem.effect = 'fadeOut';
			} else {
			theItem.status = 1;
			}
		    }
		    switch(browser) {
			case 'nice': {
			    if (theItem.item.style.opacity) 
			         theItem.item.style.opacity = theItem.opacity;
			    break;
			}
			case 'ie8': {
			    theItem.item.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity = " + theItem.opacity*100 + ")";
			    break;
			}
			default: {
			    theItem.item.style.filter="alpha(opacity = " + theItem.opacity*100 + ")";
			    break;
			}
		    }
		    break;
		}
		case 'fadeOut': {
		    if (theItem.oStep == null || theItem.oStep == 0) {
			// initialise fadeOut
			theItem.oStep = (theItem.maxOpacity - theItem.minOpacity) / theItem.steps;
			theItem.opacity = theItem.maxOpacity;
			theItem.item.style.opacity = theItem.opacity;
		    }
		    // do fadeOut
		    theItem.opacity -= theItem.oStep;
		    if (theItem.opacity < theItem.minOpacity) theItem.opacity = theItem.minOpacity;
		    if (theItem.opacity == theItem.minOpacity) {
			if (theItem.cycle == 1) {
			    theItem.effect = 'fadeIn';
			    theItem.oStep = 0;
//			    theItem.status = 1;
//			    alert('almost done'+theItem.opacity);
//		            alert(theItem.item.style.opacity);
			    
			} else {
			    theItem.status = 1;
			}
		    }
//		    if (theItem.item.style.opacity)
//		    theItem.item.style.opacity = theItem.opacity;
//		    alert(theItem.opacity);
//	            alert(theItem.item.style.opacity);
switch(browser) {
    case 'nice': {
	if (theItem.item.style.opacity) 
	     theItem.item.style.opacity = theItem.opacity;
	break;
    }
    case 'ie8': {
	theItem.item.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity = " + theItem.opacity*100 + ")";
	break;
    }
    default: {
	theItem.item.style.filter="alpha(opacity = " + theItem.opacity*100 + ")";
	break;
    }
}
break;
		    
		    break;
		}
		case 'fadeSwitch': {
		    if (theItem.oStep == null || theItem.oStep == 0) {
			// initialise fadeIn
			theItem.oStep = (theItem.maxOpacity - theItem.minOpacity) / theItem.steps;
			theItem.opacity = theItem.minOpacity;
			theItem.item.style.opacity = theItem.opacity;
			if (theItem.zOffset == null) theItem.zOffset = 0;
		    }
		    // do fadeIn
		    theItem.opacity += theItem.oStep;
		    if (theItem.opacity > theItem.maxOpacity) theItem.opacity = theItem.maxOpacity;
		    if (theItem.item.style.opacity)
    		    theItem.item.style.opacity = theItem.opacity;
		    if (theItem.opacity == theItem.maxOpacity) {
			theItem.delay = theItem.switchDelay;
			var tempId = theItem.imgArray.shift();
			theItem.imgArray.push(tempId);
			theItem.itemID = tempId;
			theItem.item = document.getElementById(theItem.itemID);
			for (var counter = 0; counter < theItem.imgArray.length; counter += 1) {
			    var tempItem = document.getElementById(theItem.imgArray[counter])
			    tempItem.style.zIndex = counter+1+theItem.zOffset;
			}
			theItem.opacity = theItem.minOpacity;
			theItem.item.style.opacity = theItem.opacity;			
//			theItem.status = 1;
		    }
		    break;
		}
	    }
	    theItem.timeOut = setTimeout(gccWIT_doEffect, theItem.pause, theItem);
	}
    }
}

function gotoName(link) {
    theElement = document.getElementById('mainheader');
    if(theElement.offsetHeight)  {
	theOffset = theElement.offsetHeight;
    } else {
	if(theElement.style.pixelheight) {
	    theOffset = theElement.style.pixelHeight;
	} else {
	    theOffset = 0;
	}
    }
    location = "#" + link;
    window.scrollBy(0, -theOffset);   
}


//////////
//
//  css additions
//  
//////////

document.write('<style type="text/css">');
document.write('.gccWIT_notseen {');
document.write('opacity: 0;');
document.write('filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";');
document.write('filter:alpha(opacity=0);');
document.write('}</style>');

//////////
//
//  initialise the system
//  
//////////

//document.write(var gccWIT_browser="nice";');

//////////
//  
//  gccWIT object properties
//  
//////////

// browser      * ='nice' if not Internet Explorer, else 'ie' plus version ex. 'ie6'
// itemID       - DOM id of html item
// item         * the html item
// effect       - the effect currently being performed (fadeIn, fadeOut, fadeSwitch)
// cycle        - 1=cycle when finished, 0=stop when finished
// pause        - length of wait between steps
// delay        - inital delay before beginning
// switchDelay  * value of delay between image swaps for fadeSwitch only
// opcaity      * object's current opacity value
// x            * object's current x value
// y            * object's current y value
// z            * object's current z-index value
// next         - DOM id of next html item
// steps        - number of steps to accomplish operation
// oStep        * size of opacity change
// xStep        * size of x change
// yStep        * size of y change
// targetX      - final x position
// targetY      - final y position
// status       * 0=not done, 1=done
// width        * screen width
// height       * screen height
// timeOut      * timeOut tracking variable
// newClass     - class to be assigned to item
// minOpacity   - low opacity value for fadeOut
// maxOpacity   - high opacity value for fadeIn
// imgArray     - array of image ids for fadeSwitch
// zOffset      - offset value for z-index


// * - indicates should not be set by user - these values are written by gccWIT!
