var max_space; // integer value specifiing the max value of the space indicator
var page_candy = false;

Element.Events.extend({
    'wheelup': {
        type: Element.Events.mousewheel.type,
        map: function(event){
            event = new Event(event);
            if (event.wheel >= 0)
                this.fireEvent('wheelup', event)
        }
    },
    'wheeldown': {
        type: Element.Events.mousewheel.type,
        map: function(event){
            event = new Event(event);
            if (event.wheel <= 0)
                this.fireEvent('wheeldown', event)
        }
    }
});

Element.extend({
    highlight:function(){
        this.effects().start({
            'opacity':0.7
        }).chain(function(){
            this.start({
                'opacity':1
            })
        });
    },
    mkClosable:function(){
        this.getElement('.preview_image').adopt(
        new Element('div',{
            'class':'closeBtn',
            'style':'position:absolute',
            'events':{
                'click':function(){
                    slotId = $(this.parentNode.parentNode.parentNode).id.split('_')[1];
                    var slot_item = $('slentry_' + slotId);
                    if(slot_item != null){
                        slot_item.removeClass('taken').effects({onComplete:function(){
                                init_slot_list_elems();
                            }}).start({'background-color':'#fff'});
                    }
                    $(this.parentNode.parentNode.parentNode).remove();
                    var data = [];
                    $('my_list').getElements('div.listentry').each(function(el){
                        data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
                    });
                    make_scrollbar($('my_list'),1);
										set_mylist_dirty();
                     
                    if($('add_slot_link_' + slotId)) 
                        $('add_slot_link_' + slotId).setStyle('display','block');
                }
            }
        }).adopt(new Element('img', {
            'alt':'entfernen',
            'src':'/images/icons/bt_schliessen_16.png',
            'styles':{
                'cursor':'pointer'
            }
        }))
    );
    },
    mkDeleteable:function(url){
        id = this.id.split('_')[1]
        if($('mlentry_' + id) == null)
        {    
            new Element('div', {
                'style':'position: absolute; height: 20px; width: 20px; z-index: 20;'
            }).adopt(
            new Element('div',{
                'class':'closeBtn',
                'id' : 'deleteBtn_' + id,
                'style':'position:absolute; top: 4px; left: 3px;',
                'events':{
                    'click':function(){                                                     
                        new Ajax(url, {method: 'get', data: 'id='+id}).request();
                        $(this.parentNode).getNext().remove();
                        $(this.parentNode).remove();
                                                        
                    }
                }
            }).adopt(new Element('img', {
                'alt':'entfernen',
                'src':'/images/icons/bt_schliessen_16.png',
                'styles':{
                    'cursor':'pointer'
                }
            }))).injectBefore(this);
            
        }
    },  
    mkShowInfo:function(url){
        _id = this.id.split('_')[1];
        
        new Element('div', {
            'style' : 'cursor:pointer',
            'styles':{
                'position' : 'absolute',
                'z-index' : '200',     
                'cursor':'pointer;'
            }          
        }).adopt(new Element('div', {
            'style' : 'cursor:pointer',
            'styles':{           
                'margin-top' : '3px',
                'margin-left' : '3px',
                'cursor':'pointer;'
            },          
            'events':{
                'click':function(){                                                     
                    this.style.cursor = "wait";                     
                    call_ajax(url +'?id=' + id ,$("feed_info"),this);
                }
            }
        }).adopt(new Element('img', {  
            'style' : 'cursor:pointer',
            'alt':'info',            
            'src':'/images/icons/information.png',
            'styles':{
                'cursor':'pointer;'
            }
        }))).injectBefore(this);
        
    },
    mkShowInfo3:function(url){
        id = this.id.split('_')[1];
        
        new Element('div', {
            'style':'position: absolute; height: 20px; width: 20px; z-index: 200;'
        }).adopt(       
        new Element('a', {
            'id' : 'showInfo_' + id,
            'style':'position:absolute; top: 5px; left: 4px; cursor:pointer;',            
            'onclick':'this.style.cursor = "wait"; call_ajax("'+url+'?id='+id+'",$("feed_info"),this)'
        }).adopt(new Element('img', {
            'alt':'info',
            'src':'/images/icons/information.png',
            'styles':{
                
            }
        }))).injectBefore(this);
        
    }
    
});





