if (typeof Assanka.$ == 'undefined') throw('Incorrect Javascript load order - load jQuery first');

Assanka.$(function() {

	var inprog = Assanka.$("#avmarketschat").length;
	if (inprog) Assanka.$("#avmarketschat").get(0).scrollTop = 999999;

	// If emoticons exist, activate them
	Assanka.$("#avemoticons img").click(function(e) {
		var img = e.target;
		if (img.title) {
			Assanka.$("#txtmsg").get(0).value += img.title;
		} else {
			Assanka.$("#txtmsg").get(0).value += "{"+img.src.substring((img.src.lastIndexOf("/")+1), img.src.lastIndexOf("."))+"}";
		}
		Assanka.$("#txtmsg").get(0).focus();
	});

	// Activate Inferno form opening and closing buttons
	Assanka.$('#infernoformclose').click(function(){
		Assanka.$("#infernoshowform").show();
		Assanka.$("#infernohideform").hide();
		Assanka.$("#inferno-input").hide();
		Assanka.ftlive.resizeInferno();
	});
	Assanka.$('#infernoformopen').click(function() {
		Assanka.$("#infernoshowform").hide();
		Assanka.$("#infernohideform").show();
		Assanka.$("#inferno-input").show();
		Assanka.ftlive.resizeInferno();
	});

	Assanka.ftlive.initHintOnBlurElements();

	// Send message on keypress
	Assanka.$("#txtmsg").keypress(function(e) { if (Assanka.$("#chksendonenter").get(0).checked && e.which==13) Assanka.ftlive.sendMsg(); });
	Assanka.$('#btnsend').click(Assanka.ftlive.sendMsg);

	// Set up scrolling event handlers
	Assanka.$("#inferno-commentlist, #avmarketschat").scroll(Assanka.ftlive.scroller.onUserScroll);

	// Define the Meteor channel and connect
	if (typeof Meteor != "undefined" && Meteor.isSupportedBrowser() && (typeof Assanka.ftlive.channels == 'object' && Object.prototype.toString.apply(Assanka.ftlive.channels) === '[object Array]')) {

		// Set Meteor's client hostid
		if (Assanka.$.cookie('MeteorHostId')) {
			Meteor.hostid = Assanka.$.cookie('MeteorHostId');
		} else {
			Meteor.hostid = Assanka.ftlive.now(true)+""+Math.floor(Math.random()*1000000);
			Assanka.$.cookie('MeteorHostId', Meteor.hostid, {path:'/'});
		}

		var serverid = (Assanka.ftlive && Assanka.ftlive.meteorserverid) ? Assanka.ftlive.meteorserverid : Math.floor(Math.random()*5)+1;
		Meteor.host = "data"+serverid+"."+Assanka.ftlive.meteorhost;

		Meteor.registerEventCallback("process", Assanka.ftlive.onmeteor.receive);
		if (inprog) Meteor.registerEventCallback("statuschanged", function(s) {
			if (s==5 && !Assanka.$('#connectmsg').length) {
				Assanka.ftlive.scroller.appendAndScroll("chat", "<div id='connectmsg' class='sysmsg'><div>Welcome to Markets Live.  New messages will appear here as they are posted.</div></div>");
			}
		});

		// Sync local and remote clocks if the user is a participant (to ensure block countdowns are accurate)
		if (Assanka.$('#avparticipantsinterface').length) {
			Assanka.$.get("/marketslive/lib/ajx/gettime?nocache="+Assanka.ftlive.now(), function(response) {
				var t = Assanka.ftlive.now();
				if (!isNaN(response)) Assanka.ftlive.servertimeoffset = response-t;
			});
		}

		// Subscribe to the channels
		for (var i=0; i<Assanka.ftlive.channels.length; i++) Meteor.joinChannel(Assanka.ftlive.channels[i], 5);

		if (location.search.indexOf("debug") != -1) Meteor.debugmode=true;
		setTimeout("Meteor.connect()", 2500);
		Assanka.ftlive.blockTick();
	}

	// Set up inferno event hooks and complete the rest of the setup after Inferno has loaded
	if (typeof Inferno != 'undefined' && inprog) {
		Assanka.ftlive.scroller.chat.obj = Assanka.$("#avmarketschat");
		InfernoConfig.hooks = InfernoConfig.hooks || {};
		InfernoConfig.hooks.load = function() {
			Assanka.ftlive.scroller.inferno.obj = Assanka.$("#inferno-commentlist");
			Assanka.ftlive.resizeChat();
			Assanka.ftlive.resizeInferno();
			Assanka.$('#avmarketsliveloader').remove();
			Assanka.$('#avmarketslivepage').css('visibility','visible');
			Assanka.$('#avmarketsliveinferno').css('visibility','visible');
			Assanka.$('#avmarketslivefooter').css('visibility','visible');
			Assanka.ftlive.scroller.scroll('inferno');
			Assanka.$("#inferno-commentlist, #avmarketschat").scroll(Assanka.ftlive.scroller.onUserScroll);
			Inferno.addFunctionHook(function(content, contid, itemid, mode, sortindex) {
				if (contid=='inferno-commentlist') Assanka.ftlive.scroller.preContentChange('inferno');
			}, 'preDOMChange');
			Inferno.addFunctionHook(function(content, contid, itemid, mode, sortindex) {
				Assanka.ftlive.scroller.postContentChange('inferno')
			}, 'postDOMChange');
		};
		InfernoConfig.hooks.newcomment = function() {
			if (typeof SiSendAdditionalTracer == 'function') SiSendAdditionalTracer("/si/comment", "");
		};
		Inferno.init();
	}
});

