
var current_rating = new Array();

function setupDescriptionToggle() {
  jQuery('.descriptionToggle').click(function(){
    jQuery(this).parent().toggle().siblings('.short,.long').toggle(); 
  });
}

function setupImgRollover() {
  jQuery('img.hoverAnimate').each(function(i){
    jQuery(this).hover(function() {
      var img = jQuery(this);

      var hs = img.attr("hoversrc");
      var rs = img.attr("src");
      if (rs != hs) { img.attr("rootsrc",rs); }
      this.src = hs;

    }, function() {
      var img = jQuery(this);

      var rs = img.attr("rootsrc");
      if (rs != null) {
        img.attr("src", rs);
      }
    });
  });
}

function setupFavBlockButtons() {
  jQuery(".fav_block_button").click(function() {
    var state = jQuery(this).parents(".favs_block").children('.hideable_block').toggle().css('display');
    if (state == 'none') {
      this.src = '/images/arrow_left.gif';
    } else {
      this.src = '/images/arrow_down.gif';
    }

  });

}

 
// Send new rating via ajax
function addRating(rating, fileId) {
  jQuery("#rating_" + fileId).html( 'Updating... <img src="/images/loading_small.gif">');
  jQuery.ajax({
    type: "GET",
    url: "/ajax/addRating/" +  fileId,
    data: "rating=" + rating,
    dataType: 'json',
    success: function(msg,status) {
      if (msg.status == 'noauth') {
        jQuery(".ratings_text").html("<a href=\"/signup\">Signup now to use this feature!</a>");
      } else if (msg.status == 'success') {
        jQuery("#rating_" + fileId).html(msg.ratingTxt); 
        current_rating["" + fileId] = msg.rating;
        displayRating(msg.rating,fileId);
      }
      return true;
    }
  });
  current_rating["" + fileId] = rating;
  // display the users rating as feedback
  displayRating(rating,fileId);
}

// Color starts on page according to rating
function displayRating (rating,fileId) {
  // round up for display, ensure in range
  if (rating < 0.0) { rating = 0.0; }
  rating = parseInt(rating + 0.5);
  if (rating > 5.0) { rating = 5.0; }
  // first make all stars gray
  for(var i=1; i<=5; i++) {
    jQuery("#" + fileId + "_star" + i).attr("src","/art/img_star_gray.gif");
  }
  // then make lower stars red
  for(var i=1; i<=rating; i++) {
    jQuery("#" + fileId + "_star" + i).attr("src","/art/img_star_red.gif");
  }
}

function join_now_montage_overlay() {
  jQuery(document).ready(function(){
    h = jQuery('img.montage').height();
    w = jQuery('img.montage').width();
    if (h < 100) { 
      setTimeout('join_now_montage_overlay()', 100);
      return;
    }

    divLeft = 0;
    if (jQuery.browser.safari) {      
      divHeight = h;
    } else {
      divHeight = h-1;
    }

    jQuery('#overlay-crop')
      .show()
      .css({left:   divLeft + 'px',
            height: divHeight + 'px',
            width:  (w) + 'px'
    });
    jQuery('#overlay')
      .css({ left:  (w+15) + 'px',
             height:(h+4) + 'px'
    });

    setTimeout('doOverlay()', 1000);
  });
}

function doOverlay() {
  jQuery('#overlay').animate({'left':'330px'}, 'slow');
}

var photo_album_thumb_pages = []; // how many times we've tried to load a given page
function loadPhotoAlbumThumbnails(bid,index) {
  if (photo_album_thumb_pages[index].success) { return; }
  photo_album_thumb_pages[index].count++;
  jQuery.ajax({
    url : '/photo/album/thumbs',
    data: { bid: bid, index: index }, // easy!
    success: function(data, textStatus) {
      jQuery('.photo_album_thumb_page_' + index).html(data);
      photo_album_thumb_pages[index].success = 1;
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      if (actor_filter_loading < 3) {
        loadActorFilter();
      } else {
        actor_filter_loading = 0;
      }
    }
  });
}

var actor_filter_loading = 0;
function loadActorFilter() {
  actor_filter_loading++;
  jQuery.ajax({ 
    url : '/static/actor_filter_options.html',
    success: function(data, textStatus) {
      jQuery('.actor_filter_box').html(data); 
      setupActorFilterFunctions();
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      if (actor_filter_loading < 3) { 
        loadActorFilter();
      } else {
        actor_filter_loading = 0;
      }
    }
  });
}

function setupActorFilterFunctions() {
  var search = window.location.search.substring(1).split('&'); 
  jQuery.each( search, function(i,val) { 
    var kv = val.split('=');
    jQuery('input').each(function() {
      if (jQuery(this).attr('name') == kv[0] && jQuery(this).val() == kv[1] ) {
        jQuery(this).attr('checked',true); 
      }
    });
  });
  
  jQuery('.actor_filter_search').click(function() {
    var arr = [ ] ;
    jQuery('input').each(function() {
      if (this.checked) { 
        arr.push(jQuery(this).attr('name') + '=' + jQuery(this).val() );
      }
    });
    jQuery('.actor_filter_box').toggle();
    if (arr.length > 0) {
      window.location = '/actor?' + arr.join('&');
    } else {
      window.location = '/actor';
    }
  });
}


