var img_count = 0;
var index = 0;

function render(num, width, height)
	{
	var cutAImg = $('.events_pics a').eq(num);
	var cutImg = cutAImg.find('img');
	var cutImgBig = cutAImg.attr('href');

	var title = cutImg.attr('title');
	var text = cutImg.attr('text');
	

	var h = $(document).height();
	//var tY = (((h*1)/2) - ((height*1)/2))-100;
	var tY = 100;
	$('.winshade').css('width',width).css('margin-left', '-'+(width/2)+'px').css('height', height*1+20).css('top', tY);
	
	var aY = (((height*1)/2) - ($('.winshade a.left').height()*1)/2);
	$('.winshade a.left').css('top',aY);
	$('.winshade a.right').css('top',aY);

	$('.shade .win-cont').css('background', 'url('+cutImgBig+')').css('height', height);
	$('.shade .info').css('top', height*1+50);
	
	if (typeof title != 'undefined')
	{
		$('.shade .info .title').html(title);
	}
	else
	{
		$('.shade .info .title').hide();
	}
	
	if (typeof text != 'undefined')
		{
		$('.shade .info p').html(text);
		}
	else
	{
			$('.shade .info p').hide();
	}
		
	Cufon.replace('.shade .info .title', {fontFamily: 'Myriad Pro'});
	}

$(document).ready(function(){
	img_count = $('.events_pics a').length;

	$('.shade').click(function(){
		$(this).hide();
		return false;
	});

	if (img_count>1)
		{
		$('.shade').find('a.left').removeClass('disabled');
		$('.shade').find('a.right').removeClass('disabled');
		}

	var imgs = new Array();
	$('.events_pics a').each(function(num){
	
		var img = $(this);
		img.unbind('click');
		img.attr('onclick','');
		img.attr('target','');

		var tmp = new Image;
		tmp.onload = function(){
				var z = $('.events_pics a').eq(num); 
				z.attr('xwidth',this.width).attr('xheight', this.height);
				}
		tmp.src = img.attr('href');

		

		img.click(function(){

			index = num;

			location='#top';
			
			
			render(num, $(this).attr('xwidth'), $(this).attr('xheight'));

			$('.shade .win-cont').unbind('click').click(function(){
				index++;
				if (index>img_count-1) index=0;								
				render(index, $('.events_pics a').eq(index).attr('xwidth'), $('.events_pics a').eq(index).attr('xheight'));
				return false;	
			});

			$('.shade a.left').unbind('click').click(function(){
				index--;
				if (index<0) index = img_count-1;								
				render(index, $('.events_pics a').eq(index).attr('xwidth'), $('.events_pics a').eq(index).attr('xheight'));
				return false;
				});

			$('.shade a.right').unbind('click').click(function(){
				index++;
				if (index>img_count-1) index=0;								
				render(index, $('.events_pics a').eq(index).attr('xwidth'), $('.events_pics a').eq(index).attr('xheight'));
				return false;
				});

			var h = $(document).height();

			var shade = $('.shade');
			shade.height(h);
			shade.show();

			return false;
		})

	});
});
