/**
 * Author: Dietrich Rordorf
 * (c) 2006-2007 Dietrich Rordorf <rordorf@mdpi.org>
 **/

/**
 * search function update
 */

function searchResults(hash)
{
	$('.searchPanel > .updateSearchResults').next().css("visibility", "visible");
	$.ajax({
		url: './ajax/search/?hash=' + hash,
			dataType: "json",
			success: function(ret){
					$('#searchResults').html(ret.html);
					$('.searchPanel > .updateSearchResults').next().css("visibility", "hidden");
					$('.searchPanel > .ppp').val(ret.ppp);
					$('.searchPanel > .page').val(ret.page);
					tmpurl = window.location.href;
					tmpurl = tmpurl.split("#");
					
					
					$('.searchPanel > .pagelinks').html(ret.pages_html.replace(/#/g,tmpurl[0] + "#"));
					tmptext = ret.count + " result";
					if(ret.count>1)
						tmptext += "s";
					tmptext += " on " + ret.pages + " page";
					if(ret.pages>1)
						tmptext += "s";
					tmptext += ". Displaying page " + ret.page + ".";
					
					$('.searchPanel > .label').text(tmptext);
			}
	});
}

/**
 * function for popups
 **/
function windowOpen(aURL, aHeight, aWidth) {
   aNewWindow = window.open(aURL, 'aNewWindow', 'location=0, menubar=0, height='+ aHeight +', width='+ aWidth +', status=0, toolbar=0, top=20, left=20, scrollbars=1');
   aNewWindow.focus();
}

/**
 * function to check if export is possible or not (articles selected or not)
 */
function export_check()
{
	for(i = 0; i < document.article_ops.elements.length; i++)
	{
		elm = document.article_ops.elements[i];
		if(elm.type == 'checkbox')
		{
			if(elm.checked == true && elm.className != 'selector')
			{
					return true;
			}
		}
	}
	alert('No article(s) selected');
	return false;
}


/**
 * function to toggle abstract in article list view
 */
function toggleAbstract(aId)
{

	if($('#article' + aId).css('display') == 'none')
	{
		if($('#article' + aId).html().length > 0)
		{
			$('#article' + aId).css({display: 'block'});
		}
		else
		{
			$.blockUI({ message: $('#ajaxDOML') });
			$.ajax({
				url: './ajax/show_abstract/' + aId,
				success: function(html){
					$('#article' + aId).html(html);
					$('#article' + aId).css({display: 'block'});
					$.unblockUI();
				}
			});
		}
	}
	else
	{
		$('#article' + aId).css({ display: 'none'});
	}
}

function toggleConferenceAbstract(aId)
{

	if($('#article' + aId).css('display') == 'none')
	{
		if($('#article' + aId).html().length > 0)
		{
			$('#article' + aId).css({display: 'block'});
		}
		else
		{
			$.blockUI({ message: $('#ajaxDOML') });
			$.ajax({
				url: './ajax/show_conference_abstract/' + aId,
				success: function(html){
					$('#article' + aId).html(html);
					$('#article' + aId).css({display: 'block'});
					$.unblockUI();
				}
			});
		}
	}
	else
	{
		$('#article' + aId).css({ display: 'none'});
	}
}

function mp_toggle(block)
{
	if($('#' + block).css('display') == 'none')
	{
		$('#' + block).css({display: 'block'});
	}
	else
	{
		$('#' + block).css({display: 'none'});
	}
}


/**
 * function to update search-form
 */
function update_search_form(changer)
{
  if(changer.name == 's_conference')
  {
  	// update section and special issue
	$.ajax({
		url: "frontend/ajax/get_sections_by_conference/" + changer.value,
		success: function(html) {
			$('#s_section').html(html);
		}
	});
  }
}

/**
 * mathcaptcha reload function
 */
$(document).ready(function(){
	$("#newcaptcha").click(function() {
		$("#mathcaptcha").attr({
			src: "./lib/mathcaptcha/mathcaptcha.php?" + Math.random()*10000
		});
	});
	$('input.focus').focus();
	
	// "Navigation" popup window
	$('#navigation a.section').click(function () {
		$('#detail').html( $('#' + 'detail-' + $(this).attr('id')).html() );
		return false;
	});
});

/**
 * searchbar clear function
 */

function clear_searchform()
{
	$('#search :input').each(function() {
		var type = this.type;
		var tag = this.tagName.toLowerCase();

		if(type == 'text')
			this.value = "";
			
		else if (tag == 'select')
			this.selectedIndex = 0;

		if(this.name == 's_conference')
		{
			updateSearchSection(0);
		}
	});
}

/**
 * abstract_list start
 */
$(document).ready(function () {
	
	var commentPages = function () {
//		select_unselect();							// select or unselect operation bind
		$('.pages a').unbind('click').click(page_function);			// page link bind
		$('#confirm_show').unbind('click').click(confirm_function);	// Confirm button bind
		deleteconfirm();
	};
	
	/**
	 * pagination function
	 */
	var page_function = function () {
		$.blockUI({ message: $('#ajaxDOML') });
		$.ajax({
			url: $(this).attr('href') + '&ajax=1',
			success: function(html){
				$('#maincol').html(html);
				commentPages();		// confirm operation bind 
				$.unblockUI();
			}
		});
	};
	
	/**
	 * "Confirm type" and "Articles per page" operation
	 */
	var confirm_function = function () {
		$.blockUI({ message: $('#ajaxDOML') });
		$.ajax({
			url: $('#abstract_list_form').attr('action') + '&ajax=1&showtype=' + $('#showtype').val() + '&pageCount=' + $('#pageCount').val(),
			success: function(html){
				$('#maincol').html(html);
				commentPages();		// confirm operation bind
				$.unblockUI();
			}
		});
	};
	
	var deleteconfirm = function () {
		$('.confirm').unbind('click').click(function () {
			if (confirm('Are you sure?')) {
				$.blockUI({ message: $('#ajaxDOML') });
				$.ajax({url: $(this).attr('href') + '&ajax=1'});	// delete url
				// update the panel
				$.ajax({
					url: 'conference/abstract_list/?ajax=1',
					success: function(html){
						$('#maincol').html(html);
						bind_actions();		// confirm operation bind 
						$.unblockUI();
					}
				});
			}
			return false;
		});
	}
	resizefunc();
	$(window).resize(resizefunc);
	
//	select_unselect();							// select or unselect operation bind
	
	$('#convert_video').click(function () {
		
		$('input[@name^=articles]').each(function () {
			if ( this.checked ) {
				$.blockUI({ message: $('#ajaxDOML') });
				$.ajax({
					url : 'user/abstract_convert_video/' + this.value,
					success : function(html) {
						$.unblockUI();
					}
				});
			}
		});
		
		return false;
	});
//	commentPages();
});

function resizefunc() {
	var scope = $('#maincol');
	$('.thickbox', scope).each(function (i, item) {
		var th = $(this);
		var href = th.attr('href');
		var width = $(window).width() - 40;
		var height = $(window).height() - 100;
		th.attr('href', href + '&width=' + width + '&height=' + height);
		var divembed = th.next();
		$('embed', divembed).attr('width', (width - 20));
		$('embed', divembed).attr('height', height);
	});
}
/**
 * abstract_list end
 */

// abstract_upload start
$(document).ready(function () {
	if(1 == $('#isadd').val() && 1 == $('#numaff').val()) {
		$("input[@name='affiliation[0][number]']").parent().parent().hide();
	}
	$('#video_script').hide();
	$('input[name=record_video]').click(function () {
		if (this.checked) {
			$('#video_script').show();
		} else {
			$('#video_script').hide();
		}
	});
});
// abstract_upload end

/**
 * frontend.js start
 */
var suppFiles = 0;
function addSupplementFile() {
	$('#supplement_div').after('<tr id="suppFile' + suppFiles + '"><td>Supplementary file:</td><td><input type="hidden" name="suupfile' + suppFiles + '"/><input type="file" name="supplement_file' + suppFiles + '" /><a href="javascript:removeSupplementFile(' + suppFiles + ')"><img src="./images/delete.gif" border="0" alt="Remove supplementary file"></a></td></tr>');
	suppFiles++;
}

function removeSupplementFile(num) {
	$('#suppFile' + num).remove();
}

function closeBackendAjax()
{
	$("#backendAjax").hide();
}

function pubmedsubmit(form)
{
	$.blockUI({ 
		message: $('#ajaxDOMP'),
		fadeOut: 10000
		});
	$("#backendAjax").slideUp("slow", function(){
		$("#backendAjaxCon").empty();
		$.post("./backend/pubmedloader",
			{pubmedloaderreport : form.pubmedloaderreport.value },
			function(data){
				$("#backendAjaxCon").html(data);
				$("#backendAjax").slideDown("slow");
				setTimeout($.unblockUI,1000);			
			}
		);
	});
}

function update_form_item(item,url)
{
	$.ajax({
		url: url,
		cache: false,
		success: function(html) {
			$('#' + item).html(html);
		}
	});
}

function update_selectors(changer,journal,section,sissue)
{
  if(changer.name == journal)
  {
  	// update section and special issue
	$.ajax({
		url: "./ajax/select_sections_by_journal/" + changer.value,
		success: function(html) {
			$('#' + section).html(html);
		}
	});
	$.ajax({
		url: "./ajax/select_special_issues_by_journal/" + changer.value,
		success: function(html) {
			$('#' + sissue).html(html);
		}
	});
  }
  else if(changer.name == section)
  {
  	// reset special issues if chosen 0
	if(changer.value == 0)
	{
		$.ajax({
			url: "./ajax/select_special_issues_by_journal/" + $('#' + journal).val(),
			success: function(html) {
				$('#' + sissue).html(html);
			}
		});
	}
	else
	{
		// update special_issues
		$.ajax({
			url: "./ajax/select_special_issues_by_section/" + changer.value,
			success: function(html) {
				$('#' + sissue).html(html);
			}
		});
	}
  }
}

/** init file for fck-editor **/
$(document).ready(function(){

	bind_actions();

});

function convert_video() {
	$('.convert').click(function () {
		var url = this.href;
		$.blockUI({ message: $('#ajaxDOML') });
		$.ajax({
			url: url,
			success: function(html){
				$.unblockUI();
				setTimeout(function () {
					alert(html);
				}, 500);
			}
		});
		return false;
	});
}

/**
 * Set require note
 */
function setRequireNote() {
	$('.required sup').text('*');
}

/**
 * The whole bind actions
 */
function bind_actions() {
	setRequireNote();
	delete_confirm();
	usermenu_action();
	hover_table();
	convert_video();
}

/**
 * confirm dialog
 */
function delete_confirm() {
	$('.confirm').click(function () {
		return confirm('Are you sure?');
	});
}

/**
 * hover table action
 */
function hover_table() {
	// hover table
	$('.hovertable tbody tr').not('th').hover(function () {
		$(this).addClass('hoverclass');
	}, function () {
		$(this).removeClass('hoverclass');
	});
}

/**
 * The user menu style 
 */
function usermenu_action() {
	$('#usermenu > li').addClass('horizontal');
}

var confirm_operation = function () {
	$('.confirm').click(function () {
		return confirm('Are you sure?');
	});
};

var select_unselect = function () {
	$('.select_unselect').click(function () {
		var checked = this.checked;
		$('input[@type=checkbox]').each(function () {
			this.checked = checked;
		});
	});
};

/*
 *
 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 2.2.2
 * Demo: http://www.texotela.co.uk/code/jquery/select/
 * 
 * $LastChangedDate$
 * $Rev$
 *
 */
 
;(function($) {
 
/**
 * Removes an option (by value or index) from a select box (or series of select boxes)
 *
 * @name     removeOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    String|RegExp|Number what  Option to remove
 * @param    Boolean selectedOnly       (optional) Remove only if it has been selected (default false)   
 * @example  $("#myselect").removeOption("Value"); // remove by value
 * @example  $("#myselect").removeOption(/^val/i); // remove options with a value starting with 'val'
 * @example  $("#myselect").removeOption(/./); // remove all options
 * @example  $("#myselect").removeOption(/./, true); // remove all options that have been selected
 * @example  $("#myselect").removeOption(0); // remove by index
 *
 */
$.fn.removeOption = function()
{
	var a = arguments;
	if(a.length == 0) return this;
	var ta = typeof(a[0]);
	var v, index;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(ta == "string" || ta == "object" || ta == "function" ) v = a[0];
	else if(ta == "number") index = a[0];
	else return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			// clear cache
			if(this.cache) this.cache = null;
			// does the option need to be removed?
			var remove = false;
			// get options
			var o = this.options;
			if(!!v)
			{
				// get number of options
				var oL = o.length;
				for(var i=oL-1; i>=0; i--)
				{
					if(v.constructor == RegExp)
					{
						if(o[i].value.match(v))
						{
							remove = true;
						}
					}
					else if(o[i].value == v)
					{
						remove = true;
					}
					// if the option is only to be removed if selected
					if(remove && a[1] === true) remove = o[i].selected;
					if(remove)
					{
						o[i] = null;
					}
					remove = false;
				}
			}
			else
			{
				// only remove if selected?
				if(a[1] === true)
				{
					remove = o[index].selected;
				}
				else
				{
					remove = true;
				}
				if(remove)
				{
					this.remove(index);
				}
			}
		}
	);
	return this;
};

/**
 * Sort options (ascending or descending) in a select box (or series of select boxes)
 *
 * @name     sortOptions
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @param    Boolean ascending   (optional) Sort ascending (true/undefined), or descending (false)
 * @example  // ascending
 * $("#myselect").sortOptions(); // or $("#myselect").sortOptions(true);
 * @example  // descending
 * $("#myselect").sortOptions(false);
 *
 */
$.fn.sortOptions = function(ascending)
{
	var a = typeof(ascending) == "undefined" ? true : !!ascending;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			// create an array for sorting
			var sA = [];
			// loop through options, adding to sort array
			for(var i = 0; i<oL; i++)
			{
				sA[i] = {
					v: o[i].value,
					t: o[i].text
				}
			}
			// sort items in array
			sA.sort(
				function(o1, o2)
				{
					// option text is made lowercase for case insensitive sorting
					o1t = o1.t.toLowerCase(), o2t = o2.t.toLowerCase();
					// if options are the same, no sorting is needed
					if(o1t == o2t) return 0;
					if(a)
					{
						return o1t < o2t ? -1 : 1;
					}
					else
					{
						return o1t > o2t ? -1 : 1;
					}
				}
			);
			// change the options to match the sort array
			for(var i = 0; i<oL; i++)
			{
				o[i].text = sA[i].t;
				o[i].value = sA[i].v;
			}
		}
	);
	return this;
};

