// Also known as IE fix
var TridentFix = new Class({
    tridentFix: function(item){
        item.addEvents({
            'mouseover':function(){
                this.addClass('iehover');
            },
            'mouseout':function(){
                this.removeClass('iehover');
            }
        });
    }
});


var DropMenu = new Class({
    Implements: [Options,TridentFix],
    /* 
        don't know about options yet
        but set it up anyways just in case 
    */
    options: {
        mode: 'vertical'
    },
    menu: null,
    initialize: function(menu,options){
        if(options) this.setOptions(options);
    
        this.menu = $(menu);
        
        // grab all of the menus children - LI's in this case
        var children = this.menu.getChildren();
        
        // loop through children
        children.each(function(item,index){
            // declare some variables 
            var fChild, list;
            
            /* 
                fChild = first child - which should be an A tag
                list = submenu UL
            */
            fChild = item.getFirst();
            list = fChild.getNext('ul');
            
            // check if IE, if so apply fix
            if(Browser.Engine.trident) this.tridentFix(item);
            
            // if there is a sub menu UL
            if(list){
                item.mel = list; // pel = parent element
                list.pel = item; // mel = menu element
                new SubMenu(list); // hook up the subMenu
            }
        },this); // binding loop to this object for trident fix

    }    
});



var SubMenu = new Class({
    Implements: [Options,TridentFix],
    /* 
        don't know about options yet
        but set it up anyways just in case 
    */
    options: {
        mode: 'vertical'
    },
    menu: null, // storage for menu object
    depth: 0, // storage for current menu depth
    initialize: function(el,depth,options){
        if(options) this.setOptions(options); // set options
        if(depth) this.depth = depth;// set depth
        
        this.menu = el; //attach menu to object
        
        if(this.depth == 0)    this.menu.addClass('submenu'); // class for first level
        if(this.depth >= 1)    this.menu.addClass('sub_submenu'); // class for deeper levels - in case :P
        
        this.menu.fade('hide'); // set menu to hid

        /*
            hook up menu's parent with event
            to trigger menu
        */
        this.menu.pel.addEvents(this.parentEvents); 
        
        // get menu's child elements
        var children = this.menu.getChildren();
            
        // loop through children
        children.each(function(item,index){
            // declare some variables 
            var fChild, list;
            
            /* 
                fChild = first child - which should be an A tag
                list = submenu UL
            */
            fChild = item.getFirst();
            list = fChild.getNext('ul');
            
            // check if IE, if so apply fix
            if(Browser.Engine.trident) this.tridentFix(item);
            
            // if the menu item has a sub_submenu
            if(list){
                /*
                    create marker for menu item
                    that has a sub_submenu
                    this is to show persistence and 
                    where you are in the menu tree
                */
                var count = new Element('span').set('html','\&raquo;').addClass('counter');
                
                item.adopt(count); // stuff it inside li
                count.fade('hide'); // hide it

                item.mel = list; // mel = menu element
                item.count = count; // attach count accessor to menu item
                list.pel = item; // pel = parent element
                
                // create new subMenu with depth incremented
                new SubMenu(list,this.depth+1);
            }
        },this); //bound to this for trident fix
    },
    // menu parent mouse events
    parentEvents: {
        'mouseover': function(){
            /*
                if it has a count accesor
                then fade it in 
            */
            if(this.count) this.count.fade('in');
            
            // fade in menu
            this.mel.fade('in');        
        },
        'mouseout': function(){
            /*
                if it has a count accesor
                then fade it out 
            */
            if(this.count) this.count.fade('out');
            
            // fade out menu
            this.mel.fade('out');
        }
    }
});

Fx.Elements = new Class({

    Extends: Fx.CSS,

    initialize: function(elements, options){
        this.elements = this.subject = $$(elements);
        this.parent(options);
    },

    compute: function(from, to, delta){
        var now = {};
        for (var i in from){
            var iFrom = from[i], iTo = to[i], iNow = now[i] = {};
            for (var p in iFrom) iNow[p] = this.parent(iFrom[p], iTo[p], delta);
        }
        return now;
    },

    set: function(now){
        for (var i in now){
            var iNow = now[i];
            for (var p in iNow) this.render(this.elements[i], p, iNow[p], this.options.unit);
        }
        return this;
    },

    start: function(obj){
        if (!this.check(arguments.callee, obj)) return this;
        var from = {}, to = {};
        for (var i in obj){
            var iProps = obj[i], iFrom = from[i] = {}, iTo = to[i] = {};
            for (var p in iProps){
                var parsed = this.prepare(this.elements[i], p, iProps[p]);
                iFrom[p] = parsed.from;
                iTo[p] = parsed.to;
            }
        }
        return this.parent(from, to);
    }

});

