/*
 * sTEPiT.js
 * Version 0.5.0.0 06/16
 */

var hglf_targetName= 'swfReplace';
var hglf_targetNameVertical= 'vswfReplace';
var hglf_targetNameVerticalMultiColumn= 'mswfReplace';
var hglf_mimeType= 'application/x-shockwave-flash';
var hglf_foundationSwfURL= '/mt01/loader.swf';
var hglf_userAgent= 0;
var hglf_prefixId= "hglf_movie_";
var hglf_MovieId= 0;
var hglf_debug= 0;

function hglf_resizeStage(id,width,height){
    var e= document.getElementById(id);
    if(!e) return;
    //alert( "id: "+id+" width:"+width+" cW:"+e.width+" cpW:"+e.parentNode.style.width+" height:"+height);
    //alert("id: "+id+"\n"+e.width+"x"+e.height+" => "+width+"x"+height);

    e.parentNode.style.overflow= "visible";
    e.width= width;
    e.parentNode.style.width= width;

    if(isMSIE()){
	e.width= width;
	e.parentNode.style.width= width;
	e.height= height;
	e.parentNode.style.height= height;
	/* note e.height should be faster than e.parentNode.style.height! */
    }
    else{
	e.width= width;
	e.parentNode.style.width= width;
	e.height= height;
	e.parentNode.style.height= "auto";
    }

    /* Note: 2006/06/02 sakai@nya.to
       On Firefox, e.parentNode.style.{width,height} seems to be a private data.
       This code still works good though it makes an error message on JavaScriptConsole.
       But this is not the problem, read SWF data TWICE is the problem on Firefox.
       MSIE will read it ONCE. */
}

function isOpera(){    
    var ua= navigator.userAgent;
    if(ua.indexOf("Opera")!=-1)
	return true;
    else
	return false;
}

function isMacIE(){
    var ua= navigator.userAgent;
    if(ua.indexOf("MSIE")!=-1 && ua.indexOf("Mac")!=-1)
	return true;
    else
	return false;
}

function isMSIE(){
    var ua= navigator.userAgent;
    if(ua.indexOf("MSIE")!=-1)
	return true;
    else
	return false;
}

function hglf_checkUserAgent(){
    if(hglf_userAgent) return hglf_userAgent;
    var ua= navigator.userAgent;
    if(ua.indexOf("MSIE")!=-1&&ua.indexOf("Windows")!=-1)
	hglf_userAgent= 1; /* MSIE on Windows */
    else if(ua.indexOf("Firefox")!=-1)
	hglf_userAgent= 2; /* Firefox */
    else if(ua.indexOf("MSIE")!=-1&&ua.indexOf("Mac")!=-1)
	hglf_userAgent= 3;
    else
	hglf_userAgent= 3;
    return hglf_userAgent;
}

function hglf_checkFlashPlayer(){
}

function hglf_makeNewInnerHTMLString(id,width,height)
    /* int id, String fontFamily, int fontSize, int width, int height */
{
    if(hglf_checkUserAgent()==1){
	return '<object id="'+id+'" type="'+hglf_mimeType+'" width="'+width+'" height="'+height+'">'
	    + '<param name="movie" value="' + hglf_foundationSwfURL + '" />'
	    + '<param name="quality" value="high" />'
	    + '<param name="wmode" value="transparent" />'
	    + '</object>';
    }
    else{
	return '<object id="'+id+'" data="' + hglf_foundationSwfURL + '" swfLiveConnect=true '
	    + 'type="'+hglf_mimeType+'" width="'+width+'" height="'+height+'">'
	    + '<param name="quality" value="high" />'
	    + '<param name="wmode" value="transparent" />'
	    + '</object>';
    }
}

var hglf_targetAttrName= 'stepit';

