jQuery.noConflict();
var $j = jQuery;

$j(document).ready(function() {

	$j(".tab-panel").hide();
 
	$j("#content-apparatus ul.tab-nav").tabs("#content-apparatus .tab-panel", { 
			initialIndex: 0,
			onClick: function() {
				if(this.getIndex() == 0) {
					$j("#content-apparatus").addClass("minimized");
				} else {
					$j("#content-apparatus").removeClass("minimized");
				}
			}
		});
	$j("#header-apparatus ul.tab-nav").tabs("#header-apparatus .tab-panel", { 
			initialIndex: 0,
			onClick: function() {
				if(this.getIndex() == 0) {
					$j("#header-apparatus").addClass("searchmode");
				} else {
					$j("#header-apparatus").removeClass("searchmode");
				}
			}
		});
	$j(".widget-popular ul.tab-nav").tabs(".widget-popular .tab-panel", { initialIndex: 0 });
	$j("#profile-activity ul.tab-nav").tabs("#profile-activity .tab-panel", { initialIndex: 0 });
	
	$j("div.scrollable").scrollable({ 
	    next: 'a.prev-persona', prev: 'a.next-persona', size: 4, speed: 400, keyboard: false,
		onBeforeSeek: function() {
			var descriptor = $j(".active .persona-description").children().clone();
			$j("#persona-description-container").empty();
			$j("#persona-description-container").append(descriptor);
		}
	}).circular();
	
	$j("div.scrollable li:first").click()
	
	$j("#header-apparatus").bind("click keydown", function() {
		$j(this).expose({
			color: '#fff', opacity: 0, loadSpeed: 0, closeSpeed: 0,
			onLoad: function() { this.getExposed().addClass('full'); $j("div:hidden:first"); },
			onClose: function() { this.getExposed().removeClass('full'); },
			api: true
		}).load();
	});
	
	$j(function(){ 
	    // find all the input elements with title attributes
		$j('input[title!=""],textarea[title!=""]').hint();
	});

	// bind "click" event for links with title="submit" 
	$j("a[title=submit]").click( function(){
	  // it submits the form it is contained within
	  $j(this).parents("form").submit();
	});

	// Expand fields on profile pages
	$j("#profile-more-fields").hide();
	$j('<a id="reveal-fields">+ Show More Options</a> ').insertBefore('#profile-more-fields');
	$j("a#reveal-fields").click(function(){
		$j("#profile-more-fields").fadeIn(1200);
		$j("a#reveal-fields").fadeOut(600);
	});

	// Auto-expand dictionary word submit form when clicking on the title
	$j("#entry-form-minimal").hide();
	$j("input#entry-title").focus(function(){ $j("#entry-form-minimal").fadeIn(600); });

	// Close element
	$j(".close-me").click(function(){ $j(this).parent().fadeOut(600); });

    mtAttachEvent('load', mtUpdateScores);
    mtAttachEvent('usersignin', mtUpdateScores);

	// Question form submission
	$j("#content-question-form").submit(function(){
		// Assign category based on which persona thumbnail is chosen
		categoryID = $j(".persona-carousel-content .active img").attr("alt");
		if (categoryID) { $j("#field_category").val(categoryID); };
// Copy question to full questionfield so as to avoid being cut off
fullquestion = $j("#apparatus-ask-step1 textarea").val();
$j("#customfield_question_full").val(fullquestion);
		// Submissions marked as anonymous are flagged as such on the backend
		if ($j("#anonymous-question").is(":checked")) {	$j("#customfield_anonymous").val("1"); }; 
		// If there is no submitter name, assign it to anonymous
		if ($j("#customfield_submitter").val()=="") { $j("#customfield_submitter").val("Anonymous"); };
		if ($j("#apparatus-ask-step1 textarea").val().length>320) { 
			$j("#content-apparatus-count").css("background-color", "#ff0");
			return false;
		 };
	});
	$j("#header-question-form").submit(function(){
		// Assign category based on which persona thumbnail is chosen
		categoryID = $j(".persona-carousel-content .active img").attr("alt");
		if (categoryID) { $j("#field_category").val(categoryID); };
// Copy question to full questionfield so as to avoid being cut off
fullquestion = $j("#apparatus-ask-step1 textarea").val();
$j("#customfield_question_full").val(fullquestion);
		// Submissions marked as anonymous are flagged as such on the backend
		if ($j("#anonymous-question").is(":checked")) {	$j("#customfield_anonymous").val("1"); }; 
		// If there is no submitter name, assign it to anonymous
		if ($j("#customfield_submitter").val()=="") { $j("#customfield_submitter").val("Anonymous"); };
		if ($j("#apparatus-ask-step1 textarea").val().length>320) { 
			$j("#header-apparatus-count").css("background-color", "#ff0");
			return false;
		 };
	});

	entry_create_loggedin();	
	check_anonymous_status();
	
	$(function() {
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});
	
	$j('#customfield_anonymous').click(function(){
		check_anonymous_status();
	})

});
function check_anonymous_status() {
    var u = mtGetUser();
	if ($j('#customfield_anonymous').attr('checked')) {
		thefield = $j('#customfield_submitternickname');
		thefield.attr("disabled", true);
		thefield.css('opacity',0.3);	
		thefield.val('Anonymous');
		$j('#customfield_anonymous').val("1");
		} else {
		thefield = $j('#customfield_submitternickname');
		thefield.removeAttr('disabled');
		thefield.css('opacity',1);
		thefield.val(u.name);
		$j('#customfield_anonymous').val("0");
	}
}
function entry_create_loggedin() {
    var u = mtGetUser();
    var loggedin = u && u.is_authenticated ? true : false;
    if (!loggedin)  {
		// Run tasks for non-logged in users
		$j("#customfield_anonymous").attr('checked', true);
		//console.log("User is NOT logged in");
    } else {
		// Pass in the magic token from MT
        var mt = document.getElementById('magic_token');
        if (mt) mt.value = u.sid;
        $j("#customfield_submitternickname").val(u.name);
		$j("#customfield_anonymous").attr('checked', false);
		$j("#customfield_anonymous").val("0");
		//console.log("User is logged in");
    }
}