function LinksWidget()
{
	this.input = null;
	this.linksRef = null;
	var self = this;
	var aList = new Array();
	var inputTags = new Array();

	this.generateLinks = function()
	{
		this.input = _e('tags');
		this.linksRef = _e('links');
		this.linksAllRef = _e('links_all');

		// get list elements and create A list
		for(li = this.linksRef.firstChild; li != null; li = li.nextSibling)
		{
			if(li.nodeName == 'LI')
			{
				for(a = li.firstChild; a != null; a = a.nextSibling)
				{
					if(a.nodeName == 'A')
					{ 
						this.attachEvents(a);
						aList.push(a);
					}
				}
			}
		}
		for(li = this.linksAllRef.firstChild; li != null; li = li.nextSibling)
		{
			if(li.nodeName == 'LI')
			{
				for(a = li.firstChild; a != null; a = a.nextSibling)
				{
					if(a.nodeName == 'A')
					{ 
						this.attachEvents(a);
						aList.push(a);
					}
				}
			}
		}
	}
	
	this.attachEvents = function(aNode)
	{
		if(document.attachEvent)
		{
			aNode.attachEvent("onmousedown",this.onMouseDown);
		}
		else if(document.addEventListener)
		{
			aNode.addEventListener("mousedown",this.onMouseDown,false);
		}		
	}

	this.onMouseDown = function(e)
	{
		e = e || window.event;
		t = getTargetObj(e).target;	
		
		var className = t.getAttribute('class') || t.getAttribute('className'); // FF or IE
		
		if(String(className).search(/selected/gi) != -1)
		{
			t.setAttribute('className',t.getAttribute('className').replace(/ selected/gi,''));// for IE
			t.setAttribute('class',t.getAttribute('class').replace(/ selected/gi,''));	// for FF
			
			self.updateInput(t,false);	
		}
		else
		{
			t.setAttribute('className',t.getAttribute('className') + ' selected');
			t.setAttribute('class',t.getAttribute('class') + ' selected');	
			
			self.updateInput(t,true);		
		}
	}
	
	this.updateInput = function(aNode,set)
	{
		var text = aNode.innerHTML;
		var found = false;
		var foundIndex = 0;

		for(var i = 0; i < inputTags.length; i++)
		{
			if(inputTags[i] == text)
			{
				found = true;
				foundIndex = i;
				break;
			}
		}
		
		if(set)
		{
			if(!found)
			{
				inputTags.push(text);
			}
		}
		else
		{
			if(found)
			{
				inputTags.splice(i,1);
			}
		} 
	
		this.input.value = inputTags.join(',');
	}
}

function init()
{
	Links.generateLinks();
}

var Links = new LinksWidget();



function trace(object)
{
	try 
	{
		console.log(object);
	}
	catch (e)
	{
		if (_e('konsole_output'))
		{
			_e('konsole_output').innerHTML+="<hr>"+object;
		}
	}
}

function _e(id)
{
	return document.getElementById(id);
}

function _b()
{
	return window.document.body;
}

function getTargetObj(ev)
{
	e = ev || window.event;
	
	var tmpT = new Object();
	tmpT.target = e.target ? e.target : e.srcElement;
	tmpT.x = e.screenX;
	tmpT.y = e.screenY;

	return tmpT;
}

  var req;

  var result;
  var call;
  var ur;	

	function check_login(u)
	{
		ur=u;
		//get form informations first
		url=ur+"index.php?action=login|get_login_info";
		retrieveURL(url,"get_login_info");
	}
	function show_hide(id1, id2)
	{
		obj1=document.getElementById(id1);
		obj2=document.getElementById(id2);		
		obj1.style.display="block";
		obj2.style.display="none";
//		obj1.style.visibility="visible";
//		obj2.style.visibility="hidden";

	}
	
	function try_login(u)
	{
		ur=u;
		//get form informations first
		username=document.forms['login'].username.value;
		password=document.forms['login'].password.value;
		url=ur+"index.php?page=login&action=log_ajax&username="+username+"&password="+password;
		retrieveURL(url,"log_ajax");
	}


 

  function retrieveURL(url,c) {

	call=c;
    if (window.XMLHttpRequest) { // Non-IE browsers

      req = new XMLHttpRequest();

      req.onreadystatechange = processStateChange;

      try {

        req.open("GET", url, true);

      } catch (e) {

        //alert(e);

      }

      req.send(null);

    } else if (window.ActiveXObject) { // IE

      req = new ActiveXObject("Microsoft.XMLHTTP");

      if (req) {

        req.onreadystatechange = processStateChange;

        req.open("GET", url, true);

        req.send();
      }

    }
	
  }

 
  function processStateChange() 
  {

     if (req.readyState == 4) 
	 { // Complete
	  result=req.responseText;
	if(call=="get_login_info")
	  {
		  if(result=="no")
		  {
			  show_hide("not_log","log");
		  }
		  else
		  {
			show_hide("log","not_log");
			document.getElementById("username_id").innerHTML = '<a href="'+ur+'utilizator/'+result+'/" title="Profilul utilizatorului '+result+'">'+result+'</a>';
		  }
		  
	  }
	  
	  if(call=="log_ajax")
	  {
		  if(result=="ok")
		  {
			  show_hide("log","not_log");
			  check_login(ur);
		  }
		  else
		  {
			document.getElementById("mesaj_eroare").innerHTML = req.responseText;
		  }
	  }
     }

  }

function changeSearchDisplay()
{
	ref=document.getElementById('searchZone2');
	disp=document.getElementById('to_display');	
	
	refImg=document.getElementById('search_more');	
	if(ref.style.display=='block')
	{
		ref.style.display='none';
		refImg.src=URL+"images/search_more.jpg";
		disp.value="none";
	}
	else
	{
		ref.style.display="block";
		refImg.src=URL+"images/search_more_up.jpg";
		disp.value="block";		
	}
}

function changeAddLinkDisplay()
{
	ref=document.getElementById('optionalField');
	disp=document.getElementById('to_display');	
	
	refImg=document.getElementById('search_more');	
	if(ref.style.display=='block')
	{
		ref.style.display='none';
		refImg.src=URL+"images/search_more.jpg";
		disp.value="none";
	}
	else
	{
		ref.style.display="block";
		refImg.src=URL+"images/search_more_up.jpg";
		disp.value="block";		
	}
}


function changeAddTagDisplay(x)
{
	ref=document.getElementById('aplicaTagZone');
	if(x==1)
	{
		ref.style.display='none';
		return true;
	}
	ref.style.display='block';
	return true;
}

function changeAddCommentDisplay(x)
{
	ref=document.getElementById('aplicaComZone');
	if(x==1)
	{
		ref.style.display='none';
		return true;
	}
	ref.style.display='block';
	return true;
}

function changeRecomDisplay(x)
{
	ref=document.getElementById('recomZone');
	if(x==1)
	{
		ref.style.display='none';
		return true;
	}
	ref.style.display='block';
	return true;
}

function changeRegisterDisplay()
{
	ref=document.getElementById('optionalField');
	disp=document.getElementById('to_display');	
	
	refImg=document.getElementById('search_more');	
	if(ref.style.display=='block')
	{
		ref.style.display='none';
		refImg.src=URL+"images/search_more.jpg";
		disp.value="none";
	}
	else
	{
		ref.style.display="block";
		refImg.src=URL+"images/search_more_up.jpg";
		disp.value="block";		
	}
}


function trigger_rate(id, rate)
{
	RatingController.getInstance().applyRate(id, rate);
}