// CONFIRM PROMPT
/* <a href="http://www.google.com/" onclick="return confirmPrompt(this.href, 'Are you sure you want to?');">Test</a> */
function confirmPrompt(url, msg) {
    if (confirm(msg)) window.location = url;
    return false;
}

// POPUP
/* <a href="http://www.google.com/" onclick="return doPopup(this.href, {location:'yes'});">Test</a> */
function doPopup(url, options) {
    var Popup = {
        open: function(url, options) {
            this.options = {
                width: 600,
                height: 500,
                name: '_blank',
                location: 'no',
                menubar: 'no',
                toolbar: 'no',
                status: 'yes',
                scrollbars: 'yes',
                resizable: 'yes',
                top: '',
                left: '',
                center: true
            }
            Object.extend(this.options, options || {});
            this.options.width = this.options.width < screen.availWidth ? this.options.width : screen.availWidth;
            this.options.height = this.options.height < screen.availHeight ? this.options.height : screen.availHeight;
            if (this.options.center) {
                this.options.top = (screen.height - this.options.height) / 2 ;
                this.options.left = (screen.width - this.options.width) / 2;
            }
            var openoptions = 'width=' +this.options.width+ ',height=' +this.options.height+ ',location=' +this.options.location+ ',menubar=' +this.options.menubar+ ',toolbar=' +this.options.toolbar+ ',scrollbars=' +this.options.scrollbars+ ',resizable=' +this.options.resizable+ ',status=' +this.options.status
            if (this.options.top != '') openoptions += ',top=' +this.options.top;
            if (this.options.left != '') openoptions += ',left=' +this.options.left;
            window.open(url, this.options.name, openoptions);
            return false;
        }
    }
    return Popup.open(url, options);
}

// HANDLE AJAX JSON RESPONSE
function handleResponse(transport) {
    var content = (transport.responseText).evalJSON();
    Object.keys(content.elements).each(function(item) {
        $(item).update(content['elements'][item]);
    });
    Object.keys(content.javascript).each(function(item) {
        eval( content['javascript'][item]);
    });
}

// SET COOKIE
function setCookie(c_name, value, expiredays, path) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires=" +exdate.toGMTString()) + ";path=" +path;
}

// GET COOKIE
function getCookie(name) {
    var search = name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // COOKIE EXISTS
        if (offset != -1) {
            offset += search.length
            // SET INDEX OF BEGINNING OF VALUE
            end = document.cookie.indexOf(";", offset);
            // SET INDEX OF END OF COOKIE VALUE
            if (end == -1) end = document.cookie.length;
            returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}


/*** CUSTOM FUNCTIONS ***/


var ln_openid = '';

function leftNav_init() {

    $$('a.leftnav').each(function(item) {

        Event.observe(item, 'click', function() {
            leftNav_click(item);
        });

        if (item.hasClassName('selected')) {
            ln_openid = item.id;
        }

    });

}

function leftNav_click(item) {

    if (ln_openid) {

        if ($(ln_openid+ '_links')) {
            Effect.toggle(ln_openid+ '_links', 'slide', {
                duration: .5
            });
        }

        $(ln_openid).removeClassName('selected');

    }

    if (item.id == ln_openid) {

        item.removeClassName('selected');

        ln_openid = '';

    } else {

        ln_openid = item.id;

        item.addClassName('selected');

    }

    Effect.toggle(item.id+ '_links', 'slide', {
        duration: .5
    });

}


// ON PAGE LOAD
Event.observe(window, 'load', function() {

    leftNav_init();

});