
$(document).ready(function() {

	if(document.getElementById('slideshowBank')) {
	    init_slideShow();
	}

	if(document.getElementById('filmstrip')) {
		filmstrip_init();
	}
	
	if($.browser.msie && parseInt($.browser.version,10) < 7) {
		$('#primaryMenu>ul>li').hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	}
	
	if(document.getElementById('serviceArea')) {
		serviceMap_init();
		setImageMap();
	}
	
	$('a.popup').click(function() {
	    var location = this.href;
	    var newWindow = window.open(location, 'popup', 'toolbar=0,resizable=1,status=0,scrollbars=1,menubar=0,width=700,height=650');
        newWindow.focus();
        return false;
	});
	var height = $('#mainBody').height();
	$('#mainBody').height(height);
});




// Input Focus and Blur:
// Place the inputFocus function on the onfocus event of an input element and the inputBlur on the onblur event.
// Use 'this' for the object parameter. 'css' and 'defaultValue' parameters are optional.

function inputFocus(object, css, defaultValue) {
    var val;
    if(defaultValue) val = defaultValue;
    else val = object.defaultValue;
    if(val == object.value) {
        object.value = "";
    }
    if(css) renderCSS(object, css);
}

function inputBlur(object, css, defaultValue) {
    if(object.value.trim() == "") {
        var val;
        if(defaultValue) val = defaultValue;
        else val = object.defaultValue;
        object.value = val;
        if(css) renderCSS(object, css);
    }
}

function renderCSS(object, css) {
    var attributes = css.split(';');
    for(var x=0;x<attributes.length;x++) {
        var elem = attributes[x].split(':');
        if(elem[0].trim() == 'text-align') {
            object.style.textAlign = elem[1].trim();
        }
        if(elem[0].trim() == 'color') {
            object.style.color = elem[1].trim();
        }
		if(elem[0].trim() == 'class') {
			object.className = elem[1].trim();
		}
    }
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

// Submit on Enter Key
// onkeypress="return enterKey(event,'document.getElementById(\'buttonid\').click()')"
function enterKey(event,code) {
	if(!event) {
		event = window.event;
	}
	if ((event.which || event.keyCode) && ((event.which == 13) || (event.keyCode == 13))) {
		eval(code);
		return false;
	}
	else {
		return true;
	}
}

function emailWindow(location) {
    var newWindow;
    if(location) {
        newWindow = window.open('/whs_emailPopup.aspx?url='+location, 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    else {
        newWindow = window.open('/whs_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);
    }
}


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);
	}
}


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_up').addClass('disabled');
		$('#filmstrip_down').addClass('disabled');
	}
	
	for(var x=0;x<filmstrip_count;x++) {
		frames[x].id = 'film-frame'+(x+1);
	}
	
    filmstrip_src = document.getElementById('primaryImage').src;
	$('#film .frame').hover(
	    function() {
	        document.getElementById('primaryImage').src = this.getElementsByTagName('img')[0].src;
	    },
	    function() {
	        document.getElementById('primaryImage').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;
}


function serviceMap_init() {
	$('#serviceArea li').hover(
		function() {
			this.className = "hover";
			$('#mapOverlay').html($(this).children('.overlay').html());
		},
		function() {
			this.className = "";
			$('#mapOverlay').html('');
		}
	);
}

function setImageMap() {
	
	$('#serviceArea .map a').hover(
		function () {
			this.className = "hover";
			var rel_val = this.rel;
			var target_id = "#serviceAreaLiId" + rel_val;
			$('#mapOverlay').html($(target_id).children('.overlay').html());
			$(target_id).addClass('hover');
		}, 
		function() {
			this.className = "";
			var rel_val = this.rel;
			var target_id = "#serviceAreaLiId" + rel_val;
			$('#mapOverlay').html('');
			$(target_id).removeClass('hover');
		}
	);
}