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 OpenPopup(windowUri, windowWidth, windowHeight, windowName)
{
    var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = (window.screen.height - windowHeight) / 2;

    newWindow = window.open(windowUri, windowName, 'resizable=0,width=' + windowWidth +
        ',height=' + windowHeight +
        ',left=' + centerWidth +
        ',top=' + centerHeight);

    newWindow.focus();
    return newWindow.name;
}


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),
        onSuccess: (params.onsuccess ? params.onsuccess : function() {
            new Effect.Appear(params.target);
        }),
        onComplete: (params.oncomplete ? params.oncomplete : function() {
            
        })
    });

    return false;
}

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'});

	}
	
}

function toggleNewsletter(div) {
    var div_1 = 'us_newsletters';
    var div_2 = 'country_newsletters';

    if ( div == div_1 ) {
        $(div_1).show();
        $(div_2).hide();
    }

    if ( div == div_2 ) {
        $(div_2).show();
        $(div_1).hide();
    }
}

function recordOutboundLink(link, category, action, label, ga_code_region, ga_code) {
    
    try {
        var pageTracker=_gat._getTracker(ga_code);
        pageTracker._trackEvent(category, action, label);

        if ( ga_code != ga_code_region) {
            var pageTracker2=_gat._getTracker(ga_code_region);
            pageTracker2._trackEvent(category, action, label);
        }

    }catch(err){}
    setTimeout('window.open("' + link.href + '", "_blank")', 200);
}

function updateVideoTeaser(location, poster, title) {
    $('video_player').fade({to:0.01, duration:0.1});
    $('video_player').src = location;
    $('video_player').poster = poster;
    $('movie_title').innerHTML = title;
    $('video_player').appear({from: 0.1, duration:0.5, delay:0.1});
}


//----------------------------------------------------------------------------
//
// 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();
        this.setupCountryDropout();
    },

    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.accordion_active')[0]);

            $$("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) {}

        try {
            var country_selector_accordion = new accordion('country_selector_accordion');
            

        } catch(e) {}
    },

    setupCountryDropout: function() {
        $('country_dropout_btn').observe('click', function(){
            if($('country_dropout').visible()) {
                $('country_dropout').slideUp({duration:0.2});
                $('country_dropout_btn').down('ins').removeClassName('close');
            } else {
                $('country_dropout').slideDown({duration:0.2});
                $('country_dropout_btn').down('ins').addClassName('close');
            }
            return false;
        });
    }
}

DTI.TextboxDefaultText = Class.create();

DTI.TextboxDefaultText.prototype = {

    initialize: function() {
        properties = $A(arguments);
        this.default_text = properties[1].text;
        this.alert_text = properties[1].alert;
        this.search_box = $(properties[0]);
        this.doValidation = properties[1].validate;
        this.search_form = $(this.search_box.form);

        if (this.search_box.value == '') {
            this.search_box.value = this.default_text;
        }

        Event.observe(this.search_box, "focus", this.textboxFocus.bind(this), false);
        Event.observe(this.search_box, "blur", this.textBoxBlur.bind(this), false);
        Event.observe(this.search_form, "submit", this.validateSearchForm.bind(this), false);
    },

    textboxFocus: function(ev) {
        if (this.search_box.value == this.default_text) {
            this.search_box.value='';
        }
    },

    textBoxBlur: function(ev) {
        if (this.search_box.value == '') {
            this.search_box.value=this.default_text;
        }
    },

    validateSearchForm: function(ev) {
        if (this.search_box.value == this.default_text) {
            this.search_box.value='';
        }
    }
}
