/**
 * COI - London Elect Javascript Object
 * Custom JS object to manipulate specific DOM elements (assisted by jQuery) 
 *
 * @author 		Start Creative, 2007
 * @copyright 	London Elects, 2007
 * @creator 	Paul Campbell <paulc@startcreative.co.uk>
 * @creator 	Matthew Lane <matthewl@startcreative.co.uk>
 */
var COI = {
	/**
	 * @function extend();
	 *
	 * Appends new object functionality (extends) 
	 */
	extend: function(a, b) {
		var id = new String(a).toUpperCase();
		if(a && b && !this[id]) {
			this[id] = b;
			return(this[id]);
		}
	},
	/**
	 * @function ieDebug();
	 *
	 * Debugs jquery stuff in the pitiful IE
	 */
	ieDebug: function(e) {
		if($.browser.msie) {
			if(typeof(e) != "object") {
				alert(e);
			} else {
				for(var i in e) {
					this.ieDebug(i + " : " + e[i]);
				}
			}
		}
	},
	/**
	 * @function printerFriendly();
	 *
	 * Apply print-the-page call to link
	 */
	printerFriendly: function() {
		$("#sharing-tools ul").append(
			'<li class="last"><a id="print-page" href="#">Print this page</a></li>'
		);
		$("#print-page").click(function() {
			window.print();
			return(false);
		})
	},
	/**
	 * @function applyFontResizer();
	 *
	 * Applies the required font method to the click handler
	 */
	applyFontResizer: function() {
		$("#lang-text").append(
			'<div id="text-size"><ol><li>Text size:</li>'
			+ '<li><a id="small" href="#" tabindex="3"><span class="hidden">small</span></a></li>'
			+ '<li><a id="medium" href="#" tabindex="4"><span class="hidden">medium</span></a></li>'
			+ '<li><a id="large" href="#" tabindex="5"><span class="hidden">large</span></a></li>'
			+ '</ol></div>'
		);
		$("#small, #medium, #large").bind("click", function() {
			COI.changeFontSize($(this).attr("id"));
			return(false);
		});
		COI.changeFontSize('#small');
	},
	/**
	 * @function changeFontSize();
	 *
	 * Managers the alternate and standard stylesheets, switching to the requested one
	 */
	changeFontSize: function(size) {
		//alert(size);
		/*if($(this) == '#medium')	{
			$('head > link:last').append('<link rel="alternate stylesheet" type="text/css" media="screen" title="large" href="/templates/default/css/medium.css" />');
		} else if($(this) == '#large') {
			$('head > link:last').append('<link rel="alternate stylesheet" type="text/css" media="screen" title="large" href="/templates/default/css/large.css" />');
		} else if($(this) == 'small') {
			$('head > link:last').append('<link rel="alternate stylesheet" type="text/css" media="screen" title="large" href="/templates/default/css/large.css" />');
		};*/
		
		$("link[@rel*=alternate]").each(function() {
			var el = $(this);
			if(el.attr("title") == size) {
				this.disabled = false;
			} else {
				this.disabled = true;
			}
		});
	},
	/**
	 * @function applyRSSLink();
	 *
	 * Applies the RSS link ref. Have to use JS as Immediacy blocks hardcoded efforts.
	 */
	applyRSSFeed: function() {
		$("head").append('<link href="/feeds/news.html" rel="alternate" type="application/rss+xml" title="London Elects 2008 News" />')
	},
	/**
	 * @function splitNavigation();
	 *
	 * Splits the main navigation element into two. Applying a <ul> seperator at
	 * the floor value between the total items in the <ul>
	 */
	splitNavigation: function() {
		var n = $("#global-navigation");
		var li = n.find("li");
		var split = Math.floor(li.length / 2 + (li.length % 2));
		var cut = n.children("ul").after('<ul class="column"><!-- null --></ul>').siblings().eq(0);
		for(var i = split; i<li.length; i++) {
			cut.append(li.eq(i).clone());
			li.eq(i).remove();
			//COI.ieDebug(n.html());
		}
	},
	/**
	 * @function applyReadMore();
	 *
	 * Adds read more to supplied link and target area
	 */
	applyReadMore: function(l,t,r) {
		var txt = $(l).html()
		$(l).click(function() {
			var el = $(t);
			if(el.attr("style").indexOf("none") > -1) {
				el.fadeIn();
				$(this).html(r);
			} else {
				el.fadeOut();
				$(this).html(txt);
			}
			return(false);
		});
		$(t).hide();
	},
	/**
	 * @function applyCurrentNavigationClass();
	 *
	 * Applies current style to specific link in global navigation area
	 */
	applyCurrentNavigationClass: function() {
		var url = new String(window.location.href);
		var cur = url.split("/")[3];
		$("#global-navigation a[@href*=" + cur + "]").eq(0).addClass("current");
	},
	/**
	 * @function applyFooterClass();
	 *
	 * Applies last class to final footer LI elements
	 */
	applyFooterClass: function() {
		$("#footer ul li:last-child").addClass("last");
	},
	/**
	 * @function searchFormHack();
	 *
	 * 
	 */
	searchFormHack: function() {
		var inp = $("#keywords");
		inp.keypress(function(e) {
			if(e.keyCode == 13) {
				$("#Template_GoSearch").click();
				return(false);
			}
		});
	},
	/**
	 * @function searchEmailFormHack();
	 *
	 * 
	 */
	searchPCFormHack: function() {
		var inp = $("#pcSearch");
		inp.keypress(function(e) {
			if(e.keyCode == 13) {
				$("#Template_pcSearch").click();
				return(false);
			}
		});
	},
	/**
	 * @function searchPostCodeHubFormHack();
	 *
	 * 
	 */
	searchPCHubFormHack: function() {
		var inp = $("#pcSearchHub");
		inp.keypress(function(e) {
			if(e.keyCode == 13) {
				$("#Template_pcSearchHub").click();
				return(false);
			}
		});
	},
	/**
	 * @function hidePDFFromMac();
	 *
	 * Hides PDF link from all mac-based browsers
	 */
	hidePDFFromMac: function() {
		if(navigator.appVersion.indexOf("Mac") > -1) {
			$("#pdf-link").hide();
		}
	},
	/**
	 * @function applyFactTicker();
	 *
	 * Repeats through relevant child tags within the answers element
	 */
	applyFactTicker: function() {
		var init = false;
		var count = 0;
		var toggle = function(e) {
			if($.browser.safari) {
				inter = setTimeout(toggle, 5000);
			}
			var el = $("#answers");
			if(el) {
				var n = el.children("p:eq(" + count + ")");
				var p = n.parent().parent();
				n.fadeIn("slow");
				p.height(p.css("height"));
				el.children("p:not(:eq(" + count++ + "))").hide();
				if(count >= el.children("p").length) {
					count = 0;
				}
			} else {
				clearInterval(inter);
				clearTimeout(inter);
			}
		}
		toggle();
		if($.browser.safari) {
			var inter = setTimeout(toggle, 5000);
		} else {
			var inter = setInterval(toggle, 5000);
		}
	},
	pixelToNumber: function(v) {
		if(v) {
			var l = v.split("px")[0];
			return(new Number(l));
		}
		return(0);
	},
	abbbrShorten: function(){

		$("abbr").each(function(){
			
			var str = $(this).text();
			$(this).text(jQuery.trim(str));
			
			//alert("Number of spans: " + $(".abbr", this).length)
			
		});	
	},
	abbrShortenIEFix: function(){
//		alert($("span.abbr:eq(0)").html())
		//alert($("abbr").length);
		c= $("span.abbr").length;
		for(var i = 0; i < c; i++){
			rplStr = $("span.abbr:eq("+i+")").html();
			//alert(rplStr);
			//alert($("abbr:eq(0)").html());
			//alert($("span.abbr:eq("+i+")").html())
		}
/*		$("span.abbr").each(function(){
			var el = $(this).html();
			alert(el);
		});*/
	}
}

// Bulk load call
$(document).ready(function() {

	COI.applyRSSFeed();
	COI.splitNavigation();
	COI.applyCurrentNavigationClass();
	COI.applyFooterClass();
	COI.searchFormHack();
	COI.searchPCFormHack();
	COI.searchPCHubFormHack();
	COI.applyFactTicker();
	COI.applyFontResizer();
	COI.printerFriendly();
	COI.hidePDFFromMac();
	COI.abbbrShorten();
	COI.abbrShortenIEFix();
	// Jquery plugins

	$("#ctl00_keywords").defaultvalue("Keywords");
	$("#pcSearch").defaultvalue("Your postcode");
});
