// General
var selEvent;
function queueSet(id,many) { 
	var url = "/djondemand_player.php";
	popupWin = open( "", "popupWin", "width=520,height=400,scrollbars=no,toolbar=no,location=no,status=no,menubar=no,resizable=no" );
	if( !popupWin || popupWin.closed || !popupWin.queueSet ) {
		popupWin = window.open( url+'?sets='+id, "popupWin", "width=520,height=400" );
	} else { 
		popupWin.focus();
		popupWin.queueSet(id);
	}
}


function convertLinks() {
	var links = $$('a.lnk_update');
	for (var index = 0, len = links.length; index < len; ++index) {
	  var itm = links[index];
	  links[index].onclick = function() {
			launchNewsBox((this.id).substring(3)); return false;
		};
	}

	links = $$('a.lnk_flow');
	for (var index = 0, len = links.length; index < len; ++index) {
	  var itm = links[index];
	  links[index].onclick = function() {
		  	var idarr = this.href.split('page=');
			loadUpdatesPage(idarr[1]);
			return false;
		};
	}

}
function toggleIncComments() { 
	if (incComments) { 
		$('inccomments').style.backgroundColor = '#000a55';
	} else {
		$('inccomments').style.backgroundColor = '#0066bb';
	}

	params = {
		action:'commentsOnFrontPage',
		incComments:incComments
	};
	new Ajax.Request('ajax.php',{parameters: params, onSuccess:function(t) { 
																			loadUpdatesPage('0',t.responseText); }																		
																		});

	incComments = !incComments;
}

function alertUser(msg) { 
	alert(msg);
}

function disableButton(a) { 
	a.setOpacity(.6);
}

function enableButton(a) { 
	a.setOpacity(1);
}

// User Photo Uploading -----------------------------------------
function createAlbum(eid) { 
	$('newAlbumID').value = eid;
	$('newAlbumForm').submit();
}

function deleteAlbum(setid) { 
	if (confirm('Are you sure you want to remove this album? All photos will be removed - cannot be undone.')) { 
		params = {
			action:'deleteAlbum',
			setid:setid
		};
		new Ajax.Request('ajax.php',{parameters: params, onSuccess:function (t) {
																			 if (t.responseText=='2') { 
																			 	alert('album cannot be deleted');
																			 } else {
																				 Effect.Puff('album'+setid);
																			 }
																			 }});
	}
}

function makeLive(setid) { 
		params = {
			action:'livePhotoSet',
			setid:setid
		};
		new Ajax.Request('ajax.php',{parameters: params, onSuccess:function (t) {  alert ('done');}});
}

function deleteImage(img) { 
	if (confirm('Are you sure you want to remove this image?')) { 
	params = {
		action:'deleteImage',
		id:img
	};
	new Ajax.Request('ajax.php',{parameters: params, onSuccess:function (t) {
		 if (t.responseText=='1') {
			 Effect.Puff('photo'+img,{ afterFinish: function() { $('glassboxcontent').removeChild($('photo'+img));}, duration: 0.5  } );
			

			 params = {
				action:'renderAlbumSummary',
				setid:tSetId
			 };
			 new Ajax.Updater('album'+tSetId,'ajax.php',{parameters: params, onComplete:function () {														
			 }
			 });					 			 
		 } else 
		 if (t.responseText=='2') {
		 	alert('You may not delete this photo because albums must contain at least 10 photos while in the published state.');
		 } else alert('You are not authorized to delete this image. Please check if you have been logged out and relogin.');
	}
	});		
	
	}
}
// Event Selector -----------------------------------------
var inEventSearch = false;
var inEventSearchKeyword = '';
var eventSearchOffset = 0;

function nextEvent(direction) {
	eventSearchOffset = eventSearchOffset + 8 * direction;
	searchEvent(inEventSearchKeyword,true);
}

function selectEvent(id,title) { 
	$('selectedEvent').update(title);
	enableButton($('btnNextUpload'));
	selEvent = id;
}

function searchEvent(a,skip) {
	if ((!skip)&&(a.length<5)) alertUser('Search keyword must be at least 5 characters long.'); 
	else {
			params = { action:'searchEvents', keyword:a, offset:eventSearchOffset };
			new Ajax.Updater('pickEvents','ajax.php',{parameters: params, success: function () {
																							 	 inEventSearch = true;
																							 	 inEventSearchKeyword = $("searchKey").value;
																							   } 
												});		
	}
	
}

function launchUserPhoto() { 
	params = {
		action:'createEventPicker',
		message:'Please pick the event you want to create a photo album for:',
		callback:'createAlbum'
	};
	new Ajax.Updater('AddAlbumBox','ajax.php',{parameters: params, onComplete:function () {
																			disableButton($('btnNextUpload'));
																			var selEvent = null;
	}
	});		

	var myBox = new GlassBox();
	myBox.dblclick = false;
	myBox.init( 'createEventBox', '580px', '340px', 'hidden', 'greyline', true, false );
	myBox.lbo( true, 0.5 ); 
	
	myBox.appear();
}

function loadUpdatesPage(page) { 
	new Ajax.Updater('updates_content','front_uphotos.php', {
	  method: 'post',
	  evalScripts:false,
	  onComplete: function(t) {
		  convertLinks();
	  },
	  parameters: 'ajax=1&page='+page //+'&user_id='+userid
	});		  	
	
}
function launchNewsBox(id) { 
	var myBox = new GlassBox();
	myBox.init( 'glassyBox', '820px', '720px', 'hidden', 'greyline', true, true );
	myBox.lbo( true, 0.13 ); 	
	myBox.appear();
	new Ajax.Updater('glassyBoxContent','news.php', {
	  method: 'post',
	  evalScripts:true,
	  parameters: 'ajax=1&id='+id
	});		  	
	
}