
var img_path="http://hmlab.info/minor_dev/gj-pack/images/";
var gj_path="http://goodjob.herokugarden.com/";

function message_gj(url,title,id)
{
	document.write('<a href="' + gj_path + 'articles/message?accesskey='+id+'&permlink='+encodeURIComponent(url) +'&title='+encodeURIComponent(title) + '&TB_iframe=true&width=460&height=600" class="thickbox">send message</a>')
}
function callback_gj(jsonp)
{

	var target = document.getElementById(encodeURI(jsonp['article']['permlink']));
	if(target.hasChildNodes())
	{
		target.removeChild(target.lastChild); 

	}
	var star = document.createElement('span'); 
	star.id= encodeURI(jsonp['article']['permlink'])+'_star';

	star.innerHTML = '';
	var count = jsonp['article']["gjcount"]
	var handred = 0;
	var ten = 0;
	var one = 0;
	
	if(count <= 9)
	{
		one = count;
	}
	else if(count > 9 && count < 100)
	{
		ten = Math.floor(count / 10);
		one = count % 10;

	}
	else if(count >= 100)
	{
		handred =  Math.floor(count / 100);
		ten =  Math.floor((count - handred) / 10);
		one = count - handred - ten;
	}

	for(var i=0;i<handred;i++)
	{
		star.innerHTML += '<img src="' + img_path + "mark100.gif" + '">';
	}
	for(var i=0;i<ten;i++)
	{
		star.innerHTML += '<img src="' + img_path + "mark10.gif" + '">';
	}

	for(var i=0;i<one;i++)
	{
		star.innerHTML += '<img src="' + img_path + "mark1.gif" + '">';
	}

	target.appendChild(star);
}


function count_gj(url,id)
{
	document.write('<span id ="'+ encodeURI(url)+ '">' + '</span>');
	var target = document.getElementById(encodeURI(url));
	var ele = document.createElement('span'); 
	ele.id= encodeURI(url)+'_star';
	
	ele.innerHTML="<img src='" + img_path + "loading-img.gif" + "'>";
	
	target.appendChild(ele);


	$.ajax({
	    url :gj_path + 'articles/count?permlink='+encodeURIComponent(url)+'&accesskey='+id,
	    dataType : "jsonp",
	    timeout : 10000,
	    success : function(json){
	        callback_gj(json);
	    },
	    error : function(){
	        alert('error');
	    }
	});

}

function increment_gj(url,title,id)
{
	var target = document.getElementById(encodeURI(url));
	if(target.hasChildNodes())
	{
		target.removeChild(target.lastChild); 
	}
	var star = document.createElement('span'); 
	star.innerHTML="<img src='" + img_path + "loading-img.gif" + "'>";
	
	target.appendChild(star);
	 
	$.ajax({
	    url :gj_path +'articles/increment/?permlink=' + encodeURIComponent(url) + '&accesskey='+id+'&title='+encodeURIComponent(title),
	    dataType : "jsonp",
	    timeout : 10000,
	    success : function(json){
	        callback_gj(json);
	    },
	    error : function(){
	        alert('error');
	    }
	});

}
