/* irc.ftou.gr	Client side code
 *
 * Author:		Aris Koufogiannakis
 * Copyright:	ftou.gr ©2002-2008
 *
 */ 


function setjs() {
	// set the field for the cgi:irc form
	if(navigator.product == 'Gecko' || (window.opera && document.childNodes)) {
		document.loginform["interface"].value = 'mozilla';
	} else if(window.opera && document.childNodes) {
		document.loginform["interface"].value = 'opera7';
	} else if(navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf("Mac_PowerPC") > 0) {
		document.loginform["interface"].value = 'konqueror';
	} else if(navigator.appName == 'Microsoft Internet Explorer' && document.getElementById && document.getElementById('ietest').innerHTML) {
		document.loginform["interface"].value = 'ie';
	} else if(navigator.appName == 'Konqueror') {
		document.loginform["interface"].value = 'konqueror';
	} else if(window.opera) {
		document.loginform["interface"].value = 'opera';
	}
}



function nickvalid() {
	// check for strange characters - nickname validation
	var nick = document.loginform.Nickname.value;
	if (nick.length > 0) {
		if(nick.match(/^[A-Za-z0-9\[\]\{\}^\\\|\_\-`]{1,32}$/)) return true;
		alert("Ôï nick ðïõ Ýäùóåò Ý÷åé ìÝóá ðåñßåñãïõò  ÷áñáêôÞñåò.\nÄþóå Ýíá óùóôü nickname.");
	} else {
		document.loginform.Nickname.style.border = "1px solid red";
		//document.loginform.Nickname.style.background = "#f77";
		$('nickbox').tween('background-color', '#FFCC00', '#FF7777');
		document.loginform.Nickname.style.color = "#000";
		document.loginform.Nickname.focus();
	}
   
	document.loginform.Nickname.value = nick.replace(/[^A-Za-z0-9\[\]\{\}^\\\|\_\-`]/g, '');
	return false;
}

// replace any strange characters
function replaceGreek(strO){

	var strI = strO;

	// greek letters
	strI = strI.replace(/á|Ü|Á|¶/g, "a");
	strI = strI.replace(/â|Â/g, "v");
	strI = strI.replace(/ã|Ã/g, "g");
	strI = strI.replace(/ä|Ä/g, "d");
	strI = strI.replace(/å|Ý|Å|¸/g, "e");
	strI = strI.replace(/æ|Æ/g, "z");
	strI = strI.replace(/ç|Þ|Ç|¹/g, "h");
	strI = strI.replace(/è|È/g, "8");
	strI = strI.replace(/é|ß|ú|À|É|º|Ú/g, "i");
	strI = strI.replace(/ê|Ê/g, "k");
	strI = strI.replace(/ë|Ë/g, "l");
	strI = strI.replace(/ì|Ì/g, "m");
	strI = strI.replace(/í|Í/g, "n");
	strI = strI.replace(/î|Î/g, "ks");
	strI = strI.replace(/ïõ|ïý|ÏÕ/g, "ou");
	strI = strI.replace(/ï|Ï|¼|ü/g, "o");
	strI = strI.replace(/ð|Ð/g, "p");
	strI = strI.replace(/ñ|Ñ/g, "r");
	strI = strI.replace(/ó|Ó|ò/g, "s");
	strI = strI.replace(/ô|Ô/g, "t");
	strI = strI.replace(/õ|ý|û|à|Õ|¾|Û/g, "y");
	strI = strI.replace(/ö|Ö/g, "f");
	strI = strI.replace(/÷|×/g, "x");
	strI = strI.replace(/ø|Ø/g, "ps");
	strI = strI.replace(/ù|þ|¿|Ù/g, "w");
	// replace the spaces
	strI = strI.replace(/ |-/g, "_");
	// kill any other trash
	strI = strI.replace(/\.|\,|\!|\~|\`|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\||\\|\/|\<|\>|\'|\"|\:|\;|\{|\}|\[|\]|\´|\?|\//g, "");

	return strI;
}

function showUsersInChan(chan, box){
	// ajax call to show connected nicknames
	if (!box) return false;
	box.innerHTML = '<div class="loading"></div>';
	var responseTxt = '';
	var req = new Request.HTML({
		url:'jsusers.php',
		onSuccess: function(tree,elms,html){
				var fillBox = (function(){box.innerHTML = html}).delay(200);
		}
	}).get({'channel': chan, 'row': 1}); 
}


var Site = {

	start: function(){

		if (window != top) top.location.href = location.href;

		//handle the menu
		if ($('leftmenu')){

			var elmCounter = 0;
			var hiddenmenuSlide = new Array();

			$$('.hiddenmenu').each(function(e){
					e.setProperty('id', 'hiddenmenu' + elmCounter++);
					hiddenmenuSlide[e.id] = new Fx.Slide(e.id, {
					duration: 350,
					fps: 60,
					link: 'chain',
					transition: Fx.Transitions.Expo.easeIn
				}).hide();
				e.setStyle('visibility', 'visible');
			});

			elmCounter = 0;

			$$('.toplevel').each(function(e){
				if (e.hasClass('active')) {
					hiddenmenuSlide['hiddenmenu' + elmCounter].show();
				}

				e.addEvent('mouseenter', function(event){
			
					if ( ! e.hasClass('active') && ! e.hasClass('highlight')) {
						hiddenmenuSlide[e.getParent('div').getElement('div').getElement('.hiddenmenu').getProperty('id')].slideIn();
						$$('.highlight').each(function(elm){
							if (elm.id != e.getParent('div').getElement('div').getElement('.hiddenmenu').getProperty('id')) {
								hiddenmenuSlide[elm.getParent('div').getElement('div').getElement('.hiddenmenu').getProperty('id')].slideOut();
								elm.removeClass('highlight');
							}
						});
						e.addClass('highlight');
					}
				});

				elmCounter++;

			});

		
		}

		if ($('nickbox')) $('nickbox').focus();

		if ($('dynamicUsers')){ 
		   	showUsersInChan($('chancombo')[$('chancombo').selectedIndex].value.replace(/^#/,''), $('dynamicUsers'));
			$('chancombo').addEvent('change', function(event){
				event.stop();
				showUsersInChan($('chancombo')[$('chancombo').selectedIndex].value.replace(/^#/,''), $('dynamicUsers'));
			});
		}

		if ($('nickbox')){
			$('nickbox').addEvent('blur', function(event){
				Site.sanitizeNickname($('nickbox'));
			});
		}

		var tweetsToShow = 3;

		if ($('moreTweety')){
			$('moreTweety').addEvent('click', function(ev){
				ev.stop();
				tweetsToShow += tweetsToShow;
				if (tweetsToShow > 15) tweetsToShow = 20;
				//$('tweety').innerHTML = '<div class="loading"></div>';
				Site.printTweeters(tweetsToShow);
				if(tweetsToShow >= 20) $('moreTweety').setStyle('display', 'none');
			});
		}

		if($('tweety')) Site.printTweeters(tweetsToShow);

	},

	sanitizeNickname: function(box){
		box.value=replaceGreek(box.value);
	},
	
	printTweeters: function(amount){
		if(!$('tweety')) return false;
		getTwitters('tweety', { 
			id: 'ftou', 
			count: amount, 
			enableLinks: true, 
			ignoreReplies: true, 
			clearContents: true,
			template: '%text% <span>(<a href="http://twitter.com/%user_screen_name%/statuses/%id%/" target="_blank">%time%</a>)</span>',
			timeout: 15,
			onTimeout: function(){
				$('tweety').innerHTML = 'Ç Ôïõßôá åßíáé down.'; 
				if($('moreTweety')) $('moreTweety').setStyle('display', 'none');
			},
			onTimeoutCancel: true
		});
	}

};

window.addEvent('domready', Site.start);


