var linkujing = -1;
var mVect, yLim;
var mStep = 0;

function moveSign() {
  var x1 = findPosX(getEl('bubble-div'));
  var y1 = findPosY(getEl('bubble-div'));
  if (y1 >= yLim) {
    mStep ++;
    getEl('bubble-div').style.top = y1 - mStep + "px";
    getEl('bubble-div').style.left = x1 + (mStep*mVect)+ "px";
    setTimeout('moveSign()', 50);
  } else {
    getEl('bubble-div').style.visibility = "hidden";
  }
}

function add_link (id) {
  if (linkujing == id) {
    alert('Tento článek máte již nalinkován !');
    return;
  } else {
    linkujing = id;
  }
    var click_id = "diglink" + id;
    var x1 = findPosX(getEl(click_id)) + 15;
    var y1 = findPosY(getEl(click_id));
    //var x2 = findPosX(getEl('moje')) - 280 ;
    //var y2 = findPosY(getEl('moje'));
    //mVect = (x2 - x1 ) / (y1 - y2) 
    //yLim = y2;
    whiteOut('linkujs-strong-'+id, true);
    currdigs = parseInt(getIn('linkujs-strong-'+id));
    //mylinkscnt = parseInt(getIn('my_links_cnt'));
    currdigs ++;
    //mylinkscnt ++;
    setTimeout('linkuj('+id+')', 1200);
    setTimeout('setIn(\'linkujs-strong-'+id+'\', '+currdigs+')', 1000);
    //setTimeout('setIn(\'my_links_cnt\', '+mylinkscnt+')', 1000);
    setTimeout('setIn(\'bubble-div\', \'<span><img src="img/ok1.gif" width="20" height="17" title="Již máte linkováno" /></span>\')', 900);
    setTimeout('getEl("bubble-div").style.top = "'+y1+'px"', 900);
    setTimeout('getEl("bubble-div").style.left = "'+x1+'px"', 900);
    setTimeout("getEl('"+click_id+"').style.visibility = 'hidden';", 1000);
    setTimeout("getEl('bubble-div').style.visibility = 'visible';", 1000);
    //setTimeout('moveSign()', 2000); 
}

function fadeIn(id,start){
  if (start) hex = 0;
  if (hex<50) {	
    hex=hex+1; // color change
    document.getElementById(id).style.color="rgb("+246+(14/50)*hex+","+225+(25/50)*hex+","+96+(129/50)*hex+")"; // Set color value.
    setTimeout("fadeIn('"+id+"')",20);	
  }
}
  
function fadeOut(id,start){
  if (start) hex = 50;
  if(hex>0) {	
    hex=hex-1; // degrease color value
    document.getElementById(id).style.color="rgb("+246+(14/50)*hex+","+225+(25/50)*hex+","+96+(129/50)*hex+")"; // Set color value.
    setTimeout("fadeOut('"+id+"')",20);	
  }
}

function whiteOut(id,start) {
  if (start) hex = 0;
  if (hex < 255) {	
    hex=hex+5; 
    document.getElementById(id).style.color="rgb("+hex+","+hex+","+hex+")"; // Set color value.
    setTimeout("whiteOut('"+id+"')",20);	
  }
}


// get value in id
function getIn(id) {
  return document.getElementById(id).innerHTML;
}

// set value in id
function setIn(id,value) {
  return document.getElementById(id).innerHTML = value;
}

// return the element id
function getEl(id) {
  return document.getElementById(id);
}




String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

// link linked
function linkuj(id) {
  var req = new XMLHttpRequest(); 
  if (req) {
    req.onreadystatechange = function() { 
                if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
                  setIn('linkujs-strong-'+id,req.responseText);
                } 
      }; 
    req.open('GET', 'ajax.php?id=addmylink&link_id=' + id); 
    req.send(null); 
    }
}

// link linked
function ajaxCall(par1,par2) {
  //alert("jo"); 
  var req = new XMLHttpRequest();
  if (req) {
    req.onreadystatechange = function() { 
                if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
                  setIn('bubble-div',req.responseText);
                } 
      }; 
    req.open('GET', 'ajax.php?id=bubble&par1='+par1+'&par2='+par2); 
    req.send(null); 
    }
}

// check the url, return pic set
function checkURL(url) {
  setIn('url_note','<span style="color:silver; font-weight:bold"><img src="img/wait.gif" height="16" widht="16" /> Kontroluji existenci zadané URL ... </span>');
  var req = new XMLHttpRequest(); 
  if (req) {
    req.onreadystatechange = function() { 
                if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
                  eval(req.responseText);
                  //setIn('link-images', req.responseText);
                } 
      }; 
    req.open('GET', 'ajax.php?id=checkurl&url=' + url); 
    req.send(null); 
    }
}

