window.addEvent('domready', function() {
	
	if (window.DD_belatedPNG) {
    // We enter all CSS selectors mathcing elements with background images
    // containing alpha transparency.
    DD_belatedPNG.fix([
      '.bubble-white-top', '.bubble-white-middle', '.bubble-white-bottom'
    ].join(', '));
  }
	
	document.body.addClass('js');
	
    /* row 2 */
    var js_hide = $$('.js-hide');
    js_hide.each(function (element) {
        element.store('height', element.getHeight());
    });
    js_hide.setStyles({
      'overflow': 'hidden',
      'position': 'relative',
      'height': '0'
    });
    
    var row2 = $('row2');
    
    $$('.min-height').setStyles({
        'height': 0,
        'min-height': 0
    });
    
    var row2_elements = $('row2').getElements('.bubble');
    row2.getElement('.wrapper').setStyle('height', row2_elements.getHeight());
    
    if(row2_elements[0] != null) {
        row2_elements[0].setStyle('left', '-5px');
    }
    if(row2_elements[1] != null) {
        row2_elements[1].setStyle('left', '300px');
    }
    if(row2_elements[2] != null) {
        row2_elements[2].setStyle('left', '600px');
    }
    
    row2_elements.addEvent('mouseenter', function() {
        var js_hide = this.getElements('.js-hide');
        js_hide.tween('height', js_hide.retrieve('height'));
        this.getElements('.js-show').setStyle('display', 'none');
    });
    
    row2_elements.addEvent('mouseleave', function() {
        this.getElements('.js-hide').tween('height', '0px');
        this.getElements('.js-show').setStyle('display', 'block');
    });
    /* row2 end */

    
    /* row3 */

    $$('#row3 .bubble h5').setStyle('color', '#fff');
    $$('#row3 .bubble span').setStyles({
        'color': '#fff',
        'font-weight': 'normal'
    });
    
    /* bubble 1 & 2 mouseenter (silver & red bubbles) */
    $('row3').getElements('.bubble').addEvent('mouseenter', function() {
        this.getElements('h5').setStyle('color', '#000');
        this.getElements('span').setStyle('color', '#C40022');
        this.getElements('.bubbles-white-top').setStyle('background', 'url(/fileadmin/templates/home2010/images/row3-bubble-white-top.gif) no-repeat 32px top');
        this.getElements('.bubbles-white-middle').setStyle('background', 'url(/fileadmin/templates/home2010/images/row3-bubble-white-middle.png) repeat-y 32px 0');
        this.getElements('.bubbles-white-bottom').setStyle('background', 'url(/fileadmin/templates/home2010/images/row3-bubble-white-bottom.png) no-repeat 0 bottom');
        
        var js_hide = this.getElements('.js-hide');
        js_hide.tween('height', js_hide.retrieve('height'));
        this.getElements('.js-show').setStyle('display', 'none');
    });
    
    /* bubble 1 mouseleave (silver) */
    if($('row3-bubble-1') != null) {
        $('row3-bubble-1').addEvent('mouseleave', function() {
            $('row3-expand-1-show').get('tween').start('height', '0px').chain(function(){
   
                $$('.bubble-silver .bubbles-white-top').setStyle('background', 'url("fileadmin/templates/home2010/images/row3-bubble-silver-top.png") no-repeat 33px top');
                $$('.bubble-silver .bubbles-white-middle').setStyle('background', 'url("fileadmin/templates/home2010/images/row3-bubble-silver-middle.png") repeat-y 33px 0');
                $$('.bubble-silver .bubbles-white-bottom').setStyle('background', 'url("fileadmin/templates/home2010/images/row3-bubble-silver-bottom.png") no-repeat 0 bottom');
    
                $('row3-expand-1-hide').setStyle('display', 'block');
                $$('#row3 .bubble-silver h5').setStyle('color', '#fff');
                $$('#row3 .bubble-silver span').setStyle('color', '#fff');
            });
        });
    }
    
    /* bubble 2 mouseleave (red) */
    if($('row3-bubble-2') != null) {
        $('row3-bubble-2').addEvent('mouseleave', function() {
            $('row3-expand-2-show').get('tween').start('height', '0px').chain(function(){
    
                $$('.bubble-red .bubbles-white-top').setStyle('background', 'url("fileadmin/templates/home2010/images/row3-bubble-red-top.png") no-repeat 33px top');
                $$('.bubble-red .bubbles-white-middle').setStyle('background', 'url("fileadmin/templates/home2010/images/row3-bubble-red-middle.png") repeat-y 33px 0');
                $$('.bubble-red .bubbles-white-bottom').setStyle('background', 'url("fileadmin/templates/home2010/images/row3-bubble-red-bottom.png") no-repeat 0 bottom');
                
                $('row3-expand-2-hide').setStyle('display', 'block');
                $$('#row3 .bubble-red h5').setStyle('color', '#fff');
                $$('#row3 .bubble-red span').setStyle('color', '#fff');
            }); 
        });
    }
});

