if(!window.console) {
	window.console = {
		log : function(str) {
			alert(str);
		}
	};
}	

// caching of bg-images
if($.browser.msie) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	}
	catch(err) {
		//
	}
}


// preloading images
var preloadImages = [
	{
		src :	"bg_layer_header.gif",
		img : null
	},
	{
		src :	"ico_close.gif",
		img : null
	},
	{	
		src :	"bg_layer_ct.gif",
		img : null
	},
	{	
		src :	"bg_hierarchy-box.gif",
		img : null
	},
	{	
		src :	"ln_dots_pagetrail.gif",
		img : null
	},
	{	
		src :	"bg_bullet.gif",
		img : null
	},
	{	
		src :	"bg_button_type1_1.gif",
		img : null
	},
	{	
		src :	"bg_button_type1_2.gif",
		img : null
	},
	{	
		src :	"bg_bt_redbar_login_left.gif",
		img : null
	},
	{	
		src :	"bg_bt_redbar_login_right.gif",
		img : null
	}	
];
for(var i = 0; i < preloadImages.length; i++) {
	preloadImages[i].img = new Image();
	preloadImages[i].img.src = "/base/image/" + preloadImages[i].src;
}



	// public instances
	// these objects need to be in a public (global) scope as they must me visible to ajax callbacks
var shoppingCart = null;
var loginDialog = null;
var waitingDialog = null;
var disclaimerDialog = null;

