/***************************************************************************
*
* IMPORTANT: This is a commercial product made by AntonLV and cannot be modified for other than personal usage. 
* This product cannot be redistributed for free or a fee without written permission from AntonLV. 
* This notice may not be removed from the source code.
*
***************************************************************************/

function alv_alb_executeRequest(sAction, aParams, onResult, sType) {
	if(onResult == undefined || onResult == null) 
		onResult = function(){};
	if(sType == undefined || sType == "")
		sType = 'xml';

	var oDate = new Date();
	aParams._t = oDate.getTime();
	$.post(sAlvAlbHomeUrl + 'actions/'+ sAction + '.php', aParams, onResult, sType);
}
function toggleLoading() {
	$('#alv_alb_loading').toggle();
}

function alv_alb_voteAlbum(iId, iVote) {
	toggleLoading();

	alv_alb_executeRequest(
		'album_vote', 
		{id: iId, vote:iVote}, 
		function(oData) {
			toggleLoading();

			if(parseInt(oData.code) != 0) {				
				alert(oData.message);
				return;
			}
			
			var iRate = parseInt($(".alv_alb_albums > #alv_alb_album" + oData.id + " #alv_alba_rate" + oData.id).html());
			$(".alv_alb_albums > #alv_alb_album" + oData.id + " #alv_alba_rate" + oData.id).html(iRate + iVote);
		},
		'json'
	);
}
function alv_alb_editAlbum(oLink, iId) {
	toggleLoading();

	alv_alb_executeRequest(
		'album_get', 
		{id: iId}, 
		function(oResult) {
			//--- Fill form ---//
			$.each($('#alv_alb_afrm :input:not(:submit,:button,:file)'), function(){
				$(this).val(oResult[$(this).attr('name')]);
			});
			toggleLoading();

			//--- Show form ---//
			var oOffsetLink = $(oLink).offset();
			$('#alv_alb_afrm').css('top', (oOffsetLink.top + 15) + 'px').css('left', (oOffsetLink.left + 20) + 'px').show('slow');
		},
		'json'
	);
}
function alv_alb_onEditAlbum(oData) {
	if(parseInt(oData.code) != 0) {
		toggleLoading();
		alert(oData.message);
		return;
	}

	alv_alb_executeRequest(
		'album_create', 
		{id: oData.id}, 
		function(sResult) {
			toggleLoading();
			$('#alv_alb_afrm').hide('slow', function(){
				$('#alv_alb_afrm :input:not(:hidden,:submit,:button)').val('');

				$(".alv_alb_albums > #alv_alb_album" + oData.id).fadeOut('slow', function(){
					$(this).replaceWith(sResult).fadeIn('slow');
				});
			});
		},
		'html'
	);
}
function alv_alb_deleteAlbum(iId) {
	toggleLoading();

	if(confirm(sAlvAlbWrnAlbumDelete))
		alv_alb_executeRequest('album_delete', {id: iId}, alv_alb_onDeleteAlbum, 'json');
}
function alv_alb_onDeleteAlbum(oData) {
	toggleLoading();

	if(parseInt(oData.code) != 0)
		alert(oData.message);
	else
		$("#alv_alb_album" + oData.id).fadeOut('slow', function(){
			$(this).remove();
		});
}
function alv_alb_createAlbum(oLink){
	var oOffsetLink = $(oLink).offset();

	$('#alv_alb_afrm').css('top', (oOffsetLink.top + 15) + 'px').css('left', (oOffsetLink.left + 20) + 'px').show('slow');
}
function alv_alb_onCreateAlbum(oData) {
	if(oData.code != 0) {
		toggleLoading();
		alert(oData.message);
		return;
	}

	alv_alb_executeRequest(
		'album_create', 
		{id: oData.id}, 
		function(sResult) {
			toggleLoading();
			$('#alv_alb_afrm').hide('slow', function(){
				$('#alv_alb_afrm :input:not(:hidden,:submit,:button)').val('');
				if($(".alv_alb_albums > .alv_alb_album").length > 0)
					$(".alv_alb_albums > .alv_alb_album:first").before($(sResult).hide()).prev('.alv_alb_album').fadeIn('slow');
				else {
					$(".alv_alb_albums > .alv_alb_empty").replaceWith($(sResult).hide());
					$(".alv_alb_albums > .alv_alb_album:first").fadeIn('slow');
				}
			});
		},
		'html'
	);
}
function alv_alb_onCloseAlbumForm() {
	$('#alv_alb_afrm').hide('slow');
	$('#alv_alb_afrm :input:not(:hidden,:submit,:button)').val('');
}


