function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


if(typeof __ === 'undefined') var __ = new tmlib;

if(typeof $ != "undefined"){
	$(document).ready(function(){
		//--page_home
		if($("#page_home").length > 0){
			var elmHomeBGContainer = $("#bgmainimages");
			
			if(elmHomeBGContainer.length > 0){
				__.homeBGPager = new __.classes.pagerManagerHash({elmsItems: elmHomeBGContainer.find(".bgmainpiece"), doUseHash: false, doShowHide: false
					,oninit: function(){
						this.elmsItems.hide();
						this.getPageCurrent().show();
					}
					,handlerAnimation: new __.classes.pagerAnimatorDissolve({cssZIndexBehind: -3, cssZIndexNormal: -2})
				});
				setInterval(function(){
					__.homeBGPager.switchToNext();
				}, 8000)
			}
/*
		}else if($("#page_home").length > 0){
			var elmHomeBGContainer = $("#bgmainimages");
			
			if(elmHomeBGContainer.length > 0){
				__.homeBGPager = new __.classes.pagerSlidingHash({elmsPages: elmHomeBGContainer.find(".bgmainpiece"), itemSelector:".bgmainpiece", contentWidth: elmHomeBGContainer.find(".bgmainpiece").width()
				});
				
				setInterval(function(){
					__.homeBGPager.switchPagesToNext();
				}, 8000)
	
			}
*/
		}
	});
}


