// JavaScript Document - General Functions
var current = 1;
var data = '';
var sessionID = '';
var maxlimit = 125;

$(document).ready(function(){

    $("#photo").click(function () {
    	
    	if(document.getElementById('mojo').value=='1'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('photo').style.backgroundColor = '#FFFFFF';
    		$("#viddler").fadeOut("slow", slideMiddle);
    		$("#text").html("Upload and Share your images.");
    		$("#flash").html("<input id='shackup' type='file' name='uploaded_file' size='10' /><input id='shackurl' type='text' style='display:none;' /><input id='shackurluploader' type='button' onclick='shackURLUpload();' style='display:none;' value='Upload' /> or <a id='shackq' onclick='pasteURL(\"shack\");' href='#'>Paste URL</a><a id='shackq2' onclick='pasteURL(\"shack\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '2';
    		shackUpload();
    	}
    	else if(document.getElementById('mojo').value=='3'){
    		document.getElementById('docs').style.backgroundColor = '#E7EFF6';
    		document.getElementById('photo').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Upload and Share your images.");
    		$("#flash").html("<input id='shackup' type='file' name='uploaded_file' size='10' /><input id='shackurl' type='text' style='display:none;' /><input id='shackurluploader' type='button' onclick='shackURLUpload();' style='display:none;' value='Upload' /> or <a id='shackq' onclick='pasteURL(\"shack\");' href='#'>Paste URL</a><a id='shackq2' onclick='pasteURL(\"shack\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '2';
    		shackUpload();
    	}
    	else if(document.getElementById('mojo').value=='4'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('photo').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Upload and Share your images.");
    		$("#flash").html("<input id='shackup' type='file' name='uploaded_file' size='10' /><input id='shackurl' type='text' style='display:none;' /><input id='shackurluploader' type='button' onclick='shackURLUpload();' style='display:none;' value='Upload' /> or <a id='shackq' onclick='pasteURL(\"shack\");' href='#'>Paste URL</a><a id='shackq2' onclick='pasteURL(\"shack\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '2';
    		shackUpload();
    	}
    	else if(document.getElementById('mojo').value=='5'){
    		document.getElementById('moretext').style.backgroundColor = '#E7EFF6';
    		document.getElementById('photo').style.backgroundColor = '#FFFFFF';
    		slideMiddle();
    		$("#text").html("Upload and Share your images.");
    		$("#flash").html("<input id='shackup' type='file' name='uploaded_file' size='10' /><input id='shackurl' type='text' style='display:none;' /><input id='shackurluploader' type='button' onclick='shackURLUpload();' style='display:none;' value='Upload' /> or <a id='shackq' onclick='pasteURL(\"shack\");' href='#'>Paste URL</a><a id='shackq2' onclick='pasteURL(\"shack\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '2';
			slideMoreTextBack();
    		shackUpload();
			maxlimit = 125;
			$("#count").html(125);
    	}
    	else{
    		//nothing
    	}
    });
    
    $("#docs").click(function () {
    	
    	if(document.getElementById('mojo').value=='1'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('docs').style.backgroundColor = '#FFFFFF';
    		$("#viddler").fadeOut("slow", slideMiddle);
    		$("#text").html("Upload your files and share it");
    		document.getElementById('mojo').value= '3';
    		$("#flash").html("<input id='docup' type='file' size='10' /><input id='scribdurl' type='text' style='display:none;' /><input id='docuploader' type='button' onclick='docUpload();' value='Upload' /><input id='docurluploader' type='button' onclick='docURLUpload();' style='display:none;' value='Upload' /> or <a id='scribdq' onclick='pasteURL(\"scribd\");' href='#'>Paste URL</a><a id='scribdq2' onclick='pasteURL(\"scribd\");' href='#' style='display:none;'>Cancel</a>");
    		docUpload();
    	}
    	else if(document.getElementById('mojo').value=='2'){
    		document.getElementById('photo').style.backgroundColor = '#E7EFF6';
    		document.getElementById('docs').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Upload your files and share it");
    		$("#flash").html("<input id='docup' type='file' size='10' /><input id='scribdurl' type='text' style='display:none;' /><input id='docuploader' type='button' onclick='docUpload();' value='Upload' /><input id='docurluploader' type='button' onclick='docURLUpload();' style='display:none;' value='Upload' /> or <a id='scribdq' onclick='pasteURL(\"scribd\");' href='#'>Paste URL</a><a id='scribdq2' onclick='pasteURL(\"scribd\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '3';
    		docUpload();
    	}
    	else if(document.getElementById('mojo').value=='4'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('docs').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Upload your files and share it");
    		$("#flash").html("<input id='docup' type='file' size='10' /><input id='scribdurl' type='text' style='display:none;' /><input id='docuploader' type='button' onclick='docUpload();' value='Upload' /><input id='docurluploader' type='button' onclick='docURLUpload();' style='display:none;' value='Upload' /> or <a id='scribdq' onclick='pasteURL(\"scribd\");' href='#'>Paste URL</a><a id='scribdq2' onclick='pasteURL(\"scribd\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '3';
    		docUpload();
    	}
    	else if(document.getElementById('mojo').value=='5'){
    		document.getElementById('moretext').style.backgroundColor = '#E7EFF6';
    		document.getElementById('docs').style.backgroundColor = '#FFFFFF';
    		slideMiddle();
    		$("#text").html("Upload your files and share it");
    		$("#flash").html("<input id='docup' type='file' size='10' /><input id='scribdurl' type='text' style='display:none;' /><input id='docuploader' type='button' onclick='docUpload();' value='Upload' /><input id='docurluploader' type='button' onclick='docURLUpload();' style='display:none;' value='Upload' /> or <a id='scribdq' onclick='pasteURL(\"scribd\");' href='#'>Paste URL</a><a id='scribdq2' onclick='pasteURL(\"scribd\");' href='#' style='display:none;'>Cancel</a>");
    		document.getElementById('mojo').value= '3';
			slideMoreTextBack();
    		docUpload();
			maxlimit = 125;
			$("#count").html(125);
    	}
    	else{
    		//nothing
    	}
    });
    
    $("#current").click(function () {
    	
    	if(document.getElementById('mojo').value=='2'){
    		document.getElementById('photo').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet using your webcam, or <a id='upload' href='#'>upload your own video</a>");
    		slideMiddleBack();
    		$("#flash").html(getRecorder());
    		document.getElementById('mojo').value= '1';
    	}
    	else if(document.getElementById('mojo').value=='3'){
    		document.getElementById('docs').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet using your webcam, or <a id='upload' href='#'>upload your own video</a>");
    		slideMiddleBack();
    		$("#flash").html(getRecorder());
    		document.getElementById('mojo').value= '1';;
    	}
    	else if(document.getElementById('mojo').value=='4'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet using your webcam, or <a id='upload' href='#'>upload your own video</a>");
    		slideMiddleBack();
    		$("#flash").html(getRecorder());
    		document.getElementById('mojo').value= '1';
    	}
    	else if(document.getElementById('mojo').value=='5'){
    		document.getElementById('moretext').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet using your webcam, or <a id='upload' href='#'>upload your own video</a>");
    		$("#flash").html(getRecorder());
    		document.getElementById('mojo').value= '1';
			slideMoreTextBack();
			maxlimit = 125;
			$("#count").html(125);
    	}
    	else{
    		//nothing
    	}
    });
    
    $("#upload").click(function () {
    	
    	if(document.getElementById('mojo').value=='1'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#viddler").fadeOut("slow", slideMiddle);
    		$("#text").html("Upload your videos, most file types, 500mb limit");
    		$("#flash").html("<input id='vidup' type='file' size='10' /><input id='videourl' type='text' style='display:none;' /><input id='vidurluploader' type='button' onclick='vidURLUpload();' style='display:none;' value='Upload' /><input type='button' onclick='videoUpload();' value='Upload' />");
    		document.getElementById('mojo').value= '4';
    		videoUpload();

    	}
    	else if(document.getElementById('mojo').value=='2'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Upload your videos, most file types, 500mb limit");
    		$("#flash").html("<input id='vidup' type='file' size='10' /><input id='videourl' type='text' style='display:none;' /><input id='vidurluploader' type='button' onclick='vidURLUpload();' style='display:none;' value='Upload' /><input type='button' onclick='videoUpload();' value='Upload' />");
    		document.getElementById('mojo').value= '4';
    		videoUpload();

    	}
    	else if(document.getElementById('mojo').value=='3'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('current').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Upload your videos, most file types, 500mb limit");
    		$("#flash").html("<input id='vidup' type='file' size='10' /><input id='videourl' type='text' style='display:none;' /><input id='vidurluploader' type='button' onclick='vidURLUpload();' style='display:none;' value='Upload' /><input type='button' onclick='videoUpload();' value='Upload' />");
    		document.getElementById('mojo').value= '4';
    		videoUpload();

    	}
    	else if(document.getElementById('mojo').value=='5'){
    		document.getElementById('moretext').style.backgroundColor = '#E7EFF6';
    		document.getElementById('upload').style.backgroundColor = '#FFFFFF';
			slideMoreTextBack();
    		slideMiddle();
    		$("#viddler").fadeOut("slow", slideMiddle);
    		$("#text").html("Upload your videos, most file types, 500mb limit");
    		$("#flash").html("<input id='vidup' type='file' size='10' /><input id='videourl' type='text' style='display:none;' /><input id='vidurluploader' type='button' onclick='vidURLUpload();' style='display:none;' value='Upload' /><input type='button' onclick='videoUpload();' value='Upload' />");
    		document.getElementById('mojo').value= '4';
    		videoUpload();
			maxlimit = 125;
			$("#count").html(125);
    	}
    	else {
    		//do nothing
    	}
    });
    
    $("#moretext").click(function () {
    	
    	if(document.getElementById('mojo').value=='1'){
    		document.getElementById('current').style.backgroundColor = '#E7EFF6';
    		document.getElementById('moretext').style.backgroundColor = '#FFFFFF';
    		$("#viddler").fadeOut("slow");
    		$("#text").html("Tweet to your hearts content!");
    		$("#flash").html('');
    		document.getElementById('mojo').value= '5';
    		maxlimit = 1400;
    		$("#count").html('1400');
			slideMoreText();
    	}
    	else if(document.getElementById('mojo').value=='2'){
    		document.getElementById('photo').style.backgroundColor = '#E7EFF6';
    		document.getElementById('moretext').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet to your hearts content!");
    		$("#flash").html('');
    		document.getElementById('mojo').value= '5';
    		maxlimit = 1400;
    		$("#count").html('1400');
			slideMiddleBackMoreText(); 
			slideMoreText();    		 		
    	}
    	else if(document.getElementById('mojo').value=='3'){
    		document.getElementById('docs').style.backgroundColor = '#E7EFF6';
    		document.getElementById('moretext').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet to your hearts content!");
    		$("#flash").html('');
    		document.getElementById('mojo').value= '5';
    		maxlimit = 1400;
    		$("#count").html('1400');
			slideMiddleBackMoreText();
			slideMoreText();    		
    	}
    	else if(document.getElementById('mojo').value=='4'){
    		document.getElementById('upload').style.backgroundColor = '#E7EFF6';
    		document.getElementById('moretext').style.backgroundColor = '#FFFFFF';
    		$("#text").html("Tweet to your hearts content!");
    		$("#flash").html('');
    		document.getElementById('mojo').value= '5';
    		maxlimit = 1400;
    		$("#count").html('1400');
			slideMiddleBackMoreText();
			slideMoreText();    		
    		
    	}
    	else{
    		//do nothing
    	}
    });
    
});
  
function slideMiddle(){
	 $("#join").animate({height: "10px"}, 1500);
	 $("#flash").animate({height: "170px"}, 1500);
	 $("#fields").animate({height: "225px"}, 1500);
	 $("#middle").animate({height: "260px"}, 1500);
	 $("#midtabcon").animate({height: "350px"}, 1500);
}

function slideMiddleBack(){
	 $("#join").animate({height: "136px"}, 1500);
	 $("#flash").animate({height: "340px"}, 1500);
	 $("#fields").animate({height: "350px"}, 1500);
	 $("#middle").animate({height: "390px"}, 1500);
	 $("#midtabcon").animate({height: "450px"}, 1500);
}

function slideMiddleBackMoreText(){
	 $("#flash").animate({height: "340px"}, 1500);
	 $("#fields").animate({height: "350px"}, 1500);
	 $("#middle").animate({height: "390px"}, 1500);
	 $("#midtabcon").animate({height: "450px"}, 1500);
}

function slideMoreText(){
	 $("#flash").html('');
	 $("#flash").width(1);	 
	 $("#join").height(10);
	 $("#join").width(480);
	 $("#fields").width(740);
 	 $("#fields").height(250);
 	 $("#message").width(740);
  	 $("#message").height(170);
}

function slideMoreTextBack(){
	$("#flash").width(450);
	 $("#join").height(125);
 	 $("#join").width(280);
	 $("#fields").width(300);
	 $("#fields").height(170);
 	 $("#message").width(280);
  	 $("#message").height(70);
}

function getRecorder(){
	$("#flash").load("getRecorder.php");
}

function startLoad(){
		$.get("update.php", function(data){
				if(data!='done'){;
					startLoad();
				}
				else{
					document.getElementById("tweetform").submit();
				}
		});
}

function textCounter(field){
	
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
		var ni = document.getElementById('count');
		ni.innerHTML = maxlimit - field.value.length;
}

function recordDone(username, user_video_number, video_id) {
	$("#flash").html("<br /><br /><br /><div id='servermessage' style='display:none;background-color:#F3FCFC;width:300px;border:1px solid #E1EFDB;padding:2px;font-size:12px;background-image:url(../images/tick.gif);background-position:-1px 50%;background-repeat:no-repeat;padding-left:12px;'>Video Recorded! - Please update your status.</div><input id='vidkey' name='vidkey' type='hidden' value='"+video_id+"' />");
	$("#servermessage").fadeIn('slow');
}

function performStatus(){
	$("#flash").html("<br /><br /><div id='loadingi'>Please Wait...<img src='images/loading.gif' alt='loading' /></div><div id='servermessage' style='display:none;background-color:#F3FCFC;width:300px;border:1px solid #E1EFDB;padding:2px;font-size:12px;background-image:url(../images/tick.gif);background-position:-1px 50%;background-repeat:no-repeat;padding-left:12px;'></div>");	
}

function endStatus(message){
	$("#servermessage").html(message +  ' Please update your status.');
	$("#loadingi").html('');
	$("#servermessage").fadeIn('slow');
}

function videoUpload(){
	new AjaxUpload('#vidup', {
		  action: 'getUploader.php',
		  name: 'file',
		  response: 'xml',
		  onSubmit: function() {
		    performStatus();
		    this.disable();
		  },
		  onComplete: function(file, response) {
			  if(response=='done'){
				  endStatus('Video Uploaded!');
			  }
			  else{
				  alert('Something went wrong, please try again.');
			  }
		  }
		});
}

function vidURLUpload(){

	var vidurl = document.getElementById('vidurl').value;
	performStatus();
	$.post("getUploader.php", { videourl:  vidurl, type: 'url'},
			  function(data){
				  if(data=='done'){
					 endStatus('Video Uploaded!');
				  }
				  else{
					  alert('Something went wrong, please try again.');
				  }
	});
}

function shackUpload(){
	new AjaxUpload('#shackup', {
		  action: 'shackupload.php',
		  name: 'shackfile',
		  data: {type: 'local'},
		  onSubmit: function() {
		    performStatus();
		    this.disable();
		  },
		  onComplete: function(file, response) {
			  if(response=='done'){
				 endStatus('Photo Uploaded!');
			  }
			  else{
				  alert('Something went wrong, please try again.');
			  }
		  }
	});
}

function shackURLUpload(){
	
	var shackurl = document.getElementById('shackurl').value;
	performStatus();
	$.post("shackupload.php", { shackurl:  shackurl, type: 'url'},
			  function(data){
				  if(data=='done'){
					 endStatus('Photo Uploaded!');
				  }
				  else{
					  alert('Something went wrong, please try again.');
				  }
	});
}

function docUpload(){
	new AjaxUpload('#docup', {
		  action: 'testscribe.php',
		  name: 'docfile',
		  data: {type: 'local'},
		  onSubmit: function() {
		    performStatus();
		    this.disable();
		  },
		  onComplete: function(file, response) {
			  if(response=='done'){
				  endStatus('Document Uploaded!');
			  }
			  else{
				  alert('Something went wrong, please try again.');
			  }
		  }
	});
}

function docURLUpload(){
	var scribdurl = document.getElementById('scribdurl').value;
	performStatus();
	$.post("testscribe.php", { shackurl:  scribdurl, type: 'url'},
			  function(data){
				  if(data=='done'){
					  endStatus('Document Uploaded!');
				  }
				  else{
					  alert('Something went wrong, please try again.');
				  }
	});
}

function pasteURL($which){
	if($which=='scribd'){
		$("#docuploader").toggle();
		$("#docurluploader").toggle();
		$("#docup").toggle();
		$("#scribdurl").toggle();
		$("#scribdq").toggle();
		$("#scribdq2").toggle();
	}else if($which=='shack'){
		$("#shackuploader").toggle();
		$("#shackurluploader").toggle();
		$("#shackup").toggle();
		$("#shackurl").toggle();
		$("#shackq").toggle();
		$("#shackq2").toggle();
	}else if($which=='video'){
		$("#viduploader").toggle();
		$("#vidurluploader").toggle();
		$("#vidup").toggle();
		$("#videourl").toggle();
		$("#videoq").toggle();
		$("#videoq2").toggle();
	}
}

function onHoverTable(){
	$('tablepub .td:magic').html('Nice');
}

function local(){
	document.getElementById('local').value = 'true';
}

function url(){
	document.getElementById('local').value = 'false';
}