function hide_feed_info()
{
    if (Timer2)
        clearTimeout(Timer2);
    status = 0;
    stopNow = 0;
    current_pic = 1;  
    $('feed_info').effects({duration: 400, transition: Fx.Transitions.Back.easeIn}).start({'left':[3,630]})
}  

function get_list_entry_from_json(data, id_prefix){
    var elemid;
    if(id_prefix != undefined){
        elemid = id_prefix + data.id;
    }
    else {
        elemid = '' + data.id;
    }
    var entry = new Element('div', {
        'id': elemid,
        'class':'listentry',
        'title':data.name + '::' + data.description
    }).adopt([new Element('div', {'class':'space_value', 'styles':{'display':'none'}}).setHTML(data.space_value), new Element('div', {'class':'entry_content'}).adopt([new Element('div', {'class':'preview_image', 'styles':{'background':'transparent url(\'' + data.preview_image + '\') left top no-repeat'}}), new Element('div', {'class':'name'}).setHTML(data.name), new Element('div', {'class':'logo_url'}).adopt(new Element('img', {'alt':'', 'src':data.logo_url}))]), new Element('div', {'class':'description'}).setHTML(data.description)]);
    return entry;
}

function fire_to_list(data, cont, id_prefix){
    cont.empty();
    var entry;
    data.each(function(row){
        entry = get_list_entry_from_json(row, id_prefix);
        cont.adopt(entry);
    });
}

var myTips;
function make_tips(elems){
    var clipper;
    $E('body').getElements('div.tool-tip').each(function(el){el.remove();});
    elems.each(function(el){
        clipper = el.getElement('div.clipper');
        if(clipper != null){
            el.setAttribute('title', clipper.innerHTML);
        }
        else{
            el.adopt(new Element('div', {
                'class':'clipper',
                'styles':{
                    'display':'none'
                }
            }).setHTML(el.getAttribute('title')));
        }
    });
    myTips = new Tips(elems, {
        maxTitleChars: 50,
        showDelay:400
    });
}

