/*
 * Global JavaScript file enclosing all of custom JavaScript functionality
 * This file declares BLOGS as the global containing object under which all unique blogs
 * functionality should contained. 
 * This file has a dependency on the following: 
 * 		string-utils.js
 *      array-utils.js
 *      jquery.js
 *  
 */

if(!ws) var ws = {};
if(!ws.community) ws.community = {};
if(!ws.community.blogs) ws.community.blogs = {};
if(BLOGS == null) var BLOGS = ws.community.blogs;

// in development set debug level
BLOGS.debugEnabled = false;

//navigation drop down menu on click event
BLOGS.metricsDropDownExe = function() {	
	jQuery("#metricsReportLevel").change(function() {		
		var destination = jQuery(this).attr("value");		
		if(!StringUtils.isEmpty(destination))
			location.href = destination;
	});	
};

BLOGS.fetchTopBloggers = function (labelString, spaceKey, maxResults, successCallback, errorCallback) {
	
	if(StringUtils.isEmpty(spaceKey)) {
		throw {
			name: "SpaceNotFoundException",
			message: "Specify valid space key"		
		}		
	}
	
	if(!maxResults) {
		maxResults = 10;
	}
	
	var selectedGroups = "";
	labelString = StringUtils.defaultIfEmpty(labelString, StringUtils.EMPTY);
	
	if(!StringUtils.isEmpty(labelString)) {
		selectedGroups = "&selectedGroups=" + labelString;
	}
	
	// request constants
	var HTTP_METHOD = "GET";
	var RESPONSE_TYPE = "html";
	var ACTION_URL = "/groupblog/topbloggers-html.action";
	
	
	// fetch top bloggers					
	jQuery.ajax({
		type: HTTP_METHOD,
		url : ACTION_URL,
		dataType: RESPONSE_TYPE,
		data: "spaceKey=" + spaceKey +"&maxResults=" +
			 maxResults + selectedGroups,
		success: successCallback,
		error: errorCallback
	});		
};

BLOGS.fetchMostViewed = function (spaceKey, maxResults, successCallback, errorCallback) {
	
	if(StringUtils.isEmpty(spaceKey)) {
		throw {
			name: "SpaceNotFoundException",
			message: "Specify valid space key"		
		}		
	}
	
	if(!maxResults) {
		maxResults = 10;
	}			
	
	// request constants
	var HTTP_METHOD = "GET";
	var RESPONSE_TYPE = "html";
	var ACTION_URL = "/groupblog/mostviewed-html.action";
		
	// fetch top bloggers					
	jQuery.ajax({
		type: HTTP_METHOD,
		url : ACTION_URL,
		dataType: RESPONSE_TYPE,
		data: "spaceKey=" + spaceKey +"&maxResults=" + maxResults,
		success: successCallback,
		error: errorCallback
	});		
};



BLOGS.fetchBlogs = function (user, labelString, spaceKey, startIndex, 
	period, month, year, successCallback, errorCallback) {
	
	if(StringUtils.isEmpty(labelString)) {	
		labelString = "";
	}
	
	if(StringUtils.isEmpty(user)) {
		user = "";
	}
		
	// request constants
	var HTTP_METHOD = "GET";
	var RESPONSE_TYPE = "html";
	var ACTION_URL = "/groupblog/bloglist-html.action";
	
	var time = "";
	if(period > 0) {		
		time = "&period=" + period + "&postingDate=" + year + "/" + month + "/01";
	}
	
	// fetch blogs					
	jQuery.ajax({
		type: HTTP_METHOD,
		cache: false,
		url : ACTION_URL,
		dataType: RESPONSE_TYPE,
		data: "spaceKey=" + spaceKey +"&startIndex=" +
			 startIndex +"&labelString=" + labelString + time + "&selectedUser=" + user,
		success: successCallback,
		error: errorCallback
	});		
};





BLOGS.fetchBlogArchives = function (spaceKey, maxResults, successCallback, errorCallback) {
	
	if(StringUtils.isEmpty(spaceKey)) {		
		throw {
			name: "SpaceNotFoundException",
			message: "Specify valid space key"		
		}		
	}
	
	if(!maxResults) {
		maxResults = 10;
	}
	
	// request constants
	var HTTP_METHOD = "GET";
	var RESPONSE_TYPE = "html";
	var ACTION_URL = "/groupblog/blogarchives-html.action";
	
	// fetch blogs					
	jQuery.ajax({
		type: HTTP_METHOD,
		url : ACTION_URL,
		dataType: RESPONSE_TYPE,
		data: "spaceKey=" + spaceKey + "&maxResults=" + maxResults,
		success: successCallback,
		error: errorCallback
	});	
};



BLOGS.fetchPopularTags = function (spaceKey, maxResults, successCallback, errorCallback) {
	
	if(StringUtils.isEmpty(spaceKey)) {
		throw {
			name: "SpaceNotFoundException",
			message: "Specify valid space key"		
		}		
	}
	
	if(!maxResults) {
		maxResults = 10;
	}
	
	// request constants
	var HTTP_METHOD = "GET";
	var RESPONSE_TYPE = "html";
	var ACTION_URL = "/groupblog/populartags-html.action";
	
	
	// fetch popular tags					
	jQuery.ajax({
		type: HTTP_METHOD,
		url : ACTION_URL,
		dataType: RESPONSE_TYPE,
		data: "spaceKey=" + spaceKey +"&maxResults=" + maxResults,
		success: successCallback,
		error: errorCallback
	});		
};

BLOGS.getUserPrefsCookie = function () {	
    var PERM_PREFS_COOKIE_KEY = "blogsPrefs";     
	return CookieUtils.readCookie(PERM_PREFS_COOKIE_KEY);    	        	          
};


BLOGS.setUserPrefsCookie = function (labelString) {		
    var PERM_PREFS_COOKIE_KEY = "blogsPrefs";     
    StringUtils.defaultIfEmpty(labelString, StringUtils.EMPTY);
	CookieUtils.createCookie(PERM_PREFS_COOKIE_KEY, labelString, 30, "/blogs");    	        	          
};


BLOGS.showRSSLinks = function(){
	
	setTimeout(function (){
		
		jQuery(".rss_icon_topbloggers").each(function() {
			
			jQuery(this).css("display", "block");
			
			
		});
		
	},250);

	
}
BLOGS.hideRSSLinks = function(){
	
	setTimeout(function (){
		
		jQuery(".rss_icon_topbloggers").each(function() {
			
			jQuery(this).css("display", "none");
			
			
		});
	},10);
	

	
}

BLOGS.showRSSLinks2 = function(){
	
	setTimeout(function (){
		
		jQuery(".rss_icon_poptags").each(function() {
			
			jQuery(this).css("display", "block");
			
			
		});
		
	},250);

	
}
BLOGS.hideRSSLinks2 = function(){
	
	setTimeout(function (){
		
		jQuery(".rss_icon_poptags").each(function() {
			
			jQuery(this).css("display", "none");
			
			
		});
	},10);
	

	
}
