var a_pages = {}, s_current_page,

gayArticles = function(){
	var o_cont;
	try {
		o_cont = $$('div.gaynl div.text')[0];
	} catch (oEx){
		//console.log(oEx);
		return;
	}
	
	$A(arguments[0]).each(function(oArticle, iIndex){
		if (iIndex > 4){
			return;
		}
		
		var o_a = new Element('a', {
			'text': oArticle.title,
			'href': oArticle.url,
			'target': '_blank'
		}).inject(o_cont).addEvent('mouseover', function(){
			$('artImg').set('src', this.data.image).set('alt', this.data.title);
		});
		o_a.data = oArticle;
		// preload images
		(new Element('img', {'src': oArticle.image}));
	});
},

outtvSchedule = function(oData){
	var o_cont, i_index, s_now, s_hours, s_minutes;
	try {
		o_cont = $$('div.outtv div.text')[0];
	} catch (oEx){
		//console.log(oEx);
		return;
	}
		
	try {
		$$('div.outtv span.date')[0].set('text', oData.date.replace(/^\d{4}(\d{2})(\d{2})$/, '$2-$1'));
		$$('div.outtv span.day')[0].set('text', oData.day);
	} catch (oEx){
		//console.log(oEx);
		return;
	}
	
	s_hours = new Date().getHours(); s_minutes = new Date().getMinutes();
	s_now = (s_hours.toString().length == 1 ? '0' : '') + s_hours + ':' + (s_minutes.toString().length == 1 ? '0' : '') + s_minutes;
	// figure out when to start
	var b_isset = false;
	$A(oData.shows).each(function(oShow, iIndex){
		if (s_now >= oShow.begin && s_now < oShow.end && b_isset === false){
			i_index = Math.max(0, (iIndex - 1));
			//i_index = iIndex;
			b_isset = true;
		}
	});
	
	$A(oData.shows).filter(function(oShow, iIndex){
		return (iIndex >= i_index && iIndex < (i_index + 5));
	}).each(function(oShow, iIndex){
		var o_a = new Element('a', {
			'text': oShow.title,
			'href': 'http://www.out.tv/nl/tv-gids',
			'class': (iIndex === 1 ? 'nw': ''),
			'target': '_blank'
		}).inject(o_cont);
		(new Element('span', {'text': oShow.begin, 'class': 'time'})).inject(o_a, 'top');
	});
},

handlePage = function(sPage){
	var s_html;
	if (s_current_page == sPage){
		return;
	}
	
	$('main').removeClass('more').addClass('loading');
	
	if ($type(a_pages[sPage]) === false){
		(new Request({
			'url': '/pages.php?' + $H({'page': sPage}).toQueryString(),
			'async': false,
			'method': 'get',
			'onComplete': function(sHTML){
				s_html = sHTML;
				a_pages[sPage] = sHTML;
			}
		})).send();
	} else {
		s_html = a_pages[sPage];
	}
	
	$('OM_content').set('html', s_html);
	
	$('OM_content').getElements('a.close').each(function(oA){
		oA.removeEvents().addEvent('click', function(oEvent){
			oEvent.stop();
			
			$('main').removeClass('more');
		});
	});
	
	$('main').removeClass('loading').addClass('more');
};

window.addEvent('domready', function(){
	$$('div.menu div.menu_item').each(function(oItem){
		oItem.addEvent('mouseover', function(){
			this.addClass('hover');
		}).addEvent('mouseout', function(){
			this.removeClass('hover');
		}).addEvent('click', function(oEvent){
			oEvent.stop();
			handlePage(this.get('id'));
		});
	});
	
	// SWF player
	swfobject.embedSWF('/assets/swf/OM_player.swf', 'OM_player', '340', '246', '9.0.28', '', {'host': 'fms.gaymedia.nl'}, {'allowScriptAccess': 'always', 'wmode': 'transparent'}, {});
	
	// current & upcoming program
	(new Request.JSON({
		'url': '/program.php',
		'method': 'get',
		'onComplete': function(oResponse){
			var s_now, s_next;
			try {
				s_now = oResponse.now[0].toString().replace(/^(\d{1,2})(\d{2})$/, '$1:$2');
				s_next = oResponse.next[0].toString().replace(/^(\d{1,2})(\d{2})$/, '$1:$2');
				$$('div.playing.now div.info p')[0].set('html', (s_now.length == 4 ? '0' : '') + s_now + ' <strong>' + oResponse.now[1] + '</strong>');
				$$('div.playing.next div.info p')[0].set('html', (s_next.length == 4 ? '0' : '') + s_next + ' <strong>' + oResponse.next[1] + '</strong>');
			} catch (oEx){
				//console.log(oEx);
				return;
			}
		}
	})).send();
	
	// Gay.nl articles
	(new Element('script', {'src': 'http://www.gay.nl/json/jsonp/articles.json?callback=gayArticles'})).inject($$('body')[0]);
	
	// Outtv schedule
	(new Element('script', {'src': '/outtv.php?callback=outtvSchedule'})).inject($$('body')[0]);
	
	$$('div.button').each(function(oBtn){
		oBtn.addEvent('click', function(oEvent){
			oEvent.stop();
			handlePage('programmering');
		});
	});
	
	try {
		$$('div.news.outtv')[0].addEvent('click', function(){
			window.open('http://www.out.tv/');
		});
	} catch (x){}
});
