
/** @description This file depends on cnnHorizontalSlider.js */
/** @namespace */
if(typeof(window.com) === 'undefined') { var com = {}; }
if(!com.cnn) { com.cnn = {}; }
if(!com.cnn.blogs) { com.cnn.blogs = {}; }
if(!com.cnn.blogs.featuredPosts) { com.cnn.blogs.featuredPosts = {}; }
if(!com.cnn.blogs.featuredPosts.util) { com.cnn.blogs.featuredPosts.util = {}; }

/** @class This extends the cnnHorizontalSlider class. */
com.cnn.blogs.featuredPosts.FeaturedPosts = (typeof Class === "object") ? Class.create() : {};
com.cnn.blogs.featuredPosts.FeaturedPosts.prototype = Object.extend(new cnnHorizontalSlider(), {
	initialize: function( config ) {
		this.config = config;
		this.defaults = {
			galleryContainerId     : 'cnn_GallerySliderContainer',
			galleryStripClass      : 'cnn_fabcaslab',
			nextButtonContainerId  : 'cnn_fabcnext',
			prevButtonContainerId  : 'cnn_fabcprev',
			displayWidth           : 944
		};
		this.settings = Object.extend( this.defaults, this.config );

		try {  // reading from settings to set value of properties possibly required by cnnHorizontalSlider
			this.elIdentifier   = this.settings.galleryStripClass;
			this.container      = this.settings.galleryContainerId;
			this.navDiv         = this.settings.navContainer;  // unused?
			this.viewPort       = this.settings.displayWidth;
			this.currentTab     = this.settings.currentTab || 0;
		} catch (e) {}

		try {
			this.locked = false;
			this.sliderWidth = this.findPanels();
			this.numScreens = this.sliderWidth;
			this.negativeOffSetMax = this.setOffSet();
			this.positiveOffSetMax = 0;
			this.currentPanel = 0;
			this.inactiveDot = "";
			this.activeDot = "";
			this.setSliderWidth();
			this.buildNav();
			this.getCurrentOffSet();
			this.showImages('init');
		} catch(settingException) {}
	},
	showImages:function(arg) {
		var whereToLook = arg ? $(this.settings.galleryContainerId).down() : $(this.settings.galleryContainerId);
		whereToLook.select('.cnn_fabcatz img').each(function (n,i) {
			if( n.className == "cnnContentImg" && n.style.display != "block") {
				n.style.display = "block";
			}
		});
	},
	
	buildNav: function() {
		var prevBtnContainer, nextBtnContainer;
		
		prevBtnContainer = $(this.settings.prevButtonContainerId);
		nextBtnContainer = $(this.settings.nextButtonContainerId);

		prevBtnContainer.className = "cnn_fabcprv_off";
		nextBtnContainer.className = this.sliderWidth > 1 ? "cnn_fabcnxt" : "cnn_fabcnxt_off";

		if ( this.sliderWidth > 1 ) {
			prevBtnContainer.observe( 'click', this.slidePrev.bind( this ) );
			nextBtnContainer.observe( 'click', this.slideNext.bind( this ) );
		}
	},

	slideNext: function ()
	{
		if ( ( this.currentPanel + 1 ) < this.numScreens ) {
			this.btnSlide( this.currentPanel + 1 );
		}
	},

	slidePrev: function ()
	{
		if ( this.currentPanel > 0 ) {
			this.btnSlide( this.currentPanel - 1 );
		}
	},

	updateNav: function() {

		if((this.currentPanel+1) < this.numScreens) {
			$(this.settings.nextButtonContainerId).className = 'cnn_fabcnxt';
		} else {
			$(this.settings.nextButtonContainerId).className = 'cnn_fabcnxt_off';
		}

		if(this.currentPanel > 0) {
			$(this.settings.prevButtonContainerId).className = 'cnn_fabcprv';
		} else {
			$(this.settings.prevButtonContainerId).className = 'cnn_fabcprv_off';
		}

	},

	resetSlider: function(resp) {
		var thisPointer = this,
			containerParent = $(thisPointer.settings.galleryContainerId).up();  // going 'up' to parent instead of hard-coding ID

		Effect.Fade(this.container, {
			duration:0.5,
			afterFinish:function() {
				$(thisPointer.container).remove();
				var newContainer = document.createElement('div');
				newContainer.setAttribute('style','display:none');
				newContainer.setAttribute('id',thisPointer.settings.galleryContainerId);
				containerParent.appendChild(newContainer);
				$(thisPointer.container).update(resp);
				thisPointer.initialize(
					Object.extend( thisPointer.settings, { currentTab: thisPointer.currentTab } )  // extending the current settings to include the currentTab
				);
				Effect.Appear(thisPointer.container,{duration:0.5});
			}
		});
	},

	varDefined:function(val) {
		var t;
		var expression = "t = (typeof(" + val + ") !== \"undefined\");";
		eval(expression);
		return t;
	},
	returnVarValue:function(val) {
		return eval(val);
	}
});


/** 
	A utility to transform a Wordpress list link into the html for the FeaturedPosts gallery.
	@static 
	@function
*/
com.cnn.blogs.featuredPosts.util.transformList = function(args) {
	var settings = Object.extend({
		galleryContainerId : "cnn_GallerySliderContainer",
		galleryStripClass : "cnn_fabcaslab",
		galleryCatClass : "cnn_fabcatz",
		size : 4
	}, args);
		
	var grps = $$("#" + settings.galleryContainerId + " ul li").inGroupsOf(settings.size);
	var slides = grps.inject([], function(slds, v, i) {
		var li = v.inject([], function(lis, v, i) {
			var lastStyle = (i + 1) % settings.size === 0 ? " cnnLast" : "";
			var link, img, title, text, blurb;
			
			if(v === null) { return lis; }
			
			// TRANSFORM ENTRY
			link = v.down().href;
			img = v.down().down().remove();
			title = "<b><a href='" + link + "'>" + v.down().innerHTML + "</a></b>";
			v.down().update("");
			v.down().insert(img);
			img = v.down().remove();
			
			text = v.innerHTML;
			blurb = "<div class='cnnBlurb'>" + title + text + "</div>";
			v.update(img);
			v.insert(blurb);
			// TRANSFORM ENTRY
			
			lis += "<div class='"+ settings.galleryCatClass + lastStyle + "'><div class='cnnGrad'>" + v.innerHTML  + "</div></div>";
			
			return lis;
		});

		slds += "<div class='" + settings.galleryStripClass + "'>" + li + "</div>";
		return slds;
	});
	
	$(settings.galleryContainerId).update(slides);
};