Assanka.$(window).resize(function() {
	Assanka.ftlive.resizeChat();
	Assanka.ftlive.resizeInferno();
});


Assanka.ftlive = Assanka.$.extend(Assanka.ftlive, {

	participants: {},
	servertimeoffset: 0,
	prepubmsgs: [],
	lastmsgid: 0,
	pagehost: location.hostname,
	startrefreshtimer: null,
	
	sendMsg: function() {
		var msg = Assanka.$("#txtmsg").val();
		if (msg.length == 0) return false;
		if (msg.length > 4096) {
			alert("Message too long.  Please restrict messages to 4096 characters.");
			return false;
		}

		// Strip all spans, as they may carry nasty MS Word formatting
		var tempstring = "";
		while (tempstring.toString() != msg.toString()) {
			tempstring = msg;
			msg = msg.replace(/<span(.*?)>(.*?)<\/span>/i, "$2");
		}
		if (Assanka.$("#chkmsgasquote").get(0).checked) {
			msg = "<span class=\"quote\"><span>"+msg+"</span></span>";
			Assanka.$("#chkmsgasquote").get(0).checked = false;
		}
		Assanka.$("#btnsend").get(0).disabled = true;
		Assanka.$("#txtmsg").get(0).blur();
		Assanka.$("#txtmsg").get(0).disabled = true;
		Assanka.$.post("/marketslive/lib/ajx/sendmsg", {msg:encodeURIComponent(msg)}, function(resp) {
			Assanka.$("#txtmsg").get(0).disabled = false;
			Assanka.$("#btnsend").get(0).disabled = false;
			if (resp != '1' && resp.length < 200) alert(resp);
			else Assanka.$("#txtmsg").val("").get(0).focus();
		});
		return false;
	},

	onmeteor: {
		receive: function(msg) {
			if (msg.indexOf('%7Bhf%3A%27') != -1 || msg.indexOf('%7B%27hf%3A%27') != -1 || msg.indexOf('%7B%27hf%27') != -1) msg = decodeURIComponent(msg);
			eval("var data = "+msg);
			if (data.hf == 'inferno') data.hf = 'Assanka.ftlive.onmeteor.inferno';
			if (data.hf == 'mlstart') data.hf = 'Assanka.ftlive.onmeteor.start';
			if (data.hf == 'mlend') data.hf = 'Assanka.ftlive.onmeteor.end';
			eval(data.hf+"(data)");
		},
			
		msg: function(data) {
			if (data.mid <= Assanka.ftlive.lastmsgid || Assanka.$("#msg_" + data.mid).length) return false;
			Assanka.ftlive.lastmsgid = data.mid;
			var par = Assanka.ftlive.participants[data.author];
			if (data.author && typeof(Assanka.ftlive.participants[data.author]) == "undefined") {
				Assanka.$.getJSON("/marketslive/lib/ajx/participantdetails?q="+data.author+"&sessionid="+Assanka.ftlive.sessionid, function(response) {
					Assanka.ftlive.participants[data.author] = response;
					if (!Assanka.$("#par"+data.author).length) {
						Assanka.$("#parsempty").remove();
						Assanka.$("#pars").append("<span class='par' id='par"+data.author+"'><span class='parcol_"+response.col+"'>"+response.initials+"</span> "+response.pseudonym+" &nbsp;</span>");
						Assanka.ftlive.publishMessage(data);
					}
				});
			} else {
				Assanka.ftlive.publishMessage(data);
			}
		},
		
		block: function(data) {
			if (Assanka.$("#msg_"+data.msgblocked).length) {
				if (!Assanka.$("#msg_"+data.msgblocked).hasClass("blocked")) {
					Assanka.$("#msg_"+data.msgblocked).addClass("blocked");
					Assanka.$("#msg_"+data.msgblocked).append(" (blocked by "+data.blockedby+")");
					
					// Rearrange to cope with block quotes
					Assanka.$("#msg_"+data.msgblocked).html(Assanka.$("#msg_"+data.msgblocked).html().replace(/\s*(<\/span><\/span>)?\s*(<\/div>)?\s*\(blocked by (.*?)\)/im, " (blocked by $3)$2$1"));
					if (Assanka.$("#blocklink_"+data.msgblocked).length) Assanka.$("#blocklink_"+data.msgblocked).remove();
				}
			}
		},
		start: function() {
			if (!Assanka.$("#avmarketschat").length) {
				Assanka.ftlive.startrefreshtimer = setTimeout(function() {
					var ispart = false;
					if (top.location.search) {
						if (top.location.search.indexOf('participant=') != -1) ispart = true;
					}
					top.location.href = 'http://'+Assanka.ftlive.pagehost+'/marketslive/?start=true&cb='+Assanka.ftlive.now()+(ispart?'&participant=true':'');
				}, 100);
			}
		},

		end: function(data) {
			clearTimeout(Assanka.ftlive.startrefreshtimer);
			if (Assanka.$("#avmarketschat").length) {
				Meteor.disconnect();
				var msg = '<div class="sysmsg" id="msgendchat"><div>This session of Markets Live has now closed';
				if (typeof data.guid != 'undefined' && data.guid) msg += ', and the <a href="'+data.guid+'">full transcript is available</a> to view';
				msg += '.</div></div>\n';
				if (!Assanka.$('#msgendchat').length) Assanka.ftlive.scroller.appendAndScroll('chat', msg);
			}
			Assanka.ftlive.toggleLiveTab(false);
		},

		readercount: function(data) {
			Assanka.$("#spnreadership").html(data.readers+" readers connected");
		},

		inferno: function(data){
			if (!Inferno) return false;
			if (data.mode == "remove") {
				Inferno.dom(false, 'inferno-item'+data.id, false, 'remove');
			} else if (!Assanka.$('#inferno-item'+data.id).length) {
				Inferno.dom(decodeURIComponent(data.html), "inferno-commentlist", 'inferno-item'+data.id, data.mode, data.sortindex);
				Inferno.showHideModOpts();
			}
		}
	},

	publishMessage: function(data) {
		var isprepub = (typeof data.emb != 'undefined' && data.emb > Assanka.ftlive.serverTime());
		try	{
			msg = decodeURIComponent(data.msg);
		} catch (err) {
			msg = "<em>[There was an error decoding this message]</em>";
		}
		msg = msg.replace(/\n/g, "<br />");

		var msghtml = '';
		msghtml += "<div class=\"";
		msghtml += (isprepub) ? 'prepub' : '';
		msghtml += " "+data.msgtype+"\" timestamp=\""+data.timestamp+"\" id=\"msg_" + data.mid + "\">";
		if (data.msgtype == 'normal' && typeof Assanka.ftlive.participants[data.author] != 'undefined') msghtml += "<span class=\"parcol_" + Assanka.ftlive.participants[data.author].col +"\">" + Assanka.ftlive.participants[data.author].initials + "</span>";
		msghtml += "<div>" + msg;
		msghtml += (isprepub) ? "<a class=\"blocklink\" id=\"blocklink_"+data.mid+"\" href=\"javascript:void(0)\" onclick=\"Assanka.ftlive.block("+data.mid+")\">Block</a>" : "";
		msghtml += "</div></div>";
		
		// Rearrange to cope with block quotes
		msghtml = msghtml.replace(/<\/span><\/span><a class=\"blocklink(.*?)<\/a>/i, "<a class=\"blocklink$1</a></span></span>");
		data.embdate = data.emb;
		Assanka.ftlive.prepubmsgs[Assanka.ftlive.prepubmsgs.length] = data;
		Assanka.ftlive.scroller.appendAndScroll('chat', msghtml);
	},
	
	toggleLiveTab: function(islive) {
		if (islive) {
			Assanka.$("#lnknavml").css({width:((Assanka.$("#lnknavml a").hasClass("on")) ? '7.25em' : '6.95em')}).addClass("live");
			var livetaburl = (Assanka.cdnhost?Assanka.cdnhost:'')+'/marketslive/lib/img/livetab.gif';
			Assanka.$("#lnkmarketslive").attr("href", "/marketslive/?inprogress=1").html("Markets<img src=\""+livetaburl+"\" /><i></i>");
		} else {
			Assanka.$("#lnknavml").css({width:'6.6em'}).removeClass("live");
			Assanka.$("#lnkmarketslive").attr("href", "/marketslive").html("Markets Live<i></i>");
		}
	},

	block: function(mid) {
		Assanka.$.get("/marketslive/lib/ajx/blockmsg?id="+mid);
		Assanka.$("#blocklink_"+mid).remove();
		Assanka.$("#txtmsg").val(Assanka.ftlive.revertFormatting(Assanka.$("#msg_"+mid).html()));
		Assanka.$("#msg_"+mid).addClass("blocked").get(0).innerHTML += " (blocked by me)";
		
		// Rearrange to cope with block quotes
		Assanka.$("#msg_"+mid).html(Assanka.$("#msg_"+mid).html().replace(/\s*(<\/span><\/span>)?\s*(<\/div>)?\s*\(blocked by (.*?)\)/im, " (blocked by $3)$2$1"));
	},
	
	blockTick: function() {
		var t = Assanka.ftlive.serverTime();
		var newlst = new Array(), curlst = Assanka.ftlive.prepubmsgs;
		Assanka.ftlive.scroller.disableScrollDetection('blocktick');
		for (var i=0; i<curlst.length; i++) {
			if (Assanka.$("#msg_"+curlst[i].mid).length && !Assanka.$("#msg_"+curlst[i].mid).hasClass("blocked")) {
				if (curlst[i].embdate < t) {
					Assanka.$("#msg_"+curlst[i].mid).removeClass("prepub");
					Assanka.$("#blocklink_"+curlst[i].mid).remove();
				} else {
					Assanka.$("#blocklink_"+curlst[i].mid).html("Block: "+Math.ceil(curlst[i].embdate-t));
					newlst[newlst.length] = curlst[i];
				}
			}
		}
		Assanka.ftlive.scroller.enableScrollDetection('blocktick');
		Assanka.ftlive.prepubmsgs = newlst;
		setTimeout(Assanka.ftlive.blockTick,500);
	},

	revertFormatting: function(htmlstring) {
		var tempstring = "";
		while (tempstring.toString() != htmlstring.toString()) {
			tempstring = htmlstring;
			htmlstring = htmlstring.replace(/<img(.*?)src="(.*?)\/lib\/img\/emoticons\/(.*?)\.(.*?)>/i, "{$3}");
		}
		htmlstring = htmlstring.replace(/<span(.*?)class=(.*?)quote(.*?)><span>(.*?)<\/span><\/span>/i, "$4");
		if (tempstring.toString() != htmlstring.toString()) {
			Assanka.$("#chkmsgasquote").get(0).checked = true;
		}
		htmlstring = htmlstring.replace(/^<span(.*?)class=\"?\'?parcol_[0-9]\"?\'?>[^<]*<\/span>/i, "");
		htmlstring = htmlstring.replace(/^\s*<div>\s*/im, "");
		htmlstring = htmlstring.replace(/\s*<\/div>\s*$/im, "");
		tempstring = "";
		while (tempstring.toString() != htmlstring.toString()) {
			tempstring = htmlstring;
			htmlstring = htmlstring.replace(/<br(.*?)>/i, "\n");
		}
		return htmlstring;
	},
	
	resizeChat: function() {
		if (Assanka.$('#avmarketssessionwrapper').length) {
			var viewportheight = Assanka.$(window).height();
			var nonchatheight = Assanka.$('#avmarketssessionwrapper').offset().top + Assanka.$('#avmarketslivefooter').outerHeight()+10;
			var workingarea = Math.max(viewportheight-nonchatheight, 700-nonchatheight);
			var paruiheight = Assanka.$("#avparticipantsinterface").outerHeight();
			Assanka.ftlive.scroller.disableScrollDetection('resizechat');
			Assanka.$('#avmarketssessionwrapper').height(workingarea-paruiheight);
			Assanka.ftlive.scroller.enableScrollDetection('resizechat');
		}
	},
	
	resizeInferno: function() {
		var win = Assanka.$("#inferno-commentlist");
		if (!Assanka.$('#ribbonad').height()) Assanka.$('#ribbonad').hide();
		if (win.length) {
			var viewportheight = Assanka.$(window).height();
			var nonchatheight = Assanka.$('#inferno-commentlist').offset().top + Assanka.$('#avmarketslivefooter').outerHeight()+6;
			var workingarea = Math.max(viewportheight-nonchatheight, 700-nonchatheight);
			var formopenercloser = 11;
			var useruiheight = formopenercloser + Assanka.$("#inferno-input:visible").outerHeight();
			Assanka.ftlive.scroller.disableScrollDetection('resizeinferno');
			win.height(workingarea-useruiheight);
			Assanka.ftlive.scroller.enableScrollDetection('resizeinferno');
		}
	},
	
	scroller: {
		inferno: { position:0, scrollpending:false, obj:null, itemtag:'li', timestampattr:'sortindex' },
		chat: { position:0, scrollpending:false, obj:null, itemtag:'div', timestampattr:'timestamp' },
		scrollblockers: {},                // Whether to react to onscroll events - only if empty
		scrolldetection: false,            // Whether to react to onscroll events
		scrolltimer: null,                 // Timer reference for scroll action
		animating: false,                  // Whether a scroll animation is in progress
		detecttimer: null,

		// Before making a change to the content of an auto-scrolling window, preContentChange should be called to check the current scroll position.  If the scroll position is near the bottom of the window, then the window will be scheduled to be scrolled after the content is added (regardless of the length of the new content).
		preContentChange: function(win) {
			var w = Assanka.ftlive.scroller[win].obj;
			if (!w) return;
			var pos = w.get(0).scrollTop+w.height();
			var max = w.get(0).scrollHeight;
			if (pos > (max-50)) Assanka.ftlive.scroller[win].scrollpending = true;
			Assanka.ftlive.scroller.disableScrollDetection('contentchange');
		},

		// After a content change to an auto-scrolling window, scroll the window to the bottom if a scroll is pending
		postContentChange: function(win) {
			if (Assanka.ftlive.scroller[win].scrollpending) Assanka.ftlive.scroller.scroll(win);
			Assanka.ftlive.scroller.enableScrollDetection('contentchange');
		},
		
		// Shortcut to add content to a window and then (if appropriate) scroll it
		appendAndScroll: function(win, content) {
			Assanka.ftlive.scroller.preContentChange(win);
			Assanka.ftlive.scroller[win].obj.append(content);
			Assanka.ftlive.scroller.postContentChange(win);
		},

		// Scroll a window, and prevent onscroll events from firing during the scroll
		scroll: function(win) {
			var s = Assanka.ftlive.scroller;
			s.disableScrollDetection('autoscroll');
			s[win].obj.get(0).scrollTop = 9999999;
			s[win].position = s[win].obj.get(0).scrollTop;
			s[win].scrollpending = false;
			s.enableScrollDetection('autoscroll');
		},

		// Fired when a user manually scrolls either the Markets Live or Inferno windows.  Also triggered by setting scrollTop in JavaScript, but autoscroll is set to false when manipulating scrollTop so that it does not cause cascading scrolls.
		onUserScroll: function(e) {
			var s = Assanka.ftlive.scroller;
			if (s.scrolltimer) clearTimeout(s.scrolltimer);
			if (Assanka.$('#chksyncscroll').get(0).checked && s.scrolldetection) {
				s.scrolltimer = setTimeout(function() {

					// If a scroll animation is currently in progress, stop it
					if (s.animating) {
						s.chat.obj.stop();
						s.inferno.obj.stop();
						Assanka.$('#avtimeslice').stop().css('opacity',0).hide();
						s.animating = false;
					}

					// Define which window has been scrolled, and set up a reference to the other one as well
					var scrolledWindow = (Assanka.$(e.target).attr("id") == "avmarketschat") ? 'chat' : 'inferno';
					var otherWindow = (scrolledWindow == "chat") ? 'inferno' : 'chat';
					scrolledWindow = s[scrolledWindow]; otherWindow = s[otherWindow];

					// Determine the offset from the top of the viewport to the vertical mid point of the chat windows, where the two windows should syncronise on the same timestamp
					var midPointOffset = (s.chat.obj.outerHeight()/2) + s.chat.obj.offset().top;

					// Determine which element, within the window that has been scrolled, is now over the mid point
					var midPointElement = null;
					scrolledWindow.obj.children(scrolledWindow.itemtag).each(function() {
						if (Assanka.$(this).offset().top > midPointOffset) return false;
						midPointElement = Assanka.$(this);
					});

					// Get the time index of the element in the middle of the window
					var ts = midPointElement.attr(scrolledWindow.timestampattr);

					// Find the element in the other window that has a time index closest to the one in the middle of the scrolled window.  Calculate how far into the content of the window the element occurs (eg it is 500px from the top of the window)
					var scrollos = 0;
					otherWindow.obj.children(otherWindow.itemtag).each(function() {
						if (Assanka.$(this).attr(otherWindow.timestampattr) > ts) return false;
						scrollos += Assanka.$(this).outerHeight();
					});

					// Deduct half the visible height of the chat window so that when treated as a scroll offset, it places the target element in the middle of the window (the two windows can have different heights due to the possible presence of the participant's interface, so always use the main ML chat box for height-of-window measurements, so that they line up horizontally)
					scrollos -= (s.chat.obj.outerHeight()/2);

					// Limit the offset to a minimum of zero and a maximum of the height of the content minus the visible height of the window
					scrollos = Math.round(Math.max(Math.min(scrollos,(otherWindow.obj.get(0).scrollHeight-otherWindow.obj.height())),0));

					// If the resulting offset is significantly different to where the window is scrolled to already, scroll it into the target position
					if (otherWindow.obj.get(0).scrollTop < (scrollos-50) || otherWindow.obj.get(0).scrollTop > (scrollos+50)) {
						s.disableScrollDetection('syncscroll');
						s.animating = true;
						otherWindow.obj.scrollTo(scrollos, 500, {onAfter:function(){
							midPointOffset -= Assanka.$("#marketslivesession").offset().top;
							s.enableScrollDetection('syncscroll');
							s.animating = false;
						}});
					}

				}, 300);
			}
		},

		disableScrollDetection: function(key) {
			Assanka.ftlive.scroller.scrollblockers[key] = 1;
			if (Assanka.ftlive.scroller.detecttimer) clearTimeout(Assanka.ftlive.scroller.detecttimer);
			Assanka.ftlive.scroller.scrolldetection=false;
		},
		enableScrollDetection: function(key) {
			delete Assanka.ftlive.scroller.scrollblockers[key];
			var numblockers = 0;
			if (Assanka.ftlive.scroller.detecttimer) clearTimeout(Assanka.ftlive.scroller.detecttimer);
			for (var i in Assanka.ftlive.scroller.scrollblockers) numblockers++;
			if (!numblockers) {
				Assanka.ftlive.scroller.detecttimer = setTimeout(function() { Assanka.ftlive.scroller.scrolldetection=true; }, 10);
			}
		}
	},
	now: function(tothemillisecond) {
		var timenow = new Date();
		return (tothemillisecond) ? timenow.getTime() : Math.round(timenow.getTime()/1000);
	},
	serverTime: function(tothemillisecond) {
		var timenow = new Date();
		var ret = (tothemillisecond) ? timenow.getTime() : Math.round(timenow.getTime()/1000);
		return ret + Assanka.ftlive.servertimeoffset;
	},
	initHintOnBlurElements: function() {
		Assanka.$("input.hintonblur").each(function(){
			if (Assanka.$(this).val() == Assanka.$(this).attr("alt"))
				Assanka.$(this).css("color", "#aaa");
		}).focus(function(){
			if (Assanka.$(this).val() == Assanka.$(this).attr("alt"))
				Assanka.$(this).val("").css("color", "black");
		}).blur(function(){
			if (Assanka.$(this).val() == Assanka.$(this).attr("alt") || !Assanka.$(this).val())
				Assanka.$(this).val(Assanka.$(this).attr("alt")).css("color", "#aaa");
		});
	}
});



