/**
 * print.js
 *
 * Contains scripts for print feature for advance text blog entries.
 * Requires jquery-1.2.6.js to be loaded first.
 *
 * Copyright (c) Advance Internet. All rights reserved.
 */
//------------------------------------------------------------------------------------
/**
 * PageQuery
 * @desc - singleton class for the page query string
 * @properties - q_str {string} raw query string
 * 				  keyValuePairs {array}
 * @methods - getValue(key) returns the value of key arg
 * 			  getParameters() returns array of keys
 */
var PageQuery = new function () {
	var q = window.location.search;
	
	this.q_str = (q.length > 1) ? q.substring(1, q.length) : null;
	this.keyValuePairs = new Array();
	if (q) {
		for (var i=0; i < this.q_str.split("&").length; i++) {
			this.keyValuePairs[i] = this.q_str.split("&")[i];
		}
	}
	this.getValue = function(s) {
		for (var j=0; j < this.keyValuePairs.length; j++) {
		 if (this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	};
	this.getParameters = function() {
		var a = new Array(this.keyValuePairs.length);
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	};
};
//------------------------------------------------------------------------------------
/**
 * printLoad
 * @desc - populates the article from the opening doc
 */
printLoad = function () {
	if (PageQuery.q_str == null) {
		var opener = window.opener.document;
		document.title = opener.title;
		var oContents = jQuery(jQuery(".full_entry",opener).contents());
		scrapeEntry(oContents, false);
	} else {
		var blog = window.location.href.replace('/print.html'+window.location.search, '');
		var entry_path = PageQuery.getValue('entry');
		jQuery.ajax({
			type: "GET",
			url: blog+entry_path,
			dataType: "html",
			success : function(html) {
				var oContents = jQuery(jQuery(".full_entry",jQuery(html))).contents();
				scrapeEntry(oContents, true);
			}
		});
	}
	//window.print();
};
//------------------------------------------------------------------------------------
/**
 * scrapeEntry
 * @desc - populates the article from the opening doc
 */
scrapeEntry = function (oContents, isAjax) {
	oContents.each( function() {
		if (this.nodeName != "#text") {
			if (this.id == "article") {
				var cp = this.cloneNode(true);
				jQuery(cp).find("a").each(function() {
					var txt = jQuery(this).text();
					jQuery(this).replaceWith('<strong>'+ txt +'</strong>');
				});
				if (isAjax) document.title = jQuery(cp).find("h1").text();
				var t = jQuery(cp).find("h5").text();
				var d = new Date(t.substr(0,t.length-2));
				jQuery("span#year").html(d.getFullYear());
				jQuery("#PrintContainer").html(cp.innerHTML)
			}
		}
	});
};
//------------------------------------------------------------------------------------
jQuery(document).ready(function() {
	printLoad();
});

