// common.js
// 18/11/2008
var configUtils = {
	limit: function(element, length) {
		if(element.value.length > length) element.value = element.value.substring(0, length);
	},
	isEmpty: function(element) {
		var whiteSpace = /^\s+$/;
		return (element.value.length == 0 || whiteSpace.test(element.value));
	},
	trim: function(element) {
		return element.innerHTML.replace(/^[ \t]+|[ \t]+$/g, '');
	}
};
Element.addMethods(configUtils);

var Img = {
	normal: function(element) {
		element.src = element.src.replace(/\_hover.png/, '.png').replace(/\_down.png/, '.png');
	},
	hover: function(element) {
	   var source = element.src;
	   source = source.replace(/_hover/, '');
	   element.src = source.replace(/\.png/, '_hover.png');
	},
	down: function(element) {
		element.src = element.src.replace(/_hover\.png/, '_down.png');
	}
};

var Search = {
	defaultText: '',
	toggle: {
		'filter': function(on) { $('sr_filter_holder').className = 'sr_holder_'+(on ? 'on' : 'off'); $('types_cont').style.display = (on ? '' : 'none'); },
		'sat': function(on) { $('sr_sat_holder').className = 'sr_holder_'+(on ? 'on' : 'off'); $('sat_cont').style.display = (on ? '' : 'none'); },
		'all': function() { this['filter'](false); this['sat'](false); }
	},
	set: function(type, text, id) {
		$(type).value = id;
		this.toggle[type](false);
		$('sr_'+type+'_holder').innerHTML = text;
	},
	zoom: function(type) {
		switch(type) {
			case 'focus': if($('query').value == this.defaultText) { $('query').style.color = '#666666'; $('query').value = ''; } break;
			case 'blur':
				if($('query').value == this.defaultText || $('query').value.length == 0) {
					$('query').style.color = '#BFBFBF'; $('query').value = this.defaultText;
				}
				break;
		}
	},
	submit: function() {
		if(($('query').value == this.defaultText || $('query').value.length == 0) && $('sat').value != 'all' && $('filter').value == 'all') {
			var Template = new Element('input',{type:'hidden', name:'gotosat', value:'true'});
			$('search_form').insert({bottom: Template});
			return true;
		}
		if($('query').value == this.defaultText || $('query').value.length == 0) {
			return false;
		}
		return true;
	}
};

function closeMenus(e) {
	var c = (typeof(window.event) != 'undefined' ) ? e.srcElement : e.target;
	var list = {
		className: ['typeItem','areaItem','iSelectItem','countryItem','satName','satDeg','list_areas']
	};
	if(list.className.indexOf(c.className) == -1) {
		Search.toggle['all']();
		Sat.closeLang();
		if($('country_holder')) Register.toogleList(false);
		if($('sat_holder')) satList.toogleList(false, 'all');
		return;
	}
}

function handleInput(e, run) {
	var asc = document.all ? event.keyCode : e.which;
	if(asc == 13) {
		run();
	}
}

/* -------------------------------------------------------------------------------------------------------------------------- */

