function ps()
{
	this.baseurl = '';
	this.member_id = '0';
	this.defaultValues = Array();
	this.member_email = '';
	this.allow_form = false;
}

ps.prototype.login = function()
{
	var fields = {
	'email':  	$('#the_email').val(),
	'password': 	$('the_password').val(),
	'form_sent': 	1
	};
	
	var req = function(response)
	{
		if(response == 'VALID')
		{
			$('#the_login_form').html("<br /><br /><div class='bold green'>Thank you, now logging in.</div><br /><br />");
			setTimeout("ps.refresh();", 1000);
		}
		else
		{
			$('#login_error').html("<div class='bold red'>Invalid username/password, please try again!</div><br /><br />");
		}
		return false;
	};
	
	var url = this.baseurl+"index.php?act=login&ajax=1";
	$.post( url, ps.formatArray(fields), req );
	return false;
}

ps.prototype.refresh = function()
{
	var url = unescape(window.location.pathname);
	window.location = url;
}


ps.prototype.getPageScroll = function()
{

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



ps.prototype.getPageSize = function()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

ps.prototype.closePopup = function()
{
	try
	{
		var objLightbox = $('#js_o_popup');
		objLightbox.fadeOut('slow');
		objLightbox.hide();
		
		this.jsOverlay(false);
		
		if(typeof this.onPopupClose == 'function')
		{
			this.onPopupClose();
		}
	}
	catch(e)
	{
	}
}

ps.prototype.tweet = function(action, pid)
{
	var req = function(response)
	{
		alert(response);
		if(response == 'VALID')
		{
			$('#twitter_share').html("<div class='center small gray uppercase'>Petition Tweeted!</div>");
		}
		else
		{
			$('#twitter_share_error').html("<div class='red uppercase bold'>Error, please try again</div><br />");
		}
	};
	
	var username = $('#twitter_username').val();
	var password = $('#twitter_password').val();
	var url = this.baseurl+"index.php?act=twitter&code="+action+"&pid="+pid+"&username="+this.encode(username)+"&password="+this.encode(password);
	$.get( url, req );
	
	return false;
}


ps.prototype.popup = function(title, htmlz, width, height, overlayClose)
{
	
	try
	{
		var html = '';
		if(overlayClose)
		{
			html += "<img onclick='ps.closePopup();' src='"+this.baseurl+"static/images/close.png' alt='X' style='z-index:3;cursor: pointer;margin-top:-15px;margin-right:-15px;float:right;' />";
		}
		
		if(title != '' && title != null)
		{
			html += "<div class='bold blue large'>"+title+"</div><div class='spacer10'></div><div class='hr'></div><div class='spacer10'></div>";
		}
		
		html += "<div class='pad10'>"+htmlz+"</div><div class='clear'></div>";
		html = this.box(html);
	
		var objOverlay = $('#js_overlay');
		objOverlay.fadeIn('slow');
		
		if($('#js_o_popup'))
		{
			var objLightbox = $('#js_o_popup');
		}
		else
		{
			var objLightbox = document.createElement('div');
			objLightbox.id = 'js_o_popup';
			objLightbox.className = 'popup';
			$('#js_html').appendChild(objLightbox);
		}
		objLightbox.html(html);
		
		objLightbox.fadeIn('slow');
		
		objLightbox.css('position', 'fixed');
		objLightbox.css('z-index', '3');
		
		if(width != '')
		{
			objLightbox.width(width);
		}
		if(height != '')
		{
			objLightbox.height(height);
		}

		var arrayPageSize = this.getPageSize();
		var arrayPageScroll = this.getPageScroll();
		
		my_height = objLightbox.height();
		my_width = objLightbox.width();
		
		objLightbox.centerOnScreen();
		
		if(overlayClose)
		{
			objOverlay.click(this.closePopup);
		}
	}
	catch(e)
	{
		alert(e);
	}
}

ps.prototype.element = function(id)
{
/*	if (document.getElementById)
	{
		return document.getElementById(id);
	}
	else if (document.all)
	{
		return document.all[id];
	}
	else if (document.layers)
	{
		return document.layers[id];
	}*/
	
	return $('#'+id);
}



ps.prototype.encode = function(url)
{
	url=url.toString();
	var regcheck=url.match(/[\x90-\xFF]/g);
	if(regcheck)
	{
		for(var i=0;i<i.length;i++)
		{
			url=url.replace(regcheck[i],'%u00'+(regcheck[i].charCodeAt(0)&0xFF).toString(16).toUpperCase());
		}
	}
	return escape(url).replace(/\+/g,"%2B");
}

ps.prototype.formatArray = function(arrayfields)
{
	var str='';
	try{
		for(var i in arrayfields)
		{
			str+=i+'='+this.encode(arrayfields[i])+'&';
		}
	}
	catch(e){}
	
	return str;
}

ps.prototype.rteSimple = function(ele, code)
{
	$(ele).append("["+code+"][/"+code+"]");
}

ps.prototype.rteLink = function(ele)
{
	var url = prompt("Please enter the URL to the link", '');
	var text = prompt("Please enter the link text (OPTIONAL)", '');
	
	url = (typeof url == 'string' ) ? url : '';
	
	if(typeof text == 'string')
	{
		$(ele).append("[url="+url+"]"+text+"[/url]");
	}
	else
	{
		$(ele).append("[url]"+url+"[/url]");
	}
}

ps.prototype.insert = function(ele, add)
{
	$(ele).append(add);
	$(ele).focus();
}

ps.prototype.deleteComment = function(id)
{

	
	var req = function(response)
	{
		if(response == 'VALID')
		{
			$('#comment_'+id).fadeOut('slow');
		}
	};
	
	var url = this.baseurl+"index.php?act=static&page=comments&code=delete&id="+id;
	$.get( url, req );
	
	return false;
	
}


ps.prototype.oauth = function()
{
	var req = function(response)
	{
		location.href = response;
	};
	
	var url = this.baseurl+"oauth/auth_url.php";
	$.get( url, req );
	
	return false;
}

ps.prototype.deleteSignature = function(id)
{

	
	var req = function(response)
	{
		if(response == 'VALID')
		{
			$('#sig_'+id).fadeOut('slow');
		}
	};
	
	var url = this.baseurl+"index.php?act=moderate&code=deletesig&id="+id;
	
	$.get( url, req );
	return false;
	
}

ps.prototype.replaceSignature = function(id)
{

	
	var req = function(response)
	{
		if(response != 'INVALID')
		{
		
			$('#sig_'+id).fadeOut('slow');
			$('#sig_comment_'+id).html(response);
			$('#sig_'+id).fadeIn('slow');
			
			
		}
	};
	
	var url = this.baseurl+"index.php?act=moderate&code=replacesig&id="+id;
	$.get( url, req );
	
	return false;
	
}

ps.prototype.checkComments = function()
{
	var block = $('#commentBlock');
	if(block.height() > 400)
	{
		$('#commentBlock').height('400px');
		$('#commentBlock').css('overflow-y', "auto");
	}
}

ps.prototype.trimInt = function(s)
{
	while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,s.length); }
	return s;
}

