new Asset.css('/lib/mootree/mootree.css');
new Asset.css('/templates/datagrid.css');
new Asset.css('/lib/elselect/theme/style.css');
var langTimer;
var treeSelectedNode;
openTreeNode = function(nodeId){
    var selectedNode = tree.get('n' + nodeId);
    var tempNode = selectedNode;
    var openNode = new Array();
    while (tempNode.text != "PRODUKTY") {
        openNode.push(tempNode);
        tempNode.toggle();
        tempNode = tempNode.parent;
    }
    selectedNode.select(true);
    correctNodeIcon();
    return selectedNode;
}
inputClick = function(params){
    params.each(function(a, b){
        if ($defined($(a.inid))) {
            $(a.inid).value = a.text;
            $(a.inid).setStyle('color', '#BBB');
            $(a.inid).addEvent('focus', function(e){
                if ($(a.inid).value == a.text) {
                    $(a.inid).value = '';
                    $(a.inid).setStyle('color', '#333');
                }
            });
            $(a.inid).addEvent('blur', function(e){
                if ($(a.inid).value == '') {
                    $(a.inid).value = a.text;
                    $(a.inid).setStyle('color', '#BBB');
                }
            });
        }
    })
}
correctNodeIcon = function(){
    var nodes = ['n11', 'n14','n428'];
    nodes.each(function(a){
        if (tree.get(a)) {
            tree.get(a).div.gadget.getFirst().setStyle('background-position', '-144px 0px');            
        }
    })
}
ajaxLoadDiv = function(param){
    $('footer').setStyle('display', 'none');
    $(param.div).set("html","<div id=loading><img src='/lib/multibox/images/loader.gif'></div>");
    aReq = new Request.HTML({
        url:param.url,
        autoCancel: true,
        method: 'get',
        update: $(param.div),
        onComplete: function(){
            if (param.onComplete) 
                param.onComplete();
            $('footer').setStyle('display', 'block');
        }
    }).send();
}
jsonConfirm = function(param){
    if (confim(param.q)) {
        var aReq = new Json.Remote(param.url, {
            onComplete: function(action){
                if (param.onComplete()) 
                    param.onComplete();
            }
        }).send(param.json)
    }
}
showCategory = function(id, ajax){
    if (id != '') {
        window.location.hash = "cat" + id;
        if (ajax == true) 
            ajaxLoadDiv({
                div: 'rightpane',
                url: "/kategoria.php?r=a&id=" + id
            });
        else 
            window.location.pathname = '/' + lang + '/cat/' + id;
    }
}
hideLangDiv = function(){
    $('langdiv').setStyle('display', 'none');
}
prepareLang = function(){
    if ($('langselect')) {
        $('langselect').addEvent('mouseenter', function(e){
            $clear(langTimer);
            $('langdiv').setStyle('display', 'block');
        });
        $('langselect').addEvent('mouseleave', function(e){
            langTimer = hideLangDiv.delay(1000);
        });
    }
    if ($('langselect')) {
        $('langdiv').addEvent('mouseenter', function(e){
            $clear(langTimer);
        });
        
        $('langdiv').addEvent('mouseleave', function(e){
            langTimer = hideLangDiv.delay(1000);
        });
        ajaxLoadDiv({
            div: 'langdiv',
            url: '/language.php?r=a'
        });
    }
}
adoptTree = function(){
    tree = new MooTreeControl({
        div: 'mytree',
        mode: 'folders',
        theme: '/mootree.gif',
        grid: true,
        onClick: function(node){
            node.toggle(false);
            if (treeSelectedNode != undefined) 
                treeSelectedNode.select(false);
            if (true) {
                var id = node.data.url.match("/cat/([0-9]*)", "i")[1];
                showCategory(id, true);
            }
        },
        onSelect: function(node){
            correctNodeIcon();
        },
        onExpand: function(node){
            correctNodeIcon();
        }
    }, {
        text: 'PRODUKTY',
        open: true
    });
    $$('.left_menu>ul').setProperty('id', 'mptt');
    tree.adopt('mptt');
    tree.root.div.main.style.display = 'none';
    correctNodeIcon();
}
initTinyMCE = function(language){
    tinyMCE.init({
        /*
         content_css:"/templates/main.css",
         */
        language: language,
        theme: "advanced",
        mode: "textareas",
        plugins: "paste,save",
        paste_auto_cleanup_on_paste: true,
        paste_preprocess: function(pl, o){
        // Content string containing the HTML from the clipboard
        //            alert(o.content);
        //            o.content = "-: CLEANED :-\n" + o.content;
        },
        paste_postprocess: function(pl, o){
        // Content DOM node containing the DOM structure of the clipboard
        //            alert(o.node.innerHTML);
        //            o.node.innerHTML = o.node.innerHTML + "\n-: CLEANED :-";
        },
        save_onsavecallback: "saveEditor",
        editor_deselector: "mceNoEditor",
        theme_advanced_buttons1: "save,cancel,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,outdent,indent,undo,redo,link,unlink,forecolor,backcolor,fontsizeselect,code",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true,
        theme_advanced_resize_horizontal: false,
        theme_advanced_path: false,
        valid_elements: "a[href|target=_blank],strong,b,div[id|class|align|style],br,span[style],li,ul,p,img[src|align|class|width|height],style,h1,h2,h3,h4,h5,table[style|cellpadding|cellspacing],tbody,tr[class|style],th,td[class|style]",
        relative_urls : false
    })
}
changeLanguage = function(lang_){
    var url = window.location.href;
    url = url.replace("/" + lang + "/", "/" + lang_ + "/");
    window.location.href = url;
}
leftMenuSelects = function(divclass){
    $$('.' + divclass + ' p').each(function(item, index){
        if (item.id) 
            new elSelect({
                container: item.id,
                onOptionClick: function(e){
                    showCategory(e);
                }
            });
    })
}
window.addEvent('domready', function(){
    ajxcat = window.location.hash;
    if (ajxcat!="" && ajxcat!="#") {
        showCategory(ajxcat.match("cat([0-9]*)", "i")[1]);
    }
    if ($('searchform')) {
        $('searchform').addEvent('submit', function(e){
            if ($('searchfield').getValue() == trans.search) 
                return false;
            else 
                return true;
        });
        inputClick([{
            inid: 'searchfield',
            text: trans.search
        }, {
            inid: 'addr',
            text: trans.enter_email
        }]);
    }
    prepareLang();
    try {
        leftMenuSelects('left_menu');
    } 
    catch (e) {
        console.log(e);
    }
});
function MM_preloadImages(){
    var d = document;
    if (d.images) {
        if (!d.MM_p) 
            d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
        for (i = 0; i < a.length; i++) 
            if (a[i].indexOf("#") != 0) {
                d.MM_p[j] = new Image;
                d.MM_p[j++].src = a[i];
            }
    }
}

function MM_swapImgRestore(){
    var i, x, a = document.MM_sr;
    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) 
        x.src = x.oSrc;
}

function MM_findObj(n, d){
    var p, i, x;
    if (!d) 
        d = document;
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) 
        x = d.all[n];
    for (i = 0; !x && i < d.forms.length; i++) 
        x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) 
        x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) 
        x = d.getElementById(n);
    return x;
}

function MM_swapImage(){
    var i, j = 0, x, a = MM_swapImage.arguments;
    document.MM_sr = new Array;
    for (i = 0; i < (a.length - 2); i += 3) 
        if ((x = MM_findObj(a[i])) != null) {
            document.MM_sr[j++] = x;
            if (!x.oSrc) 
                x.oSrc = x.src;
            x.src = a[i + 2];
        }
}

function getFlashMovieObject(movieName){
    if (document.embeds[movieName]) 
        return document.embeds[movieName];
    if (window.document[movieName]) 
        return window.document[movieName];
    if (window[movieName]) 
        return window[movieName];
    if (document[movieName]) 
        return document[movieName];
    return null;
}

