var sid;
var dokument;
var mid;
var cbapi=0;
var div;
var url;
var from;
var rpcrlf=/[\n\r\n\r]/g;
var cr="\n";
var ExitStop=false;
var ExitCounter=-1;
var ExitContent=new Array();
var ExitHover=false;
var ClickUnblog=true;
var MSGClass='black-on-white';
var Form='';
var FormFields=new Array();
var FormData;
var CBArticleID='';

//$(window).unload( function () {
//    URL Prüfen!
//    if(!ExitHover && ExitCounter==0) {
//        window.location=window.location.pathname;
//    }
//});

window.onload = function() {
    var objScript = document.getElementById("jscms");
    if(objScript) {
        var strSrc = objScript.src;
        strSrc = strSrc.substring(strSrc.indexOf("?")+1, strSrc.length);
        var arrHlp = strSrc.split("&");
        var GET = [];
        for(var strEntry in arrHlp){
            var arrH = arrHlp[strEntry].split("=");
            GET[arrH[0]] = arrH[1];
        }
        if(GET['sid'])
            sid=GET['sid'];
        if(GET['mid'])
            mid=GET['mid'];
        if(GET['dokument'])
            mid=GET['dokument'];
    }
    if($.fn.jeegoocontext) {
        $(function(){
            $('.CBMenueContex').jeegoocontext('CBContextMenu', {
                startTopOffset: 5,
                openBelowContext: 3,
                    onShow: function(e, context){
                        CBMenueContex=$(context).attr('id');
                        CBMenueContex=CBMenueContex.substr(13);
                    }
            });
        });
    }
    $('.SetLen').keyup(function() {
        name=$(this).attr('name')+'Len';
        $('#'+name).text($(this).val().length);
    });
    
    $("body").hover(function() {
        ExitHover=true;
    } , function() {
        if(!ExitContent[ExitCounter] || ExitStop)
            return;
        temp=ExitContent[ExitCounter];
        if(temp.substr(0,1)=='#')
            window.location=temp.substr(1);
        else {
            CMSMessage(temp,false);
        }
        ExitCounter++;
    }
    );
    
    $('.FormCheck').hover(function() {
        if(Form!=$(this).attr('id'))
            FormCheck($(this).attr('id'),true);
    });
    
    $(".cmMenueEditContent").click(function() {
        if(CBMenueContex)
            id=CBMenueContex;
        else
            id=mid;
        CBMenueContex='';
        if(CBArticleID)
            id=CBArticleID;
        CBArticleID='';
        location='/index.php?abe='+sid+'&mid='+id+'&MCEdit='+id;
    })
    $(".ArticleEdit").click(function() {
        alert($(this).parent().attr(is));

    })
    $(".cmMenueArchiv").click(function() {
        location='/index.php?abe='+sid+'&mid='+mid+'&MCEdit='+mid+'&archiv=1';
    })
};

$(function() {
    $(".cDatei").click(function(){
        GetDatei($(this).attr('id'));
    });
});

var Last='';

function GetDatei(id) {
    temp=id.split('|');
    if(!temp[1] || Last==id) return;
    Last=id;
    $.ajax({
        url: '?ajax=1&download='+id,
        success: function(html) {
            Last='';
            temp=html.substr(0, 1);
            html=html.substr(1);
            if(temp=='#')
                CMSMessageAktion=true; else
                CMSMessageAktion=false;
            if(temp=='!' || temp=='#')
                CMSMsg(html);
        }
    });
}

function social(vurl,vdiv) {
    url=vurl;
    div=vdiv;
    $.ajax({
        url: '/social.htm?ajax=1',
        success: function(html) {
            $.msg({
                bgPath : '/shared/images/',
                clickUnblock : ClickUnblog,
                klass : MSGClass ,
                autoUnblock : false,
                content : html,
                z:100000
            });
            // divs nun tauschen
            $('#URL2').val(url);
            temp=$(div).html();
            temp=stripHTML(temp);
            $('#Text2').val(temp);
        }
    });
}

var oMSG;
var CMSMessageAktion=false;
function unblock() {
    oMSG.unblock()
}
function CMSMsg(msg) {
    if(ExitStop) {
        oMSG.replace(html);
        return;
    }
    if(msg.substr(0,1)=='?') {
        msg=msg.substr(1);
        msg=decodeURIComponent(msg);
        
    }
    $.msg({
        msgID : 1,
        bgPath : '/shared/images/',
        klass : MSGClass ,
        clickUnblock : ClickUnblog,
        autoUnblock : CMSMessageAktion,
        content : msg,
        msg : function() {
            ExitStop=true;
            $( '#CLOSE' ).bind( 'click', function(){
                oMSG.unblock();
            });
            oMSG=this;
        },
        beforeUnblock : function() {
            ExitStop=false;
        }
    });
}

