var MLDDM_CLASS		= 'mlddm';		
var obj_menu		= new Array();	
function mlddminit()
{
	
	var candidates = document.getElementsByTagName('ul');
	var index = 0;
	
	for(var i=0; i < candidates.length; i++)
	{
		
		if(candidates[i].className == MLDDM_CLASS)
		{
			
			candidates[i].style.visibility = 'visible';
			
			var obj = candidates[i];
			var value = obj.getAttribute('params');
		
			
			obj_menu[index] = new menu(obj, index, value);
			index++;
		}
	}
}
function menu(obj, obj_n, params)	
{
	
	var _handler			= obj;			
	var _obj_num			= obj_n;		
	var _me					= this;			
	var _buttons			= new Array();	
											
	this._layers			= new Array(6);	
	this._layers[0]			= new Array();	
	this._layers[1]			= new Array();	
	this._layers[2]			= new Array();	
	this._layers[3]			= new Array();	
	this._layers[4]			= new Array();	
	this._layers[5]			= new Array();	
											
	var _closetimer			= null;			
	var _mouseout			= true;			
	var _currentlayer		= null;			
	
	var _shiftx				= 0;			
	var _shifty				= 0;			
	var _timeout			= 500;			
	var _effect				= 'none';		
	var _effect_speed		= 300;			
	var _orientation		= 'h';			
	
	var params_array;
	if(params)
	{
		params_array = params.split(",");
		if(params_array[0]) _shiftx       = params_array[0]*1;	
		if(params_array[1]) _shifty       = params_array[1]*1;
		if(params_array[2]) _timeout      = params_array[2]*1;
		if(params_array[3]) _effect	   = params_array[3];
		if(params_array[4]) _effect_speed = params_array[4]*1;
		if(params_array[5]) _orientation  = params_array[5];
		
		if(!_effect_speed) _effect_speed = 1000;
	}
	
	
	function opacity(num, opacStart, opacEnd, millisec)
	{
		
		var speed = Math.round(1000/millisec);
		var timer = 0;
		
		
		if(opacStart > opacEnd)
		{
			for(i = opacStart; i >= opacEnd; i=i-4)
			{
				setTimeout("changeOpac(" + _obj_num + "," + num + "," + i + ")", (timer * speed));
				timer++;
			}
		}
		else if(opacStart < opacEnd)
		{
			for(i = opacStart; i <= opacEnd; i=i+4)
			{
				setTimeout("changeOpac(" + _obj_num + "," + num + "," + i + ")", (timer * speed));
				timer++;
			}
		}
	}
	
	function slide(num, direction, millisec)
	{
		
		var speed = Math.round(1000/millisec);
		var timer = 0;
		
		if(_orientation == 'h')	_ori = 0;
		else					_ori = 1;
		
		if(direction == 'show')
		{
			for(i = 0; i <= 100; i=i+2)
			{
				setTimeout("changePOS(" + _obj_num + "," + num + "," + i + "," + _ori + ")", (timer * speed));
				timer++;
			}
		}
		else if(direction == 'hide')
		{
			for(i = 100; i >= 0; i=i-2)
			{
				setTimeout("changePOS(" + _obj_num + "," + num + "," + i + "," + _ori + ")", (timer * speed));
				timer++;
			}
		}
	}
	
	
	function getlevel(layer)
	{
		var level = 0;
		var currentobj = layer;
		while(currentobj.className != MLDDM_CLASS)
		{
			if(currentobj.tagName == 'UL') level++;
			currentobj = currentobj.parentNode;
		}
		
		return level;
	}
	
	
	function getbutton(layer)
	{
		var button;
		var currobj = layer;
		var index = 0;
		while(currobj.className != MLDDM_CLASS)
		{	
			if(currobj.tagName == 'LI')
			{
				index++;
				button = currobj;
			}
			currobj = currobj.parentNode;
		}
		return button;
	}
	
	
	function button_on(layer)
	{
		
		if(getlevel(layer) !=1) return -1;
		var button = getbutton(layer);
		if(button)
		{
			button = button.getElementsByTagName("a")[0];
			button.id = 'buttonhover';
			
			
		}
	}
	
	function button_off(layer)
	{
		
		if(getlevel(layer) !=1) return -1;
		var button = getbutton(layer);
		if(button)
		{
			
								  
			button = button.getElementsByTagName("a")[0];
			
				button.id = 'buttonnohover';
		}
	}
	
	
	function mopen(index)
	{
		if(!_me._layers[1][index])						
		{
			if(_effect == 'fade')
				opacity(index, 0, 100, _effect_speed);	
			else if(_effect == 'slide')
				slide(index, 'show', _effect_speed);	
			else
				_me._layers[0][index].style.visibility = 'visible';
			button_on(_me._layers[0][index]);			
			_me._layers[1][index] = true;				
		}
	}
	
	function mclose(index)
	{
		if(_me._layers[1][index])						
		{
			if(_effect == 'fade')
				opacity(index, 100, 0, _effect_speed);	
			else if(_effect == 'slide')
				slide(index, 'hide', _effect_speed);	
			else
				_me._layers[0][index].style.visibility = 'hidden';
			
			button_off(_me._layers[0][index]);			
			_me._layers[1][index] = false;				
		}
	}
	
	function closeall()
	{
		for(var i=0; i < _me._layers[0].length; i++) { mclose(i); }
	}
	
	function mclosetime()
	{	
		_closetimer = window.setTimeout(closeall, _timeout);
	}
	
	
	function mcancelclosetime()
	{
		if(_closetimer) { window.clearTimeout(_closetimer); _closetimer = null;	}
	}
	
	function getlayerindex(obj)
	{
		for(var i=0; i < _me._layers[0].length; i++)
		{
			if(_me._layers[0][i] == obj) return i;
		}
		return -1;
	}
	
	this.pcloseall = function() { closeall(); };
	
	this.eventover = function()
	{
		if(_mouseout)					
		{	
			_mouseout = false;			
			mcancelclosetime();			
			var currentli = this;		
			
			
			var layer = currentli.getElementsByTagName("ul")[0];
			var ind = getlayerindex(layer);
			if(ind >= 0) mopen(ind);
			
			
			var open_layers = new Array();
				
				
				open_layers[0] = currentli.getElementsByTagName("ul")[0];
				if(!open_layers[0]) open_layers[0] = 0;
				
				
				var currobj = currentli.parentNode;
				var num = 0;
				while(currobj.className != MLDDM_CLASS)
				{
					if(currobj.tagName == 'UL')
					{
						num++;
						open_layers[num] = currobj;
					}
					currobj = currobj.parentNode;
				}
			
			
			var layers_to_hide = new Array(_me._layers[0].length);
			
			
			for(var i=0; i < layers_to_hide.length; i++)
				layers_to_hide[i] = false;
			
			
			for(var i=0; i < open_layers.length; i++)
				layers_to_hide[getlayerindex(open_layers[i])] = true;
			
			
			for(var i=0; i < layers_to_hide.length; i++)
				if(!layers_to_hide[i] && (_currentlayer != open_layers[0])) mclose(i);
			
			
			_currentlayer = open_layers[1];
		}
	};
	this.eventout = function() { _mouseout = true; };	
	this.allout = function()   { mclosetime(); };		
	this.allover = function()  { mcancelclosetime(); };
	
	
	
	
	
	
	
	var current = _handler.getElementsByTagName("li")[0];
	var i = 0;
	while(current)
	{
		_buttons[i] = current;
		current = node_after(current);
		i++;
	}
	
	
	var count = 0;
	var all_li = _handler.getElementsByTagName("li");
	for(var i = 0; i < all_li.length; i++)
	{
		
		var layer = all_li[i].getElementsByTagName("ul")[0];
		if(layer)
		{	
			this._layers[0][count] = layer;	
			this._layers[1][count] = false;	
			count++;
		}
		
		
		all_li[i].onmouseover = this.eventover;
		all_li[i].onmouseout  = this.eventout;
	}
	_handler.onmouseout  = this.allout;		
	_handler.onmouseover = this.allover;	
	
	var layer_quantity = this._layers[0].length;	
	for(var number = 0; number < layer_quantity; number++)
	{
		
		this._layers[2][number] = getlevel(this._layers[0][number]);
		
		this._layers[3][number] = this._layers[0][number].offsetWidth;
		this._layers[4][number] = this._layers[0][number].offsetHeight;
		
		var obj = this._layers[0][number];
		var top = obj.offsetTop;
			obj.style.marginTop = 0+'px';
		var margintop = top - obj.offsetTop;
			obj.style.marginTop = margintop+'px';
			this._layers[5][number] = margintop;
	}
	
	
	for(index = 0; index < this._layers[0].length; index++)
	{
		
		var level = 0;
		var currentobj = this._layers[0][index];
		while(currentobj.className != MLDDM_CLASS)
		{
			if(currentobj.tagName == 'UL') level++;
			currentobj = currentobj.parentNode;
		}
		
		
		if((_orientation == 'h' && level > 1) || (_orientation == 'v'))
		{
			var parent_ul;	
			var parent_a;
			var curr = this._layers[0][index].parentNode;
			
			
			while(curr.tagName != 'UL')
			{	
				if(curr.parentNode) curr = curr.parentNode;
				else				return 1;
			}
			parent_ul = curr;
			
			var curr = this._layers[0][index].parentNode;
			while(curr.tagName != 'LI')
			{
				if(curr.parentNode)
					curr = curr.parentNode;
				else
					return 1;
			}
			parent_a = curr.getElementsByTagName("a")[0];
			
			
			
			var a_tags = new Array();
			var i = 0;
			curr = parent_ul.getElementsByTagName("li")[0];	
			
			while(curr)
			{
				if(curr.getElementsByTagName("a")[0]){
					
					a_tags[i] = curr.getElementsByTagName("a")[0];
					
				}
				curr = node_after(curr);
				if(curr) if(curr.tagName == 'LI') i++;
			}
			
			var ablock_width = a_tags[1].offsetWidth;
			var ablock_height = 0;
			var num = 0;			
			for(var i = 0; i < a_tags.length; i++)
			{
				if(a_tags[i] != parent_a)
				{
					ablock_height = ablock_height + a_tags[i].offsetHeight;
					num++;
				}
				else break;
			}
			
			this._layers[0][index].style.left = ablock_width  + _shiftx + 'px';
			this._layers[0][index].style.top  = ablock_height + _shifty + 'px';
		}
	}
}






