var global_imgaray = new Array();
var global_imgsize = 0;
var curpos = 0;

$(document).ready(function() {
	var imgaray = new Array();
	var imgsize = $("#imagesbox img").length;
	if (imgsize > 0) {
		for (increm = 0; increm < imgsize; increm++) {
			imgaray[increm] = $("#imagesbox img:eq(" + increm + ")").attr("src");
		}
		$("#slideshowcontrol").click(function() { flipClickHandler(this) });
		$("#slideshowbox .scroll").click(function() { scrollSlide($(this).attr("id"),'manual') });
	} else {
		$("#slideshowbox").hide();
	}
	global_imgaray = global_imgaray.concat(imgaray);
	global_imgsize = imgsize;
	
	$("#listman_nav_box dt ul").parent().hover(function(){
		$(this).addClass("open").children("ul").show();
	},function(){
		$(this).removeClass("open").children("ul").hide();
	});
	
	$("#theimage").click(function(){
		if ($("#topleftimgcontainer").hasClass("playing")) {
			$("#slideshowcontrol").click();
		}
		$("#imagesbox a:eq(" + curpos + ")").click();
	});
});

function openWindow(obj) {

  var leftPos = ((screen.availWidth / 2) - 300);
  var topPos = 10;
  var scrheight = screen.availHeight;
  if (scrheight > 900) { var height = 900; } else { var height = (scrheight - 120); }
  var windowStuffs = "height=" + height +  ",width=600,left=" + leftPos + ",top=" + topPos + ",scrollbars=yes,resizable=yes";
  sendme = obj.href;
  
  var tourWindow = window.open(sendme, '', windowStuffs);
  return false;
}

function scrollSlide(direc,call) {
	
	curimg = $("#theimage").attr("src");
	$("#theswapper").remove();
	$("#topleftimgcontainer").append("<img src=\"" + curimg + "\" id=\"theswapper\" />");
	$("#theswapper").show();
	switch (direc) {
		case "next":
			curpos++;
			if (curpos == global_imgsize) {
				curpos = 0;
			}
			break;
		case "prev":
			curpos--;
			if (curpos < 0) {
				curpos = global_imgsize-1;
			}
			break;
		default:
			curpos = direc;
			break;
	}
	$("#theimage").attr("src",global_imgaray[curpos]);
	$("#theswapper").fadeOut("slow");
	if(call == "manual" && $("#topleftimgcontainer").hasClass("playing")) { intervalClear(); }
}

function intervalSet() {
	if(($("#topleftimgcontainer").attr("class")) == "waiting") {
		scrollSlide("0");
	}
	flipInterval = setInterval("scrollSlide('next','auto')", 4000 );
	$("#topleftimgcontainer").attr("class","playing");
	$("#slideshowcontrol").html("Pause");
}

function intervalClear() {
	clearInterval(flipInterval);
	$("#topleftimgcontainer").attr("class","stopped");
	$("#slideshowcontrol").html("Play");
}

function flipClickHandler(elem) {
	var animstat = $("#topleftimgcontainer").attr("class");
	if (animstat == "playing") {
		intervalClear();
	} else {
		intervalSet();
	}
	elem.blur();
	return false;
}