// JavaScript Document

//rollover function
function ro(obj, action)
{
	switch (action)
	{
		case 'over' : document.getElementById(obj).style.visibility = 'visible'; break;
		case 'out' : document.getElementById(obj).style.visibility = 'hidden'; break;
		default : document.getElementById(obj).style.visibility = 'hidden';
	}
}

//load page function
function loadPg (page, addVar)
{
	if (addVar != "")
		window.location = page + ".php?" + addVar;
	else
		window.location = page + ".php";
}

//reveal/hide divs function
function reveal (unhide, hide)
{
	if (hide != "null")
		document.getElementById(hide).style.visibility = 'hidden';
	if (unhide != "null")
		document.getElementById(unhide).style.visibility = 'visible';
}

// move div
function moveDiv (divName, distance)
{
	//alert ("DivName: "+ divName +"\nDivTop: "+ document.getElementById(divName).style.top +"\nDistance: "+ distance);
	document.getElementById(divName).style.top = ( ( parseInt(document.getElementById(divName).style.top) + distance ) + "px");
}

// show forms for edit mode
function showForm (formName, formFocus, divMove, startDistance, moveDistance)
{
	reveal(formName, "null");
	//alert ( formName +"\n"+ divMove +"\n"+ document.getElementById(divMove).style.top +"\n"+ moveDistance +"px" );
	if (divMove != "")
	{
		document.getElementById(divMove).style.top = ( ( startDistance + moveDistance ) + "px");
	}
	if (formFocus != "null")
		document.getElementById(formFocus).focus();
}

// reset form back
function resetForm (formName, divMove, moveDistance, formToReset, oldData)
{
	// hide form area
	reveal ("null", formName);
	// move back elements that were moved to reveal said form
	if (divMove != "") {
		document.getElementById(divMove).style.top = ( ( parseInt(document.getElementById(divMove).style.top) - moveDistance ) + "px");
	}
	// reset data in form
	if (document.getElementById(formToReset).value)
		document.getElementById(formToReset).value = oldData;
	else if (document.getElementById(formToReset).innerHTML)
		document.getElementById(formToReset).innerHTML = oldDate;
}

// swap style background to a rollover state
function swapBgImg (swapDiv, imgPath)
{
	document.getElementById(swapDiv).style.backgroundImage = "url('"+ imgPath +"')";
}

// swap div class
function swapDivClass (swapDiv, swapClass)
{
	document.getElementById(swapDiv).className = swapClass;
}

// swap top art based on dropdown selection
function swapTopArt (swapDiv, swapDrpDwn)
{
	// get value from dropdown
	var thumbUsed = document.getElementById(swapDrpDwn).value;
	//alert("SwapDiv: "+ swapDiv +"\nThumbnail: "+ thumbUsed);
	if (thumbUsed == 0 || thumbUsed == "")
	{
		document.getElementById(swapDiv).style.backgroundImage = "url('images/projects/project_temp.png')";
		document.getElementById(swapDiv).style.backgroundColor = "#222222";
	} else {
		// change background image
		document.getElementById(swapDiv).style.backgroundImage = "url('"+ thumbUsed +"')";
		var imgW = document.getElementById("top_art_size").style.width;
		var imgH = document.getElementById("top_art_size").style.height;
		if (parseInt(imgH) < 400 && (imgW < imgH) ) {
			document.getElementById(swapDiv).style.backgroundSize = document.getElementById("top_art_size").style.width;
		}
	}
}