function hasCookies() {
  return document.cookie.length > 0;
}

function setCookie(key, value) {
	document.cookie = key + "=" + escape(value)+'; path=/';;
}

function getCookie(key) {
  if (hasCookies()) {
    var cookies = ' ' + document.cookie;
  }
  else {
    return NaN;
  }
  var start = cookies.indexOf(' ' + key + '=');
  if (start == -1) { return NaN; }
  var end = cookies.indexOf(";", start);
  if (end == -1) { end = cookies.length; }
  end -= start;
  var cookie = cookies.substr(start,end);
  return unescape(cookie.substr(cookie.indexOf('=') + 1, cookie.length - cookie.indexOf('=') + 1));
}

function getFontSize() {
  var body = document.getElementById('body');
  var size = parseInt(body.style.fontSize);
  if (isNaN(size)) {
    size = parseInt(getCookie('fontsize'));
  }
  return size;
}

function setFontSize(size) {
  var body = document.getElementById('body');
  body.style.fontSize = size +'px';
  setCookie('fontsize',size);
  check_heights();
}

function biggerFont() {
	var size = getFontSize();
	document.getElementById('a_small').style.textDecoration = "none";
	document.getElementById('a_reset').style.textDecoration = "none";
	document.getElementById('a_big').style.textDecoration = "underline";
	if (!isNaN(size) && (size < 17)) {
		size+=1;
    setFontSize(size);
	}
  else {
    if (isNaN(size)) {
      size=11;
		  setFontSize(size);
    }
  }
  return void(0);
}

function smallerFont() {
	var size = getFontSize();
	document.getElementById('a_small').style.textDecoration = "underline";
	document.getElementById('a_reset').style.textDecoration = "none";
	document.getElementById('a_big').style.textDecoration = "none";
	if (!isNaN(size) && (size > 10)) {
		size-=1;
    setFontSize(size);
	}
  else {
    if (isNaN(size)) {
      size=11;
      setFontSize(size);
    }
  }
}

function resetFont () {
	document.getElementById('a_small').style.textDecoration = "none";
	document.getElementById('a_reset').style.textDecoration = "underline";
	document.getElementById('a_big').style.textDecoration = "none";
	setFontSize(12);
}

function defaultFont() {
	if(hasCookies()) {
    	var size = parseInt(getCookie('fontsize'));
    	if (!isNaN(size)) {
	    	var body = document.getElementById('body');
		  	body.style.fontSize = size +'px';
		  	//alert(size);
    	}
	}
	else {
		setFontSize(11);
	}
}

function initFont() {
	defaultFont();
	//alert(document.cookie);
}

var 	vidcont1 = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"120\" height=\"87\" id=\"cjd_small\" align=\"middle\"><param name=\"allowScriptAccess\" value=\"sameDomain\" /><param name=\"movie\" value=\"/media/public/sites/cjd/video/CJD_home.swf?skin=/media/public/sites/cjd/video/CJDExternalPlayMute_klein.swf&targetURL=javascript:mov_large()&contentPath=/media/public/sites/cjd/video/cjd_final_kl.flv&autoPlay=true\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#a00\" /><param name=\"wmode\" value=\"transparent\" /><embed id=\"cjd_small\" src=\"/media/public/sites/cjd/video/CJD_home.swf?skin=/media/public/sites/cjd/video/CJDExternalPlayMute_klein.swf&targetURL=javascript:mov_large()&contentPath=/media/public/sites/cjd/video/cjd_final_kl.flv&autoPlay=true\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#a00\" width=\"120\" height=\"87\" align=\"middle\" name=\"cjd_small\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macomedia.com/go/getflashplayer\" /></object>";
		vidcont2 = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"320\" height=\"230\" id=\"cjd_large\" align=\"middle\"><param name=\"allowScriptAccess\" value=\"sameDomain\" /><param name=\"movie\" value=\"/media/public/sites/cjd/video/CJD_cont.swf?skin=/media/public/sites/cjd/video/CJDExternalAll.swf&contentPath=/media/public/sites/cjd/video/cjd_final.flv&autoPlay=true\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"fff\" /><param name=\"wmode\" value=\"transparent\" /><embed id=\"liedtke_large\" src=\"/media/public/sites/cjd/video/CJD_cont.swf?skin=/media/public/sites/cjd/video/CJDExternalAll.swf&contentPath=/media/public/sites/cjd/video/cjd_final.flv&autoPlay=true\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#fff\" width=\"320\" height=\"230\" align=\"middle\" name=\"cjd_small\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macomedia.com/go/getflashplayer\" /></object>";
		smallcont = "<img style=\"border: 0pt none ; margin: 0px\" title=\"\" alt=\"\" src=\"/media/public/sites/cjd/video/cjd_small_mv.jpg\" usemap=\"#mov\" align=\"left\" height=\"87\" width=\"120\" /><br clear=\"all\">";

