/**
 * k.Growler 1.0.0
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Written by Kevin Armstrong <kevin@kevinandre.com>
 * Last updated: 2008.10.14
 *
 * Growler is a PrototypeJS based class that displays unobtrusive notices on a page. 
 * It functions much like the Growl (http://growl.info) available on the Mac OS X. 
 *
 * Changes in 1.0.1:
 * - 
 *
 * @todo
 */
window.k = window.k || {};
;(function(){

var noticeOptions = {
	header: 			'&nbsp;'
	,speedin: 			0.3
	,speedout: 			0.5
	,outDirection: 		{ y: -20 }
	,life: 				5
	,sticky: 			false
	,className: 		""
};
var growlerOptions = {
	location: 			"tr"
	,width: 			"250px"
};
var IE = (Prototype.Browser.IE) ? parseFloat(navigator.appVersion.split("MSIE ")[1]) || 0 : 0;
function removeNotice(n, o){
	o = o || noticeOptions;
	new Effect.Parallel([
		new Effect.Move(n, Object.extend({ sync: true, mode: 'relative' }, o.outDirection)),
		new Effect.Opacity(n, { sync: true, to: 0 }) 
	], {
		duration: o.speedout
		,afterFinish: function(){
			try {
				var ne = n.down("div.notice-exit");
				if(ne != undefined){
					ne.stopObserving("click", removeNotice);
				}
				if(o.created && Object.isFunction(o.created)){
					n.stopObserving("notice:created", o.created);
				}
				if(o.destroyed && Object.isFunction(o.destroyed)){
					n.fire("notice:destroyed");
					n.stopObserving("notice:destroyed", o.destroyed);
				}
			} catch(e){}
			try {
				n.remove();
			} catch(e){}
		}
	});
}
function createNotice(growler, msg, options){
	var opt = Object.clone(noticeOptions);
	options = options || {};
	Object.extend(opt, options);
	var notice;
	if (opt.className != ""){
		notice = new Element("div", {"class": opt.className}).setStyle({display: "block", opacity: 0});
	} else {
		notice = new Element("div", {"class": "Growler-notice"}).setStyle({display: "block", opacity: 0});
	}
	if(opt.created && Object.isFunction(opt.created)){
		notice.observe("notice:created", opt.created);
	}
	if(opt.destroyed && Object.isFunction(opt.destroyed)){
		notice.observe("notice:destroyed", opt.destroyed);
	}

	if (opt.sticky){
            if (!opt.invitation){ 
		var noticeExit = new Element("div", {"class": "Growler-notice-exit"}).update("&times;");
             }else{
                var noticeExit =  new Element("div", {"class": "invitationExit"});
                var pExit     =  new Element("p", {"class": "exit"});
                var exitLink   =  new Element('a', { href: "javascript:void(0)", onClick: "close_invitation("+opt.uid+", 0)"}).update("閉じる&times;");
                exitLink.onclick = function(){close_invitation(opt.uid, 0);};
                pExit.insert(exitLink);
                noticeExit.insert(pExit);
             }
		noticeExit.observe("click", function(){ removeNotice(notice, opt); });
		notice.insert(noticeExit);
	}

	if (opt.img != ""){
	   //var noticeImgLink =  new Element('a', { 'class': 'foo', href: opt.link }).update(opt.img);
           var noticeImgLink =  new Element('a', { onClick: "openProfile('"+opt.uname+"')", href: "javascript:;"}).update(opt.img);
           noticeImgLink.onclick = function(){openProfile(opt.uname);};
	   var noticeImg = new Element("div", {"class": "invitatioPFpic"}).insert(noticeImgLink);
           //var noticeImg = new Element("div", {"class": "invitatioPFpic"}).update(opt.img);
           //noticeImg.onclick = function(){openProfile(opt.uname);};
	   notice.insert(noticeImg);
	}

	if (opt.invitation){ 
    	    //var noticeChat =  new Element('a', { 'class': 'whiteLink', style: "color:#FFF", href: opt.link }).update(opt.uname+"ちゃんと割引チャット");
            var noticeChat =  new Element('a', { 'class': "whiteLink", style: "color:#FFF",  href: "javascript:void(0)", onClick: "openInvitationVchat('"+opt.uid+"','115'); accept_invitation('"+opt.uid+"'); return false;"}).update(opt.uname+"ちゃんとチャットする");
            noticeChat.onclick = function(){openInvitationVchat(opt.uid,115); accept_invitation(opt.uid);};
	    var noticeText =  new Element("div", {"class": "invitationText"});
	    noticeText.insert(new Element("p", {"class": "invitedUser"}).update(opt.uname));
	    noticeText.insert(new Element("p", {"class": "invitePF"}).update(msg));
	    notice.insert(noticeText);
            noticeChat.observe("click", function(){ removeNotice(notice, opt); });

            var noticeBtn = new Element("div", {"class": "invitationBtn_area"});
            var noticeUL = new Element("ul", {});
	    var noticeLI  = new Element("li", {});
    	    noticeLI.insert(noticeChat);
    	    noticeUL.insert(noticeLI);
	    noticeBtn.insert(noticeUL);
            notice.insert(noticeBtn);
        }else{
	    notice.insert(new Element("div", {"class": "Growler-notice-head"}).update(opt.header));
	    notice.insert(new Element("div", {"class": "Growler-notice-body"}).update(msg));	
	}
	growler.insert(notice);
	new Effect.Opacity(notice, { to: 1, duration: opt.speedin });
	if (!opt.sticky || opt.life > 0){
		removeNotice.delay(opt.life, notice, opt);
	}
	notice.fire("notice:created");
	return notice;
}
function specialNotice(g, m, o, t, b, c){
	o.header = o.header || t;
	var n = createNotice(g, m, o);
	n.setStyle({ backgroundColor: b, color: c });
	return n;
}
k.Growler = Class.create({
	initialize: function(options){
		var opt = Object.clone(growlerOptions);
		options = options || {};
		Object.extend(opt, options);
		this.growler = new Element("div", { "class": "Growler", "id": "Growler" });
		this.growler.setStyle({ position: ((IE==6)?"absolute":"fixed"), padding: "10px", "width": opt.width, "z-index": "50000" });
		if(IE==6){
			var offset = { w: parseInt(this.growler.style.width)+parseInt(this.growler.style.padding)*3, h: parseInt(this.growler.style.height)+parseInt(this.growler.style.padding)*3 };
			switch(opt.location){
				case "br":
					this.growler.style.setExpression("left", "( 0 - Growler.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px'");
				  	this.growler.style.setExpression("top", "( 0 - Growler.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px'");
					break;
				case "tl":
					this.growler.style.setExpression("left", "( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px'");
				  	this.growler.style.setExpression("top", "( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px'");
					break;
				case "bl":
					this.growler.style.setExpression("left", "( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px'");
				  	this.growler.style.setExpression("top", "( 0 - Growler.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px'");
					break;
				default:
					this.growler.setStyle({right: "auto", bottom: "auto"});
					this.growler.style.setExpression("left", "( 0 - Growler.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px'");
				  	this.growler.style.setExpression("top", "( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px'");
					break;
			}
		} else {
			switch(opt.location){
				case "br":
					this.growler.setStyle({bottom: 0, right: 0});
					break;
				case "tl":
					this.growler.setStyle({top: 0, left: 0});
					break;
				case "bl":
					this.growler.setStyle({top: 0, right: 0});
					break;
				case "tc":
					this.growler.setStyle({top: 0, left: "25%", width: "50%"});
					break;
				case "bc":
					this.growler.setStyle({bottom: 0, left: "25%", width: "50%"});
					break;
				default:
					this.growler.setStyle({top: 0, right: 0});
					break;
			}
		}
		this.growler.wrap( document.body );		
	}
	,growl: function(msg, options) {
		return createNotice(this.growler, msg, options);
	}
	,warn: function(msg, options){
		return specialNotice(this.growler, msg, options, "Warning!", "#F6BD6F", "#000");
	}
	,error: function(msg, options){
		return specialNotice(this.growler, msg, options, "Critical!", "#F66F82", "#000");
	}
	,info: function(msg, options){
		return specialNotice(this.growler, msg, options, "Information!", "#BBF66F", "#000");
	}
	,ungrowl: function(n, o){
		removeNotice(n, o);
	}
});

})();
