$(function(){
  $('#slideshow-left').cycle({
		fx: 'scrollHorz',
		timeout: 0,
		pager: '#pagination-left',
		speed: 300
	});
	
	$('#slideshow-right').cycle({
		fx: 'scrollHorz',
		timeout: 0,
		pager: '#pagination-right',
		speed: 300
	});
	
	$('#slideshow').cycle({
	  fx: 'scrollHorz',
	  timeout: 0,
    pager: '#pagination',
		speed: 300
	});
	
	// centres the pagination links
	(function alignPagination() {
	  $('#pagination, #pagination-left, #pagination-right').each(function(idx) {
	    var anchorNum = $(this).find('a').length;
	    $(this).css({
	      width : anchorNum * 36,
	      margin : '0 auto'
	    });
	  });
	})(); // execute asap
	

});


$(document).ready(function() {
  
  $('.stars.editable').each(function() {
    
    var original_stars=0;
    
    if($(this).hasClass('one')) original_stars = 1;
    if($(this).hasClass('two')) original_stars = 2;
    if($(this).hasClass('three')) original_stars = 3;
    if($(this).hasClass('four')) original_stars = 4;
    if($(this).hasClass('five')) original_stars = 5;
    
    
    $(this).mousemove(function(e) {
      
      if($(this).hasClass('locked')) return;
      
      $(this).addClass('editing');
      
      var x = e.pageX - $(this).offset().left;
    	//var y = e.pageY - this.offsetTop;
    	
      $(this).removeClass('one two three four five');
      
      newstars=0;
      if(x > 0) newstars=1;
      if(x > 18) newstars=2;
      if(x > 36) newstars=3;
      if(x > 54) newstars=4;
      if(x > 72) newstars=5;
      
      switch(newstars) {
        case 1:
          $(this).addClass('one');
        break;
        case 2:
          $(this).addClass('two');
        break;
        case 3:
          $(this).addClass('three');
        break;
        case 4:
          $(this).addClass('four');
        break;
        case 5:
          $(this).addClass('five');
        break;
      }
      
    });
    
    $(this).mouseout(function(e) {
      
      if($(this).hasClass('locked')) return;
      
      $(this).removeClass('editing');
      $(this).removeClass('one two three four five');
      
      switch(original_stars) {
        case 1:
          $(this).addClass('one');
        break;
        case 2:
          $(this).addClass('two');
        break;
        case 3:
          $(this).addClass('three');
        break;
        case 4:
          $(this).addClass('four');
        break;
        case 5:
          $(this).addClass('five');
        break;
      }
      
    });
    
    $(this).click(function(e) {
      
      e.preventDefault();
      $(this).addClass('locked');
      
      var set_stars=0;

      if($(this).hasClass('one')) set_stars = 1;
      if($(this).hasClass('two')) set_stars = 2;
      if($(this).hasClass('three')) set_stars = 3;
      if($(this).hasClass('four')) set_stars = 4;
      if($(this).hasClass('five')) set_stars = 5;
      
      // find the link
      var voteurl = $(this).find('a').attr('href');
      voteurl = voteurl + '&vote=' + set_stars;
      
      // ajax the url to the server
      $.get(voteurl, {}, function(data) {
        // done
      });
      
      
    });
    
  });
  
});
