// disable select in #ajaxCnt
function disableSelect(){
  var el=document.getElementById("ajaxCnt");
  if(el!=null){
    el.onselectstart=function(){ return false; }
    el.onmousedown=function(){ return false; }
  }
}

function CommentAreaDefaultBehaviour(id)
{
	$("#" + id).bind("selectstart", function(event){
		event.stopPropagation();
	});
	$("#" + id).bind("mousedown", function(event){
		event.stopPropagation();
	});
}


// create ajax requests
function ajaxLoad(href,cb,cbBefore, videoPlayer){
  if(cbBefore) cbBefore(href);
	var temp_string = (unescape(decodeURI(href))).replace(/\+/g, " ");
	temp_string = temp_string.substring(temp_string.lastIndexOf("/")+1,255);
	document.title = $("#base_page_title").html() + " | " + temp_string;
  var id=href.match(/[0-9]+/);
  if(id!=null) window.location.href="#"+id[0];
  $("#ajaxCnt").load(href+" #ajaxCnt",function(){ swapAjaxLinks(cb,cbBefore); disableSelect(); if(cb) cb(href); });
  try
  {
    luciPing();
  }
  catch (e)
  {}
}

//$(function(){ disableSelect(); });

function swapAjaxLinks(cb,cbBefore){
  $("a.ajax").unbind("click").click(function(){
    ajaxLoad($(this).attr("href"),cb,cbBefore);
    return false;
  });
}

window.luci_login =function()
{
	var fail = document.getElementById('login_failed');
	fail.style.display = 'none';

	var user = document.getElementById('luci_username').value;
	var pass = document.getElementById('luci_password').value;
	var command = new Object();
	command.type='command';
	command.command='run';
	command.process='Authenticate';
	command.parameters = new Object();
	command.parameters.username = user;
	command.parameters.password = pass;
	window.luci_communication.send(JSON.stringify(command));
}

window.luci_logout =function ()
{
	window.gui_communication.send("{\"type\":\"command\", \"command\":\"run\", \"process\":\"Logout\"}");
}

function ulListSelect(id){
  $(function(){
  	//alert(id + "!");
  	//$(".menu_0").html(id);
    $(".ulList li.selected").removeClass("selected");
    if(id && id!=null) $(".ulList li.ulList_"+id).addClass("selected");
    //$(".scroll").attr("scrollTop",($(".ulList li.ulList_"+id).attr("offsetTop")-47));
    //alert("|" + ($(".ulList li.ulList_"+id).attr("offsetTop")-47));
    setTimeout('$(".scroll").attr("scrollTop",($(".ulList li.ulList_"+' + id + ').attr("offsetTop")-83));', 500);
  });
}

function xulListSelect(id){
  $(function(){
    $(".xulList li.selected").removeClass("selected");
    if(id && id!=null) $(".xulList li.xulList_"+id).addClass("selected");
    if (($(".xulList li.xulList_"+id).length != 0) && (!($(".ulList li.ulList_"+id).is(':visible'))))
			{
    	setTimeout('$(".scroll").attr("scrollTop",($(".xulList li.xulList_"+' + id + ').attr("offsetTop")-83));', 500);
    	}
  });
}


function yulListSelect(id){
  $(function(){
    $(".yulList li.selected").removeClass("selected");
    if(id && id!=null) $(".yulList li.yulList_"+id).addClass("selected");
    if (($(".yulList li.yulList_"+id).length != 0) && (!($(".ulList li.ulList_"+id).is(':visible'))))
			{
			setTimeout('$(".scroll").attr("scrollTop",($(".yulList li.yulList_"+' + id + ').attr("offsetTop")-83));', 500);
			}
  });
}

function generalMinMax(theBody, theImg){
	$(function(){
	if ($(theBody).css("display") == "none")
	{
		$(theImg).attr("src","/imgs/posfilter/up.gif");
	}
	else
	{
		$(theImg).attr("src","/imgs/posfilter/down.gif");
	}
  });
}

function generalDoMinMax(theSearchHead, theBody, theImg){
	$(function(){
	if (($(theSearchHead).css("display") == "none") || ($(theSearchHead).length == 0))
	{
		$(theImg).hide();
	}
	else
	{
		$(theImg).show();
		generalMinMax(theBody,theImg);
		$(theBody).toggle("fast");
	}
	});
}

function generalInitSearchMinMax(theSearchBody, theSearchHead, theBody, theImgS, theSearchImg, theSearchCloserImg){
	$(function(){
	$(theImgS).show();
	$(theSearchHead).click(function() {
		generalMinMax(theSearchHead,theSearchImg);
		$(theSearchBody).toggle("fast"); return false;
		});
	$(theSearchCloserImg).click(function() {$(theSearchHead).hide(); $(theSearchBody).hide(); $(theImgS).hide(); $(theImgS).attr("src","/imgs/posfilter/up.gif"); $(theBody).show(); return false;});
	});
}

