// --------------------------------

function BonJoviGallery() {
	this.artistId = 313;
	this.totalPageCount;
	this.sectionSelectListId = "gallery_sections_select";
	this.paginationSelectListId = "photo_nav_pagination"; //"gallery_pagination_select";
	this.thumbnailsWrapperId = "thumbnails";
	this.detailImageWrapperId = "photo_img";
	this.detailCaptionWrapperId = "photo_caption";
	this.xl_button = "viewLargeButton";

	//this.currentSectionId; // = 3;
	//this.getSectionSelectList
	//this.gallerySections[0].images = new Array();
	this.currentCommentPage = 1;
	this.comment_moderation = false;
}

BonJoviGallery.prototype = new ArtistGallery;

// ------------------------------------------------------------

BonJoviGallery.prototype.showExtraLargeImage = function() {
	var xlWin = window.open(this.currentDetailImage.extraLargeUrl, "xlWin", "width=800, height=700, scrollbars=yes, location=no, status=no");
	xlWin.focus();
}

BonJoviGallery.prototype.updatePreviousAndNextStates = function() {
	if (this.currentDetailImage.id == this.currentSection.maxImageId) {
		document.getElementById("detail_previous_button").style.visibility = "hidden";
	} else {
		document.getElementById("detail_previous_button").style.visibility = "visible";
	}
	if (this.currentDetailImage.id == this.currentSection.minImageId) {
		document.getElementById("detail_next_button").style.visibility = "hidden";
	} else {
		document.getElementById("detail_next_button").style.visibility = "visible";
	}
	// Also mark the current page in the list of page nums:


}

BonJoviGallery.prototype.detailShowPrevious = function() {
	var len = this.currentSection.images.length;
	currentDetailImageIndex = 0;
	for (var i=0; i<len; i++) {
		if (this.currentSection.images[i] == this.currentDetailImage) {
			currentDetailImageIndex = i;
			break;
		}
	}
	//var nIndx = currentDetailImageIndex - 1;

	//if (typeof(this.currentSection.images[nIndx]) != "object") {
		//alert('fetch prev from db');
		this.getNextOrPreviousImageFromDb("previous");
	//} else {
	//	this.currentDetailImage = this.currentSection.images[nIndx];
	//	this.doDetailSelect(this.currentDetailImage);
		//var newImg = retObj.images[nIndx];
		//currentDetailImageIndex--;
		//currentDetailImgId = newImg.id;
		//loadDetailForImage(newImg);
	//}
	this.updatePreviousAndNextStates();
} // End detailShowPrevious()

BonJoviGallery.prototype.detailShowNext = function() {
	var len = this.currentSection.images.length;
	currentDetailImageIndex = 0;
	for (var i=0; i<len; i++) {
		if (this.currentSection.images[i] == this.currentDetailImage) {
			currentDetailImageIndex = i;
			break;
		}
	}
	//var nIndx = currentDetailImageIndex + 1;

	//if (typeof(this.currentSection.images[nIndx]) != "object") {
		this.getNextOrPreviousImageFromDb("next");
	//} else {
		//this.currentDetailImage = this.currentSection.images[nIndx];
		//this.doDetailSelect(this.currentDetailImage);
		//var newImg = this.currentSection.images[nIndx];
		//currentDetailImageIndex++;
		//currentDetailImgId = newImg.id;

		//loadDetailForImage(newImg);
	//}
	this.updatePreviousAndNextStates();
} // End detailShowNext()

BonJoviGallery.prototype.getNextOrPreviousImageFromDb = function(direction) {
	var sys = this.currentSection.system.replace(" ", "");//tmp[0];
	var sectionId = this.currentSection.id; //tmp[1];

	if (direction != "previous") {
		direction = "next";
	}
	var onSuccessFunc = function(req) { handleGetNextOrPreviousImageFromDb(req, direction); }
	var onErrorFunc = function(req) { alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText); }
	var onTimeOutFunc = function(req) { alert('Timed Out!'); }
	var reqUrl = 'ajax_requests/gallery_next_or_previous.php';
	var req = AjaxRequest.post({'url':reqUrl, 'imageId':this.currentDetailImage.id, 'system':sys, 'sectionId':sectionId ,'direction':direction, 'onSuccess': onSuccessFunc, 'onError': onErrorFunc, 'onTimeout': onTimeOutFunc });
} // End getNextOrPreviousImageFromDb()