function CMSMessageDiv(url, div, add, unlock) {
    if(!unlock)
        unlock=false;
    if(!add)
        add=false;
    CMSMessageAktion=unlock;
    if(url.indexOf('?')==-1)
        url+="?ajax=1&abe="+sid; else
        url+="&ajax=1&abe="+sid;
    $.ajax({
        url: url,
        success: function(html) {
            if(!add)
                $('#'+div).html(html); else
                $('#'+div).html($('#'.CMSMessageDivTemp).html()+html);
        }
    });
}

function CMSOrder(Artikel,ArtikelID,Menge,Art) {
    //Artikel=Pos
    unlock=false;
    if(!ArtikelID)
        ArtikelID=0;
    else
        Artikel='';
    if(!Art)
        Art='wk';
    if(!Menge)
        Menge=1;
    var jetzt = new Date();
    CMSMessageAktion=unlock;
    url='/std.ajax.order.shop.htm?id='+ArtikelID+'&artikel='+encodeURI(Artikel)+'&art='+Art+'&menge='+Menge+'&abe='+sid+'&time='+jetzt.getTime();
    if(cbapi>0)
        url+='&cbapi='+cbapi;
    $.ajax({
        url: url,
        success: function(html) {
            CMSMsg(html);
        }
    });
}

function CMSOrderPost(mid2,Artikel) {
    unlock=false;
    var jetzt = new Date();
    CMSMessageAktion=unlock;
    $.ajax({
        url: '/index.php?time='+jetzt.getTime(),
        type: 'POST',
        data: encodeURI('ajax=1&Dokument=std.ajax.multiorder.shop.htm&abe='+sid+'&mid'+mid2+'&Artikelliste='+Artikel),
        success: function(html) {
            CMSMsg(html);
        }
    });
}

function CMSMessage(url, unlock) {
    if(!unlock)
        unlock=false;
    CMSMessageAktion=unlock;
    if(url.indexOf('?')==-1)
        url+="?ajax=1&abe="+sid; else
        url+="&ajax=1&abe="+sid;
    $.ajax({
        url: url,
        success: function(html) {
            CMSMsg(html);
        }
    });
}

function CMSMessageExt(url, unlock) {
    if(!unlock)
        unlock=false;
    $.ajax({
        url: "/shared/php/get.php?g="+encodeURIComponent(url),
        success: function(html) {
            CMSMsg(html);
        }
    });
}
function CMSOrderExt(Artikel,url) {
    unlock=false;
    var jetzt = new Date();
    CMSMessageAktion=unlock;
    if(!url)
        url='http://www.cyberbase.net';
    url+='/std.ajax.order.shop.htm?artikel='+Artikel+'&art=wk&menge=1&abe='+sid+'&cbex=1&time='+jetzt.getTime();
    CMSMessageExt(url);
}

function CMSMessagePost(url, parameter, unlock) {
    if(!unlock)
        unlock=false;
    CMSMessageAktion=unlock;
    if(url=='social')
        url="/index.php?ajax=1"; else 
        url="/index.php?ajax=1";
    $.ajax({
        url: encodeURI(url),
        type: 'POST',
        data: encodeURI('ajax=1&form='+form+'&abe='+sid+'&'+parameter),
        success: function(html) {
            if(html=='x')
                return;
            if(html=='#') {
                window.location=html.substr(1);
                return;
            }
            CMSMsg(html);
        }
    });
}
var CMSAjaxHTML;
function CMSAjax(url,parameter) {
    CMSAjaxHTML='';
    if(parameter.indexOf('?')==-1)
        parameter+="?ajax=1&abe="+sid; else
        parameter+="&ajax=1&abe="+sid;
    $.ajax({
        url: url+encodeURI(parameter),
        success: function(html) {
            CMSAjaxHTML=html;
        }
    });
}

function stripHTML(str){
    // remove all string within tags
    var tmp = str.replace(/(<.*['"])([^'"]*)(['"]>)/g, 
        function(x, p1, p2, p3) {
            return  p1 + p3;
        }
        );
    // now remove the tags
    return tmp.replace(/<\/?[^>]+>/gi, '');
}

