var portfolio_images = new Array();
var portfolio_pointer = 0;

function checkFlashSupport()
{
	if (navigator.mimeTypes ["application/x-shockwave-flash"] == undefined) return false;
	
	return true;
}

function gallery_load(object){
	var object = object;
	var $image_container = $('#portfolio-full');
	var $image_wrapper = $('#portfolio-image-wrapper');
	var $title_container = $('#portfolio-title');
	
	// remove the old image and display the loading-icon
	
	$image_wrapper.css({visibility: 'hidden'}).empty();
	$title_container.empty();
	$image_container.prepend('<div class="loading"></div>');
		
	// display the image-info
	
	$title_container.html('<p>'+object.title+'</p>');
	
	$(this).html(fixURLs($title_container.html()));
		
	// display the image / video
	
	portfolio_pointer = object.id;
	location.hash = object.id;
	
	var max_width = 896;
	var max_height = 504;
	
	if (object.url.match(/youtube/)){
		var clip_id = object.url.split('/');
		clip_id = clip_id.pop();
		clip_id = clip_id.replace('watch?v=', '');
		
		if (checkFlashSupport()) {
			$image_wrapper.append('<object width="' + max_width + '" height="' + max_height + '"><param name="wmode" value="transparent" /><param name="movie" value="http://www.youtube.com/v/'+clip_id+'?fs=1&amp;hl=de_DE"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+clip_id+'?fs=1&amp;hl=de_DE" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + max_width + '" height="' + max_height + '" wmode="transparent"></embed></object>');
		} else {
			noFlashMessage();
		}
		
		$image_container.find('.loading').remove();
		
		$image_wrapper.css({visibility: 'visible'});
	} else if (object.url.match(/vimeo/)) {
		var clip_id = object.url.split('/');
		clip_id = clip_id.pop();
	
		if (checkFlashSupport()) {
			$image_wrapper.append('<object width="' + max_width + '" height="' + max_height + '"><param name="allowfullscreen" value="true" /><param name="wmode" value="transparent" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+clip_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ff9933&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+clip_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ff9933&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' + max_width + '" height="' + max_height + '" wmode="transparent"/></object>');
		} else {
			noFlashMessage();
		}
		
		$image_container.find('.loading').remove();
		
		$image_wrapper.css({visibility: 'visible'});
	} else if (object.url.match(/jpg/)){	
		var uid = getUniqueId();
	
		$image_wrapper.image(object.url, {alt: object.alt, title: object.title}, function(){
			$image_container.find('.loading').remove();
				
			var image_width = $image_wrapper.find('img').width();
			var image_height = $image_wrapper.find('img').height();
			var new_image_width = max_width;
			var new_image_height = max_height;
			
			var aspect_ratio = ratio = Math.min( max_width / image_width, max_height/ image_height );
			
			new_image_width = aspect_ratio * image_width;
			new_image_height = aspect_ratio * image_height;
			
			$image_wrapper.css({visibility: 'visible'});
			$image_wrapper.find('img').css({width: new_image_width, height: new_image_height});		
		});
	}
	
	// Show / Hide Navgiation
	
	if (portfolio_pointer > 0) {
		$('#gallery-navigation .previous').fadeIn(100);
	} else {
		$('#gallery-navigation .previous').fadeOut(100);
	}
	
	if (portfolio_pointer < portfolio_images.length - 1) {
		$('#gallery-navigation .next').fadeIn(100);
	} else {
		$('#gallery-navigation .next').fadeOut(100);
	}
}


function gallery_init() {
	if (portfolio_images.length > 0) {
		hash = parseInt(location.hash.replace('#', ''));
		
		if (hash < portfolio_images.length){
			portfolio_pointer = hash;
		}
	
		gallery_load(portfolio_images[portfolio_pointer]);
		
		$('#gallery-navigation .previous').click(function(){
			portfolio_pointer = portfolio_pointer - 1;
		
			gallery_load(portfolio_images[portfolio_pointer]);
		});
		
		$('#gallery-navigation .next').click(function(){
			portfolio_pointer = portfolio_pointer + 1;
		
			gallery_load(portfolio_images[portfolio_pointer]);
		});
	} else {	
		$('#gallery-navigation').hide();
	}
}

function matchAnchors(text)
{	
	var pattern = /(http:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]+)/ig
	var tags = '';
	
	if (text.search(/bernhard.esperester.de/) == -1){
		tags += 'target="_blank"';
	}
	
	return text.replace(pattern,'<a href="$1" '+tags+'>$1</a>');
}

function fixURLs(input)
{
	if (input != undefined) {
		var anchor_pattern1 = /<a.*\/a>/ig;
		var anchor_pattern2 = /(<a.*\/a>)/ig;
		var words = input.split(anchor_pattern1);
		var output = '';
		var url_array = input.match(anchor_pattern2);
			
		for (i = 0; i < words.length; i++) {
			output = output + matchAnchors(words[i]);
			
			if (url_array != null && url_array[i] != undefined) {
				output = output + url_array[i];
			}
		}
		
		return(output);
	}
}

function noFlashMessage()
{
	$('#portfolio-image-wrapper object').remove();
	$('#portfolio-image-wrapper').append('<div class="error"><h1>Ooops</h1><p>It looks like your Browser doesn\'t support flash, sorry.</p></div>');
}

$(document).ready(function(){	
	$('a.gallery').each(function(i){
		var object = new Object();
		
		object.id = i;
		object.title = $(this).children(':first-child').attr('title');
		object.alt = $(this).children(':first-child').attr('alt');
		object.url = $(this).attr('href');
		
		if (object.title == undefined){
			object.title = object.alt;
		}
		
		portfolio_images[portfolio_images.length] = object;
		
		$(this).click(function(){
			gallery_load(object);
			
			return false;
		});
	});
	
	gallery_init();
	
	$('p').each(function(){
		$(this).html(fixURLs($(this).html()));
	});
	
	$('object').each(function(){
		if (!checkFlashSupport()) {
			noFlashMessage();
		}
	});
});