var Sat = {
	openLang: function() {
		$('select_lang').className = 'select_lang_on'; $('lang_inner').show();
	},
	closeLang: function() { $('select_lang').className = 'select_lang_off'; $('lang_inner').hide(); },
	open: function() { if($('sat_list').innerHTML.length > 10) { Sat.getList('Groups',''); } else { Search.toggle['sat'](true); } },
	cache: {},
	over: function(box) {
		$(box).setStyle({backgroundColor: '#DDEDFF', borderColor: '#8CC2FF'});
		$(box+'_d').setStyle({backgroundColor: '#FFFFFF', borderColor: '#B1B1B1'});
	},
	out: function(box) {
		$(box).setStyle({backgroundColor: '#FFFFFF', borderColor: '#CFCFCF'});
		$(box+'_d').setStyle({backgroundColor: '#FFFFF0', borderColor: '#DFDFDF'});
	},
	loadingText: '',
	currentText: '',
	listRunning: false,
	setT: function(text) {
		return {r: text.replace('^','&deg;&nbsp;').replace('&nbsp;E','&nbsp;East').replace('&nbsp;W','&nbsp;West'),
				v: text.replace('^','').replace('.0','')};
	},
	getList: function(area, degree) {
		if(this.listRunning) return;
		var parseText = function(area, text, degree) {
			var output = '<ul>';
			for(i=0;i<text.length;i++) {
				output += '<li'+(area == 'Groups' ? ' style="width:260px;"'  : '')+' id="si_'+text[i].i+'" onclick="Search.set(\'sat\',\''+(area == 'Groups' ? Sat.setT(text[i].d).r : text[i].n)+'\',\''+Sat.setT(text[i].i).v+'\');" onmouseover="Sat.over(this.id);" onmouseout="Sat.out(this.id);" class="typeItem">'+
						'<table class="typeItem" border="0" cellspacing="0" cellpadding="0"><tr><td onclick="Search.set(\'sat\',\''+(area == 'Groups' ? Sat.setT(text[i].d).r : text[i].n)+'\',\''+Sat.setT(text[i].i).v+'\');" id="si_'+text[i].i+'_d" class="satDeg">'+text[i].d.replace('^','&deg;')+'</td><td class="satName">'+text[i].n+'</td></tr></table></li>'+"\n";
			} output += '</ul>';
			
			$('sat_list').innerHTML = output;
			var current_areas = $$('a.areaItem');
			for(i=0;i<current_areas.length;i++) {
				$(current_areas[i]).setStyle({backgroundColor: (current_areas[i].id == 'area_'+area ? '#5B9AD9' : ''), color: (current_areas[i].id == 'area_'+area ? '#FFFFFF' : ''), borderColor:(current_areas[i].id == 'area_'+area ? '#2970B6' : '')});
			}
			$('area_current').innerHTML = area +' <font class="ea">('+text.length+') </font> - <font class="ea2">'+degree+'</font>';
			$('sat_list').scrollTop = '0'; $('sat_cont').style.width = '445px'; $('sat_cont').style.marginLeft = '-304px'; $('sat_list_td').show();
			Search.toggle['sat'](true);
			
			return text;
		};
		if(this.cache[area] != null) {
			parseText(area, this.cache[area], degree);
		} else {
			var gatherList = new XHConn();
			gatherList.connect('/ajax','GET','job=sat_list&area='+escape(area),{
					onStart:function(){ Sat.listRunning = true; Sat.currentText =  $('area_sel_text').innerHTML; $('area_sel_text').innerHTML = Sat.loadingText; },
					onFinish:function(){ Sat.listRunning = false; $('area_sel_text').innerHTML = Sat.currentText; }
				},  function(result) { Sat.cache[area] = parseText(area, JSON.parse(result.responseText).sats, degree); }		   		   
			);
			gatherList = null;
		}
	},
	toggle: function(layer) {
		if($(layer[0]).style.display != 'none') {
			$(layer[0]).hide(); $(layer[1]).show();
		} else {
			$(layer[1]).hide(); $(layer[0]).show();
		}
	},
	bookmark: function() {
		var L = {url: 'http://www.sathint.com', title: 'SATHINT'};
		if(window.sidebar) {
			window.sidebar.addPanel(L.title, L.url,'');	
		} else if (window.external) {
			window.external.AddFavorite(L.url,L.title);
		} else if(window.opera && window.print){
			var elem = document.createElement('a');
			elem.setAttribute('href',L.url);
			elem.setAttribute('title',L.title);
			elem.setAttribute('rel','sidebar');
			elem.click();
		} 
	},
	showCountryList: function() {
		if($('cl_holder').style.display == 'none') {
			if($('cl_holder').innerHTML.length < 50) {
				var getList = new XHConn();
				getList.connect('/ajax','GET','job=country_list',{
						onStart:function(){ $('cl_holder').show(); $('cl_holder').innerHTML = '<div style="padding:6px;"><font style="color:#FFFFFF;"><b>Loading country list...</b></font></div>'; },
						onFinish:function(){}
					},
					function(result) {
						$('cl_holder').innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><div id="cl_index">'+
												   '</div></td></tr><tr><td bgcolor="#5B9AD9" style="color:#FFF; padding:5px; text-align:center;">'+
												   '<a style="text-decoration:none; color:#FFF" href="javascript:Sat.showCountryList();">CLOSE</a></td></tr></table>';
						var countries = JSON.parse(result.responseText);
						var out =  '<ul>';
						for(var ISOcode in countries) {
							out += '<li class="countryItem"><img align="left" src="images/flags/'+ISOcode+'.png" width="16" height="11" border="0" />&nbsp;'+countries[ISOcode]+' - <b>'+ISOcode+'</b></li>'+"\n";
						}
						out += '</ul>';
						$('cl_index').innerHTML = out;
					}		   		   
				);
				getList = null;
			} else {
				$('cl_holder').show();
			}
		} else {
			$('cl_holder').hide();
		}
	}
};

