
(function(){
	if(typeof printBasket=="undefined"||!printBasket){
		var printBasket = window.printBasket = {
			//Asign id to Basket
			id: "printBasket",
			button_id: "printBasket_button",
			total_id: "printBasket_total",
			
			//Asign Cookie Names
			CookieTitles: "printBasketTitles",
			CookieURLs: "printBasketURLs",
			CookieCategories: "printBasketCategories",
			
			// Asign Printer Icon Names
			ImgAddPrinter: "images/print-basket-add.png",
			ImgRemovePrinter: "images/print-basket-delete.png",
			
			//Asign Content Div Id
			ContentDivId: "hsbody",
			
			// Assign StyleSheet Src 
			PrintCssSrc: "css/print.css",
			CssSrc: "css/style.css",
			
			// Assign Print StyleSheet Src 
			PrintClass: "printBasketPrintClass",

			//Asign Print Basket Content Div Id
			PrintContentDivId: "printBasketShadow",
			
			addButtonText: "Add page to print basket",
			removeButtonText: "Remove this page form print basket",
			
			//Asign Domain
			domain: "/"

		};
		
		// Assemble with other frameworks
		if(typeof $$=="undefined"||!$$) {	
			printBasket.util = {
				selector: function(x){
					var y = x.replace(/#/, "");
					var z = x.charAt(0);
					switch (z) {
						case "#": return document.getElementById(x); break;
						default: return document.getElementById(x);
					}
				},
				obj: function(o){
					if (typeof o == 'object')
						return true
				}
			};
			
			// Expose
			$$ = printBasket.util.selector;
			
			document.onclick = function(event){
				var o;
				try {
					o = event.target;
				} catch (exception) {
				}
				if(!o) o = window.event.srcElement;
					printBasket.Main(o, event)
			};
		}
	};
	
	
	/*- Basket Main
	--------------------------------------------*/	
	printBasket.Main = function(o, event) {
		if(o.href){	
			var regExp = /(.*)?#(\w+)?-(\d+|\*)/;
						//[href]#[fn name]-[n]
			var _regExp = /(.*)?#(\w+)/;
						//[href]#[fn name]
			var a = regExp.exec(o.href) ? regExp.exec(o.href) : _regExp.exec(o.href);
			if(a && a[2]) {
				var b = 'printBasket.' + a[2].toString() + "(" + '\"' + a[3] + '\"' + ', \"' + o + "\");";
				try {
					return eval(b);				
				} catch (exception) {
					return false;
				}
			}				
		} else if(typeof o == "string") {
			try {
				return eval('printBasket.' + o + "()");
			} catch (exception) {
				return false;
			}
		}
	};
	
	
	/*- Basket Read
	--------------------------------------------*/
	printBasket.Read = function(cookieName) {
		if(printBasket.Read.Cookie(cookieName)) {
			return printBasket.Read.Cookie(cookieName).split('_-');
		}
		return null;
	};
	
	
	/*- Basket Add
	--------------------------------------------*/
	printBasket.Add = function(cookieName, cookieValue) {
		if(cookieName != "undefined") {
			printBasket.Add.Cookie(cookieName, cookieValue);
		} else {
			var titleCookieValue = printBasket.Title();
			var urlCookieValue = printBasket.URL();
			var categoryCookieValue = printBasket.Category();
			
			titleCookieValue = printBasket.Read.Cookie(printBasket.CookieTitles) ? printBasket.Read.Cookie(printBasket.CookieTitles) + '_-' + titleCookieValue : titleCookieValue;
			urlCookieValue = printBasket.Read.Cookie(printBasket.CookieURLs) ? printBasket.Read.Cookie(printBasket.CookieURLs) + '_-' + urlCookieValue : urlCookieValue;
			categoryCookieValue = printBasket.Read.Cookie(printBasket.CookieCategories) ? printBasket.Read.Cookie(printBasket.CookieCategories) + '_-' + categoryCookieValue : categoryCookieValue;
			
			if(categoryCookieValue.length < 4096  && urlCookieValue.length < 4096  && titleCookieValue.length < 4096) {
				printBasket.Add(printBasket.CookieTitles, titleCookieValue);
				printBasket.Add(printBasket.CookieURLs, urlCookieValue);
				printBasket.Add(printBasket.CookieCategories, categoryCookieValue);
				printBasket.Update()
			}
		}
	};

	
	
	/*- Basket RemoveAll
	--------------------------------------------*/
	printBasket.RemoveAll = function() {
		if(confirm(printBasket.Msg.confirmRemoveAll)) {
			printBasket.Remove.Cookie(printBasket.CookieTitles);
			printBasket.Remove.Cookie(printBasket.CookieURLs);
			printBasket.Remove.Cookie(printBasket.CookieCategories);
			var o = $$(printBasket.id);
			o.innerHTML = printBasket.Msg.empty;
			return true;
		}
	};
	
	
	/*- Basket Remove
	--------------------------------------------*/
	printBasket.Remove = function(n) {
		
		var Titles = printBasket.Read(printBasket.CookieTitles);
		var URLs = printBasket.Read(printBasket.CookieURLs);
		var Categories = printBasket.Read(printBasket.CookieCategories);
				
		Titles.splice(n,1);
		URLs.splice(n,1);
		Categories.splice(n,1);
		
		printBasket.Cookie.Update(Titles,URLs,Categories);
		
		printBasket.Table.Update();
		
		printBasket.Update();
		
	};
	

	/*- Basket ChangeOrder
	--------------------------------------------*/	
	printBasket.ChangeOrder = function(n,i) {
		
		var Titles = printBasket.Read(printBasket.CookieTitles);
		var URLs = printBasket.Read(printBasket.CookieURLs);
		var Categories = printBasket.Read(printBasket.CookieCategories);
		
		var Title = Titles[i];
		var URL = URLs[i];
		var Category = Categories[i];
		Titles[i] = Titles[n];
		URLs[i] = URLs[n];
		Categories[i] = Categories[n];
		Titles[n] = Title;
		URLs[n] = URL;
		Categories[n] = Category;

		if(printBasket.Cookie.Update(Titles,URLs,Categories))
			printBasket.Update()
	};
	
	
	/*- Basket ChangeOrder Up
	--------------------------------------------*/
	printBasket.ChangeOrder.Up = function(n) {
		var i = (parseInt(n) - 1);
		printBasket.ChangeOrder(n,i);
	};

	
	/*- Basket ChangeOrder Bottom
	--------------------------------------------*/
	printBasket.ChangeOrder.Down = function(n) {
		var i = (parseInt(n) + 1);
		printBasket.ChangeOrder(n,i);
	};
	
	
	/*- Basket LoadHTML
	--------------------------------------------*/
	printBasket.LoadHTML = function(url){
		var htmlDoc;
		try {
			htmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (exception) {
		}
		if(htmlDoc) {
		} else {
			htmlDoc = new XMLHttpRequest();
		}
		htmlDoc.open("GET", url, false);
		htmlDoc.send(null);
		return htmlDoc.responseText;
	};
	
	
	/*- Basket Title
	--------------------------------------------*/
	printBasket.Title = function(o) {
		//return document.title;
		return $$("printBasketTitle").value;
	};

	
	/*- Basket URL
	--------------------------------------------*/
	printBasket.URL = function(o) {
		var a = window.location.toString().split(printBasket.domain);
		if(a[1]) {
			var b = a[1].split('#');
			return b[0] ? b[0] : ' ';
		}
		return printBasket.domain
	};
	
	
	/*- Basket Category
	--------------------------------------------*/
	printBasket.Category = function(n) {
		/*n = n ? n : 0;
		var regExp = /(\w+)?-(.*)/;
		var a = regExp.exec(printBasket.Title());
		//return a[n]*/
		return $$("printBasketCategory").value ? $$("printBasketCategory").value : 'undefined';
	};

	
	/*- Basket Table
	--------------------------------------------*/
	printBasket.Table = function(o) {
		var Titles = printBasket.Read(printBasket.CookieTitles);
		var URLs = printBasket.Read(printBasket.CookieURLs);
		var Categories = printBasket.Read(printBasket.CookieCategories);
		if(Titles) {
			var Table = '<div id="'+ printBasket.id +'"><br /><table cellpadding=\"0\" cellspacing=\"0\">';
			Table += '\n\t<thead>\n\t\t<tr><!--<th>Serial No.</th>--><th>Title</th><th>Order</th><th>Print</th><th>Delete</th></tr>\n\t</thead>\n\t<tbody>\n';
			var className;
			for(var n=0;n<Titles.length;n++){
				var up = n ? '#Up-' + n : '#';
				var UpArrowClass = n ? 'class="upArrow"' : 'class="upArrow upDisabled"';
				var DownArrowClass = (n==(Titles.length-1)) ? 'class="downArrow downDisabled"' : 'class="downArrow"';
				var down = (n==(Titles.length-1)) ? '#' : '#Down-' + n;
				var categoryName = (Categories[n] != 'undefined') ? '<strong>' + Categories[n] + '</strong> - ' : '';
				Table += '\t\t<tr' + className + '\"><!--<td>' + (n+1) + '</td>--><td><a href=\"' + URLs[n] + '\">' +  categoryName + '' + Titles[n] + '</td><td><a ' + UpArrowClass + 'href=\"' + up + '\"></a> <a ' + DownArrowClass + 'href=\"' + down + '\"></a></td><td><a href=\"#Print-' + n + '\">Print</a></td><td><a href=\"#Remove\-' + n + '\">Remove</a></td></tr>\n';
				if(!className)
					className=' class=\"on\"';
				else
					className=0;
			}

			Table += '\n\t\t<tr class="last_row"><td>Total pages ' + n + '</td><!--<td></td>--><td></td><td><a href=\"#Print-\*\">Print all</a></td><td><a href=\"#RemoveAll-*\">Remove all</a></td></tr>\n\t</tbody>\n</table><form><div class="printBasket_options"><input type="checkbox" value="true" id="pageBreak" /><label>Insert page break between sections</label></div></form></div>';
			
			return Table;
		}
		return printBasket.Msg.empty/* execute prior to assining to return*/;
	};
	
	
	/*- Basket Table
	--------------------------------------------*/
	printBasket.Table.Update = function() {
		if($$(printBasket.id))
			$$(printBasket.id).innerHTML = printBasket.Table/*execute table before assigning to DOM*/();
			return true;
	};
	
	
	/*- Basket Table
	--------------------------------------------*/	
	printBasket.Cookie = function() {
		return ture;
	};
	
	
	/*- Basket Cookie Update
	--------------------------------------------*/	
	printBasket.Cookie.Update = function(Titles,URLs,Categories) {
		Title = Titles.join("_-");
		URL = URLs.join("_-");
		Category = Categories.join("_-");
		printBasket.Add.Cookie(printBasket.CookieTitles, Title);
		printBasket.Add.Cookie(printBasket.CookieURLs, URL);
		printBasket.Add.Cookie(printBasket.CookieCategories, Category);
		return true;
	};
	
	
	/*- Basket Msg
	--------------------------------------------*/	
	printBasket.Msg = {
		empty: '<p>Basket is empty now</p>',
		confirmRemoveAll: 'Are you sure you want remove all pages?'
	};
	
	
	/*- Basket Write
	--------------------------------------------*/	
	printBasket.Write = function(c) {
		var content = printBasket.Main(c);
		return document.write(content);
	};

	/*- Basket Update
	--------------------------------------------*/	
	printBasket.Update = function(c) {
		if($$(printBasket.button_id)){
			$$(printBasket.button_id).innerHTML = printBasket.Button();
		}
		if($$(printBasket.total_id)){
			$$(printBasket.total_id).innerHTML = printBasket.Total();
		}
		printBasket.Table.Update();
	};

	/*- Basket Button
	--------------------------------------------*/	
	printBasket.Button = function(c) {
		if(printBasket.Unique() == null && !$$(printBasket.button_id)) {
			return '<span id="' + printBasket.button_id + '"><a href=\"#Add\">' + printBasket.addButtonText + '</a></span>'
		}
		if(printBasket.Unique() != null && !$$(printBasket.button_id)) {
			return '<span id="' + printBasket.button_id + '"><a href=\"#Remove-' + printBasket.Unique() + '\">' + printBasket.removeButtonText + '</a></span>'
		}
		if(printBasket.Unique() != null && $$(printBasket.button_id)) {
			return '<a href=\"#Remove-' + printBasket.Unique() + '\">' + printBasket.removeButtonText + '</a>'
		}
		if(printBasket.Unique() == null && $$(printBasket.button_id)) {
			return '<a href=\"#Add\">' + printBasket.addButtonText + '</a>'
		}
		
		/*<img id=\"printBasket_printer_add\" src=\"' + printBasket.ImgAddPrinter + '\" /> <a href=\"#Add\">     <img id=\"printBasket_printer_remove\" src=\"' + printBasket.ImgRemovePrinter + '\" />*/
		
		/*
		if(printBasket.Unique() == null || $$('printBasket_printer_add')) {
			if(!$$(printBasket.button_id) || c == 'undefined') {
				return '<span id="' + printBasket.button_id + '"><img id=\"printBasket_printer_add\" src=\"' + printBasket.ImgAddPrinter + '\" /> <a href=\"#Add\">Add this page</a></span>'
			} else if($$('printBasket_printer_add')){
				return '<img id=\"printBasket_printer_remove\" src=\"' + printBasket.ImgRemovePrinter + '\" /> <a href=\"#Remove-' + (printBasket.Total()-1) + '\">Remove this page</a>'
			} else {
				return '<img id=\"printBasket_printer_add\" src=\"' + printBasket.ImgAddPrinter + '\" /> <a href=\"#Add\">Add this page</a>'
			}
		} else {
			return '<img id=\"printBasket_printer_remove\" src=\"' + printBasket.ImgRemovePrinter + '\" /> <a href=\"#Remove-' + printBasket.Unique() + '\">Remove this page</a>'
		}*/
	};
	
	
	/*- Basket Total
	--------------------------------------------*/	
	printBasket.Total = function(c) {
		if(!$$(printBasket.total_id)) {
			var a = printBasket.Read(printBasket.CookieURLs);
			a = a ? a.length : 0;	
			return '<span id="' + printBasket.total_id + '">' + a + '</span>';
		};
		return printBasket.Read(printBasket.CookieURLs) ? printBasket.Read(printBasket.CookieURLs).length : 0;
	};
	
	
	/*- Basket Unique
	--------------------------------------------*/	
	printBasket.Unique = function(){
		var a = printBasket.Read(printBasket.CookieURLs) ? printBasket.Read(printBasket.CookieURLs) : 0 ;
		var b = printBasket.URL();
		for(n=0;n<a.length;n++) {
			if(a[n] == b) {
				return n;
			}
		}
		return null;
	};
	
	
	/*- Basket Print
	--------------------------------------------*/	
	printBasket.Print = function(n){
		
		var printBasketWindow = window.open();
		var printDoc = printBasketWindow.document;
		printDoc.write('<html><head></head><body style="overflow:hidden"><div><\/div><div style=\"width:100%;height:' + window.screen.availHeight + 'px;z-index:100;top:0;left:0;opacity:0.9;filter:alpha(opacity=90);position:absolute;background-color:#000;" id=\"' + printBasket.PrintContentDivId + '\"><\/div><\/body><\/html>');
		if (printDoc.createStyleSheet){
		    printDoc.createStyleSheet(printBasket.CssSrc);
			printDoc.createStyleSheet(printBasket.PrintCssSrc);
			printDoc.getElementsByTagName('link')[1].setAttribute('media', 'Print');
			
		} else {
			var css = document.createElement('link'); 
			css.setAttribute('rel', 'stylesheet'); 
			css.setAttribute('href', printBasket.CssSrc); 
			printDoc.getElementsByTagName('head')[0].appendChild(css);
			var css = document.createElement('link'); 
			css.setAttribute('rel', 'stylesheet');
			css.setAttribute('media', 'Print');
			css.setAttribute('href', printBasket.PrintCssSrc); 
			printDoc.getElementsByTagName('head')[0].appendChild(css);
		}	
		
		printDoc.getElementById(printBasket.PrintContentDivId).innerHTML = '<span id="loading" style="position:absolute;width: 260px; height: 95px;text-align:center;color:#fff;font-size:100%;"><img src="images/wait.gif" \/><br /><br /><span id="loadingMsg">Please wait...</span></span>';
		
		var imgWait = printDoc.getElementById(printBasket.PrintContentDivId).getElementsByTagName('img')[0];
		var spanWait = printDoc.getElementById(printBasket.PrintContentDivId).getElementsByTagName('span')[0];
		spanWait.style.left = ((window.screen.availWidth - parseInt(spanWait.style.width))/2) + 'px';
		spanWait.style.top = (((window.screen.availHeight - parseInt(spanWait.style.height))/2)-140) + 'px';
		var ele = document.createElement('div'), divN = 0;
		var URLs = printBasket.Read(printBasket.CookieURLs);
		ele.innerHTML = printBasket.LoadHTML(printBasket.Read(printBasket.CookieURLs)[0]);
		
		for(i=0;i<ele.getElementsByTagName('div').length;i++) {
			if(ele.getElementsByTagName('div')[i].id == printBasket.ContentDivId){
				divN = i;
				break;
			}
		}
		var printBreak = $$('pageBreak').checked ? 'style="page-break-after:always"' : '';
		if(n == '*' || n == '#') {
			for(i=0;i<URLs.length;i++) {
				ele.innerHTML = printBasket.LoadHTML(URLs[i]);
				printDoc.getElementsByTagName('div')[0].innerHTML = printDoc.getElementsByTagName('div')[0].innerHTML + '\n<div ' + printBreak + 'class=\"' + printBasket.PrintClass + '\">' + ele.getElementsByTagName('div')[divN].innerHTML + '<\/div>';
			printDoc.getElementById('loadingMsg').innerHTML = 'Please wait. Loading page...' + (i+1) + ' of ' + (URLs.length) + '.';
				printDoc.close();
			}printDoc.getElementById(printBasket.PrintContentDivId).style.display = 'none';
		} else {
			ele.innerHTML = printBasket.LoadHTML(URLs[n]);
			printDoc.getElementsByTagName('div')[0].innerHTML = printDoc.getElementsByTagName('div')[0].innerHTML + '<div class=\"' + printBasket.PrintClass + '\">' + ele.getElementsByTagName('div')[divN].innerHTML + '<\/div>';
			printDoc.close();
		}
		
		printDoc.getElementsByTagName('body')[0].style.overflow = '';		
		setTimeout("printBasket.WindowPrint()", 150);
		
		printBasket.WindowPrint = function() {
			/*setTimeout("printBasket.clearShadow()", 150);*/
			printBasketWindow.print();			
		};

		printBasket.clearShadow = function() {
			printDoc.getElementById(printBasket.PrintContentDivId).style.display = 'none';
		};
	};

})();

// Expose
printBasket.Up = printBasket.ChangeOrder.Up;
printBasket.Down = printBasket.ChangeOrder.Down;


/* Cookie Engine */
(function(){
	if(typeof cookieEngine=="undefined"|| !cookieEngine){
		var cookieEngine = window.cookieEngine = {};
	};
	// name - name of the cookie
	// value - value of the cookie
	// [expires] - expiration date of the cookie
	// (defaults to end of current session)
	// [path] - path for which the cookie is valid
	// (defaults to path of calling document)
	// [domain] - domain for which the cookie is valid
	// (defaults to domain of calling document)
	// [secure] - Boolean value indicating if
	// the cookie transmission requires a secure transmission
	// * an argument defaults when it is assigned null as a placeholder
	// * a null placeholder is not required for trailing omitted arguments
	cookieEngine.setCookie = function(name, value, expires, path, domain, secure, caution) {
		var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
		if (!caution || (name + "=" + escape(value)).length <= 4000) {
			document.cookie = curCookie;
		} else if (confirm("Cookie exceeds 4KB and will be cut\!")) {
			document.cookie = curCookie;
		}
	};

	// name - name of the cookie
	// * return string containing value
	// of specified cookie or null if cookie
	// does not exist
	cookieEngine.getCookie = function(name) {
		var prefix = name + "=";
		var cookieStartIndex = document.cookie.indexOf(prefix);
		if (cookieStartIndex == -1)
		 return null;
		var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1)
		 cookieEndIndex = document.cookie.length;
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	};

	// name - name of the cookie
	// [path] - path of the cookie
	// (must be same as path used to create cookie)
	// [domain] - domain of the cookie
	// (must be same as domain used to create cookie)
	// * path and domain default if assigned
	// null or omitted if no explicit argument proceeds
	cookieEngine.deleteCookie = function(name, path, domain) {
		if (cookieEngine.getCookie(name)) {
			document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	};

	// date - any instance of the Date object
	// * you should hand all instances of the
	// Date object to this function for "repairs"
	// * this function is taken from
	// Chapter 14, "Time and Date in JavaScript", in
	// "Learn Advanced JavaScript Programming"
	cookieEngine.fixDate = function(date) {
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0)
			date.setTime(date.getTime() - skew);
	};
	
	cookieEngine.cookieDate = function(days) {
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + days * 24 * 60 * 60 * 1000);
		return now;
	};

				
})();

// EXPOSE
printBasket.Read.Cookie = cookieEngine.getCookie;
printBasket.Add.Cookie = cookieEngine.setCookie;
printBasket.Remove.Cookie = cookieEngine.deleteCookie;
printBasket.Expire = cookieEngine.cookieDate;

	




/*------------------------------
	1. Cookie Object needs to be joint
	2. Cookie Limit needs to be tested.  


a[0] = printBasket.Category();//'Investors';
a[1] = 'Big Yellow - Reports';//printBasket.Title();
a[2] = '';


printBasket.Add(printBasket.CookieTitles, a[1]);
printBasket.Add(printBasket.CookieURLs, a[2]);
printBasket.Add(printBasket.CookieCategories, a[0]);
var a = new Array();

a[0] = 'Finance';
a[1] = 'Big Yellow - History';//printBasket.Title();
a[2] = 'http://localhost/ir/printBasket/report/interim2008/ar.jsp?page=financial-statement';


printBasket.Add(printBasket.CookieTitles, a[1]);
printBasket.Add(printBasket.CookieURLs, a[2]);
printBasket.Add(printBasket.CookieCategories, a[0]);
*/

