$(document).ready(function() {
	
	$(".news_tab").click(function() {
		$(".news_tab").removeClass("active");
		$(".items").hide();
		$(this).addClass("active");
		$($(this).attr('href').replace(/\//, "")).show();
		return false;
	});
	
	$(".video_tab").click(function() {
		$(".video_tab").removeClass("active");
		$(".video").hide();
		$(this).addClass("active");
		$($(this).attr('href').replace(/\//, "")).show();
		return false;
	});


	
	$('a[rel*=facebox]').facebox();

	//$.fn.fancyzoom.defaultsOptions.imgDir='typo3conf/ext/flickrset/res/js/ressources/';
	//$("#centerCol p img").fancyzoom({overlay:0.8});
	
	if($('.tx-timtabsociable-pi1')){
		$('.tx-timtabsociable-pi1').hide();

		$('#socials').toggle(
			function(){
				$('.tx-timtabsociable-pi1').slideDown();				
			},
			function(){
				$('.tx-timtabsociable-pi1').slideUp();				
			}			
		);	
		//$('.tx-timtabsociable-pi1').mouseout(function(){$(this).hide();});		
	}
	$('#active-image-control').css('cursor', 'pointer');
	

});


function TweetTick(ob)
{
	// This is the callback function

	var container=$('#tweet-container');
	container.html('');
	// Removing the loading gif animation

	$(ob.results).each(function(el){

		// ob contains all the tweets

		var str = '	<div class="tweet">\
		<div class="avatar"><a href="http://twitter.com/'+this.from_user+'" target="_blank"><img src="'+this.profile_image_url+'" alt="'+this.from_user+'" /></a></div>\
		<div class="user"><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a></div>\
		<div class="time">'+relativeTime(this.created_at)+'</div>\
		<div class="txt">'+formatTwitString(this.text)+'</div>\
		</div>';

		container.append(str);
		// Adding the tweet to the container
	});

	container.jScrollPane();
	// After all the tweets have been added, create the slidebar
}

function formatTwitString(str)
{
	// This function formats the tweet body text

	str=' '+str;

	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	// The tweets arrive as plain text, so we replace all the textual URLs with hyperlinks

	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	// Replace the mentions

	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	// Replace the hashtags

	return str;
}

function relativeTime(pastTime)
{
	// Generate a JavaScript relative time for the tweets

	var origStamp = Date.parse(pastTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	var difference = parseInt((currentStamp - origStamp)/1000);

	if(difference < 0) return false;

	if(difference <= 5)			return "Just now";
	if(difference <= 20)			return "Seconds ago";
	if(difference <= 60)			return "A minute ago";
	if(difference < 3600)		return parseInt(difference/60)+" minutes ago";
	if(difference <= 1.5*3600) 	return "One hour ago";
	if(difference < 23.5*3600)	return Math.round(difference/3600)+" hours ago";
	if(difference < 1.5*24*3600)	return "One day ago";

	// If the tweet is older than a day, show an absolute date/time value;

	var dateArr = pastTime.split(' ');

	return dateArr[4].replace(/\:\d+$/,'')+' '+dateArr[2]+' '+dateArr[1]+
	(dateArr[3]!=curDate.getFullYear()?' '+dateArr[3]:'');
}	





/**
 * People Selector
 */
function adjustPeopleToolButton(){
	var countryValue = $('#countryDropdown').val();
	var companyValue = $('#companyInstitutionDropdown').val();

	var peopleToolButton = $('#peopleToolButton');
	
	if (countryValue.length == 0) {
		peopleToolButton.load(
			'http://' + document.domain + '/index.php?eID=people'			
		);
		$('#peopleToolButton').attr(
			'href',
			'http://' + document.domain + '/index.php?id=239'
		);
		
	}else if(countryValue != 'undefined' && companyValue != 'undefined' ) {
		peopleToolButton.load(
			'http://' + document.domain + '/',
			'eID=people&country='+countryValue+'&company='+companyValue
		);

		$('#peopleToolButton').attr(
			'href',
			'http://' + document.domain + '/index.php?id=239&country='+countryValue+'&company='+companyValue
		);
	
	}else if(countryValue.length != 0 && companyValue.length == 0 ){
		//console.log('no company : country value : '+countryValue);
		peopleToolButton.load(
			'http://' + document.domain + '/',
			'eID=people&country=' + countryValue
		);
		$('#peopleToolButton').attr(
			'href',
			'http://' + document.domain + '/index.php?id=239&country=' + countryValue
		);
	
	}
	
}


function adjustCompanyDropdown(){
	var countryValue = $('#countryDropdown').val();
	var dropdownSet = $('#companyInstitutionDropdown');
	if(countryValue.length == 0){
		dropdownSet.attr('disabled', true);
		adjustPeopleToolButton();
	} else {
		adjustPeopleToolButton();
		
		dropdownSet.attr('disabled', false);
		dropdownSet.load(
			'http://' + document.domain + '/index.php?eID=company',
			'country=' + countryValue		
		);
		
		
	}
}	

