// sweetTitles modifikacija http://www.astahost.com/info.php/sweet-titles_t11175.html
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
    }
    else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    EventCache.add(elm, evType, fn);
    return r;
    }
    else {
    elm['on' + evType] = fn;
    }
}
function getEventSrc(e) {
    if (!e) e = window.event;

    if (e.originalTarget)
    return e.originalTarget;
    else if (e.srcElement)
    return e.srcElement;
}

/*
function addLoadEvent(func) {
var oldonload = window.onload;
    if (typeof window.onload != 'function') {
    window.onload = func;
    } else {
    window.onload =
    function() {
    oldonload();
    func();
    }
    }
}*/
var EventCache = function(){
    var listEvents = [];
    return {
    listEvents : listEvents,
    
    add : function(node, sEventName, fHandler, bCapture){
        listEvents.push(arguments);
    },
    
    flush : function(){
        var i, item;
        for(i = listEvents.length - 1; i >= 0; i = i - 1){
        item = listEvents[i];
        
        if(item[0].removeEventListener){
            item[0].removeEventListener(item[1], item[2], item[3]);
        };
        
        /* From this point on we need the event names to be prefixed with 'on" */
        if(item[1].substring(0, 2) != "on"){
            item[1] = "on" + item[1];
        };
        
        if(item[0].detachEvent){
            item[0].detachEvent(item[1], item[2]);
        };
        
        item[0][item[1]] = null;
        };
    }
    };
}();


addEvent(window,'unload',EventCache.flush, false);





/*
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/
var toolTipLib = {
    xCord : 0,
    yCord : 0,
    obj : null,
    tipElements : ['img','a','span','area'],
    attachToolTipBehavior: function() {
    if ( !document.getElementById ||
        !document.createElement ||
        !document.getElementsByTagName ) {
        return;
    }
    var i,j;    
    if ( document.captureEvents ) { // FF
        addEvent(document,'mousemove',toolTipLib.updateXY,false); //iii mousemove
        document.captureEvents(Event.MOUSEMOVE);
    } else addEvent(document,'mouseover',toolTipLib.updateXY,false); // IE
    for ( i=0;i<toolTipLib.tipElements.length;i++ ) {
        var current = document.getElementsByTagName(toolTipLib.tipElements[i]);
        for ( j=0;j<current.length;j++ ) if (current[j].getAttribute('title') != null &&
current[j].getAttribute('title') != "") {
        addEvent(current[j],'mouseover',toolTipLib.tipOver,false);
        addEvent(current[j],'mouseout',toolTipLib.tipOut,false);
        current[j].setAttribute('tip',current[j].title);
        current[j].removeAttribute('title');
        current[j].removeAttribute('alt');
       }
    }
    },
    updateXY : function(e) {
    if ( document.captureEvents ) {
        toolTipLib.xCord = e.pageX;
        toolTipLib.yCord = e.pageY;
    } else if ( window.event.clientX ) {
        toolTipLib.xCord = window.event.clientX+document.documentElement.scrollLeft;
        toolTipLib.yCord = window.event.clientY+document.documentElement.scrollTop;
    }
    },
tipOut: function(e) {// OUT
    if ( window.tID ) {
        clearTimeout(tID);
    }
    if ( window.opacityID ) {
        clearTimeout(opacityID);
    }
    var l = getEventSrc(e);
    
    var div = document.getElementById('toolTip');
    if ( div ) {
        div.parentNode.removeChild(div);  
    }
},
    
checkNode : function(obj) { // CHECK
    var trueObj = obj;
    if ( trueObj.nodeName.toLowerCase() == 'a' || trueObj.nodeName.toLowerCase() == 'span' || trueObj.nodeName.toLowerCase() == 'img' || trueObj.nodeName.toLowerCase() == 'area') {
        return trueObj;
    } else {
        return trueObj.parentNode;
    }
    },
    tipOver : function(e) {   // OVER
    toolTipLib.obj = getEventSrc(e);
    tID = setTimeout("toolTipLib.tipShow()",50)
},
    
    

tipShow : function() {
    
    var scrX = Number(toolTipLib.xCord);
    var scrY = Number(toolTipLib.yCord);
    var tp = parseInt(scrY+15);
    var lt = parseInt(scrX+10);
    var anch = toolTipLib.checkNode(toolTipLib.obj);
    var addy = '';
    var access = '';

  if (anch.getAttribute('tip') == null) return;

    var div = document.getElementById('toolTip');
    if ( div ) {
        div.parentNode.removeChild(div);  
    }

    var newDiv = document.createElement('div');
    newDiv.id = 'toolTip';
    document.getElementsByTagName('body')[0].appendChild(newDiv);
    
      ac = anch.getAttribute('accessKey');
  			if (ac) {
  			 if (ac.indexOf('maps.google.com')>=0) {
  			   newDiv.style.width = '506px'; // static google maps tooltip			   
			     this.maxOpacity = 99;
			     ac = ac+ '&key=ABQIAAAAW3qEvYZ4aY8hZKGg7WtX1xQ1I3m53hC4g-nVEvJPsXkf_invGxRjGEaBXem-FoWEQpm9fF8nLOwpkA';			     
			     //ac = ac.replace(/amp/g,'');
			     access = '<center><img src=\'http://'+ac+'\'></center>';
         } else {
  			   newDiv.style.width = '180px';			   
  			   this.maxOpacity = 99;
  			   access = '<center><img src=\'/files/'+ac+'\'></center>';
  			  }
			} else {
			     this.maxOpacity = 90;  
            attr=anch.getAttribute('tip');          
            if (!attr || attr.length<50) newDiv.style.width = null; 
                else newDiv.style.width = '180px';
        }     
       
    if ((anch.nodeName.toLowerCase() == 'img' ) && !anch.getAttribute('tip')) return; 		
      
    
		if (anch.nodeName.toLowerCase() == 'a' ) {		  
      addy = '';
		}        
		
    newDiv.style.opacity = '.1';
    newDiv.innerHTML = "<p>"+anch.getAttribute('tip')+"<em>"+access+addy+"</em></p>";
    if ( parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(newDiv.offsetWidth+lt) ) {
        newDiv.style.left = parseInt(lt-(newDiv.offsetWidth+10))+'px';
    } else {
        newDiv.style.left = lt+'px';
    }
    if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(newDiv.offsetHeight+tp) ) {
        newDiv.style.top = parseInt(tp-(newDiv.offsetHeight+10))+'px';
    } else {
        newDiv.style.top = tp+'px';
    }
    toolTipLib.tipFade('toolTip',10);
},
    
    tipFade: function(div,opac) {
    var obj = document.getElementById(div);
    if (!obj) return;
    var passed = parseInt(opac);
    var newOpac = parseInt(passed+115);
    if ( newOpac < 80 ) {
        obj.style.opacity = '.'+newOpac;
        obj.style.filter = "alpha(opacity:"+newOpac+")";
        opacityID = setTimeout("toolTipLib.tipFade('toolTip','"+newOpac+"')",50);
    }
    else {
        obj.style.opacity = '.'+this.maxOpacity;
			  obj.style.filter = "alpha(opacity:"+this.maxOpacity+")";
    }
    }
};


addEvent(window,'load',toolTipLib.attachToolTipBehavior,false);








// Atsakyti JS
//http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp
/* $(document).ready(function () {  
	$('a[rel=atsakyti]').click(function () {
		var hash = this.href;    		
		hash = hash.substring(hash.indexOf('/comment/reply'));
		sp = hash.split('/');
    cnum = sp[4];   
    if (cnum==undefined) cnum='';    // bendras įrašas formoje
    
    form = '<br><div style="small"><form action="'+hash+'" method="post" id="comment_form"><label for="edit-comment"><b>Trumpas komentaras</b> &nbsp; &nbsp; &nbsp; &nbsp; Rašyti <a href="'+hash+'">išsamų </a>komentarą, <a href="'+hash+'?quote=1">cituoti </a></label><div class="form-item"><textarea cols="50" rows="4" name="edit[comment]" id="edit-comment-'+cnum+'" class="form-textarea required"></textarea></div><input name="edit[form_id]" id="edit-comment-form" value="comment_form" type="hidden"> <input name="op" value="Išsiųsti" class="form-submit" type="submit"></form></div>';
    
    
	 	$('#atsakyti-'+cnum).html(form); 	
		return false;
	});	
});
*/

