/*  Media Converter - Desktop Manager
 *  (c) 2009 Pascal Beyeler
 *--------------------------------------------------------------------------*/

var desktopManager = Class.create({

	initialize: function() {

		//Flash is required
		if(!checkFlash()) {
			return false;
		}

		this.zindex = 1;
		this.desktop_w = false;
		this.desktop_h = false;
		this.current_hash = '';
		this.initDesktop();
		window.onresize = this.showSidebarAds.bind(this);

	},

	initDesktop: function() {

		//set up the cross domain flash
		f4a_setup();

		//create the dock
		new fisheye("menu",{"proximity":100,"position":"bottom","gap":3});

		//open the default windows
		this.desktop_w = $('desktop').getWidth() - 200;
		this.desktop_h = $('desktop').getHeight() - 95;

		if(this.desktop_w >= 860) {
			this.openWindow('wizard');
			this.openWindow('downloads');
			this.openWindow('news');
		}else{
			this.openWindow('wizard');
		}

		//load the sidebar
		this.loadSidebar();

	},

	loadSidebar: function() {

		new Ajax.Request('/sidebar/index/' + Math.random(), {
			method: 'get',
			contentType: 'text/html' ,
			onSuccess: function(transport) {

				$('sidebar').update(transport.responseText);

				//calculate the amount of ads which can be shown at once
				this.showSidebarAds();

			}.bind(this)
		});

	},

	showSidebarAds: function() {

		if(!$('sidebar_adsection')) {
			return;
		}

		var sidebar_height = parseInt($('sidebar').getHeight());
		var adsection_top = parseInt($('sidebar_adsection').cumulativeOffset().top);
		var space = sidebar_height - adsection_top - 150;
		var show_ads = Math.floor(space / 100);

		//get the content and display the window
		new Ajax.Request('/banner/sidebar/' + show_ads + '/' + Math.random(), {
			method: 'get',
			contentType: 'text/html' ,
			onSuccess: function(transport) {

				$('sidebar_adsection').update(transport.responseText);

			}

		});

	},

	openWindow: function(type) {

		if(!$(type)) {
			//create the new window
			var newwindow = document.createElement('div');
			newwindow.setAttribute('id',type);
			newwindow.setAttribute('class',type);
			newwindow.setAttribute('className',type);
			newwindow.style.zIndex = this.zindex++;

			//default positions
			if(type == 'wizard') {
				if(this.desktop_w >= 860) {
					newwindow.style.left = '35px';
					newwindow.style.top = '135px';
				} else {
					newwindow.style.left = '235px';
					newwindow.style.top = '70px';
				}
			}else if(type == 'downloads') {
				if(this.desktop_w >= 860) {
					newwindow.style.left = '670px';
					newwindow.style.top = '110px';
				}else{
					newwindow.style.left = '250px';
					newwindow.style.top = '150px';
				}
			}else if(type == 'news') {
				if(this.desktop_w >= 860) {
					newwindow.style.left = '590px';
					newwindow.style.top = '350px';
				}else{
					newwindow.style.left = '300px';
					newwindow.style.top = '140px';
				}
			}

			//get the content and display the window
			new Ajax.Request('/' + type + '/index/' + Math.random(), {
				method: 'get',
				contentType: 'text/html' ,
				onSuccess: function(transport) {
					setTimeout(function() {
						var content = transport.responseText;
						newwindow.innerHTML = content;
						newwindow.style.opacity = 0;
						newwindow.style.filter = 'alpha(opacity=0)';

						new Effect.Opacity(newwindow, { from: 0, to: 1, duration: 0.3, beforeStart: function() { $('desktop').appendChild(newwindow) }});
						new Draggable(newwindow, {handle: $(type).firstDescendant(), starteffect: false, endeffect: false, zindex: '9999999'});
						Event.observe($(type), 'click', this.increaseZindex.bind(this));

						var browser_agent = navigator.userAgent.toLowerCase();
						if (browser_agent.indexOf("firefox/3") == -1) {
							content.evalScripts();
						}
					}.bind(this), 0);

				}.bind(this)
			});

		}else{
			new Effect.Shake($(type));
			$(type).style.zIndex = this.zindex++;
		}

	},

	openLightWindow: function(type, h, w) {

		if(!$(type)){
			myLightWindow = new lightwindow();
			myLightWindow.activateWindow({
				title: '',
				caption: '',
				href: type,
				height: h,
				width: w,
				type: 'external',
				iframeEmbed: 'true'
			});
		}

	},

	closeWindow: function(obj) {

		new Effect.Opacity(obj, { from: 1.0, to: 0, duration: 0.3, afterFinish: function() { $('desktop').removeChild(obj) }});

	},

	increaseZindex: function(evt) {

		var obj = false;
		if (evt.target) {
			if (evt.currentTarget && (evt.currentTarget != evt.target)) {
				obj = evt.currentTarget
			} else {
				obj = evt.target
			}
		} else {
			obj = evt.srcElement;
			max = 6;
			i = 0;
			while(obj.parentNode.id != 'desktop' && i < max){
				obj = obj.parentNode;
				i++;
			}
		}
		obj.style.zIndex = this.zindex++;

	},

	toggleLanguages: function() {

		$('language_languages').style.width = $('language_selection').getWidth() - 22;
		Effect.toggle('language_languages', 'blind', { duration: 0.1 });

	},

	switchLanguage: function(language) {

		new Ajax.Request('/language/switchto/' + language + '/' + Math.random(), {
			method: 'get',
			onSuccess: function(transport) {

				//reload the window
				var curr_location = window.location.href;
				window.location.href = curr_location.slice(0,curr_location.lastIndexOf('/'));

			}
		});

	}

});

Event.observe(window, 'load', function() {window.desktop = new desktopManager();});



function checkFlash() {

	if (FlashDetect.versionAtLeast(8)) {
		return true;
	} else {

		var fatal_error = document.createElement('div');
		fatal_error.className = 'fatal_error';
		fatal_error.id = 'fatal_error';
		document.body.appendChild(fatal_error);

		//get the error message
		new Ajax.Request('/home/flashrequired/' + Math.random(), {
			method: 'get',
			contentType: 'text/html' ,
			onSuccess: function(transport) {

				$('fatal_error').update(transport.responseText);

			}.bind(this)
		});

		return false;
	}

}

