/* ============================================================
National Geographic Green Guide General JavaScript
* Created by Brian Talbot on 2008-10-15.
* Copyright (c) 2008 National Geographic. All rights reserved.

Notes:
================
* 2008-10-15 - Began list of general functions

In This File:
================
+ Link Boxes
+JQuery Dependent

============================================================ */

/* ------------------------------------------------------------
Link Boxes (Old and Replaced by JQuery)
------------------------------------------------------------ */
/* function prepareBoxes() {
	var boxes = getElementsByClassName(document, "li", "linkbox");
	for (var i=0; i<boxes.length; i++){
		var call = getElementsByClassName(document, "p", "call");
		for (var k=0; k<call.length; k++){
			call[k].parentNode.tabIndex = k+1;
			call[k].style.display = "none";
			call[k].parentNode.onmouseover = function() {
				addClassName(this, "hovering");
			}
			call[k].parentNode.onmouseout = function() {
				removeClassName(this, "hovering");
			}
			call[k].parentNode.onclick = function() {
				var destin = this.getElementsByTagName("a");
				window.location = destin[0].href;
			}
			call[k].parentNode.onkeypress = call[k].parentNode.onclick;
		}
	}
}

function prepareBoxesMod() {
	var boxes = getElementsByClassName(document, "li", "linkbox");
	for (var i=0; i<boxes.length; i++){
		var call = getElementsByClassName(document, "p", "call-display");
		for (var k=0; k<call.length; k++){
			call[k].parentNode.tabIndex = k+1;
			call[k].parentNode.onmouseover = function() {
				addClassName(this, "hovering");
			}
			call[k].parentNode.onmouseout = function() {
				removeClassName(this, "hovering");
			}
			call[k].parentNode.onclick = function() {
				var destin = this.getElementsByTagName("a");
				window.location = destin[0].href;
			}
			call[k].parentNode.onkeypress = call[k].parentNode.onclick;
		}
	}
} */

/* ------------------------------------------------------------
+printThis
------------------------------------------------------------ */
// Adding an unobtrusive "Printing" option to pages' interfaces (Mad Props to Roger Johansson for http://tinyurl.com/2ela69) //

function printThis() {
	if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM support
	if (!document.getElementById("page_actions")) {return;} // Check that the target element actually exists
	if (!window.print) {return;} // Check that the browser supports window.print
	
	var oTarget = document.getElementById("page_actions");
	var oListItem = document.createElement("li");
	var oLink = document.createElement("a");
	oLink.setAttribute("id","print_this_link"); // Give the link an id to allow styling
	oLink.setAttribute("href","#"); // Give the link an id to allow styling
	oLink.appendChild(document.createTextNode("Print This"));
	
	insertAfter(oListItem,email_this_link);

	oLink.onclick = function() {
		window.print(); 
		return false; // Removes default behavior that would cause the page to scroll up after printing
	}
	
	oTarget.appendChild(oListItem);
}



// --- Loading all of these functions upload page/DOM rendering --- //
/* addLoadEvent(prepareBoxes);
addLoadEvent(prepareBoxesMod); */
addLoadEvent(printThis);


/* ------------------------------------------------------------
JQuery Dependent
------------------------------------------------------------ */

