

$(document).ready(function() {
	
	if(document.getElementById('filmstrip')) {
		filmstrip_init();
	}
	
	if(document.getElementById('slideshowBank')) {
		init_slideShow();
	}
	
});

window.onload = function() {
	if(document.getElementById('homeFlash')) {
		home_init();
	}
	
	if(document.getElementById('homeSubFlash')) {
	    divisions_init();
	}
}

var theme_names = new Array();
theme_names[0] = "heavy";
theme_names[1] = "electric";
theme_names[2] = "materials";
theme_names[3] = "residential";
theme_names[4] = "environmental";

var theme_numbers = new Array();
theme_numbers['heavy'] = 0;
theme_numbers['electric'] = 1;
theme_numbers['materials'] = 2;
theme_numbers['residential'] = 3;
theme_numbers['environmental'] = 4;

function theme_setNumber(num) {
	document.getElementById('page').className = theme_names[num];
}

function home_init() {
	$('#page').addClass('heavy');
	
	$('#primaryMenu li').hover(function() {
		document.getElementById('page').className = this.className;
		changePanelById(theme_numbers[this.className]);
	});
	
	document.getElementById('homeFlash').innerHTML = renderFlashHome('/wcc_FlashHome.ashx');
	// IMPORTANT .NET FIX FOR FORM TAGS IN IE6
    window['FlashHome'] = document.forms[0]['FlashHome'];
}

var divisionMenu;
function divisions_init() {
    if($.browser.msie && parseInt($.browser.version,10) < 7) {
		$('#secondaryMenu>ul>li').hover(
			function() {
				$(this).addClass("hover");
			},
			function() {
				$(this).removeClass("hover");
			}
		);
	}

    document.getElementById('homeSubFlash').innerHTML = renderFlashDivision('/wcc_FlashDivision.ashx');
    // IMPORTANT .NET FIX FOR FORM TAGS IN IE6
    //window['FlashHome'] = document.forms[0]['FlashDivision'];
}

var filmstrip_num = 1;
var filmstrip_count = 0;
var filmstrip_src = "";

function filmstrip_init() {
	document.getElementById('filmstrip').scrollTop = 0;
	var frames = $('#film .frame');
	filmstrip_count = frames.length;
	
	if(filmstrip_count > 3) {
		$('#filmstrip_up').addClass('disabled');
	}
	else {
	    $('#filmstrip_controls').css('display','none');
	}
	
	for(var x=0;x<filmstrip_count;x++) {
		frames[x].id = 'film-frame'+(x+1);
	}
	
    var firstImage = $('#mainArea .mainCol img:first')[0];
    filmstrip_src = firstImage.src;
	$('#film .frame').hover(
	    function() {
	        $('#mainArea .mainCol img:first')[0].src = this.getElementsByTagName('img')[0].src;
	    },
	    function() {
	        $('#mainArea .mainCol img:first')[0].src = filmstrip_src;
	    }
	);
}

function filmstrip_down() {
	filmstrip_num++;
	if(filmstrip_num > filmstrip_count - 2) {
		filmstrip_num = filmstrip_count - 2;
	}
	else {
		filmstrip_move(filmstrip_num);
	}
}
function filmstrip_up() {
	filmstrip_num--;
	if(filmstrip_num < 1) {
		filmstrip_num = 1;
	}
	else {
		filmstrip_move(filmstrip_num);
	}
}

function filmstrip_buttons() {
	if(filmstrip_num >= filmstrip_count - 2) {
		$('#filmstrip_down').addClass('disabled');
		$('#filmstrip_up').removeClass('disabled');
	}
	else if(filmstrip_num <= 1) {
		$('#filmstrip_up').addClass('disabled');
		$('#filmstrip_down').removeClass('disabled');
	}
	else {
		$('#filmstrip_up').removeClass('disabled');
		$('#filmstrip_down').removeClass('disabled');
	}
}

