﻿/*
 *  Qcyx JavaScript framework, version 1.0.0
 *  FileName Qcyx.Forms.js
 *  Copyright (c) 2008 Chuxuewen
 *  Date: 2008-09-24
 *  For details, see the Prototype web site: http://www.coonar.com/
 *
 *--------------------------------------------------------------------------*/

Class("Qcyx.forms", {//对话框
    element: null,
    movementLayer: null,
    selectObjs: null,
    width:0,
    height:0,
    forms: function () {
        sfs.page.appStyle([sfs.page.scriptLocation + "css/Qcyx.foems.css"], false);
        this.element = sfs.page.root.select(
        '<div class="Qcyx_forms" >'+
            '<div sid="background" class="Qcyx_forms_background"></div>'+
        '</div>');
        this.background = this.element.select('$sid').background;
    },
    
    init: function(){
        if( this.movementLayer != null) return;
        this.movementLayer = new sfs.ui.movementLayer();
        this.movementLayer.minWidth(150);
        this.movementLayer.minHeight(80);
        this.movementLayer.alpha(100);
        this.movementLayer.element.Style({ overflow: "visible" });
        this.movementLayer.hide();
        var n = select(
        '<div><div style="position:absolute;left:0px; top:0px;width:100%">'
            +'<div class="PopBorTB" ></div>'
            +'<table border="0" cellpadding="0" cellspacing="0" width="100%" sid="table">'
            +'<tr>'
                +'<td class="PopBorLR" ></td>'
                +'<td sid="contentTd" >'
                +'<div sid="top" class="Qcyx_forms_top">'
                    +'<div sid="title" class="Qcyx_forms_title"></div>'
                    +'<div sid="button1" class="Qcyx_forms_button1"></div>'
                +'</div>'
                +'<div sid="content" class="Qcyx_forms_content">'
                    +'<iframe sid="iframe" src="about:blank" style="width:100%;" frameborder="0" scrolling="no"></iframe>'
                +'</div>'
                +'<div sid="bottom" class="Qcyx_forms_bottom">'
                    +'<div sid="button2" class="Qcyx_forms_button2"></div>'
                +'</div>'
                +'</td>'
                +'<td class="PopBorLR" ></td>'
            +'</tr></table>'
            +'<div class="PopBorTB" ></div>'
        +'</div></div>');
        
        extend(this, n.select('$sid'));
        this.elementContent = n.sub(0);
//        this.background1.alpha(40);
        this.movementLayer.element.addSubs(n);
        
        this.element.addSub(this.movementLayer.element);
        //this.movementLayer.show();
        this.background.alpha(35);
        sfs.page.onscroll.add(this.setPlace.bind(this));
        sfs.page.onresize.add( this.releaseSizeAndPosition.bind(this));
        this.movementLayer.onresizeH.add(this.setHeight.bind(this));
        this.movementLayer.onresizeW.add(this.setWidth.bind(this));
        
        this.button1.ainOnClass("Qcyx_forms_button1", "Qcyx_forms_button1_over", "Qcyx_forms_button1_over");
        this.top.ainOnClass("Qcyx_forms_top", "Qcyx_forms_top_over", "Qcyx_forms_top_over");
        this.movementLayer.mousedown.remove("defaultXyAction");
        this.top.addEvent('mousedown', this.movementLayer.action.bindAsEventListener(this.movementLayer, "xy"));
        this.button1.cancelBubble(true);
        this.button1.addEvent('click', this.close.bindAsEventListener(this));
        this.button2.addEvent('mousedown', this.movementLayer.action.bindAsEventListener(this.movementLayer, "rb"));
        this.close();
    },

    open: function (url, title, w, h) {
        if( this.movementLayer == null) this.init();
        this.hideSelect();
        this.width = w;
        this.height = h;
        ronm = Math.random()* 2000 + ( Math.random()*1000) / 3;
        if( url.indexOf('?') == -1){
            url +='?randomNum='+ ronm;
        }else{
            url +='&randomNum='+ ronm;
        }
        this.iframe.src = url;
        this.releaseSizeAndPosition();
        this.movementLayer.show();
        this.element.show();
        this.title.update(title);
    },
    
    /*初始化页面的窗口坐标和背景层*/
    releaseSizeAndPosition: function(){
        w = this.width;
        h = this.height;
        if( w == 0 || h == 0) return;
        doc = sfs.page.element;
        //var size = { width: doc.offsetWidth, height: doc.offsetHeight};
        var size = sfs.page.size();
        this.movementLayer.width(w );
        this.movementLayer.height(h );
//        this.elementContent.Width( w );
        this.elementContent.Height( h );
        
        this.table.Height(  h - 14);
        this.contentTd.Height( h - 14);
        this.content.Height( h - 56);
        
        this.movementLayer.x(size.width / 2 - w / 2 + 7);
        topy = size.height / 2 - h / 2;
        if( topy < 10 ) topy = 10;
        this.movementLayer.y( topy + 7 );
//        this.movementLayer.show();
//        this.element.show();
//        this.background1.style.left = this.background1.offsetLeft +"px";
//        this.background1.style.top = this.background1.offsetTop +"px";
        this.background.show();
        this.background.Width(size.width);
        this.background.Height( size.height);
        this.setPlace();
    },

    close: function (e) {
        if( this.movementLayer == null) this.init();
        this.showSelect();
        this.element.hide();
        this.iframe.src = "about:blank";
    },
    
    /*是否可以手动变换大小*/
    manualSize: function(m){
        this.movementLayer.isSize(m);
    },
    
    /*隐藏所有的select*/
	hideSelect : function(){
	    selects = this.selectObjs == null ? select('select') : this.selectObjs;
	    sArr = [];
		if( sfs.isArray( selects)) {
//			this.selectObjs.length=selects.length;
			for( i=0; i<selects.length; i++){
				so = selects[i];
				so.visibilitySource = so.style.visibility;
				so.style.visibility="hidden";
				sArr.push(so);
			}
		}else if( selects) {
//			this.selectObjs.length=1;
			selects.visibilitySource = selects.style.visibility;
			selects.style.visibility="hidden";
			sArr.push(selects);
		}
		this.selectObjs = sArr;
	},
	/*将所有的select还原*/
	showSelect : function(){
	    if( this.selectObjs == null) return;
	    for(i=0, len = this.selectObjs.length; i<len; i++){
	        so = this.selectObjs[i];
	        so.style.visibility = so.visibilitySource;
	    }
	},

    setPlace: function () {
        if( this.movementLayer == null) this.init();
        this.element.x(sfs.page.element.scrollLeft);
        this.element.y(sfs.page.element.scrollTop);
    },

    setHeight: function (h) {
        if( this.movementLayer == null) this.init();
        this.content.Height(h - 56);
        this.iframe.Height(h - 56);
        this.elementContent.Height( h );
        this.table.Height(  h - 14);
        this.contentTd.Height( h - 14);
        this.height = h;
//        this.background1.Height(h + 12);
    },

    setWidth: function (w) {
        if( this.movementLayer == null) this.init();
        this.elementContent.Width(w );
        this.width = w;
//        this.background1.Width(w + 12);
    }

});

Qcyx.message = new Qcyx.forms();