// get the flash movie object
var flashMovie;
var flvDirectory = '/flv/crest-products/';
var imgDirectory = '/images/crest-products/';
var currentVideo = '';
var currentVideoImage= '';
var totalVideos = 4;

//video thumbnail images
vidThumb0 = new Image();
vidThumb0.src = imgDirectory + "vidThumb0.png";
vidThumb0Playing = new Image();
vidThumb0Playing.src = imgDirectory + "vidThumb0.png";

vidThumb1 = new Image();
vidThumb1.src = imgDirectory + "vidThumb1.png";
vidThumb1Playing = new Image();
vidThumb1Playing.src = imgDirectory + "vidThumb1_nowplaying.png";

vidThumb2 = new Image();
vidThumb2.src = imgDirectory + "vidThumb2.png";
vidThumb2Playing = new Image();
vidThumb2Playing.src = imgDirectory + "vidThumb2_nowplaying.png";

vidThumb3 = new Image();
vidThumb3.src = imgDirectory + "vidThumb3.png";
vidThumb3Playing = new Image();
vidThumb3Playing.src = imgDirectory + "vidThumb3_nowplaying.png";

vidThumb4 = new Image();
vidThumb4.src = imgDirectory + "vidThumb4.png";
vidThumb4Playing = new Image();
vidThumb4Playing.src = imgDirectory + "vidThumb4_nowplaying.png";

vidThumb5 = new Image();
vidThumb5.src = imgDirectory + "vidThumb5.png";
vidThumb5Playing = new Image();
vidThumb5Playing.src = imgDirectory + "vidThumb5_nowplaying.png";

// wait for the page to fully load, only then initialize
//window.onload = init;
		
function getVideoURL (myObject)
{
	for(var i=0; i<totalVideos;i++){
		if(myObject.id == ("vid" + i)) {
			// pass video param 
			if (flashMovie) {
				flashMovie.videoURL(flvDirectory + "vid" + i + ".flv");
				currentVideo = myObject.id;
				document.getElementById(myObject.id).style.backgroundImage = "url(" + imgDirectory + "vidThumb" + i + "_nowplaying.png)";
				document.getElementById("vidCount").innerHTML = "Select a video (" + (i + 1) + " of " + totalVideos + ")";
			}
		}
		else
		{
		    document.getElementById("vid" + i).style.backgroundImage	= "url(" + imgDirectory + "vidThumb" + i + ".png)";
		}
	}
}

function getVideoURLScroll(scrollDirection)
{
    var myCurrentVideo = currentVideo.substring(3, currentVideo.length);
    
    if (scrollDirection == "up")
    {
        if (myCurrentVideo > 0)
        {
            currentVideo = "vid" + (parseInt(myCurrentVideo) - 1);
            getVideoURL(document.getElementById(currentVideo));
	    }
    }
    else if (scrollDirection == "down")
    {
        if (myCurrentVideo < (totalVideos - 1))
        {
            currentVideo = "vid" + (parseInt(myCurrentVideo) + 1);
            getVideoURL(document.getElementById(currentVideo));
	    }
    }
        
    
}

// call back from flash
function getFromFlash(name) {
	document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + "<br />" + name;
}

function initVideo() {
    if (document.getElementById) {
        flashMovie = document.getElementById("scopeVideoPlayer");
        var getFirstMovie = setTimeout(runFirstMovie, 1000);

    }
}

function runFirstMovie()
{
    if (flashMovie) 
    {
        var firstVideo = getQueryStringVar("vid");
        var vidThumb = "vidThumb0";
        
        if(firstVideo != null)
        {
            currentVideo = "vid" + firstVideo;
            vidThumb = "vidThumb" + firstVideo;
        }
        else
        {
            currentVideo = "vid0";
        }
            
		currentVideoImage = document.getElementById(currentVideo).style.backgroundImage;
		flashMovie.videoURL(flvDirectory + currentVideo + ".flv");
		document.getElementById(currentVideo).style.backgroundImage = "url(" + imgDirectory + vidThumb + "_nowplaying.png)";
    }
}

function getQueryStringVar(variable) { 
  var qString = window.location.search.substring(1); 
  var qVars = qString.split("&"); 
  for (var i=0;i<qVars.length;i++) 
  { 
    var pair = qVars[i].split("="); 
    if (pair[0] == variable) 
        return pair[1];
  }
} 


function TextScroll(scrollname, div_name, up_name, down_name)
{
    this.div_name = div_name;
    this.name = scrollname;
    this.scrollCursor = 0;
    this.speed = 117;
    this.timeoutID = 0;
    this.div_obj = null;
    this.up_name = up_name;
    this.dn_name = down_name;
    //document.getElementById("scroll_up").style.visibility = "hidden";
    {
        if (document.getElementById) {
            div_obj = document.getElementById(this.div_name);
            if (div_obj) {
                this.div_obj = div_obj;
                this.div_obj.style.overflow = 'hidden';
            }
            div_up_obj = document.getElementById(this.up_name);
            div_dn_obj = document.getElementById(this.dn_name);
            if (div_up_obj && div_dn_obj) {
                div_up_obj.onclick = function() { eval(scrollname + ".scrollUp();");};
				//div_up_obj.onmouseout = function() { eval(scrollname + ".stopScroll();");};
				div_dn_obj.onclick = function() { eval(scrollname + ".scrollDown();");};
				//div_dn_obj.onmouseout = function() { eval(scrollname + ".stopScroll();");}; 
            }
        }
    }


    this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

    this.scrollUp = function() {
        if (this.div_obj) {
			document.getElementById("scroll_down").style.visibility = "visible";
            this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
			//if(this.scrollCursor == 0)
				//document.getElementById("scroll_up").style.visibility = "hidden";
            this.div_obj.scrollLeft = this.scrollCursor;
            //this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
            getVideoURLScroll("up");
        }
    }

    this.scrollDown = function() {
	    if (this.div_obj) {
			document.getElementById("scroll_up").style.visibility = "visible";
		    this.scrollCursor += this.speed;
			if (this.scrollCursor <= document.getElementsByTagName("div")["scroll_box"].offsetWidth)
		    	this.div_obj.scrollLeft = this.scrollCursor;
		    if (this.div_obj.scrollLeft == this.scrollCursor) {
			    //this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
		    } else {
				//document.getElementById("scroll_down").style.visibility = "hidden";	
			    this.scrollCursor = this.div_obj.scrollLeft;
		    }
		  getVideoURLScroll("down");
	    }
    }


    this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollLeft = 0;
            this.scrollCursor = 0;
        }
    }
}
