
// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth(){
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight() {
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function posRight() {
	return posLeft()+pageWidth();
}

function posBottom() {
	return posTop()+pageHeight();
}
                    

function moveLB() {
    var lb = getElement('lbMax');
    var lbc = getElement('lbMin');
    var y = posBottom(); 
    if (lb && lb.style.display == '') {
        y -= 150;
        YAHOO.util.Dom.setY(lb, y);
    }
    if (lbc && lbc.style.display == '') {
        y -= 35;
        YAHOO.util.Dom.setY(lbc,y);
    } 
    
    setTimeout("moveLB();",250);
   
}

function collapseLB() {
    var lb = getElement('lbMax');
    var lbc = getElement('lbMin');
    
    if(lb && lbc){
        lb.style.display = 'none';
        lbc.style.display = '';
        url = '../lightbox/lb_action.php?a=slbs&s=0';
        YAHOO.util.Connect.asyncRequest('GET', url);
    }        
    return false;
}

function expandLB() {
    var lb = getElement('lbMax');
    var lbc = getElement('lbMin');

    if(lb && lbc){
        lb.style.display = '';
        lbc.style.display = 'none';
        url = '../lightbox/lb_action.php?a=slbs&s=1';
        YAHOO.util.Connect.asyncRequest('GET', url);
        jQuery(document).ready(function() {jQuery('#theLB').jcarousel();});
    }        
    return false;
}

function showNewLB() {
    var myD;
    if(yahooDialogs["newLB"]){
        myD = yahooDialogs["newLB"];
        getElement("_lbname").value="";
        myD.show();
    }
    else{
    	myD = new YAHOO.widget.Dialog("newLB", { postmethod:"async", width:"300px", height:"250px", x:200, y:200, underlay:"none", draggable:false, fixedcenter:true } );
        yahooDialogs["newLB"] = myD;
    }

	var hS = function() { 
		myD.submit(); 
		return true;
	};
    myD.submit = function() {
        if(this.validate()){
            this.beforeSubmitEvent.fire();
            this.doSubmit();
            this.submitEvent.fire();
            return true;
        }
        else{
            return false;
        }
    };
	myD.validate = function(){
		if ( this.getData()._lbname.trim().length < 1 ) {
			displayDialogStatusMessage("lblNewLBStatus", STATUS_ERROR, "Please enter a lightbox name!");
			return false;
		}
		return true;
	};
	var onSuccess = function(o) { id = o.responseText; if (id>0) changeLB(id);  }
	var b = [ { text:"Save", handler:hS, isDefault:true } ];
	myD.cfg.queueProperty("buttons", b);
	getElement("lblNewLBStatus").innerHTML = "";
	myD.callback.success = onSuccess;
	myD.render();
}

function changeLB(id) {
    url = '../lightbox/lb_action.php?a=slbid&lbid=' + id;
    var cb = { success: function(o) { location.reload(true);  } };
    YAHOO.util.Connect.asyncRequest('GET', url, cb);
}

function removeLBWork(lbid, wid) {
    url = '../lightbox/lb_action.php?a=rw&lid=' + lbid + '&wid=' + wid;
    var cb = { success: function(o) { location.reload(true);  } };
    YAHOO.util.Connect.asyncRequest('GET', url, cb);
}

function rateLB(lbid, mid, r) {
	url = "../lightbox/lb_action.php?a=r&l=" + lbid + "&m=" + mid + "&r=" + r;
	var cb = { success: function(o) { location.reload(true); } };
	YAHOO.util.Connect.asyncRequest('GET', url, cb);
}

function delLB(lbid) {
	if ( confirm("Are you sure you'd like to delete this lightbox?") == false) return false;
	url = '../lightbox/lb_action.php?a=d&l=' + lbid;
	var cb = { success: function(o) { location.reload(true);  } };
    YAHOO.util.Connect.asyncRequest('GET', url, cb);
    return true;
}

function lightbox_onShareClick(){
    var statusObj = getElement('lblShareLBStatus');
    statusObj.innerHTML = '';

    var handleShare = function() { this.submit(); };

    var handleClose = function() { this.cancel(); };

    var buttons = [ { text:"Share", handler:handleShare, isDefault:true },
                    { text:"Cancel", handler:handleClose }  ];

    var dialog;
    var divID = 'pnlShareLB';

    var divObj = getElement(divID);
    if(divObj) divObj.style.display = '';

    if(yahooDialogs[divID]){
        dialog = yahooDialogs[divID];
        dialog.show();
    }
    else{
        dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:"360px", x:200, y:240, underlay:"none", draggable:false, fixedcenter:true } );
        yahooDialogs[divID] = dialog;
    }

    dialog.validate = function() {
        if(this.getData().txtTo.trim() == ''){
            displayDialogStatusMessage(statusObj.id, STATUS_ERROR, MSG_ERROR_REQUIRED_USERNAME );
            return false;
        }
        return true;
    };
    
    dialog.submit = function() {
        if(this.validate()){
            this.beforeSubmitEvent.fire();
            this.doSubmit();
            this.submitEvent.fire();
            return true;
        }
        else{
            return false;
        }
    };

    var onSuccess = function(obj) {
        try {
            var xmldoc = obj.responseXML;
            var root = xmldoc.getElementsByTagName('root').item(0);
            var status = root.getElementsByTagName("status")[0].firstChild.nodeValue;

            if(status == 'true'){
                dialog.form.reset();
                //dialog.hide();
                displayDialogStatusMessage(statusObj.id, STATUS_SUCCESS, 'Lightbox shared!');

            }
            else{
                var desc = root.getElementsByTagName('description')[0].firstChild.nodeValue;
                displayDialogStatusMessage(statusObj.id, STATUS_ERROR, desc);
            }        
        }
        catch(ex){
            alert('There was an error sending your message! ' + ex.message);
        }
    };

    var onFailure = function(obj) {
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, 'Your submission failed! Status: ' + obj.status);
    };

    dialog.callback.success = onSuccess;
    dialog.callback.failure = onFailure;
    dialog.cfg.queueProperty("buttons", buttons);
    dialog.render();

    return false;
}