/* =================================================================================================== */
/*  iAct_Engine - scripts file                                                                         */
/* =================================================================================================== */

$(function(){
	$(".fg-button:not(.ui-state-disabled)")
	.hover(
		function(){
			$(this).addClass("ui-state-hover");
		},
		function(){
			$(this).removeClass("ui-state-hover");
		}
	)
})


//tipsy
$(function(){
	$('.tipsy').tipsy({gravity: 's'});
})

function replaceValueCustom(what, cssClass, whatToHide){
	var elem = $("#"+what);
	if($.trim(elem.val()) == ""){
		elem.val( elem.attr('alt') );
		if(cssClass)
			$("#" + what).addClass(cssClass);
		if(whatToHide){
			$("#" + whatToHide).hide();
		}
		elem.height(18);
	}
}

function replaceValue(what, cssClass){

	var elem = $("#"+what);
	if($.trim(elem.val()) == ""){
		elem.val( elem.attr('alt') );
		if(cssClass)
			$("#" + what).addClass(cssClass);
	}

}

function checkValue(what, cssClass){

	var elem = $("#"+what);
	if( $.trim(elem.val()) == elem.attr('alt') ){
		elem.val('');
		if(cssClass)
			$("#" + what).removeClass(cssClass);
	}

}


function EnforceMaximumLength(fld,len) {
	if(fld.value.length > len) { fld.value = fld.value.substr(0,len); }
}

/*
	jQuery TextAreaResizer plugin
	Created on 17th January 2008 by Ryan O'Dell
	Version 1.0.4

*/
(function($) {
	/* private variable "oHover" used to determine if you're still hovering over the same element */
	var textarea, staticOffset;  // added the var declaration for 'staticOffset' thanks to issue logged by dec.
	var iLastMousePos = 0;
	var iMin = 32;
	var grip;
	/* TextAreaResizer plugin */
	$.fn.TextAreaResizer = function() {
		return this.each(function() {

			if($(this).hasClass('processed'))
				return true;

		    textarea = $(this).addClass('processed'), staticOffset = null;

			// 18-01-08 jQuery bind to pass data element rather than direct mousedown - Ryan O'Dell
		    // When wrapping the text area, work around an IE margin bug.  See:
		    // http://jaspan.com/ie-inherited-margin-bug-form-elements-and-haslayout
		    $(this).wrap('<div class="resizable-textarea"><span></span></div>')
		      .parent().append($('<div class="grippie"></div>').bind("mousedown",{el: this} , startDrag));

		    var grippie = $('div.grippie', $(this).parent())[0];
		    grippie.style.marginRight = (grippie.offsetWidth - $(this)[0].offsetWidth) +'px';

		});
	};
	/* private functions */
	function startDrag(e) {
		textarea = $(e.data.el);
		textarea.blur();
		iLastMousePos = mousePosition(e).y;
		staticOffset = textarea.height() - iLastMousePos;
		textarea.css('opacity', 0.25);
		$(document).mousemove(performDrag).mouseup(endDrag);
		return false;
	}

	function performDrag(e) {
		var iThisMousePos = mousePosition(e).y;
		var iMousePos = staticOffset + iThisMousePos;
		if (iLastMousePos >= (iThisMousePos)) {
			iMousePos -= 5;
		}
		iLastMousePos = iThisMousePos;
		iMousePos = Math.max(iMin, iMousePos);
		textarea.height(iMousePos + 'px');
		if (iMousePos < iMin) {
			endDrag(e);
		}
		return false;
	}

	function endDrag(e) {
		$(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
		textarea.css('opacity', 1);
		textarea.focus();
		textarea = null;
		staticOffset = null;
		iLastMousePos = 0;
	}

	function mousePosition(e) {
		return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop };
	};
})(jQuery);


function showAlert(message, type){

	$('<div/>')
	.attr('id', 'dialog-holder-for-editing')
	.dialog({
		position: [300, 60],
		modal : true,
		width: 620,
		title : 'Message',
		buttons: {
			"Close": function() {
				$(this).remove();
			}
		},
		closeOnEscape : true,
		close: function() {
			$(this).remove();
		}
	})
	.html('<div class="notification message_'+ type +'" id="notification">'+message+'</div>');

	if(type != 'alert'){
		setTimeout( function(){
			$( '#dialog-holder-for-editing' )
			.fadeOut( function(){
				$(this).remove();
			});
		}, 3000 );
	}

};