function trim (str) {
    return str.replace(/[\n\r]/g, '').replace(/ +/g, ' ').replace(/^\s+/g, '').replace(/\s+$/g, '');
}

var antwortwin;
var urlq;
var ContentID;
function Kommentar(id) {
    var jetzt = new Date();
    ContentID=id;
    urlq=window.location.pathname;
    urlq = urlq.replace("#","");
    url=urlq+'?abe='+sid+'&getform='+form+'&id='+id+'&time='+jetzt.getTime();
    $.ajax({
        url: encodeURI(url),
        success: function(html) {
            temp=html.split('|||');
            content=temp[1];
            $.msg({
                bgPath : '/shared/images/',
                autoUnblock : false,
                afterBlock : function(){
                    antwortwin = this;
                },
                content : content
            });
        }
    });
    return;
}

function KommentarStore(data) {
    var jetzt = new Date();
    antwortwin.unblock();
    url='/index.php?mid='+mid+'&Dokument='+dokument+'&time='+jetzt.getTime();
    $.ajax({
        url: encodeURI(url),
        type: 'POST',
        data: encodeURI('form='+form+'&abe='+sid+'&store=1'+data+'&id='+ContentID),
        success: function(html) {
            temp=html.split('|||');
            div=temp[1];
            content=temp[2];
            if(content!='x') {
                $(div).append(content);
            }
        }
    });
}

function checkName(Name) {
    if(Name.length<4)
        return false;
    Name=Name.toLowerCase();
    var reg = /[aeiouyjx]/;
    return reg.exec(Name);
}

function checkText(Text) {
    if(Text.length<8)
        return false;
    Text=Text.toLowerCase();
    var reg = /[aeiou]/;
    return reg.exec(Text);
}

function checkEmail(email) {
    var strReg = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+\.+([a-zA-Z0-9]{2,4})+$";
    var regex = new RegExp(strReg);
    return(regex.test(email));
}
function checkString(value,min,max) {
    if(min && value.length<min)
        return false;
    if(max && value.length>max)
        return false;
    return true;
}

function msgAlert(msg) {
    $.msg({
        bgPath : '/shared/images/',
        klass : MSGClass ,
        autoUnblock : false,
        content : msg
    });
}


function FormCheck(FormAkt,pre) {
    Form=FormAkt;
    var x=0;
    var ok=true;
    if(pre==true) {
        document.getElementById(Form).reset();
        $('#'+Form).submit(function() {
            return FormCheck(Form);
        });
    }
    FormData=$('#'+Form).serializeArray();
    
    //temp=''; for (var i = 0; i < FormData.length; i++)  temp+=FormData[i].name+' '+FormData[i].value+"\n"; alert(temp);
    
    $('#'+Form).find("input,textarea,select").each(function(i,v) { 
        ev = $(v);
        if(ev.attr('id') && v.type!='submit') {
            id=ev.attr('id');
            if(pre) {
                $('#I'+id).remove();
                FormFields[x]=new Object();
                FormFields[x].id=id;
                FormFields[x].typ=v.type;
                FormFields[x].name=v.name;
                FormFields[x].wert=ev.val();
                FormFields[x].bg=ev.css("background-color");
                FormFields[x].color=ev.css("color");
                x++;
                FormCheckPre(id);
            } else {
                temp=FormCheckDo(id);
                if(!temp)
                    ok=false;
            }
        }  
    });   
    return ok;
}

function FormCheckPre(id) {
    ev=$('#'+id);
    temp=FormCheckFind($(this).attr('id'));
    if(FormFields[temp]['typ']=='radio' || FormFields[temp]['typ']=='checkbox' || FormFields[temp]['typ']=='select-one') {
        ev.change(function() {
            FormCheckDo(id);
        }); 
    } else {
        ev.focusout(function() {
            FormCheckDo(id);
        });
        ev.focusin(function() {
            s=$(this).attr('id');
            if(s.substr(0,1)=='_')
                s=s.substr(1);
            $('#I'+s).remove();
            temp=FormCheckFind($(this).attr('id'));
            if(temp<0 || FormFields[temp]['typ']=='radio' || FormFields[temp]['wert']!=$(this).val())
                return;
            $(this).val('');
        });    
    }
}

