var Menu = Class.create({

	elements: null,

	initialize: function() {
		this.elements = [];
		menuItems = $$('div#menu-top li.menu-top-item');
		for (var i = 0; i < menuItems.length; i++) {
			this.elements[i] = new Menu_Item(menuItems[i]);
		}
		$$('body')[0].appendChild($('sub-menu'));
	}

});

var Menu_Item = Class.create({

	element: null,
	submenu: null,
	id: null,
	timer: null,
	submenuContainer: null,

	initialize: function(htmlElement) {
		this.element = $(htmlElement);
		this.id = this.element.identify();
		this.timer = false;

		this.submenuContainer = $('sub-menu');
		this.submenu = $('sub-menu-top-' + this.id);

		if (!this.submenu) {
			return;
		}

		this.submenu.show = function() {
			this.submenuContainer.appendChild(this.submenu);
			this.submenu.removeClassName('hide');
		}.bind(this);
		this.submenu.hide = function() {
			this.submenu.addClassName('hide');
			if (this.timer !== false) {
				clearTimeout(this.timer);
				this.timer = false;
			}
		}.bind(this);

		this.submenu.setStyle({left: this.element.cumulativeOffset().left + 'px'});
		Event.observe(this.element, 'mouseover', this.menuMouseover.bindAsEventListener(this));
		Event.observe(this.submenu, 'mouseover', this.submenuMouseover.bindAsEventListener(this));
		Event.observe(this.element, 'mouseout', this.menuMouseout.bindAsEventListener(this));
		Event.observe(this.submenu, 'mouseout', this.submenuMouseout.bindAsEventListener(this));

	},

	menuMouseover: function(event) {
		if (this.timer !== false) {
			clearTimeout(this.timer);
			this.timer = false;
		}
		this.submenu.show();
	},

	submenuMouseover: function(event) {
		if (this.timer !== false) {
			clearTimeout(this.timer);
			this.timer = false;
		}
	},

	menuMouseout: function(event) {
		this.timer = setTimeout(this.submenu.hide, 300);
	},

	submenuMouseout: function(event) {
		this.timer = setTimeout(this.submenu.hide, 300);
	}

});



function twitterCallback(data) {
	var twitterContainer = $('twitter-container');
	for (var i = 0; i < data.length; i++) {
		var twitterItem = new Element('div', {className: 'twitter-item'});
		var twitterAva = new Element('a', {className: 'twitter-item-ava', href: 'http://twitter.com/' + data[i].user.screen_name});
		var twitterAuthor = new Element('a', {className: 'twitter-item-author', href: 'http://twitter.com/' + data[i].user.screen_name}).update(data[i].user.name);
		var twitterDate = new Element('a', {className: 'twitter-item-date', href: 'http://twitter.com/' + data[i].user.screen_name + '/status/' + data[i].id});
		var twitterText = new Element('div', {className: 'twitter-item-text'}).update(data[i].text);

		twitterAva.setStyle({backgroundImage: 'url(\'' + data[i].user.profile_image_url + '\')'});


		var dt = data[i].created_at.match(new RegExp('^([A-Za-z]+) (.*) ([0-9]+)$'));
		if (dt.length !== 4) {
			continue;
		}
		dt = dt[3] + ' ' + dt[2];
		var dateObject = new Date(dt);

//		var month = dateObject.getMonth() + 1 + '';
//		if (month.length < 2) {
//			month = '0' + month;
//		}
//		var day = dateObject.getDate() +'';
//		if (day.length < 2) {
//			day = '0' + day;
//		}
		var minutes = dateObject.getMinutes() +'';
		if (minutes.length < 2) {
			minutes = '0' + minutes;
		}
		var hours = dateObject.getHours() +'';
		if (hours.length < 2) {
			hours = '0' + hours;
		}

		var dateText = /*day + '.' + month + '&nbsp;' + */ hours + ':' + minutes;
		twitterDate.update(dateText);


		twitterItem.appendChild(twitterAva);
		twitterItem.appendChild(twitterAuthor);
		twitterItem.appendChild(twitterDate);
		twitterItem.appendChild(twitterText);
		twitterContainer.appendChild(twitterItem);
	}
}


var youtubeWidget;

