var Rss = {
	init: function (){
		this.link = $("rss-feed");
		this.body = $(document.body);
		if(this.link){
			this.link.observe("click", function(){
				this.mask(true);
			}.bind(this));
		}
	},
	mask: function(show){
		if(show){
			if (window.innerHeight != undefined && window.scrollMaxY  != undefined) {// Firefox
				yWithScroll = window.innerHeight + window.scrollMaxY;
			}else if(window.innerHeight  && document.body.scrollHeight > document.body.offsetHeight){
				yWithScroll = (window.innerHeight >document.body.scrollHeight)?window.innerHeight:document.body.scrollHeight;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				yWithScroll = document.body.scrollHeight;
			} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
				yWithScroll = document.body.offsetHeight;
		  	}
		  	var h = document.viewport.getHeight();
			if(yWithScroll < h){
				yWithScroll = h;
			}
			this._mask = new Element('div',{"class": "black", "style": "height: "+parseInt(yWithScroll)+"px;"});
			this.body.insert(this._mask);

			this.win = new Element('div', {"id": "rss-window"});

			var dim =  document.viewport.getScrollOffsets();
			this.win.setStyle({"top": parseInt(dim.top+150)+"px"});
			var width = document.viewport.getWidth();
			this.win.setStyle({"left": parseInt(width/2-300)+"px"});
			var feedURL = js_url+"rss?cat_id="+Rss["category_id"]+(Rss["district_id"] ? "&dis_id="+Rss["district_id"]: "");
			this.changeContent(feedURL, true);

			this.body.insert(this.win);
			var district_loc = $("district-id-loc");
			var district_nat = $("district-id-nat");
			if(district_loc && district_nat){
				district_loc.observe("click", this.changeFeedUrl.bind(this, Rss["district_id"]));
				district_nat.observe("click", this.changeFeedUrl.bind(this, null));
			}
			var ico_close = $('ico-close');
			if(ico_close){
				ico_close.observe("click", function(){
					this.mask();
				}.bind(this));
			}
		}else{
			this._mask.remove();
			this.win.remove();
		}
	},
	changeFeedUrl: function(district_id){
		feedURL = js_url+"/rss?cat_id="+Rss["category_id"]+(district_id ? "&dis_id="+district_id: "");
		this.changeContent(feedURL, (district_id ? true : false));
	},
	changeContent: function(feedURL, ischecked){
		var content = '<h2>RSS Subscription to Recycle.co.uk <a class="pointer" id="ico-close" title="Close"></a>'+
		'</h2><form action="'+js_url+'rss" method="get" id="rss-form">'+
		'<input type="hidden" name="cat_id" value="'+Rss["category_id"]+'" />'+
		(Rss["district"] ? '<p>You have choosen to recieve updates via RSS to the category '+Rss["category"]+' in '+Rss["district"]+'</p>'+
		'<p>You can also receive updates to the category '+Rss["category"]+' nationwide for more options</p><br />' :
		'<p>You have choosen to recieve updates via RSS to the category '+Rss["category"]+' in nationwide</p>')+
		(Rss["district_id"] ? '<p class="orange"><input type="radio" name="dis_id" id="district-id-loc" value="'+Rss["district_id"]+'" '+(ischecked ? 'checked="checked"':'')+' /> '+Rss["category"]+' in '+Rss["district"]+'</p>'+
		'<p class="orange"><input type="radio" name="dis_id" value="" id="district-id-nat" '+(!ischecked ? 'checked="checked"':'')+' /> '+Rss["category"]+' in (Nationwide)</p>':
		'<p class="orange"><input type="radio" name="dis_id" value="" checked="checked" /> '+Rss["category"]+' in (Nationwide)</p>')+
		'<p><div class="subscribe_service"><a href="http://add.my.yahoo.com/rss?url='+feedURL+'"><img src="http://news.carrentals.co.uk/wp-content/themes/cr/images/addmyyahoo.gif" alt="Subscribe to MyYahoo!" /></a><a href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url='+feedURL+'"><img src="http://news.carrentals.co.uk/wp-content/themes/cr/images/addnewsgator.gif" alt="Subscribe to Newsgator" /></a><a href="http://feeds.my.aol.com/add.jsp?url='+feedURL+'"><img src="http://news.carrentals.co.uk/wp-content/themes/cr/images/addmyaol.gif" alt="Subscribe to MyAOL" /></a><a href="http://www.bloglines.com/sub/'+feedURL+'"><img src="http://news.carrentals.co.uk/wp-content/themes/cr/images/addbloglines.gif" alt="Subscribe to Bloglines" /></a><a href="http://www.netvibes.com/subscribe.php?url='+feedURL+'"><img src="http://news.carrentals.co.uk/wp-content/themes/cr/images/addnetvibes.gif" alt="Subscribe to Netvibes" /></a><a href="http://fusion.google.com/add?feedurl='+feedURL+'"><img src="http://news.carrentals.co.uk/wp-content/themes/cr/images/addgoogle.gif" alt="Subscribe to Google" /></a></div></p>'+
		'<div class="clear"></div><p>...with other readers:<p>'+
		'<select onchange="Main.redirect($(this).getValue())" class="field-big">'+
		'<option value="#">(Choose Your Reader)</option>'+
		'<option value="http://my.msn.com/addtomymsn.armx?id=rss&ut='+feedURL+'&ru=http://news.carrentals.co.uk/">My MSN</option>'+
		'<option value="http://my.feedlounge.com/external/subscribe?url='+feedURL+'">FeedLounge</option>'+
		'<option value="http://www.newsburst.com/Source/?add='+feedURL+'">Newsburst</option>'+
		'<option value="http://www.rojo.com/add-subscription?resource='+feedURL+'">ROJO</option>'+
		'</select></p></p><br /><br /><p><input type="submit" value="View Feed XML" class="rss-btn" /></p>'+
		'</form>';

		this.win.update(content);

		var district_loc = $("district-id-loc");
		var district_nat = $("district-id-nat");
		if(district_loc && district_nat){
			district_loc.observe("click", this.changeFeedUrl.bind(this, Rss["district_id"]));
			district_nat.observe("click", this.changeFeedUrl.bind(this, null));
		}
		var ico_close = $('ico-close');
		if(ico_close){
			ico_close.observe("click", function(){
				this.mask();
			}.bind(this));
		}
	},
	setup: function(data){
		for(var i in data){
			Rss[i] = data[i];
		}
	}
}
Main.onReady(Rss.init, Rss);
