/* Copyright IBM Corp. 2007  All Rights Reserved.                    */
var dojoEditWidget = null;
var Tango = {
    editFormRefCount : 0,

    Util: {
        constructUrl: function(url, params) {
            var query = '';
            for (var p in params) {
                if (query.length > 0)
                    query += "&";
                query += encodeURIComponent(p);
                query += "=";
                query += encodeURIComponent(params[p]);
            }

            if (query.length > 0)
                url += "?" + query;

            return url;
        },

        getCookie: function(name) {
            key = name + "=";
            c = document.cookie;

            // set start of cookie
            start = c.indexOf("; " + key) + 2 + key.length;
            start = start > -1 + 2 + key.length ? start : c.indexOf(name + "=") + key.length;

            // set end of cookie if start is found
            if (start > -1 + key.length) {
               end = c.indexOf(";", start);
               end = end > -1 ? end : c.length;

               // return cookie value
               return unescape(c.substring(start , end));
            } else {
               // no cookie
               return null;
            }
        },

        setCookie: function(name, value) {
            document.cookie = name + "=" + escape(value) +  "; expires=" +
                new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString() + "; path=/";
        }
    }
};

function displayFeedEditForm(parentId, feedId) {
    if (Tango.editFormRefCount > 0) {
        // alert ("Sorry, multiple edit forms are disallowed. \nCancel or Submit an existing form first.");  
		return;                      
    }
	else {
	
	    //first clone the form and setup the actions, ids, etc..
	    var template = document.getElementById('editForm');
	    var newForm = template.cloneNode(true);
	    var parentNode = document.getElementById(parentId);
	    newForm.id = 'editForm'+'_'+feedId;
	    var uuid_el = getElementById(newForm, 'feed_uuid');
	    if (uuid_el != null) {
	        uuid_el.id = uuid_el.id + '_' + feedId;
	        uuid_el.value = feedId;
	    }
	
	    var type_el = getElementById(newForm, 'feed_type');
	    if (type_el != null) {
	        type_el.id = type_el.id + '_' + feedId;
	    }
	
	    var tag_choices = getElementById(newForm, 'autocompletetags_choices_edit');
	    if (tag_choices != null) {
	        tag_choices.id = tag_choices.id + '_' + feedId;
	    }
	
	    var tag_input = getElementById(newForm, 'autocompletetags_edit');
	    if (tag_input != null) {
	        tag_input.id = tag_input.id + '_' + feedId;
	    }
	
	    var cancelBtn = getElementById(newForm, 'cancelBtn');
	    var cancelCallBack = function onclick(event) {
	        destroyFeedEditForm(parentNode,newForm);
	        return false;
	    };
	    cancelBtn.onclick = cancelCallBack;
	
	    //now fill the form with data from DOM
	    var name = getElementById(newForm, 'editFeedName');
	    var editName = document.getElementById('b_name_'+feedId);
	    if ((name != null) && (editName != null)) {
	        if (editName.textContent != undefined) {
	            name.value = editName.textContent;
	        }
	        else {
	            name.value = editName.innerText;
	        }
	    }
	    var uri = getElementById(newForm, 'editFeedUrl');
	    var editUri = document.getElementById('b_uri_'+feedId);
	    if ((uri != null) && (editUri != null))
	        uri.value = editUri.getAttribute("href");
	    var desc =  getElementById(newForm, 'editFeedDescription');
	    var editDesc = document.getElementById('b_description_'+feedId);
	    if ((desc != null) && (editDesc != null)) {
	        if (editName.textContent != undefined) {
	            desc.value = trim(editDesc.textContent);
	        }
	        else {
	            desc.value = trim(editDesc.innerText);
	        }
	    }
	
	    var editTags = document.getElementById('b_tags_'+feedId);
	    if ((tag_input != null) && (editTags != null)) {
	        if (editTags.textContent != undefined) {
	            tag_input.value = editTags.textContent;
	        }
	        else {
	            tag_input.value = editTags.innerText;
	        }
	    }
	    //add the form to DOM
	    parentNode.appendChild(newForm);
	
	    var homepage =  getElementById(newForm, 'homepage');
	    var editHomepage = document.getElementById('b_homepage_'+feedId);
	    if ((homepage != null) && (editHomepage.innerHTML == 'true')) {
	        homepage.checked = true;
	    }
	
	    new Ajax.Autocompleter(tag_input.id, tag_choices.id , "autoCompleteTags.do", {paramName: "tag", tokens: [' ',  '\n', '\r', '\f', '\t', ',', '\u3000']});
	    Tango.editFormRefCount++;
	}	    
 }
