

/* ------------------------------------------------------------------------ */

var Page = {

	init: function(){

	},

	addEvent: function(obj, type, fn) {
		if(obj.addEventListener) {
			obj.addEventListener(type, fn, false);
		}
		else if(obj.attachEvent) {
			obj["e" + type + fn] = fn;
			obj[type+fn] = function() {
											obj["e" + type + fn]( window.event );
										 }
			obj.attachEvent("on" + type, obj[type + fn]);
		}
	},

	getStyle: function( element, cssRule ) {
		var value = false;
	  if( document.defaultView && document.defaultView.getComputedStyle ) {
	    value = document.defaultView.getComputedStyle( element, '' ).getPropertyValue( cssRule);
	  }
	  else if( element.currentStyle ) {
	  	cssRule = cssRule.replace(/-([a-z])/g, function( match ) {
	  		return match.charAt(1).toUpperCase();
	  	});
			value = element.currentStyle[ cssRule ];
		}
	  return value;
	},

	getElementsByClassName: function(node, className, nodeName) {
	    var a = [];
	    var re = new RegExp('(^| )' + className + '( |$)');
	    var els = [];
	    if(nodeName) {
    		els = node.getElementsByTagName(nodeName);
	    }
	    else {
	    	els = node.getElementsByTagName("*");
			}
	    for(var i = 0, j = els.length; i < j; i++) {
	        if(re.test(els[i].className)) {
	        	a.push(els[i]);
	        }
			}
	    return a;
	}

};

/* ------------------------------------------------------------------------ */

var IECSSRepair = {

	init: function() {
		if( browser.version == 6 ) {
			IECSSRepair.repairNavleft();			
		}
		IECSSRepair.repairNavOverview();
	},

	repairNavleft: function() {
		var nl = document.getElementById("navLefthand");
		if( nl ) {

				// first item
			var firstChild = nl.getElementsByTagName("li")[0];
			firstChild.className += " ie6-first-child";
			var a = firstChild.getElementsByTagName("a")[0];
				// first item is no folder
			if(firstChild.className == " ie6-first-child") {
				if(a.className && a.className == "cur") {
					a.className += " ie6-cur-fc";
				}
				else if(a.className && a.className == "act") {
					a.className += " ie6-act-fc";
				}
				else {
					a.className += " ie6-fc";
				}
			}

				// folder items
			var folders = Page.getElementsByClassName(nl, "folder", "li");
			for(var i = 0; i < folders.length; i++) {
				var fc = "";
				if(folders[i] == firstChild) {
					fc = "-fc";
				}
				var a = folders[i].getElementsByTagName("a")[0];
				if(a.className && a.className == "act") {
					a.className += " ie6-folder-act" + fc;
				}
				else if(a.className && a.className == "cur") {
					a.className += " ie6-folder-cur" + fc;
				}
				else {
					a.className += " ie6-folder" + fc;
				}
			}

		}
	},

	repairNavOverview: function() {
		var no = document.getElementById("navOverview");
		if(no) {
			var as = no.getElementsByTagName("a");
			var max = 0;
			for(var i = 0; i < as.length; i++) {
				as.item(i).style.height = "auto";
				var h = as.item(i).offsetHeight;
				if(h > max) {
					max = h;
				}
				if( (i + 1) % 3 == 0) {
					as.item(i).style.height = max + "px";
					max = 0;
				}
			}
		}
	}
	
};

/* ------------------------------------------------------------------------ */

var AbstractBrowser = function() {
	this.init.apply( this, arguments );
};

AbstractBrowser.AGENT_FF = 1;
AbstractBrowser.AGENT_IE = 2;
AbstractBrowser.AGENT_SF = 3;

AbstractBrowser.OS_WIN = 1;
AbstractBrowser.OS_MAC = 2;

AbstractBrowser.prototype = {

	agent: null,
	version: null,
	os: null,

	init: function() {

		this.agent = -1;
		this.version = -1;
		this.os = -1;

		if(navigator.userAgent.match(/msie/gi)) {
			this.agent = AbstractBrowser.AGENT_IE;
			this.os = AbstractBrowser.OS_WIN;
			try {
				var agt = navigator.userAgent;
				var token = agt.match(/MSIE [0-9\.]*;/gi);
				this.version = token[0].substring(5, token[0].length - 1);
			}
			catch(e) {
				this.version = -1;
			}
		}
		else if(navigator.userAgent.match(/firefox/gi)) {
			this.agent = AbstractBrowser.AGENT_FF;
			try {
				var agt = navigator.userAgent;
				this.version = navigator.userAgent.substring(agt.indexOf("Firefox/") + 8, agt.length);
			}
			catch(e) {
				this.version = -1;
			}
			if( navigator.platform.match(/Win/gi) ) {
				this.os = AbstractBrowser.OS_WIN;
			}
			else if( navigator.platform.match(/Mac/gi) ) {
				this.os = AbstractBrowser.OS_MAC;
			}
		}
		else if(navigator.userAgent.match(/safari/gi)) {
			this.agent = AbstractBrowser.AGENT_SF;
			this.os = AbstractBrowser.OS_MAC;
			this.version = -1;
		}
	}

};

