var FeedByContext = {
	
	scriptName: "FeedByContext",
	attrKws: {
		attrKw: "",
		wrapClass: "-wrapper",
		rowClass: "-row",
		ewId: "elseWhere",
		ewClass: "-elseWhere"
	},
	
	bindId: "feedIcon",
	
	open: false,
	
	zIndex: 100,
	
	styleSet: {
		position: "absolute",
		top: "0",
		left: "0",
		zIndex: "100"
	},

	list: null,
	
	initialize: function(list){
		this.list = list;
		this.attrKws.attrKw = (this.scriptName.substring(0, 1).toLowerCase() + this.scriptName.substring(1, this.scriptName.length));
		if ($(this.bindId)){
			$(this.bindId).addEvent('click', function(event){var x = event.page.x;var y = event.page.y;FeedByContext.click(x, y);});
		}
	},
	
	click: function(x, y){
		var p = document.body.getScroll();
		for (var i in p){
		//	alert(p[i]);
		}
		if (this.open){
			this.removeContextMenu();
			this.open = false;
		}else{
			this.createContextMenu(x, y);
			this.open = true;
		}
	},
	
	createContextMenu: function(x, y){
		var size = window.getSize();
		var elseWhere = new Element("div", {
			id: this.attrKws.attrKw + this.attrKws.ewId,
			"class": this.attrKws.attrKw + this.attrKws.ewClass,
		}).injectInside(window.document.body);
		
		var sizeX = parseInt(size["x"], 10);
		var sizeY = parseInt(size["y"], 10);
		var posX = parseInt(x, 10);
		var posY = parseInt(y, 10);
		if (sizeX - posX < 200){
			posX -= 200;
		}
		
		elseWhere.setStyles(this.styleSet);
		elseWhere.setStyles({height: sizeY, width: sizeX});
		elseWhere.addEvent('click', function(event){var x = event.page.x;var y = event.page.y;FeedByContext.click(x, y);});
		
		var wrapper = new Element("div", {
			id: this.attrKws.attrKw,
			"class": this.attrKws.attrKw + this.attrKws.wrapClass
		}).injectInside(window.document.body);
		wrapper.setStyles({top: posY, left: posX, width: "200px", zIndex: "101"});
		
		for (var i in this.list){
			var row = new Element("div", {
				"class": this.attrKws.attrKw + this.attrKws.rowClass
			}).injectInside(wrapper);
			
			var a = new Element("a", {
				href: this.list[i].url
			}).injectInside(row);
			a.innerHTML = this.list[i].title;
		}
	},
	
	removeContextMenu: function(){
		if ($(this.attrKws.attrKw)){
			$(this.attrKws.attrKw).dispose();
		}
		if ($(this.attrKws.attrKw + this.attrKws.ewId)){
			$(this.attrKws.attrKw + this.attrKws.ewId).dispose();
		}
	}
}


/**
 * [COMMON]
 * Using MooTools
 * @author	Tom Goodsun (http://www.tom-gs.com/)
 * @date	2009.01.16
 */

// Wait for the content ...
window.addEvent('domready', function() {
	/* Override SqueezeBox.close() method to customize */
	SqueezeBox.close = function(e) {
		// Confirm closing the modal if form is changed
		// Add id attribute when it is created (ref.Line 369 in SqueezeBox.js)
		if (document.getElementById("sbox-window").getElementsByTagName("iframe")[0]){
			var iframeObject = document.getElementById("sbox-window").getElementsByTagName("iframe")[0].contentWindow;
			if (iframeObject.ContactForm){
				if(iframeObject.ContactForm.getFormChanged()){
					if (!confirm(iframeObject.ContactForm.getStatusMessage("close"))){
						return this;
					}
				}
			}
		}
		// The following script is the same as the native implementation of SqueezeBox.js
		var stoppable = ($type(e) == 'event');
		if (stoppable) e.stop();
		if (!this.isOpen || (stoppable && !$lambda(this.options.closable).call(this, e))) return this;
		this.fx.overlay.start(0).chain(this.toggleOverlay.bind(this));
		this.win.setStyle('display', 'none');
		this.trash();
		this.toggleListeners();
		this.isOpen = false;
		this.fireEvent('onClose', [this.content]);
		return this;
	}

	/**
	 * That CSS selector will find all <a> elements with the
	 * class boxed
	 *
	 * The example loads the options from the rel attribute
	 */
	SqueezeBox.assign($$('a.boxed'), {
		parse: 'rel'
	});
	SqueezeBox.assign($$('a.modalimage'));

	AutoAIP.initialize({
		enableSimple   : true,
		SRCompleteMatch: false,
		enableSR       : false
	});

	/**
	 * LAST BLOG LOADER
	 */
	if ($("lastestBlogItems")){
		var myRequest = new Request({
			method: "get", 
			url: "./common/scripts/commonScripts/lastBlogLoader.php",
			onSuccess: function(txt){
				$("lastestBlogItems").innerHTML = txt;
			}
		}).send("type=list_all&tbl=dat");
	}

	var list = {
		0: {title: "ブログのRSS", url: "http://www.tom-gs.com/blog/xml-rss2.php"}
	};
	FeedByContext.initialize(list);
});




