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

__.cfg.navigationID = "mainnavigation";
__.cfg.menuPadding = 16;
__.cfg.submenuOffset = 0;
__.cfg.addedWidth = 20;

__.scrOnload = function(){
	__.topnavigationDropdownhandler = new __.classes.suckerfish({"menuID":__.cfg.navigationID, "menuPadding":__.cfg.menuPadding, "submenuOffset": __.cfg.submenuOffset, "addedWidth": __.cfg.addedWidth, doSizeAndCenterFirst: false});
	__.topnavigationDropdownhandler.sizeAndCenter2();

	var elmPageGetAQuote = document.getElementById("page_requestaquote");
	if(elmPageGetAQuote){
		var elmSelectDestination = $("#frmRowTerminal");
		__.quoteUpdateSelect = new __.classes.updateSelect({elmSelectSource: $("#fstate"), elmSelectDestination: elmSelectDestination, nameParameter: "state", urlData: "/content/requestaquote_terminals.php"
			,callback: function(data){
				data = $.parseJSON(data);
				this.elmSelectDestination.children(".forlabel").html(data.label);
				this.elmSelectDestination.children(".forfield").html(data.field);
				this.elmSelectDestination.show();
			}
		});
		elmSelectDestination.hide();
	}
}

/*-----
banner slide
-----*/
if(typeof $ != "undefined"){
	$(document).ready(function(){
		__.bannerlist = jQuery('#page_home .bannerlist');
		if(__.bannerlist.length > 0){
//			__.bannerlist.css("overflow", "hidden");
// original fadeout animation
			__.bannerlist.cycle({ 
			    sync:   true
			});			
/*
// sliding animation
			__.bannerPager = new __.classes.Pager({
				elmsPages: __.bannerlist.find(".banner")
				,oninit: function(){
					this.elmsPages.find(".image").css({"z-index": -1, opacity: 0});
					this.elmPageCurrent.find(".image").css({opacity: 1, "z-index": 0});
					var elmLabel = this.elmPageCurrent.find(".label");
					var heightLabel = elmLabel.outerHeight();
					this.elmsPages.find(".label").css("bottom", -heightLabel+"px");

					this.boot.animateImage = new __.classes.AnimateTransition({
						duration: this.boot.duration
						,stylesBefore: [
							{}
							, {opacity: 1, "z-index": -1}
						]
						,stylesTransition: [
							{opacity: 0}
							,{}
						]
						,stylesAfter: [
							{"z-index": -1}
							,{"z-index": 0}
						]
						,stylesBefore: [
							{}
							, this.boot.styleImageNextBefore
						]
						,stylesTransition: [
							{left: -(__.bannerlist.width())}
							,{left: 0}
						]
						,stylesAfter: [
							{}
							,{}
						]
					})
					this.boot.animateLabel = new __.classes.AnimateTransition({
						doMultistep: true
						,duration: this.boot.duration / 2
						,stylesBefore: [
							{}
							,{bottom:  -(elmLabel.outerHeight()), display: "block"}
						]
						,stylesTransition: [
							[
								{bottom: __.bannerlist.height()}
								,{}
							]
							,[
								{}
								,function(argElement){
									return {bottom: argElement.attr(this.boot.attrDataBottom)}
								}
							]
						]
						,stylesAfter: [
							{display: "none"}
							,{}
						]
						,boot: {attrDataBottom: "data-bottom"}
					})

					elmLabel.css({bottom: elmLabel.attr(this.boot.animateLabel.boot.attrDataBottom)+"px"});
				}
				,onswitch: function(arguments){
					var fncThis = this;
					fncThis.boot.animateImage.transitionForElements([fncThis.elmPageCurrent.find(".image"), arguments.elmPageNext.find(".image")]);
					fncThis.boot.animateLabel.transitionForElements([fncThis.elmPageCurrent.find(".label"), arguments.elmPageNext.find(".label")]);
					fncThis.elmNavigationCurrent = arguments.elmNavigationNext;
					fncThis.elmPageCurrent = arguments.elmPageNext;
					fncThis.setClasses();
				}
				,boot: {
					styleImageNextBefore: {left: __.bannerlist.width(), display: "block"}
					,duration: 750
				}			
			});
			
			setInterval(function(){__.bannerPager.switchToNext();}, 7500);
*/
		}		


		__.applicationFormDriver = jQuery("form[action='/driverapplication/']");
		if(__.applicationFormDriver.length > 0){
			__.applicationFormDriver.delegate(".bt_duplicateRow", "click", function(event){
				var elmThis = $(this);
				var elmContainer = elmThis.closest(".addbutton");
				var data = {ajaxcall: true, count: elmContainer.attr("data-count")};
				data[elmThis.attr("name")] = true;
				data[elmContainer.attr("data-count-variable")] = elmContainer.attr("data-count");
				$.ajax({
					data: data
					,success: function(argData, argStatus, argXHR){
						var countNew = Number(elmContainer.attr("data-count")) + 1
						elmContainer.attr("data-count", countNew);
						elmHiddenField = __.applicationFormDriver.find("input[name='"+elmContainer.attr("data-count-variable")+"']").val(countNew);
						elmContainer.before($(argData));
					}
					,url: elmContainer.attr("data-url")
				});
				if(event.preventDefault)
					event.preventDefault();
				return false;
			});
		}
	});
}

