var loaderImg      = new Image();
var CTX = "";

var SLIDE_pic      = new Array();
var SLIDE_picTitle = new Array();
var SLIDE_picDesc  = new Array();
var SLIDE_picCode  = new Array();
var SLIDE_load     = new Array();

var SLIDE_orientation   = new Array();
var SLIDE_status = 'SLIDE_pause';
var SLIDE_timeout;
var SLIDE_loadImageTimeout;
var SLIDE_actual    = 1;
var SLIDE_previous  = 1;
var SLIDE_count     = 0;
var SLIDE_speed     = 8000;
var SLIDE_loadSpeed = 3000;
var SLIDE_fade      = 2;
var alignThumbAttempt = 0;
var SLIDE_started = false;

var autoLoadImage = false;
var SLIDE_show = false;
var firstTime = true;

var flashvars  = {};
var params     = {
		menu: "true",
		play: "false",
		loop: "false",
		quality: "high",
		bgcolor: "#000000"
};
var attributes = {};

function SLIDE_loadImage(index) {
	SLIDE_load[index] = new Image();
	SLIDE_load[index].src = SLIDE_pic[index];
}

function SLIDE_imageLoader() {
	var success = false;
	for(i = 1; i <= SLIDE_count; i++) {
		if(SLIDE_load[i] == null) {
			SLIDE_loadImage(i);
			success = true;
			break;
		}
	}
	
	if(success) {
		SLIDE_loadImageTimeout = setTimeout("SLIDE_imageLoader()",SLIDE_loadSpeed);
	}else {
		clearTimeout(SLIDE_loadImageTimeout);
	}
}

function SLIDE_init() {
	SLIDE_count = SLIDE_pic.length-1;
	if(SLIDE_count > 0) {
		SLIDE_loadImage(1);
		if( autoLoadImage ) {
			if(SLIDE_speed !=SLIDE_loadSpeed) {
				SLIDE_loadImageTimeout = setTimeout("SLIDE_imageLoader()",SLIDE_loadSpeed);
			}
		}
	}
}

function SLIDE_pause(){
	  clearTimeout(SLIDE_timeout);
	  SLIDE_status = 'SLIDE_pause';
	  if(document.getElementById('SLIDE_pauseButton') != null) {
		  document.getElementById('SLIDE_pauseButton').style.display = "none";
	  }
	  if(document.getElementById('SLIDE_playButton') != null) {
		  document.getElementById('SLIDE_playButton').style.display = "block";
	  }
}

function SLIDE_player() {
	if(SLIDE_count <=0 || SLIDE_status != 'SLIDE_play') {
		SLIDE_pause();
		return;
	}
	  SLIDE_actual++;
	  SLIDE_slide();
	  SLIDE_timeout = setTimeout("SLIDE_player()",SLIDE_speed);
}

function SLIDE_start(){
	if(SLIDE_count > 0 && SLIDE_started == false) {
		SLIDE_slide();
		SLIDE_started = true;
		if(SLIDE_show) {
			SLIDE_timeout = setTimeout("SLIDE_player()",SLIDE_speed+4000);
		}
	}
}

function SLIDE_play(){
  SLIDE_status = 'SLIDE_play';
  if(document.getElementById('SLIDE_playButton') != null) {
	  document.getElementById('SLIDE_playButton').style.display = "none";
  }
  if(document.getElementById('SLIDE_pauseButton') != null) {
	  document.getElementById('SLIDE_pauseButton').style.display = "block";
  }
  
  SLIDE_player();
}


function SLIDE_playAndPause(){
	if( SLIDE_status == 'SLIDE_play') {
		SLIDE_pause();
	}else {
		SLIDE_play();
	}
}

/*function SLIDE_back() {
	clearTimeout(SLIDE_timeout);
	SLIDE_actual--;
	SLIDE_slide();
	if (SLIDE_status != 'SLIDE_pause') SLIDE_timeout = setTimeout("SLIDE_play()",SLIDE_speed);
}

function SLIDE_forward() {
	clearTimeout(SLIDE_timeout);
	SLIDE_actual++;
	SLIDE_slide();
	if (SLIDE_status != 'SLIDE_pause') SLIDE_timeout = setTimeout("SLIDE_play()",SLIDE_speed);
}*/

function SLIDE_set(index) {
	if (index > SLIDE_count || index < 1) {
		return;
	}

	//SLIDE_pause();
	SLIDE_actual = index;
	SLIDE_slide();
}

