/* Don't modify this file. */

var MENU_ITEM_HOME = 1;
var MENU_ITEM_INFO = 2;

var indexGallery = -1;
var indexPhoto = -1;

function setParams() {
	
	indexGallery = gup('g');
	
	if (isNaN(indexGallery) || indexGallery > photoData.length - 1 || indexGallery < 0) {
		indexGallery = 0;
	}
	
	indexPhoto = gup('p');
	
	if (isNaN(indexPhoto) || indexPhoto > getPhotoCount() - 1 || indexPhoto < 0) {
		indexPhoto = 0;
	}
}

function writeMenu(menuItemOn) {
	
	var classTextHome = (menuItemOn == MENU_ITEM_HOME) ? 'class="lk_gallery_on" ' : '';
	var classTextInfo = (menuItemOn == MENU_ITEM_INFO) ? 'class="lk_gallery_on" ' : '';
	
	document.write( 
		'<a class="lk_title" href="index.html">Rob Cohen : Photography</a>' +
		'<br/><br/>' +
		'<a ' + classTextHome + 'href="index.html">home</a> | ' +
		'<a ' + classTextInfo + 'href="info.html">info</a>' +
		'<br/><br/>');

	gallerySize = photoData.length;
	
	for (var i = 0; i < gallerySize; i++) {
		
		var classText = (i == indexGallery) ? 'class="lk_gallery_on" ' : '';
		
		document.write('<a ' + classText + 'href="gallery.html?g=' + i + '&p=0">' + photoData[i][0] + '</a><br/>');
	}
	
	document.write( 
		'<br/><br/>' +
		'Contact: <a href="mailto:cohen.rl[decrypt]gmail.com">email</a>' +
		'<br/>' +
		'Copyright © 2009');
}

function writePrevNext() {
	
	indexImgPrev = getIndexPhotoDelta(-1);
	indexImgNext = getIndexPhotoDelta(+1);
	
	document.write(
		'<a href="gallery.html?g=' + indexGallery + '&p=' + indexImgPrev + '">previous</a> | ' + 
		'<a href="gallery.html?g=' + indexGallery + '&p=' + indexImgNext + '">next</a>');
}

function doLoad(doUpdateGallery) {
	
	if (doUpdateGallery) {
		
		updateGallery();
		updatePhoto();
	}
	
	decryptEmail();
}

function updateGallery() {
	
	document.getElementById("desc_gallery").innerHTML = getDescGallery();
}

function updatePhoto() {
	
	var descGallery = getDescGallery();
	var descPhoto = getDescPhoto();
	var photoCount = getPhotoCount();
	
	document.getElementById('photo').src = 'img/' + descGallery + '/' + (indexPhoto + 1) + '.jpg';
//	document.getElementById('photo').alt = descPhoto;
//  document.getElementById('photo').title = descPhoto;
    document.getElementById("desc_photo").innerHTML = descPhoto;
    document.getElementById("count_photo").innerHTML = "(" + (indexPhoto + 1) + " / " + photoCount + ")";
    
    var indexPhotoPreload1 = getIndexPhotoDelta(+1);
    var indexPhotoPreload2 = getIndexPhotoDelta(+2);
    var imgPreload1 = new Image();
    var imgPreload2 = new Image();
    
    imgPreload1.src = 'img/' + descGallery + '/' + (indexPhotoPreload1 + 1) + '.jpg';
    imgPreload2.src = 'img/' + descGallery + '/' + (indexPhotoPreload2 + 1) + '.jpg';
}

function getDescGallery() {
	
	return photoData[indexGallery][0];
}

function getDescPhoto() {
	
	return photoData[indexGallery][1][indexPhoto];
}

function getPhotoCount() {
	
	return photoData[indexGallery][1].length;
}

function getIndexPhotoDelta(delta) {
	
	photoCount = photoData[indexGallery][1].length;
	
	var indexPhotoDelta = indexPhoto + delta;
	
	indexPhotoDelta = indexPhotoDelta % photoCount;
	
	if (indexPhotoDelta < 0) {
		indexPhotoDelta = photoCount - 1;
	}
	
	return indexPhotoDelta;
}

function gup(name) {

	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var tmpURL = window.location.href;
	var results = regex.exec(tmpURL);

    if(results == null) {

        return "-1";

    } else {

        return parseInt(results[1]);
    }
}

function decryptEmail() {

	/***********************************************
	 * Easy Email Scrambler script- © Dynamic Drive (www.dynamicdrive.com)
	 * This notice MUST stay intact for legal use
	 * Visit http://www.dynamicdrive.com/ for full source code
	 ***********************************************/

	var maildivider="[decrypt]"; //enter divider you use to divide your email address strings

	for (var i=0; i<=(document.links.length-1); i++){
		if (document.links[i].href.indexOf(maildivider)!=-1)
			document.links[i].href=document.links[i].href.split(maildivider)[0]+"@"+document.links[i].href.split(maildivider)[1];
	}
}