/*----------
©TMlib suckerfish
----------*/
__.classes.suckerfish = function (arguments){
		this.wrapperClass = (arguments.wrapperClass)? arguments.wrapperClass : "topitem";
		this.toplevelClass = (arguments.toplevelClass)? arguments.toplevelClass : "toplevel";
		this.submenuClass = (arguments.submenuClass)? arguments.submenuClass : "submenu";
		this.doSizeAndCenter = (arguments.doSizeAndCenter)? arguments.doSizeAndCenter : false;
		this.doSizeAndCenterFirst = (typeof arguments.doSizeAndCenterFirst != "undefined")? arguments.doSizeAndCenterFirst : true;
		this.doSizeAndCenterLast = (typeof arguments.doSizeAndCenterLast != "undefined")? arguments.doSizeAndCenterLast : true;
		this.menuPadding = (arguments.menuPadding)? arguments.menuPadding : 0;
		this.submenuOffset = (arguments.submenuOffset)? arguments.submenuOffset : 0;
		this.addedWidth = (arguments.addedWidth)? arguments.addedWidth : 0;
		this.classSelected = arguments.classSelected || "selected";
		
		this.elmMenu = document.getElementById(this.menuID);
		this.elmsWrapper = __.getElementsByClassName({"className": this.wrapperClass, "element": this.elmMenu});
		this.elmsMenuAssociations = this.getAllMenuPieces();
		this.elmCurrentlySelected = 0;
		
		this.timeout;
		
		this.attachListeners();
	}
	__.classes.suckerfish.prototype.getAllMenuPieces = function(){
		var fncReturnArray = new Array();
		for(var i=0; i < this.elmsWrapper.length; ++i){
			var forArray = new Array;
			var forElmToplevel = __.getElementsByClassName({"className": this.toplevelClass, "element": this.elmsWrapper[i]});
			var forElmSubmenu = __.getElementsByClassName({"className": this.submenuClass, "element": this.elmsWrapper[i]});
			fncReturnArray.push({"elmItemWrapper":this.elmsWrapper[i], "elmToplevel":forElmToplevel[0], "elmSubmenu": (forElmSubmenu[0])?forElmSubmenu[0]:null})
		}

		return fncReturnArray;
	}
	__.classes.suckerfish.prototype.attachListeners = function(){
		var fncThis = this;
		for(var i=0;i < this.elmsMenuAssociations.length; ++i){
			var forElmMenuItemArray = this.elmsMenuAssociations[i];
			if(this.elmsMenuAssociations[i]["elmSubmenu"]){
				var callbackFull = function(forElmMenuItemArray, fncThis) {
					return function(){
						clearTimeout(fncThis.timeout);
						if(!(this.elmCurrentlySelected === forElmMenuItemArray["elmItemWrapper"]))
							fncThis.dropdownOpen(forElmMenuItemArray["elmItemWrapper"]);
					}
				}(forElmMenuItemArray, fncThis);
				var callbackMouseout = function(fncThis){
					return function(){
						fncThis.timeout = setTimeout(function(fncThis){ return function(){fncThis.dropdownCloseCurrent(); };}(fncThis) ,750);
					};
	
				}(fncThis);
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "mouseover", callbackFull, false);
				__.addListeners(forElmMenuItemArray["elmToplevel"], "mouseover", callbackFull, false);
				__.addListeners(forElmMenuItemArray["elmToplevel"], "focus", callbackFull, false);
				__.addListeners(forElmMenuItemArray["elmSubmenu"], "mouseover", callbackFull, false);
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "click", callbackFull, false);
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "touchstart", callbackFull, false);
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "mouseout", callbackMouseout, false);
				
				forElmMenuItemArray["elmToplevel"].href="javascript://openMenu();";
				forElmMenuItemArray["elmToplevel"].style.cursor = "default";
			}
			// empty menu items
			else{
				var callbackEmpty = function(fncThis){
					return function(){
						fncThis.dropdownCloseCurrent();
					}
				}(fncThis)
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "mouseover", callbackEmpty, false);
				__.addListeners(forElmMenuItemArray["elmToplevel"], "focus", callbackEmpty, false);
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "click", callbackEmpty, false);
				__.addListeners(forElmMenuItemArray["elmItemWrapper"], "touchstart", callbackEmpty, false);
			}
		}
	}
	__.classes.suckerfish.prototype.dropdownOpen = function(argElement){
		if(this.elmCurrentlySelected && this.elmCurrentlySelected !== argElement)
			this.dropdownCloseCurrent();
		if(argElement){
			__.addClass(argElement, this.classSelected);
			this.elmCurrentlySelected = argElement;
		}
	}
	__.classes.suckerfish.prototype.dropdownCloseCurrent = function(){
		if(this.elmCurrentlySelected){
			__.removeClass(this.elmCurrentlySelected, this.classSelected);
			this.elmCurrentlySelected = 0;
			return 1;
		}
		return 0;
	}
	__.classes.suckerfish.prototype.dropdownCloseAll = function(){
		for(j=0;j < argElmsMenuArray.length; ++j){
			__.removeClass(argElmsMenuArray[j]["elmItemWrapper"], this.classSelected);
		}
	}


