$(document).ready(function() {

	if ($('#primaryMenu')) {setNav();}

	if($('#horizontalFrame #horizontalSlide img').length > 0) {
		filmstrip_init();
		imageSwitch();
	}
	
	if (document.getElementById("imageField")) {setImageSwap();}
	if (document.getElementById("vert_frame")) {vertstrip_init(); setImagesClickable();}
	if (document.getElementById("gallery")) {setGalleryImages();}
	if (document.getElementById("backgroundImage")) {
		var numOfSlides = $('#backgroundImage .slide').length;
		if (numOfSlides > 1) {
			slideshow_init();
		}
	}
});

function setNav() {
	$('#primaryMenu li').hover(
	
		function() {
			this.className = "hover";
			var width = this.clientWidth + "px";
			$(this).children('ul').css('width',width);
		},
		function() {
			this.className = "";
		}
	);
}

function setGalleryImages() {
	$('#gallery .galleryRow>div').hover(
		function() {
			this.className = "hover";
			$(this).css('z-index','1000');
			$(this).css('position','relative');
		},
		function() {
			this.className = "";
			$(this).css('z-index','1');
			$(this).css('position','static');
		}
	);
	
	$('#gallery .contain').click(
		function() {
			
			var popUpInfo = $(this).children('.popUpInfo')[0].innerHTML;	
			
			$('#galleryPopUp').html(popUpInfo); 
			$('#galleryPopUp').css('display','block');
			
			var popUpImage = $('#galleryPopUp').children('img')[0];
			var fullImageWidth = popUpImage.width + 40;
			var fullSizeLeft = (992 - fullImageWidth)/2 + "px";
			
			$('#galleryPopUp').css('left', fullSizeLeft);
			$('#galleryPopUp').css('width', fullImageWidth);
			
			$('#galleryPopUp .closePopUp').click(
				function() {
					$('#galleryPopUp').css('display','none');
					return false;
				}
			);
			return false;
		}
	);
}

function setImageSwap() {
	$('#imageField div').hover(
		function() {
			var current_class = this.className;
			var wanted_class = current_class.substring(0, 2) + "fullSize";
			$('.' + wanted_class).css('display','block');
		}
	);
	
	$('#fullSize div').mouseout(
		function() {
			$(this).css('display','none');
		}
	);
}

function imageSwitch() {
	
	var images = $('#displayImage div');
	var num_img = $('#displayImage div').length;
	
	for(var x=0;x<num_img;x++) {images[x].id = 'img'+(x+1);}
	
	for(var x=0;x<num_img;x++) {$('#img'+(x+1)).css('display','none');}
	
	$('#horizontalSlide img').hover(
		function() {
			var current_id = this.id;
			var id_length = current_id.length - 1;
			var wanted_id = current_id.substring(id_length);
			$('#img' + wanted_id).css('display','block');
		},
		function() {
			var current_id = this.id;
			var id_length = current_id.length - 1;
			var wanted_id = current_id.substring(id_length);
			$('#img' + wanted_id).css('display','none');
		}
	);
}


/* Horizontal Scroll 
------------------------------------ */

var filmstrip_num = 1;
var filmstrip_count = 0;

function filmstrip_init() {

	document.getElementById('horizontalFrame').scrollLeft = 0;
	var frames = $('#horizontalSlide img');
	filmstrip_count = frames.length;
	
	if(filmstrip_count > 5) {
		$('#slide_left').addClass('disabled');
	}
	else {
	    $('#slide_left').css('display','none');
		$('#slide_right').css('display','none');
	}
	
	for(var x=0;x<filmstrip_count;x++) {frames[x].id = 'film-frame'+(x+1);}
	
	$('#slideRight').click(function() {filmstrip_left();});
	$('#slideLeft').click(function() {filmstrip_right();});
	
}

function filmstrip_left() {
	filmstrip_num++;
	if(filmstrip_num > filmstrip_count - 4) {
		filmstrip_num = filmstrip_count - 4;
	}
	else {
		filmstrip_move(filmstrip_num);
	}
}
function filmstrip_right() {
	filmstrip_num--;
	
	if(filmstrip_num < 1) {
		filmstrip_num = 1;
	}
	else {
		filmstrip_move(filmstrip_num);
	}
}

function filmstrip_buttons() {
	if(filmstrip_num >= filmstrip_count - 4) {
		$('#slide_right').addClass('disabled');
		$('#slide_left').removeClass('disabled');
	}
	else if(filmstrip_num <= 1) {
		$('#slide_left').addClass('disabled');
		$('#slide_right').removeClass('disabled');
	} 
	else {
		$('#slide_left').removeClass('disabled');
		$('#slide_right').removeClass('disabled');
	}
}

