﻿var MC = Class.create();

MC.Version = "0.0.1";
MC.CSSPath = "/runtime/mc/styles/brb/";

MC.HoverBlock={};
(function() {
    var HoverBlock = Class.create();
    HoverBlock.prototype = {
	    baseobj: null,
	    obj: null,
	    maskImageStyle: '',
	    ClickFunc: null, 
	    eventLinteners: {
		    mouseover: 'onMouseOver', mouseout: 'onMouseOut', click: 'onMouseClick'
	    },
	    initialize: function(tag, id, mask_image, active_left, active_top, tooltip, click_func) {
		    var maskid=id+'_mask';
		    this.baseobj=document.getElementById(id);
		    Element.update(this.baseobj, '<'+tag+' id=\''+maskid+'\'></+tag+>');
		    this.obj=document.getElementById(maskid);
		    this.obj.style.width=this.baseobj.offsetWidth+'px';
		    this.obj.style.height=this.baseobj.offsetHeight+'px';
		    this.obj.style.backgroundColor='transparent';
		    this.obj.title=tooltip;
		    this.maskImageStyle='url(\''+mask_image+'\')';
		    this.obj.style.backgroundPosition=active_left+'px '+active_top+'px';
		    this.ClickFunc=click_func;
		    $H(this.eventLinteners).each(function(set) {
			    Event.observe(this.obj, set.key, this[set.value].bind(this));
		    }.bind(this));
		    return this.obj;
	    },
	    onMouseOver: function(event) {
		    event = event || window.event;
		    this.obj.style.backgroundImage=this.maskImageStyle;
	    },
	    onMouseOut: function(event) {
		    event = event || window.event;
		    this.obj.style.backgroundImage='';
	    },
	    onMouseClick: function(event) {
		    this.ClickFunc();
	    }
    };
    
    MC.HoverBlock=HoverBlock ;
})()


MC.AppendStyle={};
(function() {
	var stylesheet = document.styleSheets[document.styleSheets.length-1]; 
    var AppendStyle = Class.create();
    AppendStyle=function(Name, Value){
        Try.these(
            function(){
                stylesheet.addRule(Name, Value); //IE
            },
            function(){
                stylesheet.insertRule(Name + "{" + Value + "}", 
                stylesheet.cssRules.length); //MOZ
            }
        );
    };
    
    MC.AppendStyle=AppendStyle;
})()

MC.Mask={};
(function() {
    var Mask = Class.create();
    
	var isVisible = true; 
	var oMask;
	var oMessage;
	var lasticon;
	var lastmessage;
	
    Mask.Init = function(){
	    oMask = $("mcMaskBg");
	    oMessage = $("mcMaskMessage");
	    oMessage.innerHTML = "";
	    oMessage.style.backgroundImage = "";
   };
    
    Mask.Hide=function(){
        if (isVisible){
            oMask.style.display = "none";
            oMessage.style.display = "none";
            isVisible = false;
        }
    };
    
    Mask.Show=function(message, icon){
        if(lastmessage != message){
            oMessage.innerHTML = message;
        }
        if(lasticon != icon){
            if (icon){
                oMessage.style.backgroundImage = "url(" + MC.CSSPath + icon + ")";
            }else{
                icon = ""
                oMessage.style.backgroundImage = "";
            }
            lasticon = icon;
        }
        if (!isVisible){
            oMask.style.display = "block";
            oMessage.style.display = "block";
            isVisible = true;
        }
    };
    
    Mask.SetOpacity = function(percentValue){
        var digValue = percentValue/100;
	    MC.AppendStyle("#mcMaskBg", "filter:alpha(opacity="+percentValue+");-moz-opacity:"+digValue+";opacity:"+digValue+";");
    };
    
    Mask.SetPos=function(LeftPos, TopPos){
        oMask.style.top = TopPos + "px";
        oMask.style.left = LeftPos + "px";
        oMessage.style.top = TopPos + "px";
        oMessage.style.left = LeftPos + "px";
    };
    
    MC.Mask = Mask;
})()

Date.toJapaneseYMDString = function(element){
    try{
        return element.getFullYear() + "年" + 
            (element.getMonth() + 1) + "月" + 
            element.getDate() + "日" ;
    }catch(e){
    }
};

