function isiOS(){
	return (
		(navigator.platform.indexOf("iPhone") > -1) || (navigator.platform.indexOf("iPod") > -1) || (navigator.platform.indexOf("iPad") > -1)
	);
}


/*-------------------------------------------
				Tooltip
-------------------------------------------*/
this.tooltip = function() {
	if (! isiOS()) {
		/* CONFIG */		
			xOffset = -10;
			yOffset = -78;		
			// these 2 variable determine popup's distance from the cursor
			// you might want to adjust to get the right result		
		/* END CONFIG */		
		$(".tooltip").hover(function(e){											  
			this.t = this.title;
			this.title = "";									  
			$("body").append("<div id='tooltip'><div class='top "+$(this).attr('data-class')+"'>&nbsp;</div><div style='font-size: 1.8em;line-height:1em;' class='cnt "+$(this).attr('data-class')+"'>"+ this.t +"</div><div class='bot "+$(this).attr('data-class')+"'>&nbsp;</div></div>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");		
	    },
		function(){
			this.title = this.t;		
			$("#tooltip").remove();
	    });	
		$(".tooltip").mousemove(function(e){
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});
	}			
};

/*-------------------------------------------
				Subtle tooltip
-------------------------------------------*/
this.subtle_tooltip = function() {
	/* CONFIG */		
		offsetX = -80;
		offsetY = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".subtle-tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<div id='subtle-tooltip'><div class='cnt'>"+ this.t +"</div></div>");
		$("#subtle-tooltip")
			.css("top",(e.pageY + offsetY) + "px")
			.css("left",(e.pageX + offsetX) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#subtle-tooltip").remove();
    });
    /*
	$(".subtle-tooltip").mousemove(function(e){
		$("#subtle-tooltip")
			.css("top",(e.pageY + offsetY) + "px")
			.css("left",(e.pageX + offsetX) + "px");
	});			
    */
};
$(document).ready(function() {
	//Starting Tooltip script
	tooltip();
	subtle_tooltip();
	$(".lightbox").fancybox();
	
	$( '.tabs li a' ).click( function(){
		var id = $( this ).attr('id') + '-cnt';
		$( '.tabs-content' ).hide();
		$( '#' + id ).show();
		
		$( '.tabs li a' ).removeClass( 'active' );
		$( this ).addClass( 'active' );
		
		return false;
	});
});
$(function() {
	$('.details-list .expander').click(function() {
		$(this).next('.expanding-content').toggle();
		$(this).toggleClass('opened-expander');
    });

    // For each parent LP, if it has highlighted child LP, then expand it.
    $('.details-list .expanding-content').each(
        function() {
            if ($(this).find('div.restudy, div.forgotten, div.new-question').length) {
                $(this).toggle();
                $(this).prev('.expander').toggleClass('opened-expander');
            }
        });

    // Change button appearance and post request to bank/unbank the topic.
	$('.flashcard-article a.bank-review').click(function(e) {
        $(this).find('span').toggleClass('active');
        ignoreEvent(e);
        $.post(this.href, {}, update_questions_count, 'text');
	});
	
	$('#tooltabs li span').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	$('#tooltabs li').click(function() {
		$('#tooltabs li span').removeClass('active');
		$(this).find('span:eq(0)').addClass('active');
		// tab_name = $(this).attr('class');
		// tab_content_name = '#toolcontents .' + tab_name + '-content';
		// $('#toolcontents .toolcontent').hide();
		// $(tab_content_name).show();
        $('#toolcontents .toolcontent').toggle();
	});

	$('.valuenav a').click(function(){
		var id = $(this).attr('title').toLowerCase().replace(/ /gi, '') + '-cnt';
		
		$('.value-list-cnt').hide();
		$('.valuenav a').removeClass('active-value');
		
		$('#' + id).show();
		$(this).addClass('active-value');
		
		return false;
	});


    // If there is an initial message, display it.
    showInitialMessage();

    // Initialize any tutorial messages.
    initTutorialMessages();
});

function init_slider() {
	$( "#exam-slider" ).slider({
		value: slider_init_value($('#exam-slider')) || 30,
		min: 10,
		max: 50,
		step: 10,
		slide: function(event, ui) {
			var eq = ui.value / 10 -1;
			$('.exam-slider span').removeClass('orange');
			$('.exam-slider span:eq('+eq+')').addClass('orange');
			
		},
        change: function(event, ui) {
            // $('#test_total_questions').val(ui.value);
            $('#test_total_questions')[0].value = ui.value;
        }
	});
	$( "#exam-slider2" ).slider({
		value: slider_init_value($('#exam-slider2')) || 30,
		min: 10,
		max: 50,
		step: 10,
		slide: function(event, ui) {
			var eq = ui.value / 10 -1;
			$('.exam-slider span').removeClass('orange');
			$('.exam-slider span:eq('+eq+')').addClass('orange');
		},
        change: function(event, ui) {
            // Update the user's question bank threshold.
            var form = $('#exam-slider2').parent()[0];
            $.post(form.action, { threshold: ui.value });
        }
	});
}

function slider_init_value(slider) {
    return slider.parent().find('span.orange').text();
}

function next_step() {
	$( '.step1' ).hide();
	$( '.step2' ).fadeIn('dev');
	init_slider();
	return false;
}

function check_all(checkbox) {
    var parent = $(checkbox).parent().parent();
    parent.find('input').each(
        function() {
            this.checked = true;
        });
}
function uncheck_all(checkbox) {
    var parent = $(checkbox).parent().parent();
    parent.find('input').each(
        function() {
            this.checked = false;
        });
}

function update_questions_count(data, status) {
    var count = parseInt(data);
    if (! isNaN(count)) {
        $('#bq-count').text('' + count);
    }
	// MERTAUGH: remove any "you've banked these questions" message
	$('div.banked_message:visible').hide();
}

// If recall level is checked, submit form and go to next question. Otherwise,
// tell the user to select a recall level.
function next_review_question(form) {
    if ($(form).find('input[type=radio]:checked').length)
        form.submit();
    else
        alert('Please select your recall level.');
        // TODO: Figure out better way to do this?
}


//////////////////////////////////////////////////////////////////////////////
//
//  Initial alert message.

/**
 *  Display the initial message in a dialog.
 */
function showInitialMessage() {
    var initMsg = $('#initial-message');
    if (initMsg && (initMsg.length > 0) && initMsg.dialog) {
        initMsg.dialog(
            {
                autoOpen:    true,
                modal:       true,
                dialogClass: 'initial-message',  // TODO
                // width:       $.support.cssFloat ? 'auto' : 400,
                width:       500,
                draggable:   true,
                resizable:   false,
                buttons:
                    {
                        'Close': function() { $(this).dialog('close'); }
                    }
            })
    }
}

/**
 *  Initialize tutorial messages, which start hidden but are shown when
 *  triggered by user action. The message only displays once, so the "close"
 *  action also removes the dialog.
 */
function initTutorialMessages() {
    $('.tutorial-message').each(
        function() {
            $(this).dialog(
                {
                    autoOpen:    false,
                    modal:       true,
                    dialogClass: 'tutorial-message',  // TODO
                    width:       500,
                    draggable:   true,
                    resizable:   false,
                    buttons:
                        {
                            'Close': function() { $(this).dialog('close'); $(this).remove(); }
                        }
                });
        });
}

