var saveField = "";
var restoreContent = [];
function initAjax()
	{
		DWRUtil.useLoadingMessage();
		DWREngine._errorHandler =  errorHandler;
	}
	
var $j = jQuery.noConflict();
jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
	};


function showLoading(a,b) { /* element,size */
	/*
	The Google compiler changes the arguments to A and B, which breaks 
	the literal use of "element" in the eval() function, so I just
	set them to A/B myself.
	*/
	eval('restoreContent["' + a + '"] = e(a).innerHTML;');
	e(a).innerHTML = '<img src="//s3.amazonaws.com/tweetmatix/Loading2.gif" height="' + b + ' align=top>';
	}
function restoreLoading(element) {
	e(element).innerHTML = eval('restoreContent["' + element + '"]');
	}
function loadKeywords(KeywordTypeCode) {
	DWREngine._execute(_cfscriptRinktime, null, 'loadTwitterKeywords', KeywordTypeCode, UserKey, handleLoading);
	}
function addKeyword(keyword,KeywordTypeCode) {
	var thisElement = KeywordTypeCode+'SaveButton';
	if (keyword.length == 0) return;
	showLoading(thisElement,22);
	DWREngine._execute(_cfscriptRinktime, null, 'addTwitterKeyword', UserKey, escape(keyword), KeywordTypeCode, handleResult);
	}
function deleteKeyword(keyword,KeywordTypeCode)
	{
		showLoading('delete_' + keyword,15);
		DWREngine._execute(_cfscriptRinktime, null, 'deleteTwitterKeyword', UserKey, escape(keyword), KeywordTypeCode, handleDelete);
	}
function handleLoading(result)
	{	
		var Success = -1;
		var Error = -1;
		var Content = -1;
		var Type = -1;

		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("CONTENT", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Content = i;
				continue;
				}
			matchValue = new RegExp("KEYWORDTYPECODE", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Type = i;
				continue;
				}
			}
		TypeCode = result[Type].VALUE.toUpperCase();
		if (result[Success].VALUE == 1) {
			/* alert('Keyword Created'); */
			e(TypeCode + 'Div').innerHTML = result[Content].VALUE;
			}
		if (Error != -1) { 
			displayMessage(result[Error].VALUE,TypeCode+'Messages');
			}
	}
function handleResult(result)
	{	
		var Success = -1;
		var Error = -1;
		var Content = -1;
		var Count = -1;
		var Count = -1;
		
		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("CONTENT", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Content = i;
				continue;
				}
			matchValue = new RegExp("KEYWORDCOUNT", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Count = i;
				continue;
				}
			matchValue = new RegExp("KEYWORDTYPECODE", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Type = i;
				continue;
				}
			}
		TypeCode = result[Type].VALUE.toUpperCase();
		if (result[Success].VALUE == 1) {
			/* alert('Keyword Created'); */
			if (result[Count].VALUE == 1) e(TypeCode + 'Div').innerHTML = result[Content].VALUE;
			else e(TypeCode + 'Div').innerHTML = e(TypeCode + 'Div').innerHTML + result[Content].VALUE;
			restoreLoading(TypeCode+'SaveButton');

			}
		if (Error != -1) { 
			restoreLoading(TypeCode+'SaveButton');
			displayMessage(result[Error].VALUE,TypeCode+'Messages');
			}
	}
function handleDelete(result)
	{	
		var Success = -1;
		var Error = -1;
		var Type = -1;
		
		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("KEYWORDTYPECODE", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Type = i;
				continue;
				}
			}
		TypeCode = result[Type].VALUE.toUpperCase();
		if (result[Success].VALUE == 1) {
			loadKeywords(TypeCode);
			}
		if (Error != -1) { 
			displayMessage(result[Error].VALUE,TypeCode+'Messages');
			}
	}
function togglePauseKeyword(keyword,KeywordTypeCode,pauseValue)
	{
		showLoading('pause_' + keyword,15);
		DWREngine._execute(_cfscriptRinktime, null, 'togglePauseKeyword', UserKey, escape(keyword), KeywordTypeCode, pauseValue, handlePause);
	}