/**
 * Adds (single/multiple) options to a select box (or series of select boxes)
 *
 * @name     addOption
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @type     jQuery
 * @example  $("#myselect").addOption("Value", "Text"); // add single value (will be selected)
 * @example  $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected)
 * @example  $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select
 *
 */
$.fn.addOption = function()
{
	var add = function(el, v, t, sO)
	{
		var option = document.createElement("option");
		option.value = v, option.text = t;
		// get options
		var o = el.options;
		// get number of options
		var oL = o.length;
		if(!el.cache)
		{
			el.cache = {};
			// loop through existing options, adding to cache
			for(var i = 0; i < oL; i++)
			{
				el.cache[o[i].value] = i;
			}
		}
		// add to cache if it isn't already
		if(typeof el.cache[v] == "undefined") el.cache[v] = oL;
		el.options[el.cache[v]] = option;
		if(sO)
		{
			option.selected = true;
		}
	};
	
	var a = arguments;
	if(a.length == 0) return this;
	// select option when added? default is true
	var sO = true;
	// multiple items
	var m = false;
	// other variables
	var items, v, t;
	if(typeof(a[0]) == "object")
	{
		m = true;
		items = a[0];
	}
	if(a.length >= 2)
	{
		if(typeof(a[1]) == "boolean") sO = a[1];
		else if(typeof(a[2]) == "boolean") sO = a[2];
		if(!m)
		{
			v = a[0];
			t = a[1];
		}
	}
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			if(m)
			{
				for(var item in items)
				{
					add(this, item, items[item], sO);
				}
			}
			else
			{
				add(this, v, t, sO);
			}
		}
	);
	return this;
};



})(jQuery);
/**
 * frontend.js end
 */