function handleGetNextOrPreviousImageFromDb(reqObj, dir) {
		//alert(reqObj.responseText);
		var obj = eval("(" + reqObj.responseText + ")");
		if (obj.type == "error") {
			return;
		}
		/*
		currentDetailImgId = obj.id;

		if (dir == "next") {
			//currentDetailImageIndex++;
		} else {
			//currentDetailImageIndex--;
		}
		//loadDetailForImage(obj);
		*/
		//ArtistGalleryImage(id, caption, name, url, thumbUrl, extraLargeUrl, sectionId)
		gallery.currentDetailImage = new ArtistGalleryImage(obj.id, obj.caption, obj.name, obj.url, "", "", "");
		gallery.doDetailSelect(gallery.currentDetailImage);
}

// ------------------------------------------------------------

BonJoviGallery.prototype.updateFanUploadButton = function() {
	document.getElementById("upload_gallery_but").style.visibility = (this.currentSection.system == "gallery") ? "hidden" : "visible";
}

BonJoviGallery.prototype.fanUploadButtonOnclick = function() {
	if (!window.upUserName) {
		showLoginForm('http://' + location.hostname + location.pathname + "?page=" + this.currentPageNumber + "&section=" + this.currentSection.id);
	} else {
		var newWin = window.open("popups/upload_gallery_photo.php", "newWin", "width=500, height=600, scrollbars=yes, location=no, status=no");
		newWin.focus();
	}
}

BonJoviGallery.prototype.updatePaginationControl = function() {
	var wrap = document.getElementById("gallery_paging");

	wrap.innerHTML = "";


	
	for (var i=1; i<=this.totalPageCount; i++) {
		wrap.style.visibility = "visible";

		

		var tmp = document.createElement("a");
		if (i == this.currentPageNumber) {
			tmp.className = "gallery_paginator_current";
		} else {
			tmp.className = "gallery_paginator";
		}
		tmp.href = "#";
		tmp.innerHTML = i;
		tmp.onclick = function() { gallery.loadNewPage(this.innerHTML); return false; }
		tmp.innerHTML = i;
		wrap.appendChild(tmp);
		wrap.appendChild(document.createTextNode('  '));
	}
}

BonJoviGallery.prototype.updateThumbsNav = function() {
	var tmp = document.getElementById("gallery_paging");
	var els = tmp.getElementsByTagName("a");
	for (var i=0; i<els.length; i++) {
		els[i].style.fontWeight = ((i+1) == this.currentPageNumber) ? "bold" : "normal";
	}
	document.getElementById("gallery_paging_prev_button").style.visibility = (this.currentPageNumber == 1) ? "hidden" : "visible";
	document.getElementById("gallery_paging_next_button").style.visibility = (this.currentPageNumber == this.totalPageCount) ? "hidden" : "visible";

}

BonJoviGallery.prototype.paginationControlOnChange = function() {
	var tmp = document.getElementById(this.paginationSelectListId);
	this.loadNewPage(tmp[tmp.selectedIndex].value);
}

BonJoviGallery.prototype.loadNewPage = function(pageNum) {
	this.currentPageNumber = pageNum;
	this.getGalleryThumbnails();
	this.updatePreviousAndNextStates();
	this.updateThumbsNav();
}