function handlePause(result)
	{	
		var Success = -1;
		var Error = -1;
		var Type = -1;
		
		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("KEYWORDTYPECODE", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Type = i;
				continue;
				}
			}
		TypeCode = result[Type].VALUE.toUpperCase();
		if (result[Success].VALUE == 1) {
			loadKeywords(TypeCode);
			}
		if (Error != -1) { 
			displayMessage(result[Error].VALUE,TypeCode+'Messages');
			}
	}
function displayMessage(text,which) {
	e(which).innerHTML = text; 
	Effect.Appear(which);
	Effect.Pulsate(which,{ pulses: 2, duration: 1.0 });
	window.setTimeout(function() {Effect.Fade(which)}, 2000);
	}
function doUnFollow(user) {
	showLoading('unfollow_' + user,15);
	DWREngine._execute(_cfscriptRinktime, null, 'unFollow', UserKey, escape(user), handleUnFollow);
	}
function handleUnFollow(result)
	{	
		var Success = -1;
		var Error = -1;
		var User = -1;

		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("USER", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				User = i;
				continue;
				}
			}
		if (result[Success].VALUE == 1) {
			Effect.BlindUp(result[User].VALUE);
			/* displayMessage(result[User].VALUE + ' has been unfollowed.','MatchMessages'); */
			}
		if (Error != -1) { 
			e('unfollow_' + result[User].VALUE).style.display = 'none';
			displayMessage(result[Error].VALUE,'MatchMessages');
			}
	}
function loadRecentFollows(start) {
	e('RecentFollowsPagination').style.display = 'none';
	DWREngine._execute(_cfscriptRinktime, null, 'RecentFollows', UserKey, start, handleRecentFollowsResult);
	}
function handleRecentFollowsResult(result)
	{	
		var Success = -1;
		var Error = -1;
		var Content = -1;

		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("CONTENT", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Content = i;
				continue;
				}
			}
		/* Effect.BlindUp('RecentFollowsLoading'); */
		e('RecentFollows').style.display = 'none';
		
		if (result[Success].VALUE == 1) {
			e('RecentFollows').innerHTML = result[Content].VALUE;
			Effect.BlindDown('RecentFollows');
			Effect.BlindDown('RecentFollowsPagination');
			e('RecentFollowPage').innerHTML = parseInt(RecentFollowPosition) + 1;
			}
		if (Error != -1) { 
			alert(result[Error].VALUE);
			}
	}
var RecentFollowPosition = 0;
function updateRecentFollow(count) {
	RecentFollowPosition = RecentFollowPosition + count;
	if (RecentFollowPosition < 0) {RecentFollowPosition = 0; return;}
	/* Effect.BlindDown('RecentFollowsLoading'); */
	loadRecentFollows(RecentFollowPosition*5);
	}	
	
function loadRecentFollowBacks(start) {
	e('RecentFollowBackPagination').style.display = 'none';
	DWREngine._execute(_cfscriptRinktime, null, 'RecentFollowBacks', UserKey, start, handleRecentFollowbacksResult);
	}
function handleRecentFollowbacksResult(result)
	{	
		var Success = -1;
		var Error = -1;
		var Content = -1;

		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			matchValue = new RegExp("CONTENT", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Content = i;
				continue;
				}
			}
		/* Effect.BlindUp('RecentFollowsLoading'); */
		e('RecentFollowBacks').style.display = 'none';
		
		if (result[Success].VALUE == 1) {
			e('RecentFollowBacks').innerHTML = result[Content].VALUE;
			Effect.BlindDown('RecentFollowBacks');
			Effect.BlindDown('RecentFollowBackPagination');
			e('RecentFollowBacksPage').innerHTML = parseInt(RecentFollowBacksPosition) + 1;
			}
		if (Error != -1) { 
			alert(result[Error].VALUE);
			}
	}
