var tw = [];

$(document).ready( function() {
	
	if ($('#tweet_search').length>0) {
		var url = "http://search.twitter.com/search.json?q=";
		$('ul#tweet_search li').each(function(index) {
			url = url + $(this).text()+'+OR+';
		});
		url = url.substring(0,url.length-4);
		
		url = url + '&rpp=2&callback=?';
		$.getJSON(url,function(data){
			if (data.results.length>0) {
				$.each(data.results, function(i, item) {
					if (i==0) {
						$('#tweet_1 p.text').text(item.text);
						$('#tweet_1 p.tweetby').html('twitted by <a target="blank_" href="http://twitter.com/'+item.from_user+'">'+item.from_user+'</a>');
					} else {
						$('#tweet_2 p.text').text(item.text);
						$('#tweet_2 p.tweetby').html('twitted by <a target="blank_" href="http://twitter.com/'+item.from_user+'">'+item.from_user+'</a>');
					}
				});				
			} else {
				$('#body_container').css('background','white');
				$('div.twitter_container').fadeOut();
			}
		});
	} else {
		$('#body_container').css('background','white');
		$('div.twitter_container').fadeOut();
	}

});


function preloadImages(){
	// preload de images
	i1_1 = new Image();
	i1_1.src="/images/wdpaMarine/t02.jpg";
	i1_2 = new Image();
	i1_2.src="/images/wdpaMarine/t03.jpg";

	i2_1 = new Image();
	i2_1.src="/images/runnity/t02.jpg";
	
}


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec, alt, total, workn, linkn) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	document.getElementById(divid).style.backgroundRepeat = "no-repeat";		

	//make image transparent
	changeOpac(0, imageid);

	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
		
	//change class and description
	}
	substr = imageid.substring(10,11);
	for (i=1; i<=total; i++) {
		link = "workLink" + workn + i;
		document.getElementById(link).className = "";
	}

	document.getElementById("workLink"+workn+linkn).className = "current";

}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;

	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}