/**
 * Belenyúltam video-galéria miatt
 * 2009.03.02. 16:37:45 - Anti
 */
var Kwindow = Class.create();

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

/* utilok hozza: */
function urldecode(str) {
	if (str=="") return "";
	var ret = str;
	ret = ret.replace(/\+/g, '%20');
	ret = decodeURIComponent(ret);
	ret = ret.toString();
	return ret;
}

function urlencode(str) {
	if (str=="") return "";
	var ret = str;
	ret = ret.toString();
	ret = ret.replace(/\?/g, 'xkerdojelx');
	ret = encodeURIComponent(ret);
	ret = ret.replace(/%20/g, '+');
	return ret;
}

Kwindow.prototype = {

	initialize: function() {

	},
	
	init: function() {
		this.init_auto_kwindow();
		this.init_kwindow();
		this.init_kwindow_gallery();
		this.init_kwindow_video();

		// a legutolso post utan kapott valaszt tartalmazza, barhonnan el lehet erni:
		this.kwindow_post_response_text="";
		this.gallery_move_up=false;
		this.gallery_move_down=false;
		this.gallery_scrollspeed=10;
	},

	// visszaadja hogy hanyas IE alatt vagyunk:
	vIE: function() {
		return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
	},

	init_auto_kwindow: function() {
		var w = $$("a[rel=auto_kwindow]");
		if( w.length )
			w.each(function(element) {
				kwindow.kwindow_show(element);
			});
	},

	init_kwindow: function() {
		var w = $$("a[rel=kwindow]");
		if( w.length ) 
			w.each(function(element) {
				Event.observe(element, "click", function(event) {
					kwindow.kwindow_show(element);
               Event.stop(event);
				}.bind(element));
			});
	},

	init_kwindow_gallery: function() {
		var w = $$("a[rel=kwindow_gallery]");
		if( w.length ) {
			w.each(function(element) {
				Event.observe(element, "click", function(event) {
					kwindow.kwindow_show(element);
					Event.stop(event);
				}.bind(element));
			});
		}
	},

	init_kwindow_video: function() {
		var w = $$("a[rel=kwindow_video]");
		if( w.length ) {
			w.each(function(element) {
				Event.observe(element, "click", function(event) {
					kwindow.kwindow_show(element);
					Event.stop(event);
				}.bind(element));
			});
		}
	},

	init_kwindow_close: function() {
		$$("a[rel=kwindow_close]").each(function(element) {
			Event.observe(element, "click", function(event) {
			     kwindow.kwindow_close();
				// ha van run_js attributuma, akkor azt a js-t lefuttatjuk egybol a bezaras utan:
				if (element.getAttribute('run_js')) {
					var run_js_fgv_name=element.getAttribute('run_js');
					var ret=eval(run_js_fgv_name)();
				}
			     // ha volt automatikus uj ablak nyitas, akkor nyitjuk:
				if (element.getAttribute('module')) kwindow.kwindow_show(element);
	             Event.stop(event);
			}.bind(element));
		});
	},
	
	init_kwindow_post: function() {
		$$("a[rel=kwindow_post]").each(function(element) {
			Event.observe(element, "click", function(event) {
				// ha van form ellenorzes, akkor az kell:
				if (element.getAttribute('check_form_js')) {
					var check_js_fgv_name=element.getAttribute('check_form_js');
					var valid=eval(check_js_fgv_name)();
					if (valid) kwindow.kwindow_post(element);
				}
				else // ha nincs akkor egybol mehet a post:
				kwindow.kwindow_post(element);
	             Event.stop(event);
			}.bind(element));
		});
	},
	
	kwindow_post: function(element) {
		var form_id=element.getAttribute('form_id');
		var update_div=element.getAttribute('update_div');
		var url=element.getAttribute('url');

		var serializedForm = Form.serialize(form_id);

		if (update_div) {
		     // ajax post utan div update van:
			var myAjax = new Ajax.Updater(update_div, url,
				{
					asynchronous:true,
					onComplete: function(response) {
						// ha kert ablakbezarast, akkor be is zarjuk egybol:
						if (element.getAttribute('close_after_post')=="true") kwindow.kwindow_close();
						// a valaszt ezek utan barhol es barmikor el lehet erni ameg nincs ujabb valasz:
						kwindow.kwindow_post_response_text=response.responseText;
						// ha van post utani futtatando js, akkor azt is lefuttatjuk:
						if (element.getAttribute('run_js_after_post')) {
							var run_js_fgv_name=element.getAttribute('run_js_after_post');
							var ret=eval(run_js_fgv_name)();
						}
					},
					parameters: serializedForm,
					method: 'post'
				});
		}
		else {
		     // sima post van div update nelkul:
			var myAjax = new Ajax.Request(url,
				{
					asynchronous:true,
					onComplete: function(response) {
						// ha kert ablakbezarast, akkor be is zarjuk egybol:
						if (element.getAttribute('close_after_post')=="true") kwindow.kwindow_close();
						// a valaszt ezek utan barhol es barmikor el lehet erni ameg nincs ujabb valasz:
						kwindow.kwindow_post_response_text=response.responseText;
						// ha van post utani futtatando js, akkor azt is lefuttatjuk:
						if (element.getAttribute('run_js_after_post')) {
							var run_js_fgv_name=element.getAttribute('run_js_after_post');
							var ret=eval(run_js_fgv_name)();
						}
					},
					parameters: serializedForm,
					method: 'post'
				});
		}
	},
	
	kwindow_close: function(element_id) {
		// eltuntetjuk az ablakot:
		$("kwindow").style.display="none";
		// kivesszuk a DOM-bol a tartalmat:
		if ($("kwindow_content").down(0) && !kwindow.vIE()) $("kwindow_content").removeChild($("kwindow_content").down(0));
		// kivilagositjuk a fotartalmat:
	     $("photogallery2_darken").style.display="none";

		// ha a bezaras utan egybol uj ablakot kell nyitni:
		if (element_id) kwindow.kwindow_show($(element_id));

		// visszatesszuk a selecteket IE6 alatt
		if (kwindow.vIE()==6)
			$("body").getElementsBySelector("select").each(function(element) {
				element.style.visibility="visible";
		     });
	},

	// olyan elemet var, aminek megvannak legalabb a module, template attributumai:
	kwindow_show: function(element) {
		// attributumok kinyerese:
		var rel=element.getAttribute("rel");
		// sotetito hatterszin (alapbol szurke):
		var bg_color=element.getAttribute('bg_color');
		if (bg_color) $("photogallery2_darken").style.background="#"+bg_color;
		// sotetito hatterszin atlatszosaga:
		var bg_transparency=element.getAttribute('bg_transparency');
		if (bg_transparency) {
			$("photogallery2_darken").style.filter = "alpha(opacity="+bg_transparency+")";
			$("photogallery2_darken").style.opacity = (bg_transparency/100);
			$("photogallery2_darken").style.MozOpacity = (bg_transparency/100);
			$("photogallery2_darken").style.KhtmlOpacity = (bg_transparency/100);
		}

		// a betolteni kivant modul:
		var top=element.getAttribute('top');

		var positions=getScrollXY();
		var _top=positions[1]+top;
		$("kwindow_center").style.top=_top+"px";
		
		// kell e sotetites (alapbol igen):
		var disable_darken=element.getAttribute('disable_darken');
		// a betolteni kivant modul:
		var module=element.getAttribute('module');
		// es a betolteni kivant template a modulon belul:
		var template=element.getAttribute('template');
		// a betolteni kivant modul (ha ez van, akkor az adja vissza a templatet):
		var method=element.getAttribute('method');
		// az atadott parameterek (get-es formaban, url kodolassal):
		var params=element.getAttribute('params');
		if (params) params=urlencode(params);
		// azablak szelessege (alapbol 500px):
		var width=element.getAttribute('width');
		if (width) $("kwindow").style.width=width+"px";
		else $("kwindow").style.width="500px";
		// az ablak magassaga (alapbol auto):
		var height=element.getAttribute('height');
		if (height) $("kwindow").style.height=height+"px";
		else $("kwindow").style.height="auto";
		
		// automatikus bezaras (ms-ban):
		var autoclose=parseInt(element.getAttribute('autoclose'),10);
		// autoclose utan nyitando ablak adatait tartalmazo elem:
		if (autoclose>0) var kwindow_after_autoclose=element.getAttribute('kwindow_after_autoclose');
		// a porgo gif animacio megjelenitese, ameg tolti az ablak tartalmat:
		var loading_animation=element.getAttribute('loading_animation');
		
		// ha sima alertkent hasznaljuk:
		var alert_text=element.getAttribute('alert');
		if (alert_text) {
			alert_text=urlencode(alert_text);
			var alert_mode=element.getAttribute('mode');
			if (alert_mode) alert_mode=urlencode(alert_mode);
			else alert_mode='alert';
		     url="/ajax__kwindow_alert/"+alert_text+"/"+alert_mode;
		}
		
		// IE6 alatt a selecteket el kell tuntetni (mert mindig a sotetseg felett lennenek es lehetne rajuk kattintani):
		if (kwindow.vIE()==6)
			$("body").getElementsBySelector("select").each(function(element) {
				element.style.visibility="hidden";
			});

		if (disable_darken!="true")
			// elsotetitjuk az oldlat:
			$("photogallery2_darken").style.display="block";
			// az oldal tartalmanak megfelelo magassagot adunk neki:
			var bodyHeight=$("body").offsetHeight;
			$("photogallery2_darken").style.height=bodyHeight+"px";

		// ha engedelyezve van a loading animacio, akkor kirakjuk a porgo gif-et:
	     if (loading_animation!="false") $("kwindow_loading").style.display="block";
		// kiuritjuk az ablak tartalmat:
		//if ($("kwindow_content").down(0)) $("kwindow_content").removeChild($("kwindow_content").down(0));
          $("kwindow_content").innerHTML="";
		// megjelenitjuk az ablakot:
		$("kwindow").style.display="block";
		// feltoltjuk a tartalmat:
//		var url="";
		if (rel=="kwindow_gallery") {
		     // kiszedjuk a galeria parametereit:
			var gallery_id=element.getAttribute('gallery_id');
			if (!gallery_id) gallery_id="0";
			if (!template) template="1";
			var startpic=element.getAttribute('startpic');
			if (!startpic) startpic="0";
			var main_image_size=element.getAttribute('main_image_size');
			if (!main_image_size) main_image_size="666x500";
			var thumb_image_size=element.getAttribute('thumb_image_size');
			if (!thumb_image_size) thumb_image_size="130x130";
			var title=element.getAttribute('title');
			if (!title) title="-";
			title=urlencode(title);
			var filter_picture_title=element.getAttribute('filter_picture_title');
			if (!filter_picture_title) filter_picture_title="-";
			filter_picture_title=urlencode(filter_picture_title);
			var gallery_module=element.getAttribute('gallery_module');
			if (!gallery_module) gallery_module="ih_gallery";
			var gallery_scrollspeed=parseInt(element.getAttribute('scrollspeed'));
			if (!gallery_scrollspeed) gallery_scrollspeed=10;
			kwindow.gallery_scrollspeed=gallery_scrollspeed;

			// automatikus szelessegallitas:
			if (!width) {
				var mis=main_image_size.split("x");
				var tis=thumb_image_size.split("x");
				width=parseInt(mis[0])+parseInt(tis[0])+50;
				$("kwindow").style.width=width+"px";
			}

			url="/ajax__get_kwindow_gallery_template/"+gallery_id+"/"+template+"/"+startpic+"/"+main_image_size+"/"+thumb_image_size+"/"+title+"/"+filter_picture_title+"/"+gallery_module;
		}

		if (rel=="kwindow_video") {
		     // kiszedjuk a video-galeria parametereit:
			var gallery_id=element.getAttribute('video_gallery_id');
			if (!gallery_id) gallery_id="0";
			if (!template) template="1";
			var startpic=element.getAttribute('startpic');
			if (!startpic) startpic="0";
			var main_image_size=element.getAttribute('main_image_size');
			if (!main_image_size) main_image_size="666x500";
			var thumb_image_size=element.getAttribute('thumb_image_size');
			if (!thumb_image_size) thumb_image_size="130x130";
			var title=element.getAttribute('title');
			if (!title) title="-";
			title=urlencode(title);
			var filter_picture_title=element.getAttribute('filter_picture_title');
			if (!filter_picture_title) filter_picture_title="-";
			filter_picture_title=urlencode(filter_picture_title);
			var gallery_module=element.getAttribute('video_gallery_module');
			if (!gallery_module) gallery_module="ih_video";
			var gallery_scrollspeed=parseInt(element.getAttribute('scrollspeed'));
			if (!gallery_scrollspeed) gallery_scrollspeed=10;
			kwindow.gallery_scrollspeed=gallery_scrollspeed;

			// automatikus szelessegallitas:
			if (!width) {
				var mis=main_image_size.split("x");
				var tis=thumb_image_size.split("x");
				width=parseInt(mis[0])+parseInt(tis[0])+50;
				$("kwindow").style.width=width+"px";
			}

			url="/ajax__get_kwindow_video_template/"+gallery_id+"/"+template+"/"+startpic+"/"+main_image_size+"/"+thumb_image_size+"/"+title+"/"+filter_picture_title+"/"+gallery_module;
		}
			
		if (!method && template && rel!="kwindow_gallery" && rel!="kwindow_video" && !alert_text) url="/ajax__get_kwindow_content_from_template/"+module+"/"+template+"/params?"+params;
		if (method && !alert_text) url="/ajax__get_kwindow_content_from_method/"+module+"/"+method+"/params?"+params;
		var pars = '';
		var myAjax = new Ajax.Updater('kwindow_content', url, {
			method: 'get',
			parameters: pars,
			evalScripts: true,
			onComplete : function(valasz) {
				// eltuntetjuk a porgo gif-et:
			     if (loading_animation!="false") $("kwindow_loading").style.display="none";
				// inicializaljuk a bezarast:
				kwindow.init_kwindow_close();
				// es a postot:
				kwindow.init_kwindow_post();
			     // ha van automatikus bezaras, akkor elinditjuk a visszaszamlalast:
			     if (autoclose>0 && !kwindow_after_autoclose) setTimeout("kwindow.kwindow_close()",parseInt(autoclose));
				// ha meg uj ablak nyitas is van az automatikus bezaras utan, akkor ide fut ra:
			     if (autoclose>0 && kwindow_after_autoclose) setTimeout("kwindow.kwindow_close('"+kwindow_after_autoclose+"')",autoclose);
			}
		});
		// es mar kesz is vagyunk :)
	},
	
	change_main_image: function() {
		if (!$("kwindow_gallery")) return;
		$A($("kwindow_gallery").getElementsByClassName("change_kwindow_main_image")).each(function(element) {
		     element.observe("click", function(event) {
				$A($("kwindow_gallery").getElementsByClassName("main_image")).each(function(element2) {
					element2.style.display="none";
				});
				var image_id=element.up('td',0).getAttribute('image_id');
				$("kg_image_"+image_id).style.display="block";
		     });
		});
	},
	
	gallery_move: function() {
		if (!$("kwindow_gallery")) return;
		$A($("kwindow_gallery").getElementsByClassName("move_up")).each(function(element) {
		     element.observe("mouseover", function(event) {
		          kwindow.gallery_move_up=true;
				setTimeout("kwindow.gallery_scroll('up')",20);
		     });
		});
		$A($("kwindow_gallery").getElementsByClassName("move_down")).each(function(element) {
		     element.observe("mouseover", function(event) {
		          kwindow.gallery_move_down=true;
				setTimeout("kwindow.gallery_scroll('down')",20);
		     });
		});
		$A($("kwindow_gallery").getElementsByClassName("move_up")).each(function(element) {
		     element.observe("mouseout", function(event) {
		          kwindow.gallery_move_up=false;
		     });
		});
		$A($("kwindow_gallery").getElementsByClassName("move_down")).each(function(element) {
		     element.observe("mouseout", function(event) {
		          kwindow.gallery_move_down=false;
		     });
		});
	},

	gallery_scroll: function(dir) {
		if (dir=="up") {
			var top=parseInt($("kwindow_gallery_thumbs").style.top);
			if (!top) top=0;
			top+=kwindow.gallery_scrollspeed;
			if (top>0) top=0;
			$("kwindow_gallery_thumbs").style.top=top+"px";
		     if (kwindow.gallery_move_up) setTimeout("kwindow.gallery_scroll('up')",20);
		}
		if (dir=="down") {
			// kiszedjuk a thumbs div magassagat mert MUSZAJ!
			var thumbsHeight=400;
			$A($("kwindow_gallery").getElementsByClassName("thumbs")).each(function(element) {
			     thumbsHeight=parseInt(element.style.height);
			});

			var top=parseInt($("kwindow_gallery_thumbs").style.top);
			if (!top) top=0;
			top-=kwindow.gallery_scrollspeed;
			var hossz=-parseInt($("kwindow_gallery_thumbs").offsetHeight);
			hossz=hossz+thumbsHeight;
			if (top<=hossz) top=hossz;
			if (parseInt($("kwindow_gallery_thumbs").offsetHeight)<thumbsHeight) top=0;
			$("kwindow_gallery_thumbs").style.top=top+"px";
		     if (kwindow.gallery_move_down) setTimeout("kwindow.gallery_scroll('down')",20);
		}
	}
	
}

var kwindow=new Kwindow;
