
cfct = {}

cfct.loading = function() {
	return '<div class="loading"><span>Loading...</span></div>';
}

cfct.ajax_post_content = function() {
	$('ol.archive .excerpt .entry-title a').unbind().click(function() {
		var post_id = $(this).attr('rev').replace('post-', '');
		var excerpt = $('#post-excerpt-' + post_id);
		var target = $('#post-content-' + post_id + '-target');
		excerpt.hide();
		target.html(cfct.loading()).show().load(CFCT_URL + '/index.php?cfct_action=post_content&id=' + post_id, function() {
			cfct.ajax_post_comments();
			$('#post_close_' + post_id + ' a').click(function() {
				target.slideUp(function() {
					excerpt.show();
				});
				return false;
			});
			$(this).hide().slideDown();
		});
		return false;
	});
}

cfct.ajax_post_comments = function() {
	$('p.comments-link a').unbind().click(function() {
		var a = $(this);
		var post_id = a.attr('rev').replace('post-', '');
		var target = $('#post-comments-' + post_id + '-target');
		target.html(cfct.loading()).show().load(CFCT_URL + '/index.php?cfct_action=post_comments&id=' + post_id, function() {
			$(this).hide().slideDown(function() {
				a.attr('rel', a.html()).html('Hide Comments').unbind().click(function() {
					target.slideUp(function() {
						a.html(a.attr('rel'));
						cfct.ajax_post_comments();
					});
					return false;
				});
			});
		});
		return false;
	});
}

// Moves form for threaded comments: taken and modified from WordPress 2.7
addComment = {
	moveForm : function(commId, parentId, respondId, postId) {
		var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link-p' + postId), parent = t.I('comment_parent_p' + postId), post = t.I('comment_post_ID_p' + postId);

		if ( ! comm || ! respond || ! cancel || ! parent )
			return;

		t.respondId = respondId;
		postId = postId || false;

		if ( ! t.I('wp-temp-form-div-p' + postId) ) {
			div = document.createElement('div');
			div.id = 'wp-temp-form-div-p' + postId;
			div.style.display = 'none';
			respond.parentNode.insertBefore(div, respond);
		}

		comm.parentNode.insertBefore(respond, comm.nextSibling);
		if ( post && postId )
			post.value = postId;
		parent.value = parentId;
		cancel.style.display = '';

		cancel.onclick = function() {
			var t = addComment, temp = t.I('wp-temp-form-div-p' + postId), respond = t.I(t.respondId);

			if ( ! temp || ! respond )
				return;

			t.I('comment_parent_p' + postId).value = '0';
			temp.parentNode.insertBefore(respond, temp);
			temp.parentNode.removeChild(temp);
			this.style.display = 'none';
			this.onclick = null;
			return false;
		}

		try { t.I('comment-p' + postId).focus(); }
		catch(e) {}

		return false;
	},

	I : function(e) {
		return document.getElementById(e);
	}
}



$(function($) {
	if($('#subnav')) {
		$('.tab-content').hide(); // Hide all divs
		$('.tab-content:first').show(); // Show the first div
		$('#subnav li:first').addClass('active'); // Set the class of the first link to active
		$('#subnav li a').click(function(){ //When any link is clicked
		$('#subnav li').removeClass('active'); // Remove active class from all links
		$(this).parent().addClass('active'); //Set clicked link class to active
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		$('.tab-content').hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
		});		
	}
	
	if($('#hero') && $('#hero').hasClass('featured')) {
		var sections = $('#hero .ui-tabs-panel');
		if(sections.length > 1) {
			var container = document.createElement('ul'),
				index = 1;
			$(container).addClass('ui-tabs-nav');
			sections.each(function() {						
				$(container).append("<li class='ui-tabs-nav-item' id='nav-fragment-"+index+"'><a href='#fragment-"+index+"'><span>Featured "+index+"</span></a></li>")
				index++;	
			});
			$('#hero').append(container);
		}
		
		$("#hero").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 9000, true);
		$("#hero").hover(
			function() {
				$("#hero").tabs("rotate",0,true);
			},
			function() {
				$("#hero").tabs("rotate",9000,true);
			}
		);
		
	}
	
	if ((!$.browser.msie || $.browser.version.substr(0,1) != '6') && typeof CFCT_AJAX_LOAD != 'undefined' && CFCT_AJAX_LOAD) {
			cfct.ajax_post_content();
			cfct.ajax_post_comments();
		}
	
	$('a[rel*=modal]').facebox();	
});

