/* gallerySlide.js */

var SSfadeInTime = 500;
var SSfadeOutTime = 500;
var SSgrowTime = 250;
var SSchangingImage = false;
var SSdisplayState = 0;
var SSloadingImage = false;

var tempTarget = null;
var tempWidth = null;
var tempHeight = null;
var tempSrc = null;

var galleryBaseNames = Array();
var galleryImages = Array();
var galleryThumbs = Array();
var galleryWidths = Array();
var galleryHeights = Array();
var galleryQuotes = Array();
var lastHash = "";
var curInd = -1;
var curThumbGroup = 0;
var totalGroups = 0;

window.onresize = function() {
	placePrevNext();
}

function prep_SSheight(target,src,width,height,quote) {
	
	/* upscale images 10% 
	width = width * 1.1;
	height = height * 1.1;*/
	
	opacity(target, 100, 0, SSfadeInTime);
	
	tempTarget = target;
	tempSrc = src;
	tempWidth = width;
	tempHeight = height;
	
	var browserSize = getBrowserSize();
	var docH = browserSize.height;
	var scrollXY = getScrollXY();
	var galXY = getAbsolutePosition(document.getElementById('gallery_holder'));
	var galTop = galXY.y;
	
	var maxW = 1000;
	var availH = Math.round(100 + ((docH - galTop) * 0.18));
	var maxH = docH - (galTop + availH);
	var minH = 300;
	
	if (width > maxW) {
		tempHeight = (maxW / width) * height;
		tempWidth = maxW;
	} else if (height > maxH) {
		tempHeight = maxH;
		tempWidth = (width / height) * maxH;
	} else {
		tempHeight = height;
		tempWidth = width;
	}
	
	//if (tempHeight < minH && (tempWidth / tempHeight) < 3) {
	if (tempHeight < minH) {
		tempHeight = minH;
		tempWidth = (width / height) * minH;
	}
	
	setTimeout("setImage()",SSfadeInTime);
	
	SSdisplayState = 1;
	
	document.getElementById('preload_image').src = "/images/blank.gif";
	document.getElementById('preload_image').src = tempSrc;
	setTimeout("checkPreloadWidth("+width+","+height+")",200);
	
	SSloadingImage = true;
	setTimeout("showLoadingImage()",SSfadeInTime);
	
	quote = getQuote(quote,tempSrc);
	
	WriteByID('imgQuote',null,"&nbsp;"+quote+"&nbsp;");
}

function getQuote(quote,file) {
	if (trim(quote) == "") {
		file = file.split("/");
		file = file[file.length-1];
		file = file.replace(".jpg","");
		while (file.search(/_/) != -1) {
			file = file.replace(/_/," ");
		}
		file = file.replace(/\d+/,"");
		file = trim(file);
		return file;
	} else {
		return quote;
	}
}

function showLoadingImage() {
	if (SSloadingImage == true) {
		/* set loading image */
		document.getElementById(tempTarget).src = "/images/blank.gif";
		document.getElementById('mainImgDiv').style.backgroundImage = "url(/images/loading.gif)";
		document.getElementById('mainImgDiv').style.backgroundPosition = "center";
		document.getElementById('mainImgDiv').style.backgroundRepeat = "no-repeat";
	}
}

function checkPreloadWidth(width,height) {
	if (document.getElementById('preload_image').width == width) {
		SSloadingImage = false;
		display_SSimg();
	} else {
		setTimeout("checkPreloadWidth("+width+")",200);
	}
}

function setImage() {	
	var holder = tempTarget+"Div";
	var ht = 20;
	if (document.getElementById(holder).style.height) {
		ht = document.getElementById(holder).style.height.replace("px","");
	}
	growY(holder, ht, tempHeight, SSgrowTime);
	SSdisplayState = 2;
	setTimeout("completePrep()",SSgrowTime);
}

function completePrep() {
	SSdisplayState = 3;
	display_SSimg();
}

