//jCarouselLite
(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);

$(document).ready(function() {
  //comment form 
	$('.content-fragment.comment-form .field-item input').focus(function() { $(this).addClass("active"); });  
	$('.content-fragment.comment-form .field-item input').blur(function() { $(this).removeClass("active"); });  

  //conversation list 
	$('.conversation .content-list li:odd').addClass('alt');

  //last classes 
    $('.header-fragment.main-header-menu .navigation-list li:first-child').addClass('first');
	$('.video-preview-list li:last-child').addClass('last');
	$('.right-sidebar .content-list li:last-child').addClass('last');
	$('.article-teaser-list li:last-child').addClass('last');
	$('body.site_pages_default .more-help li:nth-child(odd)').addClass('first');
	$('.layout-content.content-left-sidebar-right .content-link-list.media-thumbnails ul li:nth-child(4n)').addClass('last');
    $('.layout-content.content-left-sidebar-right .ecard-thumbnails ul li:nth-child(4n)').addClass('last');
    $('.layout-content.header-top-content-left-sidebar-right .ecard-thumbnails ul li:nth-child(4n)').addClass('last');
    $('.layout-content.content-left-sidebar-right .arealist-media-thumbnails ul li:nth-child(4n)').addClass('last');
    $('.split-content .content-link-list.ecard-thumbnails ul li:nth-child(3n)').addClass('last');    
	$('.layout-content.content .content-link-list.game-thumbnails ul li:nth-child(3n)').addClass('last');
	$('.layout-content.content .content-link-list.business-platinum ul li:nth-child(5n)').addClass('last');
	$('.layout-content.content .content-link-list.business-gold ul li:nth-child(5n)').addClass('last');
	$('.layout-content.content-left-sidebar-right .layout-region.right-sidebar .buttons-146 li:nth-child(2n)').addClass('last');
	$('.action-list li:last-child').addClass('last');

  //forum 
	function isEven(value) {
	  if (value%2 == 0)
		return true;
	  else
		return false;
	}	
	
	$('ul.verified-answers li.content-item:odd').addClass('alt');
	if ($('ul.verified-answers').children().size()%2 ==0) $('ul.all-replies li.content-item:odd').addClass('alt');
	else $('ul.all-replies li.content-item:even').addClass('alt');

  //search form 
	$('.search-form .field-item.advanced-search').hide();
	$('.search-form a.advanced-search').toggle(function() {
	  $('.search-form .field-item.advanced-search').show();
	}, function() {
	  $('.search-form .field-item.advanced-search').hide();
	});

  //shop-carousel
	$('#products').cycle({
      fx: 'fade',
      timeout: 6000,
      delay: -2000
	});
		

  //video-carousel
	$(".video-carousel").jCarouselLite({
		btnNext: ".next",
        btnPrev: ".previous"
	});
  
  //homepage top picks
	$(".top-picks").jCarouselLite({
      auto: 1000,
      speed: 1000
	});


  //Secondary Navigation
	 $(".secondary-nav .expandable ul").hide();
	 $(".secondary-nav .expandable a").removeClass('expanded');
	 $(".secondary-nav .expandable a").toggle(function() {
		$(this).siblings('ul').show();
		$(this).addClass('expanded');
		return false;
	 }, function() {
		$(this).siblings('ul').hide();
		$(this).removeClass('expanded');
		return false;
	 });

});


$(window).load(function() {
  //photo widget width
	$('.photo-widget').each(function() {$(this).width($('.content-fragment-content img', this).width() + 6);});

  // tv player center width 
	$('.peta-tv.center').each(function() {$(this).width($('object', this).width());});

});


/***
****
** ui-enhancements additions for 2011 redesign - last update : CE - 12-20-2010
****
***/

