var lbshowing = false;
var images = [];
var index = 0;

function stristr (haystack, needle, bool) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '

    var pos = 0;

    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if (pos == -1){
        return false;
    } else{
        if (bool) {
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

function prependImage(image){
	// Support for youtube videos
	if(stristr(image,'youtube.com')){
		$('#imgcenter').prepend('<iframe id="curimg" title="YouTube video player" class="youtube-player" type="text/html" width="640" height="390" src="' + image + '" frameborder="0" allowFullScreen></iframe>');
	// Support for vimeo videos
	}else if(stristr(image,'vimeo.com')){
		$('#imgcenter').prepend('<iframe id="curimg" src="' + image + '" width="640" height="390" frameborder="0"></iframe>');
	// All else is interpreted as an image
	}else{
		$('#imgcenter').prepend('<img id="curimg" src="' + image + '" width="960" style="display: none;" />');
	}
}

$(document).ready(function () {
	// list of links
	$('.worktbl').find('td.im').css('cursor', 'pointer');
	$('.worktbl').find('td.im').click(showLink);
	$('.worktbl').find('td.im').mouseover(function () {
		$(this).css('color', '#f00');
	});
	$('.worktbl').find('td').mouseout(function () {
		$(this).css('color', '#777');
	});
	
	// next/prev images
	$('#nextimg').click(nextImg);
	$('#previmg').click(prevImg);
	
	// lightbox
	$('#lightbox_close').click(function () {
		if (lbshowing) {
			$('#lightbox').css('display', 'none');
			$('#mask').css('display', 'none');
			$('#curimg').attr('src', '');
			$('#curlbl').text('');
			lbshowing = false;
		}
	});
	
	// resizing
	$(document).resize(function () {
		var m = $('#mask');
		var s = clientSize();
		m.width($(document).width());
		m.height($(document).height());
	});
	
	$('#lightbox').css('width', '1000px');
});


clientSize = function() {
    if(window.innerHeight || window.innerWidth){
        return {w:window.innerWidth, h:window.innerHeight}
    }
    return {
        w:document.documentElement.clientWidth,
        h:document.documentElement.clientHeight
    }
}

showLink = function () {
	var lb = $('#lightbox');
	if (!lbshowing) {
		var n = $(this).text();
		lb.find('h3').text(n);
		lb.find('p').html('<center><big class="red"><b>loading...</b></big></center>');
		$('#mask').css('display', 'block');
		$('#previmg').css('display', 'none');
		$('#curimg').css('display', 'none');
		$('#nextimg').css('display', 'none');
		$('#curlbl').text('loading...');
		$('#lightbox').css('width', '0px');
		lb.css('display', 'block');
		$.get('work_fetch.php', {name: n}, function (data) {
			lb.find('p').html(data);
			images = data.split("\n");
			index = -1;
			nextImg();
		});
		lbshowing = true;
	}
}

nextImg = function () {
	index += 1;
	if (index >= images.length)
		index = 0;
	$('#curimg').remove();
	prependImage(images[index]);
	if (images.length == 1 && images[0] == '')
		$('#curlbl').text('no images');
	else
	{
		$('#curimg').css('display', 'block');
		$('#nextimg').css('display', 'block');
		$('#previmg').css('display', 'block');
		$('#lightbox').css('width', '1000px');
		$('#curlbl').text((index + 1) + ' of ' + (images.length));
	}
}

prevImg = function () {
	index -= 1;
	if (index < 0)
		index = images.length - 1;
	$('#curimg').remove();
	prependImage(images[index]);
	if (images.length == 1 && images[0] == '')
		$('#curlbl').text('no images');
	else
	{
		$('#curimg').css('display', 'block');
		$('#nextimg').css('display', 'block');
		$('#previmg').css('display', 'block');
		$('#lightbox').css('width', '1000px');
		$('#curlbl').text((index + 1) + ' of ' + (images.length));
	}
}
