/**
* 多瑪物件
* 菁山遊憩區
*/
var drama;
if (!drama) drama = {};

/**
* 咖啡起始功能
*/
drama.coffeeInit = function() {
    // menu按鈕
    $('a.coffee1').live('click', function() {
        $('div#contentAJAX2').hide();
        $.ajax({
            url: 'coffee_menu1.htm',
            cache: false,
            success: function(data) {
                $('div#contentAJAX2').html(data).fadeIn('slow');
                drama.setCorner();
                drama.coffeeMenuSwitch(); // 咖啡頁面選單切換
             }
        });
        return false;
    });
    
    // back按鈕
    $('a#btnBack').live('click', function() {
        $('div#contentAJAX2').hide();
        $('div#contentAJAX2').load('coffee_menu.htm', function() {
            $(this).fadeIn('slow');
            drama.setCorner();
        });
        return false;
    });
}

/**
* 咖啡頁面選單切換
*/
drama.coffeeMenuSwitch = function() {
    $('#jumpMenu').change(function() {
        $('div#contentAJAX2').hide();
        var page = $('#jumpMenu option').index($('#jumpMenu option:selected'))+1; // 取得頁數
        $('div#contentAJAX2').load('coffee_menu' + page + '.htm', function() {
            $(this).fadeIn('slow');
            drama.setCorner();
            drama.coffeeMenuSwitch();
        });
    });
}

/**
* 首頁功能起始
*/
drama.indexInit = function() {
    // 面板拖曳功能
    $('div#draggable').draggable({
        handle: 'h3',
        containment: 'div#wrap'
    }).find('h3, img#dragMap').css({cursor: 'move'});
    
    // 列印優惠卷
    $('a.lbBn').live('click', function() {
        $('div#myPrintArea').printArea();
        return false;
    });
    
    // 展開關閉功能
    $('.closePanel').collapsePanel();
    
    // 地圖拖曳功能
    $('#dragMap').draggable({
        stop: function() {
			var top = $('#dragMap').position().top;
			var left = $('#dragMap').position().left
			var topLimit = -355;
			var leftLimit = -304;
            if (top > 0) {
                $('#dragMap').animate({top: 0}, 'fast');
            }
			if (left > 0) {
				$('#dragMap').animate({left: 0}, 'fast');
			}
			if (top < topLimit) {
                $('#dragMap').animate({top: topLimit}, 'fast');
			}
			if (left < leftLimit) {
				$('#dragMap').animate({left: leftLimit}, 'fast');
			}
        }
    });
    
    $("a.news, a[name='event']").click(function(){
        var id = $(this).attr('id');
        $.ajax({
            url: 'ajax_news.php',
            cache: false,
            type: 'get',
            data: {id: id},
            success: function(data) {
                var res = $(data).filter('div#res').html();
                $('div.contentScroll').html(res);
                centerPopup();
                loadPopup();                
            }
        });

        return false;
    });
    
    $("#popupContactClose").click(function(){
        disablePopup();
        return false;
    });
    $("#backgroundPopup").click(function(){
        disablePopup();
        return false;
    });
    
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });
    
    var popupStatus = 0;
    
    //loading popup with jQuery magic!
    function loadPopup(){
        //loads popup only if it is disabled
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#popupContact").fadeIn("slow");
            popupStatus = 1;
        }
    }

    //disabling popup with jQuery magic!
    function disablePopup(){
        //disables popup only if it is enabled
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContact").fadeOut("slow");
            popupStatus = 0;
        }
    }
    
    //centering popup
    function centerPopup(){
        //request data for centering
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $("#popupContact").height();
        var popupWidth = $("#popupContact").width();
        //centering
        $("#popupContact").css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
        });
        //only need force for IE6
        
        $("#backgroundPopup").css({
            "height": windowHeight
        });
        
    }
}

/**
* 小木屋頁面起始功能
*/
drama.blockhouseInit = function() {  
    // booking按鈕
    drama.bookingButton();
    
    // 房價詳細說明展開
    drama.showPrice();
}

/**
* booking按鈕
*/
drama.bookingButton = function() {
    $('a.bn').click(function() {
        var type = $(this).attr('type');
        $('div#contentAJAX').hide().html('');
        $.ajax({
            url: 'blockhouse2.php',
            type: 'post',
            cache: false,
            data: {type: type},
            success: function(data) {
                $('div#contentAJAX')
                    .html(data)
                    .fadeIn('slow');
                    drama.setCorner();
                    // 定義step1
                    drama.roomStep1();
            }
        });
        return false;
    });
}