BonJoviGallery.prototype.updateThumbnailDisplay = function() {

	var wrap = document.getElementById(this.thumbnailsWrapperId);
	wrap.innerHTML = "";
	var subjectSection = "";
	for (var i=0; i<this.gallerySections.length; i++) {
		if (this.gallerySections[i].id == this.currentSectionId) {
			var subjectSection = this.gallerySections[i];
			break;
		}
	}
	if (subjectSection != "" && subjectSection.images.length == 0) {
		wrap.innerHTML = "<p>No images available</p>";

		//alert("No images available for this gallery section");
	} else if (subjectSection != "" && subjectSection.images.length > 0) {
		for (var i=0; i<subjectSection.images.length; i++) {
			var tmp = document.createElement("div");
			tmp.id = subjectSection.images[i].id;
			tmp.className = "thumbnail";

			//tmp.className = ((i+1)%2 == 0) ? "gallery_thumb_2" : "gallery_thumb";
			var im = document.createElement("img");
			im.style.backgroundImage = "url(" + subjectSection.images[i].thumbUrl + ")";
			im.src = "images/spacer.gif";
			im.width = 80;
			im.height = 80;
			im.className = ((i+1)%2 == 0) ? "gallery_thumb_2" : "gallery_thumb";


			tmp.appendChild(im);
			var tmp2 = eval(subjectSection.images[i]);
			var tmp3 = this;
			tmp.onclick = function() { tmp3.doDetailSelect(tmp2);	};
			tmp.onclick = function() { tmp3.doThumbClick(this);	};
			tmp.onmouseover = function() { tmp3.doThumbHover(this, 'on'); };
			tmp.onmouseout = function() { tmp3.doThumbHover(this, 'off'); };
			wrap.appendChild(tmp);

		} // for
	} // if
	this.updatePaginationControl();
} // End updateThumbnailDisplay() method

BonJoviGallery.prototype.doThumbClick = function(subj) {
	var len = this.currentSection.images.length;
	for (var i=0; i<len; i++) {
		if (this.currentSection.images[i].id == subj.id) {
			this.doDetailSelect(this.currentSection.images[i]);
			break;
		} // if
	} // for
} // End doThumbClick method

BonJoviGallery.prototype.doThumbHover = function(subj, direction) {
	if (direction != "off") {
		direction = "on"
	}
	try {
		subj.firstChild.style.cursor = (direction == "on") ? "pointer" : "default";
	} catch (e) { alert(e); }

}

BonJoviGallery.prototype.doDetailSelect = function(image) {
	var wrap = document.getElementById(this.detailImageWrapperId);
	if (!image) {
		var tmp = 0;
		for (var i=0; i < this.gallerySections.length; i++) {
			if (this.gallerySections[i].id == this.currentSectionId && this.gallerySections[i].images.length > 0) {
				image = this.gallerySections[i].images[0];

				break;
			}
		} // for
	} // if !image



	if (!image) {
		wrap.innerHTML = "<p>No image available</p>";
		document.getElementById("photo_control").style.visibility = "hidden";
		document.getElementById(this.xl_button).style.visibility = "hidden";
		//this.updatePaginationControl();
		//this.updatePreviousAndNextStates();
		//document.getElementById("gallery_paging_prev_button").style.visibility = "hidden";
		//document.getElementById("gallery_paging_next_button").style.visibility = "hidden";
		//document.getElementById("send_friend_wrapper").style.visibility = "hidden";
		//document.getElementById("detail_next_button").style.visibility = "hidden";
		//document.getElementById("detail_previous_button").style.visibility = "hidden";
		//document.getElementById("view_extra_large_wrapper").style.visibility = "hidden";
		return;
	}

	document.getElementById("photo_control").style.visibility = "visible";
	//document.getElementById("send_friend_wrapper").style.visibility = "visible";
	this.currentDetailImage = image;

	//this.loadComments();
	//$('#'+comment_form).html('');

	var tmp = document.createElement("img");
	tmp.src = image.url;

	if (wrap.firstChild) {
		wrap.replaceChild(tmp, wrap.firstChild);
	} else {
		wrap.innerHTML = "";
		wrap.appendChild(tmp);
	}
	var wrap = document.getElementById(this.detailCaptionWrapperId);
	wrap.innerHTML = image.caption;
	document.getElementById(this.xl_button).style.visibility = (this.currentDetailImage.extraLargeUrl == "") ? "hidden" : "visible";
	this.updatePreviousAndNextStates();

	// check for hook, and if so call method
	if(typeof(Hook_doDetailSelect) == 'function') { Hook_doDetailSelect(image); }
}

