$(document).ready(function()
{
	$('.checkallgroup').checkall();
	$('.fieldswap').fieldswap();
	$('.nn-info-pop').nninfopop();
});
(function($){
	$.fn.nnimage = function(options, _function){ 
		var defaultoptions = {};  
		var options = $.extend(defaultoptions, options);
			
		return $(this).each(function(){
			var image = new Image(); 
			image.src = options.src;
			if(options.title !== undefined){image.title = options.title;}
			if(options.alt !== undefined){image.alt = options.alt;}
			if(options.height !== undefined){image.height = options.height;}
			if(options.width !== undefined){image.width = options.width;}
			if(options.id !== undefined){image.id = options.id;}

			if(_function !== undefined)
			{
				image.onload = _function; 
			}
			this.appendChild(image);
		});
	};
})(jQuery);

(function($){
	$.fn.nnpagination = function(options){
		return this.each(function(){
			$(this).click(function()
			{
				$('.ajax-pagination-content').html('<img src="\/static\/common\/images\/ajax\/loading-animation.gif" alt="" \/>');
				$('.pagination a').removeClass('current');
				$(this).addClass('current');
				
				var $sUrl = $(this).attr('href');
				if($sUrl.indexOf("/ajax") != 0)
				{
					$sUrl = '/ajax' + $sUrl;
				}
				$.post($sUrl, function(response)
				{
					$('.ajax-pagination-content').html(response);
				});
				return false;
			});
		});
	};
})(jQuery);


/*
* jQuery function: nninfopop
* Description: Shows a layer, commmonly containing additional information, onmouseover.
			   If clicked the layer will stay onmouseout and become draggable.
			   The layer automatically positions itself where thre's enough room (ie left/right & top/bottom).
			   It's always relative to the icon.
			   jqueryui core & draggable are loaded on the fly when needed.
* Dependencies: jquery.ui.core.js & jquery.ui.draggable.js (onclick functionality enables drag)
*/
(function($){  
	$.fn.nninfopop = function(options){
		
		var defaultoptions = {};  
		var options = $.extend(defaultoptions, options);
		
		
		return this.each(function(){
			var obj = $(this);
			obj.show();
			
			$(this).data('toggle', true);
			
			if($(this).attr('class').match('sticky'))
			{
				obj.click(function()
				{
					obj.data('toggle', !obj.data('toggle'));
					
					if($('body').data('ui.draggable-loaded') === undefined)
					{
						$('body').append('<script type="text/javascript" src="/static/common/js/jquery-ui/jquery.ui.core.js"></script>');
						$('body').append('<script type="text/javascript" src="/static/common/js/jquery-ui/jquery.ui.draggable.js"></script>');
						$('body').data('ui.draggable-loaded', true);
						obj.data('layer').draggable();
					}
					else
					{
						obj.data('layer').draggable();
					}
				});
			}
			
			obj.mouseover(function()
			{
				$('.nn-info-pop').data('toggle', true);
				$('.nn-info-pop-layer').hide();
				
				if($(this).data('layer') === undefined)
				{
					oInfo = $(this).next('div');
					oInfo.after('<div class="nn-info-pop-layer"><div class="nn-info-pop-layer-title">' + $(this).attr('title') + '<\/div><div class="nn-info-pop-layer-content">' + oInfo.html() + '<\/div><\/div>');
					oLayer = oInfo.next('.nn-info-pop-layer');
					oLayer.hide();
					$(this).attr('title', '');
					$(this).data('layer', oLayer);
				}
				
				aItemPosition = $(this).position();
				iViewPortWidth = $(window).width();
				iViewPortHeight = $(window).height();
				iVievPortPositionTop = aItemPosition.top - $(window).scrollTop();
				iVievPortPositionLeft = aItemPosition.left - $(window).scrollLeft();
				
				iItemHeight = $(this).height()/2;
				iLayerHeight = oLayer.height();
				iTop = (iVievPortPositionTop + iLayerHeight < iViewPortHeight) ? aItemPosition.top  : aItemPosition.top - iLayerHeight + iItemHeight;
				
				iItemWidth = $(this).width();
				iLayerWidth = oLayer.width();
				iLeft = (iVievPortPositionLeft + iLayerWidth + iItemWidth < iViewPortWidth) ? aItemPosition.left + iItemWidth : aItemPosition.left - iLayerWidth - 10;
								
				$(this).data('layer').css('top', iTop);
				$(this).data('layer').css('left', iLeft);
				$(this).data('layer').show();
			});
			
			obj.mouseout(function()
			{
				if($(this).data('layer') !== undefined && $(this).data('toggle'))
				{
					$(this).data('layer').hide();
				}
			});
			
			obj.focus(function(){$(this).trigger('mouseover');});
			obj.blur(function(){$(this).trigger('mouseout');});
		});	
	};
})(jQuery);


