//var NUMBER_OF_STARS = rating_count;
var star_width = 12;
var star_height = 12;
function debug()
{
  var ratings = document.getElementsByTagName('div');
   for (var i = 0; i < ratings.length; i++)
    {
      if (ratings[i].className != 'rating')
            continue;
      alert(ratings[i].firstChild.nodeValue);
    }
}
function getXMLValue(xml_response,tag_name)
  {
    var xml_element =  xml_response.getElementsByTagName(tag_name);
    var xml_value   =  xml_element.item(0).firstChild.data.toString();
    return xml_value;   
  }

function hide_div()
{
  var tmp_div = document.getElementsByTagName('div');
  var regex = new RegExp(name, "i");
  
  for (var i = 0; i < tmp_div.length; i++)
  {
    if (tmp_div[i].className=="temp") 
    {
      $(tmp_div[i].id).hide;
      Element.hide(tmp_div[i].id);
    }
  }
}

function init_rating()
{
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i < ratings.length; i++)
    {
        if (ratings[i].className != 'rating')
            continue;
            
        var rating = ratings[i].firstChild.nodeValue;
       
        ratings[i].removeChild(ratings[i].firstChild);
       
        if (rating > NUMBER_OF_STARS || rating < 0)
            continue;
        for (var j = 0; j < NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating >= 1)
            {
                star.setAttribute('src', image_path+'/rating_on.gif');
                star.setAttribute('width', star_width);
                star.setAttribute('height', star_height);
                star.className = 'on';
                rating--;
            }
            else if(rating == 0.5)
            {
                star.setAttribute('src', image_path+'/rating_half.gif');
                star.setAttribute('width', star_width);
                star.setAttribute('height', star_height);
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', image_path+'/rating_off.gif');
                star.setAttribute('width', star_width);
                star.setAttribute('height', star_height);
                star.className = 'off';
            }
            var widgetId = ratings[i].getAttribute('id').substr(7);
            
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            star.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
            star.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
            ratings[i].appendChild(star);
        } 
    }
}

function display_average_rating()
{
    var ratings = document.getElementsByTagName('div');
    for (var i = 0; i < ratings.length; i++)
    {
        if (ratings[i].className != 'average_rating')
            continue;
            
        var rating = ratings[i].firstChild.nodeValue;
       
        ratings[i].removeChild(ratings[i].firstChild);
       
        if (rating > NUMBER_OF_STARS || rating < 0)
            continue;
        for (var j = 0; j < NUMBER_OF_STARS; j++)
        {
            var star = document.createElement('img');
            if (rating >= 1)
            {
                star.setAttribute('src', image_path+'/rating_on.gif');
                star.setAttribute('width', star_width);
                star.setAttribute('height', star_height);
                star.className = 'on';
                rating--;
            }
            else if(rating == 0.5)
            {
                star.setAttribute('src', image_path+'/rating_half.gif');
                star.setAttribute('width', star_width);
                star.setAttribute('height', star_height);
                star.className = 'half';
                rating = 0;
            }
            else
            {
                star.setAttribute('src', image_path+'/rating_off.gif');
                star.setAttribute('width', star_width);
                star.setAttribute('height', star_height);
                star.className = 'off';
            }
            var widgetId = ratings[i].getAttribute('id').substr(7);
            
            star.setAttribute('id', 'star_'+widgetId+'_'+j);
            
            ratings[i].appendChild(star);
        } 
    }
}

function displayHover(ratingId, star)
{
    for (var i = 0; i <= star; i++)
    {
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', image_path+'/rating_over.gif');
    }
}

function displayNormal(ratingId, star)
{
    for (var i = 0; i <= star; i++)
    {
        var status = document.getElementById('star_'+ratingId+'_'+i).className;
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', image_path+'/rating_'+status+'.gif');
    }
}

function reload_div(current_id,new_rating)
{
   var ratings = document.getElementsByTagName('div');
   //alert('lenth = '+ ratings.length);
    for (var i = 0; i < ratings.length; i++)
    {
      //alert("hi " + i + 'length = '+ratings.length);
      
      //alert('article_id' + widgetId);
        if (ratings[i].className != 'rating')
            continue;
        
        var widgetId = ratings[i].getAttribute('id').substr(7);
        
        if(current_id != widgetId)
        {
        
        var ini_val = $("temp_"+widgetId).innerHTML;
        //alert(ini_val);
        $("rating_"+widgetId).innerHTML = ini_val;
        }  
        else
        {
          //$("rating_"+widgetId).innerHTML = new_rating;
        }
     }       
}

