// JavaScript Document

/*****************************/
/*****FUNCOES AUXILIARES*****/
/***************************/

/*************************************************************
* NLB Background Color Fader v1.0
* Author: Justin Barlow - www.netlobo.com
*
* Description:
* The Background Color Fader allows you to gradually fade the
* background of any HTML element.
*
* Usage:
* Call the Background Color Fader as follows:
*   NLBfadeBg( elementId, startBgColor, endBgColor, fadeTime );
*
* Description of Parameters
*   elementId - The id of the element you wish to fade the
*             background of.
*   startBgColor - The background color you wish to start the
*             fade from.
*   endBgColor - The background color you want to fade to.
*   fadeTime - The duration of the fade in milliseconds.
*************************************************************/

var nlbFade_hextable = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]; // used for RGB to Hex and Hex to RGB conversions
var nlbFade_elemTable = new Array( ); // global array to keep track of faded elements
var nlbFade_t = new Array( ); // global array to keep track of fading timers
function NLBfadeBg( elementId, startBgColor, endBgColor, fadeTime )
{
	var timeBetweenSteps = Math.round( Math.max( fadeTime / 300, 30 ) );
	var nlbFade_elemTableId = nlbFade_elemTable.indexOf( elementId );
	if( nlbFade_elemTableId > -1 )
	{
		for( var i = 0; i < nlbFade_t[nlbFade_elemTableId].length; i++ )
			clearTimeout( nlbFade_t[nlbFade_elemTableId][i] );
	}
	else
	{
		nlbFade_elemTable.push( elementId );
		nlbFade_elemTableId = nlbFade_elemTable.indexOf( elementId );
	}
	var startBgColorRGB = hexToRGB( startBgColor );
	var endBgColorRGB = hexToRGB( endBgColor );
	var diffRGB = new Array( );
	for( var i = 0; i < 3; i++ )
		diffRGB[i] = endBgColorRGB[i] - startBgColorRGB[i];
	var steps = Math.ceil( fadeTime / timeBetweenSteps );
	var nlbFade_s = new Array( );
	for( var i = 1; i <= steps; i++ )
	{
		var changes = new Array( );
		for( var j = 0; j < diffRGB.length; j++ )
			changes[j] = startBgColorRGB[j] + Math.round( ( diffRGB[j] / steps ) * i );
		if( i == steps )
			nlbFade_s[i - 1] = setTimeout( 'document.getElementById("'+elementId+'").style.backgroundColor = "'+endBgColor+'";', timeBetweenSteps*(i-1) );
		else
			nlbFade_s[i - 1] = setTimeout( 'document.getElementById("'+elementId+'").style.backgroundColor = "'+RGBToHex( changes )+'";', timeBetweenSteps*(i-1) );
	}
	nlbFade_t[nlbFade_elemTableId] = nlbFade_s;
}
function hexToRGB( hexVal )
{
	hexVal = hexVal.toUpperCase( );
	if( hexVal.substring( 0, 1 ) == '#' )
		hexVal = hexVal.substring( 1 );
	var hexArray = new Array( );
	var rgbArray = new Array( );
	hexArray[0] = hexVal.substring( 0, 2 );
	hexArray[1] = hexVal.substring( 2, 4 );
	hexArray[2] = hexVal.substring( 4, 6 );
	for( var k = 0; k < hexArray.length; k++ )
	{
		var num = hexArray[k];
		var res = 0;
		var j = 0;
		for( var i = num.length - 1; i >= 0; i-- )
			res += parseInt( nlbFade_hextable.indexOf( num.charAt( i ) ) ) * Math.pow( 16, j++ );
		rgbArray[k] = res;
	}
	return rgbArray;
}
function RGBToHex( rgbArray )
{
	var retval = new Array( );
	for( var j = 0; j < rgbArray.length; j++ )
	{
		var result = new Array( );
		var val = rgbArray[j];
		var i = 0;
		while( val > 16 )
		{
			result[i++] = val%16;
			val = Math.floor( val/16 );
		}
		result[i++] = val%16;
		var out = '';
		for( var k = result.length - 1; k >= 0; k-- )
			out += nlbFade_hextable[result[k]];
		retval[j] = padLeft( out, '0', 2 );
	}
	out = '#';
	for( var i = 0; i < retval.length; i++ )
		out += retval[i];
	return out;
}
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function( val, fromIndex ) {
		if( typeof( fromIndex ) != 'number' ) fromIndex = 0;
		for( var index = fromIndex, len = this.length; index < len; index++ )
			if( this[index] == val ) return index;
		return -1;
	}
}
function padLeft( string, character, paddedWidth )
{
	if( string.length >= paddedWidth )
		return string;
	else
	{
		while( string.length < paddedWidth )
			string = character + string;
	}
	return string;
}