function setVideoType(imgHolder) {
	var ext_avi  = /.avi$/i;
	var ext_swf  = /.swf$/i;
	var ext_flv  = /.flv$/i;
	var ext_mov  = /.(mov|qt)$/i;
	var ext_mpeg = /.mp(2|a|e|eg|g|v2)$/i;
	var ext_movie  = /.movie$/i;
	var ext_la_asf  = /.(lsf|lsx)$/i;
	var ext_ms_asf  = /.as[frx]$/i;

	if( ext_avi.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/x-msvideo";
	}else if( ext_swf.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "application/x-shockwave-flash";
	}else if( ext_flv.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/x-flv";
	}else if( ext_mov.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/quicktime";
	}else if( ext_mpeg.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/mpeg";
	}else if( ext_movie.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/x-sgi-movie";
	}else if( ext_la_asf.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/x-la-asf";
	}else if( ext_ms_asf.test(SLIDE_pic[SLIDE_actual])) {
		imgHolder.type = "video/x-ms-asf";
	}else {
		imgHolder.type = "";
	}
}

function SLIDE_slide() {
	var browser = navigator.appName.toString();

	if (document.getElementById("THUMB_" + SLIDE_previous) !=null ) {
		var thumbObj =  document.getElementById("THUMB_" + SLIDE_previous).style;
		thumbObj.borderColor  = "";
		thumbObj.borderWidth  = "";
	}

	if (SLIDE_actual > (SLIDE_count)) SLIDE_actual=1;
	if (SLIDE_actual < 1) SLIDE_actual = SLIDE_count;
	
	SLIDE_adjustThumb();
	
	if(document.getElementById("SLIDE_VIDEO") != null) {
		flowplayer("SLIDE_picBoxID", CTX + "/plugins/flowplayer-3.2.7.swf", {
			clip : {
        		autoPlay: true,
        		autoBuffering: true
    		},

    		playlist: [
    	           SLIDE_pic[SLIDE_actual]
    	    ]			
		});
		
		loadPicDesc();
		SLIDE_previous = SLIDE_actual;
		return;
	}
	
	if( SLIDE_load[SLIDE_actual] == null) {
		document.images.SLIDE_picBox.src = loaderImg.src;
		if(document.getElementById("img-viewer") != null) {
			document.getElementById("img-viewer").align = "center";
			document.getElementById("img-viewer").vAlign = "middle";
		}
		
		SLIDE_loadImage(SLIDE_actual);
		
		if( browser.indexOf("Netscape")>=0 ||  browser.indexOf("Microsoft")>=0 ){
		    function testImg(){
		        if(SLIDE_load[SLIDE_actual].complete != null && SLIDE_load[SLIDE_actual].complete == true){ 
		        	SLIDE_slide2();
		                return;
		        }
		        setTimeout(testImg, 1000);
		    }
		    setTimeout(testImg, 1000);
		}else{
			SLIDE_load[SLIDE_actual].onload = function() { SLIDE_slide2(); }
		}
		
	} else {
		SLIDE_slide2();
	}
}

function SLIDE_slide2() {
	/*if (SLIDE_started == true && document.all != null) {
		if(document.images.SLIDE_picBox != null) {
			document.images.SLIDE_picBox.style.filter="blendTrans(duration=2)";
			document.images.SLIDE_picBox.style.filter="blendTrans(duration=SLIDE_fade)";
			document.images.SLIDE_picBox.filters.blendTrans.Apply();
			document.images.SLIDE_picBox.src = SLIDE_load[SLIDE_actual].src;
			document.images.SLIDE_picBox.filters.blendTrans.Play();
		}

	} else {
		if(document.images.SLIDE_picBox != null ) {
			document.images.SLIDE_picBox.src = SLIDE_load[SLIDE_actual].src;
		}
	}*/

	if(firstTime || document.images.SLIDE_picBox.src == loaderImg.src) {
		FadeInImage('SLIDE_picBoxID', SLIDE_load[SLIDE_actual].src, false);
		firstTime = false;
	}else {
		FadeOutImage('SLIDE_picBoxID');
	}

	SLIDE_previous = SLIDE_actual;
}

function scrollThumb(scrollPos) {
	if (document.getElementById('THUMB_SCROLL') !=null) {
		if( scrollPos > 0 ) {
			document.getElementById('THUMB_SCROLL').scrollLeft = scrollPos;
		} else {
			document.getElementById('THUMB_SCROLL').scrollLeft = 0;
		}
	}
}

function SLIDE_adjustThumb() {
	scrollThumb(0);
	
	if (document.getElementById("THUMB_" + SLIDE_actual) !=null ) {
		var thumbObj =  document.getElementById("THUMB_" + SLIDE_actual);
		thumbObj.style.borderColor  = "orange";
		thumbObj.style.borderWidth  = "2px";
		
		var offset;
		if( thumbObj.offsetLeft ) {
			offset =  thumbObj.offsetLeft;
		}else {
			offset = thumbObj.x;
		}


		var firstOffset = 0;
		var firstThumbObj  = document.getElementById("THUMB_1");
		if( firstThumbObj.offsetLeft ) {
			firstOffset =  firstThumbObj.offsetLeft;
		}else {
			firstOffset = firstThumbObj.x;
		}

		var newOffset = offset - firstOffset;
		scrollThumb( newOffset - 200);
	}
}

