var rootUrl = getRootUrl();
var flashHeight = "1393";

if((rootUrl.indexOf("www") == -1) && (rootUrl.indexOf("file") == -1))
{
	window.location = "http://www.chester.com.br/";
}

$(document).ready(function()
{
	$("#noflash").css("display", "none");
	embedFlash();
});

function getRootUrl()
{
	var dataReturn;
	
	var hostName = document.location.hostname;
	if (hostName == "")
	{
		dataReturn = document.location.href;
	}
	else 
	{
		dataReturn = "http://" + hostName + "/";
	}

	var arrUrl = dataReturn.split("/");
	if (arrUrl[arrUrl.length-1].indexOf(".htm") != -1)
	{
		arrUrl[arrUrl.length-1] = null;
		dataReturn = arrUrl.join("/");
	}
	
	return dataReturn;
}

function getUrlParam( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	{
		return "";
	}
	else
	{
		return results[1];
	}
}

function embedFlash()
{
	if (swfobject.hasFlashPlayerVersion("10")) 
	{
		//Has Flash
		tracker("/chester/site/geral/acesso/sucesso");
	}
	else 
	{
		//No Flash/Version
		tracker("/chester/site/geral/acesso/erro/flashplayer/" + swfobject.getFlashPlayerVersion().major);
	}

	var viewHeight = $(window).height();
	var flashvars = { root_url:rootUrl, "config_path":rootUrl + "swf/_config/app-config.xml", view_height:viewHeight };
	var params = { menu: "false", allowScriptAccess: "always", scale: "noscale", wmode: "window" };
	
	//Get Url Params
	if (getUrlParam("link") != "") 
	{
		flashvars.start_section = "cartoesdenatal";
	    flashvars.christmas_card_id = getUrlParam("link");
	}
	else if (getUrlParam("secao"))
	{
		flashvars.start_section = getUrlParam("secao");
	}
	
	//Homologacao
	var fileName = "swf/preloader.swf";
	if (window.location.href.indexOf("christmas") != -1)
	{
		fileName = "swf/preloader_christmas.swf";
	}
    
	var attributes = { id:"flash" };
	swfobject.embedSWF(fileName, "flash", "100%", flashHeight, "10.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
}

function openPolicyPopup()
{
	window.open('pages/politica_privacidade.html', 'Chester Perdigão - Política de Privacidade' , 'width=475,height=650,scrollbars=yes');
}

function printRecipe( id )
{
	//window.open(rootUrl + 'pages/printReceita.aspx?id=' + id, 'Chester Perdigão - Política de Privacidade' , 'width=475,height=650,scrollbars=yes');
	window.open(rootUrl + 'pages/printReceita.aspx?id=' + id, 'Chester' , 'width=475,height=650,scrollbars=yes');
}

function openSocialLink( name, link )
{
	var url;
	var message = "Veja%20minha%20dancinha%20com%20Chester%C2%AE.%20Crie%20a%20sua%20tamb%C3%A9m.%20" + link;
	var image = "http%3A%2F%2Fwww.chester.com.br/img/others/logo_chester_social.jpg";
	
	var strTracker;
	
	switch(name)
	{
		case "facebook" :
			url = "http://www.facebook.com/share.php?u=http://www.chester.com.br/social/index.aspx?msg=" + message;
			strTracker = "/chester/especial-de-natal/cartoes-de-natal/link-facebook";
			break;
			
		case "twitter" :
			url = "http://twitter.com/home?status=" + message;
			strTracker = "/chester/especial-de-natal/cartoes-de-natal/link-twitter";
			break;
			
		case "orkut" :
			url = "http://promote.orkut.com/preview?nt=orkut.com&tt=Chester Perdigão&cn=" + message + "&tn=" + image + "&du=http%3A%2F%2Fwww.chester.com.br";
			strTracker = "/chester/especial-de-natal/cartoes-de-natal/link-orkut";
			break;
	}
	
	if (url)
	{
		window.open(url);
	}
	
	if (strTracker)
	{
		tracker(strTracker);
	}
}

function hideScrollBar()
{
	if (navigator.appVersion.indexOf("MSIE 6.0") == -1)
	{
		$("object").attr("height", "100%");
		$("embed").attr("height", "100%");
	}
}

function showScrollBar()
{
	if (navigator.appVersion.indexOf("MSIE 6.0") == -1)
	{
		$("object").attr("height", flashHeight);
		$("embed").attr("height", flashHeight);
	}
}

function scrollTop()
{
	jQuery.fn.extend({
	  scrollTo : function(speed, easing) {
		return this.each(function() {
		  var targetOffset = $(this).offset().top;
		  $('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
	  }
	});

	$('#flash').scrollTo(500);
}


/* ----------------- GOOGLE ANALYTICS ----------------- */
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

function tracker(id)
{
	try
	{
		var pageTracker = _gat._getTracker("UA-19985709-1");
		pageTracker._trackPageview(id);
	}
	catch(err)
	{
	}
}