$(document).ready(function() {

    // Wrap What's New and Peta Files
    $("div.whats-new").wrapAll('<div id="WNPF">');
    $("div.blog-post-list").appendTo("#WNPF");
    $("#WNPF").wrapAll('<div id="WNPFCushion">');

    // move What's new "subscribe to feed" to the right div
    $("div.whats-new div.all-rss").append($("div.whats-new a.outside-rss"));
    $("div.whats-new div.all-rss a.outside-rss").addClass('rss');
    $("div.whats-new div.all-rss a.outside-rss").removeClass('outside-rss');
    $("div.whats-new div.all-rss a.outside-rss").removeClass('internal-link');

    // move LIFESTYLE "subscribe to feed" to the right div
    $("div.peta-living div.all-rss").append($("div.Cruelty-free a.outside-rss"));
    $("div.peta-living div.all-rss a.outside-rss").addClass('rss');
    $("div.peta-living div.all-rss a.outside-rss").removeClass('outside-rss');
    $("div.peta-living div.all-rss a.outside-rss").removeClass('internal-link');

    // move Action alerts "subscribe to feed" to the right div
    $("div.action-alerts div.all-rss").append($("div.action-alerts a.outside-rss"));
    $("div.action-alerts div.all-rss a.outside-rss").addClass('rss');
    $("div.action-alerts div.all-rss a.outside-rss").removeClass('outside-rss');
    $("div.action-alerts div.all-rss a.outside-rss").removeClass('internal-link');

    // add blue color to Peta Files list Widget (interior pages)
    $("div.blog-post-list").addClass('blue');


    // Remove one > in all widget footers

    $("div.all-rss a.all").each(function() {
        //alert(this.innerHTML);
        this.innerHTML = this.innerHTML.replace('&gt;&gt;', '&gt;');
    });

    // Change "read more" to " Take action " in action alerts lists (where it's not configurable)

    $(".area-link-list.action-alerts-list a.read-more").each(function() {
        //alert(this.innerHTML);
        this.innerHTML = this.innerHTML.replace('READ MORE', 'TAKE ACTION');
    });

    // remove inline styles where needed, add new inline style if needed (yak!)

    $(".action-button-list img").removeAttr("style");
    $(".videos ul.video-preview-list li a img").removeAttr("style");
    $(".videos ul.video-preview-list li a img").addClass("videoThumbnail");

    // adds class blue to shopping box if it's not there already
    $(".related-products:not(.blue)").addClass("blue");

    // styling true last child of Granny Smith Themed list
    $('.grannySmith ul.accordion li:last-child').addClass("true-last-child");

    // pushing main layout below subnav if the latter exists and there is no section header (as there is in Issues or planned giving)
    if (($('.issueHeader').length == 0) && ($('.plannedGivingHeader').length == 0) && ($('.header-fragment.content-menu').length)) {
        var layoutContentPadding = parseInt($('.layout-content').css('padding-top').replace("px", ""));
        var newPadding = layoutContentPadding + 36;
        $('.layout-content').css('padding-top', newPadding + 'px');

    }

    var logoTarget = '/';
    var logoLink = $('.header-fragment.site-banner a.site-title')
    if (logoLink.length == 1 && logoLink[0].href != '') {
        logoTarget = logoLink[0].href;
    }

    // link main logo to correct link
    $('.header-fragment.site-banner').click(function() {
        window.location = logoTarget;
    });


});


/***
****
** ui-enhancements additions for Planned Giving section - last update : CE - 01-05-2011
****
***/

