jq(function() {
	//Verify tah there's actually a carousel to pay with
	if(jq("#carousel").length != 0) {
	jq.getJSON("@@promoted-items", function (data) {
		//EVERYTHING should be called from this callback
		//outside of here, you probably don't have the data you expect
		//Setup an array to hold the nodes we get via Ajax
		nodes = [];
		//Take data from JSON query, create nodes with it, attach data to nodes for later use
		jq.each(data, function(i, item) {
			node = jq("<img width='437' height='377' src='"+item.promoImage+"' alt='"+item.Title+"' title='"+item.Title+"' />");
			node.data("promo", item);
			nodes.push(node);
		    });
		//clear out any children in the thumbnails slot currently
		jq("#gallery *").remove();
		jq("#thumbs *").remove();
		//Add the images back to the page
		jq.each(nodes, function(i, node) {
			jq("#gallery").append(node)
			    });
		//Add some control buttons
		jq("#thumbs").prepend("<div id='gallery-controls'><a id='gallery-prev' href='#'><img src='rev-arrow.gif' alt='previous' /></a> <a id='gallery-next' href='#'><img src='arrow_for_carousel.gif' alt='next' /></a></div>");
		//Setup jQuery cycle
		jq("#gallery").cycle( {
			fx: 'turnDown',
			    speed: 'fast',
			    timeout: 7000,
			    pager: "#thumbs",
			    prev: "#gallery-prev",
			    next: "#gallery-next",
			    // Creates thumbnail anchors for all the slides
			    pagerAnchorBuilder: function(idx, slide) {
			        return '<li><a href="#"><img class="gallery-thumb" src=" ' + slide.src + '" alt="' + jq(slide).attr("alt") + '" title="' + jq(slide).attr("title") + '" width="80" height="68" /></a></li>'; 
			},
			    //Update the rest of the content box
			    before: function(currSlideElement, nextSlideElement, options, forwardFlag)  {
			        elem = jq(nextSlideElement);
				jq("#carousel-inner h2").text(elem.data("promo").Title);
				jq("#carousel-inner h3").text(elem.data("promo").Description);
				jq("#carousel-inner .gallery-link").attr("href", elem.data("promo").getPath);
				jq("#thumbs img").removeClass("active-thumbnail");
				jq("#thumbs img[title='"+ elem.attr('title') + "']").addClass("active-thumbnail");
			}
			
		    });


		//end EVERYTHING HAPPENS HERE callback
	    });
	//End if statement that prevents this code from firing off the homepage
	}
	//Track PDF links

	jQuery('a[href$=.pdf]').click(function(){
                if(pageTracker){
		    var fixedLink = this.href;
		    fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
		    fixedLink = '/downloads/' + fixedLink;
		    pageTracker._trackPageview(fixedLink);
		    if(console){
			console.log('page tracked: ' + fixedLink);
		    }
		};
		
	    })
	        
	    });