/** Support function for AJAX **/

function callServerDispatcher(obj, scrollToTop) {
	if (obj.event != "NewsContainer" || obj.timer == 0) {
		clearTimer();
	}
	doStatGoogle();
	var ajaxRequest = new Ajax("class.AJAXServerDispatcher.php", {
		method: 'post',
		data: 'json=' + Json.toString(obj),
		onComplete: function(response){
//	debugInfo('response ' + response);
//	debugInfo('input elott');
			input = Json.evaluate(response);
//	debugInfo('input ' + Json.toString(input));
			$each(input, function(value, key){
//	debugInfo('value: ' + Json.toString(value) + ' key: ' + key);
//	debugInfo('id: ' + value.id);
				switch(value.action){
				   	case 'assign':
//	debugInfo('assign');
						var id = value.id;
						$each(value.attributes, function(value, key){
							switch(key){
								case 'innerHTML':
//	debugInfo('innerHTML of : ' + id);
//	debugInfo('value: ' + value);
//	debugInfo('value elotte: ' + $(id).innerHTML);
									$(id).setHTML(value);
//	debugInfo('value utana: ' + $(id).innerHTML);
									break;
								case 'className':
									$(id).className = value;
									break;
								case 'value':
									$(id).value = value;
									break;
							}
						});
						break;
				   	case 'script':
						eval(value.data);
						break;
				}
			});
		}
		}).request();	
	if (scrollToTop)
	window.scrollTo(0,0);
}

/** EOF AJAX Support functions **/

window.addEvent('load', function(){

	// ie6 flicker megállítása
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
//firebug in explorer
/*		
if (!window.console) {
  window.console = {
    timers: {},
    openwin: function() {
      window.top.debugWindow =
          window.open("",
                      "Debug",
                      "left=0,top=0,width=300,height=700,scrollbars=yes,"
                      +"status=yes,resizable=yes");
      window.top.debugWindow.opener = self;
      window.top.debugWindow.document.open();
      window.top.debugWindow.document.write('<html><head><title>debug window</title></head><body><hr /><pre>');
    },

    log: function(entry) {
      window.top.debugWindow.document.write(entry+"\n");
    },

    time: function(title) {
      window.console.timers[title] = new Date().getTime();
    },

    timeEnd: function(title) {
      var time = new Date().getTime() - window.console.timers[title];
      console.log(['<strong>', title, '</strong>: ', time, 'ms'].join(''));
    }

  }

  if (!window.top.debugWindow) { console.openwin(); }
}
*/
	
	// initialize the DHTML History
  	// framework
  	dhtmlHistory.initialize();
  	// subscribe to DHTML history change
  	// events
  	dhtmlHistory.addListener(historyChange);
	initNavigation();
	//flashplayer
	addPlayer();
	setTimeout("startTimer(0)",2000);
		})

function addPlayer(){
	var flowplayer = null;
	var playerConfig = {
  			controlsOverVideo: 'ease',
   			autoBuffering: false,
   			autoRewind:true,
   			loop:false,
   			useNativeFullScreen: true,
   			controlBarBackgroundColor: '0x453F3F',
   			initialScale:'scale',
   			emailPostUrl: false,
   			showMenu:false,
   			useHwScaling:true,
   			autoPlay:false,
   			videoFile: $("playlist").getElementsByTagName("a")[0].getAttribute("href")
		};
		var links = $("playlist").getElementsByTagName("a"); 
		if (flowplayer == null) {
				flowplayer = flashembed("bball_tv", 
				{src:"./flowplayer/FlowPlayerDark.swf", bgcolor:'#6F7485'}, 
				{config: playerConfig}
				);	
			} else {  
				flowplayer.setConfig(playerConfig); 
			}
		for (var i = 0; i < links.length; i++) {
			links[i].onclick = function() { 
			playerConfig.videoFile = this.getAttribute("href");
			flowplayer.setConfig(playerConfig); 
			return false; 
      }
	}
   	document.getElementById("bball_tv").onclick = function()  {
		links[0].onclick();
	}
}