Date.prototype.setISO8601 = function(dString) {
	var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

	if (dString.toString().match(new RegExp(regexp))) {
		var d = dString.match(new RegExp(regexp));
		var offset = 0;

		this.setUTCDate(1);
		this.setUTCFullYear(parseInt(d[1],10));
		this.setUTCMonth(parseInt(d[3],10) - 1);
		this.setUTCDate(parseInt(d[5],10));
		this.setUTCHours(parseInt(d[7],10));
		this.setUTCMinutes(parseInt(d[9],10));
		this.setUTCSeconds(parseInt(d[11],10));
		if (d[12]) {
			this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
		} else {
			this.setUTCMilliseconds(0);
		}
		if (d[13] != 'Z') {
			offset = (d[15] * 60) + parseInt(d[17],10);
			offset *= ((d[14] == '-') ? -1 : 1);
			this.setTime(this.getTime() - offset * 60 * 1000);
		}
	} else {
		this.setTime(Date.parse(dString));
	}
	return this;
};


var YoutubeWidget = Class.create({

	element: null,
	entries: null,
	feed: null,
	data: null,
	player: null,
	selectedIndex: null,
	items: null,

	initialize: function(data) {
		this.element = $('youtube-container');

		if (typeof(data) != 'object'
			|| typeof(data.feed) != 'object'
			|| typeof(data.feed.entry) != 'object'
			|| data.feed.entry.length < 1
		) {
			this.error();
			return;
		}

		this.player = null;
		this.items = [];
		this.data = data;
		this.feed = data.feed;
		this.entries = data.feed.entry;
		this.selectedIndex = 0;


		this.element.update(new Element('div', {id: 'youtube-player-wrap'}).update(new Element('div', {id: 'youtube-player'})));
		this.embed(false);




	},

	playerReady: function(playerApiId) {
		this.player = $('youtube-player');

		for (var i = 0; i < this.entries.length; i++) {
			this.items[i] = this.addItem(this.entries[i], i);
		}

		this.items[this.selectedIndex].addClassName('youtube-item-current');
		onYouTubePlayerReady = Prototype.emptyFuntion;
	},

	embed: function(autoplay) {
		var url = this.entries[this.selectedIndex].media$group.media$content[0].url + '&enablejsapi=1&playerapiid=youtubeOpenamerica&rel=0&border=0&fs=0&showinfo=0&egm=0&autoplay=' + (autoplay ? 1 : 0);
	    var params = {allowScriptAccess: 'always', allowfullscreen: 'false'};
	    var atts = {id: 'youtube-player'};
		swfobject.embedSWF(url, 'youtube-player', '260', '225', '9.0.0', false, false, params, atts);
	},

	error: function() {
		this.element.update('Error occured while loading videos. Try again later');
	},


	addItem: function(entry, index) {
		var youtubeItem = new Element('div', {className: 'youtube-item'});
		var youtubeDate = new Element('div', {className: 'youtube-item-date'});
		var youtubeText = new Element('a', {className: 'youtube-item-text', href: entry.link[0].href}).update(entry.title.$t);

		var dateObject = new Date();
		dateObject.setISO8601(entry.published.$t);
		var month = dateObject.getMonth() + 1 + '';
		if (month.length < 2) {
			month = '0' + month;
		}
		var day = dateObject.getDate() +'';
		if (day.length < 2) {
			day = '0' + day;
		}

		var dateText = day + '.' + month;
		youtubeDate.update(dateText);

		youtubeItem.appendChild(youtubeDate);
		youtubeItem.appendChild(youtubeText);
		this.element.appendChild(youtubeItem);

		Event.observe(youtubeText, 'click', this.loadVideo.bindAsEventListener(this, index));

		return youtubeItem;
	},

	loadVideo: function(event, index) {
		Event.stop(event);

		this.items[this.selectedIndex].removeClassName('youtube-item-current');
		this.selectedIndex = index;
		this.items[this.selectedIndex].addClassName('youtube-item-current');

		this.embed(true);
	},




	x: null
});

var onYouTubePlayerReady = Prototype.emptyFuntion;
function youtubeCallback(data) {
	youtubeWidget = new YoutubeWidget(data);
	onYouTubePlayerReady = youtubeWidget.playerReady.bind(youtubeWidget);
}
