/*
 * put jQuery into noconflict mode
 * if conflicts with other Javascript libaries, e.g. Prototype on GoDaddy PreviewDNS
 */
	//jQuery.noConflict();
		
/*
 * Console logger
 *  %s	String
 *  %d, %i	Integer (numeric formatting is not yet supported)
 *  %f	Floating point number (numeric formatting is not yet supported)
 *  %o	Object hyperlink
 * 
 *  only logs if globalVars.log = true
 */

	$.log = function() {
	  	if(window.console) {
			if($.browser.safari){
				// Safari console
				var args = "";
				$.each(arguments,function(i,val){
					args += " " + val;
				});
				window.console.log(args); // fix to show args, Safari doesn't like .apply
			}
			if ($.browser.mozilla) {
				// Firefox with firebug
				window.console.log.apply(this, arguments);
			}
	  	} else {
	  		// no Firebug
	    	//alert(message);
		}
	};
	

/* 
 *  fix IE6 link background image flicker bug
 */
		
	//try {  document.execCommand("BackgroundImageCache", false, true); } catch(err) {} 


/* 
 * jQuery actions when DOM is loaded
 */ 

$(document).ready(function(){

/*
 *  De-obfuscate email addresses
 * 
 *  replaces email address @ in "mailto" links and link text
 * 
 */
	function deobfuscate(x){
		return x.replace("--@--","@");
	}
	$("a").each(function (i){
		var h = this.href;
		if (h.indexOf("mailto")==0) {
			var t = deobfuscate($(this).html());
			this.href = deobfuscate(h);
			$(this).html(t);
		}
	});


	/*
	 *  external links
	 *  tags with class="external" target="_blank"
	 *  optional Google Analytics tracking code trigger
	 */
	/*
		// (add https and don't include urls that start with site domain
		var pre = "/LINK/";				// prefix to add to URL for GA tracking code
		$('a[@href^="http://"]')
			.addClass('external')			// add CSS class for styling
			.attr('target', '_blank')		// open in popup window
			.click(function(){				// GA tracking code
				var href = $(this).attr("href");
				href = href.substr(7,9999);	// trim off http://
				var i = href.indexOf("www.");	// trim off www.
				if (i===0){
					href = href.substr(4,9999);
				}
				href = encodeURI(href);		// encode URL
				$.log(pre + href);			// log href to console
				//urchinTracker(pre + href)	// old GA tracker code
				//pageTracker._trackPageview(pre + href)	// GA tracker code
				//return false;				// cancel link action
			})
		;
	*/

	// main menu
	
		$(".nav a").hover(
			function(){
				$(this).parent().addClass("active");
			},
			function(){
				$(this).parent().removeClass("active");
			}
		);
		
		
		
	/* Phil hover effect */
		var op = .75;
		$(".phil a")
			.fadeTo(0, op)
			.css("position","relative")
			.hover(
				function(){
					$(this).stop().fadeTo(10,1);
				},
				function(){
					$(this).stop().fadeTo(500, op);
				})
		;



	/* columns (columns2, columns3, columns4, columns-golden) 
	 * 	usage:
	 *    <div class="columns2">
	 *     <div class="col">content</div>
	 *     <div class="col">content</div>
	 *    </div>
	 */
	
		// add CSS class and clearing
		$(".columns2, .columns3, .columns4, .columns-golden").each(function(){
			//, .columns3>.col:last, .columns4>.col:last, .columns-golden>.col:last")
			var kids = $(this).children();
			$(kids).eq(kids.length-1).addClass("col-last").after('<div class="clear-hidden"></div>');
		});
		
	/* make columns equal height
	 *  usage: <div class="columns-equalheight">
	 */		
		function makeEqualHeight(group) {
			tallest = 0;
			group.each(function() {
				thisHeight = $(this).height();
				if(thisHeight > tallest) {
					tallest = thisHeight;
				}
			});
			group.height(tallest);
		}
		$(".columns-equalheight").each(function(){
			makeEqualHeight($(this).children(".col"));
		});


	/* page min height */
	
		var min = 650;
		var page = $(".main .content");
		if(page.innerHeight() < min){
			page.css("height",min+"px");
		}
		
	/* make product category titles clickable */
	
		$("#product-categories li").each(function(){
			var href = $(this).find("a").attr("href");
			$(this).find("span").addClass("clickable").click(function(){
				window.location = href;
			});
		});
	
/*
 *  preload images
 */

		/*
		var preloads = [
			"images/ajax-loading-white.gif"
		];
		$.each(preloads,function(i,img){
			var x = new Image;
			x.src = img;
		});
		*/

/*
 *  Innerfade slideshows
 *  docs: http://medienfreunde.com/deutsch/weblog/aus_der_praxis.html?nid=162
 *  in CSS set the following to prevent images from showing while page loads:
 * 	 #container img { display: none; }
 */ 
 
		/*
		$(".slideshow").innerfade({
		    animationtype: 'fade',
		    speed: 1500,
		    timeout: 6000,
		    type: 'sequence',
		    containerheight: '200px'
	    });
	    */



});


$(window).load(function(){
	/* product listing thumbnail image max height */
	
		$(".product-list-item").each(function(){
			max = 160;
			if($(this).find("a.thumbnail img").innerHeight() > max){
				$(this).find("a.thumbnail").css("height",max+"px");	
			}
		});
		
		

});