function FormCheckFind(id) {
    for (var i = 0; i < FormFields.length; i++) {
        if(FormFields[i].id==id)
            return i;
    }
    return -1;
}
function FormCheckFindValue(Name) {
    for (var i = 0; i < FormData.length; i++) {
        if(FormData[i].name==Name)
            return FormData[i].value;
    }
    return '';
}
function FormCheckDo(id) {
    if(id.substr(0,1)=='_')
        id=id.substr(1);
    ev=$('#'+id);
    temp=ev.attr('id');
    x=temp.indexOf('-');
    if(x==-1) return true;
    FormData=$('#'+Form).serializeArray();
    check=temp.substr(0,x);
    text=temp.substr(x+1);
    temp=check+'_______';
    check=temp.split('_');
    ok=true;
    temp=FormCheckFind(ev.attr('id'));
    if(temp==-1)
        return true;
    value=FormCheckFindValue(FormFields[temp]['name']);
    if((FormFields[temp]['typ']=='text' || FormFields[temp]['typ']=='textarea') && FormFields[temp]['wert']==value) {
        ok=false
    } else {
        if(check[0]=='email') ok=checkEmail(value);
        else if(check[0]=='name') ok=checkName(value);
        else if(check[0]=='text') ok=checkString(value,check[1],check[2]);
        else if(check[0]=='num') ok=checkNum(value,check[1],check[2]);
        else if(check[0]=='empty') {
            if(value=='') ok=false;
        }
        else if(check[0]!=value) ok=false;
    }
    $('#I'+id).remove();
    ev.css("background-color", FormFields[temp]['bg']);
    ev.css("color", FormFields[temp]['color']);
    if(ok) return true;
    text=text.replace(/_/g, ' ');
    text=text.replace(/AA/g, '!');
    if(FormFields[temp]['typ']=='text' || FormFields[temp]['typ']=='textarea') {
        ev.css("background-color",'#ff0000');
        ev.css("color",'#ffffff');
        
        FormFields[temp]['wert']=text;
        ev.val(text);
        return false;
    }
    p=ev.offset();
    $("body").append('<div class="cFormCheck" id="I'+id+'" style="position:absolute;z-index:10000;">'+text+'</div>');
    $('#I'+id).css( {
        "left": (p.left) + "px", 
        "top": (p.top) + "px"
    });
    $('#I'+id).hover(function() {
        $('#I'+id).remove();
    });
    return false;
}



// TABS
var LastTab;
var TabLast=false;
$(document).ready(function() {

    //When page loads...
    $(".tab_content").hide(); //Hide all content
    if(!TabLast) {
        if(LastTab) {
            $('#'+LastTab).fadeIn();
        } else {
            $("ul.tabs li:first").addClass("active").show(); //Activate first tab
            $(".tab_content:first").show(); //Show first tab content
        }
    } else {
        $("ul.tabs li:last").addClass("active").show(); //Activate last tab
        $(".tab_content:last").show(); //Show last tab content
    }

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();

        var activeTab = $(this).find("a").attr("href");
        $(activeTab).fadeIn();
        return false;
    });
});

jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
            ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) {
        return s;
    } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