function changeOpac(obj_num, layer_num, opacity)
{
	var object = obj_menu[obj_num];
	var layer  = object._layers[0][layer_num];
	layer.style.opacity			= (opacity / 100);
	layer.style.MozOpacity		= (opacity / 100);
	layer.style.KhtmlOpacity	= (opacity / 100);
	layer.style.filter			= "alpha(opacity="+opacity+")";
	if(opacity > 98) layer.style.filter = 'none';			
	if(opacity  > 0) layer.style.visibility='visible';
	if(opacity <= 0) layer.style.visibility='hidden';
}
function changePOS(obj_num, layer_num, pos, ori)
{
	var object		= obj_menu[obj_num];
	var layer		= object._layers[0][layer_num];
	var level		= object._layers[2][layer_num];
	var width		= object._layers[3][layer_num];
	var height		= object._layers[4][layer_num];
	var margintop	= object._layers[5][layer_num];
	
	if(level == 1 && ori == 0)
	{
		var h = height - pos*height/100;
		
		layer.style.clip	  = 'rect('+h+'px 1000px 1000px 0px)';
		layer.style.marginTop = -h+margintop+'px';
	}
	else
	{
		var w = width - pos*width/100;
		
		layer.style.clip	  = 'rect(0px 1000px 1000px '+w+'px)';
		layer.style.marginLeft = -w+'px';
	}
	if(pos <= 0) layer.style.visibility = 'hidden';
	if(pos  > 0) layer.style.visibility = 'visible';
	
}
function close() { for(var i=0; i < obj_menu.length; i++) { obj_menu[i].pcloseall(); } }
document.onclick = close;
function is_all_ws(nod)
{
  return !(/[^\t\n\r ]/.test(nod.data));
}
function is_ignorable(nod)
{
  return ( nod.nodeType == 8) ||
         ((nod.nodeType == 3) && is_all_ws(nod) );
}
function node_after(sib)
{
	while((sib = sib.nextSibling)) { if(!is_ignorable(sib)) return sib; }
	return null;
}
_LOADERS = Array();
function callAllLoaders()
{
	var i, loaderFunc;
	for(i=0; i<_LOADERS.length; i++)
	{
		loaderFunc = _LOADERS[i];
		if(loaderFunc != callAllLoaders) loaderFunc();
	}
}
function appendLoader(loaderFunc)
{
	if(window.onload && window.onload != callAllLoaders) _LOADERS[_LOADERS.length] = window.onload;
	window.onload = callAllLoaders;
	_LOADERS[_LOADERS.length] = loaderFunc;
}
appendLoader(mlddminit);




			//user = SocialHistory();
  			//var visitsDigg = user.doesVisit("Digg");
  			//var visitsSlashdot = user.doesVisit("Slashdot");
  			//var listOfVisitedSites = user.visitedSites();
			
			
			//if(hasLinkBeenVisited(button.getAttribute('href')))
				//button.style.color = "#555";
			//else


			/*
			moreSites =
			{
  	  			"Aza": [curr.getElementsByTagName("a")[0].href]
  			};
			user = SocialHistory( moreSites );
			throw( user.doesVisit("Aza") );
			*/



