
/*
Ajax workaround using an iframe for IE6 without ActiveX
************************************************************************************************************************/
if (Ext.isIE6) {
	var xhr = Ext.lib.Ajax.getConnectionObject();
	if(!xhr){
		
		// alert("Internet Explorer 6 without ActiveX");
		
		Ext.onReady(function() {
				var dummyForm = Ext.DomHelper.append("page", 
						{tag: "form", id: "dummy-form", enctype: "multipart/form-data", style: "display: block;"});
			});
		
		MyAjax = function(o) {
			 Ext.apply(this, o || {});
			this.method = this.method || 'POST';
			MyAjax.superclass.constructor.call(this);
	
			if (this.success) {
				this.on('requestcomplete', this.success);
			}
	
			if (this.failure) {
				this.on('requestfailed', this.failure);
			}
		};
		
		Ext.extend(MyAjax, Ext.data.Connection, {
			request : function(o){
				if(!o.params)
				  o.params = 'ajax=1';
				else if(typeof o.params == 'object')
				  o.params.ajax = '1';
				else
				  o.params += '&ajax=1';
				if(!o.form)
					o.form = "dummy-form";
				if(!o.isUpload)
					o.isUpload = true;
				o.async = o.async || false;
				MyAjax.superclass.request.call(this, o);
			}
		});
		
		Ext.Ajax = new MyAjax();
	}
}


/*
Site
************************************************************************************************************************/

var site = {
	
	section: null,
	service: null,
	
	servicesRoot: "services",
	bannersRoot: "/services/service-banners/",
	sections: ["audatex"],

	isServiceSite: function() {
		var urlParts = location.href.split("/");
		
		// alert("Part 3: " + urlParts[3] + ", part 4: " + urlParts[4] + ", number of url parts: " + urlParts.length);
		
		if(urlParts.length > 5 && urlParts[3] == this.servicesRoot) {
			
			this.service = {name: urlParts[4]};
			
			var banner = document.getElementById("banner");
			bannerUrl = this.bannersRoot + this.service.name + "-banner.png";
			banner.style.backgroundImage = "url(" + bannerUrl + ")";
			
			
			var bannerButton = document.getElementById("banner-button");
			if (bannerButton) {
				bannerButton.style.visibility = "visible";
			}
			
		} else {
			
			for (var i = 0; i < this.sections.length; i++) {
				
				if (urlParts[3] == this.sections[i]) {

					this.section = {name: urlParts[3]};
					
					// alert("Section: " + this.section.name);
					
					var banner = document.getElementById("banner");
					bannerUrl = this.bannersRoot + this.section.name + "-banner.png";
					banner.style.backgroundImage = "url(" + bannerUrl + ")";
					
					break;
				}
			}
		}
	}
};



/*
Menubars
***************************************************************************************************/
var menuManager = null;

if (typeof(Ext) != "undefined") {
	
	Ext.BLANK_IMAGE_URL = "/themes/abz-solera-2008/images/blank.gif";
	
	Ext.onReady(function() {
						 
		var barConfigs = [];
		barConfigs["services-menu"] = {minWidth: 170};
		
		menuManager = new Ext.ux.abz.MenubarBuilder({
				url: "/menu.html",
				temp: "menu-content-container",
				separator: false,
				splitButtons: false,
				barConfigs: barConfigs,
				useProxy: Ext.isIE6
			});
		
		menuManager.loadContent();

	});
} else {
	// alert("Ext is undefined");	
}



/*
Google search
***************************************************************************************************/

google.load("search", "1", {"nocss": true, "nooldnames": true});

var search = {
	search: null,
	resultsElement: "search-results",

	init: function() {

		var siteSearch = new google.search.WebSearch();
		siteSearch.setUserDefinedLabel("Gevonden op de website van ABZ");
		// siteSearch.setUserDefinedClassSuffix("site-search");
		siteSearch.setSiteRestriction("www.abz.nl");
		siteSearch.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
		
		var drawOptions = new google.search.DrawOptions();
		drawOptions.setSearchFormRoot(document.getElementById("search-control"));
		
		var searchControl = new google.search.SearchControl();
		var searcherOptions = new google.search.SearcherOptions();
		searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		searcherOptions.setRoot(document.getElementById("search-results"));
		
		searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
 		searchControl.addSearcher(siteSearch, searcherOptions);
		searchControl.setNoResultsString("Geen resultaten gevonden");
  		searchControl.setSearchStartingCallback(search, search.searchStarting);
		searchControl.setSearchCompleteCallback(search, search.searchComplete);
		searchControl.draw(document.getElementById(this.resultsElement), drawOptions);
				
	},
	searchStarting: function(searchControl, searcher, query) {
		// alert("Search starting for \"" + query + "\"");
		var resultsBox = document.getElementById("search-results-container");
		resultsBox.style.display = "block";
	},
	searchComplete: function(searchControl, searcher) {
		// alert("Search complete, found " + searcher.results.length + " results");
	},
	hideResults: function() {
		var resultsBox = document.getElementById("search-results-container");
		resultsBox.style.display = "none";	
	}
};

google.setOnLoadCallback(search.init);


/**************************************************************************************************/
window.onload = function() {
	site.isServiceSite();
	// console.log("Window loaded");
	
	var menuUrl = "";
	
	if (site.section) {
		menuUrl = "/" + site.section.name + "/menu.html";
		menuManager.loadContent(menuUrl);
		// alert("/" + site.section.name + "/menu.html");
	}
	
	if (site.service) {
		
		menuUrl = "/services/" + site.service.name + "/menu.html";
		menuManager.loadContent(menuUrl);
		
		var webappButton = document.getElementById("service-webapp-button");
		if (webappButton) {
			webappButton.style.width = 128;
		}
	}
	
};
