var position = "left"; // "left", or "right"

var currentAnIdeaDeeplink = "";
var currentDotComDeeplink = "";
var lastLinkRequested = "";

function setDeeplink(url)
{
	currentDotComDeeplink = url;
}

function getWidth()
{
	var width = 0;
	
	if(window.innerWidth != null)
	{
		width = window.innerWidth;		
	}
	else if(document.documentElement != null && document.documentElement.clientWidth != null)
	{
		width = document.documentElement.clientWidth;
	}
	else
	{
		width = document.body.clientWidth;		
	}	
	
	return width;
}

function getHeight()
{
	var height = 0;
	
	if(window.innerHeight != null)
	{
		height = window.innerHeight;
		
	}
	else if(document.documentElement != null && document.documentElement.clientHeight != null)
	{
		height = document.documentElement.clientHeight;
	}
	else
	{
		height = document.body.clientHeight;		
	}	
	
	return height;
}

function slide(ignoreDeeplink)
{
	var width = getWidth();
	
	$(".anidea").css("display", "block");

	$(".slider").animate({left: "-" + width + "px"}, 500 );	
	
	if(position != "right")
	{
		position = "right";
		
		// Check to ensure it does not get set twice
			
		var location = "http://" + window.location.host + "/#/anidea/" + currentAnIdeaDeeplink;
		
		if(ignoreDeeplink == null && window.location != location)
		{
			window.location = location;	
		}
	}
	
	// Load ANidea in case iFrame src was empty
	if($("#iframe").attr("src") == "")
	{
		$("#iframe").attr("src", "http://www.anidea.com/?a=1");
	}
	
	return false;
}

function loadRight(url, ignoreRightCheck)
{	
	var target = "http://www.anidea.com/" + url + "?a=1";
	var iframe = document.getElementById("iframe");
	
	// If we're looking at .com, OR if we're looking at ANidea and there's a navigation request (from back/forward, expectedly)
	if(position != "right" || (position == "right" && ignoreRightCheck))
	{		
		if(iframe.src != target && currentAnIdeaDeeplink != url)
		{
			//currentAnIdeaDeeplink = url;
			iframe.src = target;
		}
		else
		{
			// Fire event anyway, so Flash responds
			iframeChanged();
		}
	}
}

function slideBack(ignoreDeeplink)
{
	if(position != "left")
	{
		$(".slider").animate({left: "0px"}, 500, "swing", slideComplete);		
	}

	position = "left";
	
	if(ignoreDeeplink == null)
	{	
		if(currentDotComDeeplink == "/") currentDotComDeeplink = "";
		
		var location = "http://" + window.location.host + "/#/" + currentDotComDeeplink;
		
		if(window.location != location)
		{
			window.location = location;
		}
	}

	return false;
}	

function slideComplete()
{
	$(".anidea").css("display", "none");	

	sendToActionScript("SLIDE_COMPLETE");
}

function handleResize()
{	
	$(function()
	{		   	   
		width = getWidth();
		height = getHeight();
		var maxFlashHeight = 760;
				
		$(".com").width(width);
		$(".anidea").width(width);		
		$("#slider").width(width*2);
		
		$(".com").height(height);
		$(".anidea").height(height);	
		$(".anidea .back").height(height);		
		
		var flashHeight = maxFlashHeight;
		
		if(height < maxFlashHeight)
		{
			flashHeight = height;
			
			$(".anidea .back .back_inside").css("top", "0px");
		}
		else
		{
			$(".anidea .back .back_inside").css("top", (height - maxFlashHeight) / 2 + "px");
		}
			
		$(".anidea .back .back_inside").css("height", flashHeight);
		
	
		$(".flash_housing").height(flashHeight);	
		$(".flash_container").height(flashHeight);
	
		document.getElementById("dotcom").height = flashHeight;
	
		document.getElementById("iframe").width = width;
		
		if(position == "left")
		{
			document.getElementById("slider").style.left = "0px";
		}
		else if(position == "right")
		{
			document.getElementById("slider").style.left = "-" + width + "px";
		}			   
   });
}

function thisMovie(movieName)
{
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		return window[movieName];
	}
	else
	{
		 return document[movieName];
	}
}

function sendToActionScript(value)
{	 

	if(thisMovie("dotcom").sendToActionScript)
	{
		thisMovie("dotcom").sendToActionScript(value);
	}
}

function iframeChanged(iframe)
{
	// Send message back to flash to tell flash the iframe is loaded (for sidebar loader)
	
	if(position != "right")
	{
		sendToActionScript("LOAD_COMPLETE");
	}
	
	cleanLink = currentAnIdeaDeeplink;
	if(cleanLink.length > 1 && cleanLink.substring(cleanLink.length - 1) == "/") cleanLink = cleanLink.substring(0, cleanLink.length - 1);
	
	if(cleanLink != lastLinkRequested)
	{	
		$.post("/Webservices/Tagging/GetRelatedComLinks", {'url': cleanLink}, handleLoaded);		
		lastLinkRequested = cleanLink;
	}
}

function cleanAnIdeaDeeplink(url)
{
	var searchString = ".com/";
	var queryString = "?a=1";	
	var address = url.substring(url.search(searchString) + searchString.length);
	address = address.replace(queryString, "");
	
	return address;
}

// Called from ANidea iFrame and from loadRight()
function updateAnIdeaURL(url)
{	
	currentAnIdeaDeeplink = cleanAnIdeaDeeplink(url);
		
	if(position == "right")
	{		
		window.location = "http://" + window.location.host + "/#/anidea/" + currentAnIdeaDeeplink;
	}
}

function handleLoaded(data)
{	
	$(".back_inside .anidea_links").html(data);
}

$(function()
{	
	var vars = {config:"http://cdnx.agencynet.com/"};
	var params = { bgcolor: 0x000000, allowscriptaccess: "always"};
	var attributes = {id: "dotcom" };
	swfobject.embedSWF("http://cdnx.agencynet.com/Shell.swf", "dotcom", "100%", "760", "10.0.0", null, vars, params, attributes);
	
	$(".anidea .back .back_inside .dotcom_tab").mouseover(function ()
	{
		$(this).animate({left: "-1"}, 125, "swing");			
	});
	
	$(".anidea .back .back_inside .dotcom_tab").mouseout(function ()
	{
		$(this).animate({left: "-5"}, 250, "swing");
	});	
	
	if(swfmacmousewheel)
	{
		swfmacmousewheel.registerObject("dotcom");
  	}
  	
  	$(".anidea").css("display", "none");
  	
  	handleResize();
});