$.fn.center=function(a){
    var b=$(window),c=b.scrollTop();
    return this.each(function(){
        var f=$(this),e=$.extend({
            against:"window",
            top:false,
            topPercentage:0.5
        },a),d=function(){
            var h,g,i;
            if(e.against==="window"){
                h=b;
            }else{
                if(e.against==="parent"){
                    h=f.parent();
                    c=0;
                }else{
                    h=f.parents(against);
                    c=0;
                }
            }
            g=((h.width())-(f.outerWidth()))*0.5;
            i=((h.height())-(f.outerHeight()))*e.topPercentage+c;
            if(e.top){
                i=e.top+c;
            }
            f.css({
                left:g,
                top:i
            });
        };
    
        d();
        b.resize(d);
    });
};
;
( function( $, doc ){

    // global configs to be use accross the whole page
    var globalConfigs = {},

    // global var to store the auto generate msgID
    msgID = 0,

    // global var to store the setTimeout function,
    // it would be call later with clearTimeout
    autoUnblock,

    // a global var to store the beforeUnblock event handler for each msg
    beforeUnblock = [ function(){} ];

    // the jquery plugin
    $.msg = function(){
        var $content, $overlay, options, type, configs, _, publicMethods;

        options = [].shift.call( arguments );
        type = {}.toString.call( options );

        // merge default setting with globalConfigs
        configs = $.extend({
            // after block event handler
            afterBlock : function(){},
            autoUnblock : true,

            // options for $.center( center ) plugin
            center : {
                topPercentage : 0.4
            },
            css : {},

            // click overlay to unblock
            clickUnblock : true,
            content : "Please wait..." ,
            fadeIn : 200,
            fadeOut : 300,
            bgPath : '',

            // default theme
            klass : 'black-on-white',

            // jquery methodds, can be appendTo, after, before...
            method : 'appendTo',

            // DOM target to be insert into the msg
            target : 'body',

            // default auto unblock count down
            timeOut : 2400,

            // default z-index of the overlay
            z : 1000

        }, globalConfigs );

        if( type !== '[object String]' ){
            // merge default setting with user options
            $.extend( configs, options );
        }

        // private methods
        _ = {
            // private unblock method
            unblock : function(){
                // remove msg after fade out
                $overlay = $( '#jquery-msg-overlay' ).fadeOut( configs.fadeOut, function(){

                    // beforeUnblock event callback
                    beforeUnblock[ configs.msgID ]( $overlay );
                    $overlay.remove();
                });

                // clear the auto unblock function
                clearTimeout( autoUnblock );
            }
        };

        publicMethods = {

            // unblock the screen
            unblock : function( ms, _msgID ){

                // default unblock delay is 0 ms
                var _ms = ms === undefined ? 0 : ms;

                // set msgID
                configs.msgID = _msgID === undefined ? msgID : _msgID;

                setTimeout( function(){
                    _.unblock();
                }, _ms );
            },

            // replace current content in the msg
            replace : function( content ){

                // check if the to be replaced content exist
                // and make sure it's a string
                if( content === undefined && typeof( content ) === 'string' ){
                    throw '$.msg(\'replace\') error: second argument is undefined or is not a string';
                }

                // replace old contant with new content and set the msg box to center
                $( '#jquery-msg-content' ).empty().
                html( content ).
                center( configs.center );
            },

            overwriteGlobal : function( name, config ){
                globalConfigs[ name ] = config;
            }
        };

        // generate msgID
        msgID--;

        // if not specified use the auto generate msgID
        configs.msgID = configs.msgID === undefined ?
        msgID :
        configs.msgID;

        // check if the beforeUnblock event handler is defined in the user option
        // otherwise assign it a empty function
        beforeUnblock[ configs.msgID ] = configs.beforeUnblock === undefined ?
        function(){} :
        configs.beforeUnblock;

        // if options is a string
        // execute public method
        if( type === '[object String]' ){
            publicMethods[ options ].apply( publicMethods, arguments );
        }else{

            // DOM el
            // for ie fade in trans we have to use img instead of  div
            $overlay = $(
                '<div id="jquery-msg-overlay" class="' + configs.klass + '" style="position:absolute; z-index:' + configs.z + '; top:0px; right:0px; left:0px; height:' + $( doc ).height() + 'px;">' +
                '<img src="' + configs.bgPath + 'blank.gif" id="jquery-msg-bg" style="width: 100%; height: 100%; top: 0px; left: 0px;"/>' +
                '<div id="jquery-msg-content" class="jquery-msg-content" style="position:absolute;">' +
                configs.content +
                '</div>' +
                '</div>'
                );

            // configs.method can be appendTo, after ...
            $overlay[ configs.method ]( configs.target );

            // set content ( msg ) to center before hiding
            // and apply user option css if any
            $content = $( '#jquery-msg-content' ).
            center( configs.center ).
            css( configs.css ).
            hide();

            // fadein the content after fade in the bg
            // then trigger afterBlock event handler
            $overlay.
            hide().
            fadeIn( configs.fadeIn, function(){
                $content.fadeIn( 'fast' ).children().andSelf().bind( 'click', function( e ){
                    e.stopPropagation();
                });

                // execute afterBlock callback
                configs.afterBlock.call( publicMethods, $overlay );

                // apply click unblock if the config set to true
                if( configs.clickUnblock ){
                    $overlay.bind( 'click', function( e ){
                        e.stopPropagation();
                        _.unblock();
                    });
                }

                // apply auto unblock if the config set to true
                if( configs.autoUnblock ){
                    autoUnblock = setTimeout( _.unblock , configs.timeOut );
                }
            });
        }

        // return this to enable chaining
        return this;
    };

})( jQuery, document );