/*-------------
©pagerManagerHash
------------*/
__.classes.pagerManagerHash = function(arguments){
		//--required attributes
		this.elmsItems = arguments.elmsItems || false; if(!this.elmsItems || this.elmsItems.length < 1) return false;
//->return

		//--optional attributes
		this.handlerAnimation = arguments.handlerAnimation || null;
		this.handlerNavigation = arguments.handlerNavigation || null;
		this.classCurrent = arguments.classCurrent || "current";
		this.doCarousel = (typeof arguments.doCarousel != "undefined")? arguments.doCarousel: true;
		this.doShowHide = (typeof arguments.doShowHide != "undefined")? arguments.doShowHide: true;
		this.doUseHash = (typeof arguments.doUseHash != "undefined")? arguments.doUseHash: true;
		this.duration = (typeof arguments.duration != "undefined")? arguments.duration: 500;
		this.oninit = arguments.oninit || null;
		this.onpreswitch = arguments.onpreswitch || null;
		this.onpostswitch = arguments.onpostswitch || null;
		this.boot = arguments.boot || null;

		//--derived attributes
		var fncThis = this;
		this.inprogress = true;
		this.queue = new __.classes.animationQueue();
		
		//--hide all, display first
		if(window.location.hash && this.doUseHash){
			this.idCurrent = window.location.hash;
			this.elmsItems.removeClass(this.classCurrent);
			this.elmsItems.filter(window.location.hash).addClass(this.classCurrent);
		}else{
			var elmCurrentPage = this.getPageCurrent();
			if(elmCurrentPage && elmCurrentPage.length > 0){
			}else{
				elmCurrentPage = this.elmsItems.first();
				elmCurrentPage.addClass(this.classCurrent);
			}
			this.idCurrent = "#"+__.lib.escapeHash(elmCurrentPage.attr("id"));
		}
		if(this.doShowHide){
			this.elmsItems.hide();
			elmCurrentPage.show();
		}
				
		this.inprogress = false;
		
		//--set up event handlers for event manager
		if(this.handlerNavigation){
			this.handlerNavigation.onswitch = function(arguments){
				fncThis.switche(arguments.dataNew);
			}
		}
		
		//--set up animation handler
		if(this.handlerAnimation){
			this.handlerAnimation.onpostswitch = function(arguments){
				fncThis.inprogress = false;
				this.queue.dequeue();
				fncThis.idCurrent = "#"+arguments.elmTo.attr("id");
				fncThis.queue.dequeue();
			}
		}
		
		if(this.oninit)
			this.oninit.call(this);
	}
	__.classes.pagerManagerHash.prototype.switchToPrevious = function(){
		if(this.handlerNavigation)
			this.handlerNavigation.switchToPrevious();
		else{
			var elmCurrent = this.getPageCurrent();
			if(elmCurrent){
				var elmPrevious = elmCurrent.previous();
				if(elmPrevious && elmPrevious.length < 1 && this.doCarousel){
					elmPrevious = this.elmsItems.last();
				}
			}else{
				var elmPrevious = this.elmsItems.last();
			}
			this.switche("#"+elmPrevious.attr("id"));
		}
	}
	__.classes.pagerManagerHash.prototype.switchToNext = function(){
		if(this.handlerNavigation)
			this.handlerNavigation.switchToNext();
		else{
			var elmCurrent = this.getPageCurrent();
			if(elmCurrent){
				var elmNext = elmCurrent.next();
				if(elmNext && elmNext.length < 1 && this.doCarousel){
					elmNext = this.elmsItems.first();
				}
			}else{
				var elmNext = this.elmsItems.first();
			}
			this.switche("#"+elmNext.attr("id"));
		}
	}
	__.classes.pagerManagerHash.prototype.switche = function(argID){
		if(this.inprogress == true || argID == this.idCurrent){
			return false;
		}else{
			var fncThis = this;
			var localVariables = {};
			localVariables.idNext = argID;
			localVariables.elmNext = this.elmsItems.filter(localVariables.idNext);
			localVariables.elmCurrent = this.getPageCurrent();
//->return
			if(localVariables.elmNext.length < 1)
				return false;
			fncThis.inprogress = true;

			if(this.handlerAnimation)
				this.handlerAnimation.switche({elmFrom: localVariables.elmCurrent, elmTo: localVariables.elmNext});
			else{
				var callbackPostShowNext = function(){
					localVariables.elmNext.addClass(fncThis.classCurrent);
					fncThis.inprogress = false;
					if(fncThis.onpostswitch)
						fncThis.onpostswitch.call(fncThis, localVariables);
				}
				var callbackPostHideCurrent = function(){
				}
				
				//--set up queue
				if(fncThis.onpreswitch)
					fncThis.queue.queue({callback: function(){
						fncThis.onpreswitch.call(fncThis, localVariables);
					}});
				if(fncThis.doShowHide){
					fncThis.queue.queue({callback: function(){
							localVariables.elmCurrent.fadeOut(fncThis.duration, function(){
								fncThis.queue.dequeue();
							});
					}});
				}
				fncThis.queue.queue({callback: function(){
					localVariables.elmCurrent.removeClass(fncThis.classCurrent);
					fncThis.idCurrent = localVariables.idNext;
					fncThis.queue.dequeue();
				}});
				if(fncThis.doShowHide){
					fncThis.queue.queue({callback: function(){
						localVariables.elmNext.fadeIn(fncThis.duration, function(){
							fncThis.queue.dequeue();
						});
					}});
				}
				fncThis.queue.queue({callback: function(){
					localVariables.elmNext.addClass(fncThis.classCurrent);
					fncThis.queue.dequeue();
				}});
				if(fncThis.onpostswitch)
					fncThis.queue.queue({callback: function(){
						fncThis.onpostswitch.call(fncThis, localVariables);
					}});
				fncThis.queue.queue({callback: function(){
					fncThis.inprogress = false;
					fncThis.queue.dequeue();
				}});
		
				//--start queue		
				fncThis.queue.dequeue();
			}
		}
	}
	__.classes.pagerManagerHash.prototype.getPageCurrent = function(){
		var fncReturn = false;
		if(this.idCurrent)
			fncReturn = this.elmsItems.filter(this.idCurrent);
		if(!fncReturn || (fncReturn && fncReturn.length < 1))
			fncReturn = this.elmsItems.filter("."+this.classCurrent);
		return (fncReturn && fncReturn.length > 0)? fncReturn: false;
	}


