//----------------- user detail site

function hideLayer(whichLayer) {
    if (document.getElementById) {
        // this is the way the standards work
        document.getElementById(whichLayer).style.display = "none";
    }
    else if (document.all) {
        // this is the way old msie versions work
        document.all[whichlayer].style.display = "none";
    }
    else if (document.layers) {
        // this is the way nn4 works
        document.layers[whichLayer].display = "none";
    }
}

function showLayer(whichLayer) {
    if (document.getElementById) {
        // this is the way the standards work
        document.getElementById(whichLayer).style.display = "block";
    }
    else if (document.all) {
        // this is the way old msie versions work
        document.all[whichlayer].style.display = "block";
    }
    else if (document.layers) {
        // this is the way nn4 works
        document.layers[whichLayer].display = "block";
    }
}

function handleClick(whichClick) {
	
    if (whichClick == "extend it") {
        showLayer("descPanel");
        hideLayer("descPanelShort");
    }
	
    if (whichClick == "shorten it") {
        hideLayer("descPanel");
        showLayer("descPanelShort");
    }
	
    if (whichClick == "showAbout it") {
        showLayer("aboutLayer");
    }
	
    if (whichClick == "hideAbout it") {
        hideLayer("aboutLayer");
    }

    if (whichClick == "hide it") {
        hideLayer("licensePanel");
    }
	
    else if (whichClick == "show it") {
        showLayer("licensePanel");
    }
}

//----------------- /user detail site

/*
 * String manipulation
 */
function trimString( str ) {
    if (typeof str != "string") {
        return str;
    }

    var myString = str;
    var zaehler = 0;

    // cut whitespace on the beginning

    while ( myString.substr( zaehler, 1 ) == " " ) {
        zaehler = zaehler + 1;
    }
    myString = myString.substring( zaehler, myString.length );

    // cut whitespace on the trailing

    zaehler = myString.length;
    while ( myString.substr( zaehler - 1 , 1 ) == " " ) {
        zaehler = zaehler - 1;
    }
    myString = myString.substring( 0, zaehler );

    return myString;
}

/*
 * Cookie functions
 */
function manageCookie(name) {
    var myCookieName = "DAMODO_" + name;
    var myCookies = document.cookie;

    myCookies = myCookies.split( ";" );
	
    var i = 0;
    while( i < myCookies.length ) {
        var myCookie = myCookies[ i ];

        myCookie = myCookie.split( "=" );

        if ( trimString( myCookie[ 0 ] ) == myCookieName ) {
            return( myCookie[ 1 ] );
        }
        i = i + 1;
    }
	
    return -1;
}

function setCookie( myCookie, myValue ) {
    var myCookieName = "DAMODO_" + myCookie;
    var myExpires = new Date();

    //myExpires.setTime( myExpires.getTime() + (365 * 24 * 60 * 60 * 1000));
    myExpires.setTime( myExpires.getTime() + (1 * 2 * 60 * 60 * 1000));

    document.cookie = myCookieName + "=" + myValue + "; path=/; expires=" + myExpires.toUTCString() + ";"; // right cookie path?
}

// this deletes the cookie when called
function deleteCookie( name, path) {
    var myCookieName = "DAMODO_" + name;
    var cookieString = myCookieName + "=;path=/" + path + "/;expires=Thu, 01-Jan-1970 00:00:01 GMT";        
    document.cookie = cookieString;
}


/*
 * Collapsible element functions
 */
var divSearchOptions    		 = 0x0100;

function getDivViewSetting( cookieStr ) {
    var myCookie = manageCookie( cookieStr );

    if ( -1 == myCookie ) {
        myCookie = 0xfff;
    } else {
        myCookie = parseInt( myCookie );
    }
    return myCookie;
}


function manageDivs( tagDiv, cookieBit, cookieName ) {
    var viewSettings = getDivViewSetting( cookieName );

    if( viewSettings & cookieBit ) {
        document.getElementById( tagDiv + "_open" ).style.display = "";
        document.getElementById( tagDiv + "_closed" ).style.display = "none";
    } else {
        document.getElementById( tagDiv + "_open" ).style.display = "none";
        document.getElementById( tagDiv + "_closed" ).style.display = "";
    }
}


function toggleDiv( tagDiv, cookieBit, cookieName ) {
    /*
	  Wie verwenden? 
		In der aufrufenden Datei muessen zwei DIV-Elemente vorhanden sein: Eines der ausgeklappte
		Zustand (BeliebigeID_open) und eines der geschlossene Zustand (BeliebigeID_closed)
		Beim Funktionsaufruf wird _open bzw. _closed weggelassen!

		DivAlteration(<div-Id>, divSearchOptions, <Coockie-Name>);return false;
	*/
    var viewSettings = getDivViewSetting(cookieName);

    viewSettings ^= cookieBit;
    setCookie( cookieName, viewSettings );
    manageDivs( tagDiv, cookieBit, cookieName );
}

