/*
 * put jQuery into noconflict mode
 * if conflicts with other Javascript libaries, e.g. Prototype on GoDaddy PreviewDNS
 */
	//jQuery.noConflict();
		

/* 
 * jQuery actions when DOM is loaded
 */ 

$(document).ready(function(){

	/* tag external links */
	$("body").externalLinks(
		{
		     cssClass: "external",
			 target: "_blank",
		     gaPrefix: "/linkout/",
		     gaTracking: true
		}
	);

	/* uncloack email addresses */
	$("body").emailUncloak({
		at:"--@--"
	});


	// replace search box text on focus
		$("#search-form #search-text")
			.focus(function(){
				$(this).attr("value","");
				$(this).parent().addClass("active");
			})
			.blur(function(){
				$(this).parent().removeClass("active");
			});
			
	/* signup and login form focus */
	
		$("#newsletterSignup .text-input, #clientLogin .text-input")
			.focus(function(){
				var formElem = $(this).parent().parent();
				if(formElem.hasClass("active")==false){
					formElem.find(".text-input").attr("value","");
					formElem.addClass("active");
				}
			});

	/* home page billboard slideshow */
		
		// add images to DOM
		if(typeof(billboardImages)=="undefined"){
			billboardImages = [];
		}
		$.each(billboardImages,function(i,el){
			$("#billboard").append("<img src=\"" + el + "\" alt=\"\" />");
		});
		
		// start slideshow	
		$("#billboard").innerfade({
		    animationtype: 'fade',
		    speed: 1500,
		    timeout: 5000,
		    type: 'sequence',
		    containerheight: '250px'
	    });
		
	/* nav link wrappers */
	
		$(".nav a").wrapInner("<span></span>");
		
	
	/* dropdown menus 
	 * see http://users.tpg.com.au/j_birch/plugins/superfish/#options
	 */
	
		// add class to top-level menu links
		$(".nav ul:first").children().addClass("top-level").children("a").addClass("top-level");
		// implement superfish on menu truee
		$(".nav ul")
			.addClass("sf-menu")
			.superfish({
			   	hoverClass:    'sfHover',          // the class applied to hovered list items 
			    pathClass:     'active', // the class you have applied to list items that lead to the current page 
			    pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
			    delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
			    animation:     {opacity:'show'},   // an object equivalent to first parameter of jQuery’s .animate() method 
			    speed:         'fast',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
			    autoArrows:    true,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
			    dropShadows:   true,               // completely disable drop shadows by setting this to false 
			    disableHI:     false,              // set to true to disable hoverIntent detection 
			    onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul 
			    onBeforeShow:  function(){},       // callback function fires just before reveal animation begins – 'this' is the ul about to open 
			    onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul 
			    onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed 			
			})
		;

		
	/* hovers */
		
		// util buttons
		var grayed = .85;
		$(".util #twitter, .util #facebook, .util #cart").fadeTo('fast', grayed).hover(function(){$(this).fadeTo("fast",1);},function(){$(this).fadeTo("fast", grayed);});

		// Phil
		var grayed = .65;
		$("#phil").fadeTo('fast', grayed).hover(function(){$(this).fadeTo("fast",1);},function(){$(this).fadeTo("fast", grayed);});
		
		
	/* table zebra striping */
	
		$("table.basic").each(function(){
			$(this).find("tr:even").addClass("even");
			$(this).find("tr:odd").addClass("odd");
		}) 
		
		

	/* reveal content
	 * clicking on <a class="reveal" rel="elementId"> will reveal <elem id="elementId">
	 */ 
	/*
		$(".revealSource").click(function(){
			var id = $(this).attr("rel");
			$(this).hide();
			$("#" + id).slideDown();
			return false;
		})
	*/

	/* ratings */
	
		$(".sp_rating .unrated").hover(function(){
			// show action
			$(this).hide();
			//$.log("hover");
			$(this).parent()
				.find(".action")
				.show()
				.hover(function(){},function(){
					// hide action
					//$.log("hover out");
					$(this).parent().find(".base").show();
					$(this).hide();
			});
		});
			
		
		$('.score a').click(function() {
			// mark as rated
			var score = $(this).text();
			var pct = parseInt(score*20)+"%";
			//$.log(score);
			var rating = $(this).parent().parent().parent();
			rating
				.find(".unrated").removeClass("unrated").unbind()
				.find(".average").css("width",pct)
			;
			$.get("/ajax-rating.php" + $(this).attr("href") +"&update=true", {}, function(data){
				//$.log(data);
				rating.find(".votes").html("Thanks!");
				rating.find(".clickme").hide();
			});
			return false; 
		});

	/* comments */
	
		// show comments form
		$(".add-comment").click(function(){
			var el = $(this).parent().parent().parent();
			el.find(".comments-form").slideDown('fast',function(){
				$(this).find("textarea").focus();
			});
			$(this).hide();
			return false;
		});
		
		// show comments list
		$(".view-comments").click(function(){
			var el = $(this).parent().parent().parent()
			el.find(".comments-list").slideDown('fast');
			el.find(".add-comment").hide();
			$(this).hide();
			el.find(".comments-form").slideDown('fast',function(){
				$(this).find("textarea").focus();
			});
			return false;
		});
		
		/* AJAX form */
			$(".comments-form form").ajaxForm({
				clearForm: false,
				resetForm: false,
				dataType: "json",
				timeout: 120000,
				//target: "#ajaxOutputContainer",   // target element(s) to be updated with server response 
				//url: "ajax.php",
				//type: 'POST',
				beforeSubmit: function(data, form, options){
					return true;
				},
				error: function(response){
					// AJAX error
					var msg = "Error submitting form";
					$.log(msg);
				},
				success: function(response, status, form){
					//setTimeout(function(){ // FIX remove this debug timeout function
					// check for success variable
					if (typeof response.success != "undefined") {
						if (response.success) {
							$.log("successful");
						}
						else {
							$.log("failed");
						}
						// show message
						if (typeof response.message != "undefined") {
							var m = response.message;
							$(form).parent().append("<div class='message'>"+m+"</div>");
							$(form).slideUp('fast');
						}
					}
					else {
						$.log("no response");
					}
				//}, 1000); // FIX remove
				}
			});	

	
});