function toggleFeedDisplay(showBtnId, hideBtnId, feedDivId, feedObj, display) {
    var showBtn = document.getElementById(showBtnId);
    var hideBtn = document.getElementById(hideBtnId);
    var feedDiv = document.getElementById(feedDivId);
    if (display == 'show') {
        if (showBtn != null) {
            showBtn.className = "hide";
        }
        if (hideBtn != null) {
            hideBtn.className = "show";
        }
        if (feedDiv != null) {
        	feedObj.loadThread();
            feedDiv.className = "show";
        }
    }
    else {
        if (showBtn != null) {
            showBtn.className = "show";
        }
        if (hideBtn != null) {
            hideBtn.className = "hide";
        }
        if (feedDiv != null) {
            feedDiv.className = "hide";
        }
    }
}

function displayBookmarkEditForm(parentId, bookmarkId) {    
	if (Tango.editFormRefCount > 0) {
        // alert ("Sorry, multiple edit forms are disallowed. \nCancel or Submit an existing form first.");                
       	return;  
    }
	else {
	
	    // First clone the form and setup the actions, ids, etc..
	    var template = document.getElementById('editForm');
	    var newForm = template.cloneNode(true);
	    var parentNode = document.getElementById(parentId);
	    newForm.id = 'editForm'+'_'+bookmarkId;
	    var uuid_el = getElementById(newForm, 'bookmark_uuid');
	    if (uuid_el != null) {
	        uuid_el.id = uuid_el.id + '_' + bookmarkId;
	        uuid_el.value = bookmarkId;
	    }
	
	    var type_el = getElementById(newForm, 'bookmark_type');
	    if (type_el != null) {
	        type_el.id = type_el.id + '_' + bookmarkId;
	    }
	
	    var tag_choices = getElementById(newForm, 'autocompletetags_choices_edit');
	    if (tag_choices != null) {
	        tag_choices.id = tag_choices.id + '_' + bookmarkId;
	    }
	
	    var tag_input = getElementById(newForm, 'autocompletetags_edit');
	    if (tag_input != null) {
	        tag_input.id = tag_input.id + '_' + bookmarkId;
	    }
	
	    var cancelBtn = getElementById(newForm, 'cancelBtn');
	    var cancelCallBack = function onclick(event) {
	        destroyBookmarkEditForm(parentNode,newForm);
	        return false;
	    };
	    cancelBtn.onclick = cancelCallBack;
	
	    // Now fill the form with data from DOM
	    var name = getElementById(newForm, 'editBookmarkName');
	    var editName = document.getElementById('b_name_'+bookmarkId);
	    if ((name != null) && (editName != null)) {
	        if (editName.textContent != undefined) {
	            name.value = editName.textContent;
	        }
	        else {
	            name.value = editName.innerText;
	        }
	    }
	    var uri = getElementById(newForm, 'editBookmarkUrl');
	    var editUri = document.getElementById('b_uri_'+bookmarkId);
	    if ((uri != null) && (editUri != null))
	        uri.value = editUri.getAttribute("href");
	    var desc =  getElementById(newForm, 'editBookmarkDescription');
	    var editDesc = document.getElementById('b_description_'+bookmarkId);
	    if ((desc != null) && (editDesc != null)) {
	        if (editName.textContent != undefined) {
	            desc.value = trim(editDesc.textContent);
	        }
	        else {
	            desc.value = trim(editDesc.innerText);
	        }
	    }
	    var editTags = document.getElementById('b_tags_'+bookmarkId);
	    if ((tag_input != null) && (editTags != null)) {
	        if (editTags.textContent != undefined) {
	            tag_input.value = editTags.textContent;
	        }
	        else {
	            tag_input.value = editTags.innerText;
	        }
	    }
	    //add the form to DOM
	    parentNode.appendChild(newForm);
	
	    //the checkbox is has to be updated AFTER the appendChild because of IE, which ignores checkbox changes before insertion!
	    var homepage =  getElementById(newForm, 'homepage');
	    var editHomepage = document.getElementById('b_homepage_'+bookmarkId);
	    if ((homepage != null) && (editHomepage.innerHTML == 'true')) {
	        homepage.checked = "true";
	    }
	
	    new Ajax.Autocompleter(tag_input.id, tag_choices.id , "autoCompleteTags.do", {paramName: "tag", tokens: [',', ' ']});
	
	    Tango.editFormRefCount++;
	}	    
}

