$(document).ready(function(){
    $("#ajax_loading").ajaxStart(function(){
    	$(this).css("background-image", "url(../style/common/images/ajax-loader.gif)");
    });
    $("#ajax_loading").ajaxStop(function(){
    	$(this).css("background-image", "none");
    });	    
    
	jQuery.each(jQuery.browser, function(i) {
		if($.browser.mozilla) {
			$(".post_button").addClass("post_button_ff");
		}
        if ($.browser.msie) {
            $(".post_button").addClass("post_button_saf");   
        }
		if($.browser.safari) {
			$(".post_button").addClass("post_button_saf");
		}
	});
	$(".admin_group_list").click(function() {
		var targ = $(this).children("a").attr("href");
		window.location=targ;
	});
	
    $(".admin_group_list, .admin_group_list_nolink").hover(function () {
        $(this).animate({
        	backgroundColor: "#F7D3DB"
		}, 250 );
      }, function () {
	  	$(this).animate({
			backgroundColor: "#E7F3FB"
		}, 500 );      
	});		
	
	$(".conf_message").effect("highlight", {}, 3000);	
});

function killLinks (leave) {
    var llen = leave.length;
    $("a").each(function() {
        if (!$(this).attr("href")) {
            return;   
        }
        if ($(this).attr("href").substr(0, 7) == "mailto:") {
            return;   
        }             
        if (llen > 0) {
            if ($(this).attr("href").substr(0, llen) == leave) {
                return;
            }   
        }
        $(this).attr("href", "#null").css({"cursor":"not-allowed"}).attr("title","{!linkdisabledguide!}");
        if ($(this).parent().hasClass("admin_group_list")) {
            $(this).parent().unbind("click").css({"cursor":"not-allowed"}).attr("title","{!linkdisabledguide!}");
        }
    });   
}

function dalert(text) {
	$("#dialog_box").html(text);
	$("#dialog_box").dialog({ 
		autoOpen: false,
		modal: true,
		title: 'Alert!',
		buttons: { 
		    "Okay": function() { 
				$("#dialog_box").dialog("close").dialog("destroy");
			}
		}
	});		
	$("#dialog_box").dialog("open");		
}

function setmainheight() {
    $(".footer").css({"bottom": "0px"});
    var winheight = $(window).height() - $(document).height();
    $(".footer").css({"bottom": winheight+"px"});
}   

function ar(x) {
//a wrapper for the returning ajax code. Alerts anything in {curly brackets} and logs anything in [square]. Both are then removed from the returned code.
    var errors = x.match(/\{.*?\}/g);
    var log = x.match(/\[.*?\]/g);
    
    for (e in errors) {  
       alert('--'.errors[e]);
       $(".ajaxlog").append('<span class="error">'+errors[e]+'</span><br />');
       x = str_replace(errors[e], "", x);
    }  
    for (e in log) {  
       $(".ajaxlog").append(log[e]+"<br />");
       x = str_replace(log[e], "", x);
    }    
    return x;    
} 

function getUnixTime () {
		var now = new Date;
		var the_time = now.getTime();
		the_time = parseInt(the_time / 1000);
		return the_time;
}


function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}

var MAX_DUMP_DEPTH = 10;
function dumpObj(obj, name, indent, depth) {
      if (depth > MAX_DUMP_DEPTH) {
             return indent + name + ": <Maximum Depth Reached>\n";
      }
      if (typeof obj == "object") {
             var child = null;
             var output = indent + name + "\n";
             indent += "\t";
             for (var item in obj)
             {
                   try {
                          child = obj[item];
                   } catch (e) {
                          child = "<Unable to Evaluate>";
                   }
                   if (typeof child == "object") {
                          output += dumpObj(child, item, indent, depth + 1);
                   } else {
                          output += indent + item + ": " + child + "\n";
                   }
             }
             return output;
      } else {
             return obj;
      }
}

function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof obj[i] == "object") {
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}


function trim(str, chars) {
    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"), "");
}