function init_slot_list_elems(){
    var sl = $('slot_list');
    sl.getElements('div').each(function(item){
        if($('mlentry_' + item.id.split('_')[1]) != null)
            item.addClass('taken');
    });
    make_scrollbar(sl,1);
    var drop = $('my_list');
    var dropFx = drop.effect('background-color', {wait: false});
    var myElems = sl.getElements('div.listentry');
    myElems.each(function(item){
        item.removeEvents();
        if(!item.hasClass('taken')){
            item.setStyle('cursor', 'move');
            item.addEvent('mousedown', function(e) {
                e = new Event(e).stop();
                var myCoords = this.getCoordinates();
                myCoords.top = myCoords.top - sl.getSize().scroll.y;
                var clone = this.clone()
                .setStyles(myCoords)
                .setStyles({'opacity': 0.7, 'position': 'absolute'})
                .addEvent('emptydrop', function() {
                    this.remove();
                    drop.removeEvents();
                    new Sortables(drop, {ghost:false});
                }).inject(document.body);
                var dropEntries = drop.getElements('div.listentry');
                drop.addEvents({
                    'drop': function() {
                        dropEntries.each(function(el){el.setStyle('margin-bottom', 2)});
                        drop.removeEvents();                       
                        clone.remove();
                        var newElem = item.clone();
                        newElem.removeEvents();
                        var slotId = newElem.id.split('_')[1];
                        item.effects({onComplete:function(){
                                item.removeEvents().setStyle('cursor', 'default');
                                var close = item.getElement('div.closeBtn')
                                if(close){ close.remove(); }
                            
                            }}).start({'background-color':'#ddd'});
                        
                        buttons = makeButtons(slotId);
                        buttons.inject(newElem,'top'); 
                                                
                        newElem.id = 'mlentry_' + slotId;
                        newElem.injectInside(drop);
                        make_sortable(drop);
                        dropFx.start('ffffff').chain(dropFx.start.pass('dddddd', dropFx));
                        newElem.effects({duration:2000}).start({'background-color':['#ffff00', '#ffffaa']});
                        set_mylist_dirty();
                        newElem.addClass('dropped');
                        make_scrollbar(drop);
                        var data = [];
                        drop.getElements('div.listentry').each(function(el){
                            data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
                        });
                        if($('first_user_help'))
                            $('first_user_help').remove();
                        
                        if ($('deleteBtn_' + slotId))
                            $('deleteBtn_' + slotId).remove();
                                
//                        $('button_to_handy').src = '/images/button_ab_aktiv.gif';                       
												set_mylist_dirty();
                        $('add_slot_pic_' + slotId).setStyle('display','none');
                    },
                    'over': function() {
                        dropFx.start('e0a288');
                    },
                    'leave': function() {
                        dropFx.start('dddddd');
                    }
                });
                dropEntries.each(function(el){
                    el.addEvents({
                        'drop': function(e) {
                            dropEntries.each(function(el){el.setStyle('margin-bottom', 2)});
                            drop.removeEvents();
                            clone.remove();
                            var newElem = item.clone();
                            newElem.removeEvents();
                            var slotId = newElem.id.split('_')[1];
                            item.effects({onComplete:function(){
                                    item.removeEvents().setStyle('cursor', 'default');
                                    var close = item.getElement('div.closeBtn')
                                    if(close){ close.remove(); }
                                
                                }}).start({'background-color':'#ddd'});
                            
                            buttons = makeButtons(slotId);
                            buttons.inject(newElem,'top'); 
                            
                            newElem.id = 'mlentry_' + slotId;
                            newElem.injectAfter(this);
                            
                            make_sortable(drop);
                            dropFx.start('ffffff').chain(dropFx.start.pass('dddddd', dropFx));
                            newElem.effects({duration:2000}).start({'background-color':['#ffff00', '#ffffaa']});
                            set_mylist_dirty();
                            newElem.addClass('dropped');
                            make_scrollbar(drop);
                            var data = [];
                            drop.getElements('div.listentry').each(function(el){
                                data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
                            });
                            if($('first_user_help'))
                                $('first_user_help').remove();
//                            $('button_to_handy').src = '/images/button_ab_aktiv.gif';
														set_mylist_dirty();
                            $('add_slot_pic_' + slotId).setStyle('display','none');
                        },
                        'over': function() {
                            dropFx.start('e0a288');                            
                            dropEntries.each(function(el){el.setStyle('margin-bottom', 2)});                            
                            this.effect('margin-bottom', {duration:100}).start(42);
                        },
                        'leave': function() {
                            dropFx.start('dddddd');
                            this.setStyle('margin-bottom',2);
                        }
                    });
                });
                var drag = clone.makeDraggable({
                    droppables: [drop].extend(dropEntries)
                });
                drag.start(e);
            });
        }
    });
}

function close_slot(slotId)
{   
    var slot_item = $('slentry_' + slotId);
    if(slot_item != null){
        slot_item.removeClass('taken').effects({onComplete:function(){
                init_slot_list_elems();
            }}).start({'background-color':'#fff'});
    }
    $('mlentry_' + slotId).remove();
    var data = [];
    $('my_list').getElements('div.listentry').each(function(el){
        data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
    });
    make_scrollbar($('my_list'),1);
//    $('button_to_handy').src = '/images/button_ab_aktiv.gif';
    set_mylist_dirty();
    if($('add_slot_link_' + slotId)) 
        $('add_slot_link_' + slotId).setStyle('display','block');
    
    if($('add_slot_pic_' + slotId))
        $('add_slot_pic_' + slotId).setStyle('display','block');
    
}

