var utilities = {
	is_hidden : function ( element )
	{
		element = $(element);
		
		if ( !element )
			return false;
		else
			if ( element.style.display == 'none' || element.style.visibility == 'hidden' )
				return -1;
			else
				return false;
	},
	
	set_attribs : function ( element , attribs )
	{
		element = $(element);
		
		for (var x in attribs)
			element[x] = attribs[x];
	},
	
	set_styles : function ( element , styles )
	{
		element = $(element);
		
		for (var x in styles) {
			try { element.style[x] = styles[x]; }
			catch (e) {}
		}
	}
}

Element.addMethods(utilities);

var vote = {
	star_count : 5,
	img_file_name : 'vote_star.gif',
	bg_file_name : 'vote_star_heat.png',
	margin_top : 20,
	margin_bottom : 0,
	margin_left : 'auto',
	margin_right : 'auto',
	img_width : 20,
	img_height : 20,
	saver_file_name : 'vote.php',
	stars : null,
	location : null,
	voter : null,
	point : null,
	allow_vote : true,
	is_msie : (document.all && !window.opera),
	is_safari : navigator.userAgent.indexOf("Safari") != -1,
	
	build_stars : function ()
	{
		var percent = 100/((this.img_width*this.star_count));

		var length = parseFloat((this.point/this.voter)/percent);
		
		var stars = new Object();
		
		stars.container = this.create_elm( 'div' , {} , { position : 'relative' , height : (this.img_height+'px') , width : ((this.img_width*this.star_count)+'px') , marginLeft : (this.margin_left) , marginRight : (this.margin_right) , marginTop : (this.margin_top+'px') , marginBottom : (this.margin_bottom+'px') , background : '#ebf4fd' } , $(this.location) );
		
		stars.bg = this.create_elm( 'div' , {} , { top : '0px;' , left : '0px' , position : 'absolute' , width : (length+'px') , height : (this.img_height+'px') , background : ('url(images/'+this.bg_file_name+') top left no-repeat') } , $(stars.container) );
		
		if ( this.has_png_support )
			for ( var i = 0; i < this.star_count; i++ )
				var img = this.create_elm( 'img' , { src : ('images/'+this.img_file_name) } , { position : 'relative' } , $(stars.container) );
		
		return stars;
	},
	
	rebuild_degree : function ( )
	{
		var percent = 100/((vote.img_width*vote.star_count));
		
		var length = ( vote.voter != 0 ) ? parseFloat((vote.point/vote.voter)/percent) : 0;
		
		$(vote.stars.bg).set_styles({width: (length+'px')});
		
		$(vote.stars.container).set_styles({backgroundColor : '#ebf4fd'});
	},
	
	alert_on_faliure : function (  )
	{
		alert('Oyunuzun kaydedilmesi esnasında bir hata meydana geldi.\nLütfen kısa bir süre sonra tekrar deneyiniz.')
	},
	
	print_vote_results : function ( transport )
	{
		switch (transport.responseText)
		{
			case 'vote_found': alert('Bu ders kaydına daha önce oy verdiniz.'); break;
			case 'please_login': alert('Bu ders kaydına oy verebilmek için lütfen oturum açın veya üye değilseniz ücretsiz üye olun!'); break;
			default:
				var data = eval(transport.responseText);
				
				vote.voter = data[0];
				vote.point = data[1];
				
				vote.rebuild_degree();
		}
	},
	
	init : function ( tutorial_id , voter , point , location )
	{
		this.location = location;
		this.voter = voter;
		this.point = point;

		var ie_version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		
		this.has_png_support = !this.is_msie || (this.is_msie && ie_version >= 7);

		this.stars = this.build_stars();
		
		var left_pos = this.stars.container.viewportOffset()[0];
		
		if ( this.allow_vote )
		{
			if ( this.has_png_support )
				this.stars.container.set_styles({cursor : 'pointer'});
			
			Event.observe( this.stars.container, 'mousemove', function(e)
				{
					var length = Event.pointerX(e) - left_pos - 3;
					
					$(vote.stars.container).set_styles({backgroundColor : '#f9f9f9'});
					$(vote.stars.bg).set_styles({width: (length+'px')});
				}
			);
	
			Event.observe( this.stars.container, 'mouseout', vote.rebuild_degree );
	
			Event.observe( this.stars.container, 'click', function(e)
				{
					var percent = 100/((vote.img_width*vote.star_count));
					
					var point = (Event.pointerX(e) - left_pos) * percent;
	
					new Ajax.Request(('ajax/'+vote.saver_file_name+'?tutorial_id='+tutorial_id+'&point='+point), {
						method: 'get' ,
						onFailure: vote.alert_on_faliure,
						onSuccess: vote.print_vote_results });
				}
			);
		}
	},
	
	create_elm : function ( tag , attribs , styles , parent )
	{
		var el = document.createElement(tag);
		
		if ( attribs )
			$(el).set_attribs(attribs);
		
		if ( styles )
			$(el).set_styles(styles);
		
		if ( parent )
			parent.appendChild(el);	
		
		return el;
	}
}