

var fadeSpeed = 0.5; // Amount of time to fade in and out


function embedVideoPlayer(flvFile, targetDiv, go) {

	// if no existing movie playing then create container inside parent
	
	var flashvars = {};
	flashvars.curvyCorners = "0";
	flashvars.analyticsTag = "GreenPromise";
	flashvars.autostart = "false";
	if (go) { flashvars.autostart = "true"; }
	flashvars.width = "653";
	flashvars.height = "368";
	flashvars.file = "http://media.benjaminmoore.com/WebServices/prod/YouTube/"+flvFile;
	
	var params = {};
	params.allowfullscreen = "true";
	params.bgcolor = "#999999";
	
	var attributes = {};
	
	//embed the SWF
	swfobject.embedSWF("http://www.benjaminmoore.com/swf/mediaplayer-auraReskin.swf", targetDiv, "653", "368", "9", "expressInstall.swf", flashvars, params, attributes);

}

function createVideoPlayer(flvFile, targetDiv, go) {

	// if no existing movie playing then create container inside parent
	
	var flashvars = {};
	flashvars.curvyCorners = "0";
	flashvars.analyticsTag = "GreenPromise";
	flashvars.autostart = "false";
	if (go) { flashvars.autostart = "true"; }
	flashvars.width = "653";
	flashvars.height = "368";
	flashvars.file = "http://media.benjaminmoore.com/WebServices/prod/YouTube/"+flvFile;
	
	var params = {};
	params.allowfullscreen = "true";
	params.bgcolor = "#999999";
	
	var attributes = {};
	
	//embed the SWF
	swfobject.createSWF("http://www.benjaminmoore.com/swf/mediaplayer-auraReskin.swf", targetDiv, "653", "368", "9", "expressInstall.swf", flashvars, params, attributes);

}

// switch the main sections through left nav
function SwitchSection(link)
{

	// Check if current section is being called; do nothing if it is
	if (currentSection == "sect-"+link) {
		return;
	}


	lastSection = currentSection; // Set currently displayed section as last
	currentSection = "sect-"+link; // Set new value of currentSection


	// Hide displayed section.
	Effect.Fade(lastSection, { duration: fadeSpeed} );
	
	// Display new section
	Effect.Appear(currentSection, { duration: fadeSpeed} );

	// Change the leftnav highlight.
	if (lastSection != "sect-welcome") {
		document.getElementById('link-'+lastSection).className = "notCurrent";
	}

	if (currentSection != "sect-welcome") {
		document.getElementById('link-'+currentSection).className = "currentSection";
	}

	// UNLOAD ANY PLAYING MOVIES IN LAST SECTION
	var vl = document.getElementById("flshDivContent");
	if (vl) { swfobject.removeSWF("flshDivContent"); }


	// IF ANY ASSOCIATED VIDEOS IN THIS SECTION LOAD THEM HERE
	var vc = document.getElementById("video-"+link);
	if (vc) { 
		var d = document.createElement("div");
		d.setAttribute("id", "flshDivContent");
		document.getElementById("video-"+link).appendChild(d); 
	}

	// LOAD MOVIE OBJECT INTO CURRENT SECTION
	var h = gpVideos.get(link); 
	embedVideoPlayer( h ,"flshDivContent",true);

}

// switch the main sections through left nav
function SwitchPaintsVideo(video)
{

	// Check if current video is being called; do nothing if it is
	if (currentPaintsVideo == video) {
		return;
	}
	
	lastPaintsVideo = currentPaintsVideo; // 
	currentPaintsVideo = video; // 

	// Change the video
	var p = gpVideos.get(video); 
	embedVideoPlayer( p ,"flshDivContent",true);

	
	// Change the tab colors.
	document.getElementById(lastPaintsVideo+tabTag).className = "inactive";
	document.getElementById(currentPaintsVideo+tabTag).className = "active";
}


// Scroll the page manually to the position of element "link", passed to us.

function SlideStandardsInfo(std, scrollArea, offset)
{

	// Store the last section, and update the current section

	if (currentGrnStd == std) {
		return;
	}
	lastGrnStd = currentGrnStd;
	currentGrnStd = std;
	
	// Change the section highlight.
	// Extract the root section name, and use that to change the background image to 'top', revealing the alt. state

    stdTab = currentGrnStd.split("-")[0] + tabTag;
    document.getElementById(stdTab).className = "active";
    if (lastGrnStd) {
	    lastTab = lastGrnStd.split("-")[0] + tabTag;
	    document.getElementById(lastTab).className = "inactive";
	}
    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(std+paneTag));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}

var tabTag = "-tab";
var paneTag = "-pane";

// Scroll the page manually to the position of element "prod", passed to us.

function SlideProduct(prod, scrollArea, offset)
{

	// Store the last section, and update the current section

	if (currentProduct == prod) {
		return;
	}
	lastProduct = currentProduct;
	currentProduct = prod;
	
	// Change the section highlight.
	// Extract the root section name, and use that to change the state

    sectionTab = currentProduct.split("-")[0] + tabTag;
    document.getElementById(sectionTab).className = "active";
    if (lastProduct) {
	    lastTab = lastProduct.split("-")[0] + tabTag;
	    document.getElementById(lastTab).className = "inactive";
	}
    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(prod));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}

//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollVertAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}

function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}




function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}