function display_SSimg() {
	if (SSdisplayState == 3) {
		
		changeOpac(0, tempTarget);
		document.getElementById(tempTarget).src = tempSrc;
		document.getElementById(tempTarget).height = tempHeight;
		document.getElementById(tempTarget).width = tempWidth;
		
		placePrevNext();
		
		document.getElementById('mainImgDiv').style.backgroundImage = "url(/images/blank.gif)";
		setTimeout("opacity('"+tempTarget+"', 0, 100, "+SSfadeInTime+")",50);
		SSdisplayState = 0;
		
		analize();
		
	} else if (SSdisplayState > 0) {
		setTimeout("display_SSimg()",150);
	}
}

function analize() {
	try {
		var pageTracker = _gat._getTracker("UA-3685083-11");
		pageTracker._setDomainName(".jordanmatter.com");
		pageTracker._trackPageview();
	} catch(err) {}
}

function placePrevNext() {
	if (document.getElementById('gallery_mask').style.visibility != "hidden") {
		var mainImg = document.getElementById('mainImg');
		var iPos = getAbsolutePosition(mainImg);
		var prevImg = document.getElementById('prevImg');
		var nextImg = document.getElementById('nextImg');
		prevImg.style.height = mainImg.height+"px";
		prevImg.style.width = 100+"px";
		prevImg.style.top = iPos.y+"px";
		prevImg.style.left = iPos.x+"px";
		prevImg.style.visibility = 'visible';
		nextImg.style.height = mainImg.height+"px";
		nextImg.style.width = 100+"px";
		nextImg.style.top = iPos.y+"px";
		nextImg.style.left = (iPos.x + mainImg.width - 100)+"px";
		nextImg.style.visibility = 'visible';
	}
}

function buildGallery(imgData) {
	
	if (parseGalleryImages(imgData)) {
		
		thumbnails = "";
		if (galleryImages.length > 0) {
			thumbnails = '<table width="100%" height="50" cellpadding="0" cellspacing="0" border="0"><tr>';
			for (i=0; i<galleryImages.length; i++) {
				if (location.href.indexOf("#") !== -1) {
					loc = location.href.substr(location.href.indexOf("#"));
				} else {
					loc = window.location;
				}
				if (galleryBaseNames[i] != "undefined" && galleryBaseNames[i] != "") {
					thumbnails += "\n\t"+'<td align="center" width="60"><a href="'+'#'+galleryBaseNames[i]+'"><img src="'+galleryThumbs[i]+'" width="50" height="50" border="0" style="margin:0 5px 0 5px; cursor:pointer" /></a></td>';
				}
			}
			
			maxImages = (totalGroups * 10);
			if (maxImages > i) {
				for (j=0; j<maxImages; j++) {
					thumbnails += "\n\t"+'<td align="center" width="60"><img src="/images/blank.gif" width="50" height="50" border="0" style="margin:0 5px 0 5px;" /></td>';
				}
			}
			
			thumbnails += '</tr></table>';
		}
		
		WriteByID('thumbnail_holder',null,thumbnails);
		
		if (location.href.indexOf("#") !== -1 && location.hash.length > 4) {
			curInd = getImageInd(location.hash.replace("#",""));	
			curThumbGroup = Math.floor(curInd / 10);		
			buildGroupNodes();
		}
		
	}
}
function parseGalleryImages(imgData) {

	if (trim(imgData) != "") {
		lines = trim(imgData).split("\n");

		for (i=0; i<lines.length; i++) {
			imgInfo = lines[i].split("|-|");
			galleryBaseNames[i] = imgInfo[0].substring(imgInfo[0].lastIndexOf("/")+1);
			galleryImages[i] = imgInfo[0];
			galleryThumbs[i] = imgInfo[1];
			galleryWidths[i] = imgInfo[2];
			galleryHeights[i] = imgInfo[3];
			galleryQuotes[i] = imgInfo[4];
		}
		
		totalGroups = Math.ceil(galleryImages.length / 10);
		
		if (location.hash.length > 4) {
			curInd = getImageInd(location.hash.replace("#",""));
		} else {
			curInd = 0;
			if (location.href.indexOf("#") !== -1 && location.hash.length > 4) {
				if (trim(galleryBaseNames[0]) != '') {
					location.hash = galleryBaseNames[0];
				} else {
					location.hash = " ";
				}
			} else {
				location.hash = " ";
			}
			lastHash = location.hash;
		}
		
		buildGroupNodes();
		
		if (location.href.indexOf("#") !== -1) {
			loadImage(curInd);
		}
		
		return true;
	}
}
function buildGroupNodes() {
	
	var newML = 0;
	var nodesL = '';
	var nodesR = '';
	for (i=0; i<totalGroups; i++) {
		if (i < curThumbGroup) {
			nodesL += ' <a href="javascript:thumbPosition('+(600*i)+');" id="groupNode_'+i+'" class="nodeSelected noborder" ><img src="/images/arrow-leftB-12.png" border="0" style="margin:19px 0 0 -5px;" /></a> ';
		} else if (i > curThumbGroup) {
			nodesR += ' <a href="javascript:thumbPosition('+(600*i)+');" id="groupNode_'+i+'" class="nodeUnselected noborder" ><img src="/images/arrow-rightB-12.png" border="0" style="margin:19px -5px 0 0;" /></a> ';
		}
	}
	newML = curThumbGroup*600;
	WriteByID("group_nodes_left",null,nodesL);
	WriteByID("group_nodes_right",null,nodesR);
	var ML = (newML == 0) ? newML+"px" : "-"+newML+"px";
	$('#thumbnail_holder').animate({ marginLeft: ML }, 500);
	curThumbGroup = parseInt(newML / 600);
	//setCursor();
}