/*
 This is for adding slots via button.
*/
function addSlotToMylist(slotId)
{
    var item = $('slentry_' + slotId);
 
    var drop = $('my_list');
    var dropFx = drop.effect('background-color', {wait: false});
    var newElem = item.clone();
 
    newElem.removeEvents();
    
    item.effects({onComplete:function(){
            item.removeEvents().setStyle('cursor', 'default');
            var close = item.getElement('div.closeBtn')
            if(close){ close.remove(); }
                                
        }}).start({'background-color':'#ddd'});
    
    newElem.id = 'mlentry_' + slotId;
    newElem.inject(drop,'top');
    
    buttons = makeButtons(slotId);
    buttons.inject(newElem,'top');    
       
    make_sortable(drop);
    dropFx.start('ffffff').chain(dropFx.start.pass('dddddd', dropFx));
    newElem.effects({duration:2000}).start({'background-color':['#ffff00', '#ffffaa']});
    set_mylist_dirty();
    newElem.addClass('dropped');
    make_scrollbar(drop);
    var data = [];
    drop.getElements('div.listentry').each(function(el){
        data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
    });
    if($('first_user_help'))
        $('first_user_help').remove();
//    $('button_to_handy').src = '/images/button_ab_aktiv.gif';
    set_mylist_dirty();
    if($('add_slot_link_' + slotId))
        $('add_slot_link_' + slotId).setStyle('display','none');
    
    if($('add_slot_pic_' + slotId))
        $('add_slot_pic_' + slotId).setStyle('display','none');
    
}

   

function makeButtons(slotId)
{
    button_div = new Element('div', {
        'id': 'ml_buttons_' + slotId,
        'class':'ml_buttons'        
    });
    button_div.adopt(new Element('div', {        
    }).setStyles({'cursor': 'pointer', 'margin-top': '3px', 'margin-left': '3px'
    }).setHTML('<img src="/images/icons/information.png" alt="info" style="cursor: pointer;">'
).addEvent('click', function(){                
        call_ajax("/slots/feed_info/" + slotId  + ".html",$("feed_info"),this);
    }));
    button_div.adopt(new Element('br'));
    button_div.adopt(new Element('div', {        
    }).setStyles({'cursor': 'pointer', 'margin-top': '3px', 'margin-left': '3px', 'clear' : 'both'
    }).setHTML('<img style="cursor: pointer;" src="/images/icons/bt_schliessen_16.png" alt="entfernen">'
).addEvent('click', function(){                
        close_slot(slotId);
        set_mylist_dirty();
    }));
        
    return button_div;   
}



function init_slot_list(url){
    var sl = $('slot_list');
    var ml = $('my_list');
    var dropFx = ml.effect('background-color', {wait: false, duration:200});
    sl.empty();
    sl.adopt(new Element('img', {
        'src':'/images/loading.gif',
        'alt':'Laden...',
        'title':'Laden...'
    }));
    /* had to change in order to get the html from fire_to_list into templates  */
    new Ajax(url, {method: 'get', postBody: '', update: sl, evalScripts: true, onComplete: function(){
            init_slot_list_elems();       
        }
    }).request();
}

function make_sortable(cont){
    cont.getElements('div.listentry').each(function(elem){
        elem.removeEvents();
        elem.setStyle('cursor', 'move');
    });
    new Sortables(cont, {ghost:false, onComplete: function(){set_mylist_dirty();}});
}


var mylistDirty = false;
var mylistDirtySaveIt = false;
function set_mylist_dirty() {
  if (!mylistDirty) {
    mylistDirty = true;
		window.onbeforeunload = do_beforeunload;
    // set dirty-color in editor
    $('geaendert').setStyle('display', 'inline');
		//$('button_to_handy').src = '/images/button_ab_aktiv.gif';
  }
}

function set_mylist_clean() {
  if (mylistDirty) {
		window.onbeforeunload = "";
    // set dirty-color in editor
    $('geaendert').setStyle('display', 'none');
		//$('button_to_handy').src = '/images/button_ab_passiv.gif';
  }
  mylistDirty = false;
}