function getPrevNextMenu(x,stat){
  var c;
  var r={};

  r.p=$(x).prev();
  r.prev=r.p.attr("class");
  c=r.prev.split("_");
  c[2]=stat;
  r.prevNew=c.join("_");

  r.n=$(x).next();
  r.next=r.n.attr("class");
  c=r.next.split("_");
  c[1]=stat;
  r.nextNew=c.join("_");

  return r;
}

function initTabs(p,cb){
  var menu;
  $(p+" a").hover(function(){
    if(parseInt($(this).attr("class").match(/menu_([0-9]*)/)[1])!=2){
      var c=getPrevNextMenu(this,0);
      $(this).removeClass("tmenu_1").addClass("tmenu_0");
      c.p.removeClass(c.prev).addClass(c.prevNew);
      c.n.removeClass(c.next).addClass(c.nextNew);
    }
  },function(){
    if(parseInt($(this).attr("class").match(/menu_([0-9]*)/)[1])!=2){
      var c=getPrevNextMenu(this,1);
      $(this).removeClass("tmenu_0").addClass("tmenu_1");
      c.p.removeClass(c.prev).addClass(c.prevNew);
      c.n.removeClass(c.next).addClass(c.nextNew);
    }
  }).click(function(){
    var stat=parseInt($(this).attr("class").match(/menu_([0-9]*)/)[1]);
    if(stat!=2){
      var c;
      $(p+" a.tmenu_2").each(function(){
        c=getPrevNextMenu(this,1);
        $(this).removeClass("tmenu_2").addClass("tmenu_1");
        c.p.removeClass(c.prev).addClass(c.prevNew);
        c.n.removeClass(c.next).addClass(c.nextNew);
      });

      c=getPrevNextMenu(this,2);
      $(this).removeClass("tmenu_"+stat).addClass("tmenu_2");
      c.p.removeClass(c.prev).addClass(c.prevNew);
      c.n.removeClass(c.next).addClass(c.nextNew);

      if(cb) cb($(this));
    }
  });
}

