/**
 * @author Marco Süß
 * @copyright Copyright 2008, ZWÖLF Medien GbR
 * @license MIT
 * 
 * Some functions of ZwoelfUtis.String are based on php.js (Copyright 2008 Kevin van Zonneveld and others)
 * More info at: http://kevin.vanzonneveld.net/techblog/category/php2js
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MARCO SÜß OR ZWÖLF MEDIEN GBR BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
var ZwoelfUtils = new Object();
ZwoelfUtils.String = {
    replaceUmlauts : function() {
        // this requires prototype.js
        var newString = this;
        var nonLatinMap = $H({
            'ä': 'ae',
            'Ä': 'Ae',
            'ö': 'oe',
            'Ö': 'Oe',
            'ü': 'ue',
            'Ü': 'Ue',
            '&auml;': 'ae',
            '&Auml;': 'Ae',
            '&ouml;': 'oe',
            '&Ouml;': 'Oe',
            '&uuml;': 'ue',
            '&Uuml;': 'Ue'
        });
        nonLatinMap.keys().each(function(character){
           newString = newString.gsub(character, nonLatinMap.get(character));
        });
        return newString;
    },
    uc : function() { //just a shorthand
        return this.toUpperCase();
    },
    lc : function() { //just a shorthand
        return this.toLowerCase();
    },
    dc : function() { //just a shorthand
        return this.toLowerCase();
    },
    ucfirst : function() {
        return this.charAt(0).toUpperCase() + this.substr(1, this.length-1);
    },
    ucwords : function() {
        return this.replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
    },
    forceUnderscore : function() {
        return this.toLowerCase().replace(/\s/g, '_');
    },
    forceCamelCase : function() {
        // this requires prototype.js
        return this.forceUnderscore().camelize();
    },
    onlyWord : function() {
        return this.replace(/\W/g, '');
    },
    removeEntities : function() {
        return this.replace(/&[^;]*;/g, '');
    },
    trim : function(charlist) {
        charlist = !charlist ? ' \\s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
        var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
        return this.replace(re, '');
    },
    basename : function() {
        return this.replace(/^.*\/([^\/]*$)/g, '\$1')
    },
	setCommand : function(newcommand) {
		return this.replace(/(:.*)$/, ':' + newcommand);
	}
};

$extend(window.String.prototype, ZwoelfUtils.String);

/* BrowserDetect by quirksmode */
var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
        {   string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            versionSearch: "Version",    // Works for Safari 3
            identity: "Safari"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {       // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {       // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS : [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};
BrowserDetect.init();
/*alert(BrowserDetect.browser);
alert(BrowserDetect.version);*/
/*alert(navigator.userAgent);
alert(navigator.appVersion);

if(navigator.userAgent.indexOf('Version/3') != -1) alert('scheinbar safari 3');*/
ZwoelfUtils.Cookie = {
    set : function(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";// + document.location.href.replace(/http:\/\/[^\/]+\//, '');
    },
    
    get : function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    
    erase : function(name) {
        createCookie(name,"",-1);
    }
}

