var app = {
    
    iv_list: new Array(),
    lang_id: 'language_elm',
    web_selector_id: 'web_selector',
    
    init: function(){  
        app.initDropDownSelector(app.lang_id);
        app.initDropDownSelector(app.web_selector_id);
        app.initImageOverlay();
    },
    
    initViewBox: function(iv_id){
        var i = app.iv_list.length;
        var iv = new ImageViewer(iv_id, i);
        iv.init();
        app.iv_list[i] = iv;
    },
    
    initImageOverlay: function(){
        initLightbox();
    },
    
    showImageOverlay: function(obj){
        showLightbox(obj);
        return false;        
    },
    
    initDropDownSelector: function(id){
        // for IE only!
        if (document.all && document.getElementById) {
          var obj = d_obj(id);
          if (obj) { 
            obj.className = '';
            app.addEvent(obj, 'mouseover', function(e){ this.className = 'hover'; });
            app.addEvent(obj, 'mouseout', function(e){ this.className = ''; });
          }
        }
    },
    addLoadEvent: function(func) {   
        var old_onload = window.onload;
        if (typeof window.onload != 'function'){
            window.onload = func;
        } else {
                  window.onload = function(){
                  old_onload();
                  func();
               }
        }
    },
    addEvent: function( obj, type, fn ){  // the add event function
      if (obj.addEventListener) obj.addEventListener( type, fn, false );
      else if (obj.attachEvent) {
        obj["e"+type+fn] = fn;
        obj[type+fn] = function() {
          obj["e"+type+fn]( window.event );
        };
        obj.attachEvent( "on"+type, obj[type+fn] );
      }
    },
    initLightwindow: function(){
        if (jQuery('#lightwindow') != null)  {
           if (jQuery('#lightwindow').parent().get(0) != jQuery('body').get(0) ) {
               jQuery('#lightwindow').prependTo('body');
               jQuery('#map-overlay').appendTo('body');
           }
        }    
    },
    showLightwindow: function(){
        if (jQuery('#lightwindow') != null) lightwindow.show();
    }
}

app.addLoadEvent(app.init);

