if( typeof( console ) == "undefined" ) {
    console = {
        log : function( i_arg ){}
    }
}

$( function(){

    $( "[class^='popop']" ).each( function(){
    
        var pop = new $.PoPop( $( this ) );
    
    } );
    
    // お問い合わせ送信
    $( ".jContactSubmitButton" ).live("click", function(){
        var form = $(this).parent().parent("form");
        var url = form.attr("action");
        var postData = {};
        postData["fullname"] = form.find("input[name='fullname']").val();
        postData["mailaddress"] = form.find("input[name='mailaddress']").val();
        postData["contact"] = form.find("textarea[name='contact']").val();
        
        $.ajax({
            type:'POST',
            url:url,
            dataType:'json',
            data:postData,
            success:function(data, dataType) {
                if (data.ErrorCode == 0) {
                    // 正常
                    $(".popop-3-okSelector-6-child").click();
                    
                    return false;
                } else {
                    // 異常
                    var errorMessage = data.ErrorMessage;

                    $(".popop-2-errorSelector-5-child").click();
                    $(".jAttentionBlockMessage p").text(errorMessage);
                    
                    return false;
                }
            },
            error:function(reqest, status, err) {
                GGOLF.fullCoverLoadView.hide();
            }
        });

        return false;
    });

} );

$.PoPop = function( $i_target ) {
    
    this.isFirst = true;
    this.isShow = false;
    this.fixEnable = true;
    this.generation;
    this.width;
    this.height;
    this.maxY;
    this.minY = 0;
    
    this.childrens = [];
    
    this.$target = $i_target;
    this.$templete;
    this.timer;

    //initialize
    this._init.apply( this );

};
$.PoPop.prototype = {

    /*------------------------------------------------------
    * initialyze
    ------------------------------------------------------*/
    _init : function(){
    
        if( this.$target.parents( "#PopopTemplete" ).length ) {
            return;
        }
    
        var data = this.$target.attr( "class" );
        data = data.match( /(popop.+?)(\s|$)/ )[1];
        data = data.split( "-" );
        
        //外枠のテンプレート取得
        var templeteIndex = data[1];
        this.$templete = $( $.PoPop.TEMPLETE[ templeteIndex ] );
        
        //内枠のテンプレート取得
        templeteIndex = data[2];
        var $templete = $( "#PopopTemplete" ).children( "." + templeteIndex );
        var templete = $templete.html().replace( /\t/g, "" ).replace( /\n/g, "" );
        this.$templete.find( ".popBody" ).append( templete );
        
        //世代…　ポップが表示されている状態で別の同一世代を表示させた場合、もとのポップは非表示に。
        this.generation = data[4];
        
        //インデックス設定
        this.$templete.css( "zIndex", $.PoPop.Z_INDEX + Number( data[3] ) );
        
        this.$templete.find( ".popCloseBtn" ).bind( "click", $.proxy( this._onCloseBtnClick, this ) );
        this.$target.bind( "click", $.proxy( this._onClick, this ) );
    
    },
    /*------------------------------------------------------
    * on click
    ------------------------------------------------------*/
    _onClick : function( i_event ) {
    
        if( this.isShow ) {
            return false;
        }
        this.isShow = true;
        
        //初めての時
        if( this.isFirst ) {
        
            this.isFirst = false;
            $( "body" ).append( this.$templete );
            
            //テンプレートの幅、高さ取得
            this.width = this.$templete.width();
            this.height = this.$templete.height();
            
            //Bodyの高さ
            var bh = $( "body" ).height();
            this.maxY = bh - this.height;
            //Bodyの高さがポップアップより低い場合
            if( this.maxY < 0 ) {
                this.fixEnable = false;
            }
            
            var self = this;
            this.$templete.find( "[class^='popop']" ).each( function(){
                self.childrens.push( new $.PoPop( $( this ) ) );
            } );
                        
        }
        //タイマー
        if( !this.timer ) {
            this._onEnterFrame();
            this.timer = setInterval( $.proxy( this._onEnterFrame, this ), 50 );
        }
        
        //同世代のポップを非表示に
        if( $.PoPop.SHOW[this.generation] ) {
            $.PoPop.SHOW[this.generation].hide();
        }
        $.PoPop.SHOW[this.generation] = this;
        //表示
        this.$templete.show();
        
        return false;
    },
    
    /*------------------------------------------------------
    * on close btn click
    ------------------------------------------------------*/
    _onCloseBtnClick : function( i_event ) {
        this.isShow = false;
        if( this.timer ) {
            clearInterval( this.timer );
            this.timer = null;
        }
        
        var l = this.childrens.length;
        for( var i = 0; i<l; i++ ) {
            this.childrens[i].hide();
        }
        
        // 閉じる際に入力内容を消します。
        this.$templete.find("input,textarea").val("");
        this.$templete.hide();
        
        return false;
    },
    
    /*------------------------------------------------------
    * hide
    ------------------------------------------------------*/
    hide : function() {
        this._onCloseBtnClick();
    },
    
    /*------------------------------------------------------
    * on enterframe
    ------------------------------------------------------*/
    _onEnterFrame : function() {
    
        
        var sh = $.PoPop.$window.height();
        var sw = $.PoPop.$window.width();
        var x = ( sw - this.width ) / 2;
        var y = ( sh - this.height ) / 2;
        
        //最小より低いとき
        if( y < this.minY ) {
            y = this.minY;
        }
        
        //positionfix機能付き
        if( this.fixEnable ) {
        
            y += $.PoPop.$window.scrollTop();
            if( y > this.maxY ) {
                y = this.maxY;
            }
            
        }
        
        this.$templete.css( { "left" : x + "px", "top" : y + "px"  } );
    }
    
    
};

$.PoPop.Z_INDEX = 800;
$.PoPop.$window = $( window );
$.PoPop.SHOW = {};
$.PoPop.TEMPLETE = {
    "1" : '<div class="pop1"><div class="pop1Header"><div class="pop1Footer"><div class="pop1Body"><div class="closeBtnBlock"><a href="#" class="popCloseBtn"><img src="content/images/lb-close.gif" alt="lb-close" width="90" height="22" /></a></div><div class="popBody"></div></div></div></div></div>',
    "2" : '<div class="pop2"><div class="pop2Header"><div class="pop2Footer"><div class="pop2Body"><div class="closeBtnBlock"><a href="#" class="popCloseBtn"><img src="content/images/lb-close.gif" alt="lb-close" width="90" height="22" /></a></div><div class="popBody"></div></div></div></div></div>',
    "3" : '<div class="pop3"><div class="pop3Header"><div class="pop3Footer"><div class="pop3Body"><div class="closeBtnBlock"><a href="#" class="popCloseBtn"><img src="content/images/lb-close.gif" alt="lb-close" width="90" height="22" /></a></div><div class="popBody"></div></div></div></div></div>'
};