var Tip = {
	tableText: '<div id="n_tp_sat"><fieldset><legend>Satellite</legend><span id="n_tp_sat_m"></span></fieldset></div>'+
			   '<div id="n_tp_mod"><fieldset><legend>Modulation</legend><span id="n_tp_mod_m"></span></fieldset></div>'+
			   '<div id="n_tp_prov"><fieldset><legend>Provider</legend><table cellspacing="0"><tr><td id="n_tp_ti"><img id="n_tp_timg" src="images/spacer.gif" align="left" width="55" height="35" /></td><td id="n_tp_pname"></td></tr></table></fieldset></div>'+
			   '<div id="n_tp_info"><fieldset><legend>Transponder Info</legend><span id="n_tp_info_m"></span></fieldset></div>'+
			   '<div id="n_tp_count"><fieldset><legend>Channel Counts</legend><span id="n_tp_count_m"></span></fieldset></div>',
	tp: function(box, text) {
		if($('n_tp_text').innerHTML.length < 10) {
			$('n_tp_text').innerHTML = this.tableText;
		}
		// Satellite ----------------------------------------->
		if(text.sat) {
			$('n_tp_sat_m').innerHTML = text.sat;
			$('n_tp_sat').show();
		} else {
			$('n_tp_sat').hide();
		}
		// Modulation --------------------------------------->
		if(text.mod) {
			$('n_tp_mod_m').innerHTML = '<font style="color:#00578C">'+(text.mod.indexOf('8PSK') != -1 ? 'DVB-<font style="background-color:#00578C; color:#FFFFFF;padding:1px;"><b>S2</b></font> (8PSK)' : text.mod)+'</font>';
			$('n_tp_mod').show();
		} else {
			$('n_tp_mod').hide();
		}
		// Provider ----------------------------------------->
		if(text.prov) {
			if(text.tlogo) {
				$('n_tp_timg').src = 'tplogos/'+text.tlogo+'.png';
				$('n_tp_ti').show();
			} else { $('n_tp_ti').hide(); }
			$('n_tp_pname').innerHTML = text.prov;
			$('n_tp_prov').show();
		} else {
			$('n_tp_prov').hide();
		}
		// TP Info ----------------------------------------->
		if(text.info) {
			var band = text.info[2].toUpperCase();
			var band_icon = (band == 'C' || band == 'KU' ? '<img src="images/band_'+band.toLowerCase()+'.png" width="8" height="8" /> <font class="'+(band == 'C' ? 'red' : 'dgreen')+'">'+band+'</font>' : '<font class="red">'+band+'</font>');
			$('n_tp_info_m').innerHTML = 'Band: '+band_icon+', TID: <font class="red">'+text.info[0]+'</font>, NID: <font class="red">'+text.info[1]+'</font>';
			$('n_tp_info').show();
		} else {
			$('n_tp_info').hide();
		}
		// Counts ----------------------------------------->
		if(text.count) {
			$('n_tp_count_m').innerHTML = '<span class="punto10">TV: <b class="cd">'+text.count[0]+'</b>, HDTV: <b class="cd">'+text.count[1]+'</b>, Radio: <b class="cd">'+text.count[2]+'</b>, Clear: <b class="cg">'+text.count[3]+'</b>, Total: <b class="red">'+text.count[4]+'</b></span>';
			$('n_tp_count').show();
		} else {
			$('n_tp_count').hide();
		}
		var xPos = Position.positionedOffset($(box));
		$('n_tp').setStyle({top: ((xPos[1]-($('n_tp').offsetHeight/2))+6)+'px', left: (xPos[0]+137)+'px', visibility: 'visible'});

	},
	tph:function() { $('n_tp').setStyle({visibility: 'hidden'}); },
	s: function(mode, box, text) {
			if(typeof(text) == 'object') {
				if(text.sids) {
				var html  = '<fieldset class="tip_field"><legend>PIDS</legend>';
					html += 'SID: <b>'+text.sids[0]+'</b>, PMT: '+text.sids[1]+',<br/>PCR: '+text.sids[2]+', TXT: '+text.sids[3];
					html += '</fieldset>';
					if(text.cty) {
						html += '<fieldset class="tip_field"><legend>Country</legend>';
						if(text.cty.flag.length > 0) {
							html += '<img align="left" src="images/flags/'+text.cty.flag+'.png" width="16" height="11" />&nbsp;';
						}
						html += text.cty.name+'</fieldset>';
					}
					text = html;
					$(box).setStyle({backgroundColor: '#FFFF00'});
				}
			} else if(text.indexOf('[TP]') != -1) { 
				$('n_tooltip').setStyle({maxWidth:'400px'});
				text = text.split(',');
				text = TPL[text[1]].replace(/\[/g,'<').replace(/\]/g,'>');
			} else {
				if(mode == 'ce') { text = '<center>'+text+'</center>'; } 
				if(mode.indexOf('hplus') != -1) { var hplus = mode.split(','); }
			}
			$('n_tooltip_text').innerHTML = text.replace(/\[/g,'<').replace(/\]/g,'>');
			
			var xPos = Position.positionedOffset($(box));
			$('n_tooltip').setStyle({top: ((typeof(hplus) != 'undefined' ? parseInt(hplus[1]) : 0)+((xPos[1]-$('n_tooltip').offsetHeight)-1))+'px', left: (((xPos[0]+($(box).offsetWidth/2)) - ($('n_tooltip').offsetWidth/2))+1)+'px', visibility: 'visible'});
			if(mode == 'i') { $(box).setStyle({borderColor: '#B4B4B4'}); }
	},
	h: function(mode, box) {
		$('n_tooltip').setStyle({visibility: 'hidden'});
		if(mode == 'i') { $(box).setStyle({borderColor: '#DADADA'}); }
		if(mode == 'in') { $(box).setStyle({backgroundColor: '#FFFFFF'}); }
		if(mode == 'tp_out') { $(box).setStyle({borderColor: '#DADADA'}); $('n_tooltip').setStyle({maxWidth:'145px'}); }
	}
};

var cSel = {
	list: {},
	onHover: false,
	over: function(box) {
		for(var x in this.list) {
			if(x != box && $(x)) $(x).hide();
		}
		this.list[box] = true;
		$(box).show();
	},
	out: function(box) {
		this.list[box] = false;
		setTimeout(function() { if(!cSel.list[box]) $(box).hide(); },500);
	}	
};