// 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(){
   $("#page").change(function(v){
     if ($("#layout_page:checked").val() == "1") {
       $("#order").show();
       $("#url").show();
       $("#title").show();
       $("#metakeywords").show();
       $("#metadescription").show();
       $("#body").show();
     } else {
       $("#order").hide();
       $("#url").hide();
     }
   });
   $("#url").change(function(v){
     if ($("#layout_url").val() == "") {
       $("#title").show();
       $("#metakeywords").show();
       $("#metadescription").show();
       $("#body").show();
     } else {
       $("#title").hide();
       $("#metakeywords").hide();
       $("#metadescription").hide();
       $("#body").hide();
     }
   });
   $('#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());
  });
  $("#product_location").change(function(v){
       if ($("#product_location option:selected").val() == "amazon") {
         $("#amazon_index").show();
       } else {
         $("#amazon_index").hide();
       }
  });
  $(".data").hover(
    function() { 
      $(this).addClass("highlight"); 
      $(this).find(".long").show(); 
      $(this).find(".short").hide(); 
    },
    function() { 
      $(this).removeClass("highlight"); 
      $(this).find(".long").hide(); 
      $(this).find(".short").show(); 
    }
  );
})
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);
    }
}

function slideToggle(id){
  if ($(id).is(":hidden")) {
    $(id).slideDown("slow");
  } else {
    $(id).slideUp("slow");
  }
}

function addProduct(id){
  if ($('#review_safe_title').val() == "") {
    $('#review_safe_title').val($('#product_' + id + '_title').val());
    $('#review_introduction').val($('#product_' + id + '_description').val());
    $('#review_personal_opinion').val($('#product_' + id + '_long_description').val());
  }
  $('#review_sponsor_url').val(unescape($('#product_' + id + '_detail_url').val()));
  $('#image').val(unescape($('#product_' + id + '_image').val()));
  $('#asin').val($('#product_' + id + '_asin').val());
  $('#ean').val($('#product_' + id + '_ean').val());
  $("#product_title").html($('#product_' + id + '_title').val());
  $("input#product_title").html($('#product_' + id + '_title').val());
  $("#product_price").html($('#product_' + id + '_price').val());
  $("a#product_sponsor_url").attr("href", $('#product_' + id + '_detail_url').val())
  $("input#product_sponsor_url").val($('#product_' + id + '_detail_url').val());
  $("#product_asin").val($('#product_' + id + '_asin').val());
  $("#product_buyat_product_id").val($('#product_' + id + '_buyat_product_id').val());
  $("#product_ean").val($('#product_' + id + '_ean').val());
  $(".new_product").show();
  $('#product_search_form').remove();
  //$('#new_product').hide()
}

