function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function onclickHandler(){$('*').css({color:'red !important'});}


$(document).ready(function(){
	
	
	
	$('.help').tooltip({
		timein:300,
		out:100
	}); //Creates tooltips for all the "?" next to the h2s
	
	
	if(readCookie('noIntro')){
		$('#close_intro,#intro_hide,#intro_container').css({display:'none'});
	}
	else{
		var close_height = $('#intro_hide').height();
		$("#close_intro").click(function(){
			createCookie('noIntro','noIntro',30)
        	$('#intro_hide').css({height:close_height+'px'});
			$('#intro_container').fadeOut(500,function(){
				$('#intro_hide').slideUp(750);
				$('#close_intro').fadeOut(1000);
			});
      });
	}
 
	
	$('.tooltip_generated').corner();
	
	//Default everything to the first option if its a radio input
	$("ul li:first-child input:radio").attr("checked", "checked");
	
	//Yes and no functions
	$('.options').next('ul').css({display:'none'}); //If you put a container with the the class of "options" it will look for the next UL and hide it.
	$('.cancel').css({display:'none'}); //Cancel is hidden until the options are visible.
	$('.options').next('ul').children('li').children('input').attr('checked', ''); //Makes sure all the possiblilities are unchecked from the start.
	
	$('.options li:last-child input').click(function(){ //Last child equals yes.
		var fade_speed = 400;
		var options = $(this).parent('li').parent('ul').attr('id');
		var options_choices = $(this).parent('li').parent('ul').next('ul').attr('id');
		//Start the madness
		$('#'+options_choices).children('li').children('input:first:radio').attr('checked', 'checked'); //Make the first radio button selected
		$('#'+options_choices).addClass('checked'); //Add the checked class
		$('#'+options).addClass('checked').fadeOut(fade_speed,function(){
			$('#'+options_choices).fadeIn(fade_speed);
			$(this).prev('h2').children('label.cancel').fadeIn(fade_speed, function(){
				$(this).click(function(){
					$(this).fadeOut(fade_speed);
					$('#'+options_choices).fadeOut(fade_speed, function(){
						$('#'+options_choices).removeClass('checked');
						$('#'+options).fadeIn(fade_speed); //Fading options back in...
						$('#'+options_choices).children('li').children('input:first').attr('checked', ''); //Change the checkmarks to blank
						$('#'+options).children('li').children('input:first').attr('checked', 'checked'); //Revert back the default checks
					});
				});
			});
		});
	});
	
	var fade_speed_2 = 400;
	$('#jquery_plugins_choice').css({display:'none'}); //Hide the plugin choice until they select yes for the jQuery option
	
	$('#jquery_plugins_choice').prev('h2').css({display:'none'}); //Hide the title as well
	
	$('#jquery').click(function(){
		if($('#jquery_options').hasClass('checked')){ //If it was checked then fade in the plugins
			$('#jquery_plugins_choice').animate({margin:'+=0'},fade_speed_2,function(){ //The animate function is there for timing with jQuery.
				$(this).fadeIn(fade_speed_2);
			});
			$('#jquery_plugins_choice').prev('h2').animate({margin:'+=0'},fade_speed_2,function(){ //Same stuff as above.
				$(this).fadeIn(fade_speed_2);
			});
		}
		else {} //No else!
	});
	
	$('#jquery').prev('h2').children('label').click(function(){ //If you click cancel
		if($('#jquery_options').hasClass('checked')) {
			$(this).removeClass('checked'); //Remove the checked class so it hides the options again.
			$('#jquery_plugins_choice').fadeOut(fade_speed_2); //Fade out the plugins.
			$('#jquery_plugins_choice').prev('h2').fadeOut(fade_speed_2); //Fade out the title
			$('#jquery_plugins_choice li input:first').attr('checked','checked'); //Change the choice from yes to no
			$('#jquery_plugins').fadeOut(fade_speed_2); //Fade out the entire thing now.
		}
		else {}
	});
	$('.jquery_plugin_h2').children('label.cancel').click(function(){
		$('#jquery_plugins li input').attr('checked','');
	});
	
	
	//My Email hidden from spammers

	$.post('js/ajax/mailto.php',{
	  pass: "secret"
	},function(txt){
	  $('.myemail').html(txt);
	});
	
	//AJAX
	$(".project_namer").each(function(i){
		setClickable(this, i);
	});

	function setClickable(obj, i) {
	$(obj).click(function() {
		//var textarea = '<div><textarea class="editable_area">'+$(this).html()+'</textarea>';
		var textarea = '<div><input type="text" class="editable_area" value="'+$(this).html()+'" />';
		var button	 = '<div><input type="button" value="" class="save" /> <input type="button" value="" class="cancel_project" /></div></div>';
		var revert = $(obj).html();
		$(obj).after(textarea+button).remove();
		$('.editable_area').select();
			$('.save').click(function(){saveChanges(this, false, i);});
			$('.cancel_project').click(function(){saveChanges(this, revert, i);});
		})
		.mouseover(function() {
			$(obj).addClass("editable");
		})
		.mouseout(function() {
			$(obj).removeClass("editable");
		});
	}//end of function setClickable

	function saveChanges(obj, cancel, n) {
	if(!cancel) {
	var t = $(obj).parent().siblings(0).val();
	$.post("js/ajax/project_save.php",{
	  project_save: t,
	  n: n
	},function(txt){
		//alert( txt);
		//alert(t);
	});
	}
	else {
		var t = cancel;
	}
	if(t=='') t='deploy';
		$(obj).parent().parent().after('<p>'+t+'</p>').remove();
		setClickable($("p").get(n), n);
	}
	
	
	if (window.fluid) //For fluid users
	{
		var deployed = location.pathname.substring(1);
		
		//alert(deployed);
		
		
		function contactinfo(){
			alert("Contact Info:\ninfo@projectdeploy.org");
		}
		function licenseinfo(){
			alert("Copyright (c) 2008 Oscar Godson\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.");
		}
		
		/*function renameproject(){
			$(".project_namer").each(function(i){
				setClickable(this, i);
			});
		} */
		
		window.fluid.addDockMenuItem("Contact Info", contactinfo);
		window.fluid.addDockMenuItem("License Info", licenseinfo);
		//window.fluid.addDockMenuItem("Rename Project", renameproject);
		
		if(deployed == 'index.php'){
			/*window.fluid.showGrowlNotification({
			    title: "Welcome to Deploy*", 
			    description: "Simply fill out the form and press Deploy!", 
			    priority: 1, 
			    sticky: false,
			    identifier: "foo",
			});*/
		}
		else if (deployed == 'deploy.php') {
			window.fluid.showGrowlNotification({
			    title: "Deploy Finished", 
			    description: "Your Deploy* download package has been created.", 
			    priority: 1, 
			    sticky: false,
			    identifier: "foo",
			});
			
			window.fluid.dockBadge = "1";

			window.fluid.playSoundNamed("Glass");
		}
		else {
			//alert('no');
		}
	}
	
});
