// only necessary when multicursor.js is called from multiple locations
//var LOCATION = './'; // http://www.example.com/www_hack/
var LOCATION = 'http://0508.dondeveoarte.com/www_hack/';
// Remove cursor after n seconds inactive
var TIME_OUT     = 60; //seconds
// make an AJAX call every half second
var REFRESH_TIME = 500; // mili-seconds
// Remove cursor after n seconds inactive
var TIME_OUT     = 60; //seconds

// Call once the DOM is ready 
$(document).ready(function(){
	// Create arrows container
	var cursors = {};
	cursors.total = 0;
	
	// Update mouse at every mouse movement...
	var mouse = {x:0, y:0, moved:false};
	$().mousemove(function(e){
		if (e.hasOwnProperty('pageX')) {
			mouse.x = e.pageX;
			mouse.y = e.pageY;
		} else {  // IE compatibility
			mouse.x = e.clientX + document.body.scrollLeft
		    mouse.y = e.clientY + document.body.scrollTop
		}
		mouse.moved = true;

		$('#status').html(mouse.x +', '+ mouse.y);
	});
	// *****************************

	doAJAX(mouse, cursors);	
});
	

/*
 * functions 
 */
function doAJAX(mouse, cursors){
	// do not send mouse position if mouse has not moved 
	var pos = 'None';
	
	if(mouse.moved){
		var pos  = mouse.x+','+mouse.y;		
		mouse.moved = false;
	}
	
	// try ajax
	var uri  = LOCATION+'./api/json.php';
	var args = {val: pos};
	var msg  = 'nothing';
	$.getJSON(uri, args,function(data){
		msg = 'Server Info:';
		var moving_cursors = [];
		$.each(data, function(session_id, positions, timestamp){
			msg += move_cursor(cursors, session_id, positions)
			moving_cursors.push(session_id);
			cursors[session_id].ttl = TIME_OUT;
	  	});


		// decrement ttl or remove cursor
		for (var session_id in cursors) {
		    if (cursors.hasOwnProperty(session_id) && session_id!='total') {
		    	msg += 'ttl=('+cursors[session_id].ttl+')';
		        if(moving_cursors.indexOf(session_id)==-1){
		        	if(cursors[session_id].ttl > 0){
		        		cursors[session_id].ttl--;
		        	} else {
		        		// remove cursor from 
		        		cursors[session_id].remove();
		        		delete cursors[session_id];
		        		msg += 'Remove '+session_id;
		        	}
		        }
		    }
		}

		// shows api answer
	  	$('#debug_verbose').html(msg);
		if(cursors.total > 0) {
	  		$('#debug_msg').html("SCRIPT: ON");
		} else {
			$('#debug_msg').html("SCRIPT: OFF");
	  	}
	});

	// if server is down this will keep on making calls...
	// (FIX IT) only make another call when previous has been received or dropped
	setTimeout(function(){doAJAX(mouse, cursors)}, REFRESH_TIME);
}


// Each OS have a different cursor representation
// The OS has to be detected and 
// a similar image is used to represent the several cursors
function get_pointer_file(){
	switch(BrowserDetect.OS)
	{
		case 'Windows':
		  var pointer_file = LOCATION+"./inc/images/ms_pointer.png";
		  break    
		case 'Linux':
		  //Todo: get pointer for linux (depends on GUI?)
		  var pointer_file = LOCATION+"./inc/images/mac_pointer.png";
		  break
		default:
		  var pointer_file = LOCATION+"./inc/images/mac_pointer.png";
	}
	return pointer_file;
}

function move_cursor(cursors, session_id, positions){
	var msg = '';
	// create a new cursor for each new session 
	if (!cursors.hasOwnProperty(session_id)) {
		cursors.total += 1;
		cursors[session_id] = new Mover(session_id, false, get_pointer_file());
 	} 

    //alert(dump(positions));	
	position = positions.position.split(',');

	
	if (position.length >= 2){
		p = {x:position[0], y:position[1]};
		cursors[session_id].moveTo(p); 
		msg = '<p/>session_id= '+session_id+' positions= '+p.x+','+p.y;
	}
	return msg;
}

function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}