//below is script to handle the video sliders
//variables assigned in "window.onload" function indicated by a "*"
var vidNumber;				//* total number of videos in both #slideDivs
var max_value;				//* farthest a #slideDiv will scroll before reset = number of videos * scroll_static
var scroll_static = 170;	//amount a #slideDiv will slide per scroll = sliderContent div width + margin right
var m=0;					//style.left value for #slideDiv1 
var n;						//* style.left value for #slideDiv2
var scroll = scroll_static;	//amount remaining to scroll, decrements during execution
var shift = 5;				//how far things shift - more or less your scroll speed
var direction;				//left or right
var scrolling=false;		//are the images scrolling?

function scrollPics() 
{   
	scrolling=true;
	if(direction == "right")
    {
    	if(m==-max_value) 
        {
        	m=max_value;
        }
        if(n==-max_value) 
        {
        	n=max_value;
        }
        m-=shift;
        n-=shift;
    }
    else if (direction == "left")
    {
    	if(m==max_value) 
        {
        	m=-max_value;
        }
        if(n==max_value) 
        {
        	n=-max_value;
        }
        m+=shift;
        n+=shift;
    }
	
	document.getElementById('slideDiv1').style.left=m+'px';
    document.getElementById('slideDiv2').style.left=n+'px';
    scroll-=shift;
    
	if (scroll > 0)
	{
		setTimeout("scrollPics()", 0);
	}
	else
	{
		scroll=scroll_static;
		scrolling=false;
	}
}

function scroll_Left()
{
	if (scrolling==false)
	{
		direction = "left";
		scrollPics();
	}			
}
function scroll_Right()
{
	if (scrolling==false)
	{
		direction = "right";
		scrollPics();
	}	
} //end slider handling


//when the page loads, get values, assign values, load video
window.onload=function() 
{
    //get number of videos in #slideDivs
    //count divs with IDs "sliderContent#" until it doesn't find the next one
	vidNumber = 1;
    while (document.getElementById("sliderContent" + vidNumber))
    {
    	vidNumber++;
    }
    //subtract 1 for total videos. 
    //divide by 2 to get videos per slideDiv.
    //multiply by 170 to get width of a slideDiv.
    max_value = --vidNumber/2 * 170;
    n=max_value;
    if (document.getElementById('slideDiv1'))
    {
    	document.getElementById('slideDiv1').style.left=m+'px';
	    document.getElementById('slideDiv1').style.width=max_value+'px';
	    document.getElementById('slideDiv2').style.left=n+'px';
	    document.getElementById('slideDiv2').style.width=max_value+'px';
    }
    
	if(document.getElementById("videoPlayer"))
    	//loadVideo("1");
	loadStream();
	//if(document.getElementById("wbMediaspace"))
	//setTimeout("loadStream()", 4000);
};

//loadVideo function takes a number passed from a slider on a page.
//This number is used to look for video, title, description, and 
//to tell where the slider should stop.

//'G_sliderContentID' is to make the afore-mentioned number global
//NOTE: anytime you see 1*G_sliderContentID; it's just to cast it as an int
//      so the '+' counts as addition and not string concatenation 
var G_sliderContentID = 1;

