// stam

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 getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}


var debug = false;
var StarryDefaults = {
		sprite: "/img/stars01.gif",
		width: 30,
		height: 30,
		startAt: 0,
		maxLength: 5,
		multiplier: 1,
		showNull: true,
		align: 'left',
		feedback: false
};

var Starry = Class.create();
Starry.prototype = {
	initialize: function(element) {
		this.element = $(element);
		this.element.className = "starry";				
		this.options = {};
			Object.extend(this.options, StarryDefaults);
		Object.extend(this.options, arguments[1] || {});		
		this.name = this.options.name || "starry" + id.next();
		this.element.style.height = this.options.height + 'px';		
		if(debug) console.log(this.options.showNull);
		// lets build our array with an extra one for null
		this.children = new Array(this.options.maxLength + 1);
		if(debug) console.log('children length '+ this.children.length);		
		this.hidden = document.createElement("input");
		this.hidden.type = "hidden";
		this.hidden.name = this.name;
		this.element.appendChild(this.hidden);				
			for(i=0; i < this.children.length; i++) {
			this.children[i] = new SingleStar(this, i);
			this.element.appendChild(this.children[i].element);
		}
		if( this.options.feedback ) {
			this.feedback = document.createElement('div');
			this.feedback.className = "feedback";
			this.feedback.style.cssFloat = this.options.align;
			this.element.appendChild(this.feedback);
		}	
	
		this.selected = this.options.startAt;
		this.reset(this.selected);
	},
	set: function(index) {
		if(debug) console.log('set index '+ index);
	
		for(var i=1; i < this.children.length; i++)
			this.children[i].element.style.backgroundPosition = (i <= index) ? "0 -" + this.options.height * 2 + "px" : "0 0";
		if(this.options.feedback) 
			this.feedback.innerHTML = this.children[index].value;
		this.selected = index;
			this.hidden.value = this.children[index].value;
		if(debug) console.log('set value ' + this.hidden.value);
	},
	show: function(index) {
		if(debug) console.log('show index '+ index);		
				for(var i=1; i < this.children.length; i++)
			this.children[i].element.style.backgroundPosition = (i <= index) ? "0 -" + this.options.height + "px" : "0 0";
		if(this.options.feedback)
			this.feedback.innerHTML = this.children[index].value;	
	},
	reset: function() { this.set(this.selected); },
	clear: function() {
		for(var i=1; i < this.children.length; i++)
			this.children[i].element.style.backgroundPosition = "0 0";
	}
};


var SingleStar = Class.create();
SingleStar.prototype = {
	initialize: function(parent, index) {
		this.parent = parent;
		this.index = index;
		if(debug) console.log('Creating star at index '+ this.index);
		this.value = this.index * this.parent.options.multiplier;
		if(debug) console.log('value '+ this.value);
		this.element = document.createElement("div");
		this.element.style.width = this.parent.options.width + "px";
		this.element.style.height = this.parent.options.height + "px";
		this.element.style.backgroundImage = "url(" + this.parent.options.sprite + ")";
		this.element.style.backgroundPosition = (this.index == 0) ? "0 -" + this.parent.options.height * 3  +"px" : "0 0";
		this.element.className = "standard_star";
		this.element.style.cssFloat = this.parent.options.align;
		if(!this.parent.options.showNull && this.index == 0) this.element.style.display = "none";
		this.element.onclick = function() { this.parent.set(this.index);UpDateRating(this.index);}.bind(this);
		this.element.onmouseover = function() { this.parent.show(this.index); }.bind(this);
		this.element.onmouseout = this.parent.reset.bind(this.parent);
		if(debug) console.log('set onclick handler');
	}
};

var id = { start: 0, prev: 0, next: function() { return this.start + this.prev++; } };

function FireStarry () {new Starry('StarryRating',  {showNull:false, startAt:3});}


function UpDateRating(rating) {p= 'm=' + rating + '&b=' + $('StarryVideoID').innerHTML;
createCookie('video' + $('StarryVideoID').innerHTML,rating,2);	
var r = new Ajax.Request( 'StarryVideoRating.php', {method: 'post', parameters: p, onComplete:function (t){
$RattingArray = t.responseText.split('<*>');
$('StarryRating').innerHTML ='';
$('starryupdate').innerHTML= '<div><b>Всего голосов:</b> '+ $RattingArray[1] +', <b>оценка:</b> '+ $RattingArray[0] +'</div><br>';																									
																										}} );}

