String.prototype.trim = function() { return this.replace(/^\++|\++$/g, ""); };

// /T/N/K/D/ Custom functions.
// Greg Miell 2010. All rights reserved.
// If you are copying any of these functions please leave this header intact

// Global Objects

// jQueryHolder holds jQuery objects by selector for re-use
function jQueryHolder(debug) {
	this.count = 0;
	this.debug = (debug == true);
	this.get = function (idSelector) {
		if (typeof(this[idSelector]) == 'undefined') {
			this[idSelector] = $(idSelector);
			this.count++;
			this.writeDebug();
		}
		return this[idSelector];
	}
	this.destroy = function (idSelector) {
		if (typeof(this[idSelector]) == 'undefined') {
			return false;
		}
		this[idSelector] = null;
		this.count--;
		this.writeDebug();
		return true;
	}
	this.writeDebug = function () {
		if(this.debug == true) {
			this.get('#jhdebug').text(this.count);
		}
	}

	this.update = function (idSelector) {
		this[idSelector] = $(idSelector);
		return this[idSelector];
	}
}

// Global Variables

// jQueryHold is an instance of jQueryHolder
var jQueryHold = new jQueryHolder(true);

function ajaxCallbackLoadData (json) {
	$.each(json.fields, function (i, item) {
		var itemfunc = "#"+item.func;
		var itemname = "#"+item.name;
		var randomnumber=Math.floor(Math.random()*100)
                if ( jQueryHold.get(itemfunc).children(itemname).length > 0 ) {
			if (item.value == "") { item.value="&nbsp;";}
			jQueryHold.get(itemfunc).children(itemname).text(item.value); //+randomnumber);
		} else {
			//alert("Can't find " + itemfunc + " " + itemname);
		}
	});
};

function ajaxFormReturn(json) {
	alert(json);
};

function ajaxRefresh(index, elem) {
	var remotefunc = this.id;
	var a = jQueryHold.get(this.id).children(".field").map(function() { return this.id; });
	var b = "";
	for (i = 0; i < a.length; i++) {
		b = b + "+" + a[i];
	}
	b = b.trim();
	$.getJSON("ajax.php?f="+remotefunc, { fields: b }, ajaxCallbackLoadData);
};

function ajaxify (index, elem) {
	if ($(elem).is("a")) {
		$(elem).click(function () {
			if ( jQueryHold.get("#content").attr("src") == $(this).attr("href") ) {
				return false;
			}
			jQueryHold.get("#content").attr("src", $(this).attr("href"));
			jQueryHold.get("#content").fadeOut(400, function() {
				jQueryHold.get("#content").load(jQueryHold.get("#content").attr("src")+"&ajax=1", function () {
					$("#content .ajax").each(ajaxify);
					jQueryHold.get("#content").each(linksetup);
				 });
				$(this).oneTime(150, function() {
					jQueryHold.get("#content").fadeIn(400);
				});
			});
			return false;
		});
	} else if ($(elem).is("form")) {
		$(elem).submit(function() {
			$(this).attr("disabled", "disabled");
                        jQueryHold.get("#content").attr("src", $(this).attr("action"));
			jQueryHold.get("#content").attr("formserial", $(this).serialize());
                        jQueryHold.get("#content").fadeOut(400, function() {
                                jQueryHold.get("#content").load($(this).attr("src")+"&ajax=1", $(this).attr("formserial"), function () { $("#content .ajax").each(ajaxify); jQueryHold.get("#content").each(linksetup) });
				$(this).removeAttr("formserial");
                                $(this).oneTime(150, function() {
                                        jQueryHold.get("#content").fadeIn(400);
                                });
                        });
//			$.getJSON("ajax.php"+$(elem).attr("action"), ajaxFormReturn);
			return false;
		});
	}
};

function animatePause() {
	if (jQueryHold.get("#1").attr("pause")=="1") {
		jQueryHold.get('#1').attr("pause", "0");
	} else {
		jQueryHold.get('#1').attr("pause", "1");
	}
};

function animateFunc() {
        var b = this.id*1;
	if (jQueryHold.get('#1').attr("pause")=="1" && b==1) {
		jQueryHold.get('#'+b).stopTime();
		jQueryHold.get('#'+b).oneTime(200,animateFunc);
		return true;
	};

        var a = b - 1;
        var c = b + 1;
        if (a < 1) { a = 5; }
        if (c > 5) { c = 1; }

	jQueryHold.get("#"+a).stop(true, true).removeClass("menuhl", 1250);
	jQueryHold.get("#"+(a+5)).stop(true, true).removeClass("menuhl", 1250);
	jQueryHold.get('#'+b).stop(true, true).addClass("menuhl", 1250);
	jQueryHold.get("#"+(b+5)).stop(true, true).addClass("menuhl", 1250);
	var offset = 0;
	if (c == 1) { offset = 4000; }
	jQueryHold.get('#'+c).stopTime();
	jQueryHold.get("#"+c).oneTime(750+offset, animateFunc);
};

function linksetup () {
	$(this).find("a.ajax").each(ajaxify);
	$(this).find("form.ajax").each(ajaxify);
	$(this).find("a.menuchl").each( function() {
		$(this).removeClass("menuchl");
                $(this).mouseover( function() {
                        $(this).stop(true, true);
                        $(this).addClass("menuhl", 500);
                });
                $(this).mouseout( function() {
                        $(this).stop(true, true);
                        $(this).removeClass("menuhl", 500);
                });
        });
};

function setupAjaxRefresh() {
	$(".ajaxrefresh").not(".ajaxactive").each(function(index, elem) {
		$(this).stopTime();
		$(this).everyTime(5000, $(this).attr("id"), ajaxRefresh);
		$(this).addClass("ajaxactive");
	});
};

function loadDiv(selector, url) {
	if ( jQueryHold.get(selector).attr("src") == url ) {
		return false;
	}
	jQueryHold.get(selector).load(url, function() {
		setupAjaxRefresh();
	});
	jQueryHold.get(selector).attr("src", url);
};

$(function() {
	jQueryHold.get('#content').attr("src", "?page=index");
	$(".btn").button();

	if ( jQueryHold.get('#myDiv').length ) {
		jQueryHold.get("#infoborder").corner("round 8px").parent().corner("round 8px");
	};
	

	$("#1, #2, #3, #4").click(animatePause);
	jQueryHold.get("body").each(linksetup);

	jQueryHold.get("#1").oneTime(1, animateFunc); 
	
	setupAjaxRefresh();
	$("form").form();
	$("input:text, input:password").ezpz_hint();
});