/**
* step 1
*/
drama.roomStep1 = function() {
    // 日期按鈕
    $('img[name="calendarIcon"]').each(function() {
        $(this).css('cursor', 'pointer');
        $(this).click(function() {
            var index = $('img[name="calendarIcon"]').index(this);
            theDate = $('input[name="date"]').eq(index);
            $('div.calendar').hide().eq(index).show();
        });
    });
    
    // 上一步按鈕
    $('a.bookingMain').click(function() {
        $('div#contentAJAX').hide().html('');
        $.ajax({
            url: 'blockhouse1.php',
            cache: false,
            success: function(data) {
                $('div#contentAJAX')
                    .html(data)
                    .fadeIn('slow');
                    drama.setCorner();
                    // booking按鈕
                    drama.bookingButton();    
                    // 房價詳細說明展開
                    drama.showPrice();
            }
        });
        return false;
    });
    
    // 下一步按鈕
    $('a.Step2').click(function() {
        var d1 = $('#date1').val(),
            d2 = $('#date2').val(),
            dc1 = new Date(d1.replace(/-/g, '/')),
            dc2 = new Date(d2.replace(/-/g, '/')),
            today = new Date($('input#today').val()),
            dString = 'YYYY-MM-DD',
            people = $('#people').val(),
            type = $('#type').val();

        // 檢查最近天數
        if (dc1 <= today) {
            alert('請選擇大於今天的日期');
            return false;
        }
        
        // 檢查天數
        var dateDuration = (dc1-today)/(3600*24*1000);
        if (dateDuration < 3) {
            alert('最近可訂房日期為3天');
            return false;
        }
            
        // 檢查有無選擇日期
        if (d1 == dString || d2 == dString) {
            alert('請選擇正確日期');
            return false;
        }
        
        // 檢查日期是否相等
        if (d1 == d2) {
            alert('預約日期與退房日期相等');
            return false;
        }
        
        // 檢查時間是否正確
        if (dc2 < dc1) {
            alert('退房日期不能小於預約日期');
            return false;
        }
        
        $('div#contentAJAX').hide();
        $.ajax({
            url: 'blockhouse3.php',
            type: 'post',
            cache: false,
            data: {type: type, people: people, date1: d1, date2: d2},
            success: function(data) {
                $('div#contentAJAX').html(data).fadeIn();
                drama.roomStep2();
                drama.setCorner();
            }
        });
        
        return false;
    });
}

/**
* Step 2
*/
drama.roomStep2 = function() {
    $('#name').focus();
    // 下一步
    $('a.Step3').click(function() {
        if (!drama.checkValue('#name', '請填寫訂房者姓名')) return false;
        if (!drama.checkValue('#id', '請填寫身份證字號')) return false;
        if (!check_tw_id('id')) return false;
        if (!drama.checkValue('#area', '請填寫市話區碼')) return false;
        if (!drama.checkValue('#phone', '請填寫市話號碼')) return false;
        if (!drama.checkValue('#cell', '請填寫手機')) return false;
        if (!drama.checkValue('#email', '請填寫電子信箱')) return false;
        if (!drama.checkValue('#address', '請填寫聯絡住址')) return false;
        if (!checkEmail('email')) return false;
        
        var type = $('#type').val(),
            people = $('#people').val(),
            date1 = $('#date1').val(),
            date2 = $('#date2').val(),
            name = $('#name').val(),
            id = $('#id').val(),
            area = $('#area').val(),
            phone = $('#phone').val(),
            cell = $('#cell').val(),
            email = $('#email').val(),
            year = $('#year').val(),
            month = $('#month').val(),
            day = $('#day').val(),
            address = $('#address').val(),
            contactDay = $('#contactDay').val(),
            contactTime = $('#contactTime').val();
            
        $('div#contentAJAX').html('').hide();
        $.ajax({
            url: 'blockhouse4.php',
            cache: false,
            type: 'post',
            data: {type: type, people: people, date1: date1, date2: date2, name: name, id: id, area: area, phone: phone, cell: cell, email: email, year: year, month: month, day: day, address: address, contactDay: contactDay, contactTime: contactTime},
            success: function(data) {
                $('div#contentAJAX').html(data).fadeIn('slow');
                drama.setCorner();
                drama.roomStep3();
            }
        });
        
        return false;
    });
    
    // 上一步
    $('a.Step1').click(function() {
        var type = $('#type').val(),
            people = $('#people').val(),
            date1 = $('#date1').val(),
            date2 = $('#date2').val();
            
        $('div#contentAJAX').html('').hide();
        $.ajax({
            url: 'blockhouse2.php',
            type: 'post',
            cache: false,
            data: {type: type, date1: date1, date2: date2, people: people},
            success: function(data) {
                $('div#contentAJAX').html(data).fadeIn('slow');
                drama.setCorner();
                // 定義step1
                drama.roomStep1();
            }
        });
        return false;
    });
}