/* When the DOM's ready */
$(document).ready(function() {
	
	/* Homepage quick BG promo border fix */
	$(".main #contentA .chrome:first ul li:last").addClass("last");
	
	if (!$.browser.msie) {
		$("label.overlabel,#newsletter_subscribe form label").overlabel();
	}
	
	/* label2value
	 * jQuery-based script for using form labels as text field values
	 * Courtesy of Alen Grakalic (cssglobe.com)
	 * http://cssglobe.com/post/2494/using-form-labels-as-text-field-values
	 */

	/* this.label2value = function(){	

		// CSS class names
		// put any class name you want
		// define this in external css (example provided)
		var inactive = "inactive";
		var active = "active";
		var focused = "focused";

		// function
		$("#newsletter_subscribe form").each(function(){		
			obj = document.getElementById($(this).attr("for"));
			if(($(obj).attr("type") == "text") || (obj.tagName.toLowerCase() == "textarea")){			
				$(obj).addClass(inactive);			
				var text = $(this).text();
				$(this).css("display","none");			
				$(obj).val(text);
				$(obj).focus(function(){	
					$(this).addClass(focused);
					$(this).removeClass(inactive);
					$(this).removeClass(active);								  
					if($(this).val() == text) $(this).val("");
				});	
				$(obj).blur(function(){	
					$(this).removeClass(focused);													 
					if($(this).val() == "") {
						$(this).val(text);
						$(this).addClass(inactive);
					} else {
						$(this).addClass(active);		
					};				
				});				
			};	
		});		
	}; */
	
	
	/* Linkboxing */
	$("li.linkbox").addClass("js_enabled").each(function() {
		var box = $(this);
		var call = box.find(".call").hide();
		var url = call.find("a").attr("href");
		box.hover( 
			function(){ box.addClass("hovering");},
			function(){ box.removeClass("hovering");}
		);
		box.click( function() {
			location.href = url;
		});
	});
	
	/* Table Sorting */
	$("#buying-guide").tablesorter({
		sortClassAsc: 'headerSortUp',		// Class name for ascending sorting action to header
		sortClassDesc: 'headerSortDown',
		stripingRowClass: ['even','odd'],
		stripeRowsOnStartUp: true
	});

	/* $("#buying-guide-price").tablesorter({
		sortList: [[6,0]],
		sortClassAsc: 'headerSortUp',		// Class name for ascending sorting action to header
		sortClassDesc: 'headerSortDown',
		stripingRowClass: ['even','odd'],
		stripeRowsOnStartUp: true
	}); */
	
	var $columnCount = $("#buying-guide-price thead th").size();
	var $priceLocation = $columnCount - 1;
			
	$("#buying-guide-price").tablesorter({
		sortList: [[$priceLocation,0]],
		sortClassAsc: 'headerSortUp',		// Class name for ascending sorting action to header
		sortClassDesc: 'headerSortDown',
		stripingRowClass: ['even','odd'],
		stripeRowsOnStartUp: true
	});

	$("#buying-guide-name").tablesorter({
		sortList: [[0,0]],
		sortClassAsc: 'headerSortUp',		// Class name for ascending sorting action to header
		sortClassDesc: 'headerSortDown',
		stripingRowClass: ['even','odd'],
		stripeRowsOnStartUp: true
	});

	/* Table Striping 
	$(".stripeMe tbody tr:even").addClass("alt");*/
	
	/* Buying Guide Display Bubble */
	
	
	/* Site-Wide SIFR Controls */
	$.sifr({
		path: 'http://www.thegreenguide.com/projects/thegreenguide/assets/SIFR_fonts/',
		save: true
	});
		
		/* Specific SIFR Activation */
		$("#contentA h1.sifr,.just_ask #contentA h1,.just_ask #contentA .featured h2,#contentA .chrome h2,.about #contentA h1,.shopping h3,#most_popular h3,.related_links h3,#contentA h2.content_type, .recipe #contentA h3, .buying_guides #contentA h1, .individual_guide #shopping_tips h2,.individual_guide #usage_tips h2,.product_comparison #contentA h3,.smart_shoppers_list #contentA h3,.main #contentA .promo_set #fast_facts h3,.main #contentA .promo_set #just_ask h3, .main #contentA .promo_set .promo_001 h2,.main #contentA .promo_set .promo_002 h2, .main #contentA .promo_set .promo_003 h2,.main #contentA .promo_set .promo_004 h2, .main .chrome h3, .individual_guide .columnB h2, .sub_hub #related_content h2, #contentB .go_local h3, #contentC .go_local h3, #footer_setA li h3, .hub #contentB .promo h3, .three_column #contentA h3, .search_go_local h2, body.shop .search_shop h2").sifr({ font: 'Gotham_Rounded_Bold' });
		
	
	/* Just Ask Show/Hide */
	$('.just_ask #questions ul li .answer').hide();
	$('.just_ask #questions ul li').append(
		$('<a class="call answer_call" href="">Answer</a>').click(function(){
			$(this).hide();
			$(this).parent().find('.answer').show();
			return false;
		})
	);
});