// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function countWords(who, chars)
{
  var words = $('#review_' + who).val().split(/\b[\s,\.-:;]*/).length
  $("#" + who + "_count_field").val(words);
  $("#count_" + who).html(words);
  if ($("#content_count_field").length > 0) {
    var allwords = parseInt($("#introduction_count_field").val()) + parseInt($("#content_count_field").val()) + parseInt($("#quantity_count_field").val()) + parseInt($("#exclusivity_count_field").val()) + parseInt($("#updates_count_field").val()) + parseInt($("#features_count_field").val()) + parseInt($("#design_count_field").val()) + parseInt($("#extras_count_field").val()) + parseInt($("#price_count_field").val()) + parseInt($("#pros_count_field").val()) + parseInt($("#cons_count_field").val()) + parseInt($("#personal_opinion_count_field").val());
  } else {
    var allwords = parseInt($("#introduction_count_field").val()) +  parseInt($("#pros_count_field").val()) + parseInt($("#cons_count_field").val()) + parseInt($("#personal_opinion_count_field").val());
  }
  $("#overall_wordcount").html(allwords);
  countChar(who, chars);
}

function countChar(who, chars)
{
  var words = chars - $('#review_' + who).val().length
  $("#count_char_" + who).html(words);
}

function ratingUpdate() {
  if ($("#review_review_data_content_rating_0").length > 0) {
    var rating = parseFloat($("#review_review_data_content_rating_0").val()) + (parseFloat($("#review_review_data_content_rating_1").val())/10) + parseFloat($("#review_review_data_quantity_rating").val()) + parseFloat($("#review_review_data_exclusivity_rating").val()) + parseFloat($("#review_review_data_updates_rating").val()) + parseFloat($("#review_review_data_features_rating").val()) + parseFloat($("#review_review_data_design_rating").val()) + parseFloat($("#review_review_data_extras_rating").val()) + parseFloat($("#review_review_data_price_rating").val());
  } else {
    var rating = parseFloat($("#review_introduction_rating").val());
  }
  $("#rating_1").html(rating);
  $("#rating_2").html(rating);
}

function doMovieFormats(formats) {
  if (formats.length == 0) {
    alert("You need to select a Movie Format.")
  } else {
    for (var i = 0; i < formats.length; i++) 
    {
      $("#movie_formats").append("<tr class=\"movie_format\">\n\n  <td><input class=\"small\" id=\"review_new_movie_formats_review_attributes__format_name\" name=\"review[new_movie_formats_review_attributes][][format_name]\" type=\"hidden\" value=\"" + formats[i][0] + "\"/>" + formats[i][0] + "</td>\n  <td><input id=\"review_new_movie_formats_review_attributes__dimension\" name=\"review[new_movie_formats_review_attributes][][dimension]\" size=\"10\" type=\"text\" /></td>\n  <td>\n    <input id=\"review_new_movie_formats_review_attributes__bitrate\" name=\"review[new_movie_formats_review_attributes][][bitrate]\" size=\"10\" type=\"text\" />\nKbps  </td>\n  <td><a href=\"#\" onclick=\"this.up('.movie_format').remove(); return false;\">remove</a></td>\n\n</tr>\n");
    }
  }
  return false;
}

function getSelectBoxSelected(who) {
  var s = document.getElementById(who);
  for(var i=0; i<s.options.length; i++){
      if(s.options[i].selected){
          return s.options[i].text;
      }
  }
}

function getSelectSelected(who) {
  var selected = new Array();
  var s = document.getElementById(who);
  for(var i=0; i<s.options.length; i++){
      if(s.options[i].selected){
          selected.push(new Array(s.options[i].text, s.options[i].value));
      }
  }
  return selected;
}

function imageHeight(url) {
  var img = new Image();
  return img.height;
}

function preLoadImage(url, caption, silent) {
  if (silent == false) {
    $('#gallery_image').hide();
    $('#loading').show();
  }
  var image = new Image();
  image.onload = function() {
    if (silent == false) {
      $('#gallery_image').attr('src', url);
      $('#gallery_image').attr('alt', caption);
      $('#loading').hide();
      $('#gallery_image').show();
    }
  };
  image.src = url;
}

$(function(){
   $('#newsletter').click(function() {
     $('#newsletter').val("")
   });
  $('.rotator').mouseenter(function() {
    me = $(this)
    images = new Array(1,2,3,4,5,6,7,8,9);
    active = $(this).attr("id");
    preload_url = $(this).attr("src");
    for (i in images) {
      if (active == "") {
        preLoadImage(preload_url.replace("-thumb","-" + images[i] + "-thumb"), "", true)
      } else {
        preLoadImage(preload_url.replace("-" + active + "-thumb","-" + images[i] + "-thumb"), "", true)
      }
    }
    for (i in images) {
      image = images[i];
      if (active == "") {
        $(this).attr("src", $(this).attr("src").replace("-thumb","-" + images[i] + "-thumb"));
        $(this).attr("id", images[i]);
      } else {
        setTimeout(
                   function(me, active, image) {
                     me.attr("src", me.attr("src").replace("-" + active + "-thumb","-" + image + "-thumb"));
                     me.attr("id", image);
                   }, 500*images[i], $(this), active, image);
      }
      active = i;
    }
  });
  $("#reviewer_id").change(function(v){
    window.location.replace(location.href.replace(/&reviewer_id.*/, "") + "&reviewer_id=" + $("#reviewer_id").val());
  });
})
function bMark() {
  if (window.sidebar)
    {
    // firefox
    window.sidebar.addPanel(document.title,location.href, "");
    }
  else if(window.opera && window.print)
    {
    // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',location.href);
    elem.setAttribute('title',document.title);
    elem.setAttribute('rel','sidebar');
    elem.click();
    }
  else if(document.all)
    {
    // ie
    window.external.AddFavorite(location.href,document.title);
    }
}


