/**
 * Javascript library for adding dynamic blog components
 * on a page.
 *
 * Possible variables to fill:
 
     babc_topic (Required)
         Defines what to use for the blog search. If not available, script
         will abort.
 
     babc_type  (box | content) - default 'content'
         Defines what the output will be - a full SMRC component box
         or just the content from that would be in the box (in case want
         to add to an existing box).

     babc_align (right | left | center | none) - default 'right'
         Tells which way to align the information (if a full component)
     
     babc_title
         default "Recent Blogs on this Topic"
         Defines what the title of the box or the content list would be.

     babc_content_separator (top | bottom | "") - default ""
         Defines whether or not to put an '<hr />' in place
         to separate out the content (if combined in another box).
         Only used if 'babc_type = content'.
   
     babc_loading_feedback (true | false) - default "false"
         Define to give feedback for loading and/or no results feedback.
         
     babc_archive_link (true | false) - default "false"
         Define whether or not to print out the 'See All Results' link.
 
 */


/*

Examples:


Prints out a SMRC Content box aligned to the left with ZFS Blog info.

<!-- BEGIN BLOG COMPONENT -->
<script type="text/javascript" src="/bigadmin/js/ba_blogs_component.js"></script>
<script type="text/javascript">
	babc_type = "block";
	babc_align = "left";
	babc_topic = "ZFS";
	babc_init();
</script>
<!-- END BLOG COMPONENT -->


Prints out a bulleted list with ZFS Blog info.

<!-- BEGIN BLOG COMPONENT -->
<script type="text/javascript" src="/bigadmin/js/ba_blogs_component.js"></script>
<script type="text/javascript">
	babc_type = "content";
	babc_topic = "ZFS";
	babc_content_separator = "top";
	babc_init();
</script>
<!-- END BLOG COMPONENT -->


*/

/******* VARS ********/

// User definable - set to defaults
var babc_topic = "";
var babc_align = "right";
var babc_type = "content";
var babc_title = "";
var use_babc_title = "true";
var babc_content_separator = "";
var babc_loading_feedback = "";
var babc_archive_link = "";
var babc_box_width="";

// Non-user definable (private)
var babc_component_id = "ba_blog_component";
var babc_req_var;
var babc_isIE = false;
var babc_valid = false;

var loadContentString = "Loading Content...";

// Set flags per user definable var
var fLoadingFeedback = false;
var fArchiveLink = false;


/******* METHODS *******/

/**
 * Initialize the process of grabbing the content.
 */
function babc_init() {
	// Make sure we have what we need before we go anyfurther
	if (babc_topic != '') {
	
		// Set some flags we use
		if (babc_loading_feedback != "" && babc_loading_feedback == "true") {
			fLoadingFeedback = true;
		}
		
		if (babc_archive_link != "" && babc_archive_link == "true") {
			fArchiveLink = true;
		}
	
		// Set up some vars if needed
		if (babc_title == "")
			babc_title = "Recent Blogs on '" + babc_topic + "'";
		
		// Print out the correct partition
		if (babc_type == "box") {
			// Print out box
			babc_printContentBox();
		} else {
			// Content only - the default - so if not set to 'box', do this.
			babc_printContentOnly();
		}
		
		// We have what we need - append the onload queue to fill in.
		babc_addLoadEvent(babc_onLoad);
	}
}

/**
 * OnLoad method to gather the info for the blogs.
 */
function babc_onLoad() {
	// Since we added to the onload to start here, we should have
	// our elements in place. Create the content.
	babc_createBlogContent();
}

/**
 * Set up the motion and grab the content.
 */
function babc_createBlogContent() {
	babc_req_var = babc_initRequest();
	var url = "/cgi-bin/sun/bigadmin/getBlogInfo.cgi?blog_keyword=" + escape(babc_topic);
	babc_req_var.onreadystatechange = babc_processReqChange;
	babc_req_var.open("GET", url, true);
	babc_req_var.send(null);
}

/**
 * Initialize the XMLHttpRequest object.
 */
function babc_initRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		babc_isIE = true;
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

/**
 * Parse the messages back from the response and write out accordingly.
 */
function babc_parseMessages(responseXML) {
	var resultOut = "";
	var searchResults = responseXML.getElementsByTagName("results")[0];
	
	// Clear the HTML list used to display the cart contents
	var contents = document.getElementById("babc_results-output");
	contents.innerHTML = "";	
	var results = searchResults.getElementsByTagName("result");
	var query = responseXML.getElementsByTagName("query")[0].firstChild.nodeValue;
	var queryString = "";
	
	if (results.length > 0) {
		for (var I = 0 ; I < results.length ; I++) {
			var result = results[I];
			var title = result.getElementsByTagName("title")[0].firstChild.nodeValue;
			var description = result.getElementsByTagName("description")[0].firstChild.nodeValue;
			var date = result.getElementsByTagName("date")[0].firstChild.nodeValue;
			var url = result.getElementsByTagName("url")[0].firstChild.nodeValue;
			
			resultOut += "<li class=\"small\"><a href=\"" + url + "\" target=\"blog_window\" class=\"small\">" + title + "</a></li> \n";
		}
		
		// Set the inner HTML of the component with the results.
		if (babc_type != "box") {
			if (babc_content_separator == "top")
				contents.innerHTML += "<hr class=\"light\"/> \n";
			if (use_babc_title == "true")
				contents.innerHTML += "<b>" + babc_title + "</b><br /> \n";
		}
		contents.innerHTML += queryString + "<ul class=\"small\">\n" + resultOut + "</ul>";
		if (fArchiveLink) {
			contents.innerHTML += "<p align=\"right\"><a class=\"small\" href=\"" + babc_getArchiveSearchURL('blog') + "\" target=\"blog_window\"><span class=\"rightarrowblue\">&raquo;</span>&nbsp;See All Results</a></p>";
		}
		
		if (babc_type != "box" && babc_content_separator == "bottom")
			contents.innerHTML += "<hr class=\"light\"/> \n";
			
		// Bring the box to the front if there is one (this should have been
		// set to hidden in the creation).
		if (document.getElementById('ba_blog_component')) {
			var blogBlock = document.getElementById('ba_blog_component');
			blogBlock.style.display = 'block';
		}

	} else {
		if (fLoadingFeedback) {
			if (babc_type != "box" && use_babc_title == "true") {
				contents.innerHTML += "<b>" + babc_title + "</b><br /> \n";
			}
			contents.innerHTML += "<p><b class=\"small\" style=\"margin-left:5px;\">No Results Found</b></p>\n";
		}
	}
}