function thumbPosition(pos) {
	if (Math.abs(parseInt(document.getElementById("thumbnail_holder").style.marginLeft.replace("px",""))) != pos && pos % 600 == 0) {
		var ML = (pos == 0) ? pos+"px" : "-"+pos+"px";
		$('#thumbnail_holder').animate({ marginLeft: ML }, 500);
		curThumbGroup = parseInt(pos / 600);
		buildGroupNodes();
	}
}

function thumbLeft() {
	if (curThumbGroup > 0) {
		curX = parseInt(document.getElementById("thumbnail_holder").style.marginLeft.replace("px",""));
		newX = curX + 600;
		var ML = newX+"px";
		$('#thumbnail_holder').animate({ marginLeft: ML }, 500);
		curThumbGroup--;
	}
	buildGroupNodes();
}
function thumbRight() {
	if (curThumbGroup < totalGroups-1) {
		curX = parseInt(document.getElementById("thumbnail_holder").style.marginLeft.replace("px",""));
		newX = curX - 600;
		var ML = newX+"px";
		$('#thumbnail_holder').animate({ marginLeft: ML }, 500);
		curThumbGroup++;
	}
	buildGroupNodes();
}

function getImageInd(img) {	
	if (img == "") {
		return 0;
	} else {
		for (i=0; i<galleryBaseNames.length; i++) {
			if (img == galleryBaseNames[i]) {
				imgInd = i;
			}
		}
		return imgInd;
	}
}

function loadImage(ind) {
	prep_SSheight("mainImg",galleryImages[ind],galleryWidths[ind],galleryHeights[ind],galleryQuotes[ind]);
}

function showNextImg() {
	if (curInd < galleryImages.length-1) {
		curInd++;
	} else {
		curInd = 0;
	}
	showImage();
}
function showPrevImg() {
	if (curInd > 0) {
		curInd--;
	} else {
		curInd = galleryImages.length-1;
	}
	showImage();
}
function showImage() {
	if (trim(galleryBaseNames[curInd]) != "") {
		location.hash = galleryBaseNames[curInd];
	} else {
		location.hash = " ";
	}
	lastHash = location.hash;
	loadImage(curInd);
	
	gTitle = baseTitle+" | ";
	if (trim(galleryQuotes[curInd]) != '') {
		gTitle += galleryQuotes[curInd]+" | ";
	}
	gTitle += "Jordan Matter Photography";
	document.title = gTitle;
	
	groupX = Math.floor(curInd/10) * 600;
	
	var ML = "-"+groupX+"px";
	$('#thumbnail_holder').animate({ marginLeft: ML }, 500);
	curThumbGroup = parseInt(groupX / 600);
	buildGroupNodes();
}


function iniGallery() {
	target = "/getGalleryImagesB.php";
	params = "folder="+galleryFolder;
	xmlhttpCall(params, target, buildGallery);
}

