
/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */
 
(function($) {
		
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */
	 
	$.fn.equalizeCols = function(){
		var height = 0,
			reset = $.browser.msie && $.browser.version < 7 ? "1%" : "auto";
  
		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight);
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});
			
	};
	
})(jQuery);


$(function(){
	
	$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
		});
	}
	
	// preload images:
	$(['assets/site/images/nav_ribbon_current.png','assets/site/images/nav_ribbon_arrow_hover.png','assets/site/images/nav_ribbon_arrow_lastchild_current.png','assets/site/images/callout_tag.png','assets/site/images/callout_tag_hover.png','assets/site/images/nav_ribbon_firstchild.png', 'assets/site/images/nav_ribbon_firstchild_hover.png', 'assets/site/images/nav_ribbon.png', 'assets/site/images/nav_ribbon_hover.png']).preload();

    
	//set active state for main navigation
	var url = window.location.pathname.split("/");
	
	$("#main-nav a").each(function(){
	    if($(this).attr("href") == (""+url[url.length-2]+"/"+url[url.length-1])){
			$(this).parent().addClass("current");
			
		} else {
			$(this).parent().removeClass("current");
		}
    });
	
	$("#callout-tag a").mouseout(function(){
		$("#callout-tag a").css("background","#004d47");
	    $("#callout-tag span.tag-end").css("background","url(assets/site/images/callout_tag.png) no-repeat right top");				   
	}).mouseover(function(){
		$("#callout-tag a").css("background","#62C4C3");
		$("#callout-tag span.tag-end").css("background","url(assets/site/images/callout_tag_hover.png) no-repeat right top");
	});
	
	
	//set qtip popup ids
	if($("table#ingredient-tags").length > 0){
		$("table#ingredient-tags a").each(function(){
			var friendly = $(this).attr("href").split(".html")[0];
			var link = $(this).attr("href");
			$(this).attr("rel",link);
			$(this).attr("href","#"+friendly);
	    });
	}
	
	
	
	/*
	
	 $.get("/ultimate/index.php?id=13", function(data){
			  console.log(data);
			  //$("body").append(data);
			 // alert("Data Loaded: " + data);
    });
	
	*/
	
	
	if($("#trivia-module").length > 0){
		var triviaIndex = 0;
		var current,previous,next;
		
		$("#trivia-module #image img:nth-child(2)").hide();
		
		
		$("#trivia-module li:first-child").addClass("current");
		$("#trivia-module a").each(function(){
			var href = $(this).text().toLowerCase().split(" ").join("-");
			if(href.length){
			$(this).attr("href","#"+href);
				$(this).click(function(){
					current = $(this).parent().parent();
					next = $(this).parent().parent().next();
					
						current.removeClass("current");
						next.addClass("current");
						
						
						$("#trivia-module #image img:nth-child(1)").hide();
						$("#trivia-module #image img:nth-child(2)").show();
						
						
					
				});
			}
	    });
		
		
		$("#trivia-module #image img:nth-child(2)").click(function(){
			//reset qa image
			$("#trivia-module #image img:nth-child(1)").show();
			$("#trivia-module #image img:nth-child(2)").hide();
			//go to next question
			var nextQuestion = $("#qa-text li.current").next();
			var curQuestion = $("#qa-text li.current");
			
			if(nextQuestion.length > 0){
				$(nextQuestion).addClass("current");
				$(curQuestion).removeClass("current");
			} else {
				$(curQuestion).removeClass("current");
				
			}
			
			
		});
	}
	
	
	
	if($("#promo-image").length > 0){
		$("#promo-image").delay(800).fadeTo("slow", 1);
		

	}
	
	
	if($("#column-promo").length > 0){
		$("#column-promo #content").equalizeCols();
		$("#column-promo .col").mouseover(function(){
			$($(this).children()[0]).css("background","#e6ebd8");
			$($(this).children()[1]).css("visibility","visible");
		}).mouseout(function(){
			$($(this).children()[0]).css("background","#FFFFFF");
			$($(this).children()[1]).css("visibility","hidden");
		});
	}
	
	
	
	
	//ingredient cloud
	// Use the each() method to gain access to each elements attributes
   $("a[rel]").each(function()
   {
      $(this).qtip(
      {
         content: {
            // Set the text to an image HTML string with the correct src URL to the loading image you want to use
            text: '<img class="throbber" src="assets/site/images/throbber.gif" alt="Loading..." />',
            url: $(this).attr('rel')/*,
            title: {
               text: $(this).text() // Give the tooltip a title using each elements text
            }*/
         },
         position: {
            corner: {
               target: 'bottomMiddle', // Position the tooltip above the link
               tooltip: 'topMiddle'
            },
            adjust: {
               screen: true // Keep the tooltip on-screen at all times
            }
         },
         show: { 
            when: 'mouseover', 
            solo: true // Only show one tooltip at a time
         },
         hide: 'mouseout',
		 
         style: {
            tip: false, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
            border: {
               width:12,
               radius: 0,
		
			   color:"#cad2b4"
            },
            name: 'light', // Use the default light style
            width: 380 // Set the tooltip width
         }
      })
   });

});