function babc_processReqChange() {
	// only if req shows "complete"
	if (babc_req_var.readyState == 4) {
		// only if "OK"
		if (babc_req_var.status == 200) {
			babc_parseMessages(babc_req_var.responseXML.documentElement);
		} else {
			// alert("There was a problem retrieving the XML data:\n" + babc_req_var.statusText);
		}
	}
}

function babc_printContentBox() {
	var styleString = "";
	var displayString = "display:none";

	// Get the alignment and style
	if (babc_align == "") babc_align = "right";
	if (fLoadingFeedback) displayString = "";
	if (babc_box_width = "") babc_box_width = "40%";
	
	if (babc_align == "right") {
		styleString = displayString + ";margin-left:15px;margin-bottom:5px;float:" + babc_align + ";clear:" + babc_align + ";";
	} else if (babc_align == "left") {
		styleString = displayString + ";margin-right:15px;margin-bottom:5px;float:" + babc_align + ";clear:" + babc_align + ";";
	} else if (babc_align == "center") {
		styleString = displayString + ";margin-bottom:5px;width:100%;clear:all;";
	}

	document.write("<!-- BEGIN BLOG COMPONENT -->  \n");
	document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"" + babc_box_width + "\" id=\"ba_blog_component\" style=\"" + styleString + "\">  \n");
	document.write("<tr><td> \n");
	document.write("<!-- BEGIN G1 FEATURED CONTENT COMPONENT, VARIATION 4 -->  \n");
	document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">  \n");
	document.write("<tr><td class=\"tablecaption\"><div class=\"headerpadding\">" + babc_title + "</div></td></tr>  \n");
	document.write("</table>  \n");
	document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"dkgrey1\" width=\"100%\">  \n");
	document.write("<tr><td>  \n");
	document.write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" >  \n");
	document.write("<tr class=\"yellow1\">  \n");
	document.write("<td><div class=\"pad5x10\">  \n");
	document.write("	<div id=\"babc_results-output\">\n");
	if (fLoadingFeedback) {
		document.write("<p><b class=\"small\" style=\"margin-left:5px;\">" + loadContentString + "</b></p> \n");
	}
	document.write("	</div>  \n");
	document.write("</div></td></tr>  \n");
	document.write("</table>  \n");
	document.write("</td></tr>  \n");
	document.write("</table>  \n");
	document.write("<span class=\"sp10\">&#160;</span><br />  \n");
	document.write("<span class=\"sp10\">&#160;</span><br />  \n");
	document.write("<!-- END G1 FEATURED CONTENT COMPONENT, VARIATION 3 -->  \n");
	document.write("</td></tr>  \n");
	document.write("</table>  \n");
	document.write("<!-- END BLOG COMPONENT -->  \n");
}


/*

<!-- BEGIN G1 FEATURED CONTENT COMPONENT, VARIATION 4 -->
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="tablecaption"><div class="headerpadding">BigAdmin Predictive Self-Healing Page</div></td></tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="dkgrey1" width="100%" summary="">
<tr><td>
<table border="0" cellpadding="0" cellspacing="1" width="100%" >
<tr class="yellow1">
<td><div class="pad5x10">
<p>Pointers to several information sources about SMF are included in the <a href="/bigadmin/content/selfheal/">BigAdmin Predictive Self-Healing page</a>.</p>
</div></td></tr>
</table>
</td></tr>
</table>
<span class="sp10">&#160;</span><br />
<span class="sp10">&#160;</span><br />
<!-- END G1 FEATURED CONTENT COMPONENT, VARIATION 4 -->

*/



function babc_printContentOnly() {
	document.write("<div id=\"babc_results-output\">");
	if (fLoadingFeedback) {
		if (use_babc_title == "true")
			document.write("<b>" + babc_title + "</b><br /> \n");
		document.write("<p><b class=\"small\" style=\"margin-left:5px;\">" + loadContentString + "</b></p> \n");
	}
	document.write("</div>  \n");
}

/**
 * Add to the onload event so we don't overstep and overwrite an existing
 * onload even method.
 */
function babc_addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
  	}
}

/**
 * Get the search URL needed for the archive link.
 * @param Which url to retreive.
 *
 * Some OneSearch params
 * nh = number of returns
 * rf = 0 (return by relevance)
 * rf = 1 (return by date)
 * cs = case sensitivity (0 - off | 1 - true)
 */
function babc_getArchiveSearchURL(urlType) {
	if (urlType == null) {
		urlType = "blog";
	}
	
	// Replace '%22' to '"' in the string for the URL
	if (babc_topic.indexOf("%22") != -1) {
		babc_topic = babc_topic.replace(/%22/g,"\"")
	}
	
	// Return the correct URL
	if (urlType == "blog") {
		return "http://search.sun.com/blogs/index.jsp?qt=" + escape(babc_topic) + "&col=blogs&charset=utf-8&reslang=en&cs=0&rf=1";
	} else if (urlType == "training") {
	}
}


