if(typeof NICK == "undefined" || !NICK) var NICK = {};
NICK.namespace("flux");


NICK.flux = {};
NICK.flux.baseURL = 'http://daapi.flux.com/2.0/00001/JSON/';
NICK.flux.ucid = '90C3FFFF01E323B00002FFFFC390';
NICK.flux.location = window.location.href;
NICK.flux.commentsLength = 10;
NICK.flux.contributors = Array();
NICK.flux.defaultContributorImage = "http://filesll.fluxstatic.com/0000000000000000000100000000/TNV/Jpg/B-15472/AR90x90";

NICK.flux.haloContentUrl = "";
NICK.flux.haloContent = false;

$(document).ready(function(){
	/*
	 * The following commented code is used by the BigDoor gamification 
	 * project that was implemented for the 2011 Halo Awards
	 *
	
	// Check if it's a halo content item and retrieve a proper share url.
	if (KIDS.get("fccRelatedShow") === "The HALO Awards:halo-awards" || KIDS.get("fccRelatedShow") === "halo-awards"){
		
		NICK.flux.haloContent = true;
		
	    $(document).bind('bigdoor.ready', function() {
	        BDM.awesome.shorten_url_for_end_user(KIDS.get("canonicalUrl"), function(url) {
	            NICK.flux.haloContentUrl = url;
	        })
	    });

	}
	
	//Adjustments for hiding / showing QuestBar	
	$(document).bind('bigdoor.ready', function() {
		if($("#bd-bar").height() == 0){
			$('body').removeClass('questbarBackground');
		}else if($("#bd-bar").height() > 0){
			$('body').addClass('questbarBackground');
		}	
	});
	
	$('.bd-controls.bd-widget button.bd-toggler, .bd-controls.bd-widget button.bd-expander, .bd-controls.bd-widget button.bd-collapser').live('click', function() {
		if($("#bd-bar").height() == 0){
			$('body').addClass('questbarBackground');
			return false;
		}else if($("#bd-bar").height() > 0){
			$('body').removeClass('questbarBackground');
			return false;
		}	
	});
	*/
});


NICK.flux.setFBLike = function() {
	/*Not using this function, managed to get it working on the page but leaving here for now. */
	if($('td.flike').length > 0){
		var encodedUrl = encodeURIComponent(NICK.flux.location);
		//$('td.flike').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+encodedUrl+'&amp;layout=button_count&amp;show_faces=false&amp;width=72&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:72px; height:21px;" allowTransparency="true"><!--  --></iframe>');
	}
	
};
NICK.flux.getContributors = function() {
	$.ajax({
		dataType:'jsonp',
		url:NICK.flux.baseURL +NICK.flux.ucid + '/feeds/people/members/-/role/contributor/?showOnlyWithPhotos=false',
		success:function(data){
			NICK.flux.populateContributors(data.Items);
		}
	});
	
};

NICK.flux.populateContributors = function(authors) {
	for(a=0;a<authors.length;a++) {
		NICK.flux.contributors[a] = authors[a].Title;
	}
	
	$('a.blog_author').each(function(){
		var local = $(this);
		for(a=0;a<authors.length;a++){
			if(authors[a].Title.toLowerCase() === local.html().toLowerCase()){
				local.attr('href',authors[a].ProfileUrl);
			}
		}
	});
	
};

NICK.flux.share = function(layout) {

/* The Flux4 script -- 	*/

	var shareConfig = {};
	
	/** Check if this is a halo item,and weather or not to use the Big Door share url **/
	/*
	if (NICK.flux.haloContent === true && NICK.flux.haloContentUrl !== "") {
		shareConfig.contentUri = NICK.flux.haloContentUrl;
	} else {*/
	shareConfig.contentUri = 'mgid:cms:item:teennick.com:' + KIDS.get('cmsId');
	//}
	
	shareConfig.title = ' ';
	shareConfig.template = 'horizontal';
	shareConfig.elements = [
	            { id: 'Facebook', type: 'fblike', layout:'button_count', show_faces:'false', width:'72', action:'like',	colorscheme:'light', height:'21' },
	            { id: 'Twitter', originalButton: true, urlSettings:
	                     { via: 'Teennick' } },
	            { id: 'MyEmail', title: 'email' },
	            { id: 'Embed' }
	        ];
	
	shareConfig.overlayElements = [
	            { id: 'EmailTab' },
	            { id: 'EmbedTab' }
		    ];

	Flux4.createWidget('Share',shareConfig);
	
	
	/* pause video on the page when favorite is clicked */
	$('.addToMy').find('a').live('click',function(){
		if(typeof player !== 'undefined' && NICK.videoPlayer.isReady){
			player.pause();
		}
	});
	/* Hide the player when the overlay is active */
	$('div.share').find('li.myemail').live('click',function(){
		NICK.utils.hideSwfs();
		setTimeout("NICK.flux.closeOverlay()", 500);
	});

};

NICK.flux.closeOverlay = function (){
	if($('div.shareOverlay').find('.closeButton').length){
		$('div.shareOverlay').find('.closeButton').click(function(){
			NICK.utils.showSwfs();
		});	
	} else {
		setTimeout("NICK.flux.closeOverlay()", 500);
	}
}