/* ------------------------------------------------------ */

var DropDown = function() {
	this.init.apply( this, arguments );
};
DropDown.prototype = {

	rootNode: null,
	title: null,
	stateOpen: "closed",
	cols: null,

	init: function(rootNode) {
		this.rootNode = rootNode;
		this.cols = [];
		this.title = rootNode.getElementsByTagName("a")[0];
		this.title.dd = this;
		this.title.href = "javascript:void(0);";
		Page.addEvent(this.title, "click", function() {
																							if(this.dd.stateOpen == "closed") {
																								this.dd.rootNode.className = this.dd.rootNode.className.replace(/ drop\-down\-closed/, " drop-down-open" );
																								this.dd.stateOpen = "open";
																							}
																							else if(this.dd.stateOpen == "open") {
																								this.dd.rootNode.className = this.dd.rootNode.className.replace(/ drop\-down\-open/, " drop-down-closed" );
																								this.dd.stateOpen = "closed";
																							}
																							this.blur();
																					 }
		);

		var col = rootNode.getElementsByTagName("div")[0];
		this.cols.push(col);
		for(var i = 0; i < 2; i++) {
			var c = col.cloneNode(false);
			this.cols.push(c);
			this.rootNode.appendChild(c);
		}
		var bl = new BalancedList(col, 3);
		for(var i = 0; i < 3; i++) {
			this.cols[i].appendChild(bl.cols[i]);
		}
	}

};


/* ------------------------------------------------------ */

var BalancedList = function() {
	this.init.apply( this, arguments );
};
BalancedList.prototype = {

	rootNode: null,
	nrOfCols: null,
	uls: null,
	lis: null,
	cols: null,
	length: null,
	part: null,

	init: function(rootNode, nrOfCols) {
		this.rootNode = rootNode;
		this.nrOfCols = nrOfCols;
		this.cols = [];
		this.uls = this.rootNode.getElementsByTagName("ul");
		this.lis = this.rootNode.getElementsByTagName("li");

		if(this.lis.length > 0) {
			this.length = this.lis.length + this.uls.length;
			this.part = Math.ceil( this.length / this.nrOfCols);
			this.initColumns();
			this.distributeItems();
		}
	},

	initColumns: function() {
		var col = document.createElement("ul");
		col.className = "list-link";
		for(var i = 0; i < this.nrOfCols; i++) {
			var u = col.cloneNode( false );
			u.curLen = 0;
			u.maxLen = this.part;
			this.cols.push(u);
		}
		if( this.length % this.nrOfCols > 0 ) {
			this.cols[ this.nrOfCols - 1 ].maxLen = this.length % this.part;
		}
	},

	distributeItems: function() {

		var emptyItem = document.createElement("li");
		emptyItem.className = "empty";
		emptyItem.appendChild(document.createTextNode("e"));

		for( var i = this.uls.length - 1; i > -1 ; i-- ) {

			this.appendListItem( emptyItem.cloneNode(true));

			var lis = this.uls[i].getElementsByTagName("li");

			for( var j = lis.length - 1; j > -1; j-- ) {
				this.appendListItem( lis[j] );
			}

			this.rootNode.removeChild( this.uls[i] );

		}
	},

	appendListItem: function(item) {

		var list;
		var doInsert = true;
		for(var i = this.nrOfCols - 1; i > -1; i--) {
			list = this.cols[i];
			if(item.className && item.className == "empty" && list.maxLen - list.curLen == 1 ) {
				doInsert = false;
			}
			if(list.curLen < list.maxLen) {
				list = this.cols[i];
				break;
			}
		}

		if(doInsert) {
			if(list.firstChild) {
				list.insertBefore(item, list.firstChild);
			}
			else {
				list.appendChild(item);
			}
		}

		list.curLen++;
	}

};


/* ------------------------------------------------------ */
/*
var TabContainer = function() {
	this.init.apply( this, arguments );
};

TabContainer.prototype = {

	tabs: null,
	panes: null,
	rootNode: null,
	activeTabId: null,

	init: function( rootNode, activeTab ) {
		var id = null;
		this.panes = [];
		this.rootNode = rootNode;
		var divs = this.rootNode.getElementsByTagName("div");
		for(var i = 0; i < divs.length; i++ ) {
			if( divs[i].className && divs[i].className.match(/tab-page/) ) {
				this.panes.push( divs[i] );
			}
		}
		this.tabs = this.rootNode.getElementsByTagName("ul")[0].getElementsByTagName("a");
		for(var i = 0; i < this.tabs.length; i++) {
			if( this.tabs[i].className && this.tabs[i].className.match(/selected/) ) {
				id = i;
			}
			this.tabs[i].href = "javascript:void(0);";
			this.tabs[i].tabId = i;
			this.tabs[i].tc = this;
			Page.addEvent( this.tabs[i], "click", function(e) {
																							this.tc.tabClicked(this.tabId);
																							this.blur();
																						}
			);
		}
		if(id == null) {
			id = activeTab ? activeTab.tabId : 0;
		}
		this.tabClicked( id );		
	},

	tabClicked: function( tabId ) {
		if( this.activeTabId != tabId ) {
			if(this.panes[ this.activeTabId ]) {
				this.panes[ this.activeTabId ].style.display = "none";
				this.tabs[ this.activeTabId ].className = this.tabs[ this.activeTabId ].className.replace(/\s{0,1}selected/g, "");
			}
			this.activeTabId = tabId;
			this.panes[ tabId ].style.display = "block";
			this.tabs[ tabId ].className += " selected";
		}
	}	

};
*/

