var left_side_width		= 50;
var right_side_width	= 80;
var top_offset	 		= 75;
var left_arrow_offset	= 20;
var page_width 			= 950;
var min_height 			= 650;
var side_open 			= "right";
var gutter;
var leftEverOpened		= false;

//
//	Controls the left <--> right slider navigation throughout the site
//	
function toggleOpenSide(side_to_open) {
	gutter = $("left").getStyle('border-right-width').split("px");
	gutter = parseInt(gutter[0]);
	
	if(side_open != side_to_open) {
		
		if(side_to_open == "left"){
			left_side_content = "block";
			left_side_content_opacity = 1;
			right_side_content = "none";
			right_side_content_opacity = 0;
			
		} else {
			left_side_content = "none";
			left_side_content_opacity = 0;
			right_side_content = "block";
			right_side_content_opacity = 1;
		}
		
		var left_side = new Fx.Style('left', 'width',{
			duration:100,
			onComplete: function() {
				$('left_content').setStyle('display', left_side_content);
				$('left_content').setStyles({
					display:left_side_content,
					opacity: 0
				});
				new Fx.Style('left_content', 'opacity', {duration: 200} ).start(left_side_content_opacity);
				
				// On the blog, the idea scroller must be initialized but only after it's made visible
				if (curPage = "blog" && !leftEverOpened) {
					init_horizontal_scroller("idea_set_wrapper", "idea_set_content", "left_content_arrow", "right_content_arrow");
					leftEverOpened = true;
				}
			}
		});
		
		var left_side_arrow = new Fx.Style('left_arrow', 'left',{duration:100});
		var right_side = new Fx.Style('right', 'margin-left',{
			duration:100,
			onStart: function() {
				if (side_to_open == 'right') {
					new Fx.Style('right', 'left', {duration:0}).start('-8px');
				}
				else {
					new Fx.Style('right', 'left', {duration:0}).start('-3px');
				}
			},
			onComplete: function () {
				$('right_content').setStyle('display', right_side_content);
				$('right_content').setStyles({
					display:right_side_content,
					opacity: 0
				});
				new Fx.Style('right_content', 'opacity', {duration: 200} ).start(right_side_content_opacity);
				
				//Find tallest element and resize
				resize_side();
			}
		});
		
		if(side_to_open == "left"){
			left_side.start(page_width - right_side_width);
			left_side_arrow.start(page_width - right_side_width - left_arrow_offset - 8);
			right_side.start(page_width - right_side_width);
		} else {
			left_side.start(left_side_width);
			left_side_arrow.start(left_arrow_offset);
			right_side.start(left_side_width + gutter);
		}
		
		$('left_content').setStyle('display', 'none');
		$('right_content').setStyle('display', 'none');
		
		//Set the open side
		side_open = side_to_open;
	}
}

//Resize backer based on tallest content
function resize_side() {
	var tallest = min_height + top_offset;

	$$("#" + side_open + " .tall").each(
		function(el) {			
			if(el.getPosition().y + el.getSize().size.y > tallest) {
				tallest = el.getPosition().y + el.getSize().size.y + 25;
			}
		}
	);
	
	$('left').setStyle('height', tallest - top_offset);
	$('right').setStyle('height', tallest - top_offset);
}

//
//	Global onLoad function registry
//	
var window_onload = new Array;
var onloadRef = window.onload;

window.onload = function() {
	if (onloadRef != null) {
		onloadRef();
	}
	for (var i = 0; i < window_onload.length; i++) {
		eval(window_onload[i]);
	}
};

function onload_register(func) {
	window_onload.push(func);
}

//
//  Flash UFO handler
//
function showFlash(argSwf,argElement,argFlashVars){
	flash_version = 8;
	flashName = argElement +  '_flash';
	
	var site_config = {
	    	'host':location.host,
		    'src':location.href,
		    'title':document.title,
	    	'EOF':'EOF'
		};
	
	var flash_vars = '';
	
	for(k in site_config) {
		if (k != "toJSONString") {
	   		flash_vars += "&" + k + "=" + encodeURIComponent(site_config[k]);
		}
	}
	
	if(argFlashVars != null) {
		flash_vars += argFlashVars;
	}
	
	
	var FO = {
	    movie:        argSwf,
	    width:        "100%",
	    height:       "100%",
	    name:	  	   flashName,
	    id:		  	flashName,
	    allowscriptaccess:"always",
	    bgcolor:      "#FFFFFF",
	    scale:        "noscale",
	    wmode:        "transparent",
	    play:         "true",
	    quality:      "high",
	    menu:         "false",
	    majorversion: flash_version,
	    build:        "0",
		xi:           "false",
		flashvars:    flash_vars
	};
	UFO.create(FO, argElement);
}