function makeModal(injectWhere, selector, target, params, reloadParent) {
	//alert(params.modal_width);
	newwidth = 600;
	if(params && params.modal_width){
		newwidth = params.modal_width;
	}

	return $(injectWhere).html(iact.loadingElem).load(target, params).dialog({

		autoOpen: true,
        width: parseInt(newwidth),
        modal: true,
        position: [300, 60],
		closeOnEscape : true,
		open: function (){
		},
        buttons: {
            "Cancel": function() {
				$(this).dialog('close');
				$(this).dialog( 'destroy' );
            }
        },
		close : function(event, ui) {

			if(reloadParent == 'reload') {
				window.location.reload();
			} else {
				$(this).remove();
				$('<div id="dialog"></div>').appendTo('#td_content');
			}
        }
    });
}

function setHash(h){
	window.location.hash = h;
	return false;
}

function getHash(){
	var hash = window.location.hash.replace('#', '');
	return hash;
}

$().ajaxStart(function() {
    $("#loading").pause(2000).show();
});

$().ajaxStop(function() {
    $("#loading").clearQueue().hide();
});
/*
 * Jonathan Howard
 *
 * jQuery Pause
 * version 0.2
 * Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
 *
 * Feel free to do whatever you'd like with this, just please give credit where
 * credit is do.
 *
 * pause() will hold everything in the queue for a given number of milliseconds,
 * or 1000 milliseconds if none is given.
 *
 * unpause() will clear the queue of everything of a given type, or 'fx' if no
 * type is given.
 */

$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};

$.fn.clearQueue = $.fn.unpause = function(type) {
	return this.each(function(){
		type = type || "fx";
		if(this.queue && this.queue[type]) {
			this.queue[type].length = 0;
		}
	});
};
//---------------------------------------------------------------------------------------*/

// change input type on forms from text to password
function changeInputType(oldObject, oType) {
    var newObject = document.createElement('input');
    newObject.type = oType;
    if(oldObject.size) newObject.size = oldObject.size;
    //if(oldObject.value) newObject.value = oldObject.value;
    if(oldObject.name) newObject.name = oldObject.name;
    if(oldObject.id) newObject.id = oldObject.id;
    newObject.tabIndex = "2";
    if(oldObject.className) newObject.className = oldObject.className;
    oldObject.parentNode.replaceChild(newObject,oldObject);
    newObject.focus();
    return newObject;
}



function jMessage(what, where){
	$.jGrowl(what, { theme: 'alert',  position:'center' });

}


function showObject(obj){
	var str = '';
	for(prop in obj){
		str+=prop + " value: "+ obj[prop]+"\n";
	}
	alert(str);
}


function showConsoleObject(obj){

	if(null !== obj && 'object' == typeof(obj)){

		$.each( obj, function(key, value) {
		   //alert('name='+ key + ' value=' +value + '\n');
		   console.log("The value of ", key, " is ", value);
		});
	} else {
		console.log(obj);
	}
}


$(function (){

	$.fn.iact_findUL = function(){

		return $( this ).parent( 'li' ).next( 'li' ).children( 'ul' );
	}

	$( '#pmenu a.top' ).not( '.down' )
		.mouseover( function (ev){
			$( '#pmenu ul' ).fadeOut( 'slow' );
			$( this ).iact_findUL().css({
				'position' : 'absolute',
				'z-index' : 100,
				'top' : 32

				/*, 'left' : -104*/

			}).fadeIn( 'fast' );

		} )
		.hover(
			function (ev){

				var timeout = $( this ).iact_findUL().data('timeout');

				if( timeout )
					clearTimeout(timeout);

			},
			function (ev){

				var ul = $( this ).iact_findUL().get();

				$( ul ).data( 'timeout' ,setTimeout(function(){
					$( ul ).fadeOut( 'slow' ).css( 'z-index', '99' );
				}, 0 ) );


			}
		).iact_findUL().hover(
			function(){
				var timeout = $(this).data('timeout');
				if(timeout)
					clearTimeout(timeout)
			},
			function(){
				var _this = this;
				$(_this).data('timeout', setTimeout(function(){
					$(_this).fadeOut( 'slow' );
				}, 0));
			}
		);
	$('#pmenu').css({'position': 'relative',
					 'z-index' : 400});
});
