(function() {

	var butterfly_count = 4;
    var gardenSize = [120, 243];
    var duration = 4;
	var garden_img_src = "http://logstar.realworld.jp/img/hana.gif";
	var butterfly_img_src = "http://logstar.realworld.jp/img/butterfly2.gif";
	var link_url = "http://logstar.realworld.jp/campaign200712.html";
	
	var garden = new Ext.XTemplate(
		'<div id="butterfly-garden" style="right:0; position:absolute; bottom:0px;">',
		'<div id="butterfly"></div>',
		'<p><a href="#" id="fly-butterfly" onclick="return false;"><img src="{img_src}" alt="ページの先頭へ" width="120" height="220" border="0" /></a></p>',
		'</div>'
	);
	garden.compile();

	var butterfly = new Ext.XTemplate(
		'<a href="http://logstar.realworld.jp/campaign200712.html" target="_blank"><div id="{id}" style="height: 30px; width: 40px; position: absolute; background: url({img_src}) no-repeat; top:{top}px" style="left:{left}px"></div></a>'
	);
	butterfly.compile();

    var doc = document.all == undefined ? document : document.all;

    var myWidth = function() {
        return YAHOO.util.Dom.getClientWidth() + YAHOO.util.Dom.getDocumentScrollLeft(document);
    }
    var myHeight = function() {
        return YAHOO.util.Dom.getClientHeight() + YAHOO.util.Dom.getDocumentScrollTop(document);
    }
    var rand = function (r) {
        r = Math.random() > 0.5 ? r : -1*r;
        return Math.floor( Math.random()*r );
    }
    var sign = function (r) {
        r = Math.random() > 0.5 ? r : -1*r;
        return r;
    }
    var flip = function (s, m) {
        return s-m;
    }

    var xy = [flip(myWidth()/4),flip(myHeight()/4)];

    var setXY = function (e) {
        xy = YAHOO.util.Event.getXY(e);
    }
    YAHOO.util.Event.on(doc, "mousemove", setXY);
    
    var points = function (el) {
        var win = [ myWidth() ,myHeight()];
        var el = YAHOO.util.Dom.getXY(el);
        if (isNaN(el[0])) el = xy;
        var nxt = [];
        var f_xy = [flip(myWidth(), xy[0]), flip(myHeight(), xy[1])];
        var f_el = [flip(myWidth(), el[0]), flip(myHeight(), el[1])];
        if ( xy[0]+50 < win[0] && xy[1]+50 < win[1] && (xy[0]+300 > el[0] && xy[0]-300 < el[0]) && (xy[1]+300 > el[1] && xy[1]-300 < el[1]) ) {
            nxt[0] = xy[0] + rand(200);
            nxt[1] = xy[1] + rand(200);
        } else {
            nxt[0] = flip(myWidth()-50, f_el[0]>f_xy[0] ? (f_el[1]*Math.random()+sign(f_xy[0]*Math.random()))*Math.random() : (f_xy[0]*Math.random()+sign(f_el[1]*Math.random()))*Math.random() );
            nxt[1] = flip(myHeight()-100,f_el[1]>f_xy[1] ? (f_el[0]*Math.random()+sign(f_xy[1]*Math.random()))*Math.random() : (f_xy[1]*Math.random()+sign(f_el[0]*Math.random()))*Math.random() );
        }
        if (nxt[0] > myWidth()) nxt[0] = myWidth() - 100;
        if (nxt[1] > myHeight()) nxt[1] = myHeight() - 100;
        return { points: { to: nxt } };
    }
    var createEl = function (n) {
        var anim = new YAHOO.util.Motion(n, points(), duration);
        anim.goto = function() {
            this.attributes = points(this.getEl());
            this.animate();
        }
        anim.onComplete.subscribe(anim.goto);
        return anim.animate();
    }
    var switchFlying = function () {
        if (document.getElementById('butterfly').childNodes.length) {
            var child = YAHOO.util.Dom.getChildren(document.getElementById('butterfly'));
            for (var i in child) {
            	if (YAHOO.lang.isFunction(child[i])) continue;
                document.getElementById('butterfly').removeChild(child[i]);
            }
            return;
        } else {
			for(var i=0; i<butterfly_count; i++) {
    	   		butterfly.append(document.getElementById('butterfly'), {
    	   			id:'butterfly_'+i,
    	   			top:i*(gardenSize[1]/butterfly_count),
    	   			left:-50+(i*30*Math.random()),
    	   			img_src:butterfly_img_src
    	   		});
        		createEl('butterfly_'+i);
			}
        }
    }
    var welcomeLogStar = function () {
    	location.href=link_url;
    }
    
    YAHOO.util.Event.on('fly-butterfly', 'click', welcomeLogStar);
    YAHOO.util.Event.on(doc, 'scroll', function(e) {
        var el = YAHOO.util.Dom.getXY(document.getElementById('butterfly-garden'));
        if (el[0] == undefined) el = [myWidth(),myHeight()];
        var anim = new YAHOO.util.Motion('butterfly-garden', { points: { from:el, to:[myWidth()-gardenSize[0],myHeight()-gardenSize[1]]} }, 1);
        anim.animate();
    });
    
	var init = function () { 
    	garden.append(document.body, {img_src:garden_img_src});
    	switchFlying();
	}
	YAHOO.util.Event.onDOMReady(init); 
})();