//
// Horizontal content scroll controllers
//
var num_elements_to_scroll;
var scroll_element_position;
var width_of_elements_to_scroll;
var width_of_scroll_container;
var max_scroll_position;
var scroll_container;
var scroll_content;
var left_control;
var right_control;

//
function init_horizontal_scroller(container, content, left, right) {
	scroll_container = container;
	scroll_content = content;
	left_control = left;
	right_control = right;
	
	num_elements_to_scroll = $$("#"+scroll_content+" li").length;
	scroll_element_position = 0;
	width_of_elements_to_scroll = $(scroll_content).getFirst().getSize().size['x'];
	width_of_scroll_container = $(scroll_container).getSize().size.x;
	max_scroll_position = num_elements_to_scroll - Math.round(width_of_scroll_container / width_of_elements_to_scroll);
	
	$(scroll_content).setStyle("width", num_elements_to_scroll * width_of_elements_to_scroll);
	
	toggle_arrows();
}

function next_hscroll(num_spaces) {
	if(!num_spaces){ num_spaces = 1;}
	if(scroll_element_position < max_scroll_position) {
		scroll_element_position = scroll_element_position + num_spaces;
		
		if(scroll_element_position > max_scroll_position) {
			scroll_element_position = max_scroll_position;
		}
		toggle_arrows();
		
		new Fx.Style(scroll_content, 'left', {duration: 200} ).start(0 - (scroll_element_position * width_of_elements_to_scroll));
	}
}

function prev_hscroll(num_spaces) {
	if(!num_spaces){ num_spaces = 1;}
	
	if(scroll_element_position > 0) {
		scroll_element_position = scroll_element_position - num_spaces;
		
		if(scroll_element_position < 0) {
			scroll_element_position = 0;
		}
		toggle_arrows();
		
		new Fx.Style(scroll_content, 'left', {duration: 200} ).start(0 - (scroll_element_position * width_of_elements_to_scroll));
	}
}

function toggle_arrows() {
	if(scroll_element_position > 0) {
		$(left_control).setStyle("display", "block");
	} else {
		$(left_control).setStyle("display", "none");
	}
	
	if(scroll_element_position < max_scroll_position) {
		$(right_control).setStyle("display", "block");
	} else {
		$(right_control).setStyle("display", "none");
	}
}


function signUp() {
	$('header_signup').setStyle('display','block');
	//window.document["submit_idea_flash"].SetVariable("_root.registered_user", "true");
}

function setValidFlashUser() {
	window.document['submit_idea_movie_flash'].SetVariable('_root.registered_user', 'true');
}


function pageTrack(flashPN, flashMLC) {
	var flashPN = nameCleaner(flashPN);
	var flashMLC = nameCleaner(flashMLC);
	var trackPN = hbx.pndef; // ignore the passed in Page Name
	var trackMLC = hbx.mlc.split(";");
	trackMLC = trackMLC.join(flashMLC + ";");
	trackMLC = trackMLC + flashMLC;
	trackMLC = trackMLC.replace(/\/\//g,'\/'); //stripping double slashes
	_hbPageView(trackPN, trackMLC);
	if(getParameter("showTracking")) {
		alert(	"flashPageTrack\n" + 
			"--pn: " + trackPN + "\n" +
			"--mlc: " + trackMLC + "\n"
		);
	}
}

function linkTrack(flashLPOS, flashName) {
	var flashName = nameCleaner(flashName);
	var flashLPOS = nameCleaner(flashLPOS);
	_hbLink(flashName, flashLPOS);
	if(getParameter("showTracking")) {
		alert(	"flashLinkTrack\n" + 
			"--lpos: " + flashLPOS + "\n" +
			"--name: " + trackName + "\n"
		);
	}
	
}

function step1() {
	new Ajax('/api.php?func=dart_start', {evalScripts: true, update: $("dart_tag_start")}).request();	
}

function step2() {
	
}

function step3() {
	new Ajax('/api.php?func=dart_finish', {evalScripts: true, update: $("dart_tag_finish")}).request();	
}


function getParameter(aP){
	var qS = new String(location.search.substring(1,location.search.length));
	var p = qS.split("&");
	var val = "";
	if(aP){
		for(i=0;i<p.length;i++){
			if(p[i].split( "=" )[0] == aP){
				val = p[i].split( "=" )[1];
			}
		}
		return val;
	}
}

