/*
   Class that defines a standard ratings component.
*/

// Messages for display above the individual ratings units. The length of the
// array also defines how many units to include in the component.
yg_Ratings.Msgs = new Array
(
   "Ужасно",
   "Слабо",
   "Средно",
   "Добро",
   "Отлично"
);

yg_Ratings.Imgs = new Array
(
   "star_redone16_2.gif",
   "star_redtwo16_2.gif",
   "star_redthree16_2.gif",
   "star_redfour16_2.gif",
   "star_redfive16_2.gif"
);

// Path for all images.
var path = "http://www.savindustry.com/demo/hotelbg/images/raiting/";

// Image for set units.
yg_Ratings.UnitY = path + "star_red16.gif";

// Image for set units <= the mouse over point.
yg_Ratings.UnitYMouseOver = path + "star_g_r16.gif";

// Image for set units > the mouse over point.
yg_Ratings.UnitYMouseLess = path + "star_brdr_red16.gif";

// Image for unset units.
yg_Ratings.UnitN = path + "star_brdr_g16.gif";

// Image for unset units <= the mouse over point.
yg_Ratings.UnitNMouseOver = path + "star_g16.gif";
yg_Ratings.DefaultMsg = "&nbsp;";
yg_Ratings.FormAction = "";
yg_Ratings.FormStr = "";

function yg_Ratings(id, button, inputname, defaultval, nadpis)
{
   // The id parameter is the name (a string) of the variable to which the
   // instance is assigned. (The variable is sent along to event handlers,
   // so it must be in the global scope.)
   
   // id and inputname must be different or IE hates you


   var i, t;
   var attributes;
   var h1, h2;
   var d = document;

   this.showbutton = button;

   if (defaultval < 1 || defaultval > 5) {
	defaultval = 0;
   }
   this.rating = defaultval;


   attributes = 'class="ygrtngs" id="' + id + '"';
   h1 = 'onMouseOut="return yg_Ratings_mouseOut(' + id + ');"';

   if(defaultval){
	d.write('<input type="hidden" name="'+inputname+'" id="input_'+id+'" value="' + defaultval + '" />');
   } else {
	d.write('<input type="hidden" name="'+inputname+'" id="input_'+id+'" />');    
   }
   this.input = document.getElementById('input_'+id);

   d.write('<div ' + attributes + ' ' + h1 + '>');
   if (defaultval > 0) {
       d.write('<div class="msg" id="'+id+'_msg">'+yg_Ratings.Msgs[defaultval-1]+'</div>');
   } else {
       d.write('<div class="msg" id="'+id+'_msg">'+yg_Ratings.DefaultMsg+'</div>');
   }
   if (this.showbutton) { 
      d.write('<span><strong>'+nadpis+'</strong></span>');
   }


   for (i = 1; i <= yg_Ratings.Msgs.length; i++)
   {
      h1 = 'onMouseOver="return yg_Ratings_mouseOver(' + id + ', ' + i + ');"';
      h2 = 'onClick="return yg_Ratings_click(' + id + ', ' + i + ');"';
      d.write('<span class="unit "' + h1 + ' ' + h2 + '>');
      if (i <= defaultval) {
         d.write('<img src="' + yg_Ratings.UnitY + '" />');
      } else {
         d.write('<img src="' + yg_Ratings.UnitN + '" />');
      }
      d.write('</span>');
   }
   d.write('<input type="hidden"  value="" />');
   d.write('</div>');
   
   this.parent = document.getElementById(id);
   this.form = document.forms[0];
   this.images = this.parent.getElementsByTagName("img");
   this.msg = document.getElementById(id + '_msg');
   this.id = id;

   var children = this.msg.childNodes;
   var node;

   for (var i = 0; i <  children.length; i++)
   {
      node = children[i];

      if (node.nodeType == 3)
      {
            this.DefaultMsg = node.nodeValue;
      }
   } 

}