/*
 * Social Limit - Only the social you care about.
 *
 * Enables your site to know which social bookmarking badges to display to your
 * visitors. It tells you all social sites the user has gone to, or you can 
 * query for a specific one.
 * 
 * For example:
 * 
 *    var sl = SocialHistory();
 *    alert( sl.doesVisit("Digg") ); // Returns true/false, -1 if unknown.
 *    var listOfVisitedSites = sl.visitedSites();
 *    var checkedSites = sl.checkedSites();
 *
 * If you want to add more sites to check, you can pass that in as a dictionary
 * to History:
 *
 *    var more = { "Humanized": "http://humanized.com",
 *                 "Azarask.in": ["http://azarask.in", "http://azarask.in/blog"]
 *               };
 *    var sl = SocialHistory(more);
 *    alert( sl.doesVisit("Humanized") );
 *
 * For a list of built-in sites, see the sites variable below.
 *
 * Copyright (c) 2008 Aza Raskin (http://azarask.in/blog)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

var SocialHistory = function( moreSites ){

  var sites = {
    "Digg": ["http://digg.com", "http://digg.com/login"],
    "Reddit": ["http://reddit.com", "http://reddit.com/new/", "http://reddit.com/controversial/", "http://reddit.com/top/", "http://reddit.com/r/reddit.com/", "http://reddit.com/r/programming/"],
    "StumbleUpon": ["http://stumbleupon.com"],
    "Yahoo Buzz": ["http://buzz.yahoo.com"],
    "Facebook": ["http://facebook.com/home.php", "http://facebook.com", "https://login.facebook.com/login.php"],
    "Del.icio.us": ["https://secure.del.icio.us/login", "http://del.icio.us/"],
    "MySpace": ["http://www.myspace.com/"],
    "Technorati": ["http://www.technorati.com"],
    "Newsvine": ["https://www.newsvine.com", "https://www.newsvine.com/_tools/user/login"],
    "Songza": ["http://songza.com"],
    "Slashdot": ["http://slashdot.org/"],
    "Ma.gnolia": ["http://ma.gnolia.com/"],
    "Blinklist": ["http://www.blinklist.com"],
    "Furl": ["http://furl.net", "http://furl.net/members/login"],
    "Mister Wong": ["http://www.mister-wong.com"],
    "Current": ["http://current.com", "http://current.com/login.html"],
    "Menaeme": ["http://meneame.net", "http://meneame.net/login.php"],
    "Oknotizie": ["http://oknotizie.alice.it", "http://oknotizie.alice.it/login.html.php"],
    "Diigo": ["http://www.diigo.com/", "https://secure.diigo.com/sign-in"],
    "Funp": ["http://funp.com", "http://funp.com/account/loginpage.php"],
    "Blogmarks": ["http://blogmarks.net"],
    "Yahoo Bookmarks": ["http://bookmarks.yahoo.com"],
    "Xanga": ["http://xanga.com"],
    "Blogger": ["http://blogger.com"],
    "Last.fm": ["http://www.last.fm/", "https://www.last.fm/login/"],
    "N4G": ["http://www.n4g.com"],
    "Faves": ["http://faves.com", "http://faves.com/home", "https://secure.faves.com/signIn"],
    "Simpy": ["http://www.simpy.com", "http://www.simpy.com/login"],
    "Yigg": ["http://www.yigg.de"],
    "Kirtsy": ["http://www.kirtsy.com", "http://www.kirtsy.com/login.php"],
    "Fark": ["http://www.fark.com", "http://cgi.fark.com/cgi/fark/users.pl?self=1"],
    "Mixx": ["https://www.mixx.com/login/dual", "http://www.mixx.com"],
    "Google Bookmarks": ["http://www.google.com/bookmarks", "http://www.google.com/ig/add?moduleurl=bookmarks.xml&hl=en"],
    "Subbmitt": ["http://subbmitt.com/"]
  };
  
  for( var site in moreSites ) {
    // If we don't have the site, create the URL list.
    if( typeof( sites[site] ) == "undefined" ) sites[site] = [];
    
    // If the value is string, just push that onto the URL list.
    if( typeof( moreSites[site] ) == "string" )
      sites[site].push( moreSites[site] );
    else
      sites[site] = sites[site].concat( moreSites[site] );
  }
  
  var visited = {};

  function getStyle(el, scopeDoc,styleProp) {
    if (el.currentStyle)
      var y = el.currentStyle[styleProp];
    else if (window.getComputedStyle)
      var y = scopeDoc.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
    return y;
  }
  
  function remove( el ) {
    el.parentNode.removeChild( el );
  }
  
  // Code inspired by:
  // bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes
  function createIframe() {
    var iframe = document.createElement("iframe");
    iframe.style.position = "absolute";
    iframe.style.visibility = "hidden";

    document.body.appendChild(iframe);

    // Firefox, Opera
    if(iframe.contentDocument) iframe.doc = iframe.contentDocument;
    // Internet Explorer
    else if(iframe.contentWindow) iframe.doc = iframe.contentWindow.document;

    // Magic: Force creation of the body (which is null by default in IE).
    // Also force the styles of visited/not-visted links.
    iframe.doc.open();
  	iframe.doc.write('<style>');
  	iframe.doc.write("a{color: #000000; display:none;}");  	
  	iframe.doc.write("a:visited {color: #FF0000; display:inline;}");  	
  	iframe.doc.write('</style>');
    iframe.doc.close();
    
    // Return the iframe: iframe.doc contains the iframe.
    return iframe;
  }  

  var iframe = createIframe();
  
  function embedLinkInIframe( href, text ) {
    var a = iframe.doc.createElement("a");
    a.href = href;
    a.innerHTML = site;
    iframe.doc.body.appendChild( a );
  }
  
  for( var site in sites ) {
    var urls = sites[site];
    for( var i=0; i<urls.length; i++ ) {
      // You have to create elements in the scope of the iframe for IE.
      embedLinkInIframe( urls[i], site );
      
      // Automatically try variations of the URLS with and without the "www"
      if( urls[i].match(/www\./) ){
        var sansWWW = urls[i].replace(/www\./, "");
        embedLinkInIframe( sansWWW, site );
      } else {
        // 2 = 1 for length of string + 1 for slice offset
        var httpLen = urls[i].indexOf("//") + 2;
        var withWWW = urls[i].substring(0, httpLen ) + "www." + urls[i].substring( httpLen );
        embedLinkInIframe( withWWW, site );
      }
      
    }
  }
    
  var links = iframe.doc.body.childNodes;
  for( var i=0; i<links.length; i++) {
    // Handle both Firefox/Safari, and IE (respectively)
    var displayValue = getStyle(links[i], iframe.doc, "display");
    var didVisit = displayValue != "none";
      
    if( didVisit ){
      visited[ links[i].innerHTML ] = true;
    }
  }
  
  remove( iframe );
  
  return new (function(){
    var usedSites = [];
    for( var site in visited ){
      usedSites.push( site );
    }
    
    // Return an array of visited sites.
    this.visitedSites = function() {
      return usedSites;
    }
    
    // Return true/false. If we didn't check the site, return -1.
    this.doesVisit = function( site ) {
      if( typeof( sites[site] ) == "undefined" )
        return -1;
      return typeof( visited[site] ) != "undefined";
    }
    
    var checkedSites = [];
    for( var site in sites ){
      checkedSites.push( site );
    }
    // Return a list of the sites checked.
    this.checkedSites = function(){
      return checkedSites;
    }
  })();
}