/************************************************
* slowbirdsFrameWork
* since:	2009-05-26
* modified: 2009-05-26
* edited:   slowbirds@gmail.com
************************************************/

var slowbirdsFramework = function(){
	this.domain = "slowbirds.org";
	this.baseurl = "http://slowbirds.org";
	this.contact = "slowbirds@gmail.com";
	this.author = "akinori";
	this.library = null;
};
slowbirdsFramework.prototype.df = {
	//cross browser & array EventListner
	addEventListnerX: function(target,event,func) {//dom,event,function
		if(target.length > 1) {
			for(var i=0;i<target.length;i++) {
				if (window.addEventListener) {
					target[i].addEventListener(event,func,false);
				}else if (window.attachEvent && document.getElementById) {
					target[i].attachEvent("on"+event,func);
				}
			}
		}else {
			if (window.addEventListener) {
				target.addEventListener(event,func,false);
			}else if (window.attachEvent && document.getElementById) {
				target.attachEvent("on"+event,func);
			}
		}
	},

	//file_exists > only image file on samedomain
	imageFileExists: function(sImageUrl) {
		var img = new Image();
		img.src = sImageUrl;
		return img.height > 0; // 読み込みに失敗すれば 0 になる。
	},

	//rollOverImages
	rollOver: function() {
		var aPreLoad = new Array();
		var sTempSrc;
		var aImages = document.getElementsByTagName('img');
		for (var i = 0; i < aImages.length; i++) {
			if (aImages[i].className == 'rollover') {
				var src = aImages[i].getAttribute('src');
				var ftype = src.substring(src.lastIndexOf('.'), src.length);
				var hsrc = src.replace(ftype, '_s'+ftype);
				//if(!imageFileExists(hsrc)) return;
				aImages[i].setAttribute('hsrc', hsrc);
				aPreLoad[i] = new Image();
				aPreLoad[i].src = hsrc;
				aImages[i].onmouseover = function() {
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
				aImages[i].onmouseout = function() {
					if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_s'+ftype, ftype);
					this.setAttribute('src', sTempSrc);
				}
			}
		}
	},
	keepOver: function() {
		var thisPage = location.href;
		var targets = document.getElementById("navPushed");
		targets = targets.getElementsByTagName("img");
		if(thisPage.match(/\/logs\//)) {
			var src = targets[1].src;
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_s'+ftype);
			targets[1].src = hsrc;
			targets[1].className = "thisPage";
		}else if(thisPage.match(/\/scraps\//)) {
			var src = targets[2].src;
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_s'+ftype);
			targets[2].src = hsrc;
			targets[2].className = "thisPage";
		}
	},

	//replace "_blank" tag. for strict XHTML
	blanker: function() {
		var sTempHref;
		var aLinks = document.getElementsByTagName('a');
		for (var i = 0; i < aLinks.length; i++) {
			if (aLinks[i].getAttribute('rel') == 'external') {
				var href = aLinks[i].getAttribute('href');
				aLinks[i].onclick = function() {
					sTempHref = this.getAttribute('href');
					window.open(sTempHref);
					return false;
				}
			}
		}
	},

	//readonly textarea&bixes
	selectText: function() {
		var aTextareas = document.getElementsByTagName('textarea');
		var aInputs	= document.getElementsByTagName('input');
		for (var i = 0; i < aTextareas.length; i++) {
			if (aTextareas[i].getAttribute('readonly')) {
				aTextareas[i].onfocus = function() {this.select();}
				aTextareas[i].onclick = function() {this.select();}
			}
		}

		for (var i = 0; i< aInputs.length; i++) {
			if(aInputs[i].getAttribute('type') == 'text' && aInputs[i].getAttribute('readonly')) {
				aInputs[i].onfocus = function() {this.select();}
				aInputs[i].onclick = function() {this.select();}
			}
			
		}

	},
	
	//mymusic
	mp3player: function(filepath) {
		var outSource = '<object type="application/x-shockwave-flash" data="/shared/swf/player_mp3_maxi.swf" width="200" height="20">';
		outSource += '<param name="movie" value="/shared/swf/player_mp3_maxi.swf" />';
		outSource += '<param name="bgcolor" value="#ffffff" />';
		outSource += '<param name="FlashVars" value="mp3='+filepath+'&showstop=1&showinfo=1&showvolume=1&buttonwidth=20&sliderwidth=15&volumeheight=8" />';
		outSource += '</object>';
		outSource += '<p class="mymusicFiles"><a href="'+filepath+'"><img src="/shared/images/icon_download.png" />download(mp3)</a></p>';
		document.write(outSource);
		return;
	}
};

//call
var slowbirds = new slowbirdsFramework();

//function default javascripts
slowbirds.df.addEventListnerX(window,"load",function() {
	if (!document.getElementById) return
	slowbirds.df.keepOver();
	slowbirds.df.rollOver();
	slowbirds.df.blanker();
	slowbirds.df.selectText();
});
