var sugg_options = {
    script: function (input) { return "/inc/ajax/suggest.php?json=true&limit=10&input="+input+"&typ="+document.getElementById('search_co').value; },
    json:true,
    shownoresults:false,
    maxresults:10,
    cache: false,
    minchars: 3,
    timeout: 8000,
    callback: function (obj) { document.forms.form_search.submit(); }	    
};

//if(window.self!=window.top)
//	top.location="http://www."+msg6+"/?a=uvod";

function info(i,w,h){
	window.open("info.php?i="+i,"","menubar=0,scrollbar=0,status=0,title=0,top=100,left=150,width="+w+",height="+h)
}

function counter(i) 
 {
  var _element = document.getElementById ( "ServerCommunications" );
  if ( _element != null ) { document.body.removeChild ( _element ); } 
  _element = document.createElement ('script'); 
  _element.src='http://www.'+msg6+'/inc/redir.php?i='+i+''
  _element.id='ServerCommunications';
  _element.type='text/javascript';
  document.body.appendChild ( _element );
 }

function menu_show(item){
	var Item = document.getElementById (item);
	if(menu_visible[item]){
		Item.style.display='none';
		menu_visible[item]=0;
		setCookie(item,0);
	}else{
		Item.style.display='block';
		menu_visible[item]=1;
		setCookie(item,1);
	}
}

function menu_hide(item){
	var Item = document.getElementById (item);
	Item.style.display='none';
}

function setCookie(name, value) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*365);
	document.cookie= name + "=" + escape(value) +
	";expires=" + expire.toGMTString() +
	";domain="+msg6;
}

function changeDiv(outerDiv,url,typ,fn) {
      if (window.ActiveXObject) {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        httpRequest = new XMLHttpRequest();
      }
      httpRequest.open("GET", url, true);
      httpRequest.onreadystatechange= function () { changeDivProcess(outerDiv,typ,fn); } ;
      httpRequest.send(null);
}

function changeDivProcess(outerDiv,typ,fn) {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {
      var DIV = document.getElementById(outerDiv);
      if(DIV!=null){
          if(typ==1){
		var res='';
		var result1=httpRequest.responseText.split("#script#");
		for(var i=0;i<result1.length;i++){
		    if(i%2==0){
			res += result1[i];
		    }
		    else{
			eval(result1[i]);
		    }
		}
    		DIV.innerHTML = res+DIV.innerHTML;
		
          }else if(typ==2){
		var result1=httpRequest.responseText.split("#script#");
		for(var i=0;i<result1.length;i++){
		    if(i%2==0){
			DIV.innerHTML += result1[i];
		    }
		    else{
			eval(result1[i]);
		    }
		}
          }
          else{
		DIV.innerHTML = "";
		var result1=httpRequest.responseText.split("#script#");
		for(var i=0;i<result1.length;i++){
		    if(i%2==0){
			DIV.innerHTML += result1[i];
		    }
		    else{
			eval(result1[i]);
		    }
		}
          }
    	if(fn!=undefined)
	    fn();
        } else {
            //alert("Chyba pri nacitani stranky "+ httpRequest.status +":"+ httpRequest.responseText);
        }
      }
  }
}

function switchDiv(outerDiv,html,typ){
	var DIV = document.getElementById(outerDiv);
	if(typ==1){
		DIV.innerHTML = html+DIV.innerHTML;
	}else if(typ==2){
		DIV.innerHTML += html;
	}
	else{
		DIV.innerHTML = html;
	}
}

window.status=msg1;
//katalog zacatek
function el(aa){
	aa.style.backgroundColor='#EDF4FC'
}
function el2(aa){
	aa.style.backgroundColor=''
}
//katalog konec

//nahledy ajax zacatek
timerID=null;
xtop=0;
xleft=0;
ajax_height=0

function show_gal(top,left,num,typ,height,direction,detail){
	xtop=top;
	xleft=left;
	timerID=setTimeout("show_gal2("+top+","+left+",'"+num+"','"+typ+"','"+height+"','"+direction+"','"+detail+"')",500);
}

function hide_gal(){
	var el=document.getElementById("gal_info");
	el.style.display='none';
	if(timerID!=null)
		clearTimeout(timerID);
}