var do_beforeunload = function () {
	return "Hinweis: Du verlässt den Programmeditor. \n\n Die Änderungen die Du an deinem Programm gemacht hast sind noch nicht für die Übertragung auf Dein Handy gespeichert:\n\nWenn Du das Programm speichern möchtest klickst Du auf \"Abbrechen\" und dann \"Programm buchen\" unten rechts im Programmeditor.";
}


function do_beforeunload_indeed() {
    mylist_dirty_save_it();
}

function remove_field(element, item) {
  element.up(item).remove();
}

var mySlide = [];
function make_scrollbar(cont, reset){
    var contS = cont.getSize();
    var state = 0;
    var sb = $(cont.parentNode).getElement('div.scrollbar');
    
    if(mySlide[cont.id] != null && reset == 1)
        state = mySlide[cont.id].step;
    if(sb != null)
        sb.remove();
        
    if(contS.size.y < contS.scrollSize.y){
        var scrollBar = new Element('div', {
            'class':'scrollbar'
        });
        $(cont.parentNode).adopt(scrollBar);
        var scrollBarS = scrollBar.getSize();
        var scroller = new Element('div', {
            'class':'scroller',
            'styles':{
                'height':Math.max(5, Math.round((scrollBarS.size.y * scrollBarS.size.y)/cont.getSize().scrollSize.y))
            }
        });
        scrollBar.adopt(scroller);
        var scroller_top = new Element('div', { 'class':'scroller_top'  });
        var scroller_bottom = new Element('div', { 'class':'scroller_bottom'  });
        scroller.adopt(scroller_top);
        scroller.adopt(scroller_bottom);
        mySlide[cont.id] = new Slider(scrollBar, scroller, {
            'steps': cont.getSize().scrollSize.y - cont.getSize().size.y,
            'mode': 'vertical',        
            'onChange': function(step){
                cont.scrollTo(0, step);
            }
        });
        mySlide[cont.id].set(state);
        [scrollBar, cont].each(function(el){
            el.removeEvents('wheelup');
            el.removeEvents('wheeldown');
            el.addEvents({
                'wheelup':function(e){
                    e = new Event(e).stop();
                    mySlide[cont.id].set(cont.getSize().scroll.y - 50);
                },
                'wheeldown':function(e){
                    e = new Event(e).stop();
                    mySlide[cont.id].set(cont.getSize().scroll.y + 50);
                }
            });
        });
    }
    else{
        cont.scrollTo(0,0);
    }
}

function make_scrollbar_small(cont, reset){
    var contS = cont.getSize();
    var state = 0;
    var sb = $(cont.parentNode).getElement('div.scrollbar');
    
    if(mySlide[cont.id] != null && reset == 1)
        state = mySlide[cont.id].step;
    if(sb != null)
        sb.remove();
        
    if(contS.size.y < contS.scrollSize.y){
        var scrollBar = new Element('div', {
            'class':'scrollbar_small'
        });
        $(cont.parentNode).adopt(scrollBar);
        var scrollBarS = scrollBar.getSize();
        var scroller = new Element('div', {
            'class':'scroller',
            'styles':{
                'height':Math.max(5, Math.round((scrollBarS.size.y * scrollBarS.size.y)/cont.getSize().scrollSize.y))
            }
        });
        scrollBar.adopt(scroller);
        mySlide[cont.id] = new Slider(scrollBar, scroller, {
            'steps': cont.getSize().scrollSize.y - cont.getSize().size.y,
            'mode': 'vertical',        
            'onChange': function(step){
                cont.scrollTo(0, step);
            }
        });
        mySlide[cont.id].set(state);
        [scrollBar, cont].each(function(el){
            el.removeEvents('wheelup');
            el.removeEvents('wheeldown');
            el.addEvents({
                'wheelup':function(e){
                    e = new Event(e).stop();
                    mySlide[cont.id].set(cont.getSize().scroll.y - 50);
                },
                'wheeldown':function(e){
                    e = new Event(e).stop();
                    mySlide[cont.id].set(cont.getSize().scroll.y + 50);
                }
            });
        });
    }
    else{
        cont.scrollTo(0,0);
    }
}