/* ------------------------------------------------------ */


var Map = function() {
	this.init.apply( this, arguments );
};

Map.prototype = {

	areas: null,
	rootNode: null,
	activeAreaId: null,
	dataPanes: null,

	init: function( rootNode, activeArea ) {
		var id = null;
		this.rootNode = rootNode;
		this.areas = rootNode.getElementsByTagName("a");
		this.dataPanes = Page.getElementsByClassName(rootNode, "map-meta", "div");

		for(var i = 0; i < this.areas.length; i++) {
			if( this.areas[i].className && this.areas[i].className.match(/selected/) ) {
				id = i;
			}
			this.areas[i].href = "javascript:void(0);";
			this.areas[i].areaId = i;
			this.areas[i].map = this;
			Page.addEvent( this.areas[i], "click", function(e) {
																							this.map.areaClicked(this.areaId);
																							this.blur();
																						}
			);
				// fix IE z-index problem
			if( browser.agent == AbstractBrowser.AGENT_IE ) {
				this.areas[i].mouseover = function() {
																		this.parentNode.style.zIndex = "21";
				};
				this.areas[i].mouseout = function() {
																		if(this.areaId != this.map.activeAreaId) {
																			this.parentNode.style.zIndex = "1";
																		}
																		else {
																			this.parentNode.style.zIndex = "20";
																		}
				};				
				Page.addEvent( this.areas[i], "mouseover", function(e) {
																								this.mouseover();
																							}
				);
				Page.addEvent( this.areas[i], "mouseout", function(e) {
																								this.mouseout();
																							}
				);				
			}
			
		}
		if(id == null) {
			id = activeArea ? activeArea.areaId : 0;
		}
		this.areaClicked( id );
	},

	areaClicked: function( areaId ) {
		if( this.activeAreaId != areaId ) {
			if(this.areas[ this.activeAreaId ]) {
				this.areas[ this.activeAreaId ].className = this.areas[ this.activeAreaId ].className.replace(/\s{0,1}selected/g, "");
				this.dataPanes[ this.activeAreaId ].style.display = "none";
					// fix IE z-index problem
				if( browser.agent == AbstractBrowser.AGENT_IE ) {
					this.areas[ this.activeAreaId ].parentNode.style.zIndex = "1";
				}
			}
			this.activeAreaId = areaId;
			this.dataPanes[ areaId ].style.display = "block";
			this.areas[ areaId ].className += " selected";
		}
	}

};


/* ------------------------------------------------------ */


var browser = new AbstractBrowser();


/* ------------------------------------------------------ */

if(browser.agent == AbstractBrowser.AGENT_IE && browser.version < 7) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	}
	catch(err) {
		//
	}
}

/* ------------------------------------------------------ */

Page.addEvent(window, "load", function() {
																Page.init();
															}
);
Page.addEvent(window, "load", function() {
																var mc = document.getElementById("mainContent");
																if(mc) {
																	var dropDowns = Page.getElementsByClassName(mc, "drop-down", "div");
																	for(var i = 0; i < dropDowns.length; i++) {
																		new DropDown(dropDowns[i]);
																	}
																}
															}
);
/*
Page.addEvent(window, "load", function() {
																var mc = document.getElementById("mainContent");
																if(mc) {
																	var tabContainers = Page.getElementsByClassName(mc, "product-viewer", "div");
																	for(var i = 0; i < tabContainers.length; i++) {
																		new TabContainer(tabContainers[i]);
																	}
																}
															}
);
*/
Page.addEvent(window, "load", function() {
																var mc = document.getElementById("mainContent");
																if(mc) {
																	var maps = Page.getElementsByClassName(mc, "map", "div");
																	for(var i = 0; i < maps.length; i++) {
																		if( maps[i].className.search(/static/) == -1 ) {
																			new Map(maps[i]);
																		}
																	}
																}
															}
);
Page.addEvent(window, "load", function() {
																// fixing IE6 CSS
																if( browser.agent == AbstractBrowser.AGENT_IE ) {
																	IECSSRepair.init();
																}
															}
);