/*---menu sizing
---*/
	__.classes.suckerfish.prototype.sizeAndCenter1 = function(){	
		for(var i=0; i < this.elmsMenuAssociations.length; ++i){
			var forElmMenuItemArray = this.elmsMenuAssociations[i];
			if(forElmMenuItemArray["elmSubmenu"]){
				if(forElmMenuItemArray["elmSubmenu"].offsetWidth < forElmMenuItemArray["elmToplevel"].offsetWidth + this.addedWidth && !__.isIE6()){
					forElmMenuItemArray["elmSubmenu"].style.width = (forElmMenuItemArray["elmToplevel"].offsetWidth + this.addedWidth/*  - this.submenuPadding */) + "px";
				}
				if(forElmMenuItemArray["elmSubmenu"].offsetWidth > forElmMenuItemArray["elmToplevel"].offsetWidth){
					ifOffset = ((forElmMenuItemArray["elmToplevel"].offsetWidth - (forElmMenuItemArray["elmSubmenu"].offsetWidth/*  + this.ubmenuPadding */)) / 2);
					if(__.isIE6()) ifOffset += this.submenuOffset;
					forElmMenuItemArray["elmSubmenu"].style.left = ifOffset+ "px";
				}
			}
		}
	}
	__.classes.suckerfish.prototype.sizeAndCenter2 = function(){	
		for(var i=0; i < this.elmsMenuAssociations.length; ++i){
			var forElmMenuItemArray = this.elmsMenuAssociations[i];
			if(forElmMenuItemArray["elmSubmenu"] && (this.doSizeAndCenterFirst || (i != 0)) && (this.doSizeAndCenterLast || (i != this.elmsMenuAssociations.length - 1))){	
				forElmMenuItemArray["elmSubmenu"].style.visibility = "hidden";
				forElmMenuItemArray["elmSubmenu"].style.display = "block";
				if(forElmMenuItemArray["elmSubmenu"].offsetWidth < forElmMenuItemArray["elmToplevel"].offsetWidth + this.addedWidth && !__.isIE6()){
					forElmMenuItemArray["elmSubmenu"].style.width = (forElmMenuItemArray["elmToplevel"].offsetWidth + this.addedWidth + this.menuPadding*2) + "px";
				}
				if(forElmMenuItemArray["elmSubmenu"].offsetWidth > forElmMenuItemArray["elmToplevel"].offsetWidth){
					ifOffset = ((forElmMenuItemArray["elmToplevel"].offsetWidth - forElmMenuItemArray["elmSubmenu"].offsetWidth ) / 2) + this.menuPadding;
					if(__.isIE6()) ifOffset += this.submenuOffset;
					forElmMenuItemArray["elmSubmenu"].style.left = ifOffset+ "px";
				}
				forElmMenuItemArray["elmSubmenu"].style.display = "";
				forElmMenuItemArray["elmSubmenu"].style.visibility = "";
			}
		}
	}