/*-------------
©pagerAnimatorDissolve
------------*/
__.classes.pagerAnimatorDissolve = function(arguments){
		//--optional attribributes
		this.classCurrent = arguments.classCurrent || "current";
		this.cssZIndexBehind = arguments.cssZIndexBehind || -1;
		this.cssZIndexNormal = arguments.cssZIndexNormal || 0;
		this.duration = (typeof arguments.duration != "undefined")? arguments.duration: 500;
		this.oninit = arguments.oninit || null;
		this.onpreswitch = arguments.onpreswitch || null;
		this.onpostswitch = arguments.onpostswitch || null;
		this.boot = arguments.boot || null;
		
		//--derived attributes
		this.inprogress = false;
		this.queue = new __.classes.animationQueue();

		if(this.oninit)
			this.oninit.call(this);
	}
	__.classes.pagerAnimatorDissolve.prototype.switche = function(arguments){
		var fncThis = this;
//-> return
		if(fncThis.inprogress==true) return false;		

		fncThis.inprogress = true;

		var localVariables = {};
		localVariables.elmFrom = arguments.elmFrom;
		localVariables.elmTo = arguments.elmTo;

		//--set up queue
		if(fncThis.onpreswitch)
			fncThis.queue.queue({callback: function(){
				fncThis.onpreswitch.call(fncThis, localVariables);
			}});
		fncThis.queue.queue({callback: function(){
			localVariables.elmToZIndex = localVariables.elmTo.css("z-index") || fncThis.cssZIndexNormal;
			localVariables.elmTo.css("z-index", fncThis.cssZIndexBehind).show();
			localVariables.elmFrom.fadeOut(fncThis.duration, function(){
				fncThis.queue.dequeue();
			});
		}});
		fncThis.queue.queue({callback: function(){
			localVariables.elmTo.css("z-index", localVariables.elmToZIndex).addClass(fncThis.classCurrent);
			localVariables.elmFrom.removeClass(fncThis.classCurrent);
			fncThis.queue.dequeue();
		}});
		if(fncThis.onpostswitch)
			fncThis.queue.queue({callback: function(){
				fncThis.onpostswitch.call(fncThis, localVariables);
			}});
		fncThis.queue.queue({callback: function(){
			fncThis.inprogress = false;
			fncThis.queue.dequeue();
		}});

		//--start queue		
		fncThis.queue.dequeue();
	}


/*-------------
©animationQueue
------------*/
__.classes.animationQueue = function(arguments){
		if(typeof arguments == "undefined") arguments = {};
		//--optional variables
		this.name = arguments.name || "tmlib";
		this.autoDequeue = arguments.autoDequeue || false;
		
		//--derived variables
		this.objQueue = $({});
	}
	__.classes.animationQueue.prototype.queue = function(arguments){
		var fncThis = this;
		var fncName = arguments.name || this.name;
		var fncCallback = arguments.callback || arguments; //-arguments is (assumed) the callback if not set explicitely
		var fncAutoDequeue = (typeof arguments.autoDequeue != "undefined")? arguments.autoDequeue: fncThis.autoDequeue;
		var fncQueueCallback = (fncAutoDequeue)
			?function(){
					var fncArguments = arguments;
					var internalThis = this;
					fncCallback.apply(internalThis, fncArguments);
					fncThis.dequeue();
				}
			:fncCallback
		;
		this.objQueue.queue(fncName, fncQueueCallback);
	}
	__.classes.animationQueue.prototype.dequeue = function(arguments){
		if(typeof arguments != "undefined")
			var fncName = arguments.name || arguments;
		else
			var fncName = this.name;
		this.objQueue.dequeue(fncName);
	}
	__.classes.animationQueue.prototype.unshift = function(arguments){
		if(typeof arguments != "undefined"){
			var fncCallback = arguments.callback || arguments;
			var fncName = arguments.name || this.name;
		}
		if(typeof fncCallback == "undefined" || !fncCallback) return false;
//->return
		var fncQueue = this.objQueue.queue(fncName);
		fncQueue.unshift(fncCallback);
	}
	__.classes.animationQueue.prototype.clearQueue = function(arguments){
		if(typeof arguments != "undefined")
			var fncName = arguments.name || arguments;
		else
			var fncName = this.name;
		this.objQueue.clearQueue(fncName);
	}



/* ********* 
©tmlib
********* */

function tmlib(){
		this.classes = {};
		this.lib = {};
		this.cfg = {};
	}
	tmlib.prototype.isIphone = function(){
		var isIphone = (navigator.userAgent.toLowerCase().indexOf('iphone')!=-1);
		if (isIphone)
			return true;
		else
			return false;
	}
	tmlib.prototype.getHiddenElementWidthHeight = function(argElement){
		var originalSettings = {
			position: argElement.css("position"),
			left: argElement.css("left"),
			top: argElement.css("top")
		}

		argElement.css({"position":"absolute", "left":"-9000px", "top":"-1000px"})
		var width = argElement.outerWidth();
		var height = argElement.outerHeight();
		argElement.css(originalSettings);
		
		return {width: width, height: height};
	}
	__.lib.escapeHash = function(hash){
		return hash.replace(/\//g, "\\/");
	}
	__.lib.unescapeHash = function(hash){
		return hash.replace(/\\\//g, "\/");
	}
	tmlib.prototype.message = function(argument){
		if(window.console) 
			console.log(argument);
//		else alert(argument);
	}

