var Photobox = new function() {
this.boxes = null;
this.uploaded_photobox = 0;
this.cache = { boxes : null };
this.status = { renaming : false, clicks : 0 };
this.selected,this.container = null;
// initialize photobox object
this.init = function(container) {
this.container = $(container);
this.cache.boxes = [];
if(this.uploaded_photobox)
this.selectPhotobox(this.uploaded_photobox);
this.updatePhotoboxes(true);
}
// handle asynchronous requests
this.request = function(cmd, callback, newname) {
var rURL = "/php/photobox_request.php?";
switch(cmd) {
case 'new': // new photobox
rURL+= 'new';
break;
case 'rem': // remove photobox
rURL+= 'rem&photobox_id='+this.selected.data.photobox_id;
break;
case 'name': // rename
rURL+= 'name='+escape(newname.escapeHTML())+'&photobox_id='+this.selected.data.photobox_id;
break;
default: // get
rURL+= 'get';
}
asyncRequest('GET', rURL, callback);
}
// dynamicly update photoboxes array
this.updatePhotoboxes = function(redraw, select_on_redraw) {
var callback = {
success: function(o) {
eval("this.boxes =" + o.responseText);
this.updatePhotoboxesCacheDataObject();
var cookie = Cookie.read();
if ((cookie.selPBID) && !(callback.select_on_redraw)) {
this.drawPhotoboxes(cookie.selPBID);
return;
}
if(callback.redraw || (!(this.boxes)))
this.drawPhotoboxes(callback.select_on_redraw ? callback.select_on_redraw : array_first(this.boxes).photobox_id);
},
failure: function(o) {
ylog('REQUEST FAILURE:');
ylog(o.statusText);
},
scope: this,
redraw: redraw,
select_on_redraw: select_on_redraw
}
this.request(null, callback);
}
// updates the .data object of each cache element
this.updatePhotoboxesCacheDataObject = function() {
if(!this.cache.boxes.length)
return;
for(var i in this.boxes) {
if(!this.cache.boxes[this.boxes[i].photobox_id])
continue;
this.cache.boxes[this.boxes[i].photobox_id].data = this.boxes[i];
this.cache.boxes[this.boxes[i].photobox_id].boxname.innerHTML = str_truncate(this.boxes[i].name, 20);
this.cache.boxes[this.boxes[i].photobox_id].photocount.innerHTML = " ("+this.boxes[i].photo_count+")";
}
}
// draw photoboxes on the page
this.drawPhotoboxes = function(select_on_draw) {
this.container.innerHTML = '';
YAHOO.util.Event.clearCache();
for (var i=0; i < this.boxes.length; i++)
this.createPhotobox(this.boxes[i]);
this.selectPhotobox(select_on_draw);
}
// create the photobox elements and append them to the document
this.createPhotobox = function(box) {
var boxContainer = el('div');
var boxIcon = el('img');
var boxName = el('div');
var boxPhotoCount = el('span');
var boxRadio = el('div');
var br = el('br');
if (!(window.PageEditApp))
boxRadio.innerHTML = '';
boxContainer.data = box;
boxContainer.boxname = boxName;
boxContainer.icon = boxIcon;
boxContainer.photocount = boxPhotoCount;
appendChildren(boxContainer, Array(boxIcon,boxRadio,boxName,boxPhotoCount,br));
addClass(boxContainer, "photobox-container");
addClass(boxIcon, "photobox-icon photobox-icon-closed");
addClass(boxName, "photobox-name");
addClass(boxPhotoCount, "photobox-photocount");
addClass(boxRadio, "photobox-radio");
// boxContainer.setAttribute("id","photobox-"+box.photobox_id);
generateUniqueID(boxContainer);
boxIcon.setAttribute("src", "/images/transparent1px.gif");
boxName.innerHTML = str_truncate(box.name, 16);
boxPhotoCount.innerHTML = " ("+box.photo_count+")";
br.clear = "all";
this.container.appendChild(boxContainer);
hook(boxContainer, "click",
function(e, thiz) {
thiz.selectPhotobox(this);
},
this,
false
);
this.cache.boxes[box.photobox_id] = boxContainer;
}
// select a photobox by it's element or photobox_id
this.selectPhotobox = function(_el) {
_el = isObj(_el) ? _el : this.cache.boxes[_el];
if(this.selected == _el)
return;
this.unselectPhotobox(this.selected);
this.selected = _el;
Cookie.write('selPBID', _el.data.photobox_id);
addClass(_el, "photobox-selected");
swapClass(_el.icon, "photobox-icon-closed","photobox-icon-open");
if(typeof Photobox_Photos != 'undefined')
Photobox_Photos.drawThumbs(_el.data.photobox_id);
if(!(isElementInView(this.container, _el)))
scrollIntoView(this.container, _el);
if(typeof this.onSelectPhotobox == 'function')
this.onSelectPhotobox(_el.data.photobox_id);
// check radio
try {
_el.getElementsByTagName("input")[0].checked = true;
} catch (x) {
// .
}
}
// unselect a photobox by it's element or photobox_id
this.unselectPhotobox = function(_el) {
_el = isObj(_el) ? _el : this.cache.boxes[_el];
if(!_el)
return;
removeClass(_el, "photobox-selected");
swapClass(_el.icon, "photobox-icon-open", "photobox-icon-closed");
this.selected = null;
}
// selects the next OR last photobox when a photobox is deleted
this.selectNextLastPhotobox = function(photobox_id, select_photobox) {
// find the index of the photobox deleted
for (var i=0; i < this.boxes.length; i++)
if(this.boxes[i].photobox_id == photobox_id)
var deleted_index = i;
// loop through array and find the previous, or next photobox
for (var i=0; i < this.boxes.length; i++) {
if(next) continue;
if(ideleted_index) var next = this.boxes[i].photobox_id;
}
// select the box if select_photobox | OR |
// return the photobox_id if !select_photobox
if(select_photobox)
this.selectPhotobox(next?next:last);
else
return next?next:last;
}
// select photobox callback
this.onSelectPhotobox = function() {
}
// changes the icon of the photobox you are dragging over
this.PhotoboxOnDragOver = function(photobox_id) {
if(!this.cache.boxes[photobox_id])
return;
addClass(this.cache.boxes[photobox_id].icon, "photobox-icon-dragging");
addClass(this.cache.boxes[photobox_id], "photobox-container-dragover");
}
// changes back the icon of the photobox you just dragged over
this.PhotoboxOnDragOut = function(photobox_id) {
if(!this.cache.boxes[photobox_id])
return;
removeClass(this.cache.boxes[photobox_id].icon, "photobox-icon-dragging");
removeClass(this.cache.boxes[photobox_id], "photobox-container-dragover");
}
// creates a new photobox
this.newPhotobox = function() {
this.wait_dlg = waitPanel("Creating Photobox, please wait...");
this.wait_dlg.show();
var callback = {
success: function(o) {
this.updatePhotoboxes(true, o.responseText);
this.wait_dlg.hide();
this.wait_dlg.destroy();
},
failure: function(o) {
ylog('REQUEST FAILURE:');
ylog(o.statusText);
this.wait_dlg.hide();
this.wait_dlg.destroy();
},
scope: this
}
this.request('new', callback);
}
// confirm photobox removal
this.removePhotoboxConfirm = function() {
if(parseInt(this.selected.data.photo_count) == 0) {
this.removePhotobox();
return;
}
var handleYES = function() {
Photobox.removePhotobox();
this.hide();
this.destroy();
}
var handleNO = function() {
this.hide();
this.destroy();
}
var hedr = "Confirm...";
var body = "The photobox you are about to delete contains photos!
Are you sure you want to delete the selected photobox?";
var cfgPropType = "icon";
var cfgPropExtra = YAHOO.widget.SimpleDialog.ICON_WARN;
var buttons = [ { text:"Yes", handler:handleYES }, { text:"No", handler:handleNO, isDefault:true } ];
notify(hedr, body, cfgPropType, cfgPropExtra, buttons, null);
}
// removes an existing photobox
this.removePhotobox = function() {
this.wait_dlg = waitPanel("Deleting Photobox, please wait...");
this.wait_dlg.show();
var callback = {
success: function(o) {
var deleted_photobox_id = this.selected.data.photobox_id;
// remove box from page
this.container.removeChild(this.selected);
this.selected = null;
this.updatePhotoboxes(true, this.selectNextLastPhotobox(deleted_photobox_id));
this.wait_dlg.hide();
this.wait_dlg.destroy();
},
failure: function(o) {
ylog('REQUEST FAILURE:');
ylog(o.statusText);
this.wait_dlg.hide();
this.wait_dlg.destroy();
},
scope: this
}
this.request('rem', callback);
}
// show the rename photobox prompt
this.renamePhotoboxPrompt = function() {
var handleOK = function() {
Photobox.renamePhotobox($("rename_photobox_name").value);
Photobox.rename_prompt.hide();
Photobox.rename_prompt.destroy();
Photobox.rename_prompt=null;
}
var handleCancel = function() {
Photobox.rename_prompt.hide();
Photobox.rename_prompt.destroy();
Photobox.rename_prompt=null;
}
var opts = {
modal: true,
fixedcenter: true,
width: "230px",
visible: true,
buttons: [ { text: "OK", handler: handleOK, isDefault: true },
{ text: "Cancel", handler: handleCancel }
],
zIndex: 9999999
}
this.rename_prompt = new YAHOO.widget.Dialog("photoboxrenameprompt", opts);
this.rename_prompt.setHeader("Rename Photo Box");
this.rename_prompt.setBody("Photo Box Name:
");
var key_enter = new YAHOO.util.KeyListener(document, { keys: 13}, { fn: handleOK } );
this.rename_prompt.cfg.queueProperty("keylisteners", key_enter);
this.rename_prompt.render(document.body);
this.rename_prompt.mask.style.zIndex = 9999998;
// set and select name
$("rename_photobox_name").value = this.selected.data.name.unescapeHTML();
$("rename_photobox_name").focus();
selectTextRange($("rename_photobox_name"), 0, 999);
}
// rename a photobox
this.renamePhotobox = function(newname) {
this.wait_dlg = waitPanel("Renaming Photobox, please wait...");
this.wait_dlg.show();
var callback = {
success: function(o) {
this.updatePhotoboxes(true, this.selected.data.photobox_id);
this.wait_dlg.hide();
this.wait_dlg.destroy();
},
failure: function(o) {
ylog('REQUEST FAILURE:');
ylog(o.statusText);
this.wait_dlg.hide();
this.wait_dlg.destroy();
},
scope: this
}
this.request('name', callback, newname);
}
// goto upload page with selected photobox_id
this.uploadTo = function() {
window.location = "/php/upload.php?photobox_id="+this.selected;
}
// share selected photobox
this.share = function() {
if (!(this.selected))
return;
var items = [{ photobox_id: this.selected.data.photobox_id }];
window.location = '/php/share_project.php?items=' + escape(JSON.stringify(items)) + '&from=' + escape(window.location);
}
}