function hglf_getRootTags(){
    var es= document.getElementsByTagName('*');
    var es_roots= new Array();
    var str= "";
    for(var i=0; i<es.length; i++){
	if( !(es[i].getAttribute(hglf_targetAttrName)) )
	    continue;
	var e0= es[i];
	var e= e0;
	while(e.parentNode){
	    e= e.parentNode;
	    if( e.getAttribute && e.getAttribute(hglf_targetAttrName) ){
		e0= null;
		break;
	    }
	}
	if(e0){
	    es_roots[es_roots.length]= e0;
	    str += e0.tagName + " [ " + e0.getAttribute(hglf_targetAttrName) + " ] / ";
	}
    }
    //alert(str);
    return es_roots;
}

function hglf_getRootTags_01(targetName){
    var es= new Array();
    var es_roots= new Array();
    var ua= navigator.userAgent;
    if(document.all){
	/* MSIE & Opera */
	for(var i=0; i<document.all.length; i++){
	    //if(document.all[i].name==hglf_targetName) /* Opera has no property. */
	    if(document.all[i].getAttribute('name')==targetName)
		es[es.length]= document.all[i];
	}
    }else{
	es= document.getElementsByName(targetName);
    }

    for(var i=0; i<es.length; i++){
	var e0= es[i];
	var e= e0;
	while(e.parentNode){
	    e= e.parentNode;
	    if(e.getAttribute && e.getAttribute("name")==targetName){
		e0= null;
		break;
	    }
	}
	if(e0) es_roots[es_roots.length]= e0;
    }
    return es_roots;
}

function hglf_createSwf(e,id,swf,text,width,height,trans){
    if(!e) return;
    var ce;
    var ua= navigator.userAgent;
    //if(document.all){  /* Opera has document.all object. */
    if(ua.indexOf('MSIE')!=-1 && !(ua.indexOf('Opera')!=-1)){
        ce= document.createElement('em');
    }else{
        ce= document.createElement('object');
    }

    var param1= document.createElement('param');
    param1.setAttribute('name','movie');
    param1.setAttribute('value',swf);
    ce.appendChild(param1);
    var param2= document.createElement('param');
    param2.setAttribute('name','FlashVars');
    param2.setAttribute('value',text);
    ce.appendChild(param2);
    if(trans=="on"){
	var param3= document.createElement('param');
	param3.setAttribute('name', 'wmode');
	param3.setAttribute('value','transparent');
	ce.appendChild(param3);
    }

    //ce.setAttribute('id','movie_2');
    //ce.setAttribute('swfLiveConnect','true'); // No need?
    ce.setAttribute('type','application/x-shockwave-flash');
    ce.setAttribute('data',swf);
    ce.setAttribute('width',width);
    ce.setAttribute('height',height);
    ce.setAttribute('id',id);
    e.appendChild(ce);
    if(ua.indexOf('MSIE')!=-1)
	ce.focus();

    /* note focus() do not work on Mozilla.
     * but it is not necesery because Mozilla got foucs!
     */

    if(ua.indexOf('MSIE')!=-1 && !(ua.indexOf('Opera')!=-1)){
        e.innerHTML= e.innerHTML.replace(/(<\/?)em/ig,'$1object');
    }
}


function hglf_getModeFromTargetAttr(e){
    if(!e || !e.getAttribute)
	return ;
    var s= e.getAttribute(hglf_targetAttrName);
    if(!s)
	return  "hori";
    if(s.match(/mode\s*:\s*h/i)) return "hori";
    if(s.match(/mode\s*:\s*v/i)) return "vert";
    if(s.match(/mode\s*:\s*m/i)) return "multi";
    if(s.match(/^vert/i)) return "vert";
    if(s.match(/^multi/i)) return "multi";
    if(s.match(/^off\s*$/i)) return null;
    return "hori";
}

var hglf_limitLength= 400*3 * 1.2;
var hglf_limitStages= 20;

