var DTIApp = null;

function init() {
    if (arguments.callee.done) {
        return;
    }
    arguments.callee.done = true;
    DTIApp = new DTI.App();
}

document.observe("dom:loaded", function() {
    init();
});

//----------------------------------------------------------------------------
var win;
function OpenWin(wname,pfad,w,h) {
	if (Prototype.Browser.IE) {
		win = new Window({
				className:'dialog', title: wname, top:20, width:w, height:parseInt(document.viewport.getHeight()*0.9), url: pfad, showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}
		});
	} else {
		win = new Window({
				className:'mac_os_x', title: wname, top:20, width:w, height:parseInt(document.viewport.getHeight()*0.9), url: pfad, showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}
		});
	}
	win.setZIndex(500);
	win.showCenter();
}

function DocumentReload(params) {
	if (!params.target) {
		params.target = 'Content';
	}
    if (params.showprogress) {
        $(params.target).update('<img src="/images/dti_mobile_loader.gif"/>');
    }
    new Ajax.Updater(params.target, params.url , {
        asynchronous: true,
        evalScripts: false,
        parameters: params.parameters,
        insertion: (params.insertion ? params.insertion : false),
        onsucces: function() {
            new Effect.Appear(params.target);
        }
    });
}

function CMSDelete(element,options) {
    if (options.alert) {
        MyConfirm(options.alert,function() {
            new Ajax.Request(options.url, {
                parameters:options.params,
                onSuccess: function() {
                    new Effect.Puff(element);
                    return true;
                }
            });
            return true;
        });
    } else {
        new Ajax.Request(options.url, {
            parameters:options.params,
            onSuccess: function() {
                new Effect.Puff(element);
            }
        });
    }
    
    return false;
}

function MyConfirm(msg,func) {
	Dialog.confirm(msg, {
		width: 300,
        zIndex: 2000,
		okLabel: "Ok",
		cancelLabel: 'Abbrechen',
		className: "alert_lite",
		id: "myDialogId",
		cancel:function(win) {},
		ok:func
	});
}

function MyAlert(msg) {
	Dialog.alert(msg, {
		width: 300,
		okLabel: "Ok",
		cancelLabel: 'Abbrechen',
		className: "alert_lite",
		id: "myDialogId"
	});
}

function SendRequest(url,params) {
    new Ajax.Request(url,{
       parameters: params
    });
}

function fadeNAppearDiv(div){
	div_closed = div + "_open";
	div_link = div + "_link";
	
	if($(div_closed).style.display!="none"){
		$(div_link).className ="acco_down";
		new Effect.SlideUp(div_closed, {duration:0.3, queue: 'front'});
	}
	else{

		$(div_link).className ="acco_up";
		new Effect.SlideDown(div_closed, {duration:0.5, queue: 'front'});

	}
	
}


//----------------------------------------------------------------------------
//
// DTI.App Class
//
//----------------------------------------------------------------------------

// DTI-Namespace
var DTI = new Object();

DTI.App = Class.create();

DTI.App.prototype = {

    /**
     * Init-Funktion
     */
    initialize: function() {

        DTIApp = this;
        this.setupHover('#Navi a');
        this.setupAccordion();
    },

    setupHover: function(select) {
        $$(select).each(function(el) {
           el.observe('mouseover',MotorApp.setHoverActive.bindAsEventListener(MotorApp,el));
           el.observe('mouseout',MotorApp.setHoverDeactive.bindAsEventListener(MotorApp,el));
           if (el.hasClassName('active')) {
               MotorApp.setHoverActive(this, el);
           }
        });
    },

    setHoverActive: function(event,el) {
        el.down().src = el.down().src.replace(/_a\./g, '.');
        el.down().src = el.down().src.replace(/(\.[a-z]{3})$/g, '_a\$1');
    },

    setHoverDeactive: function(event,el) {
        if (!el.hasClassName('active')) {
            el.down().src = el.down().src.replace(/_a\./g, '.');
        }
    },

    setupAccordion: function() {
        try {
            var bottomAccordion = new accordion('vertical_container');
			// Open first one
			//bottomAccordion.activate($$('#vertical_container .accordion_toggle')[2]);
			$$("h1.accordion_toggle").each(function(el) { 
				Event.observe(el, 'click', function(ev) {
					$('region_pic').setStyle({backgroundImage: 'url(/images/region_pic_'+ev.target.readAttribute('rel')+'.gif)'});
				});	
			});

		} catch(e) {}
    }
}