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

var windowTimer = false;
var animationRunner = false;

$(document).ready(function(){
	////////////////////////////////////////////////////////
	//////////// handle description pop up boxes
	////////////////////////////////////////////////////////
	/*

	var x = 0;
	var y = 0;
	
	$().mousemove(function(e){
			x = e.pageX;
			y = e.pageY;
	});
	
	var hoverTimer = false;
	var descriptionShowing = false;
	
	function setTimedHover(thisElement) {
			hoverTimer = setTimeout(function(){
					var w = 0;
					var h = 0;
					var calcXpos = 0;
					var calcYpos = 0;
					
					var roboTip = $(thisElement).attr('robotip');
					
					descriptionShowing = true;
					$('#description-box').empty().append(roboTip);
					w = $('#description-box').width();
					h = $('#description-box').height();
					
					calcXpos = x - (w + 30);
					calcYpos = y - (h + 30);
					
					if(calcXpos < 0 || calcYpos < 0) {
							$('#description-box').css({top:y + 20, left:x + 20}).fadeIn(900);
					}else{
							$('#description-box').css({top:calcYpos, left:calcXpos}).fadeIn(900);
					}
					
					
					hoverTimer = false;
			
			}, 1000);
	}
	
	function cancelTimedHover() {
			if(hoverTimer) {
					clearTimeout(hoverTimer);
					hoverTimer = false;
			}
	} 

	$('.description-hover').hover(
			function(){
					var thisElement = this;
					setTimedHover(thisElement);
			}, 
			function(){
					cancelTimedHover();
					if(descriptionShowing == true) {
							$('#description-box').fadeOut(500, function(){descriptionShowing = false;});
					}
			}
	);
	
	*/
	////////////////////////////////////////////////////////
	
	
	////////////////////////////////////////////////////// abuse
  $('#report-abuse').click(function() {
  		if( $('#report-abuse-box').css('display') == 'none' ) {
  				$('#report-abuse-box').slideDown(300);
  		}else{
  				$('#report-abuse-box').slideUp(300);
  		}
  		return false;
  });

	$('#abuse-report-textarea').focus(function(){
			if( $('#abuse-report-textarea').attr('value') == $('#abuse-report-textarea').attr('defaultvalue') ) {
					$('#abuse-report-textarea').attr('value', '');
			}	
	});
	
	$('#abuse-report-submit').click(function() {
			var wl = window.location;
			$.getJSON('/users/report_abuse', $('#abuse-form').serialize()+'&tv=false'+'&wl='+wl, function(data){
					if(data.response == 'worked') {
							$('#abuse-report-textarea').attr('value', $('#abuse-report-textarea').attr('defaultvalue') );
							$('#report-abuse-box').slideUp(300);
							$('#abuse-report-feedback').empty().append('Received').slideDown(300);
					}
			});
	});
	
	$('#abuse-cancel').click(function(){
			$('#report-abuse-box').slideUp(300);
			return false;
	});
	//////////////////////////////////////////////////////
	
	
	
	
	///////////////////////////////////////////////////////////////////// hover jpeg animator
	var animatorOnTimer = 400; // how long it takes on mouseover to activate the animator (lower number is faster)
	var animatorSpeed = 75; // play back speed (lower number is faster)
	var framesPerRow = 5; // right now the tiles are 5 by 5, but if that changes, the values go here
	var frameRows = 5;  // right now the tiles are 5 by 5, but if that changes, the values go here
	
	var windowTimer = false;
	var animationRunner = false;
	var contextWidth = false;
	var contextHeight = false;
	
	$('.smirk-animated').live('mouseover', function() {
			var thisImg = this;
			var sid = $(thisImg).attr('sid');
			

			if (animationRunner) {
			 clearInterval(animationRunner);
			 animationRunner = false;
		  }

			clearTimeout(windowTimer);
			windowTimer = false;
			windowTimer = setTimeout(function() { // hover timer
					
					$.getJSON('/smirks/get_animated', 'sid='+sid, function(data) {
							
							if(data.response == 'worked') {
									
									contextWidth = parseInt( $(thisImg).attr('defaultsizesquared') ); // find original thumbnail dimensions so we can scale the tiles
									contextHeight = parseInt( $(thisImg).attr('defaultsizesquared') );
									
									var tmpImage = new Image();
									tmpImage.src = data.tiled;
									tmpImage.onload = function() {
									
											
											var w = tmpImage.width;
											var h = tmpImage.height;
											var frameWidth = w / framesPerRow; // calculate frame width and height from how many frames there are in the image
											var frameHeight = h / frameRows;
											
											
											// get scale
											var scale = contextWidth / frameWidth;
											
											$(thisImg).css({height:h * scale, width:w * scale}).attr('src', data.tiled); // recalculate image size and place in tiled image
											
											$(thisImg).addClass('animating');
											
											var x = 1;
											var y = 1;
											animationRunner = setInterval(function() {
													if(x < framesPerRow) { // go through columns
															$(thisImg).css({left: - ( (x * frameWidth) * scale)});
															x++;
													}else if(y < frameRows){ // go through rows
															$(thisImg).css({left: 0, top: - ( (y * frameHeight) * scale)});
															y++;
															x = 1;
													}else{ // reset to first row, first column
															$(thisImg).css({left: 0, top: 0});
															y = 1;
															x = 1;
													}
											
											}, animatorSpeed);
											
									} // end tmpImage.onload = function() {
							} // end if(data.response == 'worked') {
					});
					windowTimer = false;
			}, animatorOnTimer);
	});
	
	$('.smirk-animated').live('mouseout', function() {
			if(animationRunner) {
					clearInterval(animationRunner);
					animationRunner = false;
			}
			clearTimeout(windowTimer);
			windowTimer = false;
	});
	/////////////////////////////////////////////////////////////////////
	
	
	
	
});