// Status box
function status(msg){
	jQuery.roar('Status Update', msg);
}

// Update progress bars on /members/info and builder
function updateInfo(current,value) {
	status('Your information has been saved');
	var val = value.split('default-');
	var answered;
	var id = $(current).attr('id');
	
	if(val[1]){
		$('.'+current.attr('id')).html(val[1]).removeClass('blue').addClass('red');
		answered = false;
	}
	else{
		$('.'+current.attr('id')).html(value).removeClass('red').addClass('blue');	
		answered = true;
	}
	
	if(parent.document.getElementById(id))
	{
		var child = parent.document.getElementById(id);
		if (answered)
			$(child).removeClass('not-answered');
		else
			$(child).addClass('not-answered');
	}
	
	$("div[id*='cat_']").each(function(){
		
		var answers = $(this).find('.blue').length;
		var not_answered = $(this).find('.red').length;

		if (answers || not_answered) 
		{
			var total = answers + not_answered;
			var percent = (answers / total) * 100;
			
			$(this).children("[id*='c_progress_']").progressBar(percent);
		}
		
	});
}

$(document).ready(function(){
	var addClass = '';
	var oldText  = '';
	$(".rounded").corner();				
	$(document).pngFix();
	$(".alternating tr:odd").addClass('light_blueb');
	$(".rich_editor").tinymce();
	$("#floatmenu").height($('#builder_content').height() - '500');
	$(".sticky").stickyfloat();
	//input fixes
	$(":text").addClass('input');
	$(".tiny :input").addClass('tinytext');

	$('.ajaxForm').ajaxForm({target: '#ajaxFormDiv'});
	
   	// Hover buttons
	$('.header_button').hover(
			function(){
				$(this).attr('src','/images/'+$(this).attr('id')+'_sel.jpg');
			},function(){
				$(this).attr('src','/images/'+$(this).attr('id')+'.jpg');
			}
		);
	
	// Open external links in a new window
	$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function(){
		$(this).attr('target','_blank');
	});

   $(".edits").live('mouseover',function(){
	   $(this).editable(window.location.href,{
	      indicator : "Saving...",
	      tooltip : 'Click to edit...',
		  cssclass : 'text_input check_default',
		  style  : "inherit",
		  callback : function(value,settings) {
		  	updateInfo($(this),value);
		  }
	   });
	   }
   );
   
   $(".editb").editable(window.location.href,{
      indicator : "Saving...",
      tooltip : 'Click to edit...',
	  cssclass : 'text_input tinytext',
	  onblur : 'submit',
	  width : '90%',
	  style  : "inherit",
	  callback : function(){
	  	status('Your information has been saved');
	  }
   });
   
	$(".edit").editable(window.location.href,{
      indicator : "Saving...",
      tooltip : 'Click to edit...',
	  cssclass : 'text_input tinytext',
	  height : 15,
	  width : '90%',
	  style  : "inherit",
	   callback : function(){
	  	status('Your information has been saved');
	  }
   });
   
    $(".edit_areas").editable(window.location.href,{
      type : 'mce',
      submit : 'OK',
	  cancel : 'Cancel',
	  width : '100%',
      indicator : "Saving...",
      tooltip : 'Click to edit...',
	  callback : function(value,settings) {
	  	updateInfo($(this),value);		
	  }
   });
   
   	$('.edit_upload').editable(window.location.href,{
		type      : 'ajaxupload',
        submit    : 'Upload',
		cssclass : 'text_input',
        cancel    : 'Cancel',
        tooltip   : "Click to upload..."
	});
	
   $(".edit_area").editable(window.location.href,{
      type : 'mce',
      submit : 'OK',
	  cancel : 'Cancel',
      indicator : "Saving...",
      tooltip : 'Click to edit...',
	  callback : function() {
		status('Your information has been saved');
	  }
   });  
   
   $(".check_default input").live('mouseover',function(){if($(this).parent().parent().hasClass('red'))$(this).val('')});
   
   // add hover, onclick events to sidebar items
   $('.sidebar').children('.sidebaritem').hover(
   	function(){
   		$(this).addClass('hover');
   	},
	function(){
		$(this).removeClass('hover');
	}
   );
   
   $('.sidebaritem').click(function(){
   	var url = $(this).children('.more').attr('href');
	if (url)
		window.location = url;
   })
   
   // create the dialog box for all "what's this" links
   $('.whatsthis_dialog').dialog({
		modal: true,
		dialogClass: 'flora',
		title: $('.whatsthis_title').text(),
		resizable: false,
		autoOpen: false,
		width: 400,
		height: 400,
		overlay: {
			opacity: 0.5,
			background: 'black'
		},
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	
	$('.whatsthis_link').click(function(){
		$('.whatsthis_dialog').dialog('open');
	});
 
});
