﻿// JScript File 

var RatingManager = Class.create({
initialize:function()
{
//Event.observe($(window), 'load', function(){$$('div[gswidget=hprater]').each(function(s){this.CreateRater(s)}.bind(this));}.bindAsEventListener(this));
},
InitializeRaters:function(el, userRating)
{
    $(el).select('div[gswidget=hprater]').each(function(s) { this.CreateRater(s, userRating) } .bind(this));
},
CreateRater: function(el, userRating)
{
    new HPRatingContainer(el, userRating);
}
})
var HPRatingManager = new RatingManager();

var HPRatingContainer = Class.create({
initialize: function(el, userRating)
{
this.el = $(el);
this.el.observe('mouseout', this.CancelRating.bindAsEventListener(this));

if(this.el.readAttribute('gswparams'))
{
this.options = this.el.readAttribute('gswparams').evalJSON();
}
this.options = Object.extend(

    {
        userRating: userRating,
        starImages: [{ 'text': 'Not sure', 'value': 0, 'imgUrl': 'App_Themes/Av7/images/all-lang/outfit_page/norate_uh_.gif' }, { 'text': 'Like it', 'value': 1, 'imgUrl': 'App_Themes/Av7/images/all-lang/outfit_page/cool_uh_.gif' }, { 'text': 'Need it', 'value': 2, 'imgUrl': 'App_Themes/Av7/images/all-lang/outfit_page/likeIt_uh_.gif' }, { 'text': 'LOVE IT!', 'value': 3, 'imgUrl': 'App_Themes/Av7/images/all-lang/outfit_page/loveIt_uh_.gif'}]
    }, 
    this.options || {});
    
// clear the container and create an array to hold the images
this.el.update();
// then add in all the stars
this.options.starImages.each(function(s, index){this.CreateStar(s, index)}.bind(this));
this.popContent = new Element('div', {'class':'starPopUpTxt'});
this.popUp = new Element('div', {'class':'starPopUp'}).hide().update(this.popContent);
this.popUp.insert({'top':new Element('div',{'class':'starPopUpLeftEnd'})});
this.popUp.insert({'bottom':new Element('div',{'class':'starPopUpRightEnd'})});
this.el.insert({'after':this.popUp});   
this.SetUserRating();
},
CreateStar:function(s, index)
{
s.imgEl = new Element('img', {'src':absoluteRoot + s.imgUrl});
s.imgEl.observe('mouseover', this.MouseOverStar.bindAsEventListener(this, s));
s.imgEl.observe('mouseout', this.CancelRating.bindAsEventListener(this));
s.imgEl.observe('click', this.RecordUserRating.bindAsEventListener(this, s));
this.el.insert({'bottom':(new Element('div', {'class':'starWrap' + index})).update(s.imgEl)});
},
MouseOverStar:function(event, star)
{
this.isOut = false;
this.popContent.update(star.text)
this.popUp.show();
this.SetUserRating(star.value);
},
SetUserRating:function(tempRating)
{
tempRating = tempRating==undefined?this.options.userRating:tempRating;
this.options.starImages.each(function(s){this.SetStar(s, (s.value <= tempRating));}.bind(this));
},
SetStar:function(element, isHighlighted)
{
    if(isHighlighted)
    {
        element.imgEl.src = element.imgEl.src.sub('_uh_', '_h_');
    }
    else
    {
        element.imgEl.src = element.imgEl.src.sub('_h_', '_uh_');
    }
},
CancelRating:function()
{
this.isOut = true;
this.EndRating.bind(this).delay(0.05);
},
EndRating:function()
{
if(this.isOut)
{
var cumOff = this.el.cumulativeOffset();
this.SetUserRating();
this.popUp.hide();
}
},
RecordUserRating:function(event, star)
{
this.options.userRating = star.value;
RateSubmit(star.value);

}
})