$(document).ready(function(){
	if(jQuery.support.leadingWhitespace){
		applyModeratorBehaviors();
	}
});


function applyModeratorBehaviors(){
		if($('body.search_result')[0] || $('body.artist_home')[0] || $('body.artist_home_rock')[0] || $('body.artist_home_pop')[0] || $('body.artist_home_fuk')[0] || $('body.artist_home_americana')[0] || $('body.artist_home_electronic')[0] || $('body.artist_home_sxsw')[0] || $('body.artist_home_underground')[0]){
	 		$('.featured-form form').unbind().submit(function(){
	 			curForm = $(this);
				var options = { 
				    datatype:	'xml',
				    success:    function(responseXML){
					    var message = $('message', responseXML).text(); 
					    if(message){
					    	//alert('SUCCESS: This member has been saved as a featured member');
					    	$(curForm).hide();
					    	$(curForm).parents('.featured-form').siblings('.featured-check').children('.checkbox.featured').attr('checked','true');
	   						$(curForm).parents('li.artist').addClass('featured');
					    }else{
					    	alert('ERROR:This member could not be saved as a featured member.');
					    }
				    
				    },
				    complete: function(){
						document.location.reload(true);
				    }
				}; 
				curForm.ajaxSubmit(options);
				return false;
			});
	
			$('.featured-check .edit').unbind().click(function(){
	    		$(this).parents('.featured-check').siblings('.featured-form').show();
			});
			
			
			$('.checkbox.featured').unbind().click(function(){
				curCheckbox = $(this);
				if($(this).attr('checked')){
					$(this).parents('.featured-check').siblings('.featured-form').show();
					return false;
				}else{
					//$('.featured-form,.more-info').hide();
					if(confirm('Are you sure you want to remove this member from the featured list?')){
						options = {
							type:		'GET',
							url:		'/index.php',
							data:		'handler=featured&action=delete&ajax=true&mid='+$(this).attr('id'),
							datatype:	'xml',
							success: 	function(responseXML){
								if($(responseXML).find('message')){
									//alert('SUCCESS: This member has been removed from the featured list.');
									$(curCheckbox).parents('li.artist').removeClass('featured');
									$(curCheckbox).siblings('button').remove();
									if($('body.artist_home ')[0]){
										$(curCheckbox).parents('li:first').hide();
										
									}
									return true;
								}else{
									alert('ERROR:This member could not be removed from the featured list.'+message);
								}
							}
						};
						$.ajax(options);
						
					}else{
						$(curCheckbox).attr('checked',true);
						return false;
					}
				}
			});
		
			$('.featured-form button.save').unbind().click(function(){
				$(this).parents('form:first').submit();
				return false;
			});
			
			$('.featured-form button.cancel').unbind().click(function(){
				$(this).parents('.featured-form').hide();
				$('.featured-form').siblings('.checkbox.featured').attr('checked',false);
				return false;
			});
			
			/**
			 * Headline Functionality
			 **/
			 
			//Save button
			$('#Form-Headline .save').unbind().click(function(){
				$(this).parents('form:first').submit();
				return false;
			});
	
			//Delete Button
			$('#Headlines .delete').unbind().click(function(){
				curHeadline = $(this).attr('id');
				$.ajax({
					datatype: 'xml',
					url: '/index.php?handler=headline&action=delete&ajax=true&hid='+curHeadline,
					success: function(responseXML){
					    var message = $('message', responseXML).text(); 
					    if(message){
					    	location.reload(true);
					    }else{
					    	alert('ERROR:This headline could not be saved.');
					    }
					    return false;
				    }
				});
				return false;
			});
			
			
	 		$('#Form-Headline').unbind().submit(function(){
	 			curForm = $(this);
	 			var options = { 
				    datatype:	'xml',
				    beforeSubmit: function(){
				    	//////console.log('presubmit');
				    	$('fieldset textarea,input',curForm).hide();
				    	$('fieldset',curForm).append('<img src="/themes/lp33_beta/Assets/Images/ajax-loader.gif" class="loader"/>');
				    },
				    success:    function(responseXML){
				    	var message = $('message', responseXML).text(); 
					    if(message){
					    	$.ajax({
					    		url: '/index.php?handler=headline&action=get&ajax=true&channel='+$('input[name = channel]',curForm).val(),
					    		dataType: 'html',
					    		success: function(responseHTML){
					    			$(curForm).siblings('ol').empty().append($('content',responseHTML).html());
					    			$('fieldset img',curForm).remove();
				    				$('fieldset textarea',curForm).val('');
				    				$('fieldset textarea,input',curForm).show();
					    		}
					    	});
					    }else{
					    	alert('ERROR:This headline could not be saved.');
					    }
					    return false;
				    },
				    complete: function(){
					    location.reload(true);
				    }
				}; 
				$(curForm).ajaxSubmit(options);
				return false;
			});
	
	 		$('#Form-Sectional-Content').unbind().submit(function(){
	 			curForm = $(this);
	 			var options = { 
				    datatype:	'xml',
				    beforeSubmit: function(){
				    	//////console.log('presubmit');
				    	$('fieldset textarea,input',curForm).hide();
				    	$('fieldset',curForm).append('<img src="/themes/lp33_beta/Assets/Images/ajax-loader.gif" class="loader"/>');
				    },
				    success:    function(responseXML){
				    	var message = $('message', responseXML).text(); 
					    if(message){
					    	$.ajax({
					    		url: '/index.php?handler=sectional_content&action=get&ajax=true&channel='+$('input[name = channel]',curForm).val(),
					    		dataType: 'html',
					    		success: function(responseHTML){
					    			//hide the form
					    			$(curForm).hide();
					    			
					    			//replace the headlines 
					    			$('input[name = headline]',curForm).empty().append($('content h2',responseHTML).text());
					    			$(curForm).siblings('h2').empty().append($('content h2',responseHTML).html());
					    			
					    			//replace the body
					    			$('textarea[name = body]',curForm).empty().append($('content div',responseHTML).text());
					    			$(curForm).siblings('div').empty().append($('content div',responseHTML).html());
					    			
					    			//Apply behavior to the edit button
			    					$('#Sectional-Content .edit').click(function(){
										$(this).parents('#Sectional-Content').children('h2,div').hide();
										$(this).parents('#Sectional-Content').children('form').show();
										return false;		
									});
					    			
					    			//show the content
									$('fieldset img',curForm).remove();
									$('#Sectional-Content').children('form').hide();
									$('fieldset textarea,input',curForm).show();
									$('#Sectional-Content').children('h2,div').show();
									return false;
					    		}
					    	});
					    }else{
					    	alert('ERROR:This headline could not be saved due to a system error.');
					    }
					    return false;
				    }
				}; 
				$(curForm).ajaxSubmit(options);
				return false;
			});
			$('#Sectional-Content .edit').click(function(){
				$(this).parents('#Sectional-Content').children('h2,div').hide();
				$(this).parents('#Sectional-Content').children('form').show();
				return false;		
			});
	 	}
	 	
	 	if($('#Form-Sectional-Content .cancel')[0]){
	 		$('#Form-Sectional-Content .cancel').unbind().click(function(){
	 			$(this).parents('form:eq(0)').hide();
	 			$('#Sectional-Content h2, #Sectional-Content div').show();
	 		});
	 	}
	 	
	 	if($('.content')[0]){
	 		$('.content button.add').unbind().click(function(){
				$('.content #Page-Form').show();
				$('.content div.add,#WholePage h2,#WholePage .content,#WholePage .back').hide();
				return false;
			});
			$('.content .cancel').unbind().click(function(){
				//$('.content textarea').val('');
				$('.content #Page-Form').hide();
				$('.content div.add,#WholePage h2,#WholePage .content,#WholePage .back').show();
				return false;
			});
			
	 		$('.content .edit').unbind().click(function(){
				$('.content div.add,#WholePage h2,#WholePage .content,#WholePage .back').hide();
				$('#Page-Form input[name=action]').val('update');
				var title = $('#WholePage h2').clone();
				$('span',title).remove();
				$('.content #Page-Form #Page-Name').val($(title).html());
				$('.content #Page-Form #Page-Content').val($('#WholePage .content').html());
				$('.content #Page-Form').show();
				return false;
			});
			
			$('.content .delete').unbind().click(function(){
				if(confirm('Are you sure you want to delete this page?')){
					$('#Page-Form input[name=action]').val('delete');
					$('#Page-Form').submit();
				}else{
					return false;
				}
			});
	
			$('#Page-Form').unbind().submit(function(){
				curForm = $(this);
				var options = {
					datatype: 'xml',
					success: function(responseXML){
						var message = $('message', responseXML).text();
						if(message){
							if($('input[name=action]',curForm).val() == 'add'){
								document.location = '/index.php?page=content&pid='+$('content', responseXML).text();
							}else if($('input[name=action]',curForm).val() == 'delete'){
								document.location = '/index.php';
							}else{
								document.location = '/index.php?page=content&pid='+$('input[name=pid]',curForm).val();
							}
							return false;
						}else{
							alert('ERROR:This page could not be saved due to a system error.');
							return false;
						}
					}
				}
				$(curForm).ajaxSubmit(options);
				return false;
			});
	 	}
}

