//Automatic scroll gallery variables
var restarea=50; //1) width of the "neutral" area in the center of the gallery in px
var maxspeed=4; //2) top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var iedom=document.all||document.getElementById;
var scrollspeed=0;
var movestate="";
var actualwidth='';
var cross_scroll, ns_scroll;
var loadedyes=0;

//Slideshow variables
var items, CurrentImage, CurrentTitle, CurrentDescription, Next, Previous, CurrentLink;
var index, maxIndex, previousIndex;
var images, links;

// UPDATE FOR TIMER 11.19.2010 Todd Eberwine
var IsAutoPlay, ImageSpeed, IsLoop;
var to;

function setVars(a, s, l) 
{
    IsAutoPlay = a;
    IsLoop = l;
    ImageSpeed = s;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//Auto Scroll div
addLoadEvent(fillup);

$(document).ready(init);
function init()
{
	//Slideshow setup
	items = $('.smoothSlideShow div');
	index = 0;
	previousIndex = 0;
	maxIndex = items.length;
	//images = $('.smoothSlideShow div .full');
	links = $('.smoothSlideShow div a');
	
	//Hide all slides except the first
	for (var x=0; x<maxIndex; x++)
	{
		items.get(x).id = 'slide'+x;
		$('#' + items.get(x).id).hide();
	}
	$('#' + items.get(index).id).show();
	
	//Set up link
	$('.smoothSlideShow div .full').bind('click', followLink);
	
	//Next previous button
	$('.smoothSlideShow').append('<div id="Next"></div>');
	Next = $('#Next');
	Next.bind('click', moveNext);
	$('.smoothSlideShow').append('<div id="Previous"></div>');
	Previous = $('#Previous');
	Previous.addClass("disabled");
	Previous.bind('click', movePrevious);
	
	//Set up filmStrip view
	$('.smoothSlideShow').append('<div id="filmStripContainer"></div>');
	//Button
	$('#filmStripContainer').append('<div id="filmStripButton">Thumbnails</div>');
	$('#filmStripButton').click(toggleFilmStrip);
	//filmStrip
	$('#filmStripContainer').append('<div id="filmStripBack"></div>');
	$('#filmStripContainer').append('<div id="filmStrip"></div>');
	//thumbnails
	$('.smoothSlideShow div .thumb').appendTo('#filmStrip');
	for (var x=0; x<maxIndex; x++)
		$('#filmStrip .thumb').get(x).index = x;
	$('#filmStrip .thumb').hover(function(){$(this).css('opacity', 1.0);}, function(){$(this).css('opacity', 0.7);});
	$('#filmStrip .thumb').click(function() { clearTimeout(to); set($(this).get(0).index); toggleFilmStrip(); });
	
	//Setup user defined width and height
	$('.smoothSlideShow').css("width", maxWidth + 50);
	$('.smoothSlideShow').css("height", maxHeight + 72);
	$('.smoothSlideShow div p').css("width", maxWidth + 30);
	$('#Next, #Previous').css("top", (maxHeight + 72)/2-24);
	$('#filmStrip, #filmStripBack').css("height", thumbHeight + 28);

	if (IsAutoPlay == "True") {
	    if (index == maxIndex - 1) {
	        if (IsLoop == "True") {
	            to = setTimeout("set(0)", parseInt(ImageSpeed) + 2000);
	        }
	    }
	    else {
	        to = setTimeout("set(index+1)", parseInt(ImageSpeed) + 2000);
	    }
	}
}

//Slideshow functions
function toggleFilmStrip()
{
	if ($('#filmStripContainer').css('height')==thumbHeight + 50 + 'px')
	{
		$('#filmStripContainer').css('height', '22px');
	}
	else
	{
		$('#filmStripContainer').css('height', thumbHeight + 50 + 'px');
	}
}

function set(NewIndex)
{
	previousIndex = index;
	index = NewIndex;
	$('#' + items.get(index).id + ' p').animate({height:'hide', opacity:'hide'}, 1);
	$('#' + items.get(index).id + ' h1').animate({height:'hide', opacity:'hide'}, 1);
	$('#' + items.get(previousIndex).id + ' h1').animate({height:'hide', opacity:'hide'}, 400)
	$('#' + items.get(previousIndex).id + ' p').animate({height:'hide', opacity:'hide'}, 400, function() {
		$('#' + items.get(previousIndex).id).fadeOut(800, function(){
			$('#' + items.get(index).id).fadeIn(800, function() {
				$('#' + items.get(index).id + ' p').animate({height:'show', opacity:'show'}, 400);
				$('#' + items.get(index).id + ' h1').animate({height:'show', opacity:'show'}, 400);
			});
		});
	});
	if (index == maxIndex-1)
		Next.addClass("disabled");
	if (index > 0)
		Previous.removeClass("disabled");
	if (index == 0)
		Previous.addClass("disabled");
	if (index < maxIndex - 1)
	    Next.removeClass("disabled");

	if (IsAutoPlay == "True") {
	    if (index == maxIndex - 1) {
	        if (IsLoop == "True") {
	            to = setTimeout("set(0)", parseInt(ImageSpeed) + 2000);
	        }
	    }
	    else {
	        to = setTimeout("set(index+1)", parseInt(ImageSpeed) + 2000);
	    }
	}
}

function moveNext()
{
		if (index < maxIndex-1)
		{
			set(index + 1);
		}
}

function movePrevious()
{
		if (index > 0)
		{
			set(index-1);
		}
}

function followLink()
{
	window.location = links.get(index).href;
}

//Automatic scroll functions
function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}


