/**
 * The instance 'rhzSiteJs' is used for accessing members of 'RHZSiteJSObject' 
 */
var rhzSiteJs = new RHZSiteJSObject();
/**
 * RHZJS Object for encapsulating methods required by RHZ web
 */
function RHZSiteJSObject() {
    this.getAppURL = rhzsite_getAppURL;
    this.getCookie = rhzsite_getCookie;
    this.checkUserLoggedIn = rhzsite_checkUserLoggedIn;
    this.logout = rhzsite_logout;
    this.addBookmarkToVPLace = addBookmarkToVPLace;
}


/**
 * Create URL that access the applications domain for getting an application
 * specified by 'queryStr' preffix. Then set anchore's "href".
 *
 * @param queryStr relative path of for application's URL
 * @param field anchore field for setting the "herf" with target URL
 */
function rhzsite_getAppURL(queryStr, field) {
    var href = "http://" + location.hostname + ":8080/" + queryStr;
    field.setAttribute("href", href);
}

/**
 * Get cookie object with 'c_name' name
 *
 * @param c_name name of cookie
 */
function rhzsite_getCookie(c_name) {
    var c_start;
    var c_end;
    var ck = document.cookie;
    if (ck.length > 0) {
        c_start = ck.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = ck.indexOf(";", c_start);
            if (c_end == -1) c_end = ck.length;
            return unescape(ck.substring(c_start, c_end));
        }
    }
    return "";
}


/**
 * Check if cookie exist and if exist present the "Welocome" message
 */
function rhzsite_checkUserLoggedIn() {
    var session = rhzsite_getCookie("JSESSIONID");
    return session != null && session != "";
}


/**
 * Logout by deleting the 'JSESSIONID' cookie
 */
function rhzsite_logout() {
    deleteCookie("JSESSIONID", "/", document.domain);
}

/**
 * Delete cookie related with specified domain, by setting its expiry date to the past
 * @param name cookie name
 * @param path path
 * @param domain domain name
 */
function deleteCookie(name, path, domain) {
    if (rhzsite_getCookie(name)) {
        document.cookie = name + "=" +
                          ( ( path ) ? ";path=" + path : "") +
                          ( ( domain ) ? ";domain=" + domain : "" ) +
                          ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

/**
 * Open page with frame. This method is required since setting frame name
 * as target for 'href' tag, is not accepted by all browsers
 *
 * @param refPage ref page (self or parent)
 * @param frameName  name of frame
 * @param frameIndex index of frame in case that naming of frame is not supported
 * @param pageURL target page to open
 */
function openInFrame(refPage, frameName, frameIndex, pageURL) {
    try {
        refPage.frames[frameName].location.href = pageURL;
    } catch(ex) {
        refPage.frames[frameIndex].location.href = pageURL;
    }
}

function openFeedbackForm() {
    self.location.href = 'http://' + location.host + ':8080/rhz_web_tools/user_tools/rhzbrowser_feedback_form.jsp';
}

function addBookmarkToVPLace() {
    var bookmarkurl = "http://www.virtual-workplace.org";
    var bookmarktitle = "Rhizome NetWorks VPLace";
    if (document.all) {
        window.external.AddFavorite(bookmarkurl, bookmarktitle);
    } else if (window.sidebar) {
        window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
    }
}


function openFromSelfFeedbackForm() {
    self.location.href = 'http://' + location.host + ':8080/rhz_web_tools/user_tools/rhzbrowser_feedback_form.jsp';
}

function openFromSelfSignupForm() {
    self.location.href = 'http://www.virtual-workplace.org';
}


function handleMouseOnItem(item) {
    item.className = 'bullet_on';
    document.body.style.cursor = 'pointer';
}
function handleMouseOutOfItem(item) {
    item.className = 'bullet_off';
    document.body.style.cursor = 'default';
}

function openInContentIndex(refPage) {
    window.location = "http://" + location.host + "/content_index.html?" + refPage;
}