/**
* Available options: comment_page
*/
BonJoviGallery.prototype.loadComments = function(options)
{
	var me = this;

	if (options && options.comment_page) {
		this.currentCommentPage = options.comment_page;
	}
	if ((options && options.clear) || !this.currentDetailImage) {
		$('#gallery_comment_wrapper').hide();
		return;
	}

	// request comments content
	$('#gallery_comment_wrapper').hide();
	$.ajax({
		type: 'GET',
		url: '_gallery_comments.php',
		data: {
			uf_item_id: '7-' + this.currentDetailImage.id,
			ajax_parent: location.pathname.match(/\/([^\/]+)?$/)[1],
			comment_page: this.currentCommentPage
		},
		success: function(content) {
			$('#gallery_comment_wrapper').html(content).show('fast');
			me.bindCommentControls();
		},
		dataType:'html'
	});
}

/**
* bind clicks of comment edit, remove, prev and next to ajax
*/

BonJoviGallery.prototype.bindCommentControls = function()
{
	var me = this;

	this.loadCommentModeration();

	$('div.gallery_member_links a.edit').bind('click', function()
	{
		//clear any existing edit forms
		$('form.comment_edit_form').remove();

		var control_area = $(this).parent();
		var replaced_controls = control_area.html();

		//comment form expects global loadComments function
		//window.loadComments = function() { me.loadComments(); };

		$.get(this.href, function(content)
		{
			control_area.html(content);
		});

		return false;
	});

	$('div.gallery_member_links a.remove').bind('click', function()
	{
		if (!confirm('Are you sure you want to remove this comment?')) {
			return false;
		}

		var comment_id = this.href.match(/comment_id=(\d+)/)[1];

		$.ajax({
			type: 'POST',
			url: this.href.replace(/back_url=[^&]* /, ''),
			success: function(response_content)
			{
				alert(response_content == '0' ? 'Comment removed.' : 'Sorry, there was an error removing the comment.');
				//me.loadComments();
			}
		});

		return false;
	});

	$('#gallery_comment_pagination a').bind('click', function()
	{
		//me.loadComments({comment_page: this.href.match(/comment_page=(\d+)/)[1]});
		return false;
	});
}

BonJoviGallery.prototype.loadCommentModeration = function() //{{{
{
	var me = this;
	if (!this.comment_moderation) {
		//request comment moderation privileges
		$.ajax({
			type: 'GET',
			url: '/www2/lib/comment/is_moderator.php?site_id=9',
			dataType: 'html',
			success: function(response_content)
			{
				me.comment_moderation = response_content || 'n';
				me.loadCommentModeration();
			}
		});
	} else {
		//reveal moderation links according to response
		switch (this.comment_moderation) {
		case 'n':
			break;
		case 'y':
			$('div.gallery_member_links').show();
			break;
		default:
			/**
			* If user is logged in but not a moderator, the
			* response will be their ID; here we will reveal
			* moderation links for any comments that belong to
			* this user.
			*/
			$('div.gallery_member_links').filter(function()
			{
				return $(this).attr('class').match('user_' + me.comment_moderation);
			}).show();
			break;
		}
	}

} //}}}

BonJoviGallery.prototype.sectionSelectListOnChange = function() {
	this.currentPageNumber = 1;
	var tmp = document.getElementById(this.sectionSelectListId);
	this.changeSection(this.gallerySections[tmp.selectedIndex].id);
	this.updatePaginationControl();
	this.updatePreviousAndNextStates();
	//this.updateFanUploadButton();
	//document.getElementById("gallery_section_title").innerHTML = this.currentSection.name;

	//this.loadComments({clear:true});
	//$('#'+comment_form).html('');
}

