// TKF ysSimple Design Image Gallery 1.0 | TKF Ltd. | http://webdesign.tkf.hu/ysSimple
// (CC) 2011 | Creative Commons | Attribution-NonCommercial-NoDerivs 3.0 Unported | CC BY-NC-ND
// You are free to Share [to copy, distribute and transmit the work]
// Under the following conditions
//   Attribution [You must attribute the work in the manner specified by the author or licensor, but not in any way that suggests that they endorse you or your use of the work]
//   Noncommercial [You may not use this work for commercial purposes]
//   No Derivative Works [You may not alter, transform, or build upon this work]

document.write('<link href="yssimple/yssimple.css" rel="stylesheet" type="text/css" />');

var gallery='';
var imageCount=0;
var imageIndex=0;
var linkA=new Array();

var imageOrig=0;
var hrefOrig='';

var slideshowB=false;
var slideshowI;
var slideshow=5000;

var wX=600;
var wY=400;

function titleLength(str)
	{
	$('#yssimple-strlen').html(str);
	return $('#yssimple-strlen').width();
	}

function showTitle(title)
	{
	s=title;
	if (titleLength(s)>wX-220)
		{
		do
			{
			s=s.substr(0,s.length-1);
			}
		while (titleLength(s+'...')>wX-220);
		s=s+'...';
		}
	$('#yssimple-header-left').html(s);
	s=title;
	while (s.indexOf('<')>-1)
		{
		i=s.indexOf('<');
		j=s.indexOf('>',i+1);
		s=s.replace(s.substring(i,j+1),'');
		}
	$('#yssimple-header-left').attr('title',s);
	}

function loadImage()
	{
	function imageComplete()
		{
		imgX=imageTemp.width;
		imgY=imageTemp.height;
		prop=imgX/imgY;
		cX=document.documentElement.clientWidth;
		cY=document.documentElement.clientHeight;
		if (imgX+76>cX)
			{
			imgX=cX-76;
			imgY=Math.round(imgX/prop);
			}
		if (imgY+44>cY)
			{
			imgY=cY-44;
			imgX=Math.round(imgY*prop);
			}
		wX=imgX+64;
		wY=imgY+32;
		wX=Math.max(wX,600);
		wY=Math.max(wY,400);
		contX=wX-64;
		contY=wY-32;
		$('#yssimple-loading-container').hide();
		$('#yssimple-window').animate({width:wX+'px',height:wY+'px'},'slow','swing');
		$('#yssimple-left,#yssimple-right').animate({height:contY+'px'},'slow','swing',function(){
			s=imageIndex+1;
			if (gallery=='')
				{
				sg1='';
				sg2='';
				}
			else
				{
				sg1='<b>'+gallery+'</b> | ';
				sg2=' ['+s+'/'+imageCount+']';
				}
			showTitle(sg1+linkA[imageIndex].title+sg2);
			$('#yssimple-left-pseudo,#yssimple-right-pseudo').css('height',contY+'px');
			$('#yssimple-image-container,#yssimple-loading-container').css({'width':contX+'px','height':contY+'px'});
			$('#yssimple-image').css({'width':imgX+'px','height':imgY+'px'});
			$('#yssimple-image').attr('src',imageTemp.src);
			$('#yssimple-image-container').fadeIn('normal',function(){
				if (imageIndex==0) $('#yssimple-left-pseudo').hide();
				else $('#yssimple-left-pseudo').show();
				if (imageIndex==imageCount-1) $('#yssimple-right-pseudo').hide();
				else $('#yssimple-right-pseudo').show();
			});
		});
		}
	$('#yssimple-image-container').hide();
	showTitle('<b>'+gallery+'</b>');
	$('#yssimple-left-pseudo,#yssimple-right-pseudo').hide();
	srcS=linkA[imageIndex].href;
	if (srcS=='javascript:;') srcS=hrefOrig;
	$('#yssimple-loading-container').show();
	imageTemp=new Image();
	$(imageTemp).load(function(){
		imageComplete();
	});
	$(imageTemp).attr('src',srcS);
	}

function stepImage(step)
	{
	clearTimeout(slideshowI);
	imageIndex=imageIndex+step;
	loadImage();
	}

