$(document).ready(function() {
	
	if ($('#main_nav')) {setNav();}
	if ($('.qanda')) {setToggle();}
 	
	if (document.getElementById('horz_frame')) {
		filmstrip_init();
		setSlideImg();
	}
	
	if (document.getElementById('vert_slider_box')) {vertstrip_init();}
	
	if (document.getElementById('vert_frame')) {
		setVertImg();
	}
	
	if($('.slideshow .slide').length > 0) {
		slideshow_init();
	}
	
	if($.browser.msie && parseInt($.browser.version,10) < 7) {
	    if(document.getElementById('overlay_image')) {
	        fixPNG();
	    }
	   
    }
	
});

function setNav() {
	$('#main_nav li').hover(
		function() {
			this.className = "hover";
		},
		function() {
			this.className = "";
		}
	);
}

function setSlideImg() {
	$('#horz_frame .horz_slide a').hover(
		function() {
			this.className = "hover";
		},
		function() {
			this.className = "";
		}
	);
}

function setVertImg() {
	$('.vert_slide .img').hover(
		function() {
			this.className = "img_hover";
		},
		function() {
			this.className = "img";
		}
	);
}

function setToggle() {

	$('.answer a.more').click(function() {
	    this.parentNode.parentNode.className = "qanda_full";
        return false;
	});
	
	$('.answer_full a.close').click(function() {
		this.parentNode.parentNode.className = "qanda";
	    return false;	
	});
}


/* Horizontal Scroll 
------------------------------------ */

var filmstrip_num = 1;
var filmstrip_count = 0;

function filmstrip_init() {
	document.getElementById('horz_frame').scrollLeft = 0;
	var frames = $('.horz_slide a');
	filmstrip_count = frames.length;
	
	if(filmstrip_count > 3) {
		$('#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);}
	
	$('#slide_right').click(function() {filmstrip_left();});
	$('#slide_left').click(function() {filmstrip_right();});
	
}

function filmstrip_left() {
	filmstrip_num++;
	if(filmstrip_num > filmstrip_count - 2) {
		filmstrip_num = filmstrip_count - 2;
	}
	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 - 2) {
		$('#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('horz_frame');
	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;
}


/* 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 - 4) {
		vertstrip_num = vertstrip_count - 4;
	}
	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 - 4) {
		$('#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('/wces_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    else {
        newWindow = window.open('/wces_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    newWindow.focus();
}

/*--- Homepage Fading Cycle --
------------------------------*/

var slideshow_defaults = {slideSpeed:2000, 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+")";
	}
}

function fixPNG() {
    $('#overlay_image img').each( function() {
        var img_src = $(this).attr('src');
        var img_src_length = img_src.length;
        var img_src_last = img_src_length - 3;
        var img_type = img_src.substring(img_src_last);

        if (img_type == "png") {
       
            $(this).css('display', 'none');
            var img_width = $(this).attr('width');
            var img_height = $(this).attr('height'); 
            $('#overlay_image').css('width', img_width);
            $('#overlay_image').css('height', img_height);   
            $('#overlay_image').attr('writing-mode', 'tb-rl');
            $('#overlay_image').css('background-image', 'none');
            $('#overlay_image').css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+img_src+'",sizingMethod="scale")' );
        }
    });
}