/*---------- 
©tmlib
---------*/
function tmlib(){
		this.classes = {};
		this.lib = {};
		this.cfg = {};
	}
	__.lib.init = function(){
		__.addClass(document.body, "hasjavascript");
	}
	tmlib.prototype.addListeners = function(argElements, argEvent, argFunction, argBubble){
		var fncBubble = (argBubble)?argBubble : false;
		if(!__.lib.isArray(argElements))
			argElements = new Array(argElements);
		for(var i = 0; i < argElements.length; ++i){
			var forElement = argElements[i];
			if(forElement.attachEvent)
				forElement.attachEvent("on"+argEvent, argFunction);
			else
				forElement.addEventListener(argEvent, argFunction, fncBubble);
		}
	}
	//-@based from http://andrewpeace.com/javascript-is-array.html
	__.lib.isArray = function(argObject){
		if(
			(typeof argObject == 'object' && argObject instanceof Array) //-normal array
			|| ((typeof argObject == "function" || typeof argObject == "object") && typeof argObject.length == 'number' && typeof argObject.item == "function") //-nodelist
		){
			return true;
		}else{
			return false;
		}
	}
	tmlib.prototype.getElementsByClassName = function(args){
		var fncClassName = (args.className)?args.className:null; if(!fncClassName) return;
		var fncElement = (args.element)?args.element:document;
		var fncTagName = (args.tagName)?args.tagName:null;
		
		var fncReturn = [], fncElementsToSearch = [];
		var fncRegex = new RegExp('\\b'+fncClassName+'\\b');
		
		if(fncTagName){
			fncElementsToSearch = fncElement.getElementsByTagName(fncTagName);
		}
		else if(fncElement.all)
			fncElementsToSearch = fncElement.all;
		else
			fncElementsToSearch = fncElement.getElementsByTagName('*');
	
		for(var i=0; i < fncElementsToSearch.length; ++i){
			if(fncRegex.test(fncElementsToSearch[i].className))
				fncReturn.push(fncElementsToSearch[i]);
		}
		
		return fncReturn;
	}
	tmlib.prototype.addClass = function(argElement, argClass){
		if(new RegExp('\\b'+argClass+'\\b').test(argElement.className))
			return 0;
		else{
			argElement.className+=argElement.className?' '+argClass:argClass;
			return 1;
		}
	}
	tmlib.prototype.removeClass = function(argElement, argClass){
		  var fncReplace = argElement.className.match(' '+argClass)?' '+argClass:argClass;
		  argElement.className=argElement.className.replace(fncReplace,'');
	}
	tmlib.prototype.hasClass = function(argElement, argClass){
		if(new RegExp('\\b'+argClass+'\\b').test(argElement.className))
			return 1;
		else
			return 0;
	}
	tmlib.prototype.isIE = function(){
		if(this.isievar)
			return this.isie;
		else{
			this.initUA();
			if(this.browser.indexOf("Internet Explorer", 0) == -1) return 0;
			else return 1;
		}
	}
	tmlib.prototype.isIE6 = function(){
		if(this.isIE){
			if(!this.ieversion) // http://www.javascriptkit.com/javatutors/navigator.shtml
				if(/MSIE (\d+\.\d+);/.test(navigator.userAgent))
					this.ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
			if(this.ieversion == 6)
				return 1;
			else return 0;
		}
		else
			return 0;
	}
	tmlib.prototype.initUA = function(){
		if(!this.browser) this.browser = navigator.appName;
		if(!this.verion) this.version = parseFloat(navigator.appVersion);
	}
	tmlib.prototype.message = function(argument){
		if(window.console) 
			console.log(argument);
//		else alert(argument);
	}
	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};
	}

/*-----
init
-----*/
__.addListeners(window, "load", __.lib.init, false);
__.addListeners(window, "load", __.scrOnload, false);


/*--
request selection
---*/

__.classes.updateSelect = function(arguments){
		var fncThis = this;
		this.boot = arguments.boot || {};
		this.callback = arguments.callback || function(){};
		this.elmSelectSource = arguments.elmSelectSource || null;
		this.elmSelectDestination = arguments.elmSelectDestination || null;
		this.handleNull = arguments.handleNull || false;
		this.nameParameter = arguments.nameParameter || null;
		this.parametersFixed = arguments.parametersFixed || null;
		this.urlData = arguments.urlData || null;
		
		if(!this.elmSelectSource || !this.urlData){
			return false;
		}
		this.elmSelectSource.bind("change", function(){
			fncThis.handleChange();
		});
		fncThis.handleChange();
	}
	__.classes.updateSelect.prototype.handleChange = function(){
		var fncThis = this;
		var valueSelect = fncThis.elmSelectSource.val();
		if(valueSelect != "" || this.handleNull){
			var callback = function(data, textStatus, jqXHR){
				fncThis.callback.call(fncThis, data, textStatus, jqXHR);
			}
			var fncParameters = {};
			$.extend(fncParameters, fncThis.parametersFixed);
			if(this.nameParameter && valueSelect != "")
				fncParameters[this.nameParameter] = valueSelect;
			$.ajax({
				type: 'GET'
				,data: fncParameters
				,success: callback
				,url: fncThis.urlData
			});
		}
	}