function filmstrip_move(num) {
	filmstrip_buttons();
	var theScroll = document.getElementById('horizontalFrame');
	var position = findElementPos(document.getElementById("film-frame" + num));
	var offsetPos = findElementPos(document.getElementById('film-frame1'));
	position[0] = position[0] - offsetPos[0];
	filmstrip_start(theScroll, theScroll.scrollLeft, position[0]);
}

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.scrollLeft = 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 setImagesClickable () {
	$(".vert_slide .img").click(
		function() {
			var thisImage = this.innerHTML;
			
			$(".projectPopUp").css("display","block");
			$(".imgHere").html(thisImage);
			
			var imgWidth = $(".imgHere img")[0].width;
			var left = (992 - imgWidth) / 2;
			var posLeft = left + "px";
			var popUpWidth = (imgWidth) + "px";
			
			$(".projectPopUp").css("width",popUpWidth);
			$(".projectPopUp").css("left",posLeft);
			return false;
		}
	);
	$(".projectPopUp .closePopUp").click(
		function() {
			$(".projectPopUp").css("display","none");
			return false;
		}
	);
}

/* Vertical Scroll 
------------------------------------ */

var vertstrip_num = 1;
var vertstrip_count = 0;

function vertstrip_init() {
	document.getElementById('vert_frame').scrollTop = 0;
	var frames = $('.vert_slide .img');
	vertstrip_count = frames.length;
	
	if(vertstrip_count > 3) {
		$('#slide_up').addClass('disabled');
	}
	else {
	//  $('#slide_up').css('display','none');
	//	$('#slide_down').css('display','none');
	}
	
	for(var x=0;x<vertstrip_count;x++) {
		frames[x].id = 'film-frame'+(x+1);
	}

	$('#slide_up').click(function() {filmstrip_up();});
	$('#slide_down').click(function() {filmstrip_down();});
	
}

function filmstrip_down() {
	vertstrip_num++;
	if(vertstrip_num > vertstrip_count - 3) {
		vertstrip_num = vertstrip_count - 3;
	}
	else {
		vertstrip_move(vertstrip_num);
	}
}
function filmstrip_up() {
	vertstrip_num--;
	if(vertstrip_num < 1) {
		vertstrip_num = 1;
	}
	else {
		vertstrip_move(vertstrip_num);
	}
}

function vertstrip_buttons() {
	if(vertstrip_num >= vertstrip_count - 3) {
		$('#slide_down').addClass('disabled');
		$('#slide_up').removeClass('disabled');
	}
	else if(vertstrip_num <= 1) {
		$('#slide_up').addClass('disabled');
		$('#slide_down').removeClass('disabled');
	}
	else {
		$('#slide_up').removeClass('disabled');
		$('#slide_down').removeClass('disabled');
	}
}

function vertstrip_move(num) {

	vertstrip_buttons();
	var theScroll = document.getElementById('vert_frame');
	var position = findElementPos(document.getElementById("film-frame" + num));
	var offsetPos = findElementPos(document.getElementById('film-frame1'));
	position[1] = position[1] - offsetPos[1];
	vertstrip_start(theScroll, theScroll.scrollTop, position[1]);
}

var vertstrip_anim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function vertstrip_start(elem, start, end) {

	if (vertstrip_anim.timer != null) {
		clearInterval(vertstrip_anim.timer);
		vertstrip_anim.timer = null;
	}
	vertstrip_anim.time = 0;
	vertstrip_anim.begin = start;
	vertstrip_anim.change = end - start;
	vertstrip_anim.duration = 50;
	vertstrip_anim.element = elem;
	
	vertstrip_anim.timer = setInterval("vertstrip_scroll();", 15);

}
function vertstrip_scroll() {
	
	if (vertstrip_anim.time > vertstrip_anim.duration) {
		clearInterval(vertstrip_anim.timer);
		vertstrip_anim.timer = null;
	}
	else {
		move = sineInOut(vertstrip_anim.time, vertstrip_anim.begin, vertstrip_anim.change, vertstrip_anim.duration);
		vertstrip_anim.element.scrollTop = move;
		vertstrip_anim.time++;
	}
}