/******************************************/
/**********Movimentos de Elevador**********/
/******************************************/

/*Gerais*/

$(function() {
	$('div.homepage div.menu div.menuItem a').bind('click',function(event){
		var $anchor = $(this);
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1500,'easeInOutExpo');
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		*/
		event.preventDefault();
	});
});

$(function() {
	$('div.headerArea a').bind('click',function(event){
		var $anchor = $(this);
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1500,'easeInOutExpo');
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		*/
		event.preventDefault();
	});
});

/*Fim Gerais*/


/*Homepage*/

$(function() {
	$('div.contentBig a').bind('click',function(event){
		var $anchor = $(this);
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1500,'easeInOutExpo');
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		*/
		event.preventDefault();
	});
});

$(function() {
	$('div.contentSmall a').bind('click',function(event){
		var $anchor = $(this);
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1500,'easeInOutExpo');
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		*/
		event.preventDefault();
	});
});


$(document).ready(function() {
	$("#twitter").getTwitter({
		userName: "tichapenicheiro",
		numTweets: 1,
		loaderText: "",
		slideIn: false,
		slideDuration: 0,
		showHeading: false,
		headingText: "",
		showProfileLink: false,
		showTimestamp: false
	});
});


/*Fim Homepage*/


/**************************************************************************************************/
/********************************** AJAX FUNCTIONS ************************************************/
/**************************************************************************************************/
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
		if (req.status == 200) {
        // ...processing statements go here...		
		//alert(req.responseText);
		response  = req.responseXML.documentElement;
		method = response.getElementsByTagName('method')[0].firstChild.data;
		result = response.getElementsByTagName('result')[0];
		eval(method + '(\'\', result)');
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}
/*************************** FIM AJAX FUNCTION ************************************************************/





/******************************************/
/*******************Overs******************/
/******************************************/

/*Gerais*/

$(document).ready(function() {
	$('.more').each(function () {
		$(this).hover(function () {
			NLBfadeBg(this.id, '#000000','#FFFFFF', 300 );
			$(this).css("color","black");
		}, function () {

			NLBfadeBg(this.id, '#FFFFFF','#000000', 300 );
			$(this).css("color","white");
		});
	});
});

$(document).ready(function() {
	$('.headerArea').each(function () {
		var $span = $('.headerAreaOver', this).css('opacity', 0);
		$(this).find('.headerClickableArea').hover(function () {
			$span.stop().fadeTo(500, 1);
		}, function () {
			$span.stop().fadeTo(500, 0);
		});
	});
});

/*Fim Gerais*/


/*Homepage*/
			
$(document).ready(function() {
	$('.contentBig').each(function () {
		var $span = $('.contentText', this).css('opacity', 1);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 0);
		}, function () {
			$span.stop().fadeTo(500, 1);
		});
	});
});


$(document).ready(function() {
	$('.contentSmall').each(function () {
		var $span = $('.contentOverSmall', this).css('opacity', 0);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 1);
		}, function () {
			$span.stop().fadeTo(500, 0);
		});
	});
});
			
/*Fim Homepage*/


/*News*/

$(document).ready(function() {
	$('.newsTopContent').each(function () {
		$(this).hover(function () {
			$(this).css("background-color","black");
			$(this).css("color","white");
			$(this).find('.newsTopContentInside').css("color","white");
		}, function () {
			$(this).css("background-color","white");
			$(this).css("color","#777");
			$(this).find('.newsTopContentInside').css("color","black");
		});
	});
});

