uploaderSwitcher = function(obj) {
	if(jQuery(obj).attr('id') == 'embed'){
		if(!jQuery('#embed_area').is(':visible')){
			jQuery('#embed_area').slideDown();
			if(no_krunch_access_details){
			  jQuery(obj).hide();
			  jQuery('#panda_profile_select').hide();

        jQuery('#panda_embed_remove').hide();
			  jQuery('#panda_profile_select').val("");
			  jQuery('#panda_upload_button').hide();
			};
		}
		jQuery('#post_video_upload_id').val("");
	}else if(jQuery(obj).attr('id') == 'none'){
		jQuery('#embed_code').val("");
		jQuery('#post_video_upload_id').val("");
	}
}	

jQuery.bulk_pub_success = function () {
	jQuery(".c_box").each(function(){this.checked = false;});
}

jQuery.thumbnailLinkToggle = function(){
  el = jQuery('#video_uploader_thumbnail_link');
  
  if (el.html().match(/Choose/)) {
    el.html('Cancel');
  } else {
    el.html('Choose a generated thumbnail');
  }
};

jQuery.fn.qtip.styles.form_callout = { // Last part is the name of the style
   width: 200,
   background: '#ffffff',
   color: '#447499',
   textAlign: 'left',
   border: {
      width: 1,
      radius: 4,
      color: '#A1C5E5'
   },
   tip: 'leftMiddle'
};

jQuery.handleDestroyVideo = function() {
  jQuery('#video_uploader_status').hide();
  jQuery('#video_thumbnail').attr('src', "/images/post_images/missing_thumb.jpg");
  jQuery('#panda_profile_picker').show();
};

jQuery.getSignedParams = function() {
  signed_params = jQuery('#panda_upload_script').data("panda_access_details");
/*  if (signed_params == null){
    signed_params = no_krunch_access_details;
  }
*/  return signed_params;
};



