
// submit gallery form to handler.
function sendToHandler(mode) 
{
	// check that user has not entered more than 3 lines in current photo box
	if(document.getElementById('validSubmit').value == 'false')
	{
		alert('please reduce the Current Photo Text to 3 lines or less before making any other changes');
		document.getElementById('nbgl_galPhotoTextAreaStyle').focus();
	}
	else
	{
		switch (mode)
		{
			case 'updateMCE':
			tinyMCE.triggerSave("true","true");
			tinyMCE.execInstanceCommand("albumText", "mceCleanup");
			document.galleryForm.submit();
			break;
			
			case 'createAlbum':
			if(document.getElementById('selectImgButton').value == '')
			{
				alert('please browse for a photo and upload it');
			}
			else
			{
				tinyMCE.triggerSave("true","true");
				tinyMCE.execInstanceCommand("albumText", "mceCleanup");
				document.galleryForm.submit();
			}
			break;
			
			case 'addPhoto':
			if(document.getElementById('selectImgButton').value == '')
			{
				alert('please browse for a photo and upit');
			}
			else
			{
				document.galleryForm.submit();
			}
			break;
			
			default:
			document.galleryForm.submit();
			break;
		}
	}
}

// used for onchange event of 'arrange by' drop down for realtor mode.
// onchange event located at _gallerySort.php
function sortAlbums() {
	var list = document.forms[0].sortList;
	location.href = list.options[list.selectedIndex].value;
}

function showUploadButton(){
	document.getElementById('uploadImgButton').style.visibility = "visible";
	document.getElementById('uploadImgButton').style.width = "180px";
	document.getElementById('nbgl_galMessageLink').style.visibility = "hidden";
}

// used to check if any albums have been selected for deletion in _viewGallery.nb.php (which includes _gallerySort.php)
// if they have, go ahead and submit the form
function confirmAlbumDelete(formName,confirmQuestion)
{

	if(confirmQuestion == '1'){
		confirmQuestion = 'Are you sure you want to delete the selected albums? \n All images and data for the selected album(s) will be permanently deleted!'
	}
	
	for (n=0; n<formName.elements.length; n++)
	{
		if (formName.elements[n].type=="checkbox")
		{
			if(formName.elements[n].checked == 1)
			{
				var checked = true;
			}
		}

	}

	if (checked == true)
	{
		var x=window.confirm(confirmQuestion);
			
		if(x)
		{
			formName.deleteListing.value = 'true';
			formName.submit();
		}
	}
	else
	{
		alert('no albums were selected to delete');
	}

}

// submits form once user confirms they wish to replace current image with new one.
function confirmImageReplace(formName)
{
	var confirmQuestion = 'Are you sure you want to replace the current image with a new one?';
	
	var x=window.confirm(confirmQuestion);
			
		if(x)
		{
			formName.submit();
		}
	
}

// submits form once user has confirmed they wish to delete image. 
// inserts imageID into hidden field in the form so proper image is deleted.
function confirmImageDelete(formName,imageID,imageTitle)
{
	if(imageTitle == '')
	{
		confirmQuestion = 'You are about to permanently remove this image from your gallery. Proceed?';
	}
	else
	{
		confirmQuestion =  'You are about to permanently remove \'' + imageTitle + '\' from your gallery. Proceed?';
	}
	
	var x=window.confirm(confirmQuestion);
			
		if(x)
		{
			document.getElementById('imgToDelete').value = imageID;
			formName.submit();
		}	
}

// called when user is over the max amount of albums or images.  
// limitType - album or image, limitNumber - set in _galleryConfig.php
function overLimit(limitType,limitNumber)
{
	if(limitType == 'album')
	{
		var alertMessage = 'Sorry, no new albums may be created. The maximum amount of albums you may create is ' + limitNumber + '.';		
	}
	else
	{
		var alertMessage = 'Sorry, no new images may be added. The maximum amount of images you may create is ' + limitNumber + '.';	
	}
	
	alert(alertMessage);
}

// open up printint window
function printListing(path)
{
	 window.open (path,"mywindow","scrollbars=1,width=630,height=720");
}

// generic pop-up
function popUp(location,title,width,height)
{
	var attributes = '"' + 'status=0, width=' + width + ', height=' + height + ', resizable=1, scrollbars=1' + '"';
	var title = '\'' + title + '\'';
	
	
	window.open(location,"title",attributes);
}

function ajaxAlbumView(imageId, listingId, pathToImage, imgWidth, imgHeight, isSafari) {
	queryString = '?imageID=' + imageId + '&listingID=' + listingId + '&ajaxYes=true&ajaxMode=imageSwap';
	
	$.getJSON("_nbTools/_nbGallery/_system/_galleryAlbumViewAjax.php" + queryString,
		function(data) {
			$("#nbgl_currentPhoto").html(data.imgTitle);
			$("#nbgl_rightMiddleDiv").html('<span class="nbgl_promoColor">' + data.imgText.replace(/[\n]/g,"<br>") + '</span>');
			
			if (data.imgFilename == '') {
				$("#currentImage").attr('src', 'http://sst.netbiz.com/_nbSSTCentral/_masterSymLink/_nbTools/_nbGallery/_images/netBizLogo.jpg');
			} else {
				$("#currentImage").attr('src', pathToImage + data.imgFilename);
			}
			
			// safari keeps image size of previous image viewed in some cases, this is the fix
			if(isSafari == 'true') {
				$("#currentImage").attr('width', imgWidth);
				$("#currentImage").attr('height', imgHeight);
			}
	});
}


// lets user know if they have entered too much text into the current photo text box.
function lineCount(maxHeight)
{	
	var textArea = document.getElementById('nbgl_galPhotoTextAreaStyle').value;
	
	var textAreaMod = textArea.replace(/[\n]/g,"<br>");
	
	document.getElementById('nbgl_measureInput').innerHTML = textAreaMod
	
	var currentHeight = document.getElementById('nbgl_measureInput').clientHeight;

	if(currentHeight > maxHeight)
	{
		document.getElementById('overLimitMsg').innerHTML = '<span style="color: orange;">&nbsp;- you have entered too much text, please reduce it.</span>';
		document.getElementById('validSubmit').value = 'false';
	}
	else
	{
		document.getElementById('overLimitMsg').innerHTML = '<span style="color: #ccc;">&nbsp;- text is limited to about three lines</span>';
		document.getElementById('validSubmit').value = 'true';
	}
}

// if the main text area extends below it's set height, the thumb scrolling div grows with it
function stretchDiv()
{
	var textAreaHeight = document.getElementById('nbgl_rightBottomMainCell').clientHeight;
	
	if(textAreaHeight > 72)
	{
		document.getElementById('nbgl_leftColDiv').style.height = textAreaHeight + 420;
		document.getElementById('nbgl_leftColBottomDivBorders').style.top = textAreaHeight + 420;
	}
	
}

function imageResize(value)
{	
	if(value == 'true')
	{
		document.getElementById('imageResize').value = 'true';
		imageResizeForm.submit();
	}
	else
	{
		document.getElementById('imageResize').value = 'false';
		imageResizeForm.submit();
	}
}

// once update has been made, display alert informing user what has been done, and close window.
function imageResizePopAlert()
{
	var updateValue = document.getElementById('imageResizeUpdated').value;
	
	switch (updateValue)
	{
		case 'original':
			alert('the image will now display at it\'s original size');
			window.close();
		break;
		
		case 'maximum':
			alert('the image will now display at it\'s maximum size');
			window.close();
		break;
	}
}