function moveleft(){
	if (loadedyes){
		movestate="left";
		if (iedom&&parseInt($('#filmStrip').css('left'))>=(menuwidth-actualwidth)){
			$('#filmStrip').css('left',parseInt($('#filmStrip').css('left'))-scrollspeed+"px");
		}
	}
	lefttime=setTimeout("moveleft()",10);
}

function moveright(){
	if (loadedyes){
		movestate="right";
		if (iedom&&parseInt($('#filmStrip').css('left'))<0){
			cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px";
		}
	}
	righttime=setTimeout("moveright()",10);
}

function motionengine(e){
	var dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft;
	var dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop;
	var curposy=window.event? event.clientX : e.clientX? e.clientX: "";
	curposy-=mainobjoffset-dsocx;
	var leftbound=(menuwidth-restarea)/2;
	var rightbound=(menuwidth+restarea)/2;
	if (curposy>rightbound){
		scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed;
		if (window.righttime) clearTimeout(righttime);
		if (movestate!="left") moveleft();
	}
	else if (curposy<leftbound){
		scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed;
		if (window.lefttime) clearTimeout(lefttime);
		if (movestate!="right") moveright();
	}
	else
		scrollspeed=0;
}

function contains_ns6(a, b) {
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;
	return false;
}

function stopmotion(e){
	if ((window.event&&!crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))){
		if (window.lefttime) clearTimeout(lefttime);
		if (window.righttime) clearTimeout(righttime);
		movestate="";
	}
}

function fillup(){
	if (iedom){
		crossmain = document.getElementById? document.getElementById("filmStripContainer") : document.all.filmStripContainer;
		menuwidth=parseInt($('.smoothSlideShow').css('width'));
		mainobjoffset=getposOffset(crossmain, "left");
		cross_scroll=document.getElementById? document.getElementById("filmStrip") : document.all.filmStrip;
		actualwidth = cross_scroll.scrollWidth;
		//actualwidth=document.all? cross_scroll.offsetWidth : document.getElementById("filmStrip").offsetWidth;
		cross_scroll.onmousemove=function(e){
			motionengine(e);
		}
		cross_scroll.onmouseout=function(e){
			stopmotion(e);
		}
	}
	loadedyes=1;
}