Date.toMCWeekRangeString = function(start, end){
    try{
        var ret;
        var dayms = 24 * 60 * 60 * 1000;
        ret = start.getFullYear() + "年" + 
            (start.getMonth() + 1) + "月" +  
            start.getDate() + "日-" ;
        ret = ret + (end.getMonth() + 1) + "月" + 
            end.getDate() + "日 " ;
        ret = ret + (Math.floor((end.getTime()-start.getTime())/dayms) + 1) + "日間";
        return ret;
    }catch(e){
    }
};

String.insertComma = function(sourceStr){
    try{
        var destStr = sourceStr;
        var tmpStr = "";
        if (sourceStr != null){
            while (destStr != (tmpStr = destStr.replace(/^([+-]?\d+)(\d\d\d)/,"$1,$2"))) {
                destStr = tmpStr;
            }
            return destStr;
        }else{
            return "－";
        }
    }catch(e){
        return sourceStr;
    }
};

MC.XmlHelper={};
(function() {
    var XmlHelper = Class.create();
    
    var oDoc = null;
    var oTargetNode = null;
    
    XmlHelper.setXml = function(XmlDoc, XmlText){

	    try{
	        var oCommonNode = $A(XmlDoc.documentElement.getElementsByTagName("common"))[0];
	        XmlHelper.DataDate = new Date(oCommonNode.getAttribute("date"));
	        try{
	            XmlHelper.FromDate = new Date(oCommonNode.getAttribute("date_from"));
	        }catch(e){
	            XmlHelper.FromDate = null;
	        }
	        XmlHelper.Version = oCommonNode.getAttribute("version");
	        XmlHelper.Format = oCommonNode.getAttribute("format");
            if (window.ActiveXObject){
                oDoc = new ActiveXObject("Microsoft.XMLDOM");
                oDoc.async = false;
                oDoc.loadXML(XmlText);
            }else{
                oDoc = XmlDoc;
            }
	    }catch(e){
	        XmlHelper.DataDate = null;
	        XmlHelper.FromDate = null;
	        XmlHelper.Version = null;
	        XmlHelper.Format = null;
	    }
    };
    XmlHelper.setCurrent = function(code, market){
        var xpath;       //評価するXPath
        if (market){
            xpath = "/brb/data/stock[@code=\"" + code + "\"][@market=\"" + market + "\"]";
        }else{
            xpath = "/brb/data/stock[@code=\"" + code + "\"]";
        }
        oTargetNode = null;
        Try.these(
            function(){
                oDoc.setProperty("SelectionLanguage", "XPath"); 
                oTargetNode = oDoc.documentElement.selectSingleNode(xpath);
                return true;
            },
            function(){
                var type   = XPathResult.FIRST_ORDERED_NODE_TYPE; 
                var resolver  = oDoc.createNSResolver(oDoc.documentElement); 
                var expression= oDoc.createExpression(xpath, resolver); 
                var result = expression.evaluate(oDoc.documentElement, type, null); 
                oTargetNode = result.singleNodeValue;
                return true;
            }
        );
    };
    
    XmlHelper.getValue = function(param){
        if (oTargetNode){
            if (oTargetNode.getAttribute(param) != null){
                return oTargetNode.getAttribute(param);
            }else{
                return null;
            }
        }else{
            return "";
        }
    };
    
    MC.XmlHelper=XmlHelper ;
})()

MC.BGSound={};
(function() {
    var BGSound = Class.create();
    BGSound.prototype = {
	    objid: null,
	    initialize: function(id, filepath) {
	        this.objid = "sound_" + id;
            document.write(
            "<object id=\"" + this.objid + "\" classid=\"clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95\">" + 
            "<param name=\"filename\" value=\"" + filepath + "\">" + 
            "<param name=\"AutoStart\" value=\"false\">" + 
            "<param name=\"Volume\" value=\"-3000\">" + 
            "<param name=\"ShowControls\" value=\"false\">" + 
            "</object>");
	    },
        Play: function(){
            try{
                var mp = $(this.objid);
                mp.CurrentPosition=0;
                mp.play();
            }catch(e){
            }
        }
    };
    MC.BGSound=BGSound ;
})()