function show_gal2(top,left,num,typ,height,direction,detail){
	var el=document.getElementById("gal_info");
	el.innerHTML=msg5+' ...';
	ajax_height=height;
	el.style.height=height+"px";
	el.style.display='block';
	move_gal(xtop,xleft,direction);
	changeDiv("gal_info","/fcgi/gal_info.php?num="+num+"&nick="+detail+"&t="+typ);
}

function move_gal(top,left,direction){
	var el=document.getElementById("gal_info");
	if (document.documentElement) {
	    var sTop=document.documentElement.scrollTop;
	    var sLeft=document.documentElement.scrollLeft;
	}else{
	    var sTop=document.body.scrollTop;
	    var sLeft=document.body.scrollLeft;
	}
	if(el.style.display=='block'){
		if(direction=='up'){
			el.style.top=(sTop+top-18-10-ajax_height)+"px";
			el.style.left=(sLeft+left-83)+"px";
		}
		else{
			el.style.top=(sTop+top+18)+"px";
			el.style.left=(sLeft+left-83)+"px";
		}
	}else{
		xtop=top;
		xleft=left;		
	}
}
//nahledy ajax konec

//slideshow start
var sl=0,timer=1,img_loaded=0
var im=new Image();
var timeout=5
var Obrazek	//=new Object()
im.onload = obr_load
im.onerror = sl_stop
im.onabort = sl_stop

function obr_load(){
	img_loaded=1
	obr_switch()
}

function time_go(){
	timer=1
	obr_switch()
}

function sl_stop(){
	if(sl){
		sl=0
		chobr(-1)
	}
	sl=0
}
//slideshow stop

//video_nahledy
var th_next,th_el,th_timer,th_gal;
function swthumb_start(el,g){
	th_gal=g;
	th_next=1;
	th_el=el;
	swthumb_image();
}

function swthumb_stop(){
	if(th_timer!=null){
		clearTimeout(th_timer);
		th_timer=null;
	}
	th_el.src="http://cachevideo.webmisto.cz/"+th_gal+"/n.jpg";
}

function swthumb_image(){
	var im=new Image();
	im.src="http://cachevideo.webmisto.cz/"+th_gal+"/"+th_next+".jpg";
	th_next=(th_next)%4+1;
	th_el.src=im.src;
	th_timer=setTimeout("swthumb_image()",1000);
}
//video_nahledy - konec
function addToBookmarks(){
	if(navigator.appName=="Microsoft Internet Explorer"){
		external.AddFavorite('http://www.'+msg6,msg1);counter(10);
	}
	else if(navigator.appName=="Netscape"){
		alert(msg2+".");
	}
	else if(navigator.appName=="Opera"){
		alert(msg2+".");		
	}
}

function addAsHomepage(co){
	if(navigator.appName=="Microsoft Internet Explorer"){
		co.style.behavior='url(#default#homepage)';co.setHomePage('http://www.'+msg6+'/');counter(11)
	}
	else if(navigator.appName=="Netscape"){
		alert(msg3);
	}
	else if(navigator.appName=="Opera"){
		alert(msg4);		
	}
}

function web_search_select(co,id,text){
	if(document.getElementById("search1"))
	    document.getElementById("search1").className='';
	if(document.getElementById("search2"))
	    document.getElementById("search2").className='';
	if(document.getElementById("search3"))
	    document.getElementById("search3").className='';
	if(document.getElementById("search4"))
	    document.getElementById("search4").className='';
	if(document.getElementById("search5"))
	    document.getElementById("search5").className='';
	if(document.getElementById("search6"))
	    document.getElementById("search6").className='';
	id.className='search_selected';
	document.forms.form_search.co.value=co;
	document.forms.form_search.submit_btn.value=text;
//	document.forms.form_search.search.focus();
	if(co=='napoveda'){
	    document.forms.form_search.action='http://help.'+msg6+'/index.php';
	}
	else{
	    document.forms.form_search.action='/';
	}

	if(co=='lide'){
	    sugg_options.minchars=3;
	}
	else if(co=='fotky' || co=='videa'){
	    sugg_options.minchars=1;
	}
	else{
	    sugg_options.minchars=1000;	
	}
}

function show_gal_top(top,left,html){
    var el=document.getElementById("gal_info_top");
    el.innerHTML=html;
    move_gal_top(top,left);	
    el.style.display='block';
}
		
