jQuery(document).ready(function(){
	
	//alert('a');
	
	/* store original sort order */
	
	var floatleftSorted = new Array();
	jQuery(".floatleft").each(function() {
		floatleftSorted.push(jQuery(this).attr("id"));
	});
	
	
	/* IE setContentWidth */
	
	function setContentWidth(padding) {
		if (IE7) {
			var contentwidth = 0;
			var margin = jQuery(3.2).toPx({'scope': jQuery("body")}, false);
			jQuery("#content img, #content .textblock, #content .description .column").each(function() { 
				contentwidth = contentwidth + jQuery(this).width() + margin;
			});
			contentwidth = contentwidth + marginleftcontent + padding;
			//contentwidth = contentwidth + padding;
			jQuery("#content").width(contentwidth);
			//alert(contentwidth);
		}
	}
	
	//alert(0);
	
	/* toggle area */
	
	var toggleScrolled = false;
	var allShown = false;
	var marginleftcontent = parseInt(jQuery("#content").css("margin-left").slice(0, -2));
	var marginleftmenu = parseInt(jQuery("#left").css("margin-left").slice(0, -2));
	var currentTarget = "";
	function toggleArea(target, closeold) {
		
		//alert(0);
		if (target!=currentTarget) {
			currentTarget = target;
			if (target=="all") {
				if (!allShown) {
					// show all boxes, rearranged in original sortorder
					jQuery(".parentcat.floatleft").addClass("hidden");
					for(i=0; i < floatleftSorted.length; i++) {
						jQuery("#"+floatleftSorted[i]+".parentcat.floatleft").appendTo(jQuery("#content")).removeClass("hidden");
					}
					// add padding-right
					//var padding = jQuery(window).width() - jQuery(".parentcat.floatleft:last").width() - marginleftcontent - marginleftmenu;
					var padding = jQuery(window).width() - jQuery(".parentcat.floatleft:last").width() - marginleftcontent;
					if (IE7) {
						setContentWidth(padding);
					} else {
						jQuery("#content").css("padding-right", padding); // crashes in IE7 ??
					}
					// scroll
					toggleScrolled = true;
					window.scrollTo(0, 0);
					// switch
					allShown = true;
				}
			} else if (target=="close") {
				self.close();
			} else if (target=="print") {
				window.print();
			} else if (target=="off") {
				jQuery("embed.audioBackgroundEmbed").remove();
				jQuery("bgsound.audioBackgroundBgsound").remove();
			} else if (target!="") {
				target = "id-"+target;
				//alert(1);
				if (!(target=="id-start" && jQuery("#"+target+".parentcat.floatleft:not(.hidden)").length && jQuery(".parentcat.floatleft:not(.hidden)").length==1)) {
					if (!allShown && jQuery("#"+target+".parentcat.floatleft").length) {
						// move selected box to right and show
						//alert(jQuery("#"+target+".floatleft").attr("id"));
						//alert(jQuery(".floatleft:not(.hidden):last").attr("id"));
						jQuery("#"+target+".parentcat.floatleft").insertAfter(jQuery(".parentcat.floatleft:not(.hidden):last")).removeClass("hidden");
						//jQuery("#"+target+".floatleft.hidden").insertAfter(jQuery(".floatleft:not(.hidden):last")).removeClass("hidden"); // only rearrange if hidden	
						if (closeold) {
							// hide all
							jQuery(".parentcat.floatleft:not(#"+target+")").addClass("hidden");
						}
						// add padding-right
						//var padding = jQuery(window).width() - jQuery("#"+target+".parentcat.floatleft").width() - marginleftcontent - marginleftmenu;
						var padding = jQuery(window).width() - jQuery("#"+target+".parentcat.floatleft").width() - marginleftcontent;
						if (IE7) {
							setContentWidth(padding);
						} else {
							jQuery("#content").css("padding-right", padding); // crashes in IE7 ??
						}
					}
				}
				//alert(2);
				// scroll
				if (!windowopening) {
					//alert(3);
					//window.scrollTo(jQuery(window).width(), 0);
					toggleScrolled = true;
					var p = jQuery("#"+target+".parentcat.floatleft").offset();
					if (IE6) {
						marginleftcontent = marginleftcontent + 460;
					}
					window.scrollTo(p.left - marginleftcontent, 0);
				}
				//alert(4);
			}
		}
	}
	
	//alert(1);
	
	jQuery("#left select").change(function(event){
		// get option value
		var target = jQuery(this).attr("value");
		window.location.href="#"+target;
		toggleArea(target, false);
	});
	
	//alert(2);
	
	
	/* category desc 4 lines */
	
	var h1selectors = "body.catalog-category-view:not(.category-catalogue-boutique) #content > #contentnarrow > span > h1, body.cms-catalogue-new #content > #contentnarrow > span > h1, body.cms-catalogue-preview #content > #contentnarrow > span > h1, body.cms-publisher-trade #content > #contentnarrow > span > h1, body.cms-publisher-stores #content > #contentnarrow > span > h1";
	if (jQuery(h1selectors).length) {
		hem = jQuery(jQuery(h1selectors).height()).toEm({'scope': jQuery(h1selectors)}, false);
		if (hem > 5.3) {
			jQuery(h1selectors).after("<div id='category-desc-more'><a href='#'>&gt; <?= $this->__('continue') ?></a></div>");
			jQuery("h1").toggleClass("cursorpointer");
			/*jQuery("h1").bind("mouseenter mouseleave", function(e){
				jQuery("h1").toggleClass("underlined");
			});*/
			jQuery(h1selectors+", #category-desc-more").click(function(event){
				jQuery(h1selectors).css("max-height","100%");
				jQuery("h1").removeClass("underlined cursorpointer");
				jQuery("h1").unbind("mouseenter mouseleave");
				jQuery("#category-desc-more").remove();
			});
		}		
	}
	
	//alert(3);
	
	/* columnize text */
	
	jQuery('.floatleft .description').parents(".parentcat:not(#id-start)").css("visibility", "hidden").removeClass("hidden");
	//jQuery('.floatleft .description').columnize({ height: 546, width: jQuery(44.8).toPx({'scope': jQuery("body")}, false), buildOnce : true });
	//jQuery('.floatleft .description').columnize({ height: 520, width: jQuery(44.8).toPx({'scope': jQuery("body")}, false), buildOnce : true, lastNeverTallest: true });
	jQuery('.floatleft .description').columnize({ height: 500, width: jQuery(44.8).toPx({'scope': jQuery("body")}, false), buildOnce : true, lastNeverTallest: true });
	// count number of columns to set width of description
	jQuery('.description').each(function() {
		n = jQuery(this).children("div").length;
		if (jQuery(this).children(".last:empty").length > 0) {
			n--;
		}
		w = n * (45.3); // IE: instead of 44.8
		jQuery(this).css("width", w+"em");
	});
	jQuery('.floatleft .description').parents(".parentcat:not(#id-start)").addClass("hidden").css("visibility", "visible");
	
	//alert(4);
	
	/* open product links in parent window */
	
	jQuery("#id-related-titles > div.childcat > a").click(function(){
		if (window.opener==null) {
			window.open(jQuery(this).attr("href"));
		} else {
			window.opener.location.href = jQuery(this).attr("href");
			window.opener.focus();
			//self.close();
		}
		return false;
	});
	
	
	//alert(5);
	
	/* scroll correction on window open and focus */
	
	function showAndScroll() {
		var c = window.location.href.lastIndexOf("#");
		if (c != -1 && c != window.location.href.length-1) {
			var c2 = window.location.href.lastIndexOf("#id-");
			if (c2 != -1) {
				c = c2;
			}
			var target = window.location.href.slice(c+1);
			if (target=="all" || target=="close" || target=="print" || target=="off" || jQuery("#id-"+target+".parentcat.floatleft").length) {
				toggleArea(target, true);
				//jQuery("#id-start.floatleft").addClass("hidden");
				jQuery("#left select").val(target);
			}
		} else if (windowopening) {
			setContentWidth(0);
		}
	}
	var windowopening = true;
	showAndScroll();
	windowopening = false;
	//setTimeout("window.scrollTo(0, 0)", 50); // scroll hack
	
	
	//alert(6);
	
	/* handle window focus */ 
	
	var lostfocus = false;
	var oldlocation;
	jQuery(window).focus(function(event){
		if (lostfocus && oldlocation != window.location.href) { 
			showAndScroll();
			lostfocus = false;
		}
	});
	jQuery(window).blur(function(event){
		oldlocation = window.location.href;
		lostfocus = true;
	});
	
	//alert(7);
	
	/* isScrolledIntoView */
	
	function isScrolledIntoView(elem) {
		var docViewLeft = jQuery(window).scrollLeft();
		var docViewRight = docViewLeft + jQuery(window).width();
		var docViewMiddle = docViewLeft + (jQuery(window).width() / 2);
		var elemLeft = jQuery(elem).offset().left;
		var elemRight = elemLeft + jQuery(elem).width();
		//alert(docViewMiddle +" "+ elemLeft +" "+ elemRight);
		return ((elemRight >= docViewMiddle) && (elemLeft < docViewMiddle));
	}
	
	//alert(8);
	
	var elemIdInView = "";
	jQuery(window).scroll(function(event){
		if (!toggleScrolled) {
			var scrolledIntoView = false;
			jQuery(".parentcat.floatleft:not(.hidden)").each(function() {
				thisElemId = jQuery(this).attr("id");
				if (!scrolledIntoView && elemIdInView!=thisElemId && isScrolledIntoView(jQuery(this))) {
					elemIdInView = jQuery(this).attr("id");
					scrolledIntoView = true;
					//alert(elemIdInView);
					selectVal = elemIdInView;
					if (elemIdInView.indexOf("id-")==0) {
						selectVal = selectVal.substr(3);
					}
					jQuery("#left select").val(selectVal);
				}
			});
		}
		toggleScrolled = false;
	});
	
	//alert(9);

});