/*
* jQuery function: fieldswap
* Description: Toggles the value in an input box.
*/
(function($){  
	$.fn.fieldswap = function(options){
		var defaultoptions = {};  
		var options = $.extend(defaultoptions, options);
		return this.each(function(){
			var obj = $(this);
			obj.focus(function()
			{
				if($(this).data('value') === undefined)
				{
					$(this).data('value', $(this).val());
				}
				
				if($(this).data('value') == $(this).val())
				{
					$(this).val('');
				}
			});	
			obj.blur(function()
			{
				if($(this).val() == '')
				{
					$(this).val($(this).data('value'));
				}
			});
		});
	};
})(jQuery);

/*
* jQuery function: checkall
* Description: Adds a check all box that will toggle the checkboxes within the parent element.
* Options:	text: The text that is displayed next to/the toggle box. Default is "Select/Deselect All"
*/
(function($){  
	$.fn.checkall = function(options){
		var defaultoptions = {  
			text: "Select/Deselect All"
		};  
		var options = $.extend(defaultoptions, options);
		return this.each(function(){
			var obj = $(this);
			obj.append('<label><input type="checkbox" class="checkallselect" />&nbsp;'+options.text+'</label>');
			$(obj.find('.checkallselect')).click(function()
			{
				obj.find('input[@type=checkbox]').attr('checked', $(this).is(':checked'));
			});
		});
	};
})(jQuery);

/*
* jQuery function: confirmdelete
* Description: Adds a confirmation check on click.
* Options:	textBefore: The text that is displayed in front of the options. Default is "Are you sure?"
* 			confirmText: The text that is displayed as the confirm option. Default is "Yes"
* 			cancelText: The text that is displayed as the cancel option. Default is "No"
*/
(function($){  
	$.fn.confirmdelete = function(options){
		var defaultoptions = {  
			textBefore: "Are you sure?",  
			confirmText: "Yes",  
			cancelText: "No"
		};  
		var options = $.extend(defaultoptions, options);
		return this.each(function(){
			var obj = $(this);
			obj.click(function(){
				obj.after('<span class="confirm-delete-box">&nbsp;&nbsp;'+options.textBefore+' <a href="'+obj.attr('href')+'">'+options.confirmText+'</a> - <a href="#" class="confirm-delete-no">'+options.cancelText+'</a></span>');
				$('.confirm-delete-no').click(function(){
					$(this).parent('span').remove();
				});
				return false;
			});
		});
	};
})(jQuery);

/*
* jQuery function: nnclock
* Description: Adds a clock that updates runtime.
* Options:	interval: How often the clock updates in milliseconds. Default is 998 to comensate for processing time.
*			separator: Separator between hour, minute & second. default is ':'
*/
(function($){
	$.fn.nnclock = function(options){
		var defaultoptions = {
			interval: 998,
			separator: ':'
		};  
		var options = $.extend(defaultoptions, options);
		this.html('<span class="hour"></span>' + options.separator + '<span class="minute"></span>' + options.separator + '<span class="second"></span>');
		
		updateClock(options.interval);
	};
})(jQuery);

/*######## Clock Support Functions ########*/

function updateClock(a_iInterval)
{
	iInterval = parseInt(a_iInterval);
	
	var oNow = new Date();
	var iHour = oNow.getHours();
	var iMinute = oNow.getMinutes();
	var iSecond = oNow.getSeconds();
	
	$('.hour').text(appendZero(iHour));
	$('.minute').text(appendZero(iMinute));
	$('.second').text(appendZero(iSecond));
	
	setTimeout("updateClock(iInterval)", iInterval);
}

function appendZero(a_iValue)
{
	if(10 > a_iValue)
	{
		a_iValue = '0' + a_iValue;
	}
	return a_iValue;
}

/*######## Clock Support Functions End ########*/

function parsePictureID($a_sPictureID)
{
	var $sPictureID = -1;
	if($a_sPictureID !== undefined)
	{
		$sPictureID = $a_sPictureID.substr(4);
	}
	return $sPictureID;
}

/**
* Move the caret to any position with in the object (an input or a textarea)
*/
function moveCaretTo(a_oObject, a_iPosition)
{
	if(a_oObject.createTextRange) // IE
	{
		var oRange = a_oObject.createTextRange();
		oRange.move('character', a_iPosition);
		oRange.select();
	}
	else if(a_oObject.setSelectionRange)
	{
		a_oObject.focus();
		a_oObject.setSelectionRange(a_iPosition, a_iPosition);
	}
}

function isAjaxResponseOk($a_sResponse)
{
	if($a_sResponse == '')
	{
		return false;
	}
	return true;
}

function trim(str, chars) {
    if(str === undefined || str == '')
    {
    	return '';
    }
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}