$(document).ready
(
 	function()
	{
		pngAlpha('gfx/spacer.png');
		
		$('.Zoom').fancybox
		(
			{
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic'
			}
		);
	}
);

//ENABLE PNG SUPPORT FOR IE6
function pngAlpha( Spacer ) {

	if( navigator.appVersion.indexOf('MSIE 6.0') > 0 ) {

		for( i = 0; i < document.images.length; ++i ) {
			
			var Source = document.images[i].src;
			var Width = document.images[i].width;
			var Height = document.images[i].height;
			var Extension = /^.+\.png$/i;
			
			if( Extension.test(Source) ) {

				document.images[i].src = Spacer;
				document.images[i].width = Width;
				document.images[i].height = Height;
				document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + Source + "',sizingMethod='image')";

			}

		}

	}

}

ImageViewer = {
	
	open: function( ID, Data ) {
		
		--ID;
		
		$('#ImageViewer').css({display: ''});
		
		$('#Canvas').fadeIn(300, function() {
		
			ImageViewer.show( ID, Data );
			
		});
		
	},
	show: function( ID, Data ) {
		
		$('document.body').css({cursor: 'progress'});
		
		$('#Frame').css({display: ''});
		//$('#Frame').css({left: Math.round((document.body.offsetWidth-document.getElementById('Frame').offsetWidth)/2)+'px'});
		$('#Frame').css({display: 'none'});
		
		if( Data.length > 1 ) {
			
			$('#LeftButton').css({display: 'block'});
			$('#LeftButton').click(function() { ImageViewer.change(ID < 1 ? Data.length-1 : ID-1, Data); });
			
			$('#RightButton').css({display: 'block'});
			$('#RightButton').click(function() { ImageViewer.change(Data.length > ID+1 ? ID+1 : 0, Data); });
			
		}
		
		$('#ImageDescription').html(Data[ID][3]+' ('+(ID+1)+'/'+Data.length+')');
		
		$('#Image').attr({src: Data[ID][0]});
		$('#Image').attr({width: Data[ID][1]});
		$('#Image').attr({height: Data[ID][2]});
		$('#Image').load(function() {
		
			$('#Frame').fadeIn(300, function() {
			
				$('document.body').css({cursor: 'default'});
				
			});
			
		});
		
	},
	change: function( ID, Data ) {
		
		$('#Frame').fadeOut(300, function() {
		
			ImageViewer.show( ID, Data );
			
		});
		
	},
	close: function() {
		
		$('#Frame').fadeOut(300, function() {
		
			$('#LeftButton').css({display: 'none'});
			$('#RightButton').css({display: 'none'});
		
			$('#Canvas').fadeOut(300, function() {
		
				$('#ImageViewer').css({display: 'none'});
			
			});
			
		});
		
	}
	
}

function b64dec(encStr) {
	
	var bits;
	var address;
	var decOut = '';
	var i = 0;
	var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

	for(; i<encStr.length; i += 4) {
		
		bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
		
		decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
		
	}

	if( encStr.charCodeAt(i-2) == 61 ) return decOut.substring(0, decOut.length -2);
	else if( encStr.charCodeAt(i-1) == 61 ) return decOut.substring(0, decOut.length -1);
	else return decOut;
	
}