$(document).ready(function() {
	$('.newsBottomContent').each(function () {
		$(this).hover(function () {
			$(this).css("background-color","black");
			$(this).css("color","white");
			$(this).find('.newsBottomContentInside').css("color","white");
		}, function () {
			$(this).css("background-color","white");
			$(this).css("color","#777");
			$(this).find('.newsBottomContentInside').css("color","black");
		});
	});
});

$(document).ready(function() {
	$('.newsClippingContent').each(function () {
		$(this).hover(function () {
			$(this).css("background-color","black");
			$(this).css("color","white");
			$(this).find('.newsClippingContentInside').css("color","white");
		}, function () {
			$(this).css("background-color","white");
			$(this).css("color","#777");
			$(this).find('.newsClippingContentInside').css("color","black");
		});
	});
});
			
/*Fim News*/


/*Media*/

$(document).ready(function() {
	$('.galleryImage').each(function () {
		var $span = $('.galleryImageOver', this).css('opacity', 0);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 1);
		}, function () {
			$span.stop().fadeTo(500, 0);
		});
	});
});

/*Fim Media*/

/*
$(document).ready(function() {
	$('.passive').each(function () {
		$(this).hover(function () {
			$(this).css("background-color","black");
			$(this).css("color","white");
		}, function () {
			$(this).css("background-color","white");
			$(this).css("color","black");
		});
	});
});

*/

/*Career-Facts and Numbers*/

$(document).ready(function() {
	$('.buttonFactsAndNumbers').each(function () {
		$(this).hover(function () {
			NLBfadeBg(this.id, '#000000','#FFFFFF', 300 );
			$(this).css("color","black");
		}, function () {
			NLBfadeBg(this.id, '#FFFFFF','#000000', 300 );
			$(this).css("color","white");
		});
	});
});

/*Fim Career-Facts and Numbers*/


/*Goodies*/	

$(document).ready(function() {
	$('.galleryImage').each(function () {
		var $span = $('.linksImage', this).css('opacity', 0);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 1);
		}, function () {
			$span.stop().fadeTo(500, 0);
		});
	});
});

$(document).ready(function() {
	$('.galleryImage').each(function () {
		var $span = $('.contentText', this).css('opacity', 1);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 0);
		}, function () {
			$span.stop().fadeTo(500, 1);
		});
	});
});

/*Fim Goodies*/

/******************************************/
/*************Insercao de Dados************/
/******************************************/

/*Homepage*/
              
/*Fim Homepage*/


/***************************************************************************************************/
/************************************   CLIPPING **************************************************/

function addClipping(title, strDate, foto, fonte, texto, esquerdo, idClip)
{
	var myHTML = "";
	var thisMargin;
	//alert('addClipping' + title);
	if(esquerdo)
		thisMargin = 18;
	else
		thisMargin = 0;
	if(title=='')
		myHTML="";
	else
	myHTML = "<div class=\"newsClippingContent\" onclick=\"javascript:loadClippingDetail(this,'"+title+"', '"+strDate+"', '"+foto+"', '"+fonte+"', '"+texto+"');\" id =\""+ idClip +"\" style=\"margin-right:" + thisMargin + "px\"><div class=\"title\"><div class=\"newsClippingContentInside\">" + unescape(title) + "</div><div class=\"textDate\">" + strDate + "</div><div style=\"overflow:hidden; width:440px; height:274px;\"><img src=\"" + foto + "\" style=\"margin-bottom:14px;\" width=\"440\" alt=\"" + unescape(title) + "\"/></div><div style=\"font-weight:normal; margin-top:3px; margin-bottom:15px;\">" + fonte + "</div></div></div>";
	return myHTML;
}

