function ToggleDiv(strDivId) {
	if($('#' + strDivId).is(':visible')) {
		$('#' + strDivId).slideUp('fast');
	} else {
		$('#' + strDivId).slideDown('fast');
	}
}

function ToggleDivNoAnimation(strDivId) {
	if($('#' + strDivId).is(':visible')) {
		document.getElementById(strDivId).style.display = "none";
	} else {
		document.getElementById(strDivId).style.display = "block";
	}
}

function IsArray(obj) {
	if(obj.constructor.toString().indexOf("Array") == -1) {
		return false;
	} else {
		return true;
	}
}

function ValidateForm(rCheckbox) {
	switch(typeof(rCheckbox)) {
		case "undefined":
			if(confirm("Are you sure you want to perform this action?")) {
				return true;
			} else {
				return false;
			}

			break;

		case "object":
			if(IsArray(rCheckbox)) {
				var bMustConfirm = false;
				for(var i = 0; i < rCheckbox.length; i++) {
					if(rCheckbox[i].checked) { bMustConfirm = true; break; }
				}

				if(bMustConfirm) {
					if(confirm("Are you sure you want to perform this action?")) {
						return true;
					} else {
						return false;
					}
				}
			} else {
				if(rCheckbox.checked) {
					if(confirm("Are you sure you want to perform this action?")) {
						return true;
					} else {
						return false;
					}
				}
			}

			break;
	}

	return true;
}

function SetFileUploadText(strFieldId) {
	var rField = document.getElementById(strFieldId);

	rField.value = "File selected";

	return;
}

//	Gallery
//--------------------------
function SetGalleryHeight(nHeight) {
	$('#gallery-main').animate({height: nHeight}, 400);
}

function SlideThumbs(nNewIndex) {
	var arrThumbs		= $('#gallery-thumbnails img');
	var nTotalWidth		= 0;
	var nSelectedIndex	= arrThumbs.length - 1;
	var nWidthFromEdge	= 0;

	for(var i = 0; i < arrThumbs.length; i++) {
		nTotalWidth += 130;
		if($(arrThumbs[i]).hasClass('selected')) {
			nSelectedIndex	= i;
			nWidthFromEdge	= nTotalWidth;
		}
	}

	if(nNewIndex > nSelectedIndex) {
		nFirst	= nSelectedIndex;
		nSecond	= nNewIndex;
	} else {
		nFirst	= nNewIndex;
		nSecond = nSelectedIndex;
	}

	nDifferance = nSecond - nFirst;
	nPadding	= 0;

	for(i = 0; i < nDifferance; i++) {
		nPadding += 130;
	}

	$('#gallery-thumbnails-inner').animate({'left': (nNewIndex > nSelectedIndex ? '+=' : '-=') + nPadding + 'px'}, 400);
}

function ToggleShowGalleryComments(strDivId) {
	if($('#' + strDivId).is(':visible')) {
		$('#gallery-comments').animate({'height': '55px'}, 400, function() { $('#' + strDivId).css('display', 'none'); });
	} else {
		$('#' + strDivId).css('display', 'block');
		$('#gallery-comments').animate({'height': '+=' + $('#' + strDivId).height() + 'px'}, 400);
	}
}

function ChangeGalleryImage(nId) {
	var arrCommentDivs	= $('#gallery-comments div');
	var bSetTimeout		= false;
	var strDiv			= "";
	for(var i = 0; i < arrCommentDivs.length; ++i) {
		var strDiv = 'gallery' + (i + 1) + '-comments';
		if($('#' + strDiv).is(':visible')) {
			ToggleShowGalleryComments(strDiv);
			bSetTimeout = true;
			break;
		}
	}

	if(bSetTimeout) setTimeout('ToggleShowGalleryComments(\'gallery' + nId + '-comments\')', 500);

	SetGalleryHeight($('#main_' + nId).height());

	$('#gallery-main img').animate({opacity: 0}, 400);
	$('#main_' + nId).stop().animate({opacity: 1}, 400);

	$('#gallery-thumbnails img').removeClass('selected');
	$('#thumb_' + nId).addClass('selected');

	$('#comment-link-container a').css('display', 'none');
	$('#comment-link-container a#comment-link_' + nId).css('display', 'block');

	var arrImages	= $('#gallery-main img');
	var nNewIndex	= 0;

	for(i = 0; i < arrImages.length; i++) {
		if($(arrImages[i]).css('opacity') == 1) nNewIndex = i;
	}

	setTimeout(SlideThumbs(nNewIndex), 100);
}
