var G_loaderImgHTML = '<img src="/sites/all/themes/qd/custom_code/lib/img/loader/indicator_mozilla_blu.gif" alt="loading..." />';
var G_endOfTime = 0;
var persistant = false;

if(persistant){	// setup an endtime for the users aggrigator session
	G_endOfTime = timeTillDate(18,1,2038); // persistant cookie	
}else{ 	// setup a 1 day time limit for this users aggrigator session

	// get the end time data
	d = new Date;
	var cyear = d.getFullYear();
	var cmonth = d.getMonth() + 1;
	var cday = d.getDate() + 1;
	var checkDay = d.getDaysInThisMonth();
	
	// guard against overruning valid date renges
	if(cday > checkDay){ 
		cday = 1; /* make sure we are not overrunning this year/month */ 
		cmonth += 1;
	}

	if(cmonth > 12){ /* we overran the year. reset to 1 (january) */ 
		cmonth = 1;
	}

	G_endOfTime = timeTillDate(cday,cmonth,cyear); // cookie lasts 1 day
}


$(document).ready(function() {
	// laod th last insider section that this user looked at
	var lastCid = getLastCommunityID();
	loadInsider(lastCid);
	$('.loaderIndicator').html(G_loaderImgHTML);
	
	// warn against using safari
	// if($.browser.safari){
	// 	var cstring = 'Were Sorry. The Quality Digest home page article aggrigator is not currently functioning properly when using safari. \n\n We recommend using FireFox. \n\n\n Would you like to Download FireFox now?';
	// 	if(confirm(cstring)){
	// 		window.location = 'http://www.google.com/search?hl=en&q=download+firefox&btnI=true';
	// 	}
	// }
});

function menuLoad(CommunityHandleTID,activeContentType){

	// configure and load a client side menu
	$('#contentTypeHandles').html(G_loaderImgHTML);
	var menu = '<li><a id="ch_article" href="javascript:void 0;" onclick="loadType(this,\'article\',%s);">Articles</a></li>';
	menu += '<li><a id="ch_column" href="javascript:void 0;" onclick="loadType(this,\'column\',%s);">Columns</a></li>';
	menu += '<li><a id="ch_news" href="javascript:void 0;" onclick="loadType(this,\'news\',%s);">News</a></li>';
	menu += '<li><a id="ch_forum" href="javascript:void 0;" onclick="loadType(this,\'forum\',%s);">Forums</a></li>';
	menu += '<li><a id="ch_video" href="javascript:void 0;" onclick="loadType(this,\'video\',%s);">Videos</a></li>';
	menuOut = menu.replace(/%s/g,CommunityHandleTID);
	$('#contentTypeHandles').html(menuOut); // load the menu
	$('#ch_'+activeContentType).attr({'class':'active'}); // set the incoming menus content type to active

	// DO NOT DELETE =================================================================================
	// load a menu from thE server side. this is will be used if the client wants to compose communities of different content types or wishes to arrange the menus differently for each community (by number of entries, different content types, etc...)
	// show the laoder
	// $('#contentTypeHandles').html(G_loaderImgHTML);
	// 
	// $.ajax({
	// 	async: true,
	// 	type: "POST",
	// 	url: '?q=services/json',
	// 	data: 'method=headlines.menu&loadCid=' + CommunityHandleTID,
	// 	dataType: "json",
	// 	timeout: 30000,
	// 	error: function(request,estring,exception){ 
	// 		var mess = ajax_error(request,estring,exception);
	// 		$('#contentTypeHandles').html('<li class="error">Menu Could not be loaded. ErrorMsg: ' + mess + '<li>');
	// 	},
	// 	success: function(data){
	// 		
	// 		if(data.status){
	// 			$('#contentTypeHandles').html(data.data); // load the incoming menu
	// 			$('#ch_'+activeContentType).attr({class:'active'}); // set the incoming menus content type to active
	// 		}else{
	// 			$('#contentTypeHandles').html('<li class="error">Menu Could not be loaded. ErrorMsg: ' + data.data + '<li>');
	// 		}
	// 	},
	// 	complete: function(request,status){
	// 		// report(msg,{inject:'#board_sort_messages',cls:'appmsg'});
	// 	}
	// });
	// ==============================================================================================

}

function headlinesLoad(CommunityHandleTid,activeContentType,getNumHeadlines){


	// choose a return area and a loader based on the requested type
	var outputArea = (activeContentType != 'videot') ? '#contentEntries' : '#currentVidThumbs';
	var loader = (activeContentType != 'videot') ? '<p>'+G_loaderImgHTML+'</p>' : '<div class="video-thumb"><p>'+G_loaderImgHTML+'</p></div>' ;

	// show the loader
	$(outputArea).html(loader);

	$.ajax({
		async: true,
		type: "POST",
		url: '?q=services/json',
		data: 'method=headlines.list&loadCid=' + CommunityHandleTid + '&loadType=' + activeContentType,
		dataType: "json",
		timeout: 30000,
		error: function(request,estring,exception){ 
			var mess = ajax_error(request,estring,exception);
			$(outputArea).html('<li class="error">Article list could not be loaded. ErrorMsg: ' + mess + '<li>');
		},
		success: function(data){
			
			if(data.status){
				$(outputArea).html(data.data); // insert the loaded headlines
			}else{
				$(outputArea).html('<li class="error">Article list could not be loaded. ErrorMsg: ' + data.data + '<li>');
			}
		},
		complete: function(request,status){
			// report(msg,{inject:'#board_sort_messages',cls:'appmsg'});
		}
	});
	
}

function currentVideosLoad(communityHandleTid,getNumHeadlines){
	headlinesLoad(communityHandleTid,'videot');
}

function getLastCommunityID(){
	var lastCid = (Cookies.get('lastCid')) ? Cookies.get('lastCid') : 69; // Default to the metrology category. This could be any member tid from the qd_community_member table.
	// console.log(lastCid);
	return lastCid;
}

function getLastContentType(){
	var lastType = (Cookies.get('lastType')) ? Cookies.get('lastType') : 'video'; // Default to ann article content type. This could be any member content_type from the qd_community_member table.
	// console.log(lastType);
	return lastType;
}

// load an insider main area
// loadCid = load community data related to the loadCid
function loadInsider(loadCid){
	// set the handle active
	$('#communityHandles').children('li').removeClass('active');
	$('#cm_'+ loadCid).addClass('active');
	
	// save the users choice if we need to 
	Cookies.set('lastCid',loadCid,G_endOfTime);
		
	// set the insider title
	$('#communityName').html($('#cm_'+ loadCid).children('a').html());

	var lastType = getLastContentType();
	menuLoad(loadCid,lastType);

	// load the headlines for this community
	headlinesLoad(loadCid,lastType,5);

	// load the video area for this communityContentType
	currentVideosLoad(loadCid,2);
	
}

// set an insider area active
function loadType(el,communityContentType,loadCid){

	// manage the loader handles
	$('#contentTypeHandles li a').removeAttr('class');
	$(el).attr({'class':'active'});
	
	// save the users choice
	Cookies.set('lastType',communityContentType,G_endOfTime);
	
	// call the data loader; pass the type and the id into it
	headlinesLoad(loadCid,communityContentType,10);	
}