ps.prototype.checkStatement = function(obj)
{
	obj = $(obj);
	$('#statementChars').html(this.number_format(obj.val().length) + "/20,000 characters");
	if(parseInt(obj.val().length) > 20000 || parseInt(obj.val().length) < 50 )
	{
		$('#statementChars').css('color', 'red');
	}
	else
	{
		$('#statementChars').css('color', '#73BC25');
	}
}

ps.prototype.number_format = function(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

ps.prototype.checkShortname = function(obj)
{
	obj = $(obj);
	var re = /^[0-9a-zA-Z]$/;
	if(!re.test(obj.val()))
	{
		obj.val(obj.val().replace(/[^0-9a-zA-Z]+/g,''));
	}
}

ps.prototype.box = function (html)
{
	return "<div class='block'><div class='tl'></div><div class='top'></div><div class='tr'></div><div class='inner'>"+html+"</div><div class='bl'></div><div class='bottom'></div><div class='br'></div></div>";
}

ps.prototype.cancelSign = function()
{
	$('#petitionSignPopup').html('')
	this.jsOverlay(false);
	return false;
	
}

ps.prototype.doSignSubmit = function()
{
	$('#this_sign_form').unbind('submit');
	$('#this_sign_form').submit();
}

ps.prototype.doSecondStage = function()
{
	$('#petitionSignPopup').hide();
	
	var html = '';
	html += "<img src='"+this.baseurl+"static/images/createaccount.png' class='cursor' onclick='ps.doSignSubmit();' alt='Create an Account' /><br /><br />";
	html += "<img src='"+this.baseurl+"static/images/signwithtwitter.png' class='cursor' onclick='ps.signTwitter();' alt='Sign In With Twitter' />";
	html = "<div class='center'>"+html+"</div>";
	
	ps.popup("How would you like to register?", html, "400px", "300px", 1);
	this.onPopupClose = ps.doSignSubmit;
		
	return false;
}

ps.prototype.doSignTwitter = function()
{
	var username = $('#twitter-username').val();
	var password = $('#twitter-password').val();
	
	
	var fields = {
		'username':  	username,
		'password': 	password
	};
	
	var req = function(response)
	{
		if(response == 'VALID')
		{
			ps.popup("Success!", "You have successfully signed in with twitter, now sending the signature.", "400px", "300px", 0);
			ps.doSignSubmit();
		}
		else
		{
			ps.signTwitter("<div class='red bold'>Error please try again.<br /><span onclick='ps.doSignSubmit();' class='blue cursor'>Click here if you cannot login to twitter.</span></div><br />");
		}
		
		
		return false;
	};
	
	var pet = $('#current_petition').attr('name');
	var url = this.baseurl+"index.php?act=twitter&code=login_tweet&pet_id="+pet;
	$.post( url, ps.formatArray(fields), req );
}

ps.prototype.signTwitter = function(html)
{
	
	if(typeof html == 'string')
	{
		var html = html;
	}
	else
	{
		var html = '';
	}
	
	html += "<div class='left bold' style='width: 100px;'>Username:</div><div class='left'><input type='text' class='signInput' id='twitter-username' /></div><br />";
	html += "<div class='left bold' style='width: 100px;'>Password:</div><div class='left'><input type='password' class='signInput' id='twitter-password' /></div><Br /><br />";
	html += "<div class='center'><img src='"+this.baseurl+"static/images/signwithtwitter.png' class='cursor' onclick='ps.doSignTwitter();' alt='Sign In With Twitter' /></div>";

	ps.popup("Sign in With Twitter", html, "400px", "300px", 1);
	this.onPopupClose = ps.doSignSubmit;
	
	
}


ps.prototype.signPop = function(shortname)
{
	var html = '';
	html += "<div class='blue large bold'>Sign Petition</div><div class='spacer10' style='height:5px;'></div>\r\n";
	html += "<div class='green medium bold'>"+$('#petition_name').html()+"</div><div class='spacer10'></div>\r\n";
	html += "<form id='this_sign_form' action=\""+ps.baseurl+"petitions/"+shortname+"/sign/\" method=\"post\">\r\n<input type='hidden' name='code' value='save' /><input type='hidden' name='onsite' value='yes' />\r\n";
	
	html += "<div class='left' style='width: 190px;'><input type='text' class='signInput' id='sign-first_name' name='first_name' /></div>";
	html += "<div class='left' style='width: 190px;'><input type='text' class='signInput' id='sign-last_name' name='last_name' /></div>";
	html += "<div class='clear'></div>";
	
	html += "<div class='left' style='width: 190px;'><input type='text' class='signInput' id='sign-email_address' name='email_address' /></div>";
	html += "<div class='left' style='width: 190px;'><input type='text' class='signInput' id='sign-country' name='country' /></div>";
	html += "<div class='clear'></div>";
	
	html += "<div class='signTextarea'><textarea id='sign-comments' name='comments'></textarea></div>";
	html += "<div class='clear'></div><br /><input type='image' src='"+this.baseurl+"static/images/sign_petition.png' alt='Sign this petition!' class='invisible' />";
	
	html += "</form>\r\n";
	
	
	var box_width = 400;
	if($('#petitionImage1'))
	{
	
		box_width = box_width + $('#petitionImage1').width() + 30;
		html = "<div class='left' style='width: "+$('#petitionImage1').width()+"px;'><div style='position:relative;'> <div style='position:relative;top:0;left:0;'><img src='"+$('#petitionImage1').attr('src')+"' height='"+$('#petitionImage1').height()+"' width='"+$('#petitionImage1').width()+"' alt='' /></div><div class='imageBlockTL'></div><div class='imageBlockTR'></div><div class='imageBlockBL'></div><div class='imageBlockBR'></div></div></div><div class='left' style='margin-left:20px;'>"+html+"</div><div class='clear'></div>";
	}
	
	
	html = this.box(html);
	
	
	html = "<div id='petitionSignPopup'><img onclick='ps.cancelSign();' src='"+this.baseurl+"static/images/close.png' alt='X' style='z-index:3;cursor: pointer;margin-top:-15px;margin-right:-15px;float:right;' /> "+html+"</div>";
	
	$('#js_popup').hide();
	$('#js_popup').html(html);
	
	
	
	$('#petitionSignPopup').css('width', box_width+"px");
	$('#petitionSignPopup').css('position', 'fixed');
	$('#petitionSignPopup').css('top', '50%');
	$('#petitionSignPopup').css('left', '50%');
	$('#petitionSignPopup').css('margin-left', "-"+(box_width/2)+"px");
	$('#petitionSignPopup').css('z-index', '3');
	
	
	
	this.jsOverlay(true);
	$('#js_popup').fadeIn('fast');
	
	$('#this_sign_form').submit(function() { return ps.doSecondStage(); });
	
	
	var box_height = parseInt(($('#petitionSignPopup').height()/2));
	$('#petitionSignPopup').css('margin-top', "-"+box_height+"px");
	
	this.defaultValue('#sign-country', "Country");
	this.defaultValue('#sign-comments', "I support this petition.", 1);
	if(this.member_email == '')
	{
		this.defaultValue('#sign-email_address', "Email Address");
	}
	else
	{
		$('#sign-email_address').val(this.member_email);
	}
	
	if(this.member_name == '')
	{
		this.defaultValue('#sign-first_name', "First Name");
		this.defaultValue('#sign-last_name', "Last Name");
	}
	else
	{
		var name = this.member_name.split(" ", 2);
		if(name[0] != '')
		{
			$('#sign-first_name').val(name[0]);
		}
		else
		{
			this.defaultValue('#sign-first_name', "First Name");
		}
		
		if(name[1] != '')
		{
			$('#sign-last_name').val(name[1]);
		}
		else
		{
			this.defaultValue('#sign-last_name', "Last Name");
		}
	}
}

ps.prototype.contactLib = function()
{
	var url = this.baseurl+"includes/lib/contacts/index.php";
	var newwindow=window.open(url,'Invite contacts to PetitionSpot','height=350,width=730,scrollbars=1');
	if (window.focus) {newwindow.focus()}
}


ps.prototype.getTweets = function (keyword)
{
	var keyword = this.encode(keyword + " -retweet");
	var url = "http://search.twitter.com/search.json?rpp=50&q="+keyword+"&callback=?";
	
	$.getJSON(url, function(json)
	{
		$(json.results).each(function(x, y) {
			var html = "<div id='tweet_"+x+"'><div style='width: 40px;float: left;'><img height='35' width='35' src='"+y.profile_image_url+"' alt='' /></div><div style='width: 200px;float: left;'><b>"+y.from_user+" said: </b><br /><span class='uppercase tiny lgray'>"+y.created_at+"</span><br />"+y.text+"</div></div><div style='clear:both;'></div><br />";
			$("#petition_tweet").append(html);
			$('#latest_tweets').show();
		});
		
		
		
	});
	
	
	



}

ps.prototype.defaultValue = function(id, value, reset)
{
	if(!$(id))
	{
		return;
	}
	var obj = $(id);
	obj.data('default', value);
	
	if(obj.val() == '')
	{
		obj.val(value);
	}
	
	if(reset != 1)
	{
		obj.focus(function()
		{
			if($(this).val() == $(this).data('default'))
			{
				$(this).val('');
			}
		});
		
		obj.blur(function()
		{
			if($(this).val() == '')
			{
				$(this).val($(this).data('default'));
			}
		});
	}
	
}

ps.prototype.jsOverlay = function(d)
{
	if(d)
	{
		$('#js_overlay').fadeIn('slow');
		$('#js_overlay').css('height', '100%');
		$('#js_overlay').css('width', '100%');
		$('#js_overlay').css('position', 'fixed');
		$('#js_overlay').css('top', '0');
		$('#js_overlay').css('left', '0');
	}
	else
	{
		$('#js_overlay').fadeOut('slow');
	}
}