﻿
Type.registerNamespace("MobileSystem.UI");

MobileSystem.UI.DemoAlignManager = function(){
    MobileSystem.UI.DemoAlignManager.initializeBase(this);
    this._iconMouseDownHandler = null;
    this._iconElmList=null;
    this._parentElm=null;
    this._currentAlignType=MobileSystem.UI.AlignType.NOSET;
    this._alignCallback=null;//Align設定時のコールバック関数
    
    //property
    this.parentElmID="";//位置アイコンを設定する親領域（$create時に指定）
    
};

MobileSystem.UI.DemoAlignManager.prototype ={
    initialize:function() {
        MobileSystem.UI.DemoAlignManager.callBaseMethod(this,'initialize');
        
        this._parentElm = $get(this.parentElmID);
        this._iconElmList = new Array();
        this._initializeAlignPanel();
        
    },
    _initializeAlignPanel :function(){
        //画像領域を作成
//        var left_elm = this._createAlignIcon(MobileSystem.UI.AlignType.LEFT,"./img/shape_align_left.png","左に配置");
//        var center_elm= this._createAlignIcon(MobileSystem.UI.AlignType.CENTER,"./img/shape_align_center.png","中央に配置");
//        var right_elm= this._createAlignIcon(MobileSystem.UI.AlignType.RIGHT,"./img/shape_align_right.png","右に配置");
       
        var left_elm = this._createAlignIcon(MobileSystem.UI.AlignType.LEFT,"./img/icon/shape_align_left.gif","左に配置");
        var center_elm= this._createAlignIcon(MobileSystem.UI.AlignType.CENTER,"./img/icon/shape_align_center.gif","中央に配置");
        var right_elm= this._createAlignIcon(MobileSystem.UI.AlignType.RIGHT,"./img/icon/shape_align_right.gif","右に配置");
        
        this._iconElmList.push(left_elm);
        this._iconElmList.push(center_elm);
        this._iconElmList.push(right_elm);
        
        for(var i=0;i<this._iconElmList.length;i++){
            this._parentElm.appendChild(this._iconElmList[i]);
            if(null==this._iconMouseDownHandler){
                this._iconMouseDownHandler = Function.createDelegate(this, this._onMouseDown);
            }
            //イベントの追加
            //不具合113対応
            //$addHandler(this._iconElmList[i], "mousedown", this._iconMouseDownHandler);
            $addHandler(this._iconElmList[i], "mouseup", this._iconMouseDownHandler);
        }
    },
    /***********Alignアイコン画像の作成処理********************/
    _createAlignIcon :function(id,imgurl,title){
        
        var spelm=document.createElement("span");
        spelm.style.paddingRight="3px";
        spelm.style.paddingLeft="3px";
        
        var elm = document.createElement("img");
        elm.setAttribute("id",id);
        elm.setAttribute("src",imgurl);
        elm.setAttribute("height","24");
        elm.setAttribute("width","24");
        elm.setAttribute("border","0");
        elm.setAttribute("align","texttop");
        elm.setAttribute("title",title);
        elm.className="NonSelectedIcon";
        
        spelm.appendChild(elm);
        return spelm;
    },
    /*****Alignアイコン画像選択時処理******/
    _onMouseDown :function(e){
        var selectIconElm = e.target;
        var selectID = selectIconElm.id;
        if(selectID=="")return;
        if(this._currentAlignType==selectID){
            selectID = MobileSystem.UI.AlignType.NOSET;
        }
        this.setAlign(selectID);
    },
    /******Align情報の設定処理**************/
    setAlign :function(alignType){
        if(this._currentAlignType==alignType){
            return;
        }
        //選択を解除する。
        this.cleareAlignInfo();
        
        var selectElm ="";
        if(alignType==MobileSystem.UI.AlignType.LEFT){
            selectElm = this._iconElmList[0];
        }else if(alignType==MobileSystem.UI.AlignType.CENTER){
            selectElm = this._iconElmList[1];
        }else if(alignType==MobileSystem.UI.AlignType.RIGHT){
            selectElm = this._iconElmList[2];
        }
        if(selectElm!=""){
            //選択されたエレメントの子要素（img）アイコンの背景色を変更
            selectElm.childNodes[0].className = "SelectedIcon";
        }
        this._currentAlignType=alignType;
        if(null!=this._alignCallback){
            this._alignCallback(alignType);
        }
    },
    /*******選択されている位置情報をクリアする***/
    cleareAlignInfo :function(){
        for(var i=0;i<this._iconElmList.length;i++){
            //this._iconElmList[i].childNodes[0].style.backgroundColor="";
            this._iconElmList[i].childNodes[0].className="NonSelectedIcon";
        }
        this._currentAlignType="";
    },
    /*****位置設定時にコールバックする関数の登録処理**/
    seAlignCallback : function(func){
        this._alignCallback = func;
    },
    dispose: function() {
        MobileSystem.UI.DemoAlignManager.callBaseMethod(this, 'dispose');
        
        //イベントの削除
        for(var i=0;i<this._iconElmList.length;i++){
            //不具合113対応
            //$removeHandler(this._iconElmList[i], "mousedown", this._iconMouseDownHandler);
            $removeHandler(this._iconElmList[i], "mouseup", this._iconMouseDownHandler);
        }
    }
};


MobileSystem.UI.DemoAlignManager.registerClass('MobileSystem.UI.DemoAlignManager', Sys.Component);

/*AlignのTypeを定義*/
MobileSystem.UI.AlignType = function(){
    throw Error.invalidOperation();
};
MobileSystem.UI.AlignType.prototype = {
    NOSET:0,
    LEFT: 1,
    CENTER: 2,
    RIGHT: 3
};
MobileSystem.UI.AlignType.registerEnum('MobileSystem.UI.AlignType');


if (typeof(Sys) !== 'undefined')
   Sys.Application.notifyScriptLoaded(); 





