$(document).ready(function() {
	$("body").removeClass("no-js").addClass("has-js");
	$(".extras .preview a[href=#]").remove();
	$("#posts > li .display > a[href='?src=ipcts']").closest("#posts > li").addClass("noprod");
	$("#posts > li:nth-child(3n)").addClass("nth-child-3n");
	$(".annc > *:first-child").addClass("first-child");
	$(".annc > *:last-child").addClass("last-child");
	

	$("ul#posts:not(.nofilter) > li").quicksearch({
		position: 'before',
		attached: 'ul#posts:not(.nofilter)',
		loaderText: '',
		labelText: 'Quick Search',
		formId: 'quicksearch',
		delay: 100
	});
	
	function predefSearch(goTo) {
		var dest = goTo.attr("href").replace("#","").replace(/--/g, " ");
		if ( (dest.indexOf("file:///") !== -1) || (dest.indexOf("http://") !== -1) ) {
			// IE7(maybe 6 too?) tries to pass current web address if href=# alone
			dest = '';
		}
		$("#quicksearch input").val(dest).keydown().focus();
		$("html, body").animate( { scrollTop: 60 }, "fast" );
		$("#quicksearch input").keyup();
	}
	$("#predef-search ul a").click(function(){
		predefSearch($(this));
		$("body").addClass("show-control");
		return false;
	});
	$("#quicksearch").before("<div id='controls'><a href='#' class='predef reset'>reset</a></div>");
	$("#controls a.predef").live("click", function() {
		var $this = $(this);
		predefSearch($this);
		return false;
	});

	$("#quicksearch input").keyup(function(){
		if ( $(this).attr("value") !== '' ) {
			$("body").removeClass("show-control").addClass("show-control");
		} else {
			$("body").removeClass("show-control");
		}
	});
	
	//make whole LI clickable
	$("#posts:not(.nofilter) > li").click(function(e){
		// if on features list, ignore click
		// if an extras link, execute own click (except preview)
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
		if  ( $(targ).is(".features, .features *") ) {
			return false;
		} else if ( $(targ).is(".extras *") ) {
			if ( $(targ).is(".predef, .predef *") && $(targ).is(".pack-12 *, .pack-24 *") ) {
				goTo = $(targ).closest(".predef");
				predefSearch(goTo);
			} else {
				// all others, especially PDF preview links
				$("#posts li .features").removeAttr("style");
				resetActions();
				window.location = $(targ).attr("href");
			}
			return false;
		}

		var newLoc = $(this).find(".display > a").attr("href");
		
		if ( newLoc == '?src=ipcts') {
			$("#posts li .features").removeAttr("style");
			alert(noProd);
			return false;
		}
		
		window.location = newLoc;
	});
	
	var noProd = "Sorry, this product is not yet available on our main site. All products not yet available are slightly grayed out. You can still see PDF previews.";
	$("a[href='?src=ipcts']").click(function(){
		alert(noProd);
		return false;
	});


	//hover
	function showIt(e) {
		//catch any stray or erroneous displays
		$(".features", "#posts").hide();
		resetActions();
		//adjust for width etc.
		var x = $(this).offset().left;
		var xw = $(this).width();
		var xt = $(this).offset().top;
		var w = $(this).children(".features").width();
		var wy = $(this).children(".features").height();
		var overflowing = handleWidth(x,xw,xt,w,wy);
		if (overflowing == true) {
			//make alt
			$(this).children(".features").addClass("alt");
		}
		//display
		$(this).children(".features").fadeIn("slow");
		$(this).find(".extras > li img").hide().closest(".extras > li").css({"background-position" : "0 0"});
	};
	function hideIt() {
		$(this).children(".features").hide().removeClass("alt");
		$(".overflow-buffer").slideUp(500, function(){
			$(this).remove(0);
		});
		resetActions();
	};
	function resetActions() {
		//undo whatever is done on hover
		$(".extras > li img").show();
		$(".extras > li, .extras > li a").stop().attr("style", "").removeAttr("style");
	}
	$("#posts > li").hoverIntent(showIt, hideIt);

	
	//prevent popup overflow
	function handleWidth(x,xw,xt,w,wy) {
		var winWidth = $(window).width();
		var docHeight = $(document).height();
		var overflow = false;
		
		//if position and width of element plus width of popup is more width of window, it's too long
		if ( (x + w + xw + 50) >= winWidth ) {
			overflow = true;
		}
		
		//if popup will descend below window, set buffer
		if ( (docHeight - xt) <= wy ) {
			$("#superwrap").append("<div class='overflow-buffer' style='line-height:0;font-size:0;clear:both;float:none;'>&nbsp;</div>");
			$(".overflow-buffer:last-child").show().height(wy - (docHeight - xt) + 50);
		}
		
		
		return overflow;
	};
	
	// slideshow
	
	$(".slideshow .slide").hide();
	$(".slideshow .slide:first").fadeIn(1000, function(){
		$(".slideshow").cycle({
			timeout:7250,
			speed:1250,
			prev: '.slide-prev',
			next: '.slide-next',
			pause:1,
			delay: -4500,
			slideExpr: '.slide'
		});
	});
	$(".slide-next, .slide-prev").hover(function(){
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});
	
	if ( $.browser.msie ) {
		if ( parseInt($.browser.version) == 6 ) {
			$(function() {
				var zIndexNumber = 1000;
				// Put your target element(s) in the selector below!
				$("#posts > li").each(function() {
					$(this).css('zIndex', zIndexNumber);
					zIndexNumber -= 10;
				});
			});
			$("#posts > li").hover(function(){
				$(this).addClass("superOnTop");
			}, function(){
				$(this).removeClass("superOnTop");
			});
		}
	}

});