/**
* Step 3
*/
drama.roomStep3 = function() {
    // 確認送出
    $('a.confirm').click(function() {
        var type = $('#type').val(),
            people = $('#people').val(),
            date1 = $('#date1').val(),
            date2 = $('#date2').val(),
            name = $('#name').val(),
            id = $('#id').val(),
            area = $('#area').val(),
            phone = $('#phone').val(),
            cell = $('#cell').val(),
            email = $('#email').val(),
            year = $('#year').val(),
            month = $('#month').val(),
            day = $('#day').val(),
            address = $('#address').val(),
            contactDay = $('#contactDay').val(),
            contactTime = $('#contactTime').val(),
            total = $('#total').val();
            
        $('div#contentAJAX').hide();
        $.ajax({
            url: 'ajax_add_book.php',
            type: 'post',
            cache: false,
            data: {type: type, people: people, date1: date1, date2: date2, name: name, id: id, area: area, phone: phone, cell: cell, email: email, year: year, month: month, day: day, address: address, contactDay: contactDay, contactTime: contactTime, total: total},
            success: function(data) {
                if (data == 1) {
                    $('div#contentAJAX').html('');
                    $.ajax({
                        url: 'blockhouse5.php',
                        type: 'post',
                        cache: false,
                        data: {total: total},
                        success: function(data) {
                            $('div#contentAJAX').html(data).fadeIn('slow');
                            drama.setCorner();
                            // 回首頁按鈕
                            $('a.bookingMain').click(function() {
                                $('div#contentAJAX').hide().html('');
                                $.ajax({
                                    url: 'blockhouse1.php',
                                    cache: false,
                                    success: function(data) {
                                        $('div#contentAJAX')
                                            .html(data)
                                            .fadeIn('slow');
                                            drama.setCorner();
                                            // booking按鈕
                                            drama.bookingButton();    
                                            // 房價詳細說明展開
                                            drama.showPrice();
                                    }
                                });
                                return false;
                            });
                        }
                    });
                } else {
                    alert(data);
                    $('div#contentAJAX').fadeIn();
                }
            }
        });
        return false;
    });
    
    // 上一步
    $('a.Step2').click(function() {
        var type = $('#type').val(),
            people = $('#people').val(),
            date1 = $('#date1').val(),
            date2 = $('#date2').val(),
            name = $('#name').val(),
            id = $('#id').val(),
            area = $('#area').val(),
            phone = $('#phone').val(),
            cell = $('#cell').val(),
            email = $('#email').val(),
            year = $('#year').val(),
            month = $('#month').val(),
            day = $('#day').val(),
            address = $('#address').val(),
            contactDay = $('#contactDay').val(),
            contactTime = $('#contactTime').val();
            
        $('div#contentAJAX').hide();
        $.ajax({
            url: 'blockhouse3.php',
            type: 'post',
            cache: false,
            data: {type: type, people: people, date1: date1, date2: date2, name: name, id: id, area: area, phone: phone, cell: cell, email: email, year: year, month: month, day: day, address: address, contactDay: contactDay, contactTime: contactTime},
            success: function(data) {
                $('div#contentAJAX').html(data).fadeIn();
                drama.roomStep2();
                drama.setCorner();
            }
        });
        
        return false;
    });
}

/**
* 檢查空值
* obj - 物件
* str - 字串
*/
drama.checkValue = function(obj, str) {
    if ($.trim($(obj).val()) == '') {
        alert(str);
        $(obj).focus();
        return false;
    } else {
        return true;
    }
}

/**
* email 驗證
*/
function checkEmail(obj) {
    var re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (!re.test(document.getElementById(obj).value)) {
        alert('電子信箱格式錯誤');
        return false;
    } else {
        return true;
    }
}