//http://9lessons.blogspot.com/2009/06/submit-multiple-forms-jquery-ajax.html
$(document).ready(function()
{
$(".comment_button").click(function(){

var hash = this.href;    		
		hash = hash.substring(hash.indexOf('/comment/reply'));
		sp = hash.split('/');
    cnum = sp[4];   
    if (cnum==undefined) cnum='';    // bendras įrašas formoje
    
var label = '';
if (cnum) label = '<label for="edit-comment"><b>Trumpas atsakymas</b> &nbsp; &nbsp; &nbsp; &nbsp; Rašyti <a href="'+hash+'" title="Įdėt nuotrauką, formatuoti tekstą ..">išsamų </a>atsakymą, <a href="'+hash+'?quote=1">cituoti </a></label>';
form = '<form action="" method="post" onSubmit="return false;">'+label+'<div class="form-item"><textarea cols="50" rows="4" name="edit[comment]" id="textboxcontent'+cnum+'" class="form-textarea required"></textarea></div><input type="submit" value="Išsiųsti" onClick="submit_atsakymas(\''+sp[3]+'.'+cnum+'\')"></form>\n';

if ($("#atsakyti-"+cnum).html()=='') {
  $("#atsakyti-"+cnum).html(form);
  $("#atsakyti-"+cnum).hide();
}

$("#atsakyti-"+cnum).slideToggle(300);
$(this).toggleClass("active"); 

return false;});
});

function submit_atsakymas(num){
  num =  num.split('.');
  var cid = num[1];
  var txt = $("#textboxcontent"+cid).val();
  var dataString = 'txt='+txt.replace("/", "%2F").replace(/&/g, "%26").replace(/[+]/g, "%2B");
  
  if(txt=='') { alert("O kur komentaras? ;)"); }
  else {
    $("#atsakyti-"+cid).fadeIn(400).html('<br></br><br></br> &nbsp; &nbsp; &nbsp;<img src="/misc/ajax-loader.gif" align="absmiddle"> Luktelkit ..<br><br><br>');

  $.ajax({
  type: "POST",
  url: "/ajax_content/atsakyti/"+num[0]+"/"+cid,
  data: dataString,
  cache: false,
  success: function(html){
    $("#atsakyti-"+cid).html('<br>'+html);
    }
  });
}
  return false;
}