// /////////////////////////////////////////////////////
//
// PREVIEW PAGE SCRIPTS
//
// /////////////////////////////////////////////////////
// swap large image on preview page
var totalImages = 1;
function swapLargeImage (newImage, newSelect)
{
	//alert ("New Img : "+ newImage +"\nNew Select : "+ newSelect +"\nSelected : "+ selectedThumb);
	var obj = document.getElementById("preview_imgSize_"+newSelect);
	var height = obj.style.height;
	var width = obj.style.width;
	var newLrgPerct = (1/(parseInt(height)/500));
	document.getElementById("preview_large_image").style.width = (Math.round(parseInt(width) * newLrgPerct)) + "px";
	if (document.getElementById("preview_large_hide") != null)
		document.getElementById("preview_large_hide").style.width = (Math.round(parseInt(width) * newLrgPerct)) + "px";
	document.getElementById("preview_large_image").style.backgroundImage = "url(ths.php?img=" + newImage + "&width=" + (Math.round(parseInt(width) * newLrgPerct)) + "&height=" + (Math.round(parseInt(height)*newLrgPerct)) + ")";
	if (newSelect != selectedThumb)
	{
		document.getElementById("preview_thumb_"+newSelect).className = "preview_box_selected brd_accent_clr";
		document.getElementById("preview_thumb_"+selectedThumb).className = "preview_box img_brd_clr";
		selectedThumb = newSelect;
	}
}
function swapLargeControl(imageFunction, imageArray)
{
	// get current Large Image
	// then remove excess data from url
	var oldBg = document.getElementById("preview_large_image").style.backgroundImage;
	// get full image path  -  /images/projects/1/1_01.jpg
	var current_img = oldBg.slice( (oldBg.lastIndexOf('?')+5), (oldBg.lastIndexOf('.')+4) );
	// get full image path  -  /images/projects/1/1
	var newImgDir = current_img.slice(0, (current_img.length - 7) );
	// get image extension  -  .jpg
	var newImgExt = current_img.slice( current_img.lastIndexOf('.') , (current_img.length) );
	// set up array passed in
	var image_arr = new Array();
	image_arr = imageArray.split(",");
	var totalCount = image_arr.length;
	// add/subtract 1 to the image number to display the next image
	if (imageFunction == "next") {
		// get current large image
		var oldImgNum = current_img.slice( (current_img.lastIndexOf('_')+1) , (current_img.length - 4) );
		// get current image number and find next image number
		for (i=0; i <= image_arr.length; i++)
		{
			if (oldImgNum == parseInt(image_arr[i]))
			{
				var nextNum = i+1;
				//alert ("next image = "+ image_arr[nextNum]);
				var newImgNum = image_arr[nextNum];
			}
		}
		// if next image is divisible 8 slide thumbnails to next page.
		if ((parseInt(newImgNum,10)%8)==0)
			slideThumbs(1, totalCount);
	} else {
		// get current large image
		var oldImgNum = current_img.slice ((current_img.lastIndexOf('_')+1), (current_img.length - 4));
		// get current image number and find previous image number
		for (i=0; i <= image_arr.length; i++)
		{
			if (oldImgNum == parseInt(image_arr[i], 10))
			{
				if (i >= 0)
				{
					var nextNum = i-1;
					//alert ("prev image = "+ image_arr[nextNum]);
					var newImgNum = image_arr[nextNum];
				} else {
					var newImgNum = "01";
				}
			}
		}
		// if prev image is divisible 7 slide thumbnails back to prev page.
		if ((parseInt(newImgNum,10)%7)==0)
			slideThumbs(-1, totalCount);
	}
	// if next/prev image is actually in the image array then act
	// else do nothing
	//alert ("New Img Num : "+ parseInt(newImgNum) +"\nLast Img Entry : "+ parseInt(image_arr[((image_arr.length)-1)],10) );
	if ((parseInt(newImgNum, 10) > 0) && (parseInt(newImgNum, 10) <= parseInt(image_arr[((image_arr.length)-1)]), 10) )
	{
		//alert ("Old Img : "+ oldBg +"\nCurrent Img : "+ current_img +"\nImage Dir : "+ newImgDir +"\nNew Img Num : "+ newImgNum +"\nNew Img Ext : "+ newImgExt);
		swapLargeImage ((newImgDir +"_"+ newImgNum + newImgExt), newImgNum );
	}
}

//slide thumbnail images
function moveThumbs (finalPos, direction) {
	var currentPos = parseInt(document.getElementById("thumbnails").style.left);
	var moveDist = -25 * direction;
	if (direction == 1) {
		if (currentPos > finalPos) {
			document.getElementById("thumbnails").style.left = ((currentPos+moveDist) +"px" );
			var move = setTimeout ("moveThumbs("+ finalPos +", "+ direction +")", 1);
		} else {
			document.getElementById("thumbnails").style.left = ((currentPos) +"px" );
			clearTimeout(move);
		}
	} else {
		if (currentPos < finalPos) {
			document.getElementById("thumbnails").style.left = ((currentPos+moveDist) +"px" );
			var move = setTimeout ("moveThumbs("+ finalPos +", "+ direction +")", 1);
		} else {
			document.getElementById("thumbnails").style.left = ((currentPos) +"px" );
			clearTimeout(move);
		}
	}
}
function slideThumbs(direction, totalCount)
{
	// set element var to thumbnail div
	var obj = document.getElementById("thumbnails");
	var thumb_pos = parseInt(obj.style.left);
	// get number of thumbnail pages
	var pages = Math.ceil(totalCount/7);
	var pageCompare = (-1000 * direction) * (pages-1);
	
	// check position and direction sent from user
	if ((thumb_pos == 0) && (direction == 1)) {
		// set final position to next/previous page 
		var final_pos = (-1000 * direction);
		// run function to animate the thumbnails to next/prev page
		moveThumbs(final_pos, direction);
	} else if ( (thumb_pos > pageCompare) || (thumb_pos < pageCompare) ) {
		if (thumb_pos != 0) {
			// set final position to next/previous page 
			var final_pos = (thumb_pos + (-1000 * direction));
			// run function to animate the thumbnails to next/prev page
			moveThumbs(final_pos, direction);
		}
	}
}

// /////////////////////////////////////////////////////
//
// TESTING SCRIPTS
//
// /////////////////////////////////////////////////////
function animateDown (obj, dir, dist)
{
	alert (document.getElementById("user_control").style.height);
}