(function () {
	/*
	---

	script: Request.JSONP.js

	name: Request.JSONP

	description: Defines Request.JSONP, a class for cross domain javascript via script injection.

	license: MIT-style license

	authors:
	  - Aaron Newton
	  - Guillermo Rauch

	requires:
	  - Core/Element
	  - Core/Request
	  - /Log

	provides: [Request.JSONP]

	...
	*/

	Request.JSONP = new Class({

		Implements: [Chain, Events, Options],

		options: {/*
			onRetry: $empty(intRetries),
			onRequest: $empty(scriptElement),
			onComplete: $empty(data),
			onSuccess: $empty(data),
			onCancel: $empty(),
			log: false,
			*/
			url: '',
			data: {},
			retries: 0,
			timeout: 0,
			link: 'ignore',
			callbackKey: 'callback',
			injectScript: document.head
		},

		initialize: function(options){
			this.setOptions(options);
			this.running = false;
			this.requests = 0;
			this.triesRemaining = [];
		},

		check: function(){
			if (!this.running) return true;
			switch (this.options.link){
				case 'cancel': this.cancel(); return true;
				case 'chain': this.chain(this.caller.bind(this, arguments)); return false;
			}
			return false;
		},

		send: function(options){
			if (!$chk(arguments[1]) && !this.check(options)) return this;

			var type = $type(options),
					old = this.options,
					index = $chk(arguments[1]) ? arguments[1] : this.requests++;
			if (type == 'string' || type == 'element') options = {data: options};

			options = $extend({data: old.data, url: old.url}, options);

			if (!$chk(this.triesRemaining[index])) this.triesRemaining[index] = this.options.retries;
			var remaining = this.triesRemaining[index];

			(function(){
				var script = this.getScript(options);
				this.fireEvent('request', script);
				this.running = true;

				(function(){
					if (remaining){
						this.triesRemaining[index] = remaining - 1;
						if (script){
							script.destroy();
							this.send(options, index).fireEvent('retry', this.triesRemaining[index]);
						}
					} else if(this.running && script && this.options.timeout){
						script.destroy();
						this.cancel().fireEvent('failure');
					}
				}).delay(this.options.timeout, this);
			}).delay(Browser.Engine.trident ? 50 : 0, this);
			return this;
		},

		cancel: function(){
			if (!this.running) return this;
			this.running = false;
			this.fireEvent('cancel');
			return this;
		},

		getScript: function(options){
			var index = Request.JSONP.counter,
					data;
			Request.JSONP.counter++;

			switch ($type(options.data)){
				case 'element': data = document.id(options.data).toQueryString(); break;
				case 'object': case 'hash': data = Hash.toQueryString(options.data);
			}

			var src = options.url +
				 (options.url.test('\\?') ? '&' :'?') +
				 (options.callbackKey || this.options.callbackKey) +
				 '=Request.JSONP.request_map.request_'+ index +
				 (data ? '&' + data : '');

			var script = new Element('script', {type: 'text/javascript', src: src});
			Request.JSONP.request_map['request_' + index] = function(){ this.success(arguments, script); }.bind(this);
			return script.inject(this.options.injectScript);
		},

		success: function(args, script){
			if (!this.running) return false;
			if (script) script.destroy();
			this.running = false;
			this.fireEvent('complete', args).fireEvent('success', args).callChain();
		}

	});

	Request.JSONP.counter = 0;
	Request.JSONP.request_map = {};
})();

