$(document).ready(function() {

	if ($('#primaryMenu')) {setNav();}

	if($('#horizontalFrame #horizontalSlide img').length > 0) {
		filmstrip_init();
	//	imageSwitch();
	}
	else {
	    $('#slideLeft').css('display','none');
		$('#slideRight').css('display','none');
	}
	
	if (document.getElementById("imageField")) {imageSwap.init();}
	
	var mainHeight = parseInt($('#mainContent').height());
	if (mainHeight < 500) {
	    $('#mainContent').height('500px');
	}

    if (document.getElementById('leftNav')) {
        if($.browser.msie && parseInt($.browser.version,10) < 7) {
            removeBg();
        }
    }	
});

function removeBg() {

    $('#leftNav li a').hover(
        function() {
            this.className = "change";
          },
        function() {
            this.className = "";
        }
    );

}

function setNav() {
	$('#primaryMenu>li').hover(
	
		function() {
			this.className = "hover";
			
		},
		function() {
			this.className = "";
		}
	);
}

function emailWindow(location) {
    var newWindow;
    if(location) {
        newWindow = window.open('/re_emailPopup.aspx?url='+location, 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    else {
        newWindow = window.open('/re_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    newWindow.focus();
}


var imageSwap = {
	current : null,
	timeout : null,
	init : function() {
		var self = this;
		$('#imageField div').hover(
			function() {
				var current_class = this.className;
			    var class_length = current_class.length;
			    var wanted_class = "full" + current_class.substring(class_length - 2, class_length);
			    $('.' + wanted_class).css('display','block');
			}
		);
		
		$('#fullSize div').hover(
			function() {
				clearTimeout(self.timeout);
			},
			function() {
				self.current = this;
				self.timeout = setTimeout(function() { self.close() },20);
			}
		);
		
		$('#fullSize div span').mouseover(function() {
			clearTimeout(self.timeout);
		});
	},
	close : function() {
		 $(this.current).css('display','none');
	}
}
/*
function setImageSwap() {
	$('#imageField div').hover(
		function() {
			var current_class = this.className;
			var class_length = current_class.length;
			var wanted_class = "full" + current_class.substring(class_length - 2, class_length);
			$('.' + wanted_class).css('display','block');
		}
	);
	
	$('#fullSize div').mouseout(
		function() {
			$(this).css('display','none');
		}
	);
}
/* no longer using this feature for now
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) {
		$('#slideLeft').addClass('disabled');
	}
	else {
	    $('#slideLeft').css('display','none');
		$('#slideRight').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) {
		$('#slideRight').addClass('disabled');
		$('#slideLeft').removeClass('disabled');
	}
	else if(filmstrip_num <= 4) {
		$('#slideLeft').addClass('disabled');
		$('#slideRight').removeClass('disabled');
	} 
	else {
		$('#slideLeft').removeClass('disabled');
		$('#slideRight').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;
}
