/**
 * tooltip.js - Tooltip for form 
 *
 * Copyright (C) wonder-boys | http://blog.wonder-boys.net/
 * Dual licensed under the MIT <http://www.opensource.org/licenses/mit-license.php>
 * @version 1.0.0
 * author takuya fujimi
 */

/* -------------------------------- 
    Config : Config
 -------------------------------- */

var Animation_config = {
    // animation config
    minute: 10, // ループする秒数 
    per: 0.2, // 変化量
    opacity: 0.9 //透明度
}

var Wonderbox_config = {
    // wonderbox config
    bg: 'wonderbox_bg',
    box: 'wonderbox_section'
};

/* -------------------------------- 
    Utility Class : base
 -------------------------------- */

var base = {
    preventDefault: function(e){
        if(e.preventDefault){
            e.preventDefault();
        }
        else if(window.event){
	  window.event.returnValue = false;
        }
    },
    addevent: function(node,evt,func){
        if(node.addEventListener){
            node.addEventListener(evt,func,true);
        } else if(node.attachEvent){
            node.attachEvent("on"+evt,func);	
        }
    },
    bind: function(){
        var args=[];
        if(arguments){
            for(var i=0,n=arguments.length;i<n;i++){
                args.push(arguments[i]);
            }
        }
        var object=args.shift();
        var func=args.shift();
        return function(event) {
            return func.apply(object,[event||window.event].concat(args));
        }
    },
    getnode: function(parent,tag,classname){
        var arr = [];
	var node = parent.getElementsByTagName(tag);
	if(classname == "allnode"){ return node };
	for(var i=0,n=node.length;i<n;i++){
            if(base.checkclass(node[i],classname)){ arr.push(node[i]); }
        }
	return arr;
    },
    checkclass: function(node,classname){
        var reg = new RegExp('(?:^|\\s)'+classname+'(?:$|\\s)');
        if(reg.test(node.className)){ return true; }
        else { return false; };
    },
    browser: function(){
        var str = navigator.userAgent.toLowerCase();
        if(str.indexOf('msie') != -1){
            str = "ie";
        } else if(str.indexOf('firefox') != -1){
            str = "ff";
        } else {
            str = "other";
        }
        return str;
    },
    getbrowserpos: function(){
        var pos = {};
        if(window.innerWidth){
            pos.width = window.innerWidth;
            pos.height = window.innerHeight;
        } else if(document.documentElement && document.documentElement.clientWidth != 0){
            pos.width = document.documentElement.clientWidth;
            pos.height = document.documentElement.clientHeight;
        } else if(document.body){
            pos.width = document.body.clientWidth;
            pos.height = document.body.clientHeight;
        }
        return pos;
    }
};

/* -------------------------------- 
    Animation Class : Animation
 -------------------------------- */

var Animation = function(obj){
    var args = arguments[0];
    this.int;
    this.minute = Animation_config.minute;
    this.per = Animation_config.per;
    this.obj = obj;
};

Animation.prototype = {
    run: function(type,beginvalue,aftervalue,func){
        this.set_property(type,beginvalue,aftervalue,func);
        this.bvalue > this.avalue ? this.decreament() : this.increament();
    },
    easing: function(type,beginvalue,aftervalue,func){
        this.set_property(type,beginvalue,aftervalue,func);
        this.bvalue > this.avalue ? this.easing_decreament() : this.easing_increament();
    },
    // set property
    set_property: function(type,beginvalue,aftervalue,func){
        this.type = type;
        this.bvalue = beginvalue;
        this.avalue = aftervalue;
        this.type == "opacity" ? this.unit = 0 : this.unit = "px";
	this.func = func;
        this.set_style();
    },
    // set css style to elements
    set_style: function(){
        if(this.type == 'opacity'){
            var str = base.browser();
            if(str == 'ie'){ this.obj.style.filter = 'alpha(opacity='+(this.bvalue*100)+')'; }
            else if(str == 'ff'){ this.obj.style.MozOpacity = this.bvalue + this.unit; }
            else { this.obj.style[this.type] = this.bvalue + this.unit; }
        } else {
            this.obj.style[this.type] = this.bvalue + this.unit;
        }
    },
    // decreament
    decreament: function(){
        if(this.int){ clearTimeout(this.int); };
        this.bvalue -= this.per;
        if(this.bvalue > this.avalue){
            this.loop(arguments.callee);
        } else {
            this.end();
        }
    },
    // increament
    increament: function(){
        if(this.int){ clearTimeout(this.int); };
        this.bvalue += this.per;
        if(this.bvalue < this.avalue){
            this.loop(arguments.callee);
        } else {
            this.end();
        }
    },
    loop: function(obj){
        this.set_style();
        this.int = setTimeout(base.bind(this,obj),this.minute);
    },
    end: function(){
        this.obj.style[this.type] = this.avalue + this.unit;
	if(this.func){ this.func(); };
    }
};