function emailWindow(location) {
    var newWindow;
    if(location) {
        newWindow = window.open('/wce_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    else {
        newWindow = window.open('/wce_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    newWindow.focus();
}
 
/*--- Homepage Fading Cycle --
------------------------------*/

var slideshow_defaults = {slideSpeed:6000, fadeSpeed:25, fadeChange:5};
var slideshow = new Array();
function slideshow_init() {

	var frame = $('.slideshow');
	for(var y=0;y<frame.length;y++) {
		slideshow[y] = {id:'slideshow',status:0,rotate:null,fade:null,number:0,previous:0,count:0,opacity:0};
		
		var currentSlide = frame[y];
		currentSlide.style.position = "relative";
		if(currentSlide.id.length < 1) {
			currentSlide.id = "slideshow"+y;
		}
		slideshow[y].id = currentSlide.id;
		
		var slides = $(currentSlide).children('.slide');
		slideshow[y].count = slides.length;
		for(var x=0;x<slides.length;x++) {
			slides[x].style.position = "absolute";
			slides[x].style.top = "0px";
			slides[x].style.left = "0px";
			if(x > 0) {
				slides[x].style.zIndex = "100";
				slides[x].style.opacity = "0";
				slides[x].style.filter = "alpha(Opacity=0)";
			}
			else {
				slides[0].style.zIndex = "200";
			}
		}
		slideshow_callInterval(y);
	}

	$('.slideNext').click(function() {
		if(this.rel.length > 0) {
			var slideId = this.rel;
			var slideNum = slideshow_getFromId(slideId);
			slideshow_rotate(slideNum,'++');
		}
		else {
			for(var j=0;j<slideshow.length;j++) {
				slideshow_rotate(j,'++');
			}
		}
		return false;
	});
	$('.slidePrevious').click(function() {
		if(this.rel.length > 0) {
			var slideId = this.rel;
			var slideNum = slideshow_getFromId(slideId);
			slideshow_rotate(slideNum,'--');
		}
		else {
			for(var j=0;j<slideshow.length;j++) {
				slideshow_rotate(j,'--');
			}
		}
		return false;
	});
	$('.slidePause').click(function() {
		if(this.rel.length > 0) {
			var slideId = this.rel;
			var slideNum = slideshow_getFromId(slideId);
			if(slideshow[slideNum].status == 0) {
				clearTimeout(slideshow[slideNum].rotate);
				slideshow[slideNum].status = 1;
				$(this).removeClass('slidePause');
			}
			else {
				slideshow_callInterval(slideNum);
				slideshow[slideNum].status = 0;
				$(this).addClass('slidePause');
			}
		}
		else {
			for(var j=0;j<slideshow.length;j++) {
				if(slideshow[j].status == 0) {
					clearTimeout(slideshow[j].rotate);
					slideshow[j].status = 1;
				}
				else {
					slideshow_callInterval(j);
					slideshow[j].status = 0;
				}
			}
		}
		return false;
	});

}

function slideshow_getFromId(id) {
	var num = -1;
	for(var i=0;i<slideshow.length;i++) {
		if(id == slideshow[i].id) {
			num = i;
		}
	}
	return num;
}
function slideshow_callInterval(num) {
	slideshow[num].rotate = setTimeout(function() { slideshow_rotate(num); }, slideshow_defaults.slideSpeed);
}
function slideshow_state(y,num,state) {
	var node = $('#'+slideshow[y].id).children('.slide')[num];
	switch(state) {
		case 1:
			node.style.zIndex = "100";
			node.style.opacity = "0";
			node.style.filter = "alpha(Opacity=0)";
			break;
		case 2:
			node.style.zIndex = "150";
			node.style.opacity = "1";
			node.style.filter = "alpha(Opacity=100)";
			break;
		case 3:
			node.style.zIndex = "200";
			node.style.opacity = (slideshow[y].opacity / 100);
			node.style.filter = "alpha(Opacity="+slideshow[y].opacity+")";
			break;
	}
}
function slideshow_bounds(y,num) {
	if(num >= slideshow[y].count) {
		num = 0;
	}
	if(num < 0) {
		num = slideshow[y].count - 1;
	}
	return num;
}

function slideshow_rotate(y,operator) {
	clearInterval(slideshow[y].fade);
	clearTimeout(slideshow[y].rotate);
	
	if(slideshow[y].previous != slideshow[y].number) {
		// hide the previous slide
		slideshow_state(y,slideshow[y].previous,1);
		// set the previous slide to the current
		slideshow[y].previous = slideshow[y].number;
		slideshow[y].opacity = 50;
	}

	// increment the current slide
	if(operator == '++') {
		slideshow[y].number = slideshow[y].number + 1;
	}
	else if(operator == '--') {
		slideshow[y].number = slideshow[y].number - 1;
	}
	else {
		slideshow[y].number = slideshow[y].number + 1;
	}
	slideshow[y].number = slideshow_bounds(y,slideshow[y].number);
	
	// set the slides to the correct states
	slideshow_state(y,slideshow[y].previous,2);
	slideshow_state(y,slideshow[y].number,3);
	
	slideshow[y].fade = setInterval(function() {slideshow_fade(y); }, slideshow_defaults.fadeSpeed);
}

function slideshow_fade(y) {
	var nextNode = $('#'+slideshow[y].id).children('.slide')[slideshow[y].number];
	slideshow[y].opacity = slideshow[y].opacity + slideshow_defaults.fadeChange;
	if(slideshow[y].opacity >= 100) {
		nextNode.style.opacity = "1";
		nextNode.style.filter = "alpha(Opacity=100)";
		// hide the previous slide
		slideshow_state(y,slideshow[y].previous,1);
		
		slideshow[y].opacity = 0;
		slideshow[y].previous = slideshow[y].number;
		clearInterval(slideshow[y].fade);
		if(slideshow[y].status == 0) {
			slideshow[y].rotate = setTimeout(function() { slideshow_rotate(y); }, slideshow_defaults.slideSpeed);
		}
	}
	else {
		nextNode.style.opacity = (slideshow[y].opacity / 100);
		nextNode.style.filter = "alpha(Opacity="+slideshow[y].opacity+")";
	}
}
