// JavaScript Document

var vote_stars = new Array();

var star1= new Image(); 
var star2= new Image(); 
  star1.src="star.gif"; 
  star2.src="star2.gif"; 

function StarVote( aid, selected, prefix ){
  this.prefix = prefix;
  this.aid = aid;
  this.selected = selected;
}

function vote_over( id, prefix){
//  alert(id);
  for(var i=0; i< vote_stars.length; i++){
//  window.alert(vote_stars[i].aid);
      var star = document.getElementById( 'star' + prefix + vote_stars[i].aid );
      if ( star ) {
        if ( vote_stars[i].aid <= id ) {
          star.src = "star2.gif";
        }else{
          star.src = "star.gif";
        }
      }
  }
}

function vote_out( id, prefix){
  for(var i=0; i< vote_stars.length; i++){
       var star = document.getElementById( 'star' + prefix + vote_stars[i].aid );
        if ( star ) {
          if ( vote_stars[i].selected  ) {
             star.src = "star2.gif";
          }else{
             star.src = "star.gif";
          }
      }
  }
}


//   *****  in dollalbum.html function call replaced by new_vote_doll due to failure in call on 5mar by md


function vote_doll( doll_id, vote ){

  sendRequestTextGet('ajax_rate_doll.html', 'doll_id=' + doll_id + '&rating=' + vote);
  
  // now update the rating on the page

  var rating = parseFloat(document.getElementById('number_rating_' + doll_id).innerHTML) * 1.0;
  var num_votes = parseInt(document.getElementById('number_votes_' + doll_id).innerHTML);

  var new_rating = ((rating * num_votes) + vote) / (num_votes * 1.0 + 1);

//  window.alert(rating + ' x ' + num_votes)
  
  new_rating = Math.round(new_rating * 100) / 100;


  num_votes++;

  document.getElementById('number_rating_' + doll_id).innerHTML = new_rating;
  document.getElementById('number_votes_' + doll_id).innerHTML = num_votes;

  for(var i=0; i< vote_stars.length; i++){
    if (i < vote) {
      vote_stars[i].selected = true;     
    }
    else {
      vote_stars[i].selected = false;      
    }
  }

  vote_out(vote, '_' + doll_id + '_');  
 // return;
}

// ******* Created 2 functions 5 mar @auther: md
// *****  replaced vote_doll function

function new_vote_doll( doll_id, vote){
//alert("new vote");

  var rating = parseFloat(document.getElementById('number_rating_' + doll_id).innerHTML) * 1.0;
  var num_votes = parseInt(document.getElementById('number_votes_' + doll_id).innerHTML);
  
  var user_vote=parseFloat(document.getElementById('user_votes_' + doll_id).innerHTML);

   if(user_vote>0){
     rating = (rating * num_votes) - user_vote;
     num_votes--; }
 
// window.alert('pre rate calcu : '+rating + ' x ' + num_votes)   

      var new_rating = rating + vote;
      num_votes++;
      new_rating = new_rating / num_votes;
      
      new_rating = Math.round(new_rating * 100) / 100;
      user_vote = vote;

  //num_votes++;

  document.getElementById('number_rating_' + doll_id).innerHTML = new_rating;
  document.getElementById('number_votes_' + doll_id).innerHTML = num_votes;
  document.getElementById('user_votes_' + doll_id).innerHTML = user_vote;

  for(var i=0; i< vote_stars.length; i++){
     if(vote_stars[i].aid <= new_rating){
      vote_stars[i].selected = true;
     }
    else {
      vote_stars[i].selected = false;      
    }

}

 vote_out(new_rating, '_' + doll_id + '_');  
  return;
}

// *****  to Excute ajax_rate_doll.html for updating doll and doll_rating table.
// ***** Assuming that in doll_rating table only one entry per user per doll_id
// ***** @auther md

function update_doll_rating(doll_id, vote)
{
       request = new Ajax.Request(
            'ajax_rate_doll.html', {
                method : 'get',
                parameters : '?doll_id='+doll_id+'&rating=' + vote,

                onCreate : function () {
                  //  alert('starting ajax rate request');
                },

                onSuccess : function () {
                   // alert('the change made it to the backend!');
                    new_vote_doll(doll_id, vote);
                },

                onFailure : function () {
                    alert('The update failed; you might want to reload the page and try again..');
                }
            }
        );
     return;   
}

// *****  to Excute ajax_rate_doll.html for updating doll and doll_rating table.
// ***** Assuming that in doll_rating table only one entry per user per doll_id
// ***** @auther md