$(document).ready(function() {

		// fix layout of navlefthand
	$("ul#navLefthandRoot").each(function() {
		$("div#top").append('<div id="navLeftHandFix1"></div><div id="navLeftHandFix2"></div>');		
	});

		// init tab-containers
	$("div.info-tabs").each(function() {
		new ch.clx.TabContainer(this);
	});
	
		// for safari and mac-browsers
	var promoboxFixMac = "";
	if($.browser.safari || navigator.platform.match(/Mac/)) {
		promoboxFixMac = " promobox-fix-mac";
	}
	
		// make promoboxes clickable
	$("div.promobox.clickable").click(function() {
		window.location.href = $(this).find("DIV.link A").attr("href");
	}).each(function() {
		$(this).append('<div class="promobox-fix' + promoboxFixMac + '"></div>');
	});
	
		// init modal dialog
	ch.clx.ModalDialog.instance = new ch.clx.ModalDialog();

		// init login dialog
	$("div#loginbox").each(function() {
		loginDialog = new ch.clx.LoginDialog(this);
	});
	
		// init home page login container
	$("div#loginContainer").each(function() {
		var loginContainer = $(this), enterHandler;

		enterHandler = new ch.clx.EnterHandler(loginContainer);
		enterHandler.addListener("enter", loginContainer.find("ul.foot a:first").attr("onclick"));
	});
	
		// init waiting dialog
	$("div#waitingdialog").each(function() {
		waitingDialog = new ch.clx.WaitingDialog(this);
	});
	
		// init move dialogs
	$("a.moviedialog").each(function() {
		new ch.clx.MovieDialog(this);
	});
	
		// init disclaimer layer
	$("div#disclaimerlayer").each(function() {
		disclaimerDialog = new ch.clx.DisclaimerDialog(this);
	});	
	
		// init direct-helps
	$("span.direct-help").each(function() {
		new ch.clx.DirectHelp(this);
	});

		// init product finder
	$("div.product-finder").each(function() {
		new ch.clx.ProductFinder(this);
	});
	
		// init hideables
	$("div.hideable").each(function(){
		new ch.clx.HideableParagraph(this);
	});
	
		// init shopping cart
	$("div#serviceArea div.shopping-cart").each(function(){
			shoppingCart = new ch.clx.ShoppingCart(this);
	});	
	
		// init service-container hideable
	$("div#serviceArea div.service-container-hideable").each(function(){
			new ch.clx.ServiceContainer(this);
	});	
	
		// init datpicker
	$("input.datepicker").each(function(){
		new ch.clx.DatePicker(this);
	});		
	
	// init Switch
	$("input.radio.activator").each(function() {
		if(!ch.clx.ActivatorSwitch.switches[this.name]) {
			ch.clx.ActivatorSwitch.switches[this.name] = new ch.clx.ActivatorSwitch();
		}
		ch.clx.ActivatorSwitch.switches[this.name].addActivator(new ch.clx.Activator(this));
	});		
	
	// Init zoom-image
	$("div.zoom-image").each(function(){
		new ch.clx.ImageZoom(this);
	});			
	
	// Init Related Product slider
	$("div.related-products").each(function(){
			new ch.clx.ProductSlider(this);
	});	
	
		//navTrail Image replace
	$("ul#navTrail li span").each(function(){
			ch.clx.Page.addBreadcrumbImage(this);
	});	

	// fixing layout of column data
	
		// last-children of type s
	$("div.row div.column.s:last-child").each(function(){
			var lastCol = $(this);
			
				// if columns are in info-tabs
			if(lastCol.parents("div.info-tabs").length !== 0) {
				if($("body").hasClass("largetablegrid")) {
					lastCol.css({
							"width":"234px",
							"margin-right":"0"
					});
				}
				else if($("body").hasClass("subgrid")) {
					lastCol.css({
							"width":"172px",
							"margin-right":"0"
					});
				}
			}
			
			
				// if columns are on a procgrid page
			else if($("body").hasClass("procgrid")) {
				if(lastCol.hasClass("border")) {
					lastCol.css({
							"width":"300px",
							"margin-right":"0"
					});
				} 
				else { 
					lastCol.css({
							"width":"310px",
							"margin-right":"0"
					});
				}
			}
			
			
				// in any other case
			else {
				lastCol.css({
						"width":"180px",
						"margin-right":"0"
				});
			}
			
	});
	
		//last-children of type ml
	$("div.row div.column.ml:last-child").each(function(){
			var lastCol = $(this);
			
				// if last column is preceded by a column of type s
			if(lastCol.prev("div.column.s").length > 0 && lastCol.parents("div.info-tabs").length == 0) {
				lastCol.width("360px");
			}
	
	});
	
	
	$("div.row div.column.m:last-child").css("margin-right","0");
	
	$("div.row div.column.ml:first-child").css("margin-right","10px");	
	
		// fixing layout of spacers in div#largetableTop
	$("div#largetableTop div.spacer:last-child").css({
		"padding-bottom" : "0"
	});

		
		
		// only for ie6
	if ($.browser.msie && $.browser.version < 7) {
		
			// fix :first-child pseudo class for tables and matrices
		$("table.default tr td:first-child, table.matrix tr td:first-child, div.info-tabs div.tabs-body div.container table.paging td:first-child").each(function(){
			var me = $(this);
			me.addClass("first-child");
			if(me.attr("rowspan") > 0) {
				me.addClass("rowspan");
			}
		});
		
			// fix :first-child pseudo class for col + for two-col
		$("body.procgrid div#mainArea div.col:first-child, body.procgrid div#mainArea div.two-col:first-child").each(function(){
			var me = $(this);
			me.addClass("first-child");
		});
		
		// fix :first-child pseudo class for ul.process-indication li.current:first-child
		$("div#page div#mainArea ul.process-indication li.current:first-child span").each(function(){
			var me = $(this);
			me.addClass("first-child");
		});
		
		
		// fix :first-child pseudo class for table.default tr.product_replacement.missing td:first-child, table.default tr.product_replacement.suggestion td:first-child
		$("table.default tr.product_replacement.missing td:first-child, table.default tr.product_replacement.suggestion td:first-child").each(function(){
			var me = $(this);
			me.addClass("first-child");
		});
		
		// fix double selection bug class for tr.subtotal.noborder
		$("table.default tfoot tr.noborder").each(function(){
			var me = $(this);
			if(!me.hasClass("total")){
				me.removeClass("noborder");
				me.removeClass("subtotal");
				me.addClass("subtotal-noborder");
			}
		});
	
	// fix double selection bug class for span.button-typeX
		//span.button-type8.prev
		$("span.button-type8.prev").each(function(){
			var me = $(this);
			me.addClass("button-type8-prev");
		});
		
		//span.button-type9.prev
		$("span.button-type9.prev").each(function(){
			var me = $(this);
			me.addClass("button-type9-prev");
		});

	}
	
	
		// for all ie
	if ($.browser.msie) {
		
			// :last-child for col + two-col
		$("body.procgrid div#mainArea div.col:last-child,body.procgrid div#mainArea div.two-col:last-child").each(function(){
			var me = $(this);
			me.addClass("last-child");
		});	
		
		// fix :last-child pseudo class for table.default tr.product_replacement.missing td:last-child,table.default tr.product_replacement.suggestion td:last-child 
		$("table.default tr.product_replacement.missing td:last-child,table.default tr.product_replacement.suggestion td:last-child ").each(function(){
			var me = $(this);
			me.addClass("last-child");
		});
	}
	
		// for firefox on Mac
	if ($.browser.mozilla && navigator.platform.match(/Mac/)) {
			$("ul#navTop").css("top","71px");
	}
	
		// for safari
	if ($.browser.safari) {
		
			// fix navTop padding
		$("ul#navTop li a").each(function(){
				$(this).css("padding","6px 0 6px 0");
		});
		$("body.subgrid ul#navTop li a, body.procgrid ul#navTop li a, body.largetablegrid ul#navTop li a").each(function(){
				$(this).css("padding","7px 0 7px 0");
		});
		
			// fix 3 cols: if there are three cols the middle one needs a fix due to safari :last-child bug (see: http://reference.sitepoint.com/css/pseudoclass-lastchild)
		var cols = $("body.procgrid div#mainArea div.col");
		if(cols.length === 3) {
			cols.eq(1).css("margin-left", "20px");
		};		
		
			// fix :first-child pseudo class for col + for two-col
		$("body.procgrid div#mainArea div.col:first-child, body.procgrid div#mainArea div.two-col:first-child").each(function(){
			var me = $(this);
			me.addClass("first-child");
		});		
		
			// :last-child for col + two-col
		$("body.procgrid div#mainArea div.col:last-child,body.procgrid div#mainArea div.two-col:last-child").each(function(){
			var me = $(this);
			me.addClass("last-child");
		});

	}
	if ($.browser.mozilla) {
		$("div#mainArea").css("overflow", "hidden");	
	}
});

window.onload = function() {
	
		// as this element depends on the images being loaded we can only create it on window load event
	$("ul.hierarchy").each(function() {
		new ch.clx.Hierarchy(this);
	});
	
		// equal height columns fix
	ch.clx.Page.adjustColumnHeight($("body"));
	
};