BonJoviGallery.prototype.updateSectionSelectList = function() {
	var lst = document.getElementById(this.sectionSelectListId);
	var len = this.gallerySections.length;
	var selIndex = 0;
	var tmp = this;
	lst.onchange = function() { tmp.sectionSelectListOnChange(); };
	lst.innerHTML = "";
	for (var i=0; i<len; i++) {
		var selVal = (this.gallerySections[i].id == this.currentSectionId) ? true : false;
		if (selVal == true) {
			var selIndex = i;
		} // if
		lst.options[lst.options.length] = new Option(this.gallerySections[i].name, this.gallerySections[i].id);
		//var newOpt = new Option(this.gallerySections[i].name, this.gallerySections[i].id);
		//newOpt.text = this.gallerySections[i].name;
		//alert(lst.options);
		//lst.options.add(newOpt, i);
		//lst.appendChild(newOpt, false, selVal);
	} // for
	//this.currentPageNumber = 1;
	lst.selectedIndex = selIndex;
	//document.getElementById("gallery_section_title").innerHTML = this.currentSection.name;
}


/*
BonJoviGallery.prototype.loadGalleryPage = function(pageNumber) {
	if (pageNumber == currentPageNumber) {
		return;
	}
	currentPageNumber = pageNumber;
	getGalleryThumbnails();
	var pager = document.getElementById("galleryPaginationControl");
	var cells = pager.getElementsByTagName("td");
	for (var i=0; i<cells.length; i++) {
		if (i == (pageNumber - 1)) {
			cells[i].className = "page_num1";
		} else {
			cells[i].className = "page_num0";
		}
	}
} // End loadGalleryPage(pageNumber) function

function handleGetGalleryThumbnailsResponse(reqObj) {
	retObj = eval("(" + reqObj.responseText + ")");
	sectionMinImageId = retObj.sectionMinImageId;
	sectionMaxImageId = retObj.sectionMaxImageId;

	var len = retObj.images.length;
	var preImg = new Image();
	for (var i=0; i<len; i++) {
		preImg.src = retObj.images[i].thumbUrl;
	}
	// Only show the upload link if appropriate:
	if (retObj.type == "fanuploads") {
		document.getElementById("uploadImage").style.visibility = "visible";
	} else {
		document.getElementById("uploadImage").style.visibility = "hidden";
	}
	// Update the paging control:
	var paging = document.getElementById("galleryPaginationControlTable");
	try {
		paging.innerHTML = "";
	} catch(e) {
		while (paging.rows.length > 0) {
			paging.deleteRow(0);
		}
	}
	//var newRow = document.createElement("tr");
	var newRow = paging.insertRow(-1);
	for (var i=1; i<=retObj.totalPageCount; i++) {
		if (i == currentPageNumber) {
			var classMark = "1";
		} else {
			var classMark = "0";
		}
		try {
			var newTd = newRow.insertCell(-1);
			newTd.id = "pagination_" + i;
			newTd.className = "page_num" + classMark;
			newTd.innerHTML = '<a class="gallery_pagination_item" onclick="loadGalleryPage(' + i + '); return false;" href="#">' + i + '</a>';
		} catch(e) {
			newRow.innerHTML = newRow.innerHTML + '<td id="pagination_' + i + '" class="page_num' + classMark + '"><a class="gallery_pagination_item" onclick="loadGalleryPage(' + i + '); return false;" href="#">' + i + '</a></td>';
		}
		if (i % 28 == 0) {
			//paging.insertRow(-1);
			//paging.appendChild(newRow);
			//var newRow = document.createElement("tr");
			var newRow = paging.insertRow(-1);
		}
	} // for
	//paging.appendChild(newRow);


	// Update the actual thumb imgs:
	var wrap = document.getElementById("gallery_wrap");
	wrap.innerHTML = "";
	var len = retObj.images.length;

	for (i=0; i<len; i++) {
		var tmp = document.createElement("div");
		tmp.className = "photo_wrap";
		tmp.innerHTML = '&nbsp;<img src="' + retObj.images[i].thumbUrl + '" alt="' + retObj.images[i].name + '" title="' + retObj.images[i].name +
				'" id="img_' + i + '"class="thumb" onmouseover="handleThumbHover(this, event);" onmouseout="handleThumbHover(this, event);" onclick="showDetailImage(event);" />&nbsp;';
		wrap.appendChild(tmp);
	}
} // End handleGetGalleryThumbnailsResponse(reqObj) function
*/