function init_my_feed_delete(url){
    $('slot_list').getElements('div.listentry').each(function(item){
        item.mkDeleteable(url);
    });
}

function init_feed_info(url){
    $('slot_list').getElements('div.listentry').each(function(item){
        item.mkShowInfo(url);        
    });
}

function init_my_list(url){
    var ml = $('my_list');
    new Ajax(url, {
        method: 'get',
        update: 'my_list',
        evalScripts: true,
        onComplete: function(data){
            make_sortable(ml);
            ml.getElements('div.listentry').each(function(item){
            });
            $E('#my_list_cont div.footer').setStyle('display', 'block');
            var data = [];
            ml.getElements('div.listentry').each(function(el){
                data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
            });
            make_scrollbar(ml, 1);
        }
    }).request();
}

function activate_slot(id){   
    var drop = $('my_list');
    item = $('slentry_'+id)    
    var newElem = item.clone();
    newElem.removeEvents();
    var slotId = newElem.id.split('_')[1];
    item.effects({onComplete:function(){
            item.removeEvents().setStyle('cursor', 'default');
        }}).start({'background-color':'#ddd'});
    newElem.id = 'mlentry_' + slotId;
    newElem.injectTop(drop);
    newElem.mkClosable();
    make_sortable(drop);
    dropFx.start('DE4200').chain(dropFx.start.pass('dddddd', dropFx));
    newElem.effects({duration:2000}).start({'background-color':['#ffff00', '#fff']});
    make_scrollbar(drop, 1);
    var data = [];
    drop.getElements('div.listentry').each(function(el){
        data.push({'space_value':parseInt(el.getElement('div.space_value').innerHTML)});
    });
}

function init_video_list(url){
    var ml = $('video_list');
    new Ajax(url, {method: 'get', update: ml, onComplete: function(data){
            /*make_sortable(ml);
        ml.getElements('div.listentry').each(function(item){
        item.mkClosable()
        });
             */
            $E('#video_list_cont div.footer').setStyle('display', 'block');
            make_scrollbar(ml, 1);
        }
    }).request();

}

function init_audio_list(url){
    var ml = $('audio_list');
    new Json.Remote(url, {
        onComplete: function(data){
            fire_to_list(data, ml, 'mlentry_');
            make_sortable(ml);
            ml.getElements('div.listentry').each(function(item){
                item.mkClosable()
            });
            $E('#audio_list_cont div.footer').setStyle('display', 'block');
            make_scrollbar(ml,1);
        }
    }).send();
}

function init_rss_list(url){
    var ml = $('rss_list');
    new Json.Remote(url, {
        onComplete: function(data){
            fire_to_list(data, ml, 'mlentry_');
            make_sortable(ml);
            ml.getElements('div.listentry').each(function(item){
                item.mkClosable()
            });
            $E('#rss_list_cont div.footer').setStyle('display', 'block');
            make_scrollbar(ml,1);
        }
    }).send();
}

function reset_my_list(url){
    init_my_list(url);
    init_slot_list(slotListURL);
    set_mylist_clean();
}

function reset_video_list(url){
    init_video_list(url);
}

function reset_audio_list(url){
    init_audio_list(url);
}

function reset_rss_list(url){
    init_rss_list(url);
}

function save_my_list(url,logged_in){
    var ml = $('my_list');
    var slots = [];
    ml.getElements('div.listentry').each(function(entry){
        slots.push(entry.id.split('_')[1]);
    });
    
    $('my_list').effects({duration:2000}).start({'background-color':['#DE4200', '#dddddd']});
    
    if(logged_in==1)
    {
        new Ajax(url, {
            update: ('feedback'),
            data:{'slots':slots},
            method: 'post',
            evalScripts:true,
            onComplete:function(){
								set_mylist_clean();
            }
        }).request();
    }
    else
    {
		    set_mylist_clean();
        location.href = (url + '?slots=' + slots);
    }
}

