var mainCss = 'idea.css';

function observeWidths(func) {
    Event.observe(window, 'load', func);
    Event.observe(window, 'resize', func);
	var p = document.getElementById('pinIndicator');
	if (p && Element.visible(p)) {
	    Event.observe(p, 'click', func);
	    Event.observe(document.getElementById('pinIndicator2'), 'click', func);
	    Event.observe(document.getElementById('handle'), 'click', func);
	}
}

function getPageWidth() {
    var pageWidth = document.documentElement.offsetWidth;

    if (isShowingMainSidebar()) {
        pageWidth -= 195; // expanded sidebar width
    } else {
        pageWidth -= 2; // collapsed sidebar width
    }

    var p = document.getElementById('ideasProfileSidebar');
    if (p != null) {
        pageWidth -= p.offsetWidth; // profile sidebar width
    }

    return pageWidth - 90; // who knows what this 90 is for
}

function isShowingMainSidebar() {
    var collapsedSidebarPinIndicator = document.getElementById('pinIndicator');
    var sidebarIsCollapsible = collapsedSidebarPinIndicator != null && Element.visible(collapsedSidebarPinIndicator);
    var sidebarIsOpen = sidebarIsCollapsible && collapsedSidebarPinIndicator.className.indexOf('open') >= 0;

    if (!sidebarIsCollapsible) {
        return true;
    }
    return sidebarIsOpen;
}

function setWidthsForOverflowWithVoteButton(voteButtonId) {
    var voteButton = document.getElementById(voteButtonId);
    if (voteButton != null) {
        var ideaSidePadding = 20;
        var ideaSideOffsetWidth = ideaSidePadding + voteButton.offsetWidth;
        if (-1 != navigator.userAgent.indexOf ("MSIE")) {
            var lessWidthForIE6 = (-1 != navigator.userAgent.indexOf ("MSIE 6.0")) ? 24 : 0;
            setClassProp(mainCss, '.ideaSide', 'width', '80px');
		    setClassProp(mainCss, '.ideaContentWidth', 'width', (getPageWidth()-ideaSideOffsetWidth-lessWidthForIE6)+'px');
            setClassProp(mainCss, '.ideaContent', 'margin', '0 0 0 '+(ideaSideOffsetWidth)+'px');
            setClassProp(mainCss, '.ideaBody', 'padding', '10px 5px 16px 0'); // To cater for bottom padding taken up by scrollbar (add 16 px for scrollbar but subtract the original 10px) 
        } else {
            // Set the idea body content width, so that long images and URLs will trigger a horizontal scrollbar 
            var ideaContentWidth = getPageWidth()-ideaSideOffsetWidth;
            setClassProp(mainCss, '.ideaContentWidth', 'width', (ideaContentWidth)+'px');
            if (voteButton.offsetWidth > 95) {
                setClassProp(mainCss, '.ideaContent', 'margin', '0 0 0 '+(ideaSideOffsetWidth)+'px');
	    	}
	    }
    }
}

function setWidthsForOverflowWithRC() {
    if(-1 != navigator.userAgent.indexOf ("MSIE")) {
        /*
         * Set the comment body to a fixed width in IE so the comment text will be hidden if it overflows the width
         * -- IE 6 processes "overflow-x: hidden" only when the width is set in pixels.
         */
	    setClassProp(mainCss, '.ideaRCLastCommentBodyWidth', 'width', (getPageWidth()-245)+'px');
	}
}

function setClassProp(cssFile, cssClass, prop, value) {
  	var rulesKey = (document.styleSheets[0]['rules'] != null) ? 'rules' : 'cssRules';
	for (var i = 0; i < document.styleSheets.length; i++){
		var href = document.styleSheets[i].href;
		if (href != null && -1 != href.toLowerCase().indexOf(cssFile)) {
			for (var j = 0; j < document.styleSheets[i][rulesKey].length; j++) {
				if (document.styleSheets[i][rulesKey][j].selectorText == cssClass) {
					document.styleSheets[i][rulesKey][j].style[prop] = value;
				}
			}
		}
	}
}

function setVis(element, isVisible){
    element.style.visibility = isVisible ? 'visible' : 'hidden';
}

function drawPaginator(listId, containerIds, startRow, recordsPerPage, totalRecords, handler) {
	new Paginator({
	'listId':listId,
	'containerIds':containerIds,
	'currentPage':(startRow / recordsPerPage) + 1,
	'recordsPerPage':recordsPerPage,
	'totalRecords':totalRecords,
	'handler': function (pageNum) {
                    updateList((pageNum-1)*this.recordsPerPage);
                    this.setState({'currentPage': pageNum});
	   	       }
	});
}

function setElementDisplay(id, value) {
	var element = document.getElementById(id);
	if (element != null) element.style.display=value;
}

function toggleAllCheckboxes(formId, checked) {
    var inputs = document.getElementById(formId).getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'checkbox') {
            inputs[i].checked = checked;
        }
    }
}

function beforeSearchSuggestedDuplicates(isAccessibleMode) {
    setElementDisplay('similarResultsLoading', 'block');
    setElementDisplay('similarResultsContainer', 'none');
    if (isAccessibleMode) {
        document.getElementById('similarResultsLoading').focus();
    }
}

function afterSearchSuggestedDuplicates(isAccessibleMode) {
    setElementDisplay('similarResultsLoading', 'none');
    setElementDisplay('similarResultsContainer', 'block');
    if (isAccessibleMode) {
        var firstResultLink = document.getElementById('thePage:mainLayout:formIdea:pb:pbs:similarIdeasList:0:similarIdeaLink');
        if (firstResultLink != null) {
            document.getElementById('similarResultsText').focus();
        } else {
            document.getElementById('similarResultsNone').focus();
        }
    }
}

<!-- http://bugforce/bug/bugDetail.jsp?id=1000000000015PA -->
function fixInlineOpacityFunction() {
    if (-1 != navigator.userAgent.indexOf ("MSIE")) {
        Element.Methods.getInlineOpacity = function(element){
            return $(element).style.opacity || '';
        };
        Element.addMethods();
    }
}