function pollHash() {

	if (lastHash !== location.hash && location.href.indexOf("#") !== -1) {
		
		if (document.getElementById('gallery_mask').style.visibility == "hidden") {
			showGallery();
		} else {
			lastHash = location.hash;
			curInd = getImageInd(lastHash.replace("#",""));	
			loadImage(curInd);
			
			curThumbGroup = Math.floor(curInd / 10);
			newML = 600 * curThumbGroup;
			thumbPosition(newML);
		}
		
		gTitle = baseTitle+" | ";
		if (trim(galleryQuotes[curInd]) != '') {
			gTitle += galleryQuotes[curInd]+" | ";
		}
		gTitle += "Jordan Matter Photography";
		document.title = gTitle;
			
    }
}

function bootGallery() {
	var urlVars = getUrlVars();
	if (galleryFolder != '') {
		lastHash = location.hash;
		
		if (document.getElementById('mainImg').src.search(/\/images\/loading.gif/) != -1) {	
			var preloadIni = new Image();
			preloadIni.src = "/images/galleries/"+galleryFolder+"/"+lastHash.replace("#","");
			setInterval("pollHash()", 100);
			iniGallery();
			if (location.href.indexOf("#") !== -1 && location.hash.length > 4) {
				showGallery();
			}
		} else if (urlVars[0] != undefined && urlVars[0] != '') {
			setInterval("pollHash()", 100);
			location.hash = urlVars[0];
			iniGallery();
			showGallery();
		} else {
			setInterval("pollHash()", 100);
			iniGallery();
		}
	}
}

function getUrlVars() {
	
	var vars = [], hash;
    
	if (window.location.href.search(/\/v\//) != -1) {
		var hashes = window.location.href.slice(window.location.href.indexOf('\/v\/') + 3).split('#');
		for(var i = 0; i < hashes.length; i++) {
			vars[i] = hashes[i].replace(".php",".jpg");
		}
	} else if (window.location.href.search(/#/) != -1) {
		var hashes = window.location.href.split('#');
		vars[0] = hashes[1].replace(".php",".jpg");
	} else {
		vars[0] = '';
	}
	
    return vars;
}

function showGallery() {
	mask = document.getElementById('gallery_mask');
	if (mask.style.visibility == "hidden") {
		changeOpac(0, 'gallery_mask')	
		mask.style.width = "100%";
		mask.style.height = "100%";
		mask.style.visibility = "visible";
		opacity('gallery_mask', 0, 90, 600);
	}
	outer = document.getElementById('gallery_outer');
	if (outer.style.visibility == "hidden") {
		changeOpac(0, 'gallery_outer')	
		outer.style.width = "100%";
		outer.style.height = "100%";
		outer.style.visibility = "visible";
		opacity('gallery_outer', 0, 100, 600);
	}
	document.getElementById('prevImg').style.visibility = "visible";
	document.getElementById('nextImg').style.visibility = "visible";
}

function hideGallery() {
	mask = document.getElementById('gallery_mask');
	if (mask.style.visibility == "visible") {
		opacity('gallery_mask', 90, 0, 600);
		setTimeout("closeGallery()",700);
	}
	outer = document.getElementById('gallery_outer');
	if (outer.style.visibility == "visible") {
		opacity('gallery_outer', 100, 0, 600);
	}
	location.hash = " ";
	document.getElementById('prevImg').style.visibility = "hidden";
	document.getElementById('nextImg').style.visibility = "hidden";
}
function closeGallery() {
	mask = document.getElementById('gallery_mask');
	mask.style.width = 0+"px";
	mask.style.height = 0+"px";
	mask.style.visibility = "hidden";
	changeOpac(0, 'gallery_mask');	
	outer = document.getElementById('gallery_outer');
	outer.style.width = 0+"px";
	outer.style.height = 0+"px";
	outer.style.visibility = "hidden";
	changeOpac(0, 'gallery_outer');	
}

function getFlashMovieObject(movieName) {
  if (window.document[movieName]) {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  } else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    return document.getElementById(movieName);
  }
}
function setCursor() {
	var as = document.getElementById('thumbnail_div').getElementsByTagName('a'); 
	for (i=0; i<as.length; i++) {
		as[i].onmouseover = function() {
			var flashMovie=getFlashMovieObject("navHolder");
		    flashMovie.setHand();
		}
		as[i].onmouseout = function() {
			var flashMovie=getFlashMovieObject("navHolder");
		    flashMovie.unsetHand();
		}
	}
}