jQuery(function($){
	var mode = false;
	jQuery('#main_tag_list .tag').click(function(){ 
		selectTag(this);
	});

	jQuery('.cat_handle, .cat_actions, .cat_trash, .cat_stroller, #add_admin_short_form, #main_tag_list, #preview_content_area, .dragger, #teaser_content_area').hide();	


	$('.show_integration').fancybox({
			'hideOnContentClick': false,
			'autoDimensions' : false,
			'width' : 700,
			'height' : 500
		});

 	$("a.comment_context").fancybox({
		'type' : 'iframe',
 	  'onStart' : jQuery.wait,
    'onComplete' : jQuery.ready,
		'autoDimensions' : false,
		'width' : 700,
		'height' : 500
 	});
		
	jQuery('#show_all_tags_link').toggle(function(){	
		jQuery(this).html('Hide all tags');
		jQuery('#main_tag_list').show();
	}, function(){
		jQuery(this).html('Show all tags');
		jQuery('#main_tag_list').hide();
	});
		
	jQuery('#panda_profile_select').live('change' ,function(){
	  jQuery('#panda_upload_script').data("panda_access_details", all_panda_access_details[jQuery(this).val()])
	  if (jQuery(this).val() == ""){
  	  jQuery('#panda_upload_button').hide();
	  } else {
  	  jQuery('#panda_upload_button').show();
	  }
	});
	
	jQuery('#video_uploader_thumbnail_link').click(function() { 
    jQuery.thumbnailLinkToggle();
	  jQuery('#video_uploader_thumbnail_picker').slideToggle(); 
	  return false;
  });
  
	jQuery('#video_uploader_thumbnail_picker img').click( function(){
	  var screenshot_number = jQuery(this).parent().attr('id');
	  jQuery('#video_uploader_thumbnails input:first-child').val(screenshot_number)
	  jQuery('#video_thumbnail').attr('src', jQuery(this).attr('src'));
    jQuery.thumbnailLinkToggle();
	  jQuery('#video_uploader_thumbnail_picker').slideUp(function(){
  	  jQuery('#video_uploader_thumbnail_picker ul li').each(function() {
        jQuery(this).attr('style', null);
  	  });
	    jQuery('#' + screenshot_number).attr('style', 'display:none;');	    
	  });
	  return false;
	});	
	
/*  $('.fileinputs').click( function(){
    jQuery.thumbnailLinkToggle();
    if (('#video_uploader_thumbnail_link').val().match('Cancel')){
      jQuery.thumbnailLinkToggle(function(){
        $('#video_uploader_thumbnail_picker').slideUp(); 
      });
    }
  });
*/	
	jQuery('#video_uploader_file_list li').click( function(){
		jQuery('#video_uploader_file_list li').removeClass('kajabi_radio_selected');
		jQuery('#video_uploader_file_list li').addClass('kajabi_radio');
		jQuery(this).addClass('kajabi_radio_selected');
		uploaderSwitcher(this);
	});
	
	jQuery('#video_uploader_file_list li').live( 'click', function(){
		jQuery('#video_uploader_file_list li').removeClass('kajabi_radio_selected');
		jQuery('#video_uploader_file_list li').addClass('kajabi_radio');
		jQuery(this).addClass('kajabi_radio_selected');
		uploaderSwitcher(this);
	});
	
	jQuery('#embed_cancel a').live('click', function(){
    jQuery('#embed_area').hide('fast', function(){
      jQuery('#embed_code').val("");
      jQuery('#video_uploader_file_list #embed').show();
      jQuery('#panda_profile_select').show();
      jQuery('#video_uploader_file_list li').removeClass('kajabi_radio_selected');
  		jQuery('#video_uploader_file_list li').addClass('kajabi_radio');
    });
    return false;
	});
	
	jQuery('#preview_content_link, #teaser_content_link').click(function(){
		jQuery(this).next().slideToggle('slow');
	});
	//$('li.proj').hover( 
	//	function(){$(this).find('ul.admin_pab').fadeIn('fast');}, 	
	//	function(){$(this).find('ul.admin_pab').fadeOut('fast');}
	//);
	
	$(window).bind("resize", function(){
	   $("#smoke_screen").css("height", $(window).height() + 1000);
	});
	
	$('#format_font').buttonset();
	
	$('#admin_quick_add ul li').hover(
		function(){
			$(this).addClass('hover').fadeIn('fast');
			$(this).find('.edit').fadeIn('fast');
		}, 	
		function(){
			$(this).removeClass('hover');
			$(this).find('.edit').hide();
	});
	
	$('#style_display_name').change(function(){
		var selected = $(this).val();
		var items = $.parseJSON(all_themes);
		$.each(items, function(k, v){
			if(k == selected){
				$.each(v, function(id, val){
					$("#" + id + "_selector div").css('backgroundColor',  val.value);
					$("#style_settings_" + id + "_value").val(val.value);
					$("#" + id + "_holder").ColorPickerSetColor(val.value.replace(/#/g, ''));
				});
			} 
		});	
	});
	
	$('#project_root_page_redirect_attributes_destination').change(function(){
		$('#project_root_page_redirect_attributes_destination_id').val("");
		if($(this).val() == 'dashboard'){
			$('#dest_id_area').hide();
		} else {
			$('#dest_id_area').fadeIn( function(){
				$('#dest_id_area').effect("highlight", {}, 3000);
			});	
		}
	});
	
	$('#style_header_background_color').change( function(){
		$('#header_preview').css('background-color', $(this).val());
		$("#white_gradient_color, black_gradient_color").css('background-color', $(this).val());
	});
	
	$('#style_logo_text_color').change( function(){
		$('#header_preview').css('color', $(this).val());
	});
	
	$('#style_display_name').change( function(){
		$("#save_type option[value=" + $('#style_display_name').val() +"]").attr("selected","selected");
	});
	
	$('.reply_submit').ajaxSubmit();
	
	$('#background_images li').click( function(){
		//alert($(this).find('input').first().val());
		$('#header_preview').css('background-image', 'url(' + $(this).find('img').attr('src') + ')');
		$('#style_settings_header_background_image_value').val($(this).find('input').attr('value'));
		$('#background_images li').removeClass('bg_im_selected');
		$(this).addClass('bg_im_selected');
	});
	
	$('#style_settings_logo_text_font_value').change( function(){
		$('#header_preview').css('font-family', $(this).val());
	});
	
	$('#style_settings_logo_text_font_size_value').change( function(){
		$('#header_preview').css('font-size', $(this).val());
	});
	
	$('#bulk_select_box').change(function(){
		if ($(this).val() != 'select'){
			if(confirm('This will affect all checked items.  Are you sure?')){
				recordsOfImportance = null;
				var recordsOfImportance = [];
				var sub = $(this).val().split('bulk_');
				var do_action = sub[1];
				$(".c_box:checked").each(function(){
				    recordsOfImportance.push($(this).val());
				});
				var json = JSON.stringify(recordsOfImportance);
				jQuery.wait();
				if((do_action == "publish") || (do_action == "unpublish")){
					$.ajax({
						url: 					$('#' + $(this).val()).val(), 
						type:					'post',
						dataType:				'script',	
						data: 					{ perform : do_action, ids : json },
						success:				jQuery.bulk_pub_success()
					});
				} else {
			    	$.post($('#' + $(this).val()).val(), { action: do_action, ids: json }, function(){
						$(".c_box").each(function(){this.checked = false;});
					}, "script");
				}
		    	return false;
			};
		}
	});
	
	$('#posts_sort_select_box, #project_users_sort_select_box').change(function(){
		var sort_page = $(this).attr('id').split('_sort');
		jQuery.wait();
		$.ajax({
			url: 					 sort_page[0], 
			type:					'get',
			dataType:				'script',
			data: 					{ 'sort_by' : $(this).val() }
		});
	});
	
	$('#comment_filter_select_box').change(function(){
		jQuery.wait();
		$.ajax({
			url: 					'comments', 
			type:					'get',
			dataType:				'script',
			data: 					{ 'filter_by' : $(this).val() }
		});
	});
	
	/*
	$('#suggestion_filter, #question_filter, #comment_filter, #unapproved_filter').click( function(){
		$(this).toggleClass('selected');
	});
	
	$('.filter_button').click( function(){
		var filter = new Array;
		$('.list_filter li').each( function(){
			if($(this).attr('class') == 'selected'){
				filter_item = $(this).attr('id').split('_filter');
				filter.push(filter_item[0]);
			}
		});
		jQuery.wait();
		$.ajax({
			url: 					'comments', 
			type:					'get',
			dataType:				'script',
			data: 					{ 'filter_by' : filter } 
		});
	});
	*/
	
	$('.comment_reply').live('click', function(){
		$(this).closest('ul').next().slideDown('slow');
	});
	
	$('.comment_edit').live('click', function(){
		$(this).closest('ul').next().slideDown('slow');
	});
	
	$('.reply_form label').labelOver('text_over');
	
	$('.comment_close').live('click', function(){
		$(this).closest('form').slideUp('slow');
	});
	
	$('#bulk_select_check_box').click( function(){
		jQuery.select_all();
	});
	
	if($('#comment_list').is(':visible')){
		$('.reply_form textarea').autoResize({
		    onResize : function() {
		        $(this).css({opacity:0.8});
		    },
		    animateCallback : function() {
		        $(this).css({opacity:1});
		    },
		    animateDuration : 1000,
		    extraSpace : 20
		});
	}

	$('#support_note_note').autoResize({
	    onResize : function() {
	        $(this).css({opacity:0.8});
	    },
	    animateCallback : function() {
	        $(this).css({opacity:1});
	    },
	    animateDuration : 1000,
	    extraSpace : 20
	});
	
	$('.edit_comment').ajaxSubmit();
	$('.reply_form').ajaxSubmit();
	$('.approve_comment').ajaxCreate(true, "approve");
	$('.unapprove_comment').ajaxDestroy(true, "unapprove");
	$('.destroy_comment').ajaxDestroy(true, "delete");
	$('.edit_comment_link').ajaxGet();
	$('.highlight_comment').ajaxCreate(true, "highlight");
	$('.unhighlight_comment').ajaxDestroy(true, "unhighlight");
  $('.thumbnail_remove_link').postClickWithAjax(true, "remove");
  $('.destroy_vid').ajaxDestroy(true, "delete", jQuery.handleDestroyVideo);

	$('a#style_me').dropdown();
	//$('.fileinputs').initFileUploads();
	$('#save_theme_as').change(function() {
		if ($(this).val() != 'custom'){
			if ($("#save_as").is(':visible')){
				$("#save_as").hide();
			}
		} else {
			$("#save_as").show();
		}
	});
	
	$('div.category_item').hover( function(){
		$(this).find('.cat_actions').fadeIn('fast');
	}, 	function(){
		$(this).find('.cat_actions').fadeOut('fast');
	});
	
	
	$('.s_h_subsection').toggle( function(){
		$(this).html('-');
		$(this).next('div').slideToggle('slow');
	}, function(){
		$(this).html('+');
		$(this).next('div').slideToggle('slow');
	});

	$("ul.category_list_main").sortable({
		items: 'li.m',
		tolerance: 'pointer',
		revert: true,
		opacity: 0.6,
		placeholder: 'cat_ui-state-highlight',
		start: function(event, ui){
			//if($(ui.item).find('.cat_stroller').is(':visible')){
			//	$(ui.item).find('.cat_stroller').hide();
			//} else {
				
			$(this).addClass('acceptable');
			$(this).find('[id^=categories_parent]:visible').addClass('unacceptable');
			//}
		},
		stop: function(){
			$(this).removeClass('acceptable');
			$(this).find('[id^=categories_parent]:visible').removeClass('unacceptable');
			var order_sc = $('ul.category_list_main').sortable("serialize");
			var proj_id = $('ul.category_list_main').attr('id');
			jQuery.wait();
			$.ajax({
				url: 					'/admin/projects/'+ proj_id +'/categories/sort', 
				type:					'post',
				dataType:				'script',	
				data: 					 order_sc
			});
		}
	});
	
	$("ul.category_list_first_child").sortable({
		items: 'li.fc',
		tolerance: 'pointer',
		revert: true,
		opacity: 0.6,
		placeholder: 'cat_ui-state-highlight',
		start: function(event, ui){
			if($(ui.item).find('.cat_stroller').is(':visible')){
				$(ui.item).find('.cat_stroller').hide();
			}
			$(this).addClass('acceptable');
			$(this).find('[id^=categories_parent]:visible').addClass('unacceptable');
		},
		stop: function(){
			$(this).removeClass('acceptable');
			$(this).find('[id^=categories_parent]:visible').removeClass('unacceptable');
			var order_sc = $(this).sortable("serialize");
			var proj_id = $('ul.category_list_main').attr('id');
			jQuery.wait();
			$.ajax({
				url: 					'/admin/projects/'+ proj_id +'/categories/sort', 
				type:					'post',
				dataType:				'script',	
				data: 					 order_sc
			});
		}
	});
	
	$("ul.category_list_second_child").sortable({
		items: 'li.sc',
		tolerance: 'pointer',
		revert: true,
		opacity: 0.6,
		placeholder: 'cat_ui-state-highlight',
		start: function(event, ui){
			if($(ui.item).find('.cat_stroller').is(':visible')){
				$(ui.item).find('.cat_stroller').hide();
			}
			$(this).addClass('acceptable');
			$(this).find('[id^=categories_parent]:visible').addClass('unacceptable');
		},
		stop: function(){
			$(this).removeClass('acceptable');
			$(this).find('[id^=categories_parent]:visible').removeClass('unacceptable');
			var order_sc = $(this).sortable("serialize");
			var proj_id = $('ul.category_list_main').attr('id');
			jQuery.wait();
			$.ajax({
				url: 					'/admin/projects/'+ proj_id +'/categories/sort', 
				type:					'post',
				dataType:				'script',	
				data: 					 order_sc
			});
		}
	});
	
	$("ul.sortable_posts").sortable({
		items: 'li',
		tolerance: 'pointer',
		revert: true,
		opacity: 0.6,
		placeholder: 'cat_post_ui-state-highlight',
		start: function(event, ui){
			
		},
		stop: function(){
			var order_sc = $('ul.sortable_posts').sortable("serialize");
			var cat_id = $('ul.sortable_posts').attr('id');
			jQuery.wait();
			$.ajax({
				url: 					'/admin/categories/'+ cat_id +'/posts/sort', 
				type:					'post',
				dataType:				'script',	
				data: 					 order_sc
			});
		}
	});
	
	$('ul.sortable_funnel_events').sortable({
		items: 'li.funnel_event',
		opacity: 0.6,
		axis: 'y',
		handle: '.dragger',
		start: function(event, ui){
		},
		stop: function(event, ui){
			var order_sc = $('ul.sortable_funnel_events').sortable("serialize");
			var funnel_id = $('ul.sortable_funnel_events').attr('id');
			var z_in = 999;
			var c = 0;
			$('ul.sortable_funnel_events li').each( function(){
				$(this).find('.number').html(c);
				$(this).attr('style', 'z-index:' + z_in + ';');
				z_in = z_in - 1;
				c = c + 1;
			});
			jQuery.wait();
			$.ajax({
				url: 					'/admin/funnels/'+ funnel_id +'/funnel_events/sort', 
				type:					'post',
				dataType:				'script',	
				data: 					 order_sc
			});
		}
	});
	
	$('ul.sortable_disclaimers').sortable({
		items: 'li.disclaimer_item',
		opacity: 0.6,
		axis: 'y',
		start: function(event, ui){
		},
		stop: function(event, ui){
			var order_sc = $('ul.sortable_disclaimers').sortable("serialize");
			var proj_id = $('ul.sortable_disclaimers').attr('id');
			jQuery.wait();
			$.ajax({
				url: 					'/admin/projects/' + proj_id + '/disclaimers/sort', 
				type:					'post',
				dataType:				'script',	
				data: 					 order_sc
			});
		}
	});
		
	$("#sortable").disableSelection();
	
	$('.event_holder').hover( function(){
		$(this).find('.number').hide();
		$(this).find('.dragger').show();
	}, function(){
		$(this).find('.dragger').hide();
		$(this).find('.number').show();
	});
	
});
