if(typeof NUTS=="undefined"||!NUTS){var NUTS={};NUTS.startQuiz=function(quizId){var request=YAHOO.util.Cookie.set("nuts_quiz_"+quizId,"s="+Math.round(new Date().getTime()/1000.0),{path:"/"});}
NUTS.storeQuiz=function(quizId,question,answer){var cookie=YAHOO.util.Cookie.get("nuts_quiz_"+quizId);cookie=cookie+"&q"+(question+1)+"="+answer;var request=YAHOO.util.Cookie.set("nuts_quiz_"+quizId,cookie,{path:"/"});}
NUTS.endQuiz=function(quizId){var cookie=YAHOO.util.Cookie.get("nuts_quiz_"+quizId);cookie=cookie+"&e="+Math.round(new Date().getTime()/1000.0);var request=YAHOO.util.Cookie.set("nuts_quiz_"+quizId,cookie,{path:"/"});}
function sendVote(r,c,v)
{var postData='rma='+r+'&content='+c+'&rating='+v;var request=YAHOO.util.Connect.asyncRequest('POST','/application/vote',function(){},postData);}
function checkAge(checkLocation){var cookie=YAHOO.util.Cookie.get("nuts_ag");if(null===cookie){document.location='/verify/url/'+checkLocation;}}
function setAge(value)
{YAHOO.util.Cookie.set("nuts_ag",value,{path:"/"});}
function popupform(myform,windowname)
{var email=document.getElementById('newsletter_emailaddress');if(email){if(validate_email(email.value||'')){window.open('',windowname,'height=800,width=1000,scrollbars=yes');myform.target=windowname;return true;}else{alert('Please enter a valid email address.');}}
return false;}
function validate_email(email_address){var reg=/^[A-Za-z0-9_\-\.]+\@[A-Za-z0-9_\-\.]+\.([A-Za-z]{2,4})$/;var domain=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/i;reg.lastIndex=0;domain.lastIndex=0;var m=reg.exec(email_address);if(m){m=m[1];if(m.length==2||(m.length>2&&m.match(domain))){return true;}}
return false;}
function getCookie(name)
{var start=document.cookie.indexOf(name+"=");var len=start+name.length+1;if((!start)&&(name!=document.cookie.substring(0,name.length))){return null;}
if(start==-1)return null;var end=document.cookie.indexOf(';',len);if(end==-1)end=document.cookie.length;return unescape(document.cookie.substring(len,end));}
function setCookie(name,value,expires,path,domain,secure)
{var today=new Date();today.setTime(today.getTime());if(expires){expires=expires*1000*60*60*24;}
var expires_date=new Date(today.getTime()+(expires));document.cookie=name+'='+escape(value)+
((expires)?';expires='+expires_date.toGMTString():'')+
((path)?';path='+path:'')+
((domain)?';domain='+domain:'')+
((secure)?';secure':'');}
function deleteCookie(name,path,domain)
{if(getCookie(name))document.cookie=name+'='+
((path)?';path='+path:'')+
((domain)?';domain='+domain:'')+';expires=Thu, 01-Jan-1970 00:00:01 GMT';}}

function nuts_pageLoad() {
    
    $('#coolstuff').load('/application/coolstuff');
	if($('div.scrollcontainer').length>0){
		$.get('application/scroller',function(data){
			$('div.scrollcontainer').fadeOut('slow', function(){
				$('div.scrollcontainer').html(data);
				$('div.scrollcontainer').fadeIn('slow');
				$(".scroll").jCarouselLite({
					btnNext: ".scrollerNext",
					btnPrev: ".scrollerPrev",
					visible: 5,
					scroll: 5,
					speed: 800
				});
				$(this).css('backgroundImage','url(/themes/default/background/carousel-bg.jpg)');
				$('.scroller').pngFix();
			})
		})
	};
	$('ul#picks-tabs li').click(function(){
		var tab = $(this).attr('id');
		tab = '#'+tab.replace('tab-','picks-');
		$(tab).show();
		$(tab).siblings('ul.picks-content').hide();
		$(this).addClass('on');
		$(this).siblings('li').removeClass('on');
	});
	$('div#nuts-stuff-content').load('/application/nutsstuff/',{section:site_zone}, function() {
		$('div#nuts-stuff-content').fadeIn('slow');
		$('div#nuts-stuff-loader').fadeOut('slow');
	});
	$('#nuts-stuff-links a').live('click',function(){
		var href = $(this).attr('href');
		$('div#nuts-stuff-loader').fadeIn('slow');
		$('div#nuts-stuff-content').fadeOut('slow',function(){
			$.get(href,{section:site_zone}, function(data){
				$('div#nuts-stuff-content').html(data);
				$('div#nuts-stuff-content').fadeIn('slow');
				$('div#nuts-stuff-loader').fadeOut('slow');
			})
		});
		return false;
	});
	var nidCount = -1;
	var nids;
	// display any images that are currently in the viewport but hidden
	nuts_loadImages();
	// also call the function when the user scrolls or resizes the window.  Using the debounce plugin, we can ensure this only happens when the user has finished scrolling, otherwise we could overload the browser with tasks
	$(window).scroll(function(){
		$.throttle(250,nuts_loadImages());
	})
	$(window).resize(function(){
		nuts_loadImages();
	})
	//if this is a never ending gallery, retrieve a list of all the node Ids of girl galleries
	if($('#hidden-images').length){
		$.getJSON('/application/girlNodes',function(data){
			nids = data;
		});
	}
}
function nuts_loadImages(){
	// find out the bottom point that is currently visible in the viewport
	var height = $(window).height();
	var scrollTop = $(window).scrollTop();
	var scrollBottom = scrollTop + height;
	$('.hidden_image').each(function(){
		// check each hidden image to see if it should be visible
		if($(this).offset().top < scrollBottom){
			// interrogate the surrounding content to find the image title / alt tag and generate the image HTML
			if($(this).parent("div").hasClass('small_feature_content')){
				if($(this).siblings('.copy').length){
					var title = $(this).siblings('.copy').children('h3').text();
				}else{
					var title = $(this).siblings('h3').text();
				}
				var html = '<img src="'+$(this).children().attr('title')+'" width="200" height="100" alt="'+title+'" title="'+title+'" style=\"display:none\" />';
			}else if($(this).parent("div").hasClass('covergirl')){
				var title = $(this).siblings('h4').children('a').html();
				var html = '<img src="'+$(this).children().attr('title')+'" width="112" height="151" alt="'+title+'" title="'+title+'" style=\"display:none\" />';
			}else{
				var html = '<img src="'+$(this).children().attr('title')+'" style="display:none" />';
			}
			// drop in the html, fade it in and remove the hidden class so this image is skipped next time the user scrolls
			$(this).html(html);
			$(this).children().fadeIn('fast');
			$(this).removeClass('hidden_image');
		}
	});

	if($('#hidden-images').length && (($('#hidden-images').height())- scrollBottom<100)){
		nidCount++;
		// if this is a never ending gallery and the user is reaching the end of it, load the next gallery and inject it as hidden images
		if( nids[nidCount] == currentNid ){ nidCount++; }
		$.getJSON('/application/JSONGallery',{nid:nids[nidCount]}, function(data){
			$.each(data,function(i,item){
				if(item.filename!="empty"){
					var html = "<span class=\"hidden_image hidden\"><img title=\"/images/gallery/"+item.filename+"\" src=\"#\" style=\"display:none\" /></span>";
					$("#hidden-images").append(html);
				}
			})
		})
	}
 }

