/**
 * @author doStuff, Anders Mogensen 2010
 */

/*
 * JSDDM Menu for dropdown menu in admin
 */
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}
document.onclick = jsddm_close;
// end JSDDM Menu





$(document).ready(function() {
	/*
	 * Add JDSSM Menu
	 */
	$('#jsddm > li').bind('mouseover', jsddm_open);
  	$('#jsddm > li').bind('mouseout',  jsddm_timer);
	
	
	/*
	 * Adds confirm dialogue to all links with class "confirm".
	 * Utilizes jquery UI Dialog.
	 */
	$("a.confirm").click(function() {
		var confirmhref = $(this).attr("href");		
		var dialogueText = "Er du sikker?";
		var dialogueButtonNo = "Afbryd";
		var dialogueButtonYes = "OK";
		
		//try to extract confirm dialogue text and buttons from class definition: [Some text?|Nope, not really|Yes indeed]
		var spl1 = $(this).attr("class").split("[");
		if(spl1.length==2) {
			if(spl1[1].indexOf("]")>-1) {
				//yes, we have a confirm dialogue definition
				var spl2 = spl1[1].split("]");		
				var dialogueOptions = spl2[0];
				dialogueText = dialogueOptions;
				if(dialogueOptions.indexOf("|")>-1) {
					//yes and no buttons seem to exist
					var spl3 = dialogueOptions.split("|");
					
					if(spl3.length==3) {
						dialogueText = spl3[0];
						dialogueButtonNo = spl3[1];
						dialogueButtonYes = spl3[2]; 
					}					
				}						
			}
		}
		
		//create the jquery ui dialog modal confirmation
		var buttons = {};
		buttons[ dialogueButtonYes ] = function() {
			$(this).dialog('close');
			window.location = confirmhref;
		};
		buttons[ dialogueButtonNo ] = function() {
			$(this).dialog('close');
		};
		$("<div>" + dialogueText + "</div>").dialog({
			resizable: false,
			height:138,
			position:['top',100],
			modal: true,
			buttons:buttons
		});
		
		return false;
	});
	
	/*
	 * Tablesorter
	 */
	$("table.tablesorter").each(function() {
		//count number of rows in table
		var rows = $(this).find("tbody tr").size();
		if(rows>0) {
			var widgetstring = "";
			if($(this).hasClass("zebra")) {
				widgetstring = 'zebra';
			}				
			if ($(this).hasClass("paginate")) {
				$(this).tablesorter({
					widthFixed: true,
					widgets: [widgetstring]
				}).tablesorterPager({
					container: $("#tablesorter_pager")
				});
			}
			else {
				$(this).tablesorter({
					widthFixed: true
				});
			}	
		}
			
			
	});
	
	
	/*
	 * Show/hide
	 */
	$(".showHideContent").hide();
	$(".showHideHeader").click(function() {
		var $content = $(this).parent().find(".showHideContent");
		if($content.is(":hidden")) {
			$content.slideDown();
		} else {
			$content.slideUp();
		}
		
		return false;
	});
	
});


(function ($) {
// VERTICALLY ALIGN FUNCTION
// http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);