function mov_small() {
	document.getElementById('small_movie').innerHTML = vidcont1;
	}

function mov_large() {
	document.getElementById('small_movie').innerHTML = smallcont;
	document.getElementById('large_movie').style.visibility = "visible";
	document.getElementById('large_movie1').innerHTML = vidcont2;	
	}
	
function close_window() {
	document.getElementById('large_movie').style.visibility = "hidden";
	document.getElementById('large_movie1').innerHTML = "&nbsp;";
	}

function check_heights() {
    var tb = document.getElementById ( "teaser-boxes" );
	var lh = document.getElementById ( "teaser-box1-content" );
    if (!lh) return;
	var lc = document.getElementById ( "teaser-box2-content" );
	var lr = document.getElementById ( "teaser-box3-content" );
    var prop = window.ie6 ? 'height' : 'height';
    lh.style[prop] =  'auto';
    lc.style[prop] =  'auto';
    lr.style[prop] =  'auto';
    tb.style[prop] =  'auto';
    
	//alert(document.documentElement.clientWidthById ( "teaser-hbox-1" ));
	var mx = Math.max( lh.offsetHeight,lc.offsetHeight ) ;
	mx = Math.max( mx,lr.offsetHeight );
    mx -= 7;
	lh.style[prop] =  mx + 'px';
	lc.style[prop] =  mx + 'px';
    lr.style[prop] =  mx + 'px';
	tb.style[prop] =  (mx+46) + 'px';
	}

var Galleries = {
    start: function() {
        $$('.mooflow').each(function(gallery){
            Galleries.init(gallery)
        });   
    },
    init: function(container){

        var mf = new MooFlow($(container), {
            startIndex: 0,
            useSlider: true,
            useAutoPlay: true,
            useCaption: true,
            useResize: true,
            useMouseWheel: true,
            useKeyInput: true,
            useViewer:true,
            onClickView: function(obj){
                var img = new Element('img',{src:obj.src, title:obj.title, alt:obj.alt, styles:obj.coords}).setStyles({'position':'absolute','border':'none'});
                var title = (obj.title || '')+(obj.title && obj.alt ? ' - ' : '')+(obj.alt || '');
                var link = new Element('a',{'class':'remooz-element','href':obj.href,'title':title, styles:{'border':'none'}});
                $(document.body).adopt(link.adopt(img));
                var remooz = new ReMooz(link, {
                    centered: true,
                    resizeFactor: 0.8,
                    origin: link.getElement('img'),
                    onCloseEnd: function(){link.destroy()}
                });
                remooz.open();
            }
        });
        
    }
    
};

if (window.addEvent) { 
    window.addEvent('load',function(){
//            menu = new DropMenu('menu-v');
            check_heights();
            Galleries.start();
            if ($('header-zentrale')) {
                var so = new SWFObject("/media/public/sites/cjd/swf/local_head_2011.swf", "mymovie", "881", "262", "7", "#ffffff");
                so.write("header-zentrale");
            }
            else if ($('header-profil-ac')) {
                var so = new SWFObject("/media/public/sites/cjd/swf/head_profil_ac.swf", "mymovie", "881", "262", "7", "#ffffff");
                so.write("header-profil-ac");
            }
            else if ($('header-anna')) {
                var so = new SWFObject("/media/public/sites/cjd/swf/head_anna.swf", "mymovie", "881", "262", "7", "#ffffff");
                so.write("header-anna");
            }
            else if ($('header-asthmazentrum')) {
                var so = new SWFObject("/media/public/sites/cjd/swf/head_asthmazentrum.swf", "mymovie", "881", "262", "7", "#ffffff");
                so.write("header-asthmazentrum");
            }
            else {
                return;
            }
    })
}