var RecentFollowBacksPosition = 0;
function updateRecentFollowBacks(count) {
	RecentFollowBacksPosition = RecentFollowBacksPosition + count;
	if (RecentFollowBacksPosition < 0) {RecentFollowBacksPosition = 0; return;}
	/* Effect.BlindDown('RecentFollowsLoading'); */
	loadRecentFollowBacks(RecentFollowBacksPosition*5);
	}

function updateBoolean(thisValue) {
	document.forms['KeywordForm'].elements['keywordboolean'][thisValue].checked = true;
	
	if (thisValue == 1) {
		e('BooleanInclude').src = BooleanIncludeImage_on.src;
		e('BooleanExclude').src = BooleanExcludeImage_off.src;
		
		}
	else {
		e('BooleanInclude').src = BooleanIncludeImage_off.src;
		e('BooleanExclude').src = BooleanExcludeImage_on.src;
		}
	}
var doMultiple = 0;
var multiFields = '';

function saveMultiple(fields) {
	multiFields = fields.split(",");
	saveValue(multiFields[0]);
	}
function saveValue(which) {
	restoreContent = e('save-' + which).innerHTML;
	showLoading('save-' + which,15);
	saveField = which;
	DWREngine._execute(_cfscriptRinktime, null, 'update'+ which, UserKey, e(which).value, handleSaveResult);
	}
function handleSaveResult(result)
	{	
		var Success = -1;
		var Error = -1;
		var Field = -1;

		for ( var i=0, len=result.length; i<len; ++i ){
			matchValue = new RegExp("SUCCESS", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Success = i;
				continue;
				}
			matchValue = new RegExp("ERROR", "i");
			isMatch = result[i].KEY.match(matchValue);
			if (isMatch) {
				Error = i;
				continue;
				}
			}
		showResultIcon(saveField,result[Success].VALUE);
		setTimeout("e('save-' + saveField).innerHTML = restoreContent;",2000);
		if (multiFields.length > 0) {
			doMultiple++;
			if (doMultiple >= multiFields.length) {multiFields = '';doMultiple = 0; return;}
			e('save-' + saveField).innerHTML = restoreContent;
			saveValue(multiFields[doMultiple]);
			}
		else {
			setTimeout("e('save-' + saveField).innerHTML = restoreContent;",2000);
			}
	}
function showResultIcon(field,valid) {
	if (valid==1) {
		e('save-' + field).innerHTML = '<img src="//s3.amazonaws.com/tweetmatix/OK-mark.png" align="absmiddle"> <span style="font-size: 10px;font-weight:bold;color: green;">SAVED</span>';
		new Effect.Highlight(field,{startcolor:'#B2FF94'});
		}
	else {
		e('save-' + field).innerHTML = '<img src="//s3.amazonaws.com/tweetmatix/Alert-mark.png" align="absmiddle"> <span style="font-size: 10px;font-weight:bold;color: red;">NOT SAVED</span>';
		new Effect.Highlight(field,{startcolor:'#FF8E00'});
		}
	}
function checkReach() {
	if(e('Reach').value=='Local') {
		Effect.BlindDown('LocalDistanceDiv');
		//$('#LocalDistanceDiv').slideDown();
		}		
	else {
		Effect.BlindUp('LocalDistanceDiv');
		//$('#LocalDistanceDiv').slideUp();
		}
	}
function e(id) {return document.getElementById(id);}
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
	
/* Select element */
function getSelected(ob) { 
	selected = '';
	for (var i = 0; i < ob.options.length; i++) if (ob.options[ i ].selected) selected = selected + ',' + ob.options[ i ].value;
	return selected;
	}
	
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
// http://www.somacon.com/p143.php
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

$j(document).ready(function(){
	$j(".lightBox").colorbox({width:"80%", height:"80%", iframe:true});
	});

