

//Clearing field if the text is present
function clearField(field, fieldstring)
{	
	if(field.value == fieldstring){
		field.value = "";		
	}
}

//Filling field if nothing has changed
function fillField(field, fieldstring)
{
	if(field.value == "" || field.value == null){
		field.value = fieldstring;		
	}
}

function CreateBookmarkLink() {
	title = "PronLinks.com - We make your life easier"; 
	url = "http://www.pronlinks.com";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	}else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
	}else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

function getBookmark(){
	if(/chrome/.test( navigator.userAgent.toLowerCase() )){
		document.write('Bookmark (CTRL+D)');
	}else if (window.external) {
  		document.write('<a onclick="javascript:CreateBookmarkLink()" class="whitelink" style="cursor:pointer;">Add to Favorites</a>'); 
	} else if (window.sidebar) {
  		document.write('<a onclick="javascript:CreateBookmarkLink()" class="whitelink" style="cursor:pointer;">Bookmark Page</a>'); 
	} else if (window.opera && window.print) {	
   		document.write('<a onclick="javascript:CreateBookmarkLink()" class="whitelink" style="cursor:pointer;">Add Bookmark</a>');
	} 
}

function linkNavigate(id){
	window.open('/dir/link/'+id+'/');
}

//Info Square
var square, myTimer, currentEvent;

function popup(id, event){
	currentEvent = event;
	myTimer=setTimeout("popup2("+id+")", 300);
}

function popup2(id){	
	var event = currentEvent; 
	var params = $.toJSON({"ID": id});
	
	if (document.all) {
       	xMouse = window.event.x+document.body.scrollLeft;
        yMouse = window.event.y+document.body.scrollTop;
    } else {
        xMouse = event.pageX;
        yMouse = event.pageY;
    }
	res = getCached("linkInfo", id);
	//Check first if object is stored locally
	if(!res){
		
		setLoader("infoSquare", "on");
		$.post('/webservice/webservice.php', {method: "getLinkDescription", param: params}, function(res){
			setLoader("infoSquare", "off");
			var dt = new Date();
			while ((new Date()) - dt <= 50) { /* Do nothing */ }
			
			if (document.all) {
				xMouse2 = window.event.x+document.body.scrollLeft;
				yMouse2 = window.event.y+document.body.scrollTop;
			} else {
				xMouse2 = event.pageX;
				yMouse2 = event.pageY;
			}
			
			if(xMouse != xMouse2 || yMouse != yMouse2){	kill(); return;	}
			
			var res = $.evalJSON(res);
			ajaxCacher("linkInfo", id, res);
			
			if(res['success']){
				$('#infoSquare')[0].innerHTML=res["desc"];
				$('#dummyBox')[0].innerHTML=res["desc"];
			}else{
				$('#infoSquare')[0].innerHTML="Something went wrong, please contact us.";
			}
				
			Xoffset=-120;
			Yoffset=-40;
		
			boxHeight = $('#dummyBox').height();
		
			Yoffset= Yoffset - boxHeight;
		
			x = xMouse + Xoffset;
			y = yMouse + Yoffset;
		
			$('#infoSquare')[0].style.left=x+"px";
			$('#infoSquare')[0].style.top=y+"px";
		
			$('#infoSquare').width(0);
			$('#infoSquare').height(0);
		
			$('#infoSquare').animate({'width':300,'height':boxHeight},200, function(){$('#infoSquare').show();});
		});
	
	}else{
		if(res['success']){
			$('#infoSquare')[0].innerHTML=res["desc"];
			$('#dummyBox')[0].innerHTML=res["desc"];
		}else{
			$('#infoSquare')[0].innerHTML="Something went wrong, please contact us.";
		}
			
		Xoffset=-120;
		Yoffset=-40;
	
		boxHeight = $('#dummyBox').height();
	
		Yoffset= Yoffset - boxHeight;
	
		x = xMouse + Xoffset;
		y = yMouse + Yoffset;
	
		$('#infoSquare')[0].style.left=x+"px";
		$('#infoSquare')[0].style.top=y+"px";
	
		$('#infoSquare').width(0);
		$('#infoSquare').height(0);
	
		$('#infoSquare').animate({'width':300,'height':boxHeight},200, function(){$('#infoSquare').show();});	
	}
}

function kill(){
	clearTimeout(myTimer);
	$('#infoSquare').hide();
	$('#infoSquare')[0].innerHTML="";
}

function clear_field(fieldname, fieldstring)
{
	var field = document.getElementById(fieldname);
	
	if(field.value == fieldstring){
		field.value = "";		
	}
}

$(document).ready(function(){
  $(".whitelink").hover(

	function () {

	  $(this).animate({

	  color: "#999999"

	}, 300 );

	},

	function () {

	  $(this).animate({

	  color: "#eeeeee"

	}, 300 );      }

  );

});



function getGeneric(nr, name, pic_path, avg_vote, id, can_vote, rounded, url){
	
	var can_vote_string = "";
	
	if(can_vote){
		can_vote_string = "onMouseDown=\"star.update(event,this, "+id+")\" onMouseMove=\"star.mouse(event,this)\" title=\"Rate This!\"";
	}
	
	document.write(getIdDiv(nr), getNameDiv(name, url, id) + getIconDiv(id, pic_path) + getReportDiv(id) + getVoteDiv(avg_vote, can_vote_string, id, rounded));
	
}

function getIdDiv(nr){
	if(nr == 0){nr ="";}
	return "<div class=\"dataId\">"+nr+"</div>";
}

function getNameDiv(name, url, id){
	return "<div class=\"dataName\"><a href=\""+url+"\" onclick=\"linkNavigate("+id+"); return false;\" class=\"nolink\">"+name+"</a></div>";
}

function getIconDiv(id, pic_path){
	desc = "onmouseover=\'popup("+id+", event)\' onmouseout=\"kill()\"";

	if(pic_path == ""){
		pic_path = "style=\"background-image:url(/pics/na.jpg);\"";
	}else{
		pic_path = "style=\"background-image:url(/pics/link/"+pic_path+"/icon.jpg)\"";	
	}
	
	return "<div class=\"dataIcon\" "+pic_path + desc+" onclick=\"linkNavigate("+id+"); return false;\"><!-- --></div>";	
}

function getVoteDiv(avg_vote, can_vote_string, id, rounded){
	return "<div class=\"dataVote\" id=\"votenr" + id + "\"><div id=\"star\"><ul id=\"star" + id + "\" class=\"star\" " + can_vote_string + "><li id=\"starCur" + id + "\" class=\"curr\" title=\"" + avg_vote + "\" style=\"width: " + rounded + "px;\" ></li></ul><div id=\"saver" + id + "\" title=\"" + avg_vote + "\"></div></div></div>";
}

function getReportDiv(id){
	return "<div class=\"dataStatus\" id=\"status" + id + "\" onclick=\"getReportSquare(" + id + ", event)\" TITLE=\"Report as broken\"><img src=\"/pics/status_normal.png\" /></div>";
}

function setLoader(status, changeDiv){
	
	var loader = $('#'+changeDiv)[0];
	
	if(loader != null){
		if(status == 'on'){
				loader.innerHTML="<center><IMG SRC='/pics/loader.gif' /></center>";	
		}else if(status == 'off'){
				loader.innerHTML="";
		}
	}
}