function changeImg(nr) {
  getEl("pic").src = getEl('prev' + nr).src;
  getEl("img_url").value = getEl('prev' + nr).src;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

// show picture
function showImage(logic,id,obj) {
  id = "im"+id;
  if (logic) {
	  var newX = findPosX(obj);
	  var agt=navigator.userAgent.toLowerCase();
	  var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	  if ( is_ie && agt.indexOf("msie 6.")!=-1 && Left(id,3)=="img")  var newY = findPosYIE(obj); else var newY = findPosY(obj);
 	  getEl("show-image").style.top = newY + 40 + 'px';
	  getEl("show-image").style.left = newX + 50 + 'px';
    getEl("show-image-image").src = getEl(id).src;
    //getEl(id).style.border = "1px solid rgb(255, 0, 132)";
    getEl("show-image").style.visibility = "visible";  
  } else {
    getEl("show-image").style.visibility = "hidden";
    //getEl(id).style.border = "1px solid black";
  }
}

// show bubble
function showBubble(logic,id,obj,par1,par2) {
  // websnapr
  if (logic && par1 == 9) {
  	var imgUrl = "http://images.websnapr.com/?url=" + par2 + "&size=s";
	var bubUp = "<table width='183' cellpadding='0' cellspacing='0' class='bubble_tab' style='table-layout:fixed' border = '0'><tr><td class='bubble_header' height='39 px'></td></tr><tr><td class='bubble_body' align='center' >";
  	var bubDown = "</td></tr><tr><td class='bubble_footer' height='27 px'></td></tr></table>";
	setIn("bubble-div", bubUp + "<img src='" + imgUrl + "' title='websnapr' />" + bubDown);
	var newX = findPosX(obj);
	var agt=navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	if ( is_ie && agt.indexOf("msie 6.")!=-1 && Left(id,3)=="img")  var newY = findPosYIE(obj); else var newY = findPosY(obj);
	if (par1==0) {var plusX=50; var plusY=40;} else {var plusX=8; var plusY=10;}
    if (par1==2 || par1==4 || par1==9) {newX = newX - 0; newY = newY + 12;}
	getEl("bubble-div").style.left = newX + plusX + 'px';
	getEl("bubble-div").style.top = newY + plusY + 'px';
    getEl("bubble-div").style.visibility = "visible";  

  	return;
  }





  if (logic) {
    setIn("bubble-div","<img src='img/wait_dot3.gif' title='Momentík ...' />");
    ajaxCall(par1,par2);
	  var newX = findPosX(obj);
	  var agt=navigator.userAgent.toLowerCase();
	  var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	  if ( is_ie && agt.indexOf("msie 6.")!=-1 && Left(id,3)=="img")  var newY = findPosYIE(obj); else var newY = findPosY(obj);
	  if (par1==0) {var plusX=50; var plusY=40;} else {var plusX=8; var plusY=10;}
    if (par1==2 || par1==4) {newX = newX - 240; newY = newY + 30;}
	  getEl("bubble-div").style.left = newX + plusX + 'px';
 	  getEl("bubble-div").style.top = newY + plusY + 'px';
    getEl("bubble-div").style.visibility = "visible";  
  } else {
    getEl("bubble-div").style.visibility = "hidden";  
  }
}

// change del image
function delc(obj) {
  if ( Right(obj.src,6) == "bw.gif") { obj.src = "img/del.gif"; } else { obj.src = "img/del_bw.gif"; }
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	var min = obj.offsetTop;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosYIE(obj)
{
	var curtop = 0;
	var min = obj.offsetParent.offsetTop;
	//alert(obj.offsetParent.offsetTop);
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop - min;
}

function checkLen() {
  var l = getEl("description").value.length;
  if( l >250) {
    alert(' Délka zprávy může být maximálně 250 znaků. ');
    return false;
  }
  
  setIn("lab_info", "Zbývá " + (250 - l) + " znaků");
  return true;
}


      function loadcode() {
			  if ( sentence!= encodeURIComponent(document.f.q.value) ) {
					  sentence = encodeURIComponent(document.f.q.value);				
            var req = new XMLHttpRequest();
            if (req) {
              req.onreadystatechange = function () {
                if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
                   eval(req.responseText);
                } else { 
                	 info2.innerHTML = '<font color="#006dba" size="2"><b>Checking ...</b></font>';  
                }
        			};
              req.open('GET', 'ask.php?q=' + sentence);
              req.send(null);
    				  setTimeout("loadcode(encodeURIComponent(document.f.q.value))",2000);					
            }
				} else {
				    setTimeout("loadcode(encodeURIComponent(document.f.q.value))",2000);				
				}
			  
      }     		 

function inviteFriends(a) {
	a.href = 'mailto:?subject=Telegram LINKUJ.CZ&body=' + 'nazdar kamarade STOP%0D%0A%0D%0Aobjevil jsem dobry web STOP%0D%0Ajeho adresa je: http://linkuj.cz STOP%0D%0A%0D%0Amrkni na to a pridej si me do klanu STOP %0D%0A';
	return true;
}

function add_tag(tag) {
  var max_length = 75;
  var e = document.getElementById("title").value;
  if (e.lastIndexOf(" #") > 0) {e = e.substr(0, e.lastIndexOf(" #"));} 
  if ((e.length + tag.length + 1) > max_length) {
    alert("Nadpis může mít max " + max_length + " písmen");
   } else {
   document.getElementById("title").value = e +  " " + tag;
  }
}