window.addEvent('domready', function() {
    
	function since(date) {
		var diff		= ((new Date).getTime() - date.getTime()) / 1000,
				message = 'a long time',
				timespan;
	
		if (diff < 60) {
			timespan = Math.floor(diff);
			message	 = '{timespan} second';
		}
		else if (diff < 3600) {
			timespan =	Math.floor(diff / 60);
			message	 = 'about {timespan} minute';
		}
		else if (diff < 86400) {
			timespan = Math.floor(diff / 3600);
			message	 = 'around {timespan} hour';
		}
		else if (diff < 604800) {
			timespan = Math.floor(diff / 86400);
			message	 = 'about {timespan} day';
		}
		else if (diff < 2419200) {
			timespan = Math.ceil(diff / 604800);
			message	 = 'nearly {timespan} week';
		}
		else if (diff < 29030400) {
			timespan = Math.floor(diff / 2419200);
			message	 = 'about {timespan} month';
		}
	
		if (timespan !== 1 && timespan) {
			message += 's';
		}
	
		return message.replace('{timespan}', timespan) + ' ago';
	}
	
	function linkify(message) {
	    return message.replace(/(https?:\/\/\S+)/, '<a class="red" href="$1">$1</a>')
	      .replace(/(^|\s)@(\w+)/, '$1@<a class="red" href="http://twitter.com/$2">$2</a>')
	      .replace(/(^|\s)#(\w+)/, '$1#<a class="red" href="http://search.twitter.com/search?q=%23$2">$2</a>');
	}
	if($('tweets') != null && $('tweets-list') != null) {
    	var API_URL  = 'http://api.twitter.com/{version}/statuses/user_timeline.{format}',
    	    username = 'rackspaceemea',
    	    template = '{message}<br /><a class="subtitle" href="http://twitter.com/{user}">{time} from our tweets</a>',
    	    tweets   = $('tweets'),
    	    list     = $('tweets-list').set('morph');
    	    
    	// Request the latest tweets.
    	tweets.addClass('loading');
    	(new Request.JSONP({
    		url: API_URL.substitute({version: 1, format: 'json'}),
    		data: { screen_name: username },
    		onSuccess: function (json) {
    			tweets.removeClass('loading');
    			json.forEach(function (tweet) {
    				var item = new Element('li'),
    				    date = new Date();
    
    				date.setTime(Date.parse(tweet.created_at.replace(/\+\d{4}/, '')));
    	
    				// Need to highlight urls at @replies.
    				item.set('html', template.substitute({
    					message: linkify(tweet.text),
    					time: since(date),
    					user: username
    				}));
    	
    				list.grab(item);
    			});
    		}
    	})).send();
    	
    	list.set('morph', {onComplete: function () { list.store('animating', false); }});
    	
    	var navigation = tweets.getElements('.nav').addEvent('click', function (event) {
    		var width     = 320, // Calculate this dynamically.
    		    direction = (this.hash === '#next') ? -1 : 1,
    		    current   = list.getPosition(list.getOffsetParent()).x;		
    	
    		// If this button is disabled do nothing.
    		if (this.hasClass('disabled') || list.retrieve('animating')) {
    			return;
    		}
    	
    		navigation.removeClass('disabled');
    	
    		// If we're about to scroll to the first item disable the prev button.
    		if (direction === 1  && -current < (width * 2)) {
    			this.addClass('disabled');
    		}
    
    		// If we're about to scroll to the last item disable the next button.
    		if (direction === -1 && -current >= (list.getChildren().length - 2) * width) {
    			this.addClass('disabled');
    		}
    
    	    list.store('animating', true);
    		list.morph({left: current + (direction * width)});
    
    		if (event) { event.stop(); }
    	});
    }
});