/* -------------------------------- 
   Wonderbox class : Wonderbox
 -------------------------------- */

var Wonderbox = function(){ this.init(); };

Wonderbox.prototype = {
    init: function(){
        this.body = document.getElementsByTagName('body')[0];
    },
    // open wonderbox
    /*
    open: function(e,obj){
        this.createnode();
        this.bg.style.display = 'block';
        this.contents = obj;
        new Animation(this.bg).run('opacity',0,Animation_config.opacity,base.bind(this,this.setHTML));
        base.preventDefault(e);
    },
    open_img: function(e,obj){
        this.createnode();
        this.bg.style.display = 'block';
        this.contents = '<img src="'+e.target.href+'" />';
        new Animation(this.bg).run('opacity',0,Animation_config.opacity,base.bind(this,this.setHTML));
        base.preventDefault(e);
    },
    */
    open_swf: function(e){
		this._top = document.body.scrollTop || document.documentElement.scrollTop;
		
		var _rel, _filepath, _title;
		if(window.event){
			_rel = window.event.srcElement.rel;
            _filepath = window.event.srcElement.href;
            _title = window.event.srcElement.title;
		} else {
			_rel = e.target.rel;
            _filepath = e.target.href;
            _title = e.target.title;
		}

		var _width = _rel.split(';')[1].split('=')[1];
        var _height = _rel.split(';')[2].split('=')[1];

        this.createnode();
        this.bg.style.display = 'block';
    
        this.contents = '';
        this.contents += '<p>'+ _title +'</p>';
		this.contents += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ _width +'" height="'+ _height +'">';
        this.contents += '<param name="movie" value="'+ _filepath +'" />';
        this.contents += '<param name="FlashVars" value="value" />';		  
        this.contents += '<embed src="'+ _filepath +'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+ _width +'" height="'+ _height +'"></embed>';
        this.contents += '</object>';
        this.contents += '<p id="close_btn"><a href="#">閉じる</a></p>';
        new Animation(this.bg).run('opacity',0,Animation_config.opacity,base.bind(this,this.setHTML));
        base.preventDefault(e);
    },
    createnode: function(){
        //create wrapper
        this.bg = document.createElement('div');
        this.bg.id = Wonderbox_config.bg;
        // create box
        this.box = document.createElement('div');
        this.box.id = Wonderbox_config.box;
        // append body
        document.body.appendChild(this.bg);
        document.body.appendChild(this.box);
        this.bg.style.display = 'none';
        this.box.style.display = 'none';
        // set close event
        base.addevent(this.bg,'click',base.bind(this,this.closebg));
    },
    // set HTML for wonderbox
    setHTML: function(){
        this.box.innerHTML = this.contents;
        var closebtn = document.getElementById('close_btn');
        base.addevent(closebtn,'click',base.bind(this,this.closebg));

        this.box.style.display = 'block';
        this.setposition();
    },
    setposition: function(){
        var pos = base.getbrowserpos();
		var _width = pos.width/2;
		var _height = pos.height/2;
        this.box.style.left = _width - this.box.offsetWidth/2 + 'px'; 
        this.box.style.top = this._top + _height - this.box.offsetHeight/2 + 'px';
    },
    // close wonderbox
    close: function(){
        new Animation(this.box).run('opacity',Animation_config.opacity,0,function(){ this.box.style.display = 'none'; this.closebg(); });
    },
    closebg: function(e){
        new Animation(this.bg).run('opacity',Animation_config.opacity,0,this.removenode);
	base.preventDefault(e);
    },
    removenode: function(){
        var bg = document.getElementById(Wonderbox_config.bg);            
        var box = document.getElementById(Wonderbox_config.box);
        bg.parentNode.removeChild(bg);
        box.parentNode.removeChild(box);
    }
};


(function(){
base.addevent(window,"load",function(){ 
    var box_arr = [];
    var anodes = document.getElementsByTagName('a');
    for(var i=0,n=anodes.length;i<n;i++){
        var wonder = new Wonderbox();
        if(anodes[i].rel.split(';')[0] == 'wonderbox'){
            base.addevent(anodes[i],'click',base.bind(wonder,wonder.open_swf));
        }
    }
});
})();
