
// for site specific functions. either grab js from code-library.js or write your own.

$(document).ready(function() {
   anchors.addBehaviors();
   library.init();
   storeResultsPositioning();
   popup.init();
   cartToolTip.init();
});

/* =================================================================== */
// for suckerfish dhtml menus
// dependencies: jQuery
sfHover = function() {
	$('#navLevelOne li').each( function() {
		$(this).hover( function() {
			$(this).addClass('sfhover');
		}, function() {
			$(this).removeClass('sfhover');
		});
	});
};
/* =================================================================== */

/* =================================================================== */
// tweaking positioning for Store Finder search results
var storeResultsPositioning = function () {
	var results = $('div .item');
	var parentEl = '';
	var rowLength = 4;
	
	if (results) parentEl = $(results[1]).parent().attr('id');
	if (parentEl == 'storeContent') rowLength = 3;
	
	for (var x=0; x<results.length; x++) {
		if (x % rowLength == (rowLength-1)) $(results[x]).addClass('rowEnd');
	}
};
/* =================================================================== */

/* =================================================================== */
// setting up popup box functions
var popup = {
	init: function () {
		var dialog = $('.dialog');
		var closeButton = $('.dialog a.close').click(function (e) {
			e.preventDefault();
			popup.close($(this).parent());
		});
	},
	
	close: function (el) {
		$(el).hide();
	}
};
/* =================================================================== */

/* =================================================================== */
// Library setup
var library = {
	config: {
		books: ''
	},
	
	init: function () {
		library.config.books = $('#bookCarousel');
		
		if (library.config.books.length > 0) {
			library.hide();
		}
	},
	
	hide: function () {
		$(library.config.books).addClass('hide');
	},
	
	show: function () {
		$(library.config.books).removeClass('hide');
	}
};
/* =================================================================== */

/* =================================================================== */
// Shopping cart tooltip
var cartToolTip = {
	init: function () {
		var tipLinks = $('td.description a');
		var tooltips = $('div.tooltip').removeClass('hide').hide();
		
		if(tipLinks.length > 0) {
			for (var x=0; x<tipLinks.length; x++) {
				$(tipLinks[x]).hover(function () {
					cartToolTip.show($(this));
				}, function () {
					cartToolTip.hide($(this));
				});
			}
		}
	},
	
	hide: function (el) {
		var tooltip = $('div.tooltip', el.parent());
		$(tooltip).hide('fast');
	},
	
	show: function (el) {
		var tooltip = $('div.tooltip', el.parent());
		$(tooltip).show('fast');
	}
};
/* =================================================================== */

/* =================================================================== */
// various link functionality - popups, external, onstate
// original script taken from Jeremy Keith
// dependencies: jQuery
var anchors = {
	a: Object,
	addBehaviors : function() {
		$('a').each( function() {
			var $a = $(this);
			// external links
			if ( ($a.attr('rel')=="external") || $a.hasClass('external') || $a.hasClass('pdf') || $a.hasClass('popupFull') ) {
				$a.click( function(e) {
					e.preventDefault();
					return anchors.openWin(this,"");
				});
			}
			// letter
			if ( ($a.attr('rel')=='letter') ) {
				$a.click( function() {
					var width = 658;
					var height = 398;
					var left = (screen.width/2)-(width/2);
					var top = (screen.height/2)-(height/2);
					
					return anchors.openWin(this,"height=398,width=658,top="+top+", left="+left);
				});
			}
			// popup
			if ( $a.hasClass('popup') ) {
				$a.click( function() {
					return anchors.openWin(this,"height=550,width=600,scrollbars=yes");
				});
			}
			// onstate
			if ( $a.attr('href') == location.href ) {
				$a.addClass('onstate');
			}
		});
	},
	openWin : function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	}
};
/* =================================================================== */