function loadClippingDetail(objCliked,title, strDate, foto, fonte, texto){
	//TODO 
	//por a div a aparecer com fade
	document.getElementById("titleClippingNews").innerHTML = unescape(title);
	document.getElementById("dateClippingNews").innerHTML = strDate;
	document.getElementById("fotoClippingNews").setAttribute('src', foto);
	document.getElementById("thumbTextoClippingNews").setAttribute('src', foto);
	document.getElementById("fonteClippingNews").innerHTML = fonte;
	document.getElementById("textoClippingNews").innerHTML = texto;
	
	document.getElementById('textoClippingNews').setAttribute('style','display:none');
	document.getElementById('thumbTextoClippingNews').setAttribute('style','display:none');
	document.getElementById('fotoClippingNews').setAttribute('style','display:inline');

	var idObj = objCliked.id.split('_');
	
	document.getElementById('facebookLikes').innerHTML = "<div id=\"btnLike_clipping\" style=\"width: 47px; height: 24px; display: block; float: left;\"><fb:like class=\" fb_edge_widget_with_comment fb_iframe_widget\" href=\"http://www.tichapenicheiro.com/share.php?type=clipping&amp;id=\""+ idObj[0] +"\" layout=\"button_count\" show_faces=\"false\" width=\"47\" height=\"24\"><span><iframe src=\"http://www.facebook.com/plugins/like.php?channel_url=http%3A%2F%2Fstatic.ak.fbcdn.net%2Fconnect%2Fxd_proxy.php%3Fversion%3D2%23cb%3Df184c8ef584f222%26origin%3Dhttp%253A%252F%252Ftichapenicheiro.tinsight.loc%252Ffb4766dc4320de%26relation%3Dparent.parent%26transport%3Dpostmessage&amp;href=http%3A%2F%2Fwww.tichapenicheiro.com%2Fshare.php%3Ftype%3Dclipping%26id%3D" + idObj[0] +"&amp;layout=button_count&amp;locale=en_US&amp;node_type=link&amp;sdk=joey&amp;show_faces=false&amp;width=90\" class=\"fb_ltr\" title=\"Like this content on Facebook.\" style=\"border: medium none; overflow: hidden; height: 21px; width: 90px;\" name=\"f1ad9f1279003b6\" id=\"f6e36dc2db70f8\" scrolling=\"no\"></iframe></span></fb:like></div>";
	
	
	
	document.getElementById('twitweerFacebookBts').innerHTML = "<div class=\"btnShares\" id=\"btnShares_clipping\"><div class=\"btnFacebook\" onclick=\"javascript:shareFacebook('clipping'," + idObj[0] + ",1);\"></div><div class=\"btnTwitter\" onclick=\"javascript:shareTwitter('clipping', " + idObj[0] +",1);\"></div></div>";
	
	//calcula distancia ao topo
	var distTop = 0;
	do {
			distTop += objCliked.offsetTop;
		} while (objCliked = objCliked.offsetParent);
	

	document.getElementById("clippingNews").style.top = distTop + 'px';
	document.getElementById("clippingNews").style.visibility ='visible';

}

/***************************************************************************************************/
/************************************   NEWS **************************************************/

function addNews(title, strDate, fonte, strLink)
{
	var myHTML = "";
	
	myHTML = "<a href=\"" + strLink + "\" target=\"_blank\"><div class=\"newsBottomContent\"><div class=\"title\">  		<div class=\"newsBottomContentInside\">" + title + "</div><div style=\"font-weight:normal; margin-bottom:20px;\">" + strDate + "</div><div style=\"font-weight:normal;\">" + fonte + "</div></div></div></a>";

	return myHTML;
}



function reporAnimacoesHome(){
	
$('.contentBig').each(function () {
		var $span = $('.contentText', this).css('opacity', 1);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 0);
		}, function () {
			$span.stop().fadeTo(500, 1);
		});
	});
		

	$('.contentSmall').each(function () {
		var $span = $('.contentOverSmall', this).css('opacity', 0);
		$(this).hover(function () {
			$span.stop().fadeTo(500, 1);
		}, function () {
			$span.stop().fadeTo(500, 0);
		});
	});


	$('a').bind('click',function(event){
		var $anchor = $(this);
		if($($anchor.attr('href')).offset()){
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1500,'easeInOutExpo');
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		*/
		event.preventDefault();
		}
	});
	
	
/*

	$('a').bind('click',function(event){
		var $anchor = $(this);
		alert("AA"+$anchor);
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1500,'easeInOutExpo');
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		
		event.preventDefault();
	});
*/
	
	
}