function loadVideo(sliderContentID) {

	//hide old "Now Playing" text
	document.getElementById("nowPlayingArrow"+G_sliderContentID).style.visibility='hidden';
	if (document.getElementById("nowPlayingArrow"+(1*G_sliderContentID + vidNumber/2)))
		document.getElementById("nowPlayingArrow"+(1*G_sliderContentID + vidNumber/2)).style.visibility='hidden';
	else
		document.getElementById("nowPlayingArrow"+(1*G_sliderContentID - vidNumber/2)).style.visibility='hidden';
		
	//update and globalize value
	G_sliderContentID = sliderContentID;

	//show new "Now Playing" text
	document.getElementById("nowPlayingArrow"+G_sliderContentID).style.visibility='visible';
	if (document.getElementById("nowPlayingArrow"+(1*G_sliderContentID + vidNumber/2)))
		document.getElementById("nowPlayingArrow"+(1*G_sliderContentID + vidNumber/2)).style.visibility='visible';
	else
		document.getElementById("nowPlayingArrow"+(1*G_sliderContentID - vidNumber/2)).style.visibility='visible';
		
	//if longTitle is empty, use shortTitle
	if (document.getElementById("sliderLongTitle" + G_sliderContentID).innerHTML.length > 0)
	{
		document.getElementById("playerTitle").innerHTML = document.getElementById("sliderLongTitle" + G_sliderContentID).innerHTML;
	}
	else
	{
		document.getElementById("playerTitle").innerHTML = document.getElementById("sliderTitle" + G_sliderContentID).innerHTML;;
	}
		
	//if longDescription is empty, use shortDescription
	if (document.getElementById("sliderLongDescription" + G_sliderContentID).innerHTML.length > 0)
	{
		document.getElementById("playerSubText").innerHTML = document.getElementById("sliderLongDescription" + G_sliderContentID).innerHTML;
	}
	else
	{
		document.getElementById("playerSubText").innerHTML = document.getElementById("sliderDescription" + G_sliderContentID).innerHTML;
	}
	
	//prepare player
	var so = new SWFObject('/wp-content/themes/r3global/flashplayer/player.swf', 'mpl', '500', '335', '9');
		so.addParam('allowfullscreen', 'true');
		so.addParam('allowscriptaccess', 'always');
		so.addParam('wmode', 'opaque');
		so.addVariable('streamer', 'rtmp://media.r3globalproductions.com/vod/');
		so.addVariable('bufferlength', '10'); 
		so.addVariable('autostart', 'true');
		so.addVariable('skin', 'http://www.longtailvideo.com/files/skins/modieus/4/modieus.swf');
		so.addVariable('plugins', 'fbit-1,tweetit-1,gapro-1');
		so.addVariable('gapro.accountid', 'UA-10779916-1');
	
	//get and assign video name
	var videoName = "sliderVideo" + G_sliderContentID;
	videoName = document.getElementById(videoName).innerHTML;	
	so.addVariable('file', videoName);
	
	//scroll page to player and load it
	$(this).scrollTop($('#vidScrollHere').position().top);	
	so.write("videoPlayer");
	
	//move slider so currently playing video lines up on left
	setTimeout("autoSlide()", 1000);

}//end loadVideo


//function to slide videos when clicked or auto playing
function autoSlide()
{
	/*
		Here, we're going to get a number we want to assign as the
		left-attribute value to one of the slideDivs.
	*/
	
	/*
	 	For left-attribute value of slideDiv1 (variable 'm'):
		Get current video, subtract 1 for math.
		Multiply by 'scroll_static' (div width).
		Multiply by -1 (because we're scrolling right).
		e.g. -  if 'G_sliderContentID' == 1, our desired left-attribute 
				value of slideDiv1 is 0; if 'G_sliderContentID' == 2, 
				our desired left-attribute value of slideDiv1 is -170; etc.
	*/	
	var scrollToHere = (G_sliderContentID - 1) * scroll_static * -1;
	
	/*
		If 'G_sliderContentID' is on one of the videos in the second slideDiv,
		the math above results in the condition (scrollToHere <= -max_value). 
		We use "vidNumber/2 + 1" rather than "1" for math. This lets us 
		find the desired left-attribute value for slideDiv2 (variable 'n').
	*/
	if (scrollToHere <= -max_value) 
		scrollToHere = (G_sliderContentID - (vidNumber/2 + 1)) * scroll_static * -1;;
	
	//scroll until one of the left-attribute variables == desired left-attribute
	//either 'm' or 'n' will == scrollToHere
	if (m != scrollToHere && n != scrollToHere)
    {
		scrolling=true;
		if(m==-max_value) 
		{
			m=max_value;
		}
		if(n==-max_value) 
		{
			n=max_value;
	    }
		m-=shift;
		n-=shift;
		document.getElementById('slideDiv1').style.left=m+'px';
		document.getElementById('slideDiv2').style.left=n+'px';
		
		setTimeout("autoSlide()", 10);
    }
    	
	scrolling=false;
	
}//end autoSlide

//the 3 functions below will handle automated video playing
function playerReady(thePlayer) {
    player = document.getElementById(thePlayer.id);
    addListeners();
}
function addListeners() {
    if (player) {
        player.addModelListener("STATE", "stateListener");
    } else {
        setTimeout("addListeners()", 100);
    }
}
function stateListener(obj) 
{ 
	//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
    currentState = obj.newstate;
    previousState = obj.oldstate;

    var tmp = document.getElementById("stat");
    if (tmp) {
        tmp.innerHTML = "current state: " + currentState +
	"<br />previous state: " + previousState;
    }
    
    //if player is idle load the next slider video or webcast stream/ad
    if(document.getElementById("videoPlayer"))    	
	{
	    if (currentState == "IDLE")
	    {	
	    	//if we were on last video, start over with 1
	    	(1*G_sliderContentID + 1 > vidNumber) ? loadVideo("1") : loadVideo(1*G_sliderContentID + 1);    	
	    }
    }
    else if(document.getElementById("wbMediaspace"))
    {
	    if (currentState == "COMPLETED" && previousState == "IDLE")
	        loadStream();
	    if (currentState == "IDLE" && previousState == "BUFFERING")
	        loadAd();
    }
} // end automated playing handling


