/*
	AJAX PHOTO GALLERY
	download at: http://acidmartin.wemakesites.net/?pageId=AjaxImageGallery
	created by: Martin Ivanov
	email: acid_martin@yahoo.com, martin@acidmartin.com
	personal website: http://acidmartin.wemakesites.net/
	portfolio: http://wemakesites.net
	
	IMPORTANT REGULATIONS:
	
	1. IF YOU ARE USING THE OBFUSCATED VERSION OF AJAX Photo Gallery, IT IS FORBIDDEN TO DEOBFUSCATE AND MODIFY THIS SRCIPT WITHOUT AUTHOR'S WRITTEN PERMISSION. OTHERWISE SUCH ACTIONS WILL BE CONSIDERED VIOLATIONS OF THE COPYRIGHT LAWS AND LEGAL STEPS WILL BE TAKEN.
	
	2. IF YOU HAVE PURCHASED THE SOURCE CODE OF AJAX Photo Gallery, YOU CANNOT USE THIS SCRIPT ON WEBPAGES WITHOUT
	OBFUSCATING IT. OTHERWISE SUCH ACTIONS WILL BE CONSIDERED VIOLATIONS OF THE COPYRIGHT LAWS AND LEGAL STEPS WILL BE TAKEN.
*/

var applicationVersion = '1.5.0';
var freeVersion = false;

enlargeThumbnail = function(thumbnailId)
{
	var clickMessage = '(Press ESC Key or Click on the Image to Close)';
	$('image').src = $(thumbnailId).src;
	$('titlebar').innerHTML = $(thumbnailId).alt;
	$('image').alt = $(thumbnailId).alt + ' ' + clickMessage;
	$('image').title = $(thumbnailId).alt + ' ' + clickMessage;
	$('imageviewer').style.marginTop = '-' + $(thumbnailId).offsetHeight / 2 + 'px';
	$('titlebarelement').style.width = $(thumbnailId).offsetWidth + 'px';
	if(photofeed.getElementsByTagName('config')[0].getAttribute('displaytitlebar') == 'false')
	{
		$('titlebarelement').style.display = 'none';
	}
	if(photofeed.getElementsByTagName('config')[0].getAttribute('modalmode') == 'false')
	{
		$('modalelement').style.display = 'none';
	}
	else
	{
		$('modalelement').style.display = 'block';
		$('modalelement').style.height = document.documentElement.scrollHeight + 'px';
		$('modalelement').style.width = document.documentElement.offsetWidth + 'px';
	}
	$('imageviewer').style.display = 'block';
	$('imageviewer').style.marginLeft = '-' + $('image').offsetWidth / 2 + 'px';
	$('imageviewer').style.marginTop = '-' + $('image').offsetHeight / 2 + 'px';
	$('titlebarelement').style.width = $('image').offsetWidth + 'px';
}

closeViewer = function()
{
	$('imageviewer').style.display = 'none';
	$('modalelement').style.display = 'none';
}

