if (!$defined(window.openEx))
{
	window.openEx = function(url, winref, propsObj) {
		props_arr = [];
		for(x in propsObj) {
			props_arr.push(x+'='+propsObj[x]);
		}
		props_str = props_arr.join(",");

		return window.open(url, winref, props_str);
	}
}

window.NewsletterSignup_win = null;
if (!$defined(window.openNewsletterSignup))
{
	window.openNewsletterSignup = function() {
		_popup_properties = {status:0,
                             toolbar:0,
                             location:1,
                             menubar:0,
                             directories:0,
                             resizable:0,
                             scrollbars:1,
                             height:Math.min(800,screen.height),
                             width:Math.min(700,screen.width)};
        
        winx = (screen.width-_popup_properties['width'])/2;
        winy = (screen.height-_popup_properties['height'])/2;
        
        _popup_properties['left']=_popup_properties['screenX']=winx;
        _popup_properties['top']=_popup_properties['screenY']=winy;
		
		url = 'http://3-prime.us2.list-manage1.com/subscribe?u=0cbf8d3ad1701b8ab479f3f72&id=ec54ad98ae';
		
		if (window.NewsletterSignup_win!=null)
		{
			window.NewsletterSignup_win.close();
			window.NewsletterSignup_win = null;
		}
		
		window.NewsletterSignup_win = window.openEx(url, 'NewsletterSignup_win', _popup_properties);
		
		window.onfocus = function() {
			window.NewsletterSignup_win.close();
			window.NewsletterSignup_win = null;
		}
    }
}

var SopaPipa = (function() {
	
	return {};
	
	/**
	 * Private Scoped
	 */
	var poll_num_protesters = function() {
		
		url = 'http://www.3-prime.com/wp-content/themes/3prime/includes/sopa-pipa-protesters-count.php';
		new Request.HTML({
			url: url,
			method: 'get',
			update: $('elSopaPipa').getElement('.num-protestors')
			/*onSuccess: function(responseText, responseXML) {
			    
			    $$('#elSopaPipa .num-protestors')[0].set('html', responseText);
			    
			}*/
			
		}).get();
		
	};
	
	/**
	 * Public Scoped
	 */
	return {
		
		showform: function() {
			
			_popup_properties = {status:0,
                             toolbar:0,
                             location:1,
                             menubar:0,
                             directories:0,
                             resizable:0,
                             scrollbars:1,
                             height:Math.min(400,screen.height),
                             width:Math.min(400,screen.width)};
        
	        winx = (screen.width-_popup_properties['width'])/2;
	        winy = (screen.height-_popup_properties['height'])/2;
	        
	        _popup_properties['left']=_popup_properties['screenX']=winx;
	        _popup_properties['top']=_popup_properties['screenY']=winy;
			
			url = 'http://www.3-prime.com/wp-content/themes/3prime/includes/sopa-pipa-form.php';
			
			if (window.sopapipa_win!=null)
			{
				window.sopapipa_win.close();
				window.sopapipa_win = null;
			}
			
			window.sopapipa_win = window.openEx(url, 'sopapipa_win', _popup_properties);
			
			/*window.onfocus = function() {
				window.sopapipa_win.close();
				window.sopapipa_win = null;
			}*/
			
		},
		addheader: function() {
			
			var img_objects = [
				{
					'img':'/wp-content/themes/3prime/images/sopa-pipa/banner-nopa-to-sopa.jpg',
					'text':'Say NOPA to SOPA!'
				},
				{
					'img':'/wp-content/themes/3prime/images/sopa-pipa/banner-strippa-da-pipa.jpg',
					'text':'Strippa da PIPA!'}
			];
			
			idx = Math.random(0,1).round();
			imgobj = img_objects[idx];
			
			html = [
				'<a class="banner" href="#" onclick="SopaPipa.showform(); return false;" style="text-decoration: none; display: block; height: 100%; width: 100%; background: url('+imgobj.img+') no-repeat top center;">',
				'<span class="num-protestors" style="color: #f00; text-decoration: none; font: normal bold 20px Arial, Helvetica, sans-serif; display: block; position: absolute; top: 80px; left: 300px;"></span>',
				'</a>'
			].join('');
			el = new Element('div', {'id':'elSopaPipa', 'styles':{
				
				position: 'relative',
				height: '120px',
				width: '960px',
				margin: '0 auto'
				
			}}).set('html', html);
			
			b = $(document.body);
			el.injectTop(b);
			poll_num_protesters();
			poll_num_protesters.periodical(5000);
		}
		
	};
	
	
})();

