// JavaScript Document
var showDetails = true; //yes show the details
var o_h; //our original height
function rateGuy(image_id){
	target = dojo.byId('currentImageRating');
	dojo.xhrGet( { //
		// The following URL must match that used to test the server.
		url: "action/rateGuy.php?image_id="+image_id, 
		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		 //getNewImage();
		 //document.refresh(); //JAVASCRIPT REFRESH
		 //slide down our rater thingy
		 //ACTUAL GENDER?!
		 //setTimeout("window.location = 'index.php';" , 2000);
		 dojo.byId('voteGuy').style.display = 'block';
		 dojo.byId('actualGender').innerHTML = response;
		// genderDialog.show();
		 //fade it in for easierness....
		 //alert('The Rating is:'+response);
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}
var shrink = false;
function enlargeImage(){
		target = dojo.byId('currentImage');
		if(shrink == false){
			var t_w = target.offsetWidth + 200;
			shrink = true;
		} else{
			var t_w = target.offsetWidth - 200;
			shrink = false;
		}
		var args = {
			node: target,
			duration:1000,
			properties: {
				width:{ end:t_w }
				/*height:{ end:t_h }*/
			}
		};
		dojo.animateProperty(args).play();
		target = dojo.byId('currentImageContainer');
		tw += 24;
		var args = {
			node: target,
			duration:1000,
			properties: {
				width:{ end:t_w }
				/*height:{ end:t_h }*/
			}
		};
		dojo.animateProperty(args).play();
}
function rateGirl(image_id){
		target = dojo.byId('currentImageRating');
	dojo.xhrGet( { //
		// The following URL must match that used to test the server.
		url: "action/rateGirl.php?image_id="+image_id, 
		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		 //getNewImage();
		 //document.refresh(); //JAVASCRIPT REFRESH
		 //slide down our rater thingy
		 //ACTUAL GENDER?!
		 dojo.byId('voteGirl').style.display = 'block';
		 dojo.byId('actualGender').innerHTML = response;
		 //setTimeout("window.location = 'index.php';" , 2000);
		// genderDialog.show();
		 //fade it in for easierness....
		 //alert('The Rating is:'+response);
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}
function rateUnkown(image_id){
		target = dojo.byId('currentImageRating');
	dojo.xhrGet( { //
		// The following URL must match that used to test the server.
		url: "action/rateUnknown.php?image_id="+image_id, 
		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		 //getNewImage();
		 //document.refresh(); //JAVASCRIPT REFRESH
		 //slide down our rater thingy
		 //ACTUAL GENDER?!
		 setTimeout("window.location = 'index.php';" , 2000);
		 //fade it in for easierness....
		 //alert('The Rating is:'+response);
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}
function rateImage(rating, image_id){
	target = dojo.byId('currentImageRating');
	dojo.xhrGet( { //
		// The following URL must match that used to test the server.
		url: "action/rate.php?rating="+rating+"&image_id="+image_id, 
		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		 //getNewImage();
		 //document.refresh(); //JAVASCRIPT REFRESH
		 //slide down our rater thingy
		 target.innerHTML = response + '<div class="sm">overall</div>'; //multiply for 1 for number formatting options
		 //update position
		 target.style.marginLeft = 76 * response - 33 - 23 + 'px';
		 //set display to block
		 target.style.display = 'block';
		 setTimeout("window.location = 'index.php';" , 2000);
		 //fade it in for easierness....
		 //alert('The Rating is:'+response);
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}
function reportImage(){
	node = dojo.byId('reportForm');
	dojo.xhrPost( { //
		// The following URL must match that used to test the server.
		url: "action/report.php", 
		handleAs: "text",
		form:node,
		node:node,

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		  reportDialog.hide();
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}
function sendFriend(){
	node = dojo.byId('friendForm');
	dojo.xhrPost( { //
		// The following URL must match that used to test the server.
		url: "action/sendFriend.php", 
		handleAs: "text",
		form:node,
		node:node,

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		  friendDialog.hide();
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}
function fadeOut(target, end){
	var args = {
		node:target,
		properties:{
			opacity:{
				start:1.0,
				end:0.1
			}
		},
		onEnd: end
	}
	dojo.animateProperty(args).play();
}
function deleteImage(image_id){
	target = dojo.byId('image'+image_id);
	dojo.xhrGet( { //
		// The following URL must match that used to test the server.
		url: "action/delete.php?id="+image_id, 
		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		 //getNewImage();
		 fadeOut(target,function() { target.style.display = 'none' });
		 //document.refresh(); //JAVASCRIPT REFRESH
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}

function ajaxUpload(){
		node = dojo.byId('uploadForm');
		//replace the container info with the friggin loading .gif
		dojo.byId('testContainer').style.display = 'none';
		dojo.byId('loader').style.display = 'block';
		dojo.byId('loader').innerHTML = '<img src="images/loading.gif" />';
	/*dojo.io.iframe.send( { //
		// The following URL must match that used to test the server.
		url: "action/upload.php", 
		handleAs: "text",
		contentType: "multipart/form-data",
		form:node,

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		  alert(response);
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error( 'iframe.send error' );
		  console.error("HTTP status code: ", ioArgs.status); //
		  return response; //
		  }
		});*/
	
	dojo.io.iframe.send({
	        contentType: "multipart/form-data",
	        url: "action/upload.php", // change to a server where you can view the server logs
	        form: node,
	        handleAs: "text/html",
	        load: function ( response ) {
               dojo.byId('testContainer').style.display = 'block';
			   dojo.byId('loader').style.display = 'none';
			   node.reset();
			   uploadDialog.hide();
			   //document.location = 'index.php?id='+response;
	        },
	         error: function (error) {
					   	alert(error);
				   },
	                preventCache: true
	        });
	
	return false;
}
//TO-DO: FADE IN FADE OUT, REPLACE RATE BAR ETC
function getNewImage(){
		var target = dojo.byId('container');
		dojo.xhrGet( { //
		// The following URL must match that used to test the server.
		url: "action/getRandImage.php", 
		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) { //
		 target.innerHTML = response;
		  return response; //
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { //
		  console.error("HTTP status code: ", ioArgs.xhr.status); //
		  return response; //
		  }
		});
}

function fakeAjax(){
	dojo.byId('testContainer').innerHTML = 'Upload Successful';
}

function imageDetails(){
		if(showDetails){
			var target = dojo.byId('details');
			var s_h = target.offsetHeight;
			o_h = target.offsetHeight;
			var s_t = target.offsetTop;
			var e_t = s_t - 270;
			var args = {
				node: target,
				duration:500,
				properties:{
					height: {
						start:s_h,
						end:300
					},
					top: {
						start:s_t,
						end:e_t
					}
				}
			};
			
			dojo.fx.slideTo(args).play();
			showDetails = false;
		} else{
			//we will hide the details
			var target = dojo.byId('details');
			var s_h = target.offsetHeight;
			var s_t = target.offsetTop;
			var e_t = s_t + 270;
			var args = {
				node: target,
				duration:500,
				properties:{
					height: {
						start:s_h,
						end:o_h
					},
					top: {
						start:s_t,
						end:e_t
					}
				}
			};
			
			dojo.fx.slideTo(args).play();
			showDetails = true;
		}
}