$(document).ready(function(){
	$('#left_off').mouseover(function(){
				$(this).attr('id', 'left_on');
				$(this).next('li').attr('id', 'on');
	}).mouseout(function(){
				$(this).attr('id', 'left_off');
				$(this).next('li').removeAttr('id');
	});
	$('#right_off').mouseover(function(){
				$(this).attr('id', 'right_on');
				$(this).prev('li').attr('id', 'on');
	}).mouseout(function(){
				$(this).attr('id', 'right_off');
				$(this).prev('li').removeAttr('id');
	});
	$('.s').mouseover(function(){
				$(this).attr('id', 'on');
				if ($(this).prev('li').attr('id') == 'left_off'){
					$(this).prev('li').attr('id', 'left_on');
				} else {
					$(this).prev('li').attr('class', 'don');
				}
				if ($(this).next('li').attr('id') == 'right_off'){
					$(this).next('li').attr('id', 'right_on');
				} else {
					$(this).next('li').attr('class', 'don');
				}

	}).mouseout(function(){
				$(this).removeAttr('id');
				if ($(this).prev('li').attr('id') == 'left_on'){
					$(this).prev('li').attr('id', 'left_off');
				} else {
					$(this).prev('li').attr('class', 'd');
				}
				if ($(this).next('li').attr('id') == 'right_on'){
					$(this).next('li').attr('id', 'right_off');
				} else {
					$(this).next('li').attr('class', 'd');
				}
	});

	jQuery.preloadImages = function(){
	  for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	  }
	}


	$("[id^='remove']").click(function (){
		var answer = confirm("Are you sure you want to remove this question from this test?\n\nThe question will still be available in your question bank to use again.\n\nPress OK to remove the question.");
		if (answer){
			return true;

		}
		else{
			return false;
		}

	})



	$('[id="viewresultsremovecorrect"]').click(function() {
		$("div[class='question']:has(i.qw)").toggle("hide");
		$("#viewresultsremovecorrectalert").toggle("show");
		$("#viewresultsremovecorrectalertall").toggle("hide");
		return false;
	});

	$('[id="viewresultsshowunmarked"]').click(function() {
		$("div[class='question']:not(:has(i.qu))").toggle("hide");
		$("#viewresultsshowunmarkedalertall").toggle("show");
		$("#viewresultsshowunmarkedalert").toggle("hide");
		return false;
	});

	$('[id="viewresultsshowall"]').click(function() {
		$("div[class='question']").show();
		$("#viewresultsremovecorrectalertall").show();
		$("#viewresultsremovecorrectalert").hide();
		$("#viewresultsshowunmarkedalertall").show();
		$("#viewresultsshowunmarkedalert").hide();
		return false;
	});

	$(".bbcode").each(function () {

		$(this).html( bbCode( $(this).html() ) );//send html to be parsed for bbcode
		$(this).css('display', 'block');

	});



	$('#loadGif').click(function (){
		$('.loadinggif').show();
	})

	$('.popup').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});


	//quiz stuff
	$("input:image").click(function () {
		$(this).hide();
		$('#'+this.id+'_off').show();
	});

	//display pre questions
	$('#prev_questions').click(function() {
		var htmlcontent = '<div style="padding:10px 10px 20px 50px;"><table border="0" cellpadding="0" cellspacing="0">';
		$.get(prevLinksURL,	function(xml){
			counter=1;
			$(xml).find('question').each(function(){
				htmlcontent += '<tr><td width="4" valign="top"><input align="middle" name="jump_to_qid_'+$(this).attr('qid')+'" id="showbtn'+$(this).attr('qid')+'" value="'+$(this).attr('qid')+'" type="image" src="'+jump_to_image_path+'" onClick="$(\'#jump_to_this_id\').val('+$(this).attr('qid')+');$(this).hide();$(\'#\'+this.id+\'_off\').show()"/><img id="showbtn'+$(this).attr('qid')+'_off" src="'+jump_to_image_off_path+'" alt="Previous" style="display:none;" /></td><td><p class="small"><b>Q'+counter+')</b> '+bbCode(replaceLineBreak($(this).attr('q')))+'</p></td></tr><tr><td colspan="2">&nbsp;</td></tr>';
				counter++;
			});
			htmlcontent += '</table></div>';

			$(xml).find('noprevquestions').each(function(){
				htmlcontent = '<p class="small">No previous questions to show.</p>';
			});
			$(xml).find('error').each(function(){
				//show an error
				htmlcontent = '<p class="small">Sorry please try again.</p>';
			});
			$('#display_q_links').html(htmlcontent);
			$('#display_q_links').slideDown();
		}, 'xml');
	});
	//question bank
	$(':checkbox').click(function(){
			$('#d'+this.id).toggleClass("qselect").toggleClass("qunselect");
			$("#d"+this.id+"[class='qselect']").html('This question will be added to this test <strong>after</strong> you submit below.');
			$("#d"+this.id+"[class='qunselect']").html('Select here to add this question to your test.');
	});
	//whats test are questions in
	$("[id^='sqit']").click(function(){
		var htmlcontent = '';
		var counter=0;
		var display_id = this.id;
		qid = this.id.split('-')[1];//splid this.id for question_id
		$.get(questionInTestsURL+'?qid='+qid, function(xml){

			$(xml).find('test').each(function(){
				counter++;
				htmlcontent += '<p class="nopad">'+counter+') <a href="'+editTestURL+'?test_id='+$(this).attr('id')+'">'+$(this).attr('name')+'</a></p>';

			});

			$(xml).find('notests').each(function(){
				htmlcontent = '<p class="nopad"><span class="qselect">No tests are using this question.</span></p>';
			});
			$(xml).find('error').each(function(){
				//show an error
				htmlcontent = '<p class="nopad"><span class="qselect">Sorry please try again.</span></p>';
			});
			if (counter){
				htmlcontent = '<p class="nopad"><span class="qselect">This question is currently being used in the following test/s:</span></p>' + htmlcontent;
			}

			$('#d'+display_id).html(htmlcontent);
			$('#d'+display_id).slideDown();

		}, 'xml');
	});


	if ( $('#leftnavigation').length ) {
		$(".imgw").bind("load", function(e){ //if two column page, re left nav class, reduce image size if needed
			imgwidth();
		});
	}

});