function hide_gal_top(){
    var el=document.getElementById("gal_info_top");
    el.style.display='none';
}
			
			
function move_gal_top(top,left){
    var el=document.getElementById("gal_info_top");
    if (document.documentElement) {
	var sTop=document.documentElement.scrollTop;
	var sLeft=document.documentElement.scrollLeft;
    }else{
	var sTop=document.body.scrollTop;
	var sLeft=document.body.scrollLeft;
    }

    el.style.top=sTop+top+18+"px";
    el.style.left=sLeft+left-23+"px";
}

function loadSmiles(){
   if (window.ActiveXObject) {
   	sm_ajax = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
   	sm_ajax = new XMLHttpRequest();
    }
	sm_ajax.open("GET", '/inc/ajax/smiles.php?akce=get&sess='+sess, true);
	sm_ajax.onreadystatechange= function () {smProcess()} ;
	sm_ajax.send(null);
}

function smProcess() {
  if (sm_ajax.readyState == 4) {
    if(sm_ajax.status == 200) {
   		var smiles=eval(sm_ajax.responseText);
   		var html='';
   		for(var i=0;i<smiles.length;i++){
	   		html+="<a href='javascript:addsmile("+smiles[i][0]+")' title='*"+smiles[i][0]+"*'><img src='http://img2.amateri.cz/obr/smile/"+smiles[i][0]+".gif' border='0' width='"+smiles[i][1]+"' height='"+smiles[i][2]+"' style='padding:2px'></a>";
   		}
	   	document.getElementById('smilearea').innerHTML=html;
    }
  }
}

function addsmile(myValue)
{
	var myField=document.getElementById('zprava');
	myValue='*'+myValue+'*';	
	if (document.selection) {
	    myField.focus();
    	sel = document.selection.createRange();
    	sel.text = myValue;
	}
 	else if (myField.selectionStart || myField.selectionStart == '0') {
	    var startPos = myField.selectionStart;
    	var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
	} else {
	    myField.value += myValue;
  }
}

var smiles_loaded=0;
function switchSmiles(){
	var smilearea=document.getElementById('smilearea');
	var smilebutton=document.getElementById('smilebutton');
	if(smilearea.style.display!='none'){
		location='http://www.'+msg6+'/?a=smiles_edit';
	}else{
		if(!smiles_loaded)
			loadSmiles();
		smilearea.style.display='block';
		smilebutton.innerHTML=msg8;
	}
}

function getY( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
    }
    return iReturnValue;
}
				
function getX( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function bublina(el,id,width,param){
    var bl=document.getElementById("bublina");
    bl.style.width=width+'px';
    var fn=function(){
	var bl=document.getElementById("bublina");
        bl.style.display='block';
	bl.style.top=getY(el)-bl.offsetHeight-30+'px';
    }
    changeDiv("bublina",'/inc/ajax/bublina.php?id='+id+(param!=undefined ? param : ''),0,fn);
    bl.style.left=getX(el)-100+'px';
}

function anketa_vote(aid,op){
    switchDiv('anketa'+aid+'_clk',msg9+'...');
    var url='inc//ajax/anketa.php?aid='+aid+'&op='+op+'&m='+Math.random();
    changeDiv('anketa'+aid,url);
}

function getCookie(c_name)
{
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1){ 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
	} 
    }
    return "";
}

function showHideStyle(theClass) { 
    var allPageTags=document.getElementsByTagName("*"); 
    var last;
    for (i=0; i<allPageTags.length; i++) { 
	if (allPageTags[i].className==theClass) {
            if(allPageTags[i].style.display!='none'){
		allPageTags[i].style.display='none';
		last='none';
	    }
	    else{
		allPageTags[i].style.display='inline';
		last='inline';
	    }
	} 
    }
    return last;
}

function showHideGals(){
    var item=document.getElementById("plusminus");
    if(item!=null){
	var last=showHideStyle('g_old');
	if(last=='none'){
	    item.src="http://img2.amateri.cz/obr/_obr_siroka/plus.png";
	    setCookie('oldgal_show',0);
	}
	else{
	    item.src="http://img2.amateri.cz/obr/_obr_siroka/minus.png";
	    setCookie('oldgal_show',1);
	}
    }
}

