var dragarray = Array();
var data = new Object();
function replaceOptions(reltag, replacement) {
    // projdu všechny tagy rel
    $$('select[class='+reltag+']').each(function(curselect){
        // načtu všechny vybrané
        selected = new Array();
        curselect.getSelected().each(function(el) {   
            selected.push(el.value);   
        });
        curselect.empty();
        if ($type(replacement) == 'object') {
            objekt = new Hash(replacement);    
        } else {
            objekt = replacement;
            
        } 
        objekt.each(function(item, index){
           option = new Element('option',
                                    {'value':index,
                                     'text':item}); 
           if (selected.contains(index)==true) {
              option.set('selected','selected');
           }
           option.inject(curselect);
        });
    });
    
}

function createWindow(name, urlx, e) {
    if (!$(name)) {
        wina = new Element('div',
                                {
                                 'styles': { 'position': 'absolute',
                                             'left':0,
                                             'top':0,
                                             'z-index':100
                                             }
                                });
        win = new Element('div',
                                {'class':'windowclass',
                                 'styles': { 'left': e.page.x,
                                              'top': e.page.y}
                                });
        win.fade('hide');
        inwin = '<div id="head'+name+'" class="windowhead">\n'+
                '<img src="/images/close.gif" alt="close" onclick="$(\'head'+name+'\').getParent().getParent().destroy();" />\n' +
                '<div class="clear"></div></div>\n' +
                '<div class="windowbody" id="'+name+'">aaa</div><div class="clear"></div>';
        win.set('html', inwin);
        
        win.inject(wina);
        $$('body').grab(wina);
          
        dragarray[name] = new Drag.Move(win, {'snap':10,
                                         'handle':$('head'+name)});
			//document.ondragstart = function () { return false; }; //IE drag hack
        //alert(urlx);
        

        var ajax = new Request({
                        url:urlx, 
                        method: 'get',
                        onComplete: function(data) {
                            // nastav obsah vracenych elementu
                            updateElements(JSON.decode(data));
                            win.fade('in');
                            //alert(data);
                            //$('response').setHTML(data);
                        }
                    }
                ).send();
        
        
    }
    return false;
}

function updateElements(data){
    if(data.fcebefore) { eval(data.fcebefore); }
    if (data.elements) {
        new Hash(data.elements).each(function(item, index){
            // pokud existuje
            if($(index)){
                if ($(index).get('tag') == 'textarea' || $(index).get('tag') == 'input') {
                    $(index).value = item;
                } else {
                    $(index).set('html', item);
                }
            }
        });
    }
    if(data.fceafter) { eval(data.fceafter); }
    //$('response').setHTML(Json.toString(data));
}

function newWin(url) {
    wasOpen  = false;
    win = window.open(url);    
    return ($type(win)=='object')?false:true;
}

function headerLogin() {
    $('login-header-form').addEvent('submit', function(e) {
        $('login-header-form').set('send', {
                        url: '/prihlasit', 
                        method: 'post',
                        onComplete: function(data) {
                            // nastav obsah vracenych elementu
                            updateElements(JSON.decode(data));
                            //alert(data);
                            //$('response').setHTML(data);
                        }
                    }
                );
        $('login-header-form').send();
        e.stop(); 
    });
}
window.addEvent("domready", function() {
    $$('.hiddenJSblock').each(function(el) {
        el.setStyle('display', 'block');
            
    });
    
    if ($('login-header-form')) {
        headerLogin();
    }
    
    if ($('tagnoscript')) {
        el = new Element('input',
            	                   {'type':'hidden',
            	                    'name':'spamNum',
            	                    'value': 4+3});
        el.inject($('tagnoscript'), 'after');
    }
    
    if ($('headnum')) {
        $('headnum').set('value', 3);
    }
    
    if ($('debate-block')) blockNext('debate-prev', 'debate-next', '/debate-block-next/');
    if ($('blog-block')) blockNext('blog-prev', 'blog-next', '/blog-block-next/');
    if ($('help-block')) blockNext('help-prev', 'help-next', '/help-block-next/');
    if ($('help-home-block')) blockNext('help-prev', 'help-next', '/help-home-block-next/');
    if ($('news-block')) blockNext('news-prev', 'news-next', '/news-block-next/');
    if ($('advisor-block')) blockNext('advisor-prev', 'advisor-next', '/advisor-block-next/');
    
    $$('.needLogin').each(function(el) {
        actualEvent = '';
        if (el.get('tag') == 'a') {
        	actualEvent = 'click';
        } else if (el.get('tag') == 'form') {
        	actualEvent = 'submit';
        }
    	
    	el.addEvent(actualEvent, function(ev) {
            if ($('UserLoginHead')) {
                alert('Pro tuto akci musíte být přihlášen/a!');
                ev.stop();
            }
        }
        
        );
            
    });
    
    $$('a.ext').each(function(el) {
        el.addEvent('click', function(e) { 
            e = new Event(e); 
            e.stop();     
            window.open(this.getProperty('href')); 
        }.bind(el)); 
    });
    $$('.mailto').each(function(el) {
        reArg = /mailto:(.*)/;
        textnew = reArg.exec(el.get('href'));
        if (textnew !=null) {
            text2 = '';
            for (i=textnew[1].length;i>0;i--) {
                text2 +=textnew[1].charAt(i-1);
            }
            el.set('href','mailto:'+text2);
            if (el.get('rel')) {
                el.set('html',el.get('rel'));
            } else {
                el.set('html',text2);
            }
        }
            
    });
    
});

function blockNext(idPrev, idNext, url) {
    
    if ($(idPrev)) {
        e = $(idPrev);
        e.set('href', url+e.rel);
        e.addEvent('click', function(ev) {
            var ajax = new Request({
                        url:this.href, 
                        method: 'get',
                        onComplete: function(data) {
                            // nastav obsah vracenych elementu
                            updateElements(JSON.decode(data));
                            //alert(data);
                            //$('response').setHTML(data);
                        }
                    }
                ).send();
            
            ev.stop();
        });
    }
    if ($(idNext)) {
        e = $(idNext);
        e.set('href', url+e.rel);
        e.addEvent('click', function(ev) {
            var ajax = new Request({
                        url:this.href, 
                        method: 'get',
                        onComplete: function(data) {
                            // nastav obsah vracenych elementu
                            updateElements(JSON.decode(data));
                            //alert(data);
                            //$('response').setHTML(data);
                        }
                    }
                ).send();
            
            ev.stop();
        });
    }
    
    
}