var LightBoxSRC = '';
function openLightBox(url,allowScroll) {
	LightBoxSRC = url;
	try {
		if (allowScroll) e('lightboxframe').style.overflow='auto';
		else e('lightboxframe').style.overflow='hidden';
		}
	catch(err) {
		}
	var verticaloffset = 50;
	var element = document.getElementById('lightbox');
	element.style.top =  verticaloffset + 'px';
	hideflash();
	//new Effect.BlindDown('lightbox');
	$j('#lightbox').slideDown();
	new Effect.toggle('lightboxfade', 'appear', {
		duration: 1.0, from: 0.0, to: 0.7,
		afterFinish: setFrameSRC
		});
}

function closeLightBox() {
	LightBoxSRC = '';
	new Effect.BlindUp('lightbox');
	new Effect.toggle('lightboxfade', 'appear', {
		duration: 1.0, from: 0.7, to: 0.0, 
		afterFinish: setFrameSRC
		});
	showflash();
}
function setFrameSRC() {
	e('lightboxframe').src = LightBoxSRC;
	}
function hideflash()
{
        /* hide all flash in the page */
        flash = document.getElementsByTagName('embed')
        for (var i = 0; i < flash.length; i++) 
        { 
                flash[i].style.visibility = 'hidden';
        }
}

function showflash()
{
        /* show all flash */
        flash = document.getElementsByTagName('embed')
        for (var i = 0; i < flash.length; i++) 
        { 
                flash[i].style.visibility = 'visible';
        }
}
function charCounter(c, a, d) {
   if(document.all) {
      c = c.replace(/\r/g,"")} var b=c.length;
      if(c.charAt(b - 1) == " ") {
         b--;
		 }
      if(b > 140) {
         $(d).addClassName("warn");
		 }
      else {
         $(d).removeClassName("warn");
		 }
      e(a).innerHTML = b;
      return false;
	  }
	  
function toggleReply(id) {
	new Effect.toggle('replyto_' + id,'slide');
	}
	
	
	
function replyTo(text,inReplyTo,key) {
	e('replytoactions_' + inReplyTo).style.display = "none";
	e('replytoresult_' + inReplyTo).style.display = "none";
	DWREngine._execute(_cfscriptRinktime, null, 'updateStatus', key, text, inReplyTo, handleReplyTo);
	}
function handleReplyTo(result) {	
	var Success = -1;
	var Error = -1;
	var inReplyTo = -1;

	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("INREPLYTO", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			inReplyTo = i;
			continue;
			}
		}
	resultDiv = 'replytoresult_' + result[inReplyTo].VALUE;
	actionsDiv = 'replytoactions_' + result[inReplyTo].VALUE;
	
	//new Effect.SlideDown(resultDiv);
	$j('#'+resultDiv).slideDown();
	
	if (result[Success].VALUE == 1) {
		$(resultDiv).removeClassName("warn");
		$(resultDiv).addClassName("replytoResult");
		e(resultDiv).innerHTML = "Your message has been sent!<br/>";
		toggleReply(result[inReplyTo].VALUE);
		}
	if (Error != -1) { 
		$(resultDiv).addClassName("warn");
		$(resultDiv).removeClassName("replytoResult");
		e(resultDiv).innerHTML = "There was an error sending your message.<br/>";
		}
	window.setTimeout("new Effect.SwitchOff('replytoresult_"+result[inReplyTo].VALUE+"');" , 4000);
	e(actionsDiv).style.display = "block";
	}
BooleanIncludeImage_on = new Image();
BooleanIncludeImage_on.src = '//s3.amazonaws.com/tweetmatix/icon_include_on.png';

BooleanIncludeImage_off = new Image();
BooleanIncludeImage_off.src = '//s3.amazonaws.com/tweetmatix/icon_include_off.png';

BooleanExcludeImage_on = new Image();
BooleanExcludeImage_on.src = '//s3.amazonaws.com/tweetmatix/icon_exclude_on.png';

BooleanExcludeImage_off = new Image();
BooleanExcludeImage_off.src = '//s3.amazonaws.com/tweetmatix/icon_exclude_off.png';