//change image size if too big for page width
function checkImageSize(Obj){
	myImage = Obj;
	if (myImage.width > 420){
		myImage.width = 420;
	}

}

search = new Array(
		/\[img\](http.[^ \\"\n\r\t<]*?)\[\/img\]/ig,
		/\[url\](http.[^ \\"\n\r\t<]*?)\[\/url\]/ig,
		/\[url=(http.[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/ig,
		/\[b\](.[\s\S]*?)\[\/b\]/ig,
		/\[i\](.[\s\S]*?)\[\/i\]/ig,
		/\[u\](.[\s\S]*?)\[\/u\]/ig,
		/\[s\](.[\s\S]*?)\[\/s\]/ig,
		/\[sup\](.*?)\[\/sup\]/ig,
		/\[sub\](.*?)\[\/sub\]/ig,
		/\[color=([#a-zA-Z0-9]*?)\](.[\s\S]*?)\[\/color\]/ig,
		/\[yt\](.{11,11})\[\/yt\]/ig
		);

replace = new Array(
		"<img src=\"$1\" class=\"imgw\" alt=\"\" border=\"0\">",
		"<a href=\"#\" onclick=\"javascript:window.open('$1','_blank');return false;\">$1</a>",
		"<a href=\"#\" onclick=\"javascript:window.open('$1','_blank');return false;\">$2</a>",
		"<strong>$1</strong>",
		"<i>$1</i>",
		"<u>$1</u>",
		"<del>$1</del>",
		"<sup>$1</sup>",
		"<sub>$1</sub>",
		"<span style='color:$1;'>$2</span>",
		"<br /><object width='415' height='336'><param name='movie' value='http://www.youtube.com/v/$1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/$1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='415' height='336'></embed></object>"
		);
function bbCode(str){

	if( str.indexOf("[") == -1) {
		return str;
	}

	for(i = 0; i < search.length; i++) {
		str = str.replace(search[i],replace[i]);
	}

	return str;
}

function imgwidth(){

	$(".imgw").each(function(){

		if ($(this).width() > 350){
			$(this).css({width:'350px'});
		}

	});

}
//self.setTimeout('imgwidth()', 1000)
function replaceLineBreak(str){

	if (str.length)	{
		str = str.replace( /\</g, '&lt;' );
		str = str.replace( /\>/g, '&gt;' );
		return str.replace( /\n/g, '<br />' );
	} else {
		return str;
	}

}

//var multiAnswer	= 1// 1 or 0

function reset_all(id, prefix){
		//deselect images
		$("img[id^='answer_" + id + "_']").attr("src", select_image_off);
		//deselct radio buttons
		$("input[id^='" + prefix + id + "_']").attr("checked", false);

}
function reset_all_radio(id, prefix){
		//deselect images
		$("img[id^='answer_" + id + "_']").attr("src", select_radio_image_off);
		//deselct radio buttons
		$("input[id^='" + prefix + id + "_']").attr("checked", false);

}
function selectButton(id, id2, prefix, multiAnswer){
	//alert(id + '_' + id2);
		if (!multiAnswer)//turn all off so only can select one answer at a time
		{

			$("img[id^='answer_" + id + "_']").attr("src", select_image_off);
			$("input[id^='" + prefix + id + "_']").attr("checked", false);

		}
		//toggle on and off
		if ($("img[id^='answer_" + id + '_' + id2 + "']").attr("src") == select_image_off)
		{
			$("img[id^='answer_" + id + '_' + id2 + "']").attr("src", select_image_on);//checkbox_activating
			//set hidden checkbox
			$("#" + prefix + id + '_' + id2 + '_rb').attr("checked", true);

		} else
		{
			$("img[id^='answer_" + id + '_' + id2 + "']").attr("src", select_image_off);
			//set hidden checkbox
			$("#" + prefix + id + "_" + id2 + '_rb').attr("checked", false);
		}

}
function selectRadioButton(id, id2, prefix, multiAnswer){
	//alert(id + '_' + id2);
		if (!multiAnswer)//turn all off so only can select one answer at a time
		{

			$("img[id^='answer_" + id + "_']").attr("src", select_radio_image_off);
			$("input[id^='" + prefix + id + "_']").attr("checked", false);

		}
		//toggle on and off
		if ($("img[id^='answer_" + id + '_' + id2 + "']").attr("src") == select_radio_image_off)
		{
			$("img[id^='answer_" + id + '_' + id2 + "']").attr("src", select_radio_image_on);//checkbox_activating
			//set hidden checkbox
			$("#" + prefix + id + '_' + id2 + '_rb').attr("checked", true);

		} else
		{
			$("img[id^='answer_" + id + '_' + id2 + "']").attr("src", select_radio_image_off);
			//set hidden checkbox
			$("#" + prefix + id + "_" + id2 + '_rb').attr("checked", false);
		}

}
