var SubsStickr = {
	imgArray : {
			'1': 'SubsStickr_icon_60x30.png',
			'2': 'SubsStickr_icon_120x60.png',
			'3': 'SubsStickr_icon_180x90.png',
			'4': 'SubsStickr_icon_60x30_en.png',
			'5': 'SubsStickr_icon_120x60_en.png',
			'6': 'SubsStickr_icon_180x90_en.png'
	},
	getTag : function() {
		return [
			'<div class="SubsStickr_shift">',
			'<div class="SubsStickr_shadow">',
			'<div class="SubsStickr_main">',
			'<iframe src="http://www.rocaz.net/SubsStickr/show?' + SubsStickrParams.FeedURL + '" width="600px" height="420px" frameborder="0" scrolling="no" style="border: 0px; margin: 0px; padding: 0px; position: relative; left: -5px; top: -5px;">',
			'</iframe>',
			'<div class="SubsStickr_footer">',
			'<a href="#" onclick="SubsStickr.hiddentags();return false;" style="border: 0px; text-decoration: none; background-color: transparent;"><img src="http://www.rocaz.net/pic/window_close.png" border="0"/><span style="font-size: 24px; color: #808080;">CLOSE</span></a>',
			'</div>',
			'</div>',
			'</div>',
			'</div>'
		].join('');
	},
	showtags : function () {
		if(!document.getElementById('SubsStickrTag')) {
			var stickrtag = document.createElement('div');
			stickrtag.id = "SubsStickrTag";
			stickrtag.style.position = 'absolute';
			stickrtag.style.display = 'none';
			stickrtag.style.width = '610px';
			stickrtag.style.height = '600px';
			var objBody = document.getElementsByTagName("body").item(0);
			objBody.appendChild(stickrtag);
		} else {
			var stickrtag = document.getElementById('SubsStickrTag');
		}

		var tags = this.getTag();
		stickrtag.innerHTML = tags;

		var pSize = this.getWindowSize();
		var eSize = this.getElementSize(stickrtag);
		stickrtag.style.left = Math.floor((pSize.width - eSize.width) / 2) + 'px'; 
		stickrtag.style.display = "block";

		SubsStickr.onUpdatePositions();
		SubsStickr.onScroll();
		SubsStickr.updatePositions();
		SubsStickr.floatOn();

	},
	getElementSize : function(elm) {
		var width, height;
		if(elm.offsetWidth) {
			width = elm.offsetWidth;
			height = elm.offsetHeight;
		} else {
			if(elm.style.pixelWidth) {
				width = elm.style.pixelWidth;
				height = elm.style.pixelHeight;
			} else {
				if(elm.style.width) {
					width = elm.style.width;
					height = elm.style.height;
				}
			}
		}
		if(typeof width == "string") {
			if(width.indexOf("px",0) > -1) {
				width = width.substr(0, width.indexOf("px",0));
			}
		}
		if(typeof height == "string") {
			if(height.indexOf("px",0) > -1) {
				height = height.substr(0, height.indexOf("px",0));
			}
		}
		return {"width":width, "height":height};
	},
	getWindowSize : function() {
		var width, height;
		if(self.innerHeight) {
			width = self.innerWidth;
			height = self.innerHeight;
		} else {
			if(document.documentElement && document.documentElement.clientHeight) {
				width = document.documentElement.clientWidth;
				height = document.documentElement.clientHeight;
			} else {
				if(document.body) {
					width = document.body.clientWidth;
					height = document.body.clientHeight;
				}
			}
		}
		return {"width":width, "height":height};
	},
	getScrollTop : function() {
		var pos;
		if(document.documentElement && document.documentElement.scrollTop) {
			pos = document.documentElement.scrollTop;
		} else {
			if(document.body){
				pos = document.body.scrollTop;
			}
		}
		return pos;
	},
	hiddentags : function () {
		document.getElementById('SubsStickrTag').style.display = "none";
		document.getElementById('SubsStickrTag').innerHTML = "";
	},

	floatOn : function(){
		var bottletag = document.getElementById('SubsStickrTag');
    	bottletag.style.setProperty('position', 'fixed', null);
    	bottletag.style.setProperty('top', '25px', null);
    	bottletag.style.removeProperty('margin-top');
	},
	floatOff : function(){
		var bottletag = document.getElementById('SubsStickrTag');
    	bottletag.style.removeProperty('position');
    	bottletag.style.removeProperty('top');
    	bottletag.style.setProperty('margin-top', '25px', null);
	},
	updatePositions : function () {
		var bottletag = document.getElementById('SubsStickrTag');
		var top = SubsStickr.getScrollTop();
		bottletag.style.top  = Math.floor(top + 10) + 'px';
	},

	onUpdatePositions : function (){

		if(typeof window.addEventListener == 'function') {
			window.addEventListener('scroll', SubsStickr.updatePositions, false);
		} else if(typeof window.attachEvent == 'object') {
			window.attachEvent('onscroll', SubsStickr.updatePositions);
		}

	},
	onScroll : function (){

		if(typeof window.addEventListener == 'function') {
			window.addEventListener('scroll', SubsStickr.floatOn, false);
		} else if(typeof window.attachEvent == 'object') {
			window.attachEvent('onscroll', SubsStickr.floatOn);
		}

	},

	initial : function () {
		document.write('<a href="#" onclick="SubsStickr.showtags();return false;" style="border: 0px; text-decoration: none; background-color: transparent;"><img src="http://www.rocaz.net/pic/' + this.imgArray[SubsStickrParams.ImageNo] + '"  title="このブログを購読します" border="0"/></a>');
		document.write('<style type="text/css">');
		document.write('.SubsStickr_shift {position: relative; left: 5px; top: 5px;}');
		document.write('.SubsStickr_shadow {background-color: #d1cfd0;}');
		document.write('.SubsStickr_shadow .SubsStickr_main {background-color: white; border: 1px solid gray; padding: 6px; position: relative; left: -5px; top: -5px;}');
		document.write('.SubsStickr_footer {padding: 2px 0px;text-align: right; border-top: 1px solid gray;}');
		document.write('</style>');
	}
};

SubsStickr.initial();