function imgprotect(theClass){
    var h,w,src;
    var allHTMLTags=document.getElementsByTagName('*');
    for (var i=0; i<allHTMLTags.length; i++) {
        if (allHTMLTags[i].className==theClass) {
	    allHTMLTags[i].onload=imgprotect_process;
	    allHTMLTags[i].oncontextmenu=function () {return false;};
	}
    }	
}

function imgprotect_process(){
    var co=this;
    if(!co.parentNode || co.tagName!='IMG'){
	return;
    }

    var img=new Image();	//ie bug
    img.src=co.src;
    var DIV=document.createElement("div");
    DIV.style.width=img.width+'px';
    DIV.style.height=img.height+'px';
    DIV.style.border=co.style.border;
    DIV.style.margin=co.style.margin;
    DIV.id=co.id;
    DIV.style.backgroundImage="url("+co.src+")";
    DIV.innerHTML="<img src='http://img2.amateri.cz/obr/b.gif' height='"+img.height+"' width='"+img.width+"' />"
    co.parentNode.replaceChild(DIV,co);
}

function img_process_restore(id,theClass){
    var DIV=document.getElementById(id);
    if(DIV==null || DIV.tagName!="DIV")
	return;
    var IMAGE_NEW=document.createElement("IMG");
    IMAGE_NEW.style.border=DIV.style.border;
    IMAGE_NEW.style.margin=DIV.style.margin;
    IMAGE_NEW.id=DIV.id;
    IMAGE_NEW.className=theClass;
    IMAGE_NEW.src=DIV.style.backgroundImage.substring(DIV.style.backgroundImage.indexOf("http"),DIV.style.backgroundImage.indexOf("jpg")+3);
    DIV.parentNode.replaceChild(IMAGE_NEW,DIV);
    IMAGE_NEW.onload=imgprotect_process;
}

function FitToContent(id, maxHeight)
{
    var text = id && id.style ? id : document.getElementById(id);
    if ( !text )
        return;

   var adjustedHeight = text.clientHeight;
   if ( !maxHeight || maxHeight > adjustedHeight ){
     adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
     if ( maxHeight )
          adjustedHeight = Math.min(maxHeight, adjustedHeight);
     if ( adjustedHeight > text.clientHeight){
         text.style.height = 15+adjustedHeight + "px";
     }
  }else{
	text.style.overflowY='auto';
   }
}

window.onload = function() {
   var allHTMLTags=document.getElementsByTagName('textarea');
   for (var i=0; i<allHTMLTags.length; i++) {
       if (allHTMLTags[i].className!="txt_noresize") {
	    allHTMLTags[i].style.overflowY='hidden';
	    allHTMLTags[i].onkeyup=function () {
	    	FitToContent( this, 500 );
	    };
	    FitToContent( allHTMLTags[i], 500 );
        }
    }
}

function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { return new XMLHttpRequest(); } catch(e) {}
    //alert("XMLHttpRequest not supported");
  return null;
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
    obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
      var r = obj.attachEvent("on"+evType, fn); 
      return r; 
 } else { 
      return false; 
  } 
}

function toCharRef(str){
    var charRefs = [], codePoint, i;
    for(i = 0; i < str.length; ++i){
	codePoint = str.charCodeAt(i);
	if(0x17 <= codePoint && codePoint <= 0x7E){ // printable ASCII characters (32 - 226)
	    charRefs.push(str.substr(i, 1))
	}
	else{
	    if(0xD800 <= codePoint && codePoint <= 0xDBFF){ // UTF-16
		codePoint = 0x2400 + ((codePoint - 0xD800) << 10) +	str.charCodeAt(++i);
	    }
	charRefs.push('&#' + codePoint + ';');
	}
    }
    return charRefs.join('');
}

function rotate_content(id,videoid,step,img,timeout){
    imageObj = new Image();
    imageObj.onload=function(){
	obj=document.getElementById(id);
        if(obj!=null){
	    obj.src=imageObj.src;
	    img=(img+step)%16;
	    if(img==0)
		img=16;
	}
	setTimeout(function(){rotate_content(id,videoid,step,img,timeout)},timeout);		
    };
    imageObj.src='http://stream2.amateri.cz/nahledy/'+videoid+'/n/'+img+'.jpg';
}
