var KeysActive = false;

document.onkeydown = function( e ) {
	
	if( !e ) e = window.event;
	
	//alert(e.keyCode);
	
	if( e.keyCode == 17000 ) {
		
		$('#Options').fadeOut(300);
		
		$OptionsTimeout = setTimeout("$('#Options').fadeIn(300)", 1000);
		
	}
	
	//if( !KeysActive ) return false;
	
	if( e.keyCode == 37000 ) {
		
		clearInterval(ContentInterval);
		
		PrevContent();
		
	}
	
	if( e.keyCode == 39000 ) {
		
		clearInterval(ContentInterval);
		
		NextContent();
		
	}
		
}

document.onkeyup = function( e ) {
	
	if( !e ) e = window.event;
	
	if( e.keyCode == 27 ) clearTimeout($OptionsTimeout);
	
}

var Content = 0;

$(document).ready(function() {
	
	if( Contents.length ) {
		
		var SwitcherContent = '';
		
		for( Content in Contents ) {
			
			SwitcherContent += '<img id="ContentSwitcher'+Contents[Content].ID+'_0"'+(CurrentContent == Content && !CurrentSubcontent ? ' class="Selected"' : '')+' src="'+SwitcherImage+'" width="8" height="8" alt="'+Contents[Content].Title+'" onmouseover="$(this).addClass(\'Active\'); $(\'#ContentTitle\').html(\''+Contents[Content].Title+'\')" onmouseout="$(this).removeClass(\'Active\'); $(\'#ContentTitle\').html(Contents[CurrentContent].Title)" onclick="SwitchContent('+Content+', 0)" />';
			
			Contents[Content].Subcontents = new Array(false);
			
			$('#Content'+Contents[Content].ID+' .Subcontent').each(function(i) {
				
				Contents[Content].Subcontents[i] = this;
				
				if( i ) {
					
					SwitcherContent += '<img id="ContentSwitcher'+Contents[Content].ID+'_'+i+'"'+(CurrentSubcontent == i ? ' class="Selected"' : '')+' src="'+SwitcherImage+'" width="8" height="8" alt="'+Contents[Content].Title+'" onmouseover="$(this).addClass(\'Active\'); $(\'#ContentTitle\').html(\''+Contents[Content].Title+'\')" onmouseout="$(this).removeClass(\'Active\'); $(\'#ContentTitle\').html(Contents[CurrentContent].Title)" onclick="SwitchContent('+Content+', '+i+')" />';
					
					$(this).css({display: 'none'});
					
				}
				
			});
			
			if( CurrentContent == Content ) $('#ContentTitle').html(Contents[Content].Title);
			else $('#Content'+Contents[Content].ID).css({display: 'none'});
			
		}
		
		if( SwitchTime ) {
			
			$('.Head').mouseenter(function() {
			
				clearInterval(ContentInterval);
				
				ContentInterval = null;

			});
			
			$('.Head').mouseleave(function() {
				
				ContentInterval = setInterval('NextContent()', SwitchTime);
				
			});
			
		}
		
		$('#ContentSwitcher img').replaceWith(SwitcherContent);
		
	}
	
	$('ul.Menu li').each(function(i) {
		
		$(this).children('a').mouseenter(function() {
			
			$(this.parentNode).addClass('Active');
			
			if( document.getElementById('SubMenu'+this.parentNode.id) ) $('#SubMenu'+this.parentNode.id).stop(true, true).slideDown(300);
			
		});
		
		$(this).mouseleave(function() {
			
			$(this).removeClass('Active');
			
			if( document.getElementById('SubMenu'+this.id) ) $('#SubMenu'+this.id).slideUp(300);
			
		});
		
	});

});



function SwitchContent( c, s ) {
	
	if( !s ) s = 0;
	
	if( ( Contents.length < 2 && Contents[c].Subcontents.length < 2 ) || ( CurrentContent == c && CurrentSubcontent == s ) || ContentSwitching ) return false;
	
	ContentSwitching = true;
	
	$('#ContentSwitcher img').removeClass('Selected');
	
	$('#ContentSwitcher'+Contents[c].ID+'_'+s).addClass('Selected');
	
	if( CurrentContent != c ) {
		
		$(Contents[c].Subcontents).css({display: 'none'});
		
		$(Contents[c].Subcontents[s]).css({display: 'block'});
		
		$('#ContentTitle').html(Contents[c].Title);
		
		$('#Content'+Contents[CurrentContent].ID).fadeOut(500, function() {
			
			$('#Content'+Contents[c].ID).fadeIn(500, function() {
			
				ContentSwitching = false;
			
			});
			
		});
		
	}
	else {
		
		$('#ContentTitle').html(Contents[c].Title);
		
		$(Contents[c].Subcontents[CurrentSubcontent]).fadeOut(500, function() {
			
			$(Contents[c].Subcontents[s]).fadeIn(500, function() {
			
				ContentSwitching = false;
			
			});
		
		});
	
	}
	
	CurrentContent = c;
	CurrentSubcontent = s;
	
}

function PrevContent() {
	
	if( CurrentSubcontent == 0 ) SwitchContent(CurrentContent-1 < 0 ? Contents.length-1 : CurrentContent-1, 0);
	else SwitchContent(CurrentContent, CurrentSubcontent-1 );
	
}

function NextContent() {
	
	if( CurrentSubcontent == Contents[CurrentContent].Subcontents.length-1 ) SwitchContent(CurrentContent+1 < Contents.length ? CurrentContent+1 : 0, 0);
	else SwitchContent(CurrentContent, CurrentSubcontent+1 );
	
}

//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;
	
}