var blog={
  init:function(id){
    $(".blogItem").click(function(){ $(".firstlis_" + $(this).attr("id").split("_")[1] + "_0").hide(); $("#blogLess_" + $(this).attr("id").split("_")[1]).hide(); $("#blogMore_" + $(this).attr("id").split("_")[1]).show(); $("#blogQuest_"+$(this).attr("id").split("_")[1]).toggle("fast"); });
    $(".blogMore").click(function(){ $("#blogQuest_"+$(this).attr("id").split("_")[1]+" li").show(); $(this).hide(); $("#blogLess_" + $(this).attr("id").split("_")[1]).show();});
    $(".blogLess").click(function(){ $(".firstlis_" + $(this).attr("id").split("_")[1] + "_0").hide(); $(this).hide(); $("#blogMore_" + $(this).attr("id").split("_")[1]).show(); $(".scroll").attr("scrollTop",($("#blogItem_" + $(this).attr("id").split("_")[1]).attr("offsetTop")-47));});
		//alert('03');
		var hid=window.location.href.match(/#[0-9]+/);

    if(hid!=null) ajaxLoad("/blog/view/"+hid[0].substr(1),blog.load,blog.loadBefore);
    swapAjaxLinks(blog.load,blog.loadBefore);
    if(id){
      ulListSelect(id);
      //hide_player();
      $(".ulList_"+id).each(function(){
        if($(this).css("display")=="none") $(this).parents(".blogQuest").children(".blogMore").trigger("click");
      });
    }

    $("#blog_list_head").click(function()	{ generalDoMinMax("#blog_search_head","#blog_list_body","#blog_list_minmax"); });

    blog.load();
  },
  loadBefore:function(href){

  	//ulListSelect(id);       //modded for error list

  	if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();

  	if (href)
  	{
		var did = href.match(/[0-9]+/);

		ulListSelect(did);

		//alert(did);

		$(".ulList_"+did).each(function(){
        if($(this).css("display")=="none") $(this).parents(".blogQuest").children(".blogMore").trigger("click");
        });

  	}



  },
  load:function(href){
  	run_scripts();
	$(".fav").click(function(){
      var a=$(this);
      $.get("/blog/fav/"+$(this).attr("id").split("_")[1],function(d){
        if(parseInt(d)==1) a.addClass("favYes"); else a.removeClass("favYes");
      });
    });
  },
  search:function(){
    $("#ajaxCnt").load("/blog/search",{q:$("#searchQuery").val()},function(){
      $("#searchResultScroll").html($("#resultUL").html());
      swapAjaxLinks(blog.load, blog.loadBefore);
      generalInitSearchMinMax("#blog_search_body", "#blog_search_head", "#blog_list_body", "#blog_list_minmax", "#blog_search_minmax", "#blog_search_closer");
      $(".scroll").attr("scrollTop",0);
    });
    return false;
  }
};

var terms={
  init:function(id){
    var hid=window.location.href.match(/#[0-9]+/);
    if(hid!=null) ajaxLoad("/terms/view/"+hid[0].substr(1),terms.load,terms.loadBefore);
    swapAjaxLinks(terms.load,terms.loadBefore);
    if(id) ulListSelect(id);

    $("#terms_list_head").click(function()	{ generalDoMinMax("#terms_search_head","#terms_list_body","#terms_list_minmax");  });

    terms.load();
  },
  loadBefore:function(href){
  	//hide_player();
  	if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();
    if(href) ulListSelect(href.match(/[0-9]+/));
  },
  load:function(href){
    imgs.init();
    $(".fav").click(function(){
      var a=$(this);
      $.get("/terms/fav/"+$(this).attr("id").split("_")[1],function(d){
        if(parseInt(d)==1) a.addClass("favYes"); else a.removeClass("favYes");
      });
    });
  },
  showImage:function(id){
    $(".imgSlide").hide();
    $("#imgSlide_"+id).show();

    imgs.current = id;
    imgs.fade();
    },
  search:function(){
    $("#ajaxCnt").load("/terms/search",{q:$("#searchQuery").val()},function(){
      $("#searchResultScroll").html($("#resultUL").html());
      swapAjaxLinks(terms.load, terms.loadBefore);
      generalInitSearchMinMax("#terms_search_body", "#terms_search_head", "#terms_list_body", "#terms_list_minmax", "#terms_search_minmax", "#terms_search_closer");
      $(".scroll").attr("scrollTop",0);
    });
    return false;
  }
};

var doctor={
  init:function(id){
		$(".doctorItem").click(function(){$(".bla_"+$(this).attr("id").split("_")[1]+"_ize_0").hide(); $("#doctormore_" + $(this).attr("id").split("_")[1]).show(); $("#doctorless_" + $(this).attr("id").split("_")[1]).hide(); $("#doctorQuest_"+$(this).attr("id").split("_")[1]).toggle("fast");});
    $(".blogMore").click(function(){ $(".bla_"+$(this).attr("id").split("_")[1]+"_ize_0").show(); $(this).hide(); $("#doctorless_" + $(this).attr("id").split("_")[1]).show();});
    $(".blogLess").click(function(){ $(".bla_"+$(this).attr("id").split("_")[1]+"_ize_0").hide(); $(this).hide(); $("#doctormore_" + $(this).attr("id").split("_")[1]).show(); $(".scroll").attr("scrollTop",($("#doctorItem_" + $(this).attr("id").split("_")[1]).attr("offsetTop")-47));});

    var hid=window.location.href.match(/#[0-9]+/);
    if(hid!=null) ajaxLoad("/doctor/view/"+hid[0].substr(1),doctor.load,doctor.loadBefore);
    swapAjaxLinks(doctor.load,doctor.loadBefore);
    if(id) ulListSelect(id);
    //alert('asd');

    $(".ulList_"+id).each(function(){
		      if($(this).css("display")=="none") {
		      var parent_num = $(this).parents(".doctorQuest").attr("id").split("_")[1];
		      $(".bla_" + parent_num + "_ize_0").show();
					$("#doctorless_" + parent_num).show();
					$("#doctormore_" + parent_num).hide();
					}
      	});

    $("#doctor_list_head").click(function()	{ generalDoMinMax("#doctor_search_head","#doctor_list_body","#doctor_list_minmax"); });
    doctor.load();
  },
  loadBefore:function(href){
  	if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();

    if(href)
			{
			var doc_num = href.match(/[0-9]+/);

			ulListSelect(doc_num);


    	$(".ulList_"+doc_num).each(function(){
		      if($(this).css("display")=="none") {
		      var parent_num = $(this).parents(".doctorQuest").attr("id").split("_")[1];
		      $(".bla_" + parent_num + "_ize_0").show();
					$("#doctorless_" + parent_num).show();
					$("#doctormore_" + parent_num).hide();
					//.trigger("click");    //gyuri CSUTORTOK
					//alert(id);
					}
      	});
      }

  },
  load:function(href){
	run_scripts();
    $(".fav").click(function(){
      var a=$(this);
      $.get("/doctor/fav/"+$(this).attr("id").split("_")[1],function(d){
        	if(parseInt(d)==1) a.addClass("favYes"); else	a.removeClass("favYes");
			});
    });
  },
  search:function(){
    $("#ajaxCnt").load("/doctor/search",{q:$("#searchQuery").val()},function(){
      $("#searchResultScroll").html($("#resultUL").html());
      swapAjaxLinks(doctor.load,doctor.loadBefore);
      generalInitSearchMinMax("#doctor_search_body", "#doctor_search_head", "#doctor_list_body", "#doctor_list_minmax", "#doctor_search_minmax", "#doctor_search_closer");
			$(".scroll").attr("scrollTop",0);
		});

		return false;
  }
};


//	pózok

var pos={
  init:function(id){
    $("#pos_filter_list_closer").click(function()
		 {
		 $("#searchMan").change(pos.fillter).val("");
		 $("#searchWoman").change(pos.fillter).val("");
		 pos.fillter();
		 return false;
		 });

		$("#pos_filter_list_head").click(function() {generalMinMax("#pos_filter_list_body", "#pos_filter_list_minmax"); $("#pos_filter_list_body").toggle("fast");});
		$("#pos_basic_list_head").click(function() {$(".firsthalf_pos_0").hide(); $("#blogless_basic").hide(); $("#blogmore_basic").show(); generalMinMax("#pos_basic_list_body", "#pos_basic_list_minmax"); $("#pos_basic_list_body").toggle("fast");});
		$("#pos_other_list_head").click(function() {generalMinMax("#pos_other_list_body", "#pos_other_list_minmax"); $("#pos_other_list_body").toggle("fast");});

		$("#pos_search_head").click(function() {
		     generalMinMax("#scrillMainDiv", "#pos_search_minmax");
		     $("#scrillMainDiv").toggle("fast");
		  });

		//it's called blog but only to have the right css


		$(".blogItem").click(function(){ $("#blogQuest_"+$(this).attr("id").split("_")[1]).toggle("fast"); });

		$("#blogmore_basic").click(function(){ $(".firsthalf_pos_0").show(); $("#blogless_basic").show(); $(this).hide(); });
		$("#blogless_basic").click(function(){ $(".firsthalf_pos_0").hide(); $("#blogmore_basic").show(); $(this).hide(); $(".scroll").attr("scrollTop",($("#pos_basic_list_head").attr("offsetTop")-47)); });

		// attr("offsetTop")

		/*
		$(".blogLess").click(function(){ $("#blogQuest_"+$(this).attr("id").split("_")[1]).toggle("fast"); });
    */


		//code modified from w3schools.com

		var male_pos_select_default = "";
		var c_name = "male_pos_select_remember=";
		var c_start=document.cookie.indexOf(c_name);
  	if (c_start!=-1)
    {
    c_start=c_start + c_name.length;
    var c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    male_pos_select_default = document.cookie.substring(c_start,c_end);
    //alert("!" + document.cookie.substring(c_start,c_end) + "!");
    }

    var female_pos_select_default = "";
		c_name = "female_pos_select_remember=";
		c_start=document.cookie.indexOf(c_name);
  	if (c_start!=-1)
    {
    c_start=c_start + c_name.length;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    female_pos_select_default = document.cookie.substring(c_start,c_end);
    //alert("!" + document.cookie.substring(c_start,c_end) + "!");
    }

		//end of modified w3schools.com code

		//alert(female_pos_select_default + male_pos_select_default);

    $("#searchMan").change(pos.fillter).val(male_pos_select_default);
    $("#searchWoman").change(pos.fillter).val(female_pos_select_default);

    pos.fillter();
		if(id)
			{
			ulListSelect(id);
			xulListSelect(id);
			yulListSelect(id);
			$(".xulList_"+id).each(function()
				{
		    if($(this).parents(".blogQuest").css("display")=="none")
					{
		     	$(".firsthalf_pos_0").show();
					$("#blogless_basic").show();
					$("#blogmore_basic").hide();
		     	}
      	});
			}
    swapAjaxLinks(pos.load,pos.loadBefore);
		pos.load();
  },
  textClick:function(x){
    $(".textCntItem").hide();
    $("#"+x.attr("id")+"Cnt").show();
    if (x.attr("id") == "story")
    	{
			document.getElementById("title_last_3_visible").style.display = "none";
			}
			else
			{
			document.getElementById("title_last_3_visible").style.display = "block";
			}
  },
  mediaClick:function(x){
    $(".mediaCntItem").hide();
    $("#"+x.attr("id")+"Cnt").show();
              //  2
    //document.write("#"+x.attr("id")+"Cnt");
    if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();
	if (x.attr("id") == "vids")
    {
		$("#vidsList").show();
	}
  },
  showImage:function(id){
    $(".imgSlide").hide();
    $("#imgSlide_"+id).show();
    $("#imgsList").hide();

    imgs.current = id;
    imgs.fade();

    $("#imgsShow").show();
    if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();
  },
  showImgList:function(){
    $("#imgsList").show();
    $("#imgsShow").hide();

    document.getElementById('video_player_div').innerHTML	="";
  },
  showVideo:function(id, id_name, coverimg){
    $("#vidsList").hide();
    //$("#vidsListItem_"+id).show();
      document.getElementById('video_player_div').innerHTML	="";
      //{"type":"command","command":"run", "application":"VideoPlayer"}
    var command	=
	{
		"type":				"command",
		"command":			"run",
		"application":		"VideoPlayer",

		"parameters":
		{
			"video":		(id),
			"video_name":	(id_name),
			"cover":		(coverimg)
		}
	};

	//'app/Videoplayer:video:' + id + '/video_name:' + id_name + '/cover:' + coverimg
	alert(JSON.stringify(command));
	if (window.gui_communication.communication_ready)
	{
		window.gui_communication.send(JSON.stringify(command));
	}
    else
    {
	 	alert('NEMREDI');
	 }
  },
  showVideoList:function(){
    //$(".vidsListItem").show();
    $("#vidsList").show();

    document.getElementById('vidsList').style.display="block";
    $("#vidsCnt").show();
    //$("#haha").trigger("click");
    /*if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();*/
  },
  loadBefore:function(href){
  	if (window.gui_videoplayer)
		window.gui_videoplayer.close_player();

    if(href)
			{
			var doc_num = href.match(/[0-9]+/);

			ulListSelect(doc_num);
      xulListSelect(doc_num);
      yulListSelect(doc_num);

    	if($("#pos_filter_list_head").css("display")=="block") //if it's minimzed, restore
    		{
    		$(".ulList_"+doc_num).each(function()	{
		      if($(this).css("display")!="none")
						{
						alert(doc_num);
		      	$("#pos_filter_list_body").show();
		      	}
      		});
				}
			}


    	$(".xulList_"+doc_num).each(function()
				{
		    if($("#pos_basic_list_body").css("display")=="none")
					{
					$("#pos_basic_list_body").show();
					}
		    if($(this).parents(".blogQuest").css("display")=="none")
					{
		     	$(".firsthalf_pos_0").show();
					$("#blogless_basic").show();
					$("#blogmore_basic").hide();
		     	}
      	});

      $(".yulList_"+doc_num).each(function()
				{
		    if($("#pos_other_list_body").css("display")=="none")
					{
					$("#pos_other_list_body").show();
					}
				});
  },
  load:function(href){
		initTabs("#textCnt",pos.textClick);
		initTabs("#mediaCnt",pos.mediaClick);
		imgs.init();
		$(".fav").click(function(){
			var a=$(this);
			$.get("/positions/fav/"+a.attr("id").split("_")[1],function(d){
				var nr = parseInt(d);
				switch (nr)
				{
					case 0:
						a.removeClass("favYes");
						break;
					case 1:
						a.addClass("favYes")
						break;
					case 2:
						$("#notloggedImg").hide();
						$("#favWarn").show();
						break;
				}
			});
		});
		CommentAreaDefaultBehaviour("PosComment");
  },
  fillter:function(){

		//$("#searchMan").val("u");

		$("#pos_filter_list_minmax").attr("src","/imgs/posfilter/up.gif");

		var m=$("#searchMan").val();
    var w=$("#searchWoman").val();
    /*alert(m+" AAA "+w);*/

    document.cookie = "male_pos_select_remember=" + escape(m) + "; path=/positions/;";
		document.cookie = "female_pos_select_remember=" + escape(w) + "; path=/positions/;";
    /*document.write("AAA");
		window.gui_event_log.add('asd',document.cookie);*/

    if (m != "" || w != "")
    	{
			$("#pos_filter_list_head").show();
			$("#pos_filter_list_body").show();
			}
			else
			{
			$("#pos_filter_list_head").hide();
			$("#pos_filter_list_body").hide();
			}

    $(".ulList li").each(function(){
      var rel=$(this).attr("rel");
      var tm=rel.substr(0,1);
      var tw=rel.substr(1,1);
			//window.gui_event_log.add('asd',document.cookie);
			//alert(m+" "+w+" "+tm+" "+tw);
      var ok=0;
      if((m=="" || m==tm) && (w=="" || w==tw)) ok=1;
      if(ok) $(this).show(); else $(this).hide();
    });

    $(".scroll").attr("scrollTop",0);
  }
};

var imgs={
  init:function(){
    var imgSlide=$(".imgSlide");
    imgs.current=0;
    imgs.count=imgSlide.size();
    imgs.fade();
    //imgSlide.lightbox();
  },
  fade:function(){
    if(imgs.current==0) $("#imgPrev").addClass("invisible"); else $("#imgPrev").removeClass("invisible");
    if(imgs.current==imgs.count-1) $("#imgNext").addClass("invisible"); else $("#imgNext").removeClass("invisible");
  },
  next:function(){
    if(imgs.current<imgs.count-1){
      $("#imgSlide_"+imgs.current).hide();
      imgs.current++;
      $("#imgSlide_"+imgs.current).show();
    }
    imgs.fade();
    return false;
  },
  prev:function(){
    if(imgs.current>0){
      $("#imgSlide_"+imgs.current).hide();
      imgs.current--;
      $("#imgSlide_"+imgs.current).show();
    }
    imgs.fade();
    return false;
  },
  fullSize:function(){
    $("#imgSlide_"+imgs.current).trigger("click");

    return false;
  }

};

var main={
	init_myaccount_toggle:function(){
	$("#main_myaccount_head").click(function() {generalMinMax("#main_myaccount_body","#main_myaccount_minmax"); $("#main_myaccount_body").toggle("fast");});

	},

	init_content_toggle:function(){
	$("#main_content_head").click(function() {generalMinMax("#main_content_body","#main_content_minmax"); $("#main_content_body").toggle("fast");});

	},

	init_article_toggle:function(){
	$("#artHead").click(function(){ $(".artlist_rest_n").hide(); $("#articlesLess").hide(); $("#articlesMore").show(); $("#blogQuest_art").toggle("fast"); });
  $("#articlesMore").click(function(){ $(this).hide(); $(".art_list_item").show();  $("#articlesLess").show();});
  $("#articlesLess").click(function(){ $(".artlist_rest_n").hide(); $(this).hide(); $("#articlesMore").show(); $(".scroll").attr("scrollTop",($("#artHead").attr("offsetTop")-47)); });
	},

	init_support_toggle:function(){
	$("#main_support_head").click(function() {generalMinMax("#main_support_body","#main_support_minmax"); $("#main_support_body").toggle("fast");});

	},
	init_search_more:function(){
  $("#blogItem_pos").click(function(){ $(".firstlis_pos_0").hide(); $("#blogless_main_search_pos").hide(); $("#blogmore_main_search_pos").show(); $("#blogQuest_pos").toggle("fast"); });
  $("#blogmore_main_search_pos").click(function(){ $("#blogQuest_pos li").show(); $(this).hide(); $("#blogless_main_search_pos").show();});
  $("#blogless_main_search_pos").click(function(){ $(".firstlis_pos_0").hide(); $(this).hide(); $("#blogmore_main_search_pos").show(); $(".scroll").attr("scrollTop",($("#blogItem_pos").attr("offsetTop")-47)); });

	$("#blogItem_terms").click(function(){ $(".firstlis_terms_0").hide(); $("#blogless_main_search_terms").hide(); $("#blogmore_main_search_terms").show(); $("#blogQuest_terms").toggle("fast"); });
  $("#blogmore_main_search_terms").click(function(){ $("#blogQuest_terms li").show(); $(this).hide(); $("#blogless_main_search_terms").show();});
  $("#blogless_main_search_terms").click(function(){ $(".firstlis_terms_0").hide(); $(this).hide(); $("#blogmore_main_search_terms").show(); $(".scroll").attr("scrollTop",($("#blogItem_terms").attr("offsetTop")-47)); });

  $("#blogItem_blog").click(function(){ $(".firstlis_blog_0").hide(); $("#blogless_main_search_blog").hide(); $("#blogmore_main_search_blog").show(); $("#blogQuest_blog").toggle("fast"); });
  $("#blogmore_main_search_blog").click(function(){ $("#blogQuest_blog li").show(); $(this).hide(); $("#blogless_main_search_blog").show();});
  $("#blogless_main_search_blog").click(function(){ $(".firstlis_blog_0").hide(); $(this).hide(); $("#blogmore_main_search_blog").show(); $(".scroll").attr("scrollTop",($("#blogItem_blog").attr("offsetTop")-47)); });

  $("#blogItem_doctor").click(function(){ $(".firstlis_doctor_0").hide(); $("#blogless_main_search_doctor").hide(); $("#blogmore_main_search_doctor").show(); $("#blogQuest_doctor").toggle("fast"); });
  $("#blogmore_main_search_doctor").click(function(){ $("#blogQuest_doctor li").show(); $(this).hide(); $("#blogless_main_search_doctor").show();});
  $("#blogless_main_search_doctor").click(function(){ $(".firstlis_doctor_0").hide(); $(this).hide(); $("#blogmore_main_search_doctor").show(); $(".scroll").attr("scrollTop",($("#blogItem_doctor").attr("offsetTop")-47)); });

  $("#blogItem_articles").click(function(){ $(".firstlis_articles_0").hide(); $("#blogless_main_search_articles").hide(); $("#blogmore_main_search_articles").show(); $("#blogQuest_articles").toggle("fast"); });
  $("#blogmore_main_search_articles").click(function(){ $("#blogQuest_articles li").show(); $(this).hide(); $("#blogless_main_search_articles").show();});
  $("#blogless_main_search_articles").click(function(){ $(".firstlis_articles_0").hide(); $(this).hide(); $("#blogmore_main_search_articles").show(); $(".scroll").attr("scrollTop",($("#blogItem_articles").attr("offsetTop")-47)); });

	$("#main_search_head").click(function() {generalMinMax("#main_search_body","#main_search_minmax"); $("#main_search_body").toggle("fast");});

	$("#main_search_closer").click(function() {$("#main_search_head").hide(); $("#main_search_body").hide(); return false;});

	return false;
	},
  search:function(){
    $("#searchResultScroll").load("/main/search",{q:$("#searchQuery").val()});
    $(".scroll").attr("scrollTop",0);
		return false;
  },
  bookmark:function(){
    if(document.all) window.external.AddFavorite(location.href,document.title);
    else if(window.sidebar) window.sidebar.addPanel(document.title,location.href,'');
    return false;
  }
};


$(function(){
  // menu mouse hover
  $("div.menu a").hover(function(){
    var stat=parseInt($(this).attr("class").match(/menu_([0-9]+)/)[1]);
    $(this).attr("defStat",stat+"");
    var c;
    if(stat!=3){
      $(this).attr("defStat",stat);
      c=getPrevNextMenu(this,2);
      $(this).removeClass("menu_"+stat).addClass("menu_2");
      c.p.removeClass(c.prev).addClass(c.prevNew);
      c.n.removeClass(c.next).addClass(c.nextNew);
    }
  },function(){
    var stat=parseInt($(this).attr("defStat"));
    if(!isNaN(stat)){
      var c;
      if(stat!=3){
        var c=getPrevNextMenu(this,stat);
        $(this).removeClass("menu_2").addClass("menu_"+stat);
        c.p.removeClass(c.prev).addClass(c.prevNew);
        c.n.removeClass(c.next).addClass(c.nextNew);
      }
    }
  });
});

function repos_video()
{
	return false;
	//alert('posing');
	var posobj = document.getElementById('video_target');
	if (!posobj)
		return false;
	var pos = findPos(posobj);
	//alert(document.getElementById('video_player_div'));
	//alert("POS:" + pos);
	document.getElementById('video_player_div').style.left=pos[0] + 'px';
	document.getElementById('video_player_div').style.top=pos[1] + 'px';
}

function hide_player()
{
	if (document.getElementById('video_player_div'))
	{
		document.getElementById('video_player_div').style.display='none';
	}
}

function show_player()
{
	if (document.getElementById('video_player_div'))
	{
		document.getElementById('video_player_div').style.display='block';
	}
}

function run_scripts()
{
	var obj = document.getElementById('runscript');
	var counter = 0;
	while (obj && counter<100)
	{
		try
		{
			eval(obj.value);
			obj.setAttribute('id','runned_script');

			if (parentof(obj))
				parentof(obj).removeChild(obj);
		}
		catch(ex)
		{}
		counter++;
		obj = document.getElementById('runscript');
	}
}


function parentof(obj) {
    if (obj.parentElement) {
    	return obj.parentElement;
    } else {
        return obj.parentNode;
    }
}

function clean_div(div_id)
{
	var obj = document.getElementById(div_id);
	if(obj)
		obj.innerHTML = '';
	return true;

}

function sendGoogleTrack(pagename)
{
	try
	{
		pageTracker._trackPageview("/" + pagename);
	}
	catch (e)
	{

	}

}

$.clientCoords = function(){
    if(window.innerHeight || window.innerWidth){
        return {w:window.innerWidth, h:window.innerHeight}
    }
    return {
        w:document.documentElement.clientWidth,
        h:document.documentElement.clientHeight
    }
}

var bubble_timers = new Array();
var bubble_open = new Array();

function showBubble_timer(obj, bubbleid)
{
	clearBubble_timer(bubbleid);

	bubble_timers[bubbleid] = setTimeout("showBubble(" + bubbleid + ")", 100);
}

function hideBubble_timer(bubbleid)
{
	clearBubble_timer(bubbleid);

	bubble_timers[bubbleid] = setTimeout("hideBubble(" + bubbleid + ")", 1000);
}

function clearBubble_timer(bubbleid)
{
	if (bubble_timers[bubbleid])
		clearTimeout(bubble_timers[bubbleid]);
}

function showBubble(bubbleid)
{
	var bubobj = $("#bubble_info_" + bubbleid);
	var markobj = $("#bubble_questionmark_" + bubbleid);

	for (var open_bub_id in bubble_open)
	{
		if (bubble_open[open_bub_id])
			hideBubble(open_bub_id);
	}

	if (bubobj)
	{
		bubble_open[bubbleid] = 1;
		var mark_left = markobj.position().left;
		var parent_left = markobj.parent().position().left;
		bubobj.css('left',mark_left + markobj.width());

    	bubobj.fadeIn("slow");

		var clientcoords = $.clientCoords();
		var bubobjoffset = bubobj.offset();
		var bubpos		 =  bubobj.position();

		if(bubobjoffset.left + bubobj.width() >= clientcoords.w) {

//			confirm();
//			confirm(bubpos.left)
			var diff=bubobjoffset.left+bubobj.width()-clientcoords.w;
			bubobj.css('left', bubpos.left-diff-20);
//			confirm(clientcoords.w+);
		}

	}

}

function hideBubble(bubbleid)
{
	var bubobj = $("#bubble_info_" + bubbleid);
	if (bubobj)
	{
		bubble_open[bubbleid] = 0;
    	bubobj.fadeOut("slow");
    }
}

function getIndexOfValue(select, value)
{
	for (var i = 0; i < select.options.length; ++i)
	{
		if (select.options[i].value == value)
			return i;
	}
	return 0;
}


function ajax_login(user, sessid)
{
	alert(user + " " + sessid);
	$.ajax({
		type: "POST",
		cache: false,
		url: "/login/",
		data: "user=" + user + "&gui_session_id=" + sessid,
		success: function(msg) {}
	});

}

function getElementBySpecial(o,tagname, nr)
{
	var list = o.getElementsByTagName(tagname);
	if (list.length > 0)
	{
		if (nr == "last") return list[list.length-1];
		else if (nr == "first") return list[0];
		else if (paresInt(nr) >= list.length) return false;
		else return list[nr];
	}
}

function sendUserTimeZone(data){

  if ( data == 0 ) {
    var d = new Date()
    var gmtHours = -d.getTimezoneOffset()/60;

    $.ajax({
      type: "POST",
      cache: false,
      url: "/main/setUserTimeZone",
      data: "TimeZone=" + gmtHours
    });
  }
}

function showDialog(name_,width_,height_,modal_) {

	var button = "";

	if ( name_ == "app_under_construction" ) button = '';
	else if (name_ == "hankypanky_send")
	{
		hankyDialogInit();
		button = '';
	}
	else if (name_ == "check_payment") button = '';
	else if (name_ == "save_nick") button = '';
	else if (name_ == "hamkypanky_delete_fav") button = '';
	else if (name_ == "hankypanky_send_ok" || name_ == "hankypanky_send_nook" || name_ == "article_save_ok"  || name_ == "article_save_nook") button = '';
	else if ( gengo == 'hu' && rogu_kiroku == 0 ) button = { 'Bejelentkezés': function() { window.location = '/login'; }, 'Regisztráció': function() { window.location = '/signup'; } };
	else if ( gengo == 'en' && rogu_kiroku == 0 ) button = { 'Log in': function() { window.location = '/login'; }, 'Sign up': function() { window.location = '/signup'; } };
	else if ( gengo == 'hu' && rogu_kiroku == 1 ) button = { 'Előfizetés': function() { window.location = '/buy'; } };
	else if ( gengo == 'en' && rogu_kiroku == 1 ) button = { 'Subscription': function() { window.location = '/buy'; } };

	$("#"+name_).dialog({
		autoOpen: false,
		width: width_,
		height: height_,
		modal: modal_,
		resizable: false,
		buttons: button
	});


	$("#"+name_).dialog('open');
}

function closeDialog(name)
{
	$("#" + name).dialog("close");
}

function luciPing()
{
	try
	{
		window.luci_communication.send({"type":"command", "command":"run", "process":"PingSessionTimeout"});
	}
	catch (e)
	{
	}
}
