// global
var FB_URL = 'http://ssl.fbmanager.jp/fb/jal/';

// exec once
if (window.addEventListener) { //for W3C DOM
  window.addEventListener("load", initialize, false);
} else if (window.attachEvent) { //for IE
  window.attachEvent("onload", initialize);
} else  {
  window.onload = initialize;
}

// DOMアクセス環境を整える
function cleanWhitespace(element) {
    element = element || document;
    var cur = element.firstChild;
    
    while(cur != null) {
        if (cur.nodeType == 3 && !/\S/.test(cur.nodeValue)) {
            element.removeChild(cur);
        }
        else if (cur.nodeType == 1) {
            cleanWhitespace(cur);
        }
        
        cur = cur.nextSibling;
    }
}

function initialize(){
	// add css
	var link = document.createElement('link');
	link.rel   = 'stylesheet';
	link.type  = 'text/css';
	link.href  = FB_URL + 'style.css';
	link.media = 'all';
	var head = document.getElementsByTagName('head')[0];
	head.appendChild(link);
	// make form
	var elements = document.getElementsByTagName('div');
	for (var i = 0; i < elements.length; i++ ) {
		if( elements[i].className == 'fb' ) {
			elements[i].id = 'fb-' + i;
            cleanWhitespace(elements[i]);
            var p = elements[i].firstChild;
            cleanWhitespace(p);         
            var a = p.firstChild;
            cleanWhitespace(a.parentNode);
            var fb_url_id = a.href;

if(typeof log_data != "undefined" && log_data.session) fb_url_id+= '?session='+log_data.session;
if(typeof log_data != "undefined" && log_data.session) 
			if( a.style.background ){
				var background = a.style.background;
				background = 'background:'+ background +' ! important';
			}
			else{
				background = '';
			}
			var inner_text = a.innerHTML;
			inner_html = '<p><a href="'+ fb_url_id +'" target="_blank" onClick="fbButton(this);return false;" onMouseOver="fbRollOver(this)" onMouseOut="fbRollOut(this)">'+ inner_text +'</a></p>';
			inner_html+= '<iframe src="'+ fb_url_id +'" class="hidden" height="0" width="0" frameborder="0">';
			inner_html+= ' <p><a href="'+ fb_url_id +'">CLICK HERE !!</a></p>';
			inner_html+= '</iframe>';
			elements[i].innerHTML = inner_html;
		}
	}
}

// button click
function fbButton(a){
    cleanWhitespace(a.parentNode);
	if( iframe = a.parentNode.parentNode.getElementsByTagName('iframe')[0] ){
		if( iframe.className == 'show' ){
			iframe.className = 'hidden';
			iframe.width  = 0;
			iframe.height = 0;
			if( ( a.firstChild.firstChild ) && a.firstChild.firstChild.src ){
				var img = a.firstChild.firstChild;
				img.src = img.src.replace( /__c/, '__o' );
				img.alt = a.lastChild.innerHTML;
				var span = a.lastChild;
				var text_open  = span.innerHTML;
				var text_close = img.alt;
				img.alt        = text_open;
				span.innerHTML = text_close;
			}
			else{
				var text_open  = a.firstChild.innerHTML;
				var text_close = a.lastChild.innerHTML;
				a.firstChild.innerHTML = text_close;
				a.lastChild.innerHTML  = text_open;
			}
		}
		else if( iframe.className == 'hidden' ){
			iframe.className = 'show';
			iframe.width  = 400;
			iframe.height = 300;
			if( ( a.firstChild.firstChild ) && a.firstChild.firstChild.src ){
				var img  = a.firstChild.firstChild;
				img.src  = img.src.replace( /__o/, '__c' );
				var span = a.lastChild;
				var text_open  = img.alt;
				var text_close = span.innerHTML;
				img.alt        = text_close;
				span.innerHTML = text_open;
			}
			else{
				var text_open  = a.firstChild.innerHTML;
				var text_close = a.lastChild.innerHTML;
				a.firstChild.innerHTML = text_close;
				a.lastChild.innerHTML  = text_open;
			}
		}
	}
}
// roll over
function fbRollOver(a){
	if( ( a.firstChild.firstChild ) && a.firstChild.firstChild.src ){
		var img = a.firstChild.firstChild;
		img.src = img.src.replace( /__n/, '__r' );
	}
	else if( a.firstChild.className ){
		var span = a.firstChild;
		span.className = 'over';
	}
}
// roll out
function fbRollOut(a){
	if( ( a.firstChild.firstChild ) && a.firstChild.firstChild.src ){
		var img = a.firstChild.firstChild;
		img.src = img.src.replace( /__r/, '__n' );
	}
	else if( a.firstChild.className ){
		var span = a.firstChild;
		span.className = 'out';
	}
}
// popup
function fbPopup(url, width, height){
	var switches = {
		'menubar':    'no',
		'toolbar':    'no',
		'location':   'no',
		'status':     'no',
		'scrollbars': 'no',
		'resizable':  'no'
	}
	if(3 < arguments.length){
		for(i = 3; i<arguments.length; i++){
			switches[arguments[i]] = 'yes';
		}
	}
	var parameters = 
		'width      = '+width+','+
		'height     = '+height+','+
		'menubar    = '+switches['menubar']+','+
		'toolbar    = '+switches['toolbar']+','+
		'location   = '+switches['location']+','+
		'status     = '+switches['status']+','+
		'scrollbars = '+switches['scrollbars']+','+
		'resizable  = '+switches['resizable']
	;
	window.open(
		url,
		'_blank',
		parameters
	);
}