function filmstrip_move(num) {

	filmstrip_buttons();
	var theScroll = document.getElementById('filmstrip');
	var position = findElementPos(document.getElementById("film-frame" + num));
	var offsetPos = findElementPos(document.getElementById('film-frame1'));
	position[1] = position[1] - offsetPos[1];
	filmstrip_start(theScroll, theScroll.scrollTop, position[1]);
}

var filmstrip_anim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function filmstrip_start(elem, start, end)
{

	if (filmstrip_anim.timer != null) {
		clearInterval(filmstrip_anim.timer);
		filmstrip_anim.timer = null;
	}
	filmstrip_anim.time = 0;
	filmstrip_anim.begin = start;
	filmstrip_anim.change = end - start;
	filmstrip_anim.duration = 50;
	filmstrip_anim.element = elem;
	
	filmstrip_anim.timer = setInterval("filmstrip_scroll();", 15);

}
function filmstrip_scroll()
{
	if (filmstrip_anim.time > filmstrip_anim.duration) {
		clearInterval(filmstrip_anim.timer);
		filmstrip_anim.timer = null;
	}
	else {
		move = sineInOut(filmstrip_anim.time, filmstrip_anim.begin, filmstrip_anim.change, filmstrip_anim.duration);
		filmstrip_anim.element.scrollTop = move;
		filmstrip_anim.time++;
	}
}

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )
	return Array(elemX, elemY);
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}


/* Homepage Slideshow */
/* ---------------------------------------------------------------------------- */

// Set the slideshow speed (in milliseconds)
var SlideShowSpeed = 3000;

var SScount = 1;
var SSmax = 0;
var SSinterval;
var SSopacity = 0;
var SSeffect;

function init_slideShow() {
	var bankImages = document.getElementById('slideshowBank').getElementsByTagName('img');
	SSmax = bankImages.length;
	if(SSmax > 1) {
		document.getElementById('slideshow').getElementsByTagName('img')[0].id = "ssM";
		for(var x = 0; x < SSmax; x++) {
			bankImages[x].id = "ssM" + (x+1);
		}
		
		var fadeImage = document.createElement('img');
		fadeImage.id = "fadeImage";
		document.getElementById('slideshow').appendChild(fadeImage);
	
		fadeImage.style.opacity = "0";
		fadeImage.style.filter = "alpha(Opacity=0)";
		
		fade_slideShow();
	}
}

function fade_slideShow() {
		
	var fadeImage = document.getElementById('fadeImage');
	var newImage = document.getElementById('ssM'+SScount);
	fadeImage.src = newImage.src;
	fadeImage.alt = newImage.alt;

	SSeffect = setInterval('fadeIn_slideShow()', 20);
}

function fadeIn_slideShow() {
	SSopacity = SSopacity + 2;
	object = document.getElementById('fadeImage');
	if(SSopacity < 100) {
		object.style.opacity = SSopacity * .01;
		object.style.filter = "alpha(Opacity="+SSopacity+")";
	}
	else {
		clearInterval(SSeffect);
		object.style.opacity = "1";
		object.style.filter = "alpha(Opacity=100)";
		
	    SScount++;
	    if (SScount > SSmax)
			SScount = 1;
		if (SScount < 1)
			SScount = SSmax;
			
		var image = document.getElementById('ssM');
		image.src = object.src;
		image.alt = object.alt;
		
		SSopacity = 0;
		object.style.opacity = "0";
		object.style.filter = "alpha(Opacity=0)";
		
		SSinterval = setTimeout('fade_slideShow()', SlideShowSpeed);
	}
}

function emailWindow(location) {
    var newWindow;
    if(location) {
        newWindow = window.open('/wcc_emailPopup.aspx?url='+location, 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    else {
        newWindow = window.open('/wcc_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    newWindow.focus();
}

function customValidationStyle(oElem, isValid) {
    if ("INPUT" == oElem.tagName || "SELECT" == oElem.tagName || "TEXTAREA" == oElem.tagName) {
        if (isValid) {
            oElem.style.background = "none";
        }
        else {
            oElem.style.background = "#EACCBA";
        }
    }
    else {
        design_validationStyle(oElem, isValid);
    }
}