function save_video_list(){
    var ml = $('video_list');
    var slots = [];
    ml.getElements('div.listentry').each(function(entry){
        slots.push(entry.id.split('_')[1]);
    });
    new Ajax('/slots/save_list', {
        data:{'slots':slots},
        method: 'post',
        evalScripts:true
    }).request();
    
}

function save_audio_list(){
    var ml = $('audio_list');
    var slots = [];
    ml.getElements('div.listentry').each(function(entry){
        slots.push(entry.id.split('_')[1]);
    });
    new Ajax('/slots/save_list', {
        data:{'slots':slots},
        method: 'post',
        evalScripts:true
    }).request();
    
}

function save_rss_list(){
    var ml = $('rss_list');
    var slots = [];
    ml.getElements('div.listentry').each(function(entry){
        slots.push(entry.id.split('_')[1]);
    });
    new Ajax('/slots/save_list', {
        data:{'slots':slots},
        method: 'post',
        evalScripts:true
    }).request();
    
}

var myReq;
function fetch_by_xhr(url, cont, intime){
    if(!cont)
        cont = $('main');
    if(intime === true){}
    else{
        cont.empty().adopt(new Asset.image('/images/loading.gif', {
            'alt':'Laden...',
            'title':'Laden...'
        }));
    }
    
    myReq = new Ajax(url, {
        data:{'rid':new Date().getTime()},
        method: 'get',
        evalScripts:true,
        update: cont        
    }).request();
}

var selectTabs = false;

function init_select(tabtype,item, item_index){
    var myCont = $('select');
    var active = false;
    var i = 0;
    var accordion = new Accordion('#Kategorien h4', '#Kategorien div.content', {
        opacity: false,
        onComplete:function(e){
            make_scrollbar(myCont,1);
        },
        onBackground:function(el){
            el.removeClass('active');
            el.getNext().getElements('.active').each(function(elem){elem.removeClass('active');});
        },
        onActive:function(el){
            el.addClass('active');
        }
    });
    if (tabtype == 'Category'){             
        accordion.display(item_index);
        update_breadcrump('Kategorie / ' + item);
    }
    $ES('#Kategorien h4 a').each(function(el){
        el.addEvent('click', function(){this.blur();});
    });
    $ES('#Kategorien div.content a').each(function(el){
        el.addEvent('click', function(e){
            $ES('#Kategorien div.content a.active').each(function(lnk){lnk.removeClass('active');});
            $(this).addClass('active');
            this.blur();
        });
    });
    selectTabs = new mootabs('select_cont', {
        'width':myCont.getStyle('width'),
        'height':myCont.getStyle('height'),
        'changeTransition':'none'
    });
    if (tabtype == 'Tag'){
        selectTabs.activate('Tags');
        update_breadcrump('Tag / ' + item);
    }
		if (tabtype == 'Search'){
        selectTabs.activate('Search');
        update_breadcrump('Suche nach:  ' + item);
    }    

    $('mootabs_title').getElements('li').each(function(title){
        title.addEvent('click', function(){            
            window.setTimeout("make_scrollbar($('select'),0)", 500);
        });
    });
        
    
}


function init_slider(){
    var slider = new Element('div', {
        'id':'slider',
        'styles':{'display':'none'}
    });
    slider.adopt(new Element('div', {'class':'linner'}));
    slider.adopt(new Element('div', {'class':'minner'}));
    slider.adopt(new Element('div', {'class':'rinner'}));
		if ($('main_nav') == null)
			return;
    $('main_nav').getElement('div.inner').getElements('div').each(function(lCont){
        if(lCont.getElement('div.sublinks') != null){
            lCont.getElement('a').addEvent('mouseenter', function(e){
                new Event(e).stop();
                var slider = $('slider');
                slider.setStyle('display', 'block');
                var minner = slider.getElement('div.minner');
                minner.empty();
                var slnkc;
                $(this.parentNode).getElement('div.sublinks').getElements('a').each(function(slnk){                    
                    
                    slnkc = slnk.clone();                    
                    if (slnkc.href.indexOf('#') != -1)
                        slnkc.onclick = xhr_bind;
                    slnkc.injectInside(minner);
                });
                if (lCont.getElement('a').innerHTML == 'Support')
                  slider.effect('padding-left').start(this.getCoordinates().left - $('page').getCoordinates().left - 280);
                else
                  slider.effect('padding-left').start(this.getCoordinates().left - $('page').getCoordinates().left - 50);
            });
        }
        else
        { 
            lCont.getElement('a').addEvent('mouseenter', function(){                 
                $('slider').setStyle('display', 'none');
            });
        }
    });
    $('main_nav').addEvent('mouseleave', function(e){
        new Event(e).stop();
        $('slider').setStyle('display', 'none');
    });
    $('main_nav').getElement('div.inner').adopt(slider);
}