function alv_alb_selectPhoto(iId) {
	alv_alb_hideStat();
	$('#alv_alb_preview_image').css('background-image', 'url(' + sAlvAlbImgUrl + 'preloader.gif)');

	alv_alb_executeRequest('photo_select', {id: iId}, alv_alb_onSelectPhoto);
	return false;
}
function alv_alb_onSelectPhoto(oData) {
	oNode = oData.getElementsByTagName('photo')[0];
	if(oNode.getAttribute('id') != null) {				
		var oImage = new Image();
		oImage.onload = function(){
			$('#alv_alb_preview_image').css('background-image', 'url(' + oImage.src + ')');
			alv_alb_showStat();
		}				
		oImage.src = sAlvAlbPhotosUrl + oNode.getAttribute('image') + '_p.' + oNode.getAttribute('extension');

		iSelectedId = parseInt(oNode.getAttribute('id'));
		iSelectedWidth = parseInt(oNode.getAttribute('width'));
		iSelectedHeight = parseInt(oNode.getAttribute('height'));

		$('#alv_alb_preview_title').text(oNode.firstChild.nodeValue);
		if(oNode.getAttribute('views_today') != null && oNode.getAttribute('views_all') != null) {
			$('#alv_alb_preview_viewst').text(oNode.getAttribute('views_today'));
			$('#alv_alb_preview_viewsa').text(oNode.getAttribute('views_all'));
		}
	}
	else
		alert(sAlvAlbErrCannotLoadImage);
}
function alv_alb_previous() {	
	var oLastVisible = $('#alv_alb_chooser > div:visible:last');
	var oLastHidden = $('#alv_alb_chooser > div:hidden:lt(' + $('#alv_alb_chooser > div.thumbnail').index($('#alv_alb_chooser > div.thumbnail:visible:first')) + '):last');

	if(oLastHidden.length > 0) {
		oLastVisible.hide();
		oLastHidden.show();
	}	
}
function alv_alb_next() {
	var oFirstVisible = $('#alv_alb_chooser > div:visible:first');	
	var oFirstHidden = $('#alv_alb_chooser > div:visible:first ~ div:hidden:first');		
	
	if(oFirstHidden.length > 0) {
		oFirstHidden.show();
		oFirstVisible.hide();
	}	
}
function alv_alb_moreInfo(oButton) {
	var oMoreWnd = $('.alv_alb_vr_album');
	if(oMoreWnd.css('display') != 'block') {
		alv_alb_hideStat();

		if(jQuery.browser.mozilla)
			oMoreWnd.slideDown(500);
		else
			oMoreWnd.show();
		$(oButton).css('background-image', 'url(' + sAlvAlbImgUrl + 'less.png)');
	}
	else {		
		if(jQuery.browser.mozilla)
			oMoreWnd.slideUp(500, alv_alb_showStat);
		else
			oMoreWnd.hide(1, alv_alb_showStat);
		$(oButton).css('background-image', 'url(' + sAlvAlbImgUrl + 'more.png)');
	}
}
function alv_alb_hideStat() {
	$('.alv_alb_vr_pinfo').hide();
}
function alv_alb_showStat() {
	var oInfo = $('.alv_alb_vr_pinfo');

	if(jQuery.browser.mozilla)
		oInfo.slideDown(500);
	else
		oInfo.show();
}
function alv_alb_showViewer(iAlbumId) {
	window.open(sAlvAlbSysRootUrl + 'alv_viewer.php?id=' + iAlbumId, 'viewer_for_' + iAlbumId, 'width=500,height=598,left=10,top=10,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no');
}
function alv_alb_showPhoto(iPhotoId, iWidth, iHeight) {
	window.open(sAlvAlbSysRootUrl + 'alv_photo.php?id=' + iPhotoId, 'photo_' + iPhotoId, 'width=' + iWidth + ',height=' + iHeight + ',left=10,top=10,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no');
}


function alv_alb_addPhotoField() {
	var oHolder = document.getElementById('alv_alb_images');

	var sBrowser = navigator.userAgent.toLowerCase();
	
	var oDiv =  (sBrowser.indexOf("msie") != -1) && (sBrowser.indexOf("opera") == -1) && (sBrowser.indexOf("webtv") == -1) ? document.createElement('<div>') : document.createElement('div');	
	oDiv.className = 'row';

	var oTitle =  (sBrowser.indexOf("msie") != -1) && (sBrowser.indexOf("opera") == -1) && (sBrowser.indexOf("webtv") == -1) ? document.createElement('<input name="titles[]">') : document.createElement('input');
	oTitle.name = 'titles[]';
	oTitle.type = 'text';
	oTitle.className = 'text';

	var oBrowse =  (sBrowser.indexOf("msie") != -1) && (sBrowser.indexOf("opera") == -1) && (sBrowser.indexOf("webtv") == -1) ? document.createElement('<input name="images[]">') : document.createElement('input');
	oBrowse.name = 'images[]';
	oBrowse.type = 'file';	
	oBrowse.className = 'file';
	
	oDiv.appendChild(oTitle);
	oDiv.appendChild(oBrowse);

	oHolder.appendChild(oDiv);
}