NICK.flux.comments = function(comTitle) {
	Flux4.createWidget('ActivityFeed', {
	     contentId:'mgid:cms:item:teennick.com:' + KIDS.get("cmsId"),
	     maxResults: NICK.flux.commentsLength, 
	     activityMap: 'disabled',
	     includeTweets: false,
	     enabledFilters:['All', 'Comments', 'UserActivity'],
	     showPager: true
	}, function (widget) {
	
		widget.onCommented = function () { 
			/** Big Door Interaction **/
			/*if (NICK.flux.haloContent === true) {
				
				// Report to big door that a rating has occured on a halo content item.
				if (typeof BDM !== "undefined") {
					BDM.trigger('teennick.comment'); 
				}
			}*/
			/** End Big Door Interaction **/
		}
		
	});
};

NICK.flux.contentAction = function() {
	Flux.createWidget('ContentAction', { 
			contentUri: 'mgid:cms:item:teennick.com:' + KIDS.get("cmsId"),
			layout: 'horizontal', 
			size: 'small',
			metricsAsPermalink: false,
			hideShareToGroup: false,
			items: [ 
			        { id: 'addToFavorites', title: 'Favorite' }, 
			        { id: 'rate', ratingSystem: 'thumbsUpDown', title: 'Rate it!' },
			        { id: 'contentRating', type: 'thumbsUpDown', title: { thumbsUpTitle: '', thumbsDownTitle: '' } 
			       }
			    ] 
	}, function(widget) {
		
		widget.onRated = function (method) { 
			/** Big Door Interaction **/
			/*
			if (NICK.flux.haloContent === true) {
				
				// Report to big door that a rating has occured on a halo content item.
				if (typeof BDM !== "undefined") {
					BDM.trigger('teennick.rate'); 
				}
			}
			*/
			/** End Big Door Interaction **/
		}	
		
  }); 
	
};

NICK.flux.commentsCount = function(itemId) {
	Flux.createWidget('ContentAction', { 
			contentUri: 'mgid:cms:item:teennick.com:' + itemId,
			layout: 'horizontal', 
			size: 'small',
			metricsAsPermalink: false,
			hideShareToGroup: false,
			items:	[ { id: 'commentCount', title: 'Comments' } ] 
	});
};

NICK.flux.content = function(url, title, items) {
	var srcUrl = NICK.flux.baseURL +NICK.flux.ucid + url;
	Flux.createWidget('Content', { 
		   title: title,
		   dataSourceUrl: srcUrl, 
		   layout: 'list',
		   numberOfColumns: 1,
		   contentThumbnail: { enabled: true, size: '90x90', videoSize: '120x90' },
		   numberOfItemsPerPage: 5,
		   actionLink: { title: 'View All', url: 'http://community.mtv.com/VideoCategories' },
		   items: items
		}); 

};

NICK.flux.chart = function(types, view) {
	Flux.createWidget('Chart', { 
		   defaultView: view,
		   numberOfItemsPerPage: 5,
		   contentTypes: types
		}); 

};

NICK.flux.communityActivity = function(widgetTitle, filter,max) {
	max = (typeof max === 'undefined') ? 8 : max;
	Flux.createWidget('CommunityActivity', {
		alternativeTitle: widgetTitle,
		members: {
			title: widgetTitle,
			numberOfItemsPerPage: max,
			numberOfPages: 1,
			updateInterval: 30,
			subjectFilter: filter
		}
	});
};

NICK.flux.createSignInWidget =  function() {
		    Flux.createWidget('Authentication', { fluxHosted: true },
		     function(authenticationWidget) {
		      authenticationWidget.showSignInForm();
		     });
		   },
NICK.flux.createSignUpWidget =  function() {
		    Flux.createWidget('Authentication', { fluxHosted: true },
		     function(authenticationWidget) {
		      authenticationWidget.showSignUpForm();
		     });
		   },
NICK.flux.createInterimWidget =  function() {
		    Flux.createWidget('Authentication', { fluxHosted: true },
		     function(authenticationWidget) {
		      authenticationWidget.showInterimForm();
		     });
		   };


NICK.flux.getQuizRatings = function(){
	$('.quiz-item-rating-container').each(function(index){
		var container = $(this);
		var id = container.children('.itemid').val();
		
		$.ajax({
			dataType:'jsonp',
			url:NICK.flux.baseURL +NICK.flux.ucid + '/feeds/content/?q=' + id,
			success:function(data){
				NICK.flux.setRating(data,container);
			}
		});
		
	}
	);
};
		   
NICK.flux.setRating = function(data,container){
	container.html(data.Rating.ThumbUp + "%");
}

NICK.flux.createFacebookButton =  function() {
	Flux.createWidget(
			'Rpx', {
				elements: [{ id: 'FacebookConnectButton', size: 'medium', length: 'long' }],
				containerId: 'facebookConnectHolder'
			},
			function(rpxWidget) {
				rpxWidget.onConnected = function() { showSettingsOverlay = false; [RegisterAfterSignUpHandler] };
			});
};

NICK.flux.UserBar = function(){
	Flux4.createWidget('UserBar', {
	    displayMode: 'EmbeddedTop'
	});
}

