var position = "left"; // "left", or "right"

var currentAnIdeaDeeplink = "";
var currentDotComDeeplink = "";
var lastLinkRequested = "";
var firstTime = true;

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 + "/#/blogs/" + currentAnIdeaDeeplink;
		var location = "http://www.ariadna.com.br/index.php#/blogs/" + 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){
	alert("loadright");	
	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;
		var location = "http://www.ariadna.com.br/index.php#/" + 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();
		//alert("width> "+width + " //  height> "+height)
		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("lef", "-104px");
			
		$(".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;
		//$("#iframe1").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){
	$("#iframe1").width(getWidth());
	//alert("iframe chance");
	// 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);
	
	slide();
	
	/*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 + "/#/blogs/" + currentAnIdeaDeeplink;
		window.location = "http://www.ariadna.com.br/index.php";		
	}
}

/*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);*/	
	var version = navigator.appVersion.toLowerCase();
	
	if(version.indexOf("chrome") != -1){
		setTimeout(start, 1000);
	}else	
		start();	
});

function setDivNoFlahs(){
	var html = '<div class="contnoflash">';
     html += '         <div class="noflashcontent1">';
     html += '                      <h1 class="expa_titulo">Necesita tener JavaScript activado en su navegador, o requiere de la versión mas actualizada de Flash Player</h1>';
     html += '                      <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>';
     html += '                 </div>';                                        
     html += '                 <div class="noflashcontent2">';
     html += '                     <h1 class="expa_titulo">Ariadna agencia de publicidad digital</h1>';
     html += '                     <p class="expa_txt">Ariadna holding lo conforman un conjunto de empresas alrededor de un objetivo común: guiar y desarrollar los negocios de Latinoamérica en un nuevo mundo digital de crecimiento acelerado que exige a los negocios evolucionar al interior y al exterior, ofreciendo productos profesionales y especializados de entrenamiento, compra de medios, innovación, investigación y soporte en producción a terceros.</p>';		
     html += '                     <ul>';
     html += '                         <li><a href="http://cdi.ariadna.com.co/" class="btn_txt" style="background-color:#000;" target="_blank" >CDI Ariadna </a></li>';
     html += '                         <li><a href="http://www.ariadna.us/" class="btn_txt" style="background-color:#000;" target="_blank" >Ariadna inglés  </a></li>';
     html += '                         <li><a href="http://www.ariadna.com.br/" class="btn_txt" style="background-color:#000;" target="_blank">Ariadna portugués  </a></li>';
     html += '                   </ul>';
     html += '                 </div>';
     html += '               </div>;'
	 
	 $("#flashcontent").html(html);		
}

function start(){
	setDivNoFlahs();
	var h = window.innerHeight;
	var espacio;
	var ew;
	var w;
	var anchoSwf = 0;
	var altoSwf = 0;
	if(window.innerHeight){
		altoSwf = window.innerHeight;
		anchoSwf = window.innerWidth;
	}else if(document.documentElement.clientHeight){
		altoSwf = document.documentElement.clientHeight;
		anchoSwf = document.documentElement.clientWidth;
	}else{
		espacio = 600;	
	}
	w = 980 / (600/espacio);
	if( espacio == 600){
		espacio = 600;
		w = 980;	
	}
	if( w > ew){
		w = ew - 20;
		espacio = 600 / (980/ew);
	}                
	var flashvars = {h: altoSwf}; //Se envia el alto del area visible de la pagina
	var params = {
		quality: "high",
		scale: "default",
		wmode: "transparent",
		allowscriptaccess: "always",
		menu: false,
		swliveconnect: "true"
	};
	var attributes = {
		id: "movie",
		name: "movie"
	};
	swfobject.embedSWF("shell.swf", "flashcontent", anchoSwf, altoSwf, "9.0.124", "expressInstall.swf", flashvars, params, attributes);
	
	$(".anidea .back .back_inside .dotcom_tab").mouseover(function ()
	{
		$(this).animate({left: "100px"}, 125, "swing");			
	});
	
	$(".anidea .back .back_inside .dotcom_tab").mouseout(function ()
	{
		$(this).animate({left: "-4px"}, 250, "swing");
	});	
	/*if(swfmacmousewheel)	{
		swfmacmousewheel.registerObject("dotcom");
  	}*/  	
  	$(".anidea").css("display", "none");  	
  	handleResize();	
}
