jQuery(function($){
	
		String.prototype.supplant = function (o) {
	    return this.replace(/{([^{}]*)}/g,
	        function (a, b) {
			    var r = o[b];
				if ( o['type'] != 'Printables' && b == 'grange' && o['ptype'] != '' && o[b] != '' ) { r = ' | ' + o[b]; }	            		
				if ( b == "by" && o[b] != '' ) { r = 'By ' + o[b]; }
				return typeof r === 'string' || typeof r === 'number' ? r : a;
	        }
	    );
		};
		
		$('.seemore a').toggle(
			function(){$(this).text('See Fewer...').parent().prevAll().show();},
			function(){$(this).text('See More...').parent().prevAll().hide();}
		);
						
		$('.sect').toggle(
			function(){ $(this).parent().addClass('closed').next().hide(); },
			function(){ $(this).parent().removeClass('closed').next().show(); }			
		);	
		$('a.summarylink').toggle(function(){
					$(this)
						.css('background-image','url(/content/presentation/www/images/icon_search_minus.gif)')
						.text('Hide Book Summary')
						.parent().next().show();
				},function(){
					$(this)
						.css('background-image','url(/content/presentation/www/images/icon_search_plus.gif)')
						.text('Show Book Summary')
						.parent().next().hide();
				});
		
		var $callout, searchTimer, searchDelayHide, searchDisappearDelay = 500, searchVisible;
				
		$('.calloutimg').each(function(index){			
			$(this).bind('mouseover', function(){

				$this = $(this);
				clearTimeout(searchTimer);
				searchTimer = setTimeout( function() {
					setUpCallout();													
					populateCallout(index);				
					setCalloutPosition($this);	
					$callout.show();
					searchVisible = true;					
				}, 500);     
				
			});		
			$(this).bind('mouseout', function(){
				
				if (searchVisible) {
					searchDelayHide=setTimeout(function() {
						$callout.hide();
						searchVisible = false;
					},searchDisappearDelay);
				}
				else {
					clearTimeout(searchTimer);
				}
				
			});
		});
		$('.callout').each(function(index){			
			$(this).bind('mouseover', function(){

				$this = $(this);
				clearTimeout(searchTimer);
				searchTimer = setTimeout( function() {
					setUpCallout();													
					populateCallout(index);				
					setCalloutPosition($this);	
					$callout.show();
					searchVisible = true;
				}, 500);    

			});		
			$(this).bind('mouseout', function(){
				
				if (searchVisible) {
					searchDelayHide=setTimeout(function() {
						$callout.hide();
						searchVisible = false;
					},searchDisappearDelay);
				}
				else {
					clearTimeout(searchTimer);
				}
				
			});
		});
		
		function setUpCallout() {
			if( !$('#callout').get(0) ) {
					$('body').append('<div id="callout"><div id="calloutInner"><div class="calloutContent"></div><div class="calloutPointy"></div></div></div>');	
					$callout = $('#callout');				
					$callout.bind('mouseover', function(){
						if ( typeof searchDelayHide != "undefined" ) clearTimeout(searchDelayHide);
					});
					$callout.bind('mouseout', function(){
						if (searchVisible) {
							searchDelayHide=setTimeout(function() {
								$callout.hide();
								searchVisible = false;
							},searchDisappearDelay);
						}
						else {
							clearTimeout(searchTimer);
						}
					});
			}
		}
		
		function setCalloutPosition(obj){
			var offset = obj.offset();
			var pOffset = obj.parents('li').offset();
			var cHeight = $('#callout').innerHeight();
			var oLeft;
			if (offset.left < 500) {
				//oLeft = offset.left + obj.parents('li').innerWidth() + 5;
				oLeft = offset.left + 120;
				$callout.removeClass().addClass("right");
			}
			else {
				oLeft = offset.left - (413 + 5);
				$callout.removeClass().addClass("left");
			}
			
			var oTop = pOffset.top - (cHeight / 2) + 55;
			if (oTop < 0) { oTop = 10; }

			$callout.css({left: oLeft + 'px', top: oTop + 'px'});
			$('.calloutPointy').css('top', (cHeight/2) - 25 + 'px');
		}
		
		var template_products = '<p><span class="type">{type}</span><br /><a href="{titleLink}">{title}</a><br /><span class="by">{by}</span></p><table border="0" cellpading="0" cellspacing="0" width="366"><tr><td width="100"><img src="{prodImg}" border="0" alt="" /><div class="a2c"><a href="{storeLink}" onclick="var s=s_gi(s_account);s.linkTrackVars=\'prop33,eVar50,events,products\';s.linkTrackEvents=\'scAdd,event41\';s.events=\'scAdd,event41\';s.products=\';{productid};;;;evar26=Main:{keyword}|evar2=Main:Search Results\';s.tl(true,\'o\',\'Add to Cart from Search Results\');"><img src="/content/presentation/www/images/btnAdd2Cart.gif" alt="Add To Cart" border="0" /></a><br /><span>For Educators Only</span></div></td><td class="desc"><span>{ptype}{grange}</span><br />{desc}<div class="dprice"><strong>{price}</strong><br /><span class="save">{usave}</span></div></td></tr><tr><td></tr></table>';
		var template_schlstore = '<p><span class="type">{type}</span><br /><a href="{titleLink}">{title}</a><br /><span class="by">{by}</span></p><table border="0" cellpading="0" cellspacing="0" width="366"><tr><td width="100"><img src="{prodImg}" border="0" alt="" /><div class="a2c"><a href="{storeLink}" onclick="var s=s_gi(s_account);s.linkTrackVars=\'prop33,eVar50,events,products\';s.linkTrackEvents=\'scAdd,event41\';s.events=\'scAdd,event41\';s.products=\';{productid};;;;evar26=Main:{keyword}|evar2=Main:Search Results\';s.tl(true,\'o\',\'Add to Cart from Search Results\');"><img src="/content/presentation/www/images/btnAdd2Cart.gif" alt="Add To Cart" border="0" /></a></div></td><td class="desc"><span>{ptype}{grange}</span><br />{desc}<div class="dprice"><strong>{price}</strong><br /><span class="save">{usave}</span></div></td></tr><tr><td></tr></table>';
		var template_printables = '<p><span class="type">{type}</span><br /><a href="{titleLink}">{title}</a><br /><span class="by">{by}</span></p><table border="0" cellpading="0" cellspacing="0" width="366"><tr><td width="100"><img src="{prodImg}" border="0" alt="" /></td><td class="desc"><span>{ptype}</span><br /><span>{grange}</span><br />{desc}<div class="subj">Subject: {subjects}</div></td></tr><tr><td></tr></table>';
		
		function populateCallout(index) {				
			var html, stype = pts.getProdType(index);						
			if (stype == 'Printables') { html = template_printables.supplant(pts.getCalloutData(index)); }
			else if (stype == 'The Scholastic Store') { html = template_schlstore.supplant(pts.getCalloutData(index)); }
			else { html = template_products.supplant(pts.getCalloutData(index)); }
			$('.calloutContent').html(html);
		}			
	});