function hglf_replace_core(target,mode) {
    var es= hglf_getRootTags(target);
    var fontFamily, fontSize, width, height, backgroundImage, backgroundColor;
    var swfStageNum= 0;
    for(var i=0; i<es.length; i++){
	mode = hglf_getModeFromTargetAttr(es[i]);
	if(!mode) continue;

	var id= hglf_prefixId + i.toString();
	var text= es[i].innerHTML;
	var trans= es[i].getAttribute('trans');

	if(es[i].innerHTML.length>hglf_limitLength){
	    alert("文字数制限オーバーです！");
	    continue;
	}

	if(es[i].currentStyle){
	    // MSIE
	    fontFamily= es[i].currentStyle.fontFamily;
	    fontSize= es[i].currentStyle.fontSize;
	    width= es[i].offsetWidth;
	    height= es[i].offsetHeight;
	    backgroundImage= es[i].currentStyle.backgroundImage;
	    backgroundColor= es[i].currentStyle.backgroundColor;
	}
	else if(typeof(getComputedStyle)=='function') {

	    if(isOpera()){
		var str='';
		str= es[i].style.fontFamily;
		str= es[i].getAttribute('style');
		//alert(str);
	    }

	    // mozilla and Opera
	    var style= getComputedStyle(es[i],null);
	    //fontFamily= style.fontFamily;
	    fontFamily= style.getPropertyValue("font-family");
	    /* both are OK. but Opera dosen't correct font-family. */
	    fontSize= style.fontSize;
	    width= style.width;
	    height= style.height;
	    backgroundImage= style.backgroundImage;
	    backgroundColor= style.backgroundColor;
	}
	else if(typeof(document.defaultView.getComputedStyle)=='function'){
	    /* Safari */
	    var style= document.defaultView.getComputedStyle(es[i], null);
	    fontFamily= style.getPropertyValue("font-family");
	    fontSize= style.fontSize;
	    width= style.width;
	    height= style.height;
	    backgroundImage= style.backgroundImage;
	    backgroundColor= style.backgroundColor;
	}
	else {
	    alert("else section");
	    ;
	}

	if(hglf_debug){
	    alert('font-family['+fontFamily+"]\nfont-size["+fontSize+"]\n"
		  + "width["+width+"]\nheight["+height+"]"
		  + "\n backgroundImage[" + backgroundImage + "]"
		  + "\n backgroundColor[" + backgroundColor + "]"
		  + "\n innerHTML[" + text + "]"
		  );
	}
	if( !isMacIE() && (width && width!="auto")
	    && (height && height!="auto") && fontSize && text ){
	    
	    if(++swfStageNum>hglf_limitStages)
		return;

	    /* When couldn't get fontFamily property,
	     *  use fall-back fonts like IPA */
	    es[i].innerHTML= "";
	    text= text.replace(/&/g,"__amp__");
	    text= text.replace(/\+/g,"__plus__");
	    hglf_MovieId++;
	    var id= hglf_prefixId + hglf_MovieId;
	    hglf_createSwf(es[i],id,hglf_foundationSwfURL,
			   'hglf_text='+text
			   +'&hglf_id='+id
			   +'&hglf_fontSize='+fontSize
			   +'&hglf_fontFamily='+fontFamily
			   +'&hglf_width='+width
			   +'&hglf_height='+height
			   +'&hglf_href='+location.href
			   +'&hglf_pathname='+location.pathname
			   +'&hglf_backgroundImage='+backgroundImage
			   +'&hglf_backgroundColor='+backgroundColor
			   +'&hglf_mode='+mode,
			   width,height,trans);
	}
    }
}

function hglf_replace(){
    //hglf_replaceVertical();
    //hglf_replaceHorizontal();
    hglf_replace_core(hglf_targetName,'hori');
    //hglf_replace_core(hglf_targetNameVertical,'vert');
    //hglf_replace_core(hglf_targetNameVerticalMultiColumn,'multi');
}

function hglf_onload(){
    hglf_replace();
}

// Set function hglf_replace() as the handler of window.onload().

var hglf_onloadSaveFunc= null;
if(typeof(window.onload)=="function"){
    hglf_onloadSaveFunc= window.onload;
    window.onload=function(){
	var retval= hglf_onloadSaveFunc();
	hglf_onload();
	return retval;
    };
}else{
    window.onload= hglf_onload;
}

/* __END__ */