/**
* 身份證驗證
*/
function check_tw_id(id) {
    var LegalID = "0123456789"
    var fResult=true;
    var sId = document.getElementById(id).value;
    if(sId.length < 10) {
        alert('身份證字串長度錯誤');
        return false;
    } else {
        if((sId.charAt(0)=='A') || (sId.charAt(0)=='a')) value=10
        else if((sId.charAt(0)=='B') || (sId.charAt(0)=='b')) value=11
        else if((sId.charAt(0)=='C') || (sId.charAt(0)=='c')) value=12
        else if((sId.charAt(0)=='D') || (sId.charAt(0)=='d')) value=13
        else if((sId.charAt(0)=='E') || (sId.charAt(0)=='e')) value=14
        else if((sId.charAt(0)=='F') || (sId.charAt(0)=='f')) value=15
        else if((sId.charAt(0)=='G') || (sId.charAt(0)=='g')) value=16
        else if((sId.charAt(0)=='H') || (sId.charAt(0)=='h')) value=17
        else if((sId.charAt(0)=='J') || (sId.charAt(0)=='j')) value=18
        else if((sId.charAt(0)=='K') || (sId.charAt(0)=='k')) value=19
        else if((sId.charAt(0)=='L') || (sId.charAt(0)=='l')) value=20
        else if((sId.charAt(0)=='M') || (sId.charAt(0)=='m')) value=21
        else if((sId.charAt(0)=='N') || (sId.charAt(0)=='n')) value=22
        else if((sId.charAt(0)=='P') || (sId.charAt(0)=='p')) value=23
        else if((sId.charAt(0)=='Q') || (sId.charAt(0)=='q')) value=24
        else if((sId.charAt(0)=='R') || (sId.charAt(0)=='r')) value=25
        else if((sId.charAt(0)=='S') || (sId.charAt(0)=='s')) value=26
        else if((sId.charAt(0)=='T') || (sId.charAt(0)=='t')) value=27
        else if((sId.charAt(0)=='U') || (sId.charAt(0)=='u')) value=28
        else if((sId.charAt(0)=='V') || (sId.charAt(0)=='v')) value=29
        else if((sId.charAt(0)=='X') || (sId.charAt(0)=='x')) value=30
        else if((sId.charAt(0)=='Y') || (sId.charAt(0)=='y')) value=31
        else if((sId.charAt(0)=='W') || (sId.charAt(0)=='w')) value=32
        else if((sId.charAt(0)=='Z') || (sId.charAt(0)=='z')) value=33
        else if((sId.charAt(0)=='I') || (sId.charAt(0)=='i')) value=34
        else if((sId.charAt(0)=='O') || (sId.charAt(0)=='o')) value=35
        else fResult = false ;
    }
 
    if(fResult==true) {
        value = Math.floor(value/10) + (value%10)*9 + parseInt(sId.charAt(1))*8 +
                parseInt(sId.charAt(2))*7 + parseInt(sId.charAt(3)) * 6 + parseInt(sId.charAt(4)) * 5 +
                parseInt(sId.charAt(5))*4 + parseInt(sId.charAt(6)) * 3+ parseInt(sId.charAt(7)) * 2+
                parseInt(sId.charAt(8)) + parseInt(sId.charAt(9)) ;
        value = value % 10 ;
        if(value !=0 ) fResult = false ;
        var i;
        var c;
        for (i = 1; i < sId.length; i++) {
            c = sId.charAt(i);
            if (LegalID.indexOf(c) == -1) fResult = false;
        }
    }
   
    if(fResult == false) {
        alert('身份證格式錯誤');
        return false;
    }
    else {
        return true;
    }
}

/**
* 房價詳細說明展開
*/
drama.showPrice = function() {
    $(".OpenDetail1").click(function () {
        $(".OpenDetail2, .priceDtail1").slideDown();
        $(".OpenDetail1, .priceDtail2").slideUp();
    });
    
    $(".OpenDetail2").click(function () {
        $(".OpenDetail1, .priceDtail2").slideDown();
        $(".OpenDetail2, .priceDtail1").slideUp();
    });
}

/**
* 圓角定義
*/
drama.setCorner = function() {
    $(".subsubNav a, a.lbBn, a.bn, .msgBt a, a.barMenu").corner("5px");    
    $(".priceOpen").corner("bottom 5px");
    $('.aboutService a').hover(function() {
        $(this).stop().animate({"top": "-=5px"}, 200);
    }, function() {
        $(this).stop().animate({"top": "0px"}, 200);
    }); 
}

/**
* 首頁面板展開關閉功能
*/
$.fn.collapsePanel = function() {
    return this.each(function() {
        $(this).click(function() {
            $(this).next('div').is(':visible') ? $(this).next('div').hide() : $(this).next('div').show();
        });
    });
}