//Object ImageViewer
function ImageViewer(id, viewer_index){
    
    this.id = id;
    this.obj = d_obj(id);
    this.image_obj = null;
    this.a_image_obj = null;
    this.viewer_index = viewer_index;
    
    this.items = new Array();
    this.image_thumbs = new Array();
    this.image_view_box = new Array();
    this.image_full_screen = new Array();
    this.current_index = null;
    
    this.init = function(){
        if (this.obj) {
            
            //init view box
            this.image_obj = d_obj(this.id+'_image');
            this.a_image_obj = this.image_obj.parentNode;
            
            if (this.image_obj) {
                var items_div = document.getElementById('thumbs');
                
                var items = items_div.getElementsByTagName('div');
                
                for (var i = 0; i<items.length; i++) {
                    
                    this.items[i] = items[i];
                    
                    //a-Tag
                    var anchors = items[i].getElementsByTagName('a');
                    var attr_href = anchors[0].getAttribute('href');
                    this.image_full_screen[i] = attr_href;
                    anchors[0].setAttribute('href', 'javascript:void(0)');
                    
                    //img-Tag
                    var imgs = items[i].getElementsByTagName('img');
                    this.image_thumbs[i] = imgs[0].getAttribute('src');
                    this.image_view_box[i] = imgs[0].getAttribute('rel');
                    
                    items[i].setAttribute('id',this.id+'_item_'+i);
                    items[i].setAttribute('index',i);
                    items[i].setAttribute('viewer_index',this.viewer_index);
                    
                    
                    //Events
                    app.addEvent(items[i],'mouseover', function(){
                        var viewer_index = this.getAttribute('viewer_index');
                        app.iv_list[viewer_index].flashItem(this);
                    });
                    
                    app.addEvent(items[i],'mouseout', function(){
                        var viewer_index = this.getAttribute('viewer_index');
                         app.iv_list[viewer_index].restoreItem(this);
                    });
                    
                    app.addEvent(items[i],'click', function(){
                        var viewer_index = this.getAttribute('viewer_index');
                        app.iv_list[viewer_index].selectItem(this);
                    });
                    
                    //Init first element
                    if (i==0) {
                        this.current_index = 0;
                        this.items[i].className = 'item act';
                        this.image_obj.setAttribute('src',this.image_view_box[i]);
                        
                        if (!this.image_full_screen[i]) this.a_image_obj.setAttribute('href',this.image_view_box[i]);
                        else this.a_image_obj.setAttribute('href',this.image_full_screen[i]);
                        
                        this.a_image_obj.setAttribute('onclick',"return app.showImageOverlay(this)");
                        this.a_image_obj.onclick = function(){ app.showImageOverlay(this); return false; };
                        
                        
                    }
                    
                }//for
                
                
            }//if (this.image_obj)
        }
    };
    
    this.selectItem = function (obj) {
        var i = obj.getAttribute('index');
        var viewer_index = obj.getAttribute('viewer_index');
        var iv = app.iv_list[viewer_index];
        
        var last_index = iv.current_index;
        iv.current_index = i;
        
        iv.items[last_index].className = 'item';
        iv.items[i].className = 'item act';
        
        iv.image_obj.setAttribute('src',iv.image_view_box[i]);
        
        iv.a_image_obj.onclick = function(){ app.showImageOverlay(this); return false; };
        
        if (!iv.image_full_screen[i]) iv.a_image_obj.setAttribute('href',iv.image_view_box[i]);
        else iv.a_image_obj.setAttribute('href',iv.image_full_screen[i]);
        
    };
    
    this.flashItem = function (obj){
        var i = obj.getAttribute('index');
        var viewer_index = obj.getAttribute('viewer_index');
        var iv = app.iv_list[viewer_index];
    
        iv.image_obj.setAttribute('src',iv.image_view_box[i]);
    };
    
    this.restoreItem = function (obj){
        var i = obj.getAttribute('index');
        var viewer_index = obj.getAttribute('viewer_index');
        var iv = app.iv_list[viewer_index];
        var current_index = iv.current_index;
    
        iv.image_obj.setAttribute('src',iv.image_view_box[current_index]);
    };
    
}//ImageViewer(id)

//d_obj functions
function d_support() {
    return (document.layers ||
     document.all ||
     document.getElementById);
}
function d_style(id) {
    if (document.layers) {
 return d_obj(id);
    } else if (document.all || document.getElementById) {
 return d_obj(id).style;
    } 
 return null;
}
function d_obj(id) {
    if (document.layers) {
 return document.layers[id];
    } else if (document.all) {
 return document.all[id];
    } else if (document.getElementById) {
 return document.getElementById(id);
    } else return null;
}
/* ****************************************************************** */
function openwindow(t_url, sbars, width, height, bnav) {
    var wx, wy;
    if (bnav == 1) {
    bnav = "menubar=yes,toolbar=yes,";
    } else {
    bnav = "";
    }
    if (sbars == 1) {
        scrollb = "scrollbars=yes,";
    } else {
        scrollb = "";
    }
    if (screen.availWidth >= width) {
        wx = (screen.availWidth - width) / 2;
    } else {
        wx = 0;
    }
    if (screen.availHeight >= height) {
        wy = (screen.availHeight - height) / 2;
    } else {
        wy = 0;
    }
    return window.open(t_url, '', scrollb + bnav + 'width=' + width + ',height=' + height + ',left=' + wx + ',top=' + wy + ', resizable=yes');
}
/* ****************************************************************** */
/* Formulare - Pflichtfelderbehandlung - Allgemeine Funktionen */ 
function markingField(id){
    d_obj(id).className = "red";
    fieldsValid = false;
}
function clearField(id){
    d_obj(id).className = "normal";
}
function markingRadioSet(id){
    d_obj(id).className = "radio_red";
    fieldsValid = false;
}
function clearRadioSet(id){
    d_obj(id).className = "radio_normal";
}