function update_photo_rating(photo_id, vote)
{
//alert("update_phot");
//alert("photo_id "+photo_id);
//alert("rating "+vote);

       request = new Ajax.Request(
            'ajax_photo_rate.html', {
                method : 'get',
                parameters : '?photo_id='+photo_id+'&rating=' + vote,

                onCreate : function () {
                  //  alert('starting ajax rate request');
                },

                onSuccess : function () {
                  //  alert('update photo the change made it to the backend!');
                    //alert("after photo_id "+photo_id);
		    //alert("after rating "+vote);

                    new_vote_photo(photo_id, vote);
                },

                onFailure : function () {
                    alert('The update failed; you might want to reload the page and try again..');
                }
            }
        );
     return;   
}


function update_user_room_photo_rating(user_id, user_session_id, vote)
{
	disableRatingStars();
	
    request = new Ajax.Request(
		'ajax_user_room_photo_rate.html', {
			method : 'get',
			parameters : 'selected_user_id='+user_id+'&rating=' + vote,

			onCreate : function () {
				//alert('starting ajax rate request');
			},

			onSuccess : function (transport) {
				var xml = transport.responseXML; 
				new_user_room_photo_vote(xml);
				//window.setTimeout('enableRatingStars()', 5000);
			},

			onFailure : function () {
				alert('The update failed; you might want to reload the page and try again..');
			}
		}
	 );
     return;   
}

function new_user_room_photo_vote(xmlDoc) {
	var avg_rating = 0, num_ratings = 0, user_vote = 0;
	
	if(xmlDoc && xmlDoc.childNodes) {
		for(var i = 0; i < xmlDoc.childNodes.length; ++i) {
			if(xmlDoc.childNodes[i].nodeName.toUpperCase() == 'RESULT') {
				result = xmlDoc.childNodes[i];
				for(var j = 0; j < result.childNodes.length; ++j) {
					if(result.childNodes[j].nodeName.toUpperCase() == 'AVG_RATING') {
						avg_rating = result.childNodes[j].firstChild.data;
					}
					else if(result.childNodes[j].nodeName.toUpperCase() == 'NUM_RATINGS') {
						num_ratings = result.childNodes[j].firstChild.data;
					}
					else if(result.childNodes[j].nodeName.toUpperCase() == 'USER_VOTE') {
						user_vote = result.childNodes[j].firstChild.data;
					}
				}
			}
		}
	}

	var rounded_avg_rating = parseFloat(avg_rating) > 0 ? Math.round(parseFloat(avg_rating)) : 0;
	for(var i=0; i< vote_stars.length; i++){
		vote_stars[i].selected = vote_stars[i].aid <= rounded_avg_rating ? true : false;      
	}
	vote_out(rounded_avg_rating, '_user_room_photo_');  

	document.getElementById('avgrating').innerHTML = avg_rating;
	document.getElementById('numratings').innerHTML = num_ratings;
	document.getElementById('user_vote').innerHTML = user_vote;
}

// ******* Created 13 mar @auther: md
// *****  replaced new_vote_doll function in include_photo_slideshow.html

function new_vote_photo( photo_id, vote){
//alert("new vote photo");

  var rating = parseFloat(document.getElementById('avgrating').innerHTML) * 1.0;
  var num_votes = parseInt(document.getElementById('numratings').innerHTML);
  
  var user_vote=parseFloat(document.getElementById('user_vote').innerHTML);

   if(user_vote>0){
     rating = (rating * num_votes) - user_vote;
     num_votes--; }
 
// window.alert('pre rate calcu : '+rating + ' x ' + num_votes)   

      var new_rating = rating + vote;
      num_votes++;
      new_rating = new_rating / num_votes;
      
      new_rating = Math.round(new_rating * 100) / 100;
      user_vote = vote;

  //num_votes++;

  document.getElementById('avgrating').innerHTML = new_rating;
  document.getElementById('numratings').innerHTML = num_votes;
  document.getElementById('user_vote').innerHTML = user_vote;

  for(var i=0; i< vote_stars.length; i++){
     if(vote_stars[i].aid <= new_rating){
      vote_stars[i].selected = true;
     }
    else {
      vote_stars[i].selected = false;      
    }

}

 vote_out(new_rating, '_');  
  return;
}

function disableRatingStars() {
	var star, star_link;
	for(var i=0; i< vote_stars.length; i++){
	  star = document.getElementById( 'star' + vote_stars[i].prefix + vote_stars[i].aid );
	  if ( star ) {
		star_link = star.parentNode;
		star_link.onclick2 = star_link.onclick;
		star_link.onclick = function() {alert('Rating not allowed. You already voted on this room');};
	  }
	}
}

function enableRatingStars() {
	var star, star_link;
	for(var i=0; i< vote_stars.length; i++){
	  star = document.getElementById( 'star' + vote_stars[i].prefix + vote_stars[i].aid );
	  if ( star ) {
		star_link = star.parentNode;
		star_link.onclick = star_link.onclick2;
		star_link.onclick2 = false;
	  }
	}
}