function stepSlide()
	{
	clearTimeout(slideshowI);
	if (imageIndex==imageCount-1) slideshowB=false;
	if (slideshowB)
		{
		$('#yssimple-button-slideshow').attr('title','Stop slideshow');
		stepImage(1);
		slideshowI=setTimeout('stepSlide()',slideshow);
		}
	else
		{
		$('#yssimple-button-slideshow').attr('title','Start slideshow');
		}
}

function showGallery()
	{
	$('#yssimple-back').fadeTo('fast',0.9);
	$('#yssimple-container').fadeIn('fast',function(){
		y=wY-32;
		$('#yssimple-header,#yssimple-left,#yssimple-right').show();
		$('#yssimple-left,#yssimple-right').animate({width:'31px',height:y+'px'},'normal');
		$('#yssimple-header').animate({height:'31px'},'normal',function(){
			loadImage();
		});
	});
	}

function hideGallery()
	{
	clearTimeout(slideshowI);
	slideshowB=false;
	linkA[imageOrig].href=hrefOrig;
	y=wY-1;
	$('#yssimple-header').animate({height:'0px'},'normal');
	$('#yssimple-left,#yssimple-right').animate({width:'0px',height:y+'px'},'normal',function(){
		$('#yssimple-header,#yssimple-left,#yssimple-right,#yssimple-loading-container').hide();
		$('#yssimple-button-slideshow').attr('title','Start slideshow');
		showTitle('');
	});
	$('#yssimple-image-container,#yssimple-container,#yssimple-back').fadeOut('fast');
	}

$(document).ready(function(){
	$('body').append('<div id="yssimple-back"></div><table id="yssimple-container"><tr><td><div id="yssimple-strlen"></div><div id="yssimple-window"><div id="yssimple-header"></div><table id="yssimple-left"></table><table id="yssimple-right"></table><table id="yssimple-image-container"></table><table id="yssimple-loading-container"></table><div id="yssimple-left-pseudo" title="Previous"></div><div id="yssimple-right-pseudo" title="Next"></div></div></td></tr></table>');
	$('#yssimple-header').append('<div id="yssimple-header-left"></div><div id="yssimple-button-slideshow" title="Start slideshow"></div><div id="yssimple-button-close" title="Close"></div>');
	$('#yssimple-left').append('<tr><td><div id="yssimple-left-arrow"></div></td></tr>');
	$('#yssimple-right').append('<tr><td><div id="yssimple-right-arrow"></div></td></tr>');
	$('#yssimple-image-container').append('<tr><td><img id="yssimple-image" src="" alt="" /></td></tr>');
	$('#yssimple-loading-container').append('<tr><td><img id="yssimple-loading" src="yssimple/ysload.gif" alt="" /></td></tr>');

	$('a[rel^="yssimple"]').click(function(){
		s=$(this).attr('rel');
		gallery=s.substring(9,s.length-1);
		imageCount=$('a[rel="'+s+'"]').size();
		imageIndex=$(this).index('a[rel="'+s+'"]');
		linkA=$('a[rel="'+s+'"]').toArray();
		imageOrig=imageIndex;
		hrefOrig=$(this).attr('href');
		$(this).attr('href','javascript:;');
		showGallery();
	});
	
	$('#yssimple-button-slideshow').click(function(){
		slideshowB=!slideshowB;
		stepSlide();
	});
	$('#yssimple-button-close').click(function(){
		hideGallery();
	});
	
	$('#yssimple-left-pseudo').hover(function(){
		$('#yssimple-left-arrow').css('background','url(yssimple/yssimple.bmp) -157px -19px no-repeat');
		$('#yssimple-left').css('background-color','#3D3D3D');
		},function(){
			$('#yssimple-left-arrow').css('background','url(yssimple/yssimple.bmp) -157px 0px no-repeat');
			$('#yssimple-left').css('background-color','#323232');
	});
	$('#yssimple-right-pseudo').hover(function(){
		$('#yssimple-right-arrow').css('background','url(yssimple/yssimple.bmp) -167px -19px no-repeat');
		$('#yssimple-right').css('background-color','#3D3D3D');
		},function(){
			$('#yssimple-right-arrow').css('background','url(yssimple/yssimple.bmp) -167px 0px no-repeat');
			$('#yssimple-right').css('background-color','#323232');
	});
	$('#yssimple-left-pseudo').click(function(){
		stepImage(-1);
	});
	$('#yssimple-right-pseudo').click(function(){
		stepImage(1);
	});
});
