var btn_up
var btn_down
var scrollTarget
var total_steps = 0
var curr_step = 0
var moving = false;

var is_home
var page_name
var window_height = 186

var isIE = false
var firstRun = true

if(!is_home){
	if (document.getElementById && document.getElementsByTagName) {
		if (window.addEventListener) window.addEventListener('load', init, false);
		else if (window.attachEvent) window.attachEvent('onload', init);
	}
}


function init(){
	// take unto you that which is necessary
	btn_up = document.getElementById("scroll-up")
	btn_down = document.getElementById("scroll-down")
	//bg_btn_up = document.getElementById("btn_up_bg")
	//bg_btn_down = document.getElementById("btn_down_bg")
	
	scrollTarget = document.getElementById("scroll-inner")
	
	// enable the butties
	btn_up.onclick = moveUp
	btn_down.onclick = moveDown
	
	// enable the butties
	//btn_up.className = "btn_on"
	//btn_down.className = "btn_on"
	
	// get the browser flavour
	do_sniff()
	
	// find out how many videos in nav
	prod_count = document.getElementsByName("promo").length;
	
	// set the number of steps possible
	total_steps = prod_count-2
	
	// move the navbar to show the current story
	//do_story_hilite()
	restore_menu()
	
}

function highlightProduct(obj) {
   obj.className="related-product-over";
}
function unHighlightProduct(obj) {
	obj.className="related-product";
}

function restore_menu(){
	
	// check if we know what the last story was
	last_pos = readCookie("last_menu")
	
	if(last_pos){
		// set the current position to what it was last time
		the_step = last_pos	
	} else {
		
		// set it to the first one
		the_step = 0
	}
	
	// set the global
	curr_step = the_step
	
	moveDown()
	firstRun = false

}

function blank(){
	
}

function do_story_hilite(){
	// cut up the url
	the_url = document.location.toString()
	the_url = the_url.split("/")
	
	// get the name
	the_name = the_url[the_url.length - 2]
	page_name = the_name
	
	elem = document.getElementById(the_name)
	
	// get the A element
	t = elem.getElementsByTagName("A")[0]
	t.onmouseover = blank
	t.onmouseout = blank
	t.removeAttribute("href")
	t.className = "selected"
	
	// switch the image
	img = elem.getElementsByTagName("IMG")[0]
	img.src = "/share/stories/_thumbs/" + img.name + "_d.jpg"
}

function do_roll(arg_obj,arg_state){
	
	// suffix
	suffix = ".jpg"
	
	// get the image object
	the_img = arg_obj.firstChild
	
	// get the src
	the_src = the_img.src
	
	// get the_name
	the_name = the_img.name
	
	//pre = "/vanity/bestform/html/images/related-products/" + the_name
	
	// change the src
		//if(arg_state){
			//next_src = pre + "_o" + suffix
		//} else {
		//	next_src = pre + suffix
		//}	
		
		// swap the image
	the_img.src = next_src
}


function do_sniff(){
	
	if (navigator.userAgent.indexOf("MSIE") >= 0)
	{
		isIE = true
	} else {
		scrollTarget.style.position = "relative"
	}
}

function set_button_status(){
	
	// remember where we are
	createCookie("last_menu",(curr_step-1))
	
	/*
	if(curr_step==1){
		btn_up.className = "btn_off"
		bg_btn_up.style.display = "block"
		if(curr_step==total_steps) {
			btn_down.className = "btn_off"
			bg_btn_down.style.display = "block"	
		} else {
			btn_down.className = "btn_on"
			bg_btn_down.style.display = "none"	
		}
	} else if(curr_step==total_steps){
		btn_down.className = "btn_off"	
		bg_btn_down.style.display = "block"
		//if(curr_step==2) {
			btn_up.className = "btn_on"
			bg_btn_up.style.display = "none"
		//}
	} else {
		btn_up.className = "btn_on"
		bg_btn_up.style.display = "none"
		btn_down.className = "btn_on"
		bg_btn_down.style.display = "none"	
	}
	*/
	
}
		
function moveDown() {
		if (moving) return;
		if (!scrollTarget.currentPos) scrollTarget.currentPos = [0,0]; //if no mem is set, set it first;
		
		// work out the next x
		xNow = scrollTarget.currentPos[1]
		
		// find my limits
		xLimit = -scrollTarget.scrollHeight 			

		xTest = -curr_step*window_height 

		if(curr_step!=total_steps) {
			
			// set the new value
			xNext = xTest
	
			// move to the next position
			if(firstRun){
				//alert(xNext)
				// move to it quickly
				moving = true;
				doPosChangeMem(scrollTarget,scrollTarget.currentPos,[0,xNext],1,1,0.5);
			} else {
				// take your time
				//alert(xNext)
				//xNext -=4
				moving = true;
				doPosChangeMem(scrollTarget,scrollTarget.currentPos,[0,xNext],20,20,0.5);
			}
			
			// increment position counter
			if(curr_step!=total_steps){
				curr_step++	
			}
			set_button_status()
		}	
	}

function show_scroller(){
	scrollTarget.style.display = "block"	
}

function moveUp() {
	if (moving) return;
	if (!scrollTarget.currentPos) scrollTarget.currentPos = [0,0]; //if no mem is set, set it first;

	// work out the next x
	xNow = scrollTarget.currentPos[1]
	if(xNow!=0){
		
		xTest = -(curr_step-2)*window_height

		if(xTest<=0){
				//alert(xNext)
	
			//xTest +=3
			// move to the next position
			moving = true;
			doPosChangeMem(scrollTarget,scrollTarget.currentPos,[0,xTest],20,20,0.5);
			
			// make sure we know where we are
			curr_step--
			
		}
		set_button_status()
	} 
}

//*******************

function doPosChangeMem(elem,startPos,endPos,steps,intervals,powr) {
//Position changer with Memory by www.hesido.com
	if (elem.posChangeMemInt) window.clearInterval(elem.posChangeMemInt);
	var actStep = 0;
	elem.posChangeMemInt = window.setInterval(
		function() {
			elem.currentPos = [
				easeInOut(startPos[0],endPos[0],steps,actStep,powr),
				easeInOut(startPos[1],endPos[1],steps,actStep,powr)
				];
			elem.style.left = elem.currentPos[0]+"px";
			
			//
			if(isIE){
				elem.style.marginTop = elem.currentPos[1]+"px";
			} else {
				elem.style.top = elem.currentPos[1]+"px";
				
			}
			actStep++;
			if (actStep > steps) {
				window.clearInterval(elem.posChangeMemInt);
				show_scroller()
				checkBoundaries();
				moving = false;
			}
		}
		,intervals)

}

var loopMap = {1:4, 5:2};

function checkBoundaries(){
	if (loopMap[curr_step]){
		curr_step = loopMap[curr_step];
		doPosChangeMem(scrollTarget,scrollTarget.currentPos,[0,(curr_step-1)*(-window_height)],1,1,0.5);
	}
}

//******************* 

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
//Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

/*
	Cookie Monsta
*/

function createCookie(name,value,days)
{
	var expires;
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

/*
	Popup Windows
*/

var _POPUP_FEATURES = '';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
	var href = src.getAttribute('href') + page_name
    //alert(src.getAttribute('target'))
	return raw_popup(href, src.getAttribute('target') || '_blank', features);
}