function destroyBookmarkEditForm(parentNode, formNode) {
    parentNode.removeChild(formNode);
    Tango.editFormRefCount--;
}

function destroyFeedEditForm(parentNode, formNode) {
    parentNode.removeChild(formNode);
    Tango.editFormRefCount--;
}

// Find child element by id.
function getElementById(parentNode, tagId) {
    var retval = null;
    var childTags = parentNode.getElementsByTagName("*");
    for (var ii=0; ii<childTags.length; ii++) {
        if (childTags[ii].id == tagId) {
            retval = childTags[ii];
            break;
        }
    }
    return (retval);
}
function selectTab(tabSelectedId) {
    var tabSelected = dojo.byId(tabSelectedId);

    var tabsContainer = dojo.dom.getFirstAncestorByTag(tabSelected, 'ul');
    var tab = dojo.dom.getFirstChildElement(tabsContainer, 'li');

    do {
        dojo.html.removeClass(tab, 'selected');
        var tabContentId = tab.id+'Content';
        dojo.html.hide(tabContentId);
        tab = dojo.dom.getNextSiblingElement(tab, 'li');
    } while (tab != undefined);

    dojo.html.setClass(tabSelected, 'selected');
    dojo.html.show(tabSelectedId+'Content');
}
function toggleDisplay(showId, hideId) {
    dojo.html.show(showId);
    dojo.html.hide(hideId);
}
function displayCommunityCreate() {
    Element.toggle('newCommunityForm');
    initEditor('newCommunityForm','addCommunityDescription');
}
function cancelCommunityCreate() {
    clearCommunityForm();
    Element.toggle('newCommunityForm');
}
function clearCommunityForm(){
    var name =  document.getElementById('addCommunityName');
    if (name != null) {
        name.value ="";
    }
    var publicAccess =  document.getElementById('addPublicAccess');
    if (publicAccess != null) {
        publicAccess.checked = true;
    }
    var autoMembers = document.getElementById('autocompletemembers');
    if (autoMembers != null) {
        autoMembers.value ="";
    }
    var desc = document.getElementById('addCommunityDescription');
    if (desc != null) {
        desc.value = "";
    }
    if (dojoEditWidget != null) {
        dojoEditWidget.close(false);
        dojoEditWidget = null;
    }
    var photo = document.getElementById('photoId');
    if (photo != null) {
        photo.value ="";
    }
    var autoTags = document.getElementById('autocompletetags');
    if (autoTags != null) {
        autoTags.value ="";
    }
}
function displayCommunityEdit() {
    Element.toggle('editCommunityForm');
    Element.toggle('colRight');
    Element.toggle('communityMetadata');
    Element.toggle('communityPage');
    initEditor('editCommunityForm', 'editcommunityDescription');
}
function initEditor(form, editor){
    var editForm = dojo.byId (form);
    if (editForm != null) {
        if (editForm.style.display != 'none' && dojoEditWidget == null) {
            dojoEditWidget = dojo.widget.createWidget("Editor2", {height:100, postRteFocus: 'postRteFocusPlaceholder', htmlEditing: true, shareToolbar: false, toolbarAlwaysVisible: true,
                                                      toolbarTemplatePath:"src/widget/templates/EditorToolbarOnelineBrief.html"}, dojo.byId (editor));
        }
    }
}
function displayPhoto(editFlag) {
    dojo.html.hide('photoUploadLink');
    dojo.html.show('photoUploadHelp');
    dojo.html.show('photoUpload');
    if (editFlag) {
        dojo.html.show('photoUploadRemove');
    }
}
function togglePhotoRemove(removeCtl) {

    var uploadCtl = document.getElementById("photoId");
    if(removeCtl.checked) {
        uploadCtl.value="";
        uploadCtl.disabled = true;
    }
    else {
        uploadCtl.disabled = false;
    }
}