$(document).ready(function(){
	
	/*
	** landing page rotator 
	*/
	
	/* clean all */
	
	/*function plannedGivingCleanAllHeaders() {
		$('.plannedGivingHeader').each(function(){
			if ($(this).css('display')!='none') {
				$(this).fadeOut(100);
				}
			});
	}*/
	
	function plannedGivingCleanAllHeaders() {
		clearTimeout(plannedGivingHeaderLoop);
		$('.plannedGivingHeader').hide();
	}
	   
	/* click events */   
	   
	$('.numberedBox.one').click(function(){
		plannedGivingCleanAllHeaders();
		$('.plannedGivingHeader.first').fadeIn(300);
		});
	$('.numberedBox.two').click(function(){
		plannedGivingCleanAllHeaders();
		$('.plannedGivingHeader.second').fadeIn(300);
		});
	$('.numberedBox.three').click(function(){
		plannedGivingCleanAllHeaders();
		$('.plannedGivingHeader.third').fadeIn(300);
		});
	$('.numberedBox.four').click(function(){
		plannedGivingCleanAllHeaders();
		$('.plannedGivingHeader.fourth').fadeIn(300);
		});   
	
	/* rotation functions */
	
	// set the delay
	var theDelay = 6000;
	
	function plannedGivingDisplayFirst() {
	plannedGivingCleanAllHeaders();
	$('.plannedGivingHeader.first').fadeIn(300);
	plannedGivingHeaderLoop = window.setTimeout(plannedGivingDisplaySecond,theDelay);
	}
	
	function plannedGivingDisplaySecond() {
	plannedGivingCleanAllHeaders();
	$('.plannedGivingHeader.second').fadeIn(300);
	plannedGivingHeaderLoop = window.setTimeout(plannedGivingDisplayThird,theDelay);
	}
	
	function plannedGivingDisplayThird() {
	plannedGivingCleanAllHeaders();
	$('.plannedGivingHeader.third').fadeIn(300);
	plannedGivingHeaderLoop = window.setTimeout(plannedGivingDisplayFourth,theDelay);
	}
	
	function plannedGivingDisplayFourth() {
	plannedGivingCleanAllHeaders();
	$('.plannedGivingHeader.fourth').fadeIn(300);
	plannedGivingHeaderLoop = window.setTimeout(plannedGivingDisplayFirst,theDelay);
	}
	
	/* run slideshow */
	
	plannedGivingHeaderLoop = window.setTimeout(plannedGivingDisplaySecond,theDelay);
		
	
	/*
	** end landing page rotator 
	*/
	
	
	/*
	** member spotlight randomizer 
	*/
	
	if ($('.memberSpotlight ul.article-teaser-list li').length>1) {
	var rand = Math.floor(Math.random()*$('.memberSpotlight ul.article-teaser-list li').length);
	/*alert (rand);*/
	/* displaying a random item in the list */
	$('.memberSpotlight ul.article-teaser-list li').eq(rand).show();}
	
	/*
	** end member spotlight randomizer 
	*/
	
	/*
	** additional link area in the headers 
	*/
	 // retrieve the item link
	 var link1 = $('.plannedGivingHeader.first a.hiddenLink').attr('href');
	 var link2 = $('.plannedGivingHeader.second a.hiddenLink').attr('href');
	 var link3 = $('.plannedGivingHeader.third a.hiddenLink').attr('href');
	 var link4 = $('.plannedGivingHeader.fourth a.hiddenLink').attr('href');
	
	 // link the content div to the related page
	   $('.plannedGivingHeader.first .plannedGivingHeaderContentText').click(function(){
		window.location=link1;   
	   });
	   $('.plannedGivingHeader.second .plannedGivingHeaderContentText').click(function(){
		window.location=link2;   
	   });
	   $('.plannedGivingHeader.third .plannedGivingHeaderContentText').click(function(){
		window.location=link3;   
	   });
	   $('.plannedGivingHeader.fourth .plannedGivingHeaderContentText').click(function(){
		window.location=link4
		;   
	   });
	/*
	** end member spotlight randomizer 
	*/
});


/***
****
** Text Size Tool
****
***/

//The function call here is at onload of the page - this will cause the text size
//function to see if a value has already been stored in the cookie, and if so, will
//use this value to adjust the text at page load.
$(document).ready(function() {
    textSize(0);
});

//Standard function to set a client cookie.
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? ";expires=" + expires.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure" : "");
}

//Standard function to set a client cookie.
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 to allow for the increase/decrease of the text size on the page.
//This function only targets text inside of paragraph tags.
function textSize(size) {
    var curFontSize;
    var newFontSize;

    //First, check the session cookie to see if a value has been set.  If it has, then
    //use this value as the starting point.
    curFontSize = getCookie("currentTextSize")

    if (curFontSize == null) {
        curFontSize = 14;
    } else {
        curFontSize = parseInt(curFontSize, 10);
    }

    if (size != 0) {
        newFontSize = size;
    } else {
        newFontSize = curFontSize;
    }

        var pageElements = document.getElementsByTagName("*");
        var pageElementsLen = pageElements.length;

        for (var i = 0; i < pageElementsLen; i++) {
            if (pageElements[i].className.indexOf("content-area") != -1) {
                pageElements[i].style.fontSize = (newFontSize) + "px";
            }
        }

        var today = new Date();
        var expires = new Date(today.getTime() + (56 * 86400000));

        setCookie("currentTextSize", newFontSize, expires, "/");
}
