// Added this to keep track of which options section is visible
var visible_radio_options = [];

	/* @begin cookie script */
	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'});}
	/* @note end Cookie Script */
	
	$(document).ready(function(){
		/* @note start read cookie and hide the intro text if it is set */
		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);
				});
	      });
		}
		/* @note end check cookie */
		
		/* @version 1.4 */
		$('.help').each(function(){
			var toolTip = $(this).text();
			$(this).parent().append("<sup class=\"help red {tooltip: {text:'"+toolTip+"'}}\">?</sup>");
			$(this).remove();
		});
		
		/* @note tooltip start */
		$('.help').tooltip({
			timein:100,
			out:100
		}); //Creates tooltips for all the "?" next to the h2s
		/* @note tooltip end */
		
		/* @version 1.4 */
		//Default everything to the first option if its a radio input
		var fadeSpeed = 200;
		$("ul li:first-child input:radio").attr('checked','checked');
		$('ul[class*=options]').css({display:'none'});
		$('ul[class*=options]').prev('h2').css({display:'none'});
		$('.cancel').css({display:'none'});
		var optionIdSet = 1;
		$('ul.options').each(function(){
			var optionId = optionIdSet++;
			$(this).attr('id','optionId'+optionId);
		});
		
		/*================
		@note: TechWraith
		=================*/
		
		
		$('ul[class*=options]').children().children().click(function(){
			var conOp = $(this).attr('value');
			
			switch($(this).attr('type'))
			{
				default:
					console.log('Not radio or checkbox.');
					break;
					
				
				case 'radio':
				
					$(this).parent().parent().parent().children('h2').children().click(function() {
						if($('.conditional_options-'+conOp).hasClass('selected')) {
							$('.conditional_options-'+conOp).removeClass('selected').hide(fadeSpeed).prev().hide(fadeSpeed).next().children().children().attr('checked','');
						}
					});
				

					if (visible_radio_options.length > 0)
					{
						visible_radio_options[$(this).attr('name')] = conOp;
						$('.conditional_options-'+conOp).addClass('selected').show(fadeSpeed).prev().show(fadeSpeed);
					}
					// Comment by Robin
					// This seems to be false the second time around
					//else if (visible_radio_options[$(this).attr('name')] != conOp) 
					else if (!$('.conditional_options-'+conOp).hasClass('selected')) // Replaced by Robin
					{
						$('.conditional_options-'+visible_radio_options[$(this).attr('name')]).removeClass('selected').hide(fadeSpeed).prev().hide(fadeSpeed);
						visible_radio_options[$(this).attr('name')] = conOp;
						$('.conditional_options-'+conOp).addClass('selected').show(fadeSpeed).prev().show(fadeSpeed);
					}
					
					break;
					
				case 'checkbox':
					$(this).parent().parent().parent().children('h2').children().click(function() {
						if($('.conditional_options-'+conOp).hasClass('selected')) {
							$('.conditional_options-'+conOp).removeClass('selected').hide(fadeSpeed).prev().hide(fadeSpeed).next().children().children().attr('checked','');
						}
					});
					
					if($('.conditional_options-'+conOp)) {
						if($('.conditional_options-'+conOp).hasClass('selected')) {
							$('.conditional_options-'+conOp).removeClass('selected').hide(fadeSpeed).prev().hide(fadeSpeed);
						}
						else{
							$('.conditional_options-'+conOp).addClass('selected').show(fadeSpeed).prev().show(fadeSpeed);
						}
					}
					break;
				
			}
			
		});
		/*===================
		@note: TechWraith end
		=====================*/
		$('input:radio').click(function(){
			$(this).parent().prev().children().addClass('no');
			var radioType = $(this).parent().text();
			var options = '#'+$(this).parent().parent().siblings('ul').attr('id');
			if(radioType == ' Yes' && $(this).parent().prev().text() == ' No'){ //Notice the space before the ' Yes' and ' No'!
				//If it is a yes or no question...
				if($(options).hasClass('options')){
					$(options).prev().append('<label class="cancel">x cancel</label>');
					$(this).parent().parent().fadeOut(fadeSpeed).prev().fadeOut(fadeSpeed,function(){
						$(this).parent().children('.options').fadeIn(fadeSpeed).prev().fadeIn(fadeSpeed).children('label').fadeIn(fadeSpeed).click(function(){
							$('.no').attr('checked','checked').removeClass('no');
							$(this).parent().parent().children('.options').fadeOut(fadeSpeed).prev().fadeOut(fadeSpeed,function(){
								$(options).prev().prev().fadeIn(fadeSpeed).prev().fadeIn(fadeSpeed,function(){
									$(this).parent().children('.options').each(function(){
										if($(options).children().children().attr('type') == 'checkbox'){
											$(this).children().children().attr('checked','');
										}
										else if($(options).children().children().attr('type') == 'radio'){
											$(this).children('li:first-child').children().attr('checked','checked');
										}
										else{
											alert('At this time Deploy* only supports radio and checkbox inputs');
										}
									});
								});
							});
						});
					});
				}
				else{
					alert("Error:\nEither you have forgotten to add a class of options to the subset of options to your yes or no question or have your unordered lists mixed up.");
				}
			}
		});
		/* end @version 1.4 */
		
		//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){
		});
		}
		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);
			
			
			
			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.");
			}

			
			window.fluid.addDockMenuItem("Contact Info", contactinfo);
			window.fluid.addDockMenuItem("License Info", licenseinfo);
			
			if(deployed == 'index.php'){

			}
			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 {
			}
		}
		
	});