function initNavigation(){
	Navigation = {
		jsonPath: 'includes/navigation.json',
		dataset: '',
		objectToPass: null,
		initialize: function() {
			new Json.Remote(this.jsonPath, { method: 'get', async: true, onComplete: function(resp) {
			 		Navigation.dataset=resp;
					//restoring browser state from js state
					var link = location.hash.toString();
					if ((link!='')) {
	//debug			
	//debugInfo(link);
	//debugInfo(link.substring(1).replace(/!!/g,'\''));
	//debug			
						link =link.substring(1).replace(/\+\+/g,')');
						link =link.replace(/\+/g,'(');
						link =link.replace(/==/g,'}');
						link =link.replace(/=/g,'{');
						eval(link.replace(/%/g,'\''));
					}
	//debug			
	//debugInfo('ajax ready');
	//debug			
				}
			}).send();
		},
		doNavigation: function(name, extension) {
	//debug			
	//debugInfo('name: ' + name);
	//debug			
			if (eval('this.dataset.' + name)) {
				var control = eval('this.dataset.' + name);
				Object.extend(control.objectProperties, extension);
				this.objectToPass = control.objectProperties;
	//debug			
	//debugInfo('objectToPass: ' + Json.toString(this.objectToPass));
	//debug			
				control.toEvaluate.each(function(command){
	//debug			
	//debugInfo('save to history - command: ' + command);
	//debugInfo('save to history - eval: ' + command.replace(/createdObject/g, 'Navigation.objectToPass'));
	//debugInfo(Navigation.objectToPass);
	//debug			
					eval(command.replace(/createdObject/g, 'Navigation.objectToPass'));
				});
			} else {
				console.log?console.log('Navigációs hiba.'):'';
	//debug			
	//debugInfo('nincs ilyen függvényhívás');
	//debug			
			}
		}
	};
	Navigation.initialize();
}

function historyChange(newLocation, historyData) {
	initNavigation();
	if (newLocation=="") {
		Navigation.doNavigation('goMainPage',{});
	}else{
		newLocation=newLocation.replace(/\+\+/g,')');
		newLocation=newLocation.replace(/\+/g,'(');
		newLocation=newLocation.replace(/==/g,'}');
		newLocation=newLocation.replace(/=/g,'{');
		eval(newLocation.replace(/%/g,'\''));
		//callServerDispatcher(historyData);
	}
}

function encode4XML(sin){
	sout='';
	for(var i=0;i<sin.length;i++){
		if(sin.charCodeAt(i)>127 || sin.charCodeAt(i) == 34)
			switch(sin.charCodeAt(i)){
				case 245:
					sout+='&#337;';
					break;
				case 212:
				case 213:
					sout+='&#336;';
					break;
				case 251:
					sout+='&#369;';
					break;
				case 219:
					sout+='&#368;';
					break;
				case 225:
					sout+='!!300!';
					break;
				case 233:
					sout+='!!301!';
					break;
				case 237:
					sout+='!!302!';
					break;
				case 243:
					sout+='!!303!';
					break;
				case 246:
					sout+='!!304!';
					break;
				case 337:
					sout+='!!305!';
					break;
				case 250:
					sout+='!!306!';
					break;
				case 252:
					sout+='!!307!';
					break;
				case 369:
					sout+='!!308!';
					break;
				case 193:
					sout+='!!309!';
					break;
				case 201:
					sout+='!!310!';
					break;
				case 205:
					sout+='!!311!';
					break;
				case 211:
					sout+='!!312!';
					break;
				case 214:
					sout+='!!313!';
					break;
				case 336:
					sout+='!!314!';
					break;
				case 218:
					sout+='!!315!';
					break;
				case 220:
					sout+='!!316!';
					break;
				case 368:
					sout+='!!317!';
					break;
				case 34:
					sout+='!!318!';
					break;
				default:
					sout+='&#'+sin.charCodeAt(i)+';';
			}
		else
			sout+=sin.charAt(i);
	}
	return sout;
}
function addComment(cikk_id,save){
	obj=new Object();
	obj['event']='AddComment';
	obj['cikk_id']=cikk_id;
	obj['save']=save;
	if (save==1){
		if($('nev')!=null && innertrim($('nev').value) ==""){
			$('nev').focus();
			alert("Töltse ki a nevet!");
			return;
		} else if ($('email')!=null && innertrim($('email').value) == ""){
			$('email').focus();
			alert("Töltse ki az e-mail címet!");
			return;
		} else if (innertrim($('szoveg').value) == ""){
			$('szoveg').focus();
			alert("Töltse ki a kommentet!");
			return;
		}
	}
	if ($('nev')!=null){
		obj['nev']=encode4XML($('nev').value);
		obj['email']=encode4XML($('email').value);
	}
	obj['szoveg']=encode4XML($('szoveg').value.replace(/\r\n/g,"<br />").replace(/\n/g,"<br />"));
	callServerDispatcher(obj);
}

function goComment(cikk_id){
	obj=new Object();
	obj['event']='Comment';
	obj['cikk_id']=cikk_id;
	callServerDispatcher(obj);	
}

function goSearch(page){
	obj=new Object();
	obj['event']='Search';
	obj['page']=page;
	obj['mit']=encode4XML($('searchfield').value);
	callServerDispatcher(obj);
}
function innertrim(str){
	rv="";
	for(i=0;i<str.length;++i)
		if(str.charAt(i) != ' ')
			rv+=str.charAt(i);
	return rv;
}
function startTimer(page){
page =page % 4 + 1;
Navigation.doNavigation('goNewsContainer',{id:page});
t=setTimeout("startTimer(" + page + ")",7000);
}

function clearTimer(){
	if (typeof t != 'undefined') {
		clearTimeout(t);
	}
}
