	$(document).ready(function() {
		var cropInstance;
		var cropX, cropY, cropX2, cropY2, cropW, cropH, cropS, cropSrc;
		
		function showCoords(c){
			cropX = c.x;
			cropY = c.y;
			cropX2 = c.x2;
			cropY2 = c.y2;
			cropH = $('#main_image_holder').height();
			cropW = $('#main_image_holder').width();
			cropSrc = $('#main_image').attr('src');
		};
		
		$('#btnMakeAvatar').click(function(){
			cropInstance = $.Jcrop('#main_image', {setSelect: [ 100, 100, 50, 50 ]});
			cropInstance.setOptions({ aspectRatio: 1, minSize: [200, 200 ], onChange: showCoords });
			$('#submit_avatar').show();
		});
		
		$('#submit_avatar').click(function(){
			$.post("/makeAvatar", { 'cropX': cropX, 'cropY': cropY, 
									'cropX2': cropX2, 'cropY2': cropY2,
									'cropW': cropW, 'cropH': cropH, 'cropSrc': cropSrc },
				function(data){
					cropInstance.destroy();
					$('#submit_avatar').hide();
			});
		});
		
		$('#show_hide_tools').click(function() {
			if($('#tool_drawer').css('display') == 'none'){
				$.post('/setUserPref', { 'keep_tools_open': 'yes' })
				$('#tool_drawer').css('display', 'block');
				$('#hidden_tools').css('display', 'block');
				$('#show_hide_tools img').attr('src', '/images/toolbar_close.png');
			}else{
				$.post('/setUserPref', { 'keep_tools_open': 'no' })
				$('#tool_drawer').css('display', 'none');
				$('#hidden_tools').css('display', 'none');
				$('#show_hide_tools img').attr('src', '/images/toolbar.png');
			}
		});
		
		$('#btn_like_unlike').click(function() {
			if ($('#img_like_unlike').attr('src') == '/images/like.png'){
				$.post("/photo/like", { 'photo_id': $('#image_id').val() }, function(data){ }); 
				$('#img_like_unlike').attr('src', '/images/liked.png');
			} else {
				$.post("/photo/unlike", { 'photo_id': $('#image_id').val() }, function(data){ });
				$('#img_like_unlike').attr('src', '/images/like.png');
			}
		});
		
		$('#btn_open_search').click(function(){
			if($('#search_bar').css('display') == 'none'){
				$('#search_bar').css('display', 'block');
				$('#search_bar').css('display', 'block').slideDown();
			}else{
				$('#search_bar').css('display', 'none');
			}
		});
		
		$('#txtSearch').click(function(){
			$(this).val('');
		});
		
		$('#btn_add_tag').click(function(){
			$('#tag_box').append('<span class="tag">' + $('#txt_tag').val() + '</span>');
			$('#txt_tag').val('');
		});
		
		$('.tool').tooltip({ 
		    track: true, showURL: false,
			delay: 0
		});
		
		$('.toolbox').click(function(){
			var thing = $(this).attr('href');
			if(thing != "#avatar"){
				if(cropInstance){
					cropInstance.destroy();
				}
			}
			$("#tool_drawer div").removeClass('open');
			$(thing).addClass('open');
		});
		
		$(window).load(function(){
			if($(window).height() > 500){
				while ((Math.floor($('#main_image_holder').offset().top) + Math.floor($('#main_image_holder').height()) + 120) > $(window).height()){
					var oldWidth = $('#main_image_holder').width();
					$('#main_image_holder').width(oldWidth - 1);
				}
			}	
		});
		
	});
