
// overide debugger to not output at all
// debug.log = function(e){return false;};
// debugger is now silent

$(document).ready(function(){

  // helper method for posting JSON
	$.postJSON = function(url, data, callback) {
		$.post(url, data, callback, "json");
	};

	$.getJSON = function(url, data, callback) {
		$.get(url, data, callback, "json");
	};

  /* bind jQuery routes (named events) to product_wars events */
  		$(document).bind('/cast_vote', function(e,options){
  			product_wars.cast_vote(options); // the current onSuccess handler for cast_vote is "/load_battle"
  		});

  		$(document).bind('/load_battle', function(e,options){
			product_wars.fadeCount = 2;
  			product_wars.load_battle(options);
  		});

  		$(document).bind('/load_war', function(e,options){
  			product_wars.load_war(options);
  		});


  		$(document).bind('/view_battle_history', function(e,options){
  			product_wars.view_battle_history(options);
  		});
  /* end jQuery routes (named events) to product_wars events */

  /* bind user interaction events to jQuery event handlers */


      // category slider
        $('#catlist').categorySlider();
      // end category slider


  		// hover over each product to highlight / unhighlight
  		$('.product_area').hover(function(e){
  			//debug.log('hover in');

  			if($(this).attr('id') == "player_one"){product_wars.lastVote  = "left";}else{product_wars.lastVote  = "right";}

  			if($('.loading').css('display') == 'none'){
  				$('.vote_bottom', this).show();
  				$('.vote_top', this).show();
  			}
  		},
  		function(e){
  			//debug.log('hover out');

  			if($('.loading').css('display') == 'none'){
  				$('.vote_bottom', this).hide();
  				$('.vote_top', this).hide();
  				product_wars.lastVote = '';
  			}

  		});

  		// click on a product to vote for it
  		$('.product_area').click(function(e){

  		  if($(e.target).hasClass('product_link')){
  		    return true;
  		  }

  	  	/* could remove this line if DOM structure was slightly updated */
  		  var myVote = '';if($(this).attr('id') == "player_one"){myVote = "left";}else{myVote = "right";}
		  product_wars.fadeCount = 3;
  		  $(document).trigger('/cast_vote', {
  		    vote : myVote,
  		    product_id : $(this).data('product_id')
  		  });

  		});


      $('#draw').click(function(e){
		product_wars.fadeCount = 2;
        $(document).trigger('/cast_vote', {
      	  vote : 'draw'
      	});
      });

      /* slide menu events */
      $('#catlist a').click(function(e){



        // update currentCategory sidebar
        $('#currentCategory').html($('span span',$(this)).html());

        $('#catlist li').removeClass('active');

        $(this).parent().addClass('active');

        $(document).trigger('/load_war', {
      	  url : $(this).attr('href')
      	});

      	// toggle menu to close it
		$('#catlist dt').click();

      	// cancel bubbling so link doesnt actually fire href
        return false;
      });

      /* end slide menu events */

  		/* battle history UI events */

      		// click previous or next in history to view previous battles
      		$('#prev_history').click(function(e){

      		  // we don't want next / previous history buttons to work if there is no history
      		  if(product_wars.history.length <= 1){
      		    return false;
      		  }

	          product_wars.fadeReady = 3;

      		  product_wars.lastHistory = "left";

      		  var prev_position = product_wars.history.length - 1; // default position to last element for "wrap around"
      		  if(product_wars.history_active_index != 0) { prev_position = product_wars.history_active_index - 1; }

      		  $(document).trigger('/view_battle_history', {
      		    historyIndex : prev_position
      		  });

      		});

      		$('#next_history').click(function(e){

      		  // we don't want next / previous history buttons to work if there is no history
      		  if(product_wars.history.length <= 1){
      		    return false;
      		  }

            product_wars.fadeReady = 3;

      		  product_wars.lastHistory = "right";

      		  var next_position = 0; // default position to first element for "wrap around"
      		  if(product_wars.history_active_index != product_wars.history.length - 1 ) { next_position = product_wars.history_active_index + 1; }

      		  $(document).trigger('/view_battle_history', {
      		    historyIndex : next_position
      		  });
      		});

      		$('#prev_history').hover(function(e){
      			//debug.log('hover in');
      			// we don't want next / previous history buttons to work if there is no history
      		  if(product_wars.history.length > 1){
      		    $(this).css('background', 'url(/images/battles/show/battle_sprite.png) no-repeat -964px -511px');
      			  $(this).css('cursor', 'pointer');
      		  }
      		},
      		function(e){
      			//debug.log('hover out');
      			if(product_wars.history.length > 1){
      				$(this).css('background', 'url(/images/battles/show/battle_sprite.png) no-repeat -993px -511px');
      			}
      			else{
      				$(this).css('background', 'url(/images/battles/show/battle_sprite.png) no-repeat -1020px -511px');
      			}
       		    $(this).css('cursor', '');
      			product_wars.lastHistory = '';
      		});

      		$('#next_history').hover(function(e){
      			//debug.log('hover in');
      			if(product_wars.history.length > 1){
      				$(this).css('background', 'url(/images/battles/show/battle_sprite.png) no-repeat -964px -483px');
      				$(this).css('cursor', 'pointer');
      			}
      		},
      		function(e){
      			//debug.log('hover out');
      			if(product_wars.history.length > 1){
      				$(this).css('background', 'url(/images/battles/show/battle_sprite.png) no-repeat -993px -483px');
      			}
      			else{
      				$(this).css('background', 'url(/images/battles/show/battle_sprite.png) no-repeat -1020px -482px');
      			}
      			product_wars.lastHistory = '';
      			    $(this).css('cursor', '');
      		});

      		$('#battle_history').hover(function(e){
      			//debug.log('hover in');
      			$('#battle_history button').show();
      		},

      		function(e){
      			//debug.log('hover out');
      			$('#battle_history button').hide();
      		});

  		/* end battle history UI events */

  /* end user interaction events */


  /* base state fade in cheat so images gracefully load in, this could be be refactored as part of product_wars.fade_in_products */
  var firstFade = 0;

  $('#left_product_image img').error(function() {
    $(this).attr('src', '/images/product_default_medium.jpg')
  });

  $('#left_product_image img').load(function(e) {

    var imgRatio = $('#left_product_image img').width() / $('#left_product_image img').height();


    if(imgRatio > product_wars.product_image_ratio){
      //$('#left_product_image img').width('1000%');
    }
    else{
      //$('#left_product_image img').height('1000%');
    }



     firstFade ++;
     if(firstFade>1){
       $('.loading').hide();
       $('#right_product_image img').fadeIn(500);
       $('#left_product_image img').fadeIn(500);
     }
 	});

  $('#right_product_image img').error(function() {
    $(this).attr('src', '/images/product_default_medium.jpg')
  });

  $('#right_product_image img').load(function(e) {


    var imgRatio = $('#right_product_image img').width() / $('#right_product_image img').height();

    if(imgRatio > product_wars.product_image_ratio){
      //$('#right_product_image img').width('1000%');
    }
    else{
      //$('#right_product_image img').height('1000%');
    }

     firstFade ++;
     if(firstFade>1){
       $('.loading').hide();
       $('#right_product_image img').fadeIn(500);
       $('#left_product_image img').fadeIn(500);
     }
 	});

});







