$(document).ready(main);

function main()
{
	$('iframe').attr('src', getIframeUrl() );
	registerEvents();
	resizeIframe();
}

function getIframeUrl()
{
	var url = window.location.href;
	var iframe_url = 'index.php?option=com_content&view=frontpage';
	var param_start = url.indexOf("iframe=");
	if( param_start != -1 ) 
		iframe_url = url.substr(param_start+7,url.length-param_start-7);
	if( iframe_url.indexOf("http://") == -1) 
		//iframe_url = "http://" + iframe_url;
		iframe_url = iframe_url;
	
	return iframe_url;
}

function registerEvents()
{
	$(window).resize( function() {resizeIframe();} );
	$("#back").bind("click",function(){window.history.back();});
	$("#arrow").bind("click",onArrow);
	$("#searchBtn").bind("click",onSearch);
	$("#close").bind("click", function(){window.location.href = $("iframe").attr("src");});
	$("#twitter").bind("click", function(){ window.location.href = "http://twitter.com/?status="+getIframeUrl()+" - ";});
	$("#search").bind("keypress", function(e)
		{
			e = e || window.event;
		
			if( e.keyCode == 13 )
			{
				onSearch();
			}
		} );
}

var arrawState = "up";

function onArrow()
{
	if( arrawState == "up" ) onDownArrow();
		else onUpArrow();
}

function onDownArrow()
{
	
	$("#toolbar").animate(
		{
			height: 125
		}, 1000, "swing", function()
			{
				$("#arrow").css("background-position", "-32px 0");
				resizeIframe();
				arrawState = "down";
			});
}

function onUpArrow()
{
	$("#toolbar").animate(
		{
			height: 25
		}, 1000, "swing", function()
			{
				$("#arrow").css("background-position", "-16px 0");
				resizeIframe();
				arrawState = "up";
			});
}

function onSearch()
{
	var qs = $("#qs").val();
	$('iframe').attr('src', 'http://www.google.com/search?q='+qs);	
}


function resizeIframe()
{
	$("#iframe").height( WindowHeight() - getObjHeight(document.getElementById("toolbar")) );
}

function WindowHeight()
{
	var de = document.documentElement;
	return self.innerHeight || 
		(de && de.clientHeight ) ||
		document.body.clientHeight;
}

function getObjHeight(obj)
{
	if( obj.offsetWidth )
	{
		return obj.offsetHeight;
	}		
	return obj.clientHeight;
}

function mostra_data(){
	mydate = new Date();
	myday = mydate.getDay();
	mymonth = mydate.getMonth();
	myweekday= mydate.getDate();
	weekday= myweekday;
	
	if(myday == 0)
	day = " Domingo, "
	
	else if(myday == 1)
	day = " Segunda - Feira, "
	
	else if(myday == 2)
	day = " Terça - Feira, "
	
	else if(myday == 3)
	day = " Quarta - Feira, "
	
	else if(myday == 4)
	day = " Quinta - Feira, "
	
	else if(myday == 5)
	day = " Sexta - Feira, "
	
	else if(myday == 6)
	day = " Sábado, "
	
	if(mymonth == 0)
	month = "Janeiro "
	
	else if(mymonth ==1)
	month = "Fevereiro "
	
	else if(mymonth ==2)
	month = "Março "
	
	else if(mymonth ==3)
	month = "Abril "
	
	else if(mymonth ==4)
	month = "Maio "
	
	else if(mymonth ==5)
	month = "Junho "
	
	else if(mymonth ==6)
	month = "Julho "
	
	else if(mymonth ==7)
	month = "Agosto "
	
	else if(mymonth ==8)
	month = "Setembro "
	
	else if(mymonth ==9)
	month = "Outubro "
	
	else if(mymonth ==10)
	month = "Novembro "
	
	else if(mymonth ==11)
	month = "Dezembro "
	
	var returning, returning2;
	date=new Date();
	returning = day;
	returning2 = " " + myweekday+" de "+month+ " de " + date.getFullYear();
	var myDiv1 = document.getElementById("data");
	myDiv1.appendChild(document.createTextNode(returning+returning2));
}

