var modes= new Array();
modes['narrow'] = new Array()
modes['narrow']['image_start']="/HTML/typo3conf/ext/naksci_feedback/js/thumbs.png";
modes['narrow']['image_done']='/HTML/typo3conf/ext/naksci_feedback/js/thumbs-green.png';
modes['narrow']['yoffset']='42px';
modes['narrow']['width']='50px';
modes['narrow']['votes']='';
modes['narrow']['text-size']='12px';
modes['narrow']['line']='I like';
modes['narrow']['height']='61px';

modes['wide-small'] = new Array()
modes['wide-small']['image_start']="/HTML/typo3conf/ext/naksci_feedback/js/thumbs-wide2.png";
modes['wide-small']['image_done']='/HTML/typo3conf/ext/naksci_feedback/js/thumbs-wide-green2.png';
modes['wide-small']['yoffset']='26px';
modes['wide-small']['width']='70px';
modes['wide-small']['height']='40px';
modes['wide-small']['votes']=' votes';
modes['wide-small']['text-size']='10px';
modes['wide-small']['line']='I like this';

modes['wide'] = new Array();
modes['wide']['image_start']="/HTML/typo3conf/ext/naksci_feedback/js/thumbs-wide.png";
modes['wide']['image_done']='/HTML/typo3conf/ext/naksci_feedback/js/thumbs-wide-green.png';
modes['wide']['yoffset']='41px';
modes['wide']['width']='105px';
modes['wide']['votes']=' votes';
modes['wide']['text-size']='13px';
modes['wide']['line']='I like this';
modes['wide']['height']='61px';

 $(document).ready(function() {
   var preload= new Image();
   var dat;
   // generate markup
   $(".rating").each(function(){
		nam = $(this).attr('name')+'';
		if(!is_array(modes[nam]))
			nam='wide-small';
		dat = modes[nam];
   		preload.src = dat['image_done'];
		$.post("/HTML/typo3conf/ext/naksci_feedback/js/rate.php", {id: $(this).attr('id'), view: 1},function(xml) {
       // format and output result
       			if(!in_cookie($("name", xml).text())){
				 $("#"+$("name", xml).empty());
       				$("#"+$("name", xml).text()).append(
	"<img style='border:none;position:absolute;top:0px;left:0px;margin:0px' src='"+dat['image_done']+"'><div class='rating-text' style='position:absolute;top:"+dat['yoffset']+";width:100%; text-align:center; font-weight:bold;'>"+$("count", xml).text()+dat['votes']+"</div>");
			}else{
				$("#"+$("name", xml).text()).empty();
				$("#"+$("name", xml).text()).append(
"<a href='#' style='text-decoration:none;color:black'><img style='border:none;position:absolute;top:0px;left:0px;margin:0px' src='"+dat['image_start']+"'><div class='rating-text' style='position:absolute; top:"+dat['yoffset']+"; width:100%; text-align:center' >"+dat['line']+"</div></a> ");
   			// add markup to container and apply click handlers to anchors
   				$(".rating a").click(function(e){
   			  // stop normal link click
     				e.preventDefault();

     				// send request
     				$.post("/HTML/typo3conf/ext/naksci_feedback/js/rate.php", {rating: 1, id: $(this).parent().attr('id')}, function(xml) {
       				// format and output result
       					$("#"+$("name", xml).text()).html(
						"<img style='border:none;position:absolute;top:0px;left:0px;margin:0px' src='"+dat['image_done']+"'><div class='rating-text' style='position:absolute; top:"+dat['yoffset']+"; width:100%; text-align:center; font-weight:bold;'>"+$("count", xml).text()+dat['votes']+"</div>");
       					add_cookie($("name", xml).text());
     				});
   			});

			}
                            });
	});
   $(".rating").css('position','relative');
   $(".rating").css('width',dat['width']);
   $(".rating").css('height',dat['height']);
   $(".rating").css('font-family','verdana');
   $(".rating").css('font-size',dat['text-size']);
   $(".rating").css('font-weight','bold');
   $(".rating").css('margin-right','13px');
   $(".rating").css('margin-bottom','6px');
   $(".rating-text").css('top',dat['yoffset']);

 });

function add_cookie(id){
	cookie=readCookie('naksci_thumbs');
	if(is_array(cookie)){
		cooks=cookie.split(',');
	}else{
		cooks=new Array();
	}
	out=','+id+',';
	var c=0;
	for(var i in cooks){
		c=c+1;
		if(i.length()>0 && c<21){
			out=out+i+',';
		}
	}
	createCookie('naksci_thumbs',out,7);
}

function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}

function in_cookie(id){
	var cook;
	cook=readCookie('naksci_thumbs');
	if(cook!== null && cook.indexOf(','+id+',')>-1)
		return false;
	else
		return true;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


