/* JPEGCam v1.0.9 */
/* Webcam library for capturing JPEG images and submitting to a server */
/* Copyright (c) 2008 - 2009 Joseph Huckaby <jhuckaby@goldcartridge.com> */
/* Licensed under the GNU Lesser Public License */
/* http://www.gnu.org/licenses/lgpl.html */
window.webcam={version:'1.0.9',ie:!!navigator.userAgent.match(/MSIE/),protocol:location.protocol.match(/https/i)?'https':'http',callback:null,swf_url:'webcam.swf',shutter_url:'shutter.mp3',api_url:'',loaded:false,quality:90,shutter_sound:true,stealth:false,hooks:{onLoad:null,onComplete:null,onError:null},set_hook:function(name,callback){if(typeof(this.hooks[name])=='undefined')
return alert("Hook type not supported: "+name);this.hooks[name]=callback;},fire_hook:function(name,value){if(this.hooks[name]){if(typeof(this.hooks[name])=='function'){this.hooks[name](value);}
else if(typeof(this.hooks[name])=='array'){this.hooks[name][0][this.hooks[name][1]](value);}
else if(window[this.hooks[name]]){window[this.hooks[name]](value);}
return true;}
return false;},set_api_url:function(url){this.api_url=url;},set_swf_url:function(url){this.swf_url=url;},get_html:function(width,height,server_width,server_height){if(!server_width)server_width=width;if(!server_height)server_height=height;var html='';var flashvars='shutter_enabled='+(this.shutter_sound?1:0)+'&shutter_url='+escape(this.shutter_url)+'&width='+width+'&height='+height+'&server_width='+server_width+'&server_height='+server_height;if(this.ie){html+='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="webcam_movie" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+this.swf_url+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/></object>';}
else{html+='<embed id="webcam_movie" src="'+this.swf_url+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="webcam_movie" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" />';}
this.loaded=false;return html;},get_movie:function(){if(!this.loaded)return alert("ERROR: Movie is not loaded yet");var movie=document.getElementById('webcam_movie');if(!movie)alert("ERROR: Cannot locate movie 'webcam_movie' in DOM");return movie;},set_stealth:function(stealth){this.stealth=stealth;},snap:function(url,callback,stealth){if(callback)this.set_hook('onComplete',callback);if(url)this.set_api_url(url);if(typeof(stealth)!='undefined')this.set_stealth(stealth);this.get_movie()._snap(this.api_url,this.quality,this.shutter_sound?1:0,this.stealth?1:0);},freeze:function(){this.get_movie()._snap('',this.quality,this.shutter_sound?1:0,0);},upload:function(url,callback){if(callback)this.set_hook('onComplete',callback);if(url)this.set_api_url(url);this.get_movie()._upload(this.api_url);},reset:function(){this.get_movie()._reset();},configure:function(panel){if(!panel)panel="camera";this.get_movie()._configure(panel);},set_quality:function(new_quality){this.quality=new_quality;},set_shutter_sound:function(enabled,url){this.shutter_sound=enabled;this.shutter_url=url?url:'shutter.mp3';},flash_notify:function(type,msg){switch(type){case'flashLoadComplete':this.loaded=true;this.fire_hook('onLoad');break;case'error':if(!this.fire_hook('onError',msg)){alert("JPEGCam Flash Error: "+msg);}
break;case'success':this.fire_hook('onComplete',msg.toString());break;default:alert("jpegcam flash_notify: "+type+": "+msg);break;}}};

/* Author: 
	Max Nanis [max@maxnanis.com]
*/

var skypeText,
	shadow = 2,
	up = true;

function loadSVGIcons() {
	$(".icon").each(function (i) {
		var element = $(this)[0];
		
		if( $(this).hasClass('code') ) {
			var paper = new Raphael(element, 50, 50);
			c = paper.path('M24.36,18.424l-2.327,1.215c0.708,1.174,1.384,2.281,1.844,3.033l2.043-1.066C25.538,20.822,24.966,19.653,24.36,18.424zM19.143,14.688c0.445,0.84,1.342,2.367,2.274,3.926l2.414-1.26c-0.872-1.763-1.72-3.452-2.087-4.116c-0.896-1.621-1.982-3.108-3.454-5.417c-1.673-2.625-3.462-5.492-4.052-4.947c-1.194,0.384,1.237,4.094,1.876,5.715C16.73,10.147,17.991,12.512,19.143,14.688zM26.457,22.673l-1.961,1.022l1.982,4.598c0,0,0.811,0.683,1.92,0.213c1.108-0.469,0.81-1.706,0.81-1.706L26.457,22.673zM24.35,15.711c0.168,0.339,0.349,0.701,0.541,1.09l0.001-0.001l0.261,0.531l0.268,0.54l-0.001,0.001l0.013,0.024c0.614,1.248,1.188,2.421,1.562,3.186l0.271,0.558h1.986v-5.929H24.35zM20.973,20.191l-0.002,0.001l-0.283-0.474l-0.333-0.552l0.002-0.001c-0.91-1.521-1.586-2.68-2.017-3.455h-4.726l-0.336,0.583l-0.023,0.039l-3.065,5.307h11.659c-0.29-0.479-0.57-0.941-0.838-1.385L20.973,20.191zM3.231,21.613l3.437-5.902H2.083v5.929h1.133L3.231,21.613zM15.048,10.145c0-0.93-0.754-1.685-1.685-1.685c-0.661,0-1.231,0.381-1.507,0.936l2.976,1.572C14.97,10.725,15.048,10.444,15.048,10.145zM14.343,12.06l-3.188-1.684l-1.535,2.636l3.197,1.689L14.343,12.06zM3.192,26.886l-0.384,1.108v0.299l0.298-0.128l0.725-0.896l2.997-2.349l-3.137-1.657L3.192,26.886zM9.02,14.044l-4.757,8.17l3.23,1.706l4.728-8.186L9.02,14.044z').attr({fill: "#000", stroke: "none"}).scale(1.5);
		}		
		else if( $(this).hasClass('science') )
		{
			var paper = new Raphael(element, 50, 50);
			c = paper.path('M24.875,1.375H8c-1.033,0-1.874,0.787-1.979,1.792h1.604c1.102,0,2,0.898,2,2c0,1.102-0.898,2-2,2H6v0.999h1.625c1.104,0,2.002,0.898,2.002,2.002c0,1.104-0.898,2.001-2.002,2.001H6v0.997h1.625c1.102,0,2,0.898,2,2c0,1.104-0.898,2.004-2,2.004H6v0.994h1.625c1.102,0,2,0.898,2,2.002s-0.898,2.002-2,2.002H6v0.997h1.624c1.104,0,2.002,0.897,2.002,2.001c0,1.104-0.898,2.002-2.002,2.002H6.004C6.027,28.252,6.91,29.125,8,29.125h16.875c1.104,0,2-0.896,2-2V3.375C26.875,2.271,25.979,1.375,24.875,1.375zM25.25,8.375c0,0.552-0.447,1-1,1H14c-0.553,0-1-0.448-1-1V4c0-0.552,0.447-1,1-1h10.25c0.553,0,1,0.448,1,1V8.375zM8.625,25.166c0-0.554-0.449-1.001-1-1.001h-3.25c-0.552,0-1,0.447-1,1.001c0,0.552,0.449,1,1,1h3.25C8.176,26.166,8.625,25.718,8.625,25.166zM4.375,6.166h3.251c0.551,0,0.999-0.448,0.999-0.999c0-0.555-0.448-1-0.999-1H4.375c-0.553,0-1,0.445-1,1C3.374,5.718,3.822,6.166,4.375,6.166zM4.375,11.167h3.25c0.553,0,1-0.448,1-1s-0.448-1-1-1h-3.25c-0.553,0-1,0.448-1,1S3.822,11.167,4.375,11.167zM4.375,16.167h3.25c0.551,0,1-0.448,1-1.001s-0.448-0.999-1-0.999h-3.25c-0.553,0-1.001,0.446-1.001,0.999S3.822,16.167,4.375,16.167zM3.375,20.165c0,0.553,0.446,1.002,1,1.002h3.25c0.551,0,1-0.449,1-1.002c0-0.552-0.448-1-1-1h-3.25C3.821,19.165,3.375,19.613,3.375,20.165z').attr({fill: "#000", stroke: "none"}).scale(1.5);			
		}
		else if( $(this).hasClass('audio') )
		{
			var paper = new Raphael(element, 50, 50);
			c = paper.path('M4.998,12.127v7.896h4.495l6.729,5.526l0.004-18.948l-6.73,5.526H4.998z M18.806,11.219c-0.393-0.389-1.024-0.389-1.415,0.002c-0.39,0.391-0.39,1.024,0.002,1.416v-0.002c0.863,0.864,1.395,2.049,1.395,3.366c0,1.316-0.531,2.497-1.393,3.361c-0.394,0.389-0.394,1.022-0.002,1.415c0.195,0.195,0.451,0.293,0.707,0.293c0.257,0,0.513-0.098,0.708-0.293c1.222-1.22,1.98-2.915,1.979-4.776C20.788,14.136,20.027,12.439,18.806,11.219z M21.101,8.925c-0.393-0.391-1.024-0.391-1.413,0c-0.392,0.391-0.392,1.025,0,1.414c1.45,1.451,2.344,3.447,2.344,5.661c0,2.212-0.894,4.207-2.342,5.659c-0.392,0.39-0.392,1.023,0,1.414c0.195,0.195,0.451,0.293,0.708,0.293c0.256,0,0.512-0.098,0.707-0.293c1.808-1.809,2.929-4.315,2.927-7.073C24.033,13.24,22.912,10.732,21.101,8.925z M23.28,6.746c-0.393-0.391-1.025-0.389-1.414,0.002c-0.391,0.389-0.391,1.023,0.002,1.413h-0.002c2.009,2.009,3.248,4.773,3.248,7.839c0,3.063-1.239,5.828-3.246,7.838c-0.391,0.39-0.391,1.023,0.002,1.415c0.194,0.194,0.45,0.291,0.706,0.291s0.513-0.098,0.708-0.293c2.363-2.366,3.831-5.643,3.829-9.251C27.115,12.389,25.647,9.111,23.28,6.746z').attr({fill: "#000", stroke: "none"}).scale(1.5);
		}
		else
		{
			
		}
		
	});	
}

function makeImageModals() {
	$('#project #project_images a').fancybox();
}

function ajaxContactForm() {

	$("form#contact").submit(function(e) {
		var name      = $('input#name').val();
		var email     = $('input#email').val();
		var msg       = $('textarea#message').val();
			$.ajax({
				type: "POST",
				url: "contact",
				data: "name=" + name + "&email=" + email + "&message=" + msg,
				success: function(){
					$('form#contact').hide();
					$('#thankyou').fadeIn();
				}
			});
		e.preventDefault();
	});
}

function ajaxWeblife(feeds) {
	$.ajax({
		type: "GET",
		url: "api/v1/weblife",
		success: function(data){
			if(data == "error") {
				//maybe try again? but right now don't do anything because data was bad
				var lifeUL = $('#about ul#latest').delay(2000);
				if(lifeUL.length) {
					ajaxWeblife(lifeUL);
				}
			} else {
				//got a good result bad, so now fill the page with it
				feeds.empty();
				feeds.html(data).slideDown("slow", function() {
					skypeText = $('#skypeStatus a');
						if($('#skypeStatus a').html() == "Available") {
						 	setInterval(function() {
							 	if (shadow == 10) {
									up = false;
								} else if (shadow == 2) {
									up = true;
								}
								if(up) {
									shadow++;
								} else {
									shadow--;
								}
								skypeText.css({textShadow: '#92c032 0px 0px '+shadow+'px'});
						 	}, 200);						
						}
				});
			}
		},
		error: function(data) {
			//maybe try again? but right now don't do anything because data was bad
			var lifeUL = $('#about ul#latest').delay(2000);
			if(lifeUL.length) {
				ajaxWeblife(lifeUL);
			}
		}
	});
}

function ajaxFaces(type, feeds) {
	$.ajax({
		type: "GET",
		url: "api/v1/faces." + type,
		success: function(data){
			if(data == "error") {
				//maybe try again? but right now don't do anything because data was bad
				var faceDiv = $('#results #viewer').delay(2000);
				if(faceDiv.length) {
					ajaxFaces("newest", faceDiv);
				}
			} else {
				//got a good result bad, so now fill the page with it
				feeds.empty();
				feeds.html(data).slideDown("slow");
			}
		},
		error: function(data) {
			//maybe try again? but right now don't do anything because data was bad
			var faceDiv = $('#results #viewer').delay(2000);
			if(faceDiv.length) {
				ajaxWeblife("newest", faceDiv);
			}
		}
	});	
}

function sugarLoad() {
	$.ajax({
		type: "GET",
		url: "api/v1/sugar",
		success: function(data){
			if(data == "error") {
			} else {
				//got a good result bad, so now fill the page with it
				$('#results').empty().hide();
				$('#results').html(data);
				
				$('#results').fadeIn(800, function() {
						sugarSubmit();
				});
			}
		},
		error: function(data) {
		}
	});
}

function sugarSubmit() {

/*
	$('li#1').click(function(e) {
		$("input[name='answer']:nth(0)").attr("checked","checked");
	})	
	$('li#2').click(function(e) {
		$("input[name='answer']:nth(1)").attr("checked","checked");
	})	
	$('li#3').click(function(e) {
		$("input[name='answer']:nth(2)").attr("checked","checked");
	})	
	$('li#4').click(function(e) {
		$("input[name='answer']:nth(3)").attr("checked","checked");
	})	
	$('li#5').click(function(e) {
		$("input[name='answer']:nth(4)").attr("checked","checked");
	})	
*/

	$("form#sugar").submit(function(e) {
		var res = $("input[name='answer']:checked").val();
		var ligand_id = $("input[name='ligand_id']").val()
		if(res == undefined) {
			alert("You must enter an answer")
		} else {
			$.ajax({
				type: "POST",
				url: "api/v1/sugar",
				data: "answer=" + res + "&ligand_id=" + ligand_id,
				success: function(){
					sugarLoad()
				}
			});		
		}
		e.preventDefault();
	});

}


function snapped(msg) {
	webcam.reset();
	// If we are the faces page
	var faceDiv = $('#results #viewer');
	if(faceDiv.length) {
		ajaxFaces("newest", faceDiv);
	}
	$('#loading').fadeOut('slow');
}

$("document").ready(function() {
/* 	loadSVGIcons(); */
	makeImageModals();
	ajaxContactForm();

	// If we are the about page
	var lifeUL = $('#about ul#latest');
	if(lifeUL.length) {
		ajaxWeblife(lifeUL);
	}

	// If there is the home page canvas
	if($('#canvas').length) {
		init();
	}

	if($('#sugar').length) {
		sugarLoad();
		$('#skip').click(function(e) {
			sugarLoad();
		})
	}

	if($('#faces').length) {
		var name = '';
		var msg = '';
		var lat = 0;
		var long = 0;

		//navigation controls
		$('#flash').click(function() {
			$('#artiststatment').hide();
			$('#objcapp').hide();
			$('#flashvideo').slideToggle();
		})
		
		$('#statement').click(function() {
			$('#flashvideo').hide();
			$('#objcapp').hide();
			$('#artiststatment').slideToggle();
		})
		
		$('#app').click(function() {
			$('#flashvideo').hide();
			$('#artiststatment').hide();
			$('#objcapp').slideToggle();
		})

		// If we are the faces page
		var faceDiv = $('#results #viewer');
		if(faceDiv.length) {
			ajaxFaces("newest", faceDiv);
		}

/*
		navigator.geolocation.getCurrentPosition(function(position) {
			lat = position.coords.latitude;
			long = position.coords.longitude;
			webcam.set_api_url( 'api/v1/faces?name='+name+'&message='+msg+'&latitude=' + lat + '&longitude=' + long );		
		});
*/

		webcam.set_swf_url( 'flash/webcam.swf' );
		webcam.set_api_url( 'api/v1/faces?name='+name+'&message='+msg+'&latitude=' + lat + '&longitude=' + long );
		webcam.set_quality( 100 );
		webcam.set_shutter_sound( false );
		webcam.set_stealth( true );
		webcam.set_hook( 'onComplete', 'snapped' );
		$('#webcam').html(webcam.get_html(320, 240, 640, 480));
		$('#config').click(function() {
			webcam.configure()
		})
		$('#snapshot').click(function() {
			name = $('input#name').val();
			msg  = $('textarea#message').val();
			webcam.set_api_url( 'api/v1/faces?name='+name+'&message='+msg+'&latitude=' + lat + '&longitude=' + long );		

			webcam.snap();
			$('#loading').fadeIn('fast');
		})
		$('li#rand').click(function() {
			ajaxFaces("random", $('#results #viewer'));
			console.log("asdfsdf" + lat);
		})
		$('li#new').click(function() {
			ajaxFaces("newest", $('#results #viewer'));
		})
		$('li#old').click(function() {
			ajaxFaces("oldest", $('#results #viewer'));
		})
	}
});