function createAlbum()
{
	var viewercss = document.createElement('link');
	viewercss.type = 'text/css';
	viewercss.rel = 'stylesheet';
	viewercss.href = scriptsDirectory + 'AjaxPhotoGallery/Scripts/AjaxPhotoGallery.css';
	document.getElementsByTagName('head')[0].appendChild(viewercss);
	
	document.getElementsByTagName('body')[0].innerHTML += viewerloader;
	if(window.ActiveXObject)
	{
		photofeed = new ActiveXObject("Microsoft.XMLDOM");
		photofeed.async = false;
		photofeed.load(albumFeedFile);
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		photofeed = document.implementation.createDocument("","",null);
		photofeed.async = false;
		photofeed.load(albumFeedFile);
	}
	
	var viewerskin = document.createElement('link');
	viewerskin.type = 'text/css';
	viewerskin.rel = 'stylesheet';
	viewerskin.href = scriptsDirectory + 'AjaxPhotoGallery/Skins/' + photofeed.getElementsByTagName('config')[0].getAttribute('skin') + '.css';
	document.getElementsByTagName('head')[0].appendChild(viewerskin);
	
	var x = photofeed.getElementsByTagName('photo');
	var visible;
	var isbreak;
	var allowenlarge;
	var allowenlargecursor;
	for (i = 0; i < x.length; i++)
	{
		if(photofeed.getElementsByTagName('config')[0].getAttribute('enabled') == "true")
		{
			if(x[i].getAttribute('visible') == "false")
			{
				visible = 'display: none';
			}
			else
			{
				visible = 'display: block';
			}
			if(x[i].getAttribute('isbreak') == "true")
			{
				isbreak = '<br style="clear: both;" />\n';
			}
			else
			{
				isbreak = '';
			}
			
			if(photofeed.getElementsByTagName('config')[0].getAttribute('displayclosebutton') == "false")
			{
				$('closebutton').style.display = 'none';
			}
			else
			{
				$('closebutton').style.display = 'block';
			}
			if(photofeed.getElementsByTagName('config')[0].getAttribute('displaytitlebartext') == "false")
			{
				$('titlebar').style.display = 'none';
			}
			else
			{
				$('titlebar').style.display = 'block';
			}
			if(photofeed.getElementsByTagName('config')[0].getAttribute('displaythumbcaption') == "false")
			{
				$(albumLoaderId).className += ' nocaption';
			}
			else
			{
				$(albumLoaderId).className += ' showcaption';
			}
			if(x[i].getAttribute('allowenlarge') == "false")
			{
				allowenlarge = 'alert(\'AJAX Photo Gallery Info:\\n\\nThis thumbnail cannot be enlarged.\\nIf you are the owner of the gallery, please, check the ALLOWENLARGE property value\\nof the current image in MyPhotoCollection.xml.\\nIt should be set to TRUE in order to allow fullsize image view.\')';
				allowenlargecursor = 'cursor: no-drop;';
			}
			else
			{
				allowenlarge = 'enlargeThumbnail(this.id);window.open(\'#\', \'_self\', \'\');';
				allowenlargecursor = '';
			}
			if(photofeed.getElementsByTagName('config')[0].getAttribute('thumbcaptionposition') == "top" && photofeed.getElementsByTagName('config')[0].getAttribute('displaythumbcaption') == "true")
			{
				$(albumLoaderId).className += ' showcaption topposition';
			}
			if(photofeed.getElementsByTagName('config')[0].getAttribute('thumbcaptionposition') == "bottom" && photofeed.getElementsByTagName('config')[0].getAttribute('displaythumbcaption') == "true")
			{
				$(albumLoaderId).className += ' showcaption bottomposition';
			}
			$(albumLoaderId).innerHTML += '<div class="thumbnail" unselectable="on" id="AjaxPhotoGallery_Photo_' + x[i].getAttribute('imagename') + '" style="' + visible + '; width: ' + photofeed.getElementsByTagName('config')[0].getAttribute('thumbnailwidth') + 'px; height: ' + photofeed.getElementsByTagName('config')[0].getAttribute('thumbnailheight') + 'px; ' + allowenlargecursor + '">\n\t<img unselectable="on" galleryimg="no" src="' + photofeed.getElementsByTagName('config')[0].getAttribute('imagesfolder') + '/' + x[i].getAttribute('imagename') + '" style="width: 100%;" alt="' + x[i].getAttribute('description') + '" title="' + x[i].getAttribute('description') + '" id="' + x[i].getAttribute('imagename') + '" onclick="' + allowenlarge + '" />\n<div id="caption" unselectable="on" style="width: '+ photofeed.getElementsByTagName('config')[0].getAttribute('thumbnailwidth') +';">&nbsp;' + x[i].getAttribute('description') + '</div></div>\n' + isbreak;
		}
		if(photofeed.getElementsByTagName('config')[0].getAttribute('enabled') == "false")
		{
			$(albumLoaderId).style.display = 'none';
		}
	}
	nocreditline = 'AJAX Photo Gallery ' + applicationVersion + ' Info:\n\nCannot create gallery!\n\nYou are using the free version of this software that displays a credit line to my website.\nScript engine was unable to locate the credit line. It is either hidden or removed.\n\nThe free version cannot run without this credit line.';
	$(albumLoaderId).innerHTML += '<div id="creditline">Created with the free version of <a href="http://acidmartin.wemakesites.net/?pageId=AjaxImageGallery" title="AJAX Photo Gallery ' + applicationVersion + '" target="_blank">AJAX Photo Gallery ' + applicationVersion + '</a></div><div id="additionalinfo">Click  <a href="javascript: galleryinfo();" title="View Additional Info About this Album">Here</a> for Additional Info About this Album</div>';
	if(photofeed.getElementsByTagName('config')[0].getAttribute('displayalbuminfo') == "false")
	{
		$('additionalinfo').style.display = 'none';
	}
	else
	{
		$('additionalinfo').style.display = 'block';
	}
	if(document.all && $('creditline').currentStyle.display == "none")
	{
		alert(nocreditline);
		window.open("http://acidmartin.wemakesites.net/?pageId=AjaxImageGallery", "_self", "");
	}
	if(document.all && $('creditline').currentStyle.visibility == "hidden")
	{
		alert(nocreditline);
		window.open("http://acidmartin.wemakesites.net/?pageId=AjaxImageGallery", "_self", "");
	}
	if(freeVersion == false)
	{
		$('creditline').style.display = 'none';
	}
}

