/*  podcaster.de version 1.1.5 (c) 2008-2010 Jonas Girnatis, Fabio Bacigalupo */
var Podcaster = {
	init: function(){
		var ref = this;

		//progressivly enhance the DOM
		this.enhanceDom();

		//handle events
		this.registerEventHandlers();

 		//loadModules
		this.initPageLoadModule();

		//ie6 fixes
		if(typeof ltie7 != 'undefined'){ fixIE(); }

		this.startTracking();

        (function(){
            ref.fadeOutMessage(15.0);
        })();
	},

	pageLoadModules: [],

	addPageLoadModule: function(obj){
		this.pageLoadModules.push(obj);
	},

	initPageLoadModule: function(){
		this.pageLoadModules.invoke('init');
	},

	enhanceDom: function(){
		var ref = this;
	},

	registerEventHandlers: function(){
		var ref = this;

		if($('site')){
            $('site').select('.externicon').invoke('observe', 'click', function(e){ window.open(this.href); e.stop(); } );
		}

		if($('infomenu')) {
			$('infomenu').select('.action_show_login_form').invoke('observe', 'click', function(e){ ref.showLoginForm(); e.stop(); });
		}
	},

	toggleSubmenu: function(a){
		var submenu = a.next('ul');

		if(submenu.hasClassName('submenu')){
			submenu.removeClassName('submenu');
		}else{
			submenu.up('ul').select('.submenu').invoke('removeClassName', 'submenu');
			submenu.addClassName('submenu');
		}

		document.observe('mousedown', openSubmenuItem );
		function openSubmenuItem(e){
			if(!e.element().up('.submenu')) {
				submenu.up('ul').select('.submenu').invoke('removeClassName', 'submenu');
			}
			document.stopObserving('mousedown', openSubmenuItem);
		}
	},

	showLoginForm: function(a){
		$('login_box').appear({duration: 0.2, afterFinish: function(){
			$('login_email').activate();

			document.observe('mousedown', hideLoginForm );
			function hideLoginForm(e){
				if(!e.element().up('#login_box')) {
					$('login_box').fade({duration: 0.2});
					document.stopObserving('mousedown', hideLoginForm);
				}
			}
		}});
	},

	collapse: function(link, obj, saveId){
		var ref = this;
		link.onclick = null;

		saveId = (saveId) ? saveId : false;

		new Effect.BlindUp(obj, {duration: 0.25, afterFinish: function(){
			link.removeClassName('collapse');
			link.addClassName('expand');
			link.onclick = function(){ ref.expand(link, obj, saveId); return false; }

		} });

		if(saveId) { Cookie.set(saveId, 1, 90); }
	},

	expand: function(link, obj, saveId){
		var ref = this;
		link.onclick = null;
		saveId = (saveId) ? saveId : false;

		new Effect.BlindDown(obj, {duration: 0.25, afterFinish: function(){
			link.removeClassName('expand');
			link.addClassName('collapse');
			link.onclick = function(){ ref.collapse(link, obj, saveId); return false; }
		} });

		if(saveId) { Cookie.del(saveId); }
	},

	close: function(obj){
		new Effect.Fade(obj);
		Cookie.set('blk_whatis', 1, 90);
	},

	toggleRegisterCard: function(link, targetContainer){
		var targetCard = $(link).up();
		if(targetCard.hasClassName('current')) { return false };

		targetCard.addClassName('current').adjacent('li').invoke('removeClassName', 'current');

		if(targetContainer){
			targetContainer.adjacent('.toggleable').invoke('hide');
			targetContainer.show();
		}
		link.blur();
		return false;
	},

	makeUrl: function(params){
		//  calling -> makeUrl({'module':'mymodule', 'action':'generateReport', 'param2': 'foo bar'});
		var url = SGL_JS_WEBROOT;
		var moduleName  =  (params.module) ? params.module : '';
		var managerName =  (params.manager) ? params.manager : moduleName;
		url += SGL_JS_FRONT_CONTROLLER ? '/' + SGL_JS_FRONT_CONTROLLER : '';
		url += '/' + encodeURI(moduleName) + '/' + encodeURI(managerName) + '/';
		for (var x in params) {
			if ((x == 'module') || (x =='manager')) { continue; }
			url += encodeURI(x) + '/' + encodeURI(params[x]) + '/';
		}
		return url;
	},

	getAjaxUrl: function(params){
		var aSessId = SGL_JS_SESSID.split('=');
		params[aSessId[0]] = aSessId[1];

		return this.makeUrl(params);
	},

	popUp: function(url, options){
		options = options || {};
		var width = options.width || 400;
		var height = options.height || 300;
		var left = options.left || 0;
		var top = options.top || 0;
		var resizable = options.resizable || 'yes';
		var location = options.location || 'no';
		var scrollbars = options.scrollbars || 'no';
		var status = options.status || 'no';

		var name = options.name || 'popup';

		var popup = window.open(url, name, 'width='+width+',height='+height+',left='+left+',top='+top+',resizable='+resizable+',location='+location+',scrollbars='+scrollbars+',status='+status);
		popup.focus();
	},

	openPlayer: function(listId, type, showId){
		showId = showId ? showId : 0;
		var url = this.makeUrl({ module: 'player', action: 'list', listId: listId, type: type, showId: showId });
		this.popUp(url);
	},

	openerLink: function(link){
		try{
			var old = opener.location.href;
			opener.location.href = link.href;
			opener.focus();
		}catch(e){
			window.open(link.href, 'opener', 'left=0,top=0,resizable=yes,location=yes,scrollbars=yes,status=yes');
		}
	},

	getPlugins: function(mime){
		return Plugin.getPluginsForMimeType(mime);
	},

	startTracking: function(){
		if(typeof _gat != 'undefined'){
			var pageTracker = _gat._getTracker("UA-17486831-2");
			pageTracker._initData();
			pageTracker._trackPageview();
		}
	},

	raiseError: function(msg, target){
		var error = document.createElement('div');
		error.className = 'error';
		error.innerHTML = msg;
		target.parentNode.insertBefore(error, target);
	},

    showMessage: function(message, type) {
        var ref = this;
		ref.removeMessage();
        switch (type) {
        case 1:
            $('content').insert({ top: '<div id="messageboard" class="infoMessage">' + message +'</div>'});
            break;
        case 2:
            $('content').insert({ top: '<div id="messageboard" class="warningMessage">' + message +'</div>'});
            break;
        case 3:
            $('content').insert({ top: '<div id="messageboard" class="confirmMessage">' + message +'</div>'});
            break;
        case 0:
        default:
            $('content').insert({ top: '<div id="messageboard" class="errorMessage">' + message +'</div>'});
            break;
        }
    },

    fadeOutMessage: function(timeOut) {
        var ref = this;
        var mb = $('messageboard');
        if (mb){
            new Effect.Fade(
                'messageboard', {
                    duration: timeOut,
                    from: 1,
                    to: 0,
                    afterFinish: function() {
                        ref.removeMessage();
                    }
                }
            );
        }
    },

    removeMessage: function() {
        $('messageboard') ? $('messageboard').remove() : null;
    },

    toggleControl: function(container) {
        var ref = this;
        if(container.style.display == 'none'){
            window.setTimeout( function(){ document.onclick = function(){ ref.toggleControl(container); } }, 10);
        }else{
            document.onclick = null;
            //window.location.href = '#';
        }
        container.toggle();
    },

    showDialog: function(node, title, params, width){
        var ref = this;

        if(!width){
            width = 385;
        }

        Modalbox.show(node, {title: title, width: width, loadingString: 'Lädt. Bitte warten...', closeString: 'Schließen', slideDownDuration: 0.5, params: params});
    },
}

var Cookie = {
	set: function(name, value, days) {
		var expire = '';
		if (days) {
			var d = new Date();
			d.setTime(d.getTime() + (days * 86400000));
			expire = '; expires=' + d.toGMTString();
		}
		return (document.cookie = encodeURI(name) + '=' + encodeURI(value || '') + expire + '; path=/');
	},

	get: function(name) {
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + encodeURI(name) + '=([^;\\s]*)'));
		return (cookie ? decodeURI(cookie[2]) : null);
	},

	del: function(name) {
		var cookie = this.get(name) || true;
		this.set(name, '', -1);
		return cookie;
	}
};

function fixIE() {
	if($('menu')){
		$('menu').select('li.main').invoke('observe', 'mouseover', function(e){
			this.addClassName('hover');
		}).invoke('observe', 'mouseout', function(e){
			this.removeClassName('hover');
		});
	}
	try{ document.execCommand("BackgroundImageCache", false, true); }catch(e){}
}