function yg_Ratings_set(n, oflag)
{
   // The n parameter is the unit to set (starting at 1). Set oflag to true
   // when the mouse is outside of the ratings component, or you're not sure.
   if (arguments.length < 2)
      oflag = true;
   
   this.rating = n;
   this.DefaultMsg = yg_Ratings.Msgs[n-1];
   if (this.showbutton) {
	   this.showBtn(n);
   }
   this.update(n, oflag);
   yg_Ratings.FormStr = this.setForm();
}

function yg_Ratings_setMsg(m)
{
   var children = this.msg.childNodes;
   var node;

   for (var i = 0; i <  children.length; i++)
   {
      node = children[i];

      if (node.nodeType == 3)
      {
            node.nodeValue = m;
      }
   } 
}

function yg_Ratings_setForm()
{
    var str="";
    for (var i=0;i<this.form.elements.length;i++)
    {
        if (this.form.elements[i].name!='')
        {
            var name = this.form.elements[i].name;
            str += (str == '') ? '' : '&'
            str += name + '=' + escape(this.form.elements[i].value);
        }
    }
    return str;
}

function yg_Ratings_get()
{
   return this.rating;
}

function yg_Rating_showBtn(n)
{
    this.btn = document.getElementById('btnSave');
    this.btn.style.display="block";
}

function yg_Ratings_update(n, oflag)
{
   // The oflag parameter is true when the mouse is outside of the ratings
   // component. The n parameter is the 
   if (oflag)
      this.setMsg(this.DefaultMsg);
   else
      this.setMsg(yg_Ratings.Msgs[n - 1]);
      
   if (n == this.rating)
      this.input.setAttribute("value", n);


   for (i = 1; i <= yg_Ratings.Msgs.length; i++)
   {
      if (oflag)
      {
         if (i <= this.rating)
            this.images[i - 1].src = yg_Ratings.UnitY;
         else
            this.images[i - 1].src = yg_Ratings.UnitN;
      }
      else
      {
         if (i <= n)
         {
            if (i <= this.rating)
               this.images[i - 1].src = yg_Ratings.UnitYMouseOver;
            else
               this.images[i - 1].src = yg_Ratings.UnitNMouseOver;
         }
         else
         {
            if (i <= this.rating)
               this.images[i - 1].src = yg_Ratings.UnitYMouseLess;
            else
               this.images[i - 1].src = yg_Ratings.UnitN;
         }
      }
   }

   return true;
}

function yg_Ratings_XmlHttp()
{
var xmlHttp=null;
    if(document.all&&document.documentElement)
    {
        var obj=(document.all&&navigator.userAgent.toLowerCase().indexOf("msie 5")!=-1)?"Microsoft.XMLHTTP":"Msxml2.XMLHTTP";
        try
        {
            xmlHttp=new ActiveXObject(obj);
        }
        catch(e)
        {
            xmlHttp=false;
        }
    }
    else
    {
        xmlHttp=new XMLHttpRequest();
    }
return xmlHttp;
}

function yg_Ratings_send(sUrl)
{
    this.parent.innerHTML="<span>Your Rating</span><img src="+path+yg_Ratings.Imgs[this.rating-1]+">";
    this.parent.onmouseout=null;
    
//    var xmlHttp = yg_Ratings_XmlHttp();
//    xmlHttp.open("POST",sUrl, false);
//    xmlHttp.send(yg_Ratings.FormStr);
}

function yg_Ratings_submit()
{
   yg_Ratings_send(yg_Ratings.FormAction);
}

function yg_Ratings_click(obj, n)
{
   obj.set(n, false);
   return true;
}

function yg_Ratings_mouseOver(obj, n)
{
   obj.update(n, false);
   return true;
}

function yg_Ratings_mouseOut(obj)
{
   obj.update(0, true);
   return true;
}


yg_Ratings.prototype.set = yg_Ratings_set;
yg_Ratings.prototype.setMsg = yg_Ratings_setMsg;
yg_Ratings.prototype.get = yg_Ratings_get;
yg_Ratings.prototype.setForm = yg_Ratings_setForm;
yg_Ratings.prototype.showBtn = yg_Rating_showBtn;
yg_Ratings.prototype.update = yg_Ratings_update;