var viewerloader = [
	'<div id="modalelement"><!-- --></div>' +
	'<div id="imageviewer" unselectable="on">' +
		'<div id="titlebarelement">' +
			'<span id="titlebar" unselectable="on"></span>' +
			'<a href="javascript:closeViewer();" title="Close" id="closebutton" unselectable="on">X</a>' +
		'</div>' +
		'<img galleryimg="no" src="AjaxPhotoGallery/Scripts/DumpImage.gif" id="image" alt="" onclick="closeViewer();" unselectable="on" />' +
	'</div>'
]

galleryinfo = function()
{
	if(confirm('AJAX Photo Gallery ' + applicationVersion + ' Album Info\n\nAlbum Name: ' + photofeed.getElementsByTagName('config')[0].getAttribute('albumname') + ' (album feed file - ' + albumFeedFile + ')\nAlbum Owner: ' + photofeed.getElementsByTagName('config')[0].getAttribute('albumowner') + '\t\t\nCreated: ' + photofeed.getElementsByTagName('config')[0].getAttribute('datecreated') + '\nLast Updated: ' + photofeed.getElementsByTagName('config')[0].getAttribute('dateupdated') + '\n\nWould you like to check the website of AJAX Photo Gallery ' + applicationVersion + ' for updates and additional skins?'))
	{
		window.open("http://acidmartin.wemakesites.net/?pageId=AjaxImageGallery", "_blank", "");
	}
}

function $(stringId)
{
	return document.getElementById(stringId);
}

function pressEscKeyToClose(e)
{
	var kC = (window.event) ? event.keyCode : e.keyCode;
	var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE
	if(kC == Esc)
	{
		if($('imageviewer') != null)
		{
			closeViewer();
		}
	}
}

if(window.attachEvent)
{
	document.documentElement.attachEvent('onkeydown', 
		function(e)
		{
			pressEscKeyToClose(e);
		}
	);
}
else
{
	document.documentElement.addEventListener('keypress', 
		function(e)
		{
			pressEscKeyToClose(e);
		}, true
	);
}

function pageLoad()
{
}

if(window.attachEvent)
{
	window.attachEvent('onload', function(e)
		{
			pageLoad();
		}
	);
}
else
{
	window.addEventListener('load', function(e)
		{
			pageLoad();		
		}, true	
	);
}