// JavaScript Document

/* Global Variables
------------------------------------------------------------------------------------------------- */
var appConfig={auth:{facebook:{key:'8105df7d6b6ca57246e88854ace0b35f',secret:'5b731622226a511b311dff4bdaa87d86'}}};


/* Splash Tabs
------------------------------------------------------------------------------------------------- */
var tabCurrent="";
var tabList=new Array("headlines","friends","truth");
var tabConfig={"headlines":[{"src":"/images/v2/splash/news-tabs/headlines/headlines-on.jpg"},
						   {"src":"/images/v2/splash/news-tabs/headlines/friends-off.jpg"},
						   {"src":"/images/v2/splash/news-tabs/headlines/truth-off.jpg"},
						   {"src":"/images/v2/splash/news-tabs/headlines/x-right.jpg"}],
			   "friends"  :[{"src":"/images/v2/splash/news-tabs/friends/headlines-off.jpg"},
						   {"src":"/images/v2/splash/news-tabs/friends/friends-on.jpg"},
						   {"src":"/images/v2/splash/news-tabs/friends/truth-off.jpg"},
						   {"src":"/images/v2/splash/news-tabs/friends/x-right.jpg"}],
			   "truth"    :[{"src":"/images/v2/splash/news-tabs/truth/headlines-off.jpg"},
						   {"src":"/images/v2/splash/news-tabs/truth/friends-off.jpg"},
						   {"src":"/images/v2/splash/news-tabs/truth/truth-on.jpg"},
						   {"src":"/images/v2/splash/news-tabs/truth/x-right.jpg"}]
				};
				
var tabConfigImgHover={"headlines":"/images/v2/splash/news-tabs/headlines-hover.jpg",
					   "friends":"/images/v2/splash/news-tabs/friends-hover.jpg",
					   "truth":"/images/v2/splash/news-tabs/truth-hover.jpg"};

var tabConfigContainerMap={"headlines":"tabHandle0",
						   "friends":"tabHandle1",
						   "truth":"tabHandle2"};

function skinTabs(userContext) {
	for(var i=0; i<tabConfig[userContext].length; i++) {
		$('#tabHandle' + i).attr("src",tabConfig[userContext][i]["src"]);
		$('#tabHandle' + i).attr("srcOff",tabConfig[userContext][i]["src"]);
	}
}

function showTabContent(userTabName) {
	// show news items
	$('#tabContentBody').load('/news.get/' + userTabName,{},showImgContent);
}

function exeTab(userTabName) {
	$('#tabContentLoadingMsg').show();
	skinTabs(tabCurrent);
	showTabContent(tabCurrent);		
	$('#tabContentLoadingMsg').hide();
	
	// show the image content
	showImgContent();
}

/* Splash Tabs - END
------------------------------------------------------------------------------------------------- */


/* News Image Promotion
------------------------------------------------------------------------------------------------- */
function showImgContent() {

	// Offset coordinates for placing of background image
	var bgPosH="-5px -60px";
	var bgPosV="-5px -100px";

	// Iterate through all the imgCanvas nodes making the specified pix as the background-image
	$("#tabContentBody .imgCanvas").each(function(n){
								  
		// Apply the background image
		$(this).css("background-image","url('" + $(this).attr("pix") + "')");

		// Apply the right background position
		switch($(this).attr("pixPos"))
		{
			case "h":
				$(this).css("background-position",bgPosH);
			break;
			case "v":
				$(this).css("background-position",bgPosV);
			break;
		}
	});
		
}


/* Global OnReady Block
------------------------------------------------------------------------------------------------- */

function initFB() {

	//FB_RequireFeatures(["XFBML"], function(){FB.init(appConfig["auth"]["facebook"]["key"], "/data/Facebook/xd_receiver.htm", {"reloadIfSessionStateChanged":true});});
	FB.init({appId:appConfig["auth"]["facebook"]["key"], status:true,cookie:true,xfbml:true});
 	FB.Event.subscribe('auth.login', function(response){window.location.reload();});
}


function initNewsStoryHover() 
{
	alert($('#splashTab .colGroup .colItem .gridItemContainer').length);
	// Splash: Bind an mouseover + mouseout handler to high light he new items
	$('#splashTab .colGroup .colItem .gridItemContainer').each(function(){
		$(this).mouseover(function(){
			alert("Hello World");						   
		});
	});
	
}

// Retrieves the comments for a particular page or context
function pageCommentsLoad(userSrcId){
	if($('#pageComments')) {
		$('#pageComments').load('/comments.get/' + userSrcId);
	}
}

// Broker function to perform the AJAX call of making a comment for a particular page
function pageCommentsMake(userOpts) {
			
		// Step #2: Make the AJAX call
		$.post('/comments.make',
			   {accountId:userOpts["accountId"],
			    source:userOpts["source"],
				sourceId:userOpts["sourceId"],
				pageLink:userOpts["pageLink"],
				comment:userOpts["comment"]},			   
			   function(data){
				   	//alert(data);
		   			$('#userComment').val("");
					$('#userComment').attr('disabled',false);
					$('#btnPageMakeComment').attr('value','Post Comment');
					$('#btnPageMakeComment').attr('disabled',false);
					
					// Load the new comments
					pageCommentsLoad(userOpts["sourceId"]);
			   });
}

/* Global OnReady Block
------------------------------------------------------------------------------------------------- */
$(document).ready(function(){ 	
		
	// Set starting tab only if we're on the splash pages
	if($('#splashTabContainer'))
	{
		// assign a click handler to all the img containers
		$('#tabHandles img').each(function(event){
	
			// When clicked reskin tab set
			$(this).click(function(event){
				tabCurrent=$(this).attr("name");
				exeTab(tabCurrent);
			});
			
			// When the mouse goes over and out ..
			$(this).hover(function(event){		
						  if(tabCurrent!=$(this).attr("name")) {
								$('#' + tabConfigContainerMap[$(this).attr("name")]).attr("src",tabConfigImgHover[$(this).attr("name")]);
							}	
						  },function(event){
							if(tabCurrent!=$(this).attr("name")) {
								$('#' + tabConfigContainerMap[$(this).attr("name")]).attr("src",$('#' + tabConfigContainerMap[$(this).attr("name")]).attr("srcOff"));
							}						  
						  });		
		});
		
		// Set the initial tab context
		tabCurrent="headlines";
		//exeTab(tabCurrent);
		
		// Show the image content
		showImgContent();
	}
	
	
	// Any error messages?
	if($('#dohContainer')) {
		window.setTimeout(function(){$('#dohContainer').hide('slow');},10000);
	}
	
	// Initializae the page comments if the function exists
	if(typeof initPageComments=='function') {
		initPageComments();	
	}
	
	// Do an adHoc onPageLoadDO()
	if(typeof onThePageLoadDO=='function') {
		onThePageLoadDO();	
	}

	// Initialize and starting blocks
	if(typeof initSignin=='function'){
		initSignin();	// Code to implement sign-in process
	}

});
