$(document).ready(function(){	
	
	var site_url = "http://www.nosejobs.info/nosejobtool";
	
	$('.top_chatbox').hide();
	$('#container_btm1, #container_btm2').hide();
	
	/* HIDE MESSAGE */
	$('#message').hide();
	
	/* SAVE & RESET BUTTONS */
	$('.tool_buttons a').each(function() {
		$(this).mouseover(function() {
			$(this).css('opacity', .6);
		}).mouseout(function() {
			$(this).css('opacity', 1);
		})
	})
	
	/* AJAX LOAD NOSES */
	$('.noses_female_link').click(function() {
		var container = $('#noses_female');
		if ($(this).hasClass('noses_female_link_shown')) {
			container.hide()
			$(this).removeClass('noses_female_link_shown');
			$(this).css('backgroundImage', 'url("./images/plus_bg.gif")');
			$('#container_btm1').hide();
		} else if ($(this).hasClass('noses_female_link_loaded')) {
			container.show();
			$(this).css('backgroundImage', 'url("./images/minus_bg.gif")');
			$(this).addClass('noses_female_link_shown');
			$('#container_btm1').show();
		} else {
			$('#container_btm1').show();
			$(this).css('backgroundImage', 'url("./images/minus_bg.gif")');
			$(this).addClass('noses_female_link_shown');
			$(this).addClass('noses_female_link_loaded');
			
			container.html('<div class="center"><img src="./images/loader.gif" /></div>');
			action = "./home/get_noses_female";
			$.post(action, { 
			},
				function(data) {
					//console.log(data);
					container.html(data);	
					setDraggable();						
				}
			);
		}
	});
	
	$('.noses_male_link').click(function() {
		var container = $('#noses_male');
		if ($(this).hasClass('noses_male_link_shown')) {
			container.hide()
			$(this).removeClass('noses_male_link_shown');
			$(this).css('backgroundImage', 'url("./images/plus_bg.gif")');
			$('#container_btm2').hide();
		} else if ($(this).hasClass('noses_male_link_loaded')) {
			container.show();
			$(this).css('backgroundImage', 'url("./images/minus_bg.gif")');
			$(this).addClass('noses_male_link_shown');
			$('#container_btm2').show();
		} else {
			$(this).css('backgroundImage', 'url("./images/minus_bg.gif")');
			$(this).addClass('noses_male_link_shown');
			$(this).addClass('noses_male_link_loaded');
			$('#container_btm2').show();
			
			container.html('<div class="center"><img src="./images/loader.gif" /></div>');
			action = "./home/get_noses_male";
			$.post(action, { 
			},
				function(data) {
					container.html(data);	
					setDraggable();					
				}
			);
		}
	});	
	
	$('.close_video_link').click(function() {
		var container = $('#video_container');
		if ($(this).hasClass('close_video_link_shown')) {
			container.hide()
			$(this).removeClass('close_video_link_shown');
			$(this).css('backgroundImage', 'url("./images/plus.gif")');
		} else {
			container.show();
			$(this).css('backgroundImage', 'url("./images/close.gif")');
			$(this).addClass('close_video_link_shown');
		} 
	});
	
	/* CONTROL THE DRAGGABLES */	
	var max_width = 500; //max width of boobs
	var lastClicked = null;
	var lastResizeContainer = null;
	function initializeClick() {
		$('.nose').click(function() {
			lastClicked = $('img', this);
			//$(this).resizable({aspectRatio: 'TRUE'});
			w = $(lastClicked).css('width').replace('px', '');
			h = $(lastClicked).css('height').replace('px', '');
			aspect =  w / h;
			$(this).css('height', h+'px');
			$(this).resizable({aspectRatio: aspect, resize: function() {
				var width = $(this).css('width').replace('px', '');
				$('img', this).css('width', width +'px');
				var value = (width / max_width) * 99;
				//$('#slider1').slider('option', 'value', value);
			}});
			$(this).hover(function() {
				$('.ui-resizable-handle').show();
				//$('.ui-resizable-handle').fadeTo(1, 1);
			}, function() {
				$('.ui-resizable-handle').hide();
				//$('.ui-resizable-handle').fadeTo('slow', 0);
			});		
		});
	}
	
	var lastX = "0";
	var lastY = "0";
	function resetAll() {
		$('.nose').each(function() {
			//console.log($(this).attr('height'));
			$(this).css('height', $(this).attr('height'));
			$(this).css('width', '78px');		
			$(this).resizable('destroy');
			$('.ui-draggable').css({'position' : 'relative', 'left' : 0, 'top' : 0});
			$('.ui-draggable img').css('width', '78px');
			$('img', this).css({'position' : 'relative', 'left' : 0, 'top' : 0});
			$('img', this).css('width', '78px');
			$('img', this).css('opacity', '1');			
		});
		//$('#slider2').slider('option', 'value', 99);
	}
	
	function setDraggable() {
		var count = 1;
		$('.nose').each(function() {
			this.id = 'nose'+ count;
			$(this).attr('height', $('img', this).css('height'));
			$(this).draggable({
				opacity : 0.6, 
				cursor : 'move',
				drag : function() {
					$('img', this).css({'position' : 'absolute'});
				}
             });
			$('#noses_female, #noses_male').droppable({
			  drop: function(event, ui) {
				resetAll();
			  }
			});
			$('#tool_image').droppable({
			  //out: function(event, ui) {
			  //	$('.ui-draggable img').css('width', '83.33px');
			  //},
			  drop: function(event, ui) {	
				if (lastClicked == null)
					var width = 78;
				else 
					var width = $(lastClicked).css('width').replace('px', '');				
				var value = (width / max_width) * 99;
				//$('#slider1').slider('option', 'value', value);
			}
			});
			count++;
		});
		
		initializeClick();
	}
	$('#reset_everything').click(function() {
		resetAll();
		$('#image_placeholder').css('width', max_width +'px');
	});
	
	/* DEAL WITH THE SLIDERS */
	$('#increase').click(function() {
		width = $(lastClicked).css('width').replace('px', ''); 
		width *= 1.5;
		$(lastClicked).css({'position' : 'absolute', 'width' : width +'px'});
	});
	
	/*
		$('#slider1').slider(
			{	
				slide : function()
				{
					if (lastClicked != null) {
					var value = $(this).slider('option', 'value') + 1;
					var width = $(lastClicked).css('width').replace('px', '');				
					var new_width = (max_width / 100) * value;
					$(lastClicked).css('width', new_width +'px');	
					}		
				}
			}
		);	
	
		
		$('#slider2').slider(
			{	
				value: 99,
				slide : function()
				{
					var value = $(this).slider('option', 'value') + 1;
					var opacity = value / 100;
					//var width = $('#image_placeholder').opacity.replace('px', '');				
					//var new_width = (max_width / 100) * value;
					console.log(lastClicked);
					$(lastClicked).css('opacity', opacity);			
				}
			}
		);	
	*/
	
	/* SAVE IMAGE */
	$('#save_image').click(function() {
		imageY = $('#image_placeholder').offset().top;
		imageX = $('#image_placeholder').offset().left;
	 	calcX = $(lastClicked).offset().left - imageX;
		calcY = $(lastClicked).offset().top - imageY;
		
		//console.log($(lastClicked).css('opacity'));
		
		$('#tool_top').hide();
		$('#sliders').hide();
		$('#video').hide();
		$('#message').show().html('<img src="./images/loader.gif" /> Loading...');
		action = "./home/save_image";
		$.post(action, { 
			file : $('#image_placeholder').attr('src'),
			boobs : $(lastClicked).attr('src'),
			imageWidth : $('#image_placeholder').css('width'), 
			imageHeight :  $('#image_placeholder').css('height'),
			boobsWidth : $(lastClicked).css('width'),
			boobsHeight : $(lastClicked).css('height'),
			boobsX : calcX,
			boobsY : calcY,
			opacity : $(lastClicked).css('opacity')
		},
			function(data) {
				resetAll();
				$('#noses_container').hide();
				html = '&lt;a href=&quot;http://www.boobify.me&quot; target=&quot;blank&quot;&gt;&lt;img src=&quot;'+ site_url +'/uploaded/saved/'+ data +'&quot; alt=&quot;Give Yourself an Online Boob Job - Boobify.Me&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;';
				
				img_code = '[IMG]'+ site_url +'/uploaded/saved/'+ data +'[/IMG]';
				page_url = site_url +'/home/view/'+ data;
				image_url = site_url +'/uploaded/saved/'+ data;
				
				$('#image_placeholder').attr('src', './uploaded/saved/'+ data);
				$('#message').show().html('<span class="saved">Image saved!</span><div id="allow_share"><br />Boobify.me will NOT display your image on our site unless you give us permission to. Allow us to share in galleries section?<div class="clear" style="height: 10px">&nbsp;</div><label>Title*:</label><input type="text" maxlength="50" name="file_title" id="file_title" class="highlight"><div class="clear" style="height: 10px">&nbsp;</div><label>Email*:</label><input type="text" maxlength="50" name="email" id="email" class="highlight"><div class="clear" style="height: 10px">&nbsp;</div><label>Description:</label><input type="text" name="file_description" id="file_description" class="highlight"><div class="clear" style="height: 10px">&nbsp;</div><label>&nbsp;</label><a href="javascript:;" id="share_image" class="blocklink_green">Yes! Save & Share!</a></div><br /><a href="./home/view/'+data+'" class="view_image">View image</a><br /><a href="./home/save_image_desktop/'+data+'" class="save_image">Download Image</a><br /><a href="'+site_url+'" class="do_another">Do Another</a><br /><a rel="nofollow" class="facebook" href="http://www.facebook.com/share.php?u='+encodeURIComponent(page_url)+'&t='+encodeURIComponent("Check out this Boobify.Me pic!")+'" onclick="return fbs_click()" target="_blank" class="fb_share_link">Share on Facebook</a><div class="clear">&nbsp;</div><br /><h2><b>Embed & Share</b></h2><script type="text/javascript">var addthis_pub="jawnknee";</script><a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="images/share.gif" /></a><script type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script><label>HTML for Websites</label><input type="text" class="select_all" value="'+html+'" /><div class="clear">&nbsp;</div><label>IMG Code for Forums & Message Boards</label><input type="text" class="select_all" value="'+img_code+'" /><div class="clear">&nbsp;</div><label>URL for Email & IM</label><input type="text" class="select_all" value="'+page_url+'" /><div class="clear">&nbsp;</div><label>Direct Link for Layouts</label><input type="text" class="select_all" value="'+image_url+'" /><div class="clear">&nbsp;</div><label>Send it VIA E-Mail <small>(seperate with commas)</small></label><input type="hidden" name="filename" id="filename" value="'+data+'" /><input type="text" style="width: 165px" id="send_email" value="" /><a href="javascript:;" class="submit_link" id="email_link">GO</a><div class="clear">&nbsp;</div>');
				//top.location = 'home/view/23';
				//console.log(data);
				function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}
				initializeShare();	
				initializeEmail();
				$('.select_all').each(function() {
					$(this).click(function() {
						$(this).select();
					});		
				});		
			}
		);
	});
	
	
	/* SHARE IMAGE */
	function initializeShare() {
		$('#share_image').click(function() {
			title = $('#file_title').val();
			email = $('#email').val();
			description = $('#file_description').val();
			
			if (jQuery.trim(title) == "") {
				alert('Title and Email Required');
			} else if (jQuery.trim(email) == "") {
				alert('Title and Email Required');
			} else {
				action = "./home/share_image";
				$.post(action, { 
					file : $('#image_placeholder').attr('src'),
					title : title,
					email : email,
					description : description
				}, function(data) {
						//console.log(data);
						$('#allow_share').fadeTo('slow', 0, function() {
							$(this).hide();
							$(this).html('<b>Thanks for the share!</b>');
							$(this).show().fadeTo('slow', 1);
						});
					}
				);
			}
		});
	}
	
	/* EMAIL PAGE */
	function initializeEmail() {
		$('#email_link').click(function() {
			action = site_url +"/home/send_emails";
			$.post(action, { 
				email : $('#send_email').val(),
				link : site_url +'/home/view/'+ $('#filename').val()
			}, function(data) {
					//console.log(data);
					//good
					//if (data != 'success') {
						$('#message').prepend('<span style="color: #0000FF">Successfully sent to friends!</span><br />');
						$('#embed_bar').append('<span style="color: #0000FF">Successfully sent to friends!</span>');
					//} 
					//bad
					//else {						
						//$('#embed_bar').append('ERROR: Check addresses. Email was not sent');
					//}
				}
			);
		});
	}
	initializeEmail();
	
	/* SELECT ALL INPUTS */
	$('.select_all').each(function() {
		$(this).click(function() {
			$(this).select();
		});		
	});
	
	/* VOTING */
	$('.star').rating({ 
	  callback: function(value, link){ 
			//alert(value);
	    	action = site_url + '/home/vote';
			$.post(action, { 
				rating : value,
				image_id : $('#image_id').val()			
			}, function(data) {
				//console.log(data);
				if (data != "error") {
					$('#current_rating').html('('+data+'/5)');
					$('#rating_stars').html('Thanks for voting!');
					top.location = site_url +'/home/random'; //redirect
				}
			});
	  } 
	});
	

});