function SLIDE_speeds_inc() {
	SLIDE_speed = SLIDE_speed + 1000;
	document.getElementById("SLIDE_speedText").innerHTML = "seconds " + (SLIDE_speed/1000);
}

function SLIDE_speeds_dec() {
	if (SLIDE_speed > 1000) {
		SLIDE_speed = SLIDE_speed - 1000;
	}
	document.getElementById("SLIDE_speedText").innerHTML = "seconds " + (SLIDE_speed/1000);
}

function thumbScrollDown() {
	document.getElementById('thumbHolder').scrollTop += 468;	
}

function thumbScrollUp() {
	var topVal = document.getElementById('thumbHolder').scrollTop;
	
	if (topVal > 0)
		topVal -=468;

	if (topVal < 0)
		topVal = 0;
	
	document.getElementById('thumbHolder').scrollTop = topVal;	
}

function setPosition(obj, offLeftsize, offTopsize) {
	var curleft = curtop = 0;
	var img = obj;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	if(offTopsize != 0 ) {
		curtop = curtop-(img.offsetParent.offsetTop + offTopsize );
		img.style.top = curtop + "px";
	}

	if(offLeftsize != 0) {
		curleft = curleft - (img.offsetParent.offsetLeft + offLeftsize);
		img.style.left = curleft + "px";
	}	
}

function unSetPosition(obj) {
	obj.style.top='';
	obj.style.left='';
}

function SLIDE_rotate_left() {
	if(document.images.SLIDE_picBox != null) {
		alert(document.images.SLIDE_picBox.style);
	}
}

function SLIDE_rotate_right() {
	if(document.images.SLIDE_picBox != null) {
	}
}

function alignThumb() {
	alignThumbAttempt++;
	for(var i=1; i<=21;i++) {
		var thumbObj =  document.getElementById("THUMB_IMG_" + i);
		if(thumbObj == null) {
			return;
		}
		var height = thumbObj.height;
		if(height !=null && height <=0 && alignThumbAttempt<5) {
			setTimeout("alignThumb()", 2000);
			return;
		}
		if(height != null && height > 0 && height < 50) {
			var offset = (50 - height)/2;
			offset = parseInt(offset);
			thumbObj.style.marginTop = offset + "px";
		}
	}
}

function SetOpacity(object,opacityPct) {
  if(document.all != null) {
	  // IE.
	  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
  } else if (object.style.MozOpacit != null ){
	  // Old mozilla and firefox
	  object.style.MozOpacity = opacityPct/100;
  }else {
	  //Everything else.
	  object.style.opacity = opacityPct/100;
  }
}

function ChangeOpacity(id, msDuration, msStart, fromO, toO) {
  var element = document.getElementById(id);
  var opacity = element.style.opacity * 100;
  var msNow = (new Date()).getTime();
  opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity<0) {
    SetOpacity(element,0);
    FadeInImage('SLIDE_picBoxID', SLIDE_load[SLIDE_actual].src, false);
  }
  else if (opacity>100) {
    SetOpacity(element,100);
  }
  else {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
  }
}
function FadeIn(id) {
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",0,100)",1);
}
function FadeOut(id) {
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",100,0)",1);
}

function FadeOutImage(foregroundID) {
	  var foreground=document.getElementById(foregroundID);
	  if (foreground.timer) window.clearTimeout(foreground.timer); 
	  var startMS = (new Date()).getTime();
	  foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',500," + startMS + ",100,0)",10);
}

function FadeInImage(foregroundID,newImage,backgroundID) {
  var foreground=document.getElementById(foregroundID);
  if (backgroundID)   {
    var background=document.getElementById(backgroundID);
    if (background)     {
      background.style.backgroundImage = 'url(' + foreground.src + ')';
      background.style.backgroundRepeat = 'no-repeat';
    }
  }
  
  SetOpacity(foreground,0);
  if(document.getElementById("img-viewer") != null) {
	document.getElementById("img-viewer").align = "left";
	document.getElementById("img-viewer").vAlign = "top";
  }

  foreground.src = newImage;
  loadPicDesc();
  if (foreground.timer) window.clearTimeout(foreground.timer); 
  var startMS = (new Date()).getTime();
  foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1500," + startMS + ",0,100)",10);
}

function loadPicDesc() {
	if(document.getElementById("SLIDE_picTitle") != null) {
		document.getElementById("SLIDE_picTitle").innerHTML= SLIDE_picTitle[SLIDE_actual];
	}
	if( document.getElementById("SLIDE_picDesc")  != null) {
		document.getElementById("SLIDE_picDesc").innerHTML= SLIDE_picDesc[SLIDE_actual];
	}
	if( document.getElementById("SLIDE_picCode")  != null) {
		document.getElementById("SLIDE_picCode").innerHTML= SLIDE_picCode[SLIDE_actual];
	}
}