function rotatePartner(){
    var myCont = $('partner_banner');
		if (myCont == null)
			return;
    var first = myCont.getElement('a');
    first.effects({
        transition: Fx.Transitions.Circ.easeOut
    }).start({'margin-left':-50}).chain(function(){
        myCont.adopt(first);
        first.setStyle('margin-left', 5);
        document.cookie = 'current_partner=' + first.id + ';path=/';
    });
}

function activate_tag(tag)
{
    if(active_tag)
    {
        active_tag.removeClass('tag_active');
    }
      
    selectTabs.activate('Tags');
    
    if(tag)
    {
        tag.addClass('tag_active');
        active_tag = tag;
        update_breadcrump('Tag / ' + tag.innerHTML);
    }
    else
        update_breadcrump('Tag');
}

function update_breadcrump(string)
{   
    $('slot_breadcrump').setHTML(string);
}

function init_page()
{
    window.addEvent('domready', function(){

        $('top_nav').getElements('li').each(function(el){
            el.setStyle('height',1);
        });
        $('main_nav').effects({duration:1300}).start({
            'left':[2000, 43]
        });
        $('meta_nav').effects({duration:800}).start({'right':[1000,45]});
        $('top_nav').effects({duration:1000}).start({
            'margin-right':[900, 0]
        });
                
        var x = new Chain();

        $('partner_banner').effects({duration:3000}).start({
            'opacity':[0, 1]
        });
        

    });
    page_candy = false;
}
window.addEvent('domready', function(){
    var partnerChange = window.setInterval("rotatePartner()", 3000);
        
    window.setTimeout("init_slider()", 1000);
});

// Popup

function hidePopup() {
  $('dmtv-overlay-content').style.display = 'none';
  $('dmtv-overlay').style.display = 'none';
  return false;
}


// for ie mobile :(
function toggle_display(what)
{
    if (document.getElementById(what).style.display == 'block')
        document.getElementById(what).style.display = 'none';
    else
        document.getElementById(what).style.display = 'block';
          
}

function SlideShow(path,id){    
    clearTimeout(Timer);
    current_pic >= max_pics ? current_pic = 1 : current_pic++;                     
    $('thumbnail_preview_'+id).src = path +'/0000000'+current_pic+'.jpg';                
    Timer = window.setTimeout("SlideShow('"+path+"','"+id+"');", 1500);
}

// for ie mobile :(
function toggle_display(what)
{
    if (document.getElementById(what).style.display == 'block')
        document.getElementById(what).style.display = 'none';
    else
        document.getElementById(what).style.display = 'block';
          
}

function clearInputField(field, condition)
{
    if (field.value == condition)
        field.value = '';
}

   function activate_slot(what)
   {
       if (what.parentNode.parentNode.hasClass("dmtv-slot-active")) {
           what.parentNode.parentNode.removeClass("dmtv-slot-active");
         what.parentNode.parentNode.addClass("dmtv-slot-active");
       } else {
           what.parentNode.parentNode.addClass("dmtv-slot-active");
         what.parentNode.parentNode.removeClass("dmtv-slot-active");
  		}
   }
   

  function init_slots() {}

	/* handy select at download */
	function check_select_value()
    {      
        if ($('user_phoneType').value == "-1" )
            $('handy_name').setStyle('display','block');
        else
            $('handy_name').setStyle('display','none');
    }
