/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 250}, options || {});
  
  this.each(function() {
    
    var root = this, zIndex = 1000;
	var hoverTimerId;
	var hoverOutTimerId;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
		
		var subnav = getSubnav(this);
		if (!subnav) return;
		clearTimeout(hoverTimerId);
		//writeStuff("Hide: "+hoverTimerId+"  ");
		$.data(subnav, 'cancelHide', false);
		hoverOutTimerId = setTimeout(function() {
			if (!$.data(subnav, 'cancelHide')) {
				$(subnav).slideUp(options.speed);
				if (this.nodeName != null){
					if (this.nodeName.toLowerCase() == 'ul') {
						var li = getActuator(this);
						$(li).removeClass('expanded');
						$('> a', li).removeClass('expanded');
					}
				}
			}
		}, 300);
    }
  
    function show() {
		var subnav = getSubnav(this);
	 	if (!subnav) {	return;	}
		//writeStuff("Show pries: "+hoverTimerId+"  ");
		//sustabdo submeniu susitraukima, jei vel uzeinama ant meniu
		//clearTimeout(hoverOutTimerId);
		//sutraukia meniu
		clearTimeout( hoverTimerId );		
		hoverTimerId = setTimeout( function() {			
			//writeStuff("Show po: "+hoverTimerId+"  ");
			$.data(subnav, 'cancelHide', true);
			$(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
			
			if (this.nodeName != null){
				if (this.nodeName.toLowerCase() == 'ul') {
					var li = getActuator(this);
					$(li).addClass('expanded');
					$('> a', li).addClass('expanded');
				}
			}
			
		}, 500);
		
    }
    
	$('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('expanded'); $('> a', this).addClass('expanded'); },
      function() { $(this).removeClass('expanded'); $('> a', this).removeClass('expanded'); }
    );
    
  });
  
};
$('#langs a.cur').click(function() {
        $('#langs .other').toggle();
        $('#langs').toggleClass('selected');
        return false;
    });
	
function writeStuff(strToWrite) {
    var myDiv = document.getElementById("div1");
    myDiv.innerHTML = "<h4>"+ myDiv.textContent  + strToWrite + "</h4>";
}