function submitRating(evt)
{
    var tmp = Event.element(evt).getAttribute('id').substr(5);
    var widgetId = tmp.substr(0, tmp.indexOf('_'));
    var starNbr = tmp.substr(tmp.indexOf('_')+1);
    
    var pagename = $('page').value;
    
    if(pagename == "video")
    {
    	filename = WEBSITE_URL + 'rate_video.php';
    }
    else
    {
    	filename = WEBSITE_URL + 'rate_article.php';
    }
    //alert(filename); 
    
    //window.location=filename+'?rating_id='+widgetId+'&rating_value='+starNbr;
    
    new Ajax.Request(filename, {
      method: 'post',
      parameters: {rating_id: widgetId, rating_value: starNbr},
      onSuccess: function(transport) {
         /*
         var text = transport.responseText;
         alert(text);*/
         var response = transport.responseXML;
         
         
         var new_date = getXMLValue(response,'date');
         var new_avg = getXMLValue(response,'avg-rating');
         var vote = getXMLValue(response,'vote');
         //alert(new_avg);
         var new_rating = parseInt(starNbr) + 1;
         
         $("rating_"+widgetId).innerHTML = new_avg;
         //$("rating_customer_date_"+widgetId).innerHTML = new_date;
         $("avg_"+widgetId).innerHTML = new_avg;
         //$("vote_"+widgetId).innerHTML = vote;
         reload_div(widgetId,new_rating);
         prototypeInit();
         $("rating_customer_value_" + widgetId).innerHTML = new_rating; 
         $("temp_"+widgetId).innerHTML = new_rating;       
      },
      onFailure:function()
      {
        alert("Ajax Failing");
      }  
      
      
    });
}

function prototypeInit()
{
   init_rating(); 
   display_average_rating();   
   hide_div(); 
    
    var rate = document.getElementsByClassName('rating');
    rate.each(function(n){
                        
                        n.immediateDescendants().each(function(c){
                             
                             Event.observe(c, 'click', submitRating);
                        });
                    });
  
    $('rarea').show();     
             
}

CSS = {
    // Adds a class to an element.
    AddClass: function (e, c) {
        if (!e.className.match(new RegExp("\\b" + c + "\\b", "i")))
            e.className += (e.className ? " " : "") + c;
    },

    // Removes a class from an element.
    RemoveClass: function (e, c) {
        e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
    }
};

// Functions for handling tabs.
Tabs = {
    // Changes to the tab with the specified ID.
    GoTo: function (contentId, skipReplace) {
        // This variable will be true if a tab for the specified
        // content ID was found.
        var foundTab = false;

        // Get the TOC element.
        var toc = document.getElementById("toc");
        if (toc) {
            var lis = toc.getElementsByTagName("li");
            for (var j = 0; j < lis.length; j++) {
                var li = lis[j];

                // Give the current tab link the class "current" and
                // remove the class from any other TOC links.
                var anchors = li.getElementsByTagName("a");
                for (var k = 0; k < anchors.length; k++) {
                    if (anchors[k].hash == "#" + contentId) {
                        CSS.AddClass(li, "current");
                        foundTab = true;
                        break;
                    } else {
                        CSS.RemoveClass(li, "current");
                    }
                }
            }
        }

        // Show the content with the specified ID.
        var divsToHide = [];
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if (div.className.match(/\bcontent\b/i)) {
                if (div.id == "_" + contentId)
                    div.style.display = "block";
                else
                    divsToHide.push(div);
            }
        }

        // Hide the other content boxes.
        for (var i = 0; i < divsToHide.length; i++)
            divsToHide[i].style.display = "none";

        // Change the address bar.
        //if (!skipReplace) window.location.replace("#" + contentId);
    },

    OnClickHandler: function (e) {
        // Stop the event (to stop it from scrolling or
        // making an entry in the history).
        if (!e) e = window.event;
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

        // Get the name of the anchor of the link that was clicked.
        Tabs.GoTo(this.hash.substring(1));
    },

    Init: function () {
        
        if (!document.getElementsByTagName) return;

        // Attach an onclick event to all the anchor links on the page.
        var anchordivs = document.getElementById("toc");
        
        var anchors = anchordivs.getElementsByTagName("a");
        for (var i = 0; i < anchors.length; i++) {
            var a = anchors[i];
            if (a.hash) a.onclick = Tabs.OnClickHandler;
        }

        var contentId;
        if (window.location.hash) contentId = window.location.hash.substring(1);

        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if (div.className.match(/\bcontent\b/i)) {
                if (!contentId) contentId = div.id;
                div.id = "_" + div.id;
            }
        }

        if (contentId) Tabs.GoTo(contentId, true);
    }
};

// Hook up the OnLoad event to the tab initialization function.
//window.onload = Tabs.Init;

// Hide the content while waiting for the onload event to trigger.
var contentId = window.location.hash || "#Introduction";

if (document.createStyleSheet) {
    var style = document.createStyleSheet();
    style.addRule("div.content", "display: none;");
    style.addRule("div" + contentId, "display: block;");
} else {
    var head = document.getElementsByTagName("head")[0];
    if (head) {
        var style = document.createElement("style");
        style.setAttribute("type", "text/css");
        style.appendChild(document.createTextNode("div.content { display: none; }"));
		style.appendChild(document.createTextNode("div" + contentId + " { display: block; }"));
        head.appendChild(style);
    }
}


