// ***********************************************************************************************************//
// *** JQUERY FUNCTIONS

// ***********************************************************************************************************//
// *** TEST FOR JS

		$('html').addClass('js');


	$(function(){

// ***********************************************************************************************************//
// *** DELETE EXAM
		$('div.exam_nfo a img').click(function(event){
			var $mydiv = $(this).parent().parent().parent();
			var examid = $mydiv.attr("id");
			var message = "Do you really want to delete this exam?";
			if(confirm(message))
			{
				$.get('templates/templates.php', 
				{ 'action': 'del_exam', 'examid': examid }, 
				function() {
					$mydiv.slideUp();
				});
			}
			return false;
		});

		$('div.exam_del a img').click(function(event){
			var $myrow = $(this).parent().parent().parent().parent();
			var examid = $myrow.attr("id");
			var message = "Do you really want to delete this exam?";
			if(confirm(message))
			{
				$.get('templates/templates.php', 
				{ 'action': 'del_exam', 'examid': examid }, 
				function() {
					$myrow.addClass('delete_exam');
				});
			}
			return false;
		});

// ***********************************************************************************************************//
// *** DELETE NEWS IMAGE

		$('img.newsimg_del').click(function(event){
			var imgid = $(this).attr("id");
			var $myrow = $(this).parent().parent().parent();
			var message = "Do you really want to delete this image?";
			if(confirm(message))
			{
				$.get('templates/templates.php', 
				{ 'action': 'del_newsimg', 'imgid': imgid }, 
				function() {
					$myrow.addClass('delete_img');
				});
			}
			return false;
		});


// ***********************************************************************************************************//
// *** APPLY logMAR TOOLTIP LINK 

		$('a[href^=logmar]').queue(function(){
			var $this = $(this); 
			var args = $this.attr("href").split('?')[1];
			if(typeof args != 'undefined')
			{
				if(args.charAt(0) == "l") 
				{
					var lm = args.substring(2, args.length);
					$.get("templates/templates.php", {'action': 'lm2sn', 'l' : lm }, 
					function(response){
						if(response.length > 0)
						{
							$this.tooltip({
								bodyHandler: function() { 
									return response;
								},
								showURL: false
							});
						}
					});
				}
			}
		});


// ***********************************************************************************************************//
// *** APPLY OUTSIDE LINK STYLE
		
		$('div.outside_links a').filter(function(){
			return this.hostname && this.hostname !== location.hostname;
		}).addClass('linkout');

// ***********************************************************************************************************//
// *** APPLY NEWS IMAGE STYLE

		$('div.news_txt img, div.print_article img').addClass('news_img'); 
				
// ***********************************************************************************************************//
// *** NEWSLETTER FUNCTIONS


		// newsitem checkbox toggle
		$('.newsitem').click(function(event){
			$chkbox = $(this);
			item_id = $chkbox.attr("id");
			item_class = $chkbox.attr("class").split(' ');
			if(this.checked) {
				// apply used class, add to used items for this nl, display in template
				$chkbox.parent('label').addClass('nl_used_item');
				$.get("templates/templates.php", 
				{'action': 'add_item', 'itemid' : item_id }, 
				function(response){
					// $(response).insertAfter('#drop_' + item_class[1]).addClass('nl_item').slideDown();
					$(response).appendTo('#drop_' + item_class[1]).addClass('nl_item').slideDown();
				});
				
			} else {
				//  remove used class, remove from used items for this nl, remove from template
				$chkbox.parent('label').removeClass('nl_used_item');
				$.get('templates/templates.php', 
				{'action': 'del_item', 'itemid': item_id}, 
				function() {
					$('#temp' + item_id).slideUp('fast');
				});
			}
		}); // end newsitem checkbox toggle
		
		$('#mail_box').click(function(){
			if(this.checked){
				$.get('templates/templates.php', {'action': 'mail_box_on'});
			}else{
				$.get('templates/templates.php', {'action': 'mail_box_off'});
			}
		});
		
		// delete unused item
		$('.delitem').click(function(event){
			var $mydiv = $(event.target).parent().parent();
			var item_id = $mydiv.find('label').attr("for");
			var message = "Are you sure you want to DELETE this? \n Itemid: " + item_id;
			if(confirm(message)){
				$.get('templates/templates.php', 
				{ 'action': 'del_unused', 'itemid': item_id }, 
				function() {
					$mydiv.slideUp();
				});
			}
			return false;
		}); // end delete unused item

// ***********************************************************************************************************//
// *** FONT SIZE CHANGER FUNCTIONS

		if($.cookie('mvtfont') !== null)
		{
			var preferedSize = $.cookie('mvtfont');
			var num = parseFloat(preferedSize, 10);
			$('#content').css('fontSize', num + 'px');
		}

		$('span.ft').click(function() 
		{
			var $content = $('#content');			// use $ in $speech because it is storing a jQuery object
			var currentSize = $content.css('fontSize');
			var num = parseFloat(currentSize, 10);		// convert string to a number, second argument is for base-10
			var unit = currentSize.slice(-2);			// returns a substring beginning at the specified character, expect 'px'
			if (this.id == 'font-large') {
				//if(num < 40) num *= 1.4;			// make larger -- no limit???
				num *= 1.4;
			} else if (this.id == 'font-small') {
				if(num > 14)  num /= 1.4;			// make smaller if > 14px
			}
			$content.css('fontSize', num + unit);
			$.cookie('mvtfont', num, {expires: 365, path: '/'});
			return false;
		});


// ***********************************************************************************************************//
// *** NEWS IMAGE TABS


		$('#newsimagesource').tabs().bind('tabsselect', function(event, ui) {

			if(ui.index < 2) 
			{
				$('#img_alt_input').show();
			}
			else
			{
				$('#img_alt_input').hide();
			}
		});

		
		$('#img_alt_edit').hide();
		
		$('#newsimageedit').tabs({selected:2}).bind('tabsselect', function(event, ui) {

			if(ui.index < 2) 
			{
				$('#img_alt_edit').show();
			}
			else
			{
				$('#img_alt_edit').hide();
			}

		});

// ***********************************************************************************************************//
// *** NEWS Popular/Recommended/Recent LISTS

		$('#news_pop').cycle({ 
    			fx:     'fade', 
    			speed:  'fast', 
    			timeout: 0, 
    			after:   onPopChg,
    			next:   '#next2', 
    			prev:   '#prev2' 
		});

		$('#news_recent').cycle({ 
    			fx:     'fade', 
    			speed:  'fast', 
    			timeout: 0, 
    			next:   '#next1', 
    			prev:   '#prev1' 
		});

		function onPopChg(){
			$('#feednav > span').text(this.id.substring(1));
			return false;
		}

// ***********************************************************************************************************//
// *** news tags edit

		// SELECT FIRST TAG
		$('#newstags').change(function() {
			var tag = $('#newstags option:selected').attr("label");
			$("input[name='cat1']").val(tag);
		}).change();

		// SELECT SECOND TAG
		$('#newstags2').change(function() {
			var tag = $('#newstags2 option:selected').attr("label");
			$("input[name='cat2']").val(tag);
		}).change();

		// RADIO OPERATION CONTROL
		$("input[name='op']").change(function() {
			var op = $("input[name='op']:checked").val();
			var txt = '';
			if(op == "merg") {
				txt = 'Merge From Tag: ';
				$("#merge_to").show();
			} else if(op == "ren") {
				txt = 'Rename Tag: ';
				$("#merge_to").hide();
			} else if(op == "del") {
				txt = 'Delete Tag: ';
				$("#del_conf").text("Are you sure you want to delete tags?").show().fadeOut(1500);
				$("#merge_to").hide();
			} else {
				txt = 'Error - operation undefined ';
			}
			$("input[name='action']").val(op);
			$("#cat1_label").text(txt);
			// $("#merge_to").toggle(true);
			//alert(op);
		}).change();

// ***********************************************************************************************************//
// *** NEWSLETTER ARCHIVE ACCORDION

		$('#nl_archive').accordion({
		header: 'div.nl_title',
		active: false,
		alwaysOpen: false,
		autoHeight: false,
		animated: false
		});

// ***********************************************************************************************************//
// *** FEATURED NEWS

	$('#featnews').cycle({
		fx: 'fade',
		timeout: 20000,
		speed: 1000,
		sync: 1,
		delay: -2000,
		pause: 0,
		pager: '#featnav'
	})


// ***********************************************************************************************************//
// *** PLACE CURSOR IN FORMS

		if (document.login_form) 
		{
			document.login_form.email.focus();
		}
		else if(document.contact_form)
		{
			document.contact_form.sender.focus();
		}
		else if(document.friend_form)
		{
			document.friend_form.to_name.focus();
		}
		else if(document.act_form)
		{
			document.act_form.email.focus();
		}
		else
		{
		}

// ***********************************************************************************************************//		

	}); // end jquery