function showDiv( tagDiv, cookieBit, cookieName ) {
    var viewSettings = getDivViewSetting( cookieName );
    manageDivs( tagDiv, cookieBit, cookieName );
}


function getCookie(name, defaultValue) {
    var cookie = manageCookie(name);
    if (cookie == -1) {
        setCookie(name, defaultValue);
        cookie = defaultValue;
    }
    return cookie;
}

// TODO: change style-toggling to http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
function toggleStyleAndDiv(linkId, styles, tagDiv, tagDiv2) {
    toggleStyle(linkId, styles);
    toggleStyleDivs( tagDiv );
    toggleStyleDivs( tagDiv2 );
}

/**
 * Will cycle through the css-styles given in styles.
 * @param linkId - the id of the <link> tag containing the href to the css
 * @param styles - an array of styles. styles[0] should be the default style
 *                 e.g. the one given in the href.
 * @return
 */
function toggleStyle(linkId, styles) {
    var idx = parseInt(getCookie("style_idx", 0));
    idx = (idx + 1)
    idx = idx % styles.length;
    document.getElementById(linkId).href = styles[idx];
    setCookie("style_idx", idx);
    setCookie("style_link", styles[idx]);
}

var win;
 
function printPreview(printArea) {
    var style = $('styleLink');
    var content = printArea;
    win = window.open("", 'popup', 'toolbar = no, status = no, resizable = yes, scrollbars = yes');
    win.focus();
    win.document.write("<html><head>");
    win.document.write('<link id="styleLink" rel="stylesheet" type="text/css" href="');
    win.document.write(style['href']);
    win.document.write('"/>');
    win.document.write("</head><body>");
    win.document.write('<div class="mainWrap">');
    win.document.write('<a href="" onclick="javascript:self.print();self.close();return false" titel="cancel">Print</a> <a href="" onclick="javascript:self.close();return false" titel="cancel">Cancel</a><br />');
    win.document.write('<div class="mainContent clearfix">');
    win.document.write('<div class="mainContent clearfix">');
    win.document.write('<div class="content accountStatement">');
    win.document.write(content);
    win.document.write('</div>');
    win.document.write('</div>');
    win.document.write('</div>');
    win.document.write('</div>');
    win.document.write("</body></html>");
    win.document.close();
}

function toggleStyleDivs( tagDiv ) {
    if (document.getElementById( tagDiv + "_open" )!=undefined) {
        var idx = parseInt(getCookie("style_idx", 0));
        if( idx == 0 ) {
            document.getElementById( tagDiv + "_open" ).style.display = "";
            document.getElementById( tagDiv + "_closed" ).style.display = "none";
        } else {
            document.getElementById( tagDiv + "_open" ).style.display = "none";
            document.getElementById( tagDiv + "_closed" ).style.display = "";
        }
    }
}

/**
 * Call this on page load to reset the style to what is saven in the cookie
 * @param linkId - the id of the <link> tag containing the href to the css
 * @param styles - an array of styles. styles[0] should be the default style
 *                 e.g. the one given in the href.
 * @return
 */
function restoreStyle(linkId) {
    var style = getCookie("style_link", "0");
    if (style != "0") {
        document.getElementById(linkId).href = style;
    }
}

function startList(elementId) {
    document.getElementById(elementId).style.display = 'block';
}

function closeList(elementId) {
    document.getElementById(elementId).style.display = 'none';
}

// we start to put functions, vars and objects by damodo in namespace 'damodo'

if (damodo == null) var damodo = {};
damodo.showPopup = function(el, init, title, w) {
    if (w==undefined) {
        w=375;
    }
    var dialog = $(el);
    dialog.visible=false;
    Modalbox.show(dialog, {
        title: title,
        overlayOpacity: .55,
        width: w,
        afterLoad: init,
        inactiveFade: false
    });
};
	
damodo.setValue = function(htmlId, value) {
    if (!$(htmlId).innerText) {
        // -> Firefox
        $(htmlId).textContent = value;
    } else {
        // -> IE
        $(htmlId).innerText = value;
    }
};

damodo.showSpinner = function() {
    var dialog = $('spinner');
    dialog.visible=false;
    Modalbox.show(dialog, {
        title: "In Progress",
        width: 75,
        overlayOpacity: .75,
        overlayClose: false
    });
};

damodo.showLayer = showLayer;
damodo.hideLayer = hideLayer;