//begin Brig: Show The Plan dropDown list handling
function getDropDownValue(dropDown)
{    
	if (dropDown.attr("value") == "")
	{
		var videoPath = "STP-BRIG/stp_hart_04";
		var videoTitle = "Show the Plan: Whole-sale Retail";
	}
	else if (dropDown.attr("value") == "#")
	{
		var videoPath = "STP-BRIG/stp_hart_01";
		var videoTitle = "Show the Plan: Welcome";
	}
	else
	{
		var videoPath = dropDown.attr("value");
		var videoTitle = "Show the Plan: " + dropDown.text();
	}	
	var videoDescription = "Follow the step-by-step lessons 'Show the Plan' offers.";
    loadPlayer(videoPath, videoTitle, videoDescription);
    $(this).scrollTop($('#vidScrollHere').position().top);

}


function loadPlayer(videoPath, videoTitle, videoDescription)
{
	var so = new SWFObject('/wp-content/themes/r3global/flashplayer/player.swf', 'mpl', '500', '335', '9');
        so.addParam('allowfullscreen', 'true');
        so.addParam('allowscriptaccess', 'always');
        so.addParam('wmode', 'opaque');
		so.addVariable('bufferlength', '10'); 
		so.addVariable('autostart', 'true');
        so.addVariable('file', videoPath);
        so.addVariable('streamer', 'rtmp://media.r3globalproductions.com/vod/');
        //so.addVariable('skin', 'modieus.zip');
        so.addVariable('skin', 'http://www.longtailvideo.com/files/skins/modieus/4/modieus.swf');
        so.addVariable('plugins', 'fbit-1,tweetit-1,gapro-1');
        so.addVariable('gapro.accountid', 'UA-10779916-1');
        so.write('videoPlayer');
        
        $("#playerTitle").html(videoTitle);
        $("#playerSubText").html(videoDescription);
 }   
//end Brig: Show The Plan dropDown list handling

//below will handle the live webcast page's necessary scripts
// It was moved from the live.webcast.php file and 
// placed in this .js file.

var ads = [
			"VideoGallery/ImagineConference-Fine_1.flv",
			"TPC2011/RVL_Video Plus.flv"
			/*
			"videogallery/video_r3g_tpc_teaser.flv",
			"dreamdays/2010/video_r3g_promo_dreamdays2010_1.flv",
			"FamilyReunion/Preview.flv",
	         "FamilyReunion/SeasonOfTheWhy.flv",
	         "FamilyReunion/Testimonials.flv"
	         "BBS_v9.flv"
			*/
           ];
var idx = 0;
	    
function loadStream()
{
	var location = document.getElementById("wbLocation").innerHTML;
		
	var so = new SWFObject('/wp-content/themes/r3global/flashplayer/player.swf', 'mpl', '500', '335', '9');
		so.addParam('allowfullscreen', 'true');
		so.addParam('allowscriptaccess', 'always');
		so.addParam('wmode', 'opaque');
		so.addVariable('file', 'FL'); // will swap between 'FL' and 'CA'
		so.addVariable('streamer', 'rtmp://r3global.total-stream.net/R3Global');
		//so.addVariable('streamer', 'rtmp://69.1.90.10/R3Global/');
		so.addVariable('skin', 'http://www.longtailvideo.com/files/skins/modieus/4/modieus.swf');
		so.addVariable('plugins', 'fbit-1,tweetit-1,gapro-1');
		so.addVariable('gapro.accountid', 'UA-10779916-5');
		so.addVariable('autostart', 'true');
		so.addVariable('bufferlength', '5');
		so.write('wbMediaspace');
}
	
function loadAd() 
{
	if (idx == ads.length)
		idx = 0;
	    
	var so = new SWFObject('/wp-content/themes/r3global/flashplayer/player.swf', 'mpl', '500', '335', '9');
		so.addParam('allowfullscreen', 'true');
		so.addParam('allowscriptaccess', 'always');
		so.addParam('wmode', 'opaque');
		so.addVariable('file', ads[idx++]);
		so.addVariable('streamer', 'rtmp://media.r3globalproductions.com/vod/');
		so.addVariable('skin', 'http://www.longtailvideo.com/files/skins/modieus/4/modieus.swf');
		//so.addVariable('controlbar', 'none');
		so.addVariable('plugins', 'fbit-1,tweetit-1,gapro-1');
		so.addVariable('gapro.accountid', 'UA-10779916-5');
		so.addVariable('autostart', 'true');
		so.addVariable('bufferlength', '5');
		so.write('wbMediaspace');    
}