;
(function(b,a){
    b.fn.center=function(c){
        var d,e;
        d=b(a);
        e=d.scrollTop();
        return this.each(function(){
            var h,g,f;
            h=b(this);
            g=b.extend({
                against:"window",
                top:false,
                topPercentage:0.5
            },c);
            f=function(){
                var k,j,i,l;
                k=g.against;
                if(k==="window"){
                    j=d;
                }else{
                    if(k==="parent"){
                        j=h.parent();
                        e=0;
                    }else{
                        j=h.parents(k);
                        e=0;
                    }
                }
                i=((j.width())-(h.outerWidth()))*0.5;
                l=((j.height())-(h.outerHeight()))*g.topPercentage+e;
                if(g.top){
                    l=g.top+e;
                }
                h.css({
                    left:i,
                    top:l
                });
            };
        
            f();
            d.resize(f);
        });
    };

})(jQuery,window);

(function($){
    var menu,shadow,trigger,content,hash,currentTarget;
    var defaults={
        menuStyle:{
            listStyle:'none',
            padding:'1px',
            margin:'0px',
            backgroundColor:'#fff',
            border:'1px solid #999',
            width:'100px'
        },
        itemStyle:{
            margin:'0px',
            color:'#000',
            display:'block',
            cursor:'default',
            padding:'3px',
            border:'1px solid #fff',
            backgroundColor:'transparent'
        },
        itemHoverStyle:{
            border:'1px solid #0a246a',
            backgroundColor:'#b6bdd2'
        },
        eventPosX:'pageX',
        eventPosY:'pageY',
        shadow:true,
        onContextMenu:null,
        onShowMenu:null
    };
    
    $.fn.contextMenu=function(id,options){
        if(!menu){
            menu=$('<div id="jqContextMenu"></div>').hide().css({
                position:'absolute',
                zIndex:'500'
            }).appendTo('body').bind('click',function(e){
                e.stopPropagation()
            })
        }
        if(!shadow){
            shadow=$('<div></div>').css({
                backgroundColor:'#000',
                position:'absolute',
                opacity:0.2,
                zIndex:499
            }).appendTo('body').hide()
        }
        hash=hash||[];
        hash.push({
            id:id,
            menuStyle:$.extend({},defaults.menuStyle,options.menuStyle||{}),
            itemStyle:$.extend({},defaults.itemStyle,options.itemStyle||{}),
            itemHoverStyle:$.extend({},defaults.itemHoverStyle,options.itemHoverStyle||{}),
            bindings:options.bindings||{},
            shadow:options.shadow||options.shadow===false?options.shadow:defaults.shadow,
            onContextMenu:options.onContextMenu||defaults.onContextMenu,
            onShowMenu:options.onShowMenu||defaults.onShowMenu,
            eventPosX:options.eventPosX||defaults.eventPosX,
            eventPosY:options.eventPosY||defaults.eventPosY
        });
        var index=hash.length-1;
        $(this).bind('contextmenu',function(e){
            var bShowContext=(!!hash[index].onContextMenu)?hash[index].onContextMenu(e):true;
            if(bShowContext)display(index,this,e,options);
            return false
        });
        return this
    };
        
    function display(index,trigger,e,options){
        var cur=hash[index];
        content=$('#'+cur.id).find('ul:first').clone(true);
        content.css(cur.menuStyle).find('li').css(cur.itemStyle).hover(function(){
            $(this).css(cur.itemHoverStyle)
        },function(){
            $(this).css(cur.itemStyle)
        }).find('img').css({
            verticalAlign:'middle',
            paddingRight:'2px'
        });
        menu.html(content);
        if(!!cur.onShowMenu)menu=cur.onShowMenu(e,menu);
        $.each(cur.bindings,function(id,func){
            $('#'+id,menu).bind('click',function(e){
                hide();
                func(trigger,currentTarget)
            })
        });
        menu.css({
            'left':e[cur.eventPosX],
            'top':e[cur.eventPosY]
        }).show();
        if(cur.shadow)shadow.css({
            width:menu.width(),
            height:menu.height(),
            left:e.pageX+2,
            top:e.pageY+2
        }).show();
        $(document).one('click',hide)
    }
    function hide(){
        menu.hide();
        shadow.hide()
    }
    $.contextMenu={
        defaults:function(userDefaults){
            $.each(userDefaults,function(i,val){
                if(typeof val=='object'&&defaults[i]){
                    $.extend(defaults[i],val)
                }else defaults[i]=val
            })
        }
    }
})(jQuery);
$(function(){
    $('div.contextMenu').hide()
});
