/**
*【Drama Network Inc. 多瑪數位廣告】
* web http://www.drama.com.tw
* email service@drama.com.tw
* tel 02 2771 0701
* fax 02 2771 1767
*/ 

var drama;
if (!drama) drama = {};

drama.findIdInit = function ($lan) {
    $.getScript('../js/jquery.blockUI.js', function() {
        $.blockUI.defaults.css.border = 'none';
        $.blockUI.defaults.css.cursor = 'default';
        $.blockUI.defaults.overlayCSS.cursor = 'default';
    });
    
    switch ($lan) {
        case 2:
            $msgName = '請輸入姓名';
            $msgOther = '公司名稱或手機號碼請擇一輸入';
            $msgSend = '您的Email帳號為 ';
            $msgFail = '查無符合資料';
            $msgProcessing = '處理中…';
            $msgClose = '關閉';
            break;
        case 1:
        case 3:
        case 4:
        case 5:
            $msgName = 'Please input name';
            $msgOther = 'Please input company name or cell phone number';
            $msgSend = 'Your Email is ';
            $msgFail = 'No match data';
            $msgProcessing = 'Processing...';
            $msgClose = '關閉'
            break;
    }
    
    // 查詢按鈕
    $('a#btnCheck').click(function() {
        var $name = $('#name').val(),
            $company = $('#company').val(),
            $cell = $('#cell').val();
        if ($.trim($name) == '') {
            alert($msgName);
            return false;
        }
        if ($.trim($company) == '' && $.trim($cell) == '') {
            alert($msgOther);
            return false;
        }
        
        $.blockUI({
            message: '<h3>' + $msgProcessing + '</h3>'
        });
        
        $('a#modalClose').live('click', function() {
            $.unblockUI();
            return false;
        });
        
        $.post('../ajax_find_email.php', {name: $name, company: $company, cell: $cell}, function($res) {
            if ($res != 0) {
                $.blockUI({
                    message: '<h3>' + $msgSend + $res + '</h3>' + '<a href="#" id="modalClose">' + $msgClose + '</a>'
                 });
            } else {
                $.blockUI({
                    message: '<h3>' + $msgFail + '</h3>'
                 });
                setTimeout($.unblockUI, 3000);
            }
        });
        
        return false;
    });
    
    // 清除按鈕
    $('a#benCancel').click(function() {
        $('#name').val('');
        $('#company').val('');
        $('#cell').val('');
        return false;
    });
}

/**
* Product Detail Page Initial
* lan - language
*/
drama.productDetailInit = function(lan) {
    var frm = $('#inquiryForm'),
		msg = new Array();
        
    // 導入blockUI
    $.getScript('../js/jquery.blockUI.js', function() {
        // 設定ui預設值
        $.blockUI.defaults.css.border = 'none';
        $.blockUI.defaults.css.padding = '6px';
    });
    
	switch (lan) {
		case 1:
		case 3:
		case 4:
		case 5:
			msg['need'] = 'This field is required';
			msg['digits'] = 'Please enter only digits';
            msg['processing'] = '<h3>Processing....</h3>';
			break;
		case 2:
			msg['need'] = '請填寫內容';
			msg['digits'] = '請填寫正確的數字格式';
            msg['processing'] = '<h3>處理中....</h3>';
			break;
	}
	
    // form validation
    frm.validate({
        rules: {
            qty: {
                required: true,
                digits: true
            },
            msg: 'required'
        },
		messages: {
			qty: {
				required: msg['need'],
				digits: msg['digits']
			},
			msg: {
				required: msg['need']
			}
		},
        submitHandler: function() {
            $.blockUI({
                message: msg['processing']
            });
            $.post('ajaxInquiry.php', frm.serialize(), function(data) {
                $.blockUI({
                    message: '<h3>' + data + '</h3>'
                });
                $('#btnReset').trigger('click');
                setTimeout($.unblockUI, 2000);
            });
        }
    });
    
    // reset button
    $('#btnReset', frm).click(function() {
        frm
            .find('input, textarea')
            .val('')
            .end()
            .find('input:first')
            .focus();
        return false;
    });
}

/**
* Require Password Page Initial
*/
drama.findpwInit = function($lang) {
    var frm = $('#pwForm'),
        $msg = new Array();
    switch($lang) {
        case 1:
        case 3:
        case 4:
        case 5:
            $msg['need'] = 'This field is required.';
            $msg['email'] = 'Please enter a valid email address.';
            break;
        case 2:
            $msg['need'] = '請輸入此欄位.';
            $msg['email'] = '請輸入正確Email格式.';
            break;
    }
    frm.validate({
       rules: {
           name: 'required',
           email: {
               required: true,
               email: true
           }
       },
       messages: {
           name: $msg['need'],
           email: {
               required: $msg['need'],
               email: $msg['email']
           }
       },
       submitHandler: function(form) {
           $.post('ajaxPw.php',{email: $('#email', frm).val(), name: $('#name', frm).val()}, function(data) {
                alert(data);
           });
       }
    });
}

/**
* Product Page Initial
*/
drama.productInit = function() {
    // $('#viewpoint').carousel('#btnGoLeft', '#btnGoRight');
	$.getScript('../js/jquery.dramaSlider.js', function() {
		$('#viewpoint').dramaSlider({
			navA: '#btnGoLeft',
			navB: '#btnGoRight'
		});
	});
}

/**
* Product List Page Initial
*/
drama.productListInit = function() {
    $('div.list > ul:odd')
        .removeClass()
        .addClass('right');
}

/**
* Index Page Initial
*/
drama.indexInit = function() {
    // $('#viewpoint').carousel('#btnGoLeft', '#btnGoRight');
	$.getScript('../js/jquery.dramaSlider.js', function() {
		$('#viewpoint').dramaSlider({
			navA: '#btnGoLeft',
			navB: '#btnGoRight'
		});
	});
    $(window).load(function() {
        $('ul.list img').reflect({
            height: 0.2
        });
    });
}

/**
* Member Edit Page Initial
*/
drama.memberEditInit = function() {
    var frm = $('#memberForm');
    
    // reset button
    $('#btnReset').click(function() {
        $('input, textarea', frm).val('');
        $('input:first', frm).focus();
        return false;
    })
       
    // validation
    frm.validate({
        rules: {
            usn: {
                required: true,
                email: true
            },
            pwd: {
                required: true,
                rangelength: [4, 10]
            },
            confirm: {
                required: true,
                equalTo: '#pwd'
            },
            realname: 'required',
            title: 'required',
            comapanyName: 'required',
            tax: 'required'
        }
    })
}

/**
* Login Page Initial
*/
drama.loginInit = function($lang) {
    var frm = $('#loginForm'),
        $msg = new Array();
    
    $('input:first', frm).focus();
    
    // refresh
    $('a.verify').click(function() {
        $.ajaxSetup({
            cache: false
        })
        $('span#num').load('ajaxNewConfirm.php');
        return false;
    });
    
    switch ($lang) {
        case 1:
        case 3:
        case 4:
        case 5:
            $msg['need'] = 'This field is required.';
            $msg['email'] = 'Please enter a valid email address.';
            $msg['notMatch'] = 'verification Code Error.';
            break;
        case 2:
            $msg['need'] = '請輸入此欄位';
            $msg['email'] = '請輸入正確的Email格式';
            $msg['notMatch'] = '驗證碼錯誤';
            break;
    }
    
    // validation
    frm.validate({
        rules: {
            usn: {
              required: true,
              email: true  
            },
            pwd: {
                required: true
            },
            confirm: {
                required: true
            }
        },
        messages: {
            usn: {
              required: $msg['need'],
              email: $msg['email']  
            },
            pwd: {
                required: $msg['need']
            },
            confirm: {
                required: $msg['need']
            }
        },
        submitHandler: function(form) {
           $.post('ajaxConfirm.php','', function(data) {
               if ($('#confirm').val() != data) {
                   alert('the verification code is invalid')
               } else {
                   form.submit();
               }
           });
        }
    })
}

/**
* Join 03 Page Initial
*/
drama.join03Init = function() {
    // reset button
    $('#btnReset').click(function() {
        window.history.back();
        return false;
    });
}

/**
* Join 02 Page Initial
*/
drama.join02Init = function($lang) {
    var frm = $('#registerForm'),
        $msg = new Array();
    
    $('input:first', frm).focus();

    switch ($lang) {
        case 1:
        case 3:
        case 4:
        case 5:
            $msg['need'] = 'This field is required.';
            $msg['email'] = 'Please enter a valid email address.';
            $msg['again'] = 'Please input password again.';
            $msg['notMatch'] = 'Password is not match.';
            $msg['range'] = 'Please enter a value between 4 and 10 characters long.';
            break;
        case 2:
            $msg['need'] = '請輸入此欄位';
            $msg['email'] = '請輸入正確的Email格式';
            $msg['again'] = '請再次輸入密碼';
            $msg['notMatch'] = '密碼不相符';
            $msg['range'] = '請輸入4-6個字';
            break;
    }
    
    // validation
    frm.validate({
        rules: {
            usn: {
                required: true,
                email: true
            },
            pwd: {
                required: true,
                rangelength: [4, 10]
            },
            confirm: {
                required: true,
                equalTo: '#pwd'
            },
            realname: 'required',
            title: 'required',
            comapanyName: 'required',
            tax: 'required'
        },
        messages: { 
            usn: {
                required: $msg['need'],
                email: $msg['email']
            },
            pwd: {
                required: $msg['need'],
                rangelength: $msg['range']
            },
            confirm: {
                required: $msg['need'],
                equalTo: $msg['notMatch']
            },
            realname: $msg['need'],
            title: $msg['need'],
            comapanyName: $msg['need'],
            tax: $msg['need']
        }
    })
    
    // reset button
    $('#btnReset').click(function() {
        $('input, textarea', frm).val('');
        $('input:first', frm).focus();
        return false;
    })   
}

/**
* News Page Initial
*/
drama.newsInit = function() {
    $('#newsImg').reflect({
        height: 0.25,
        opacity: 0.4
    });
}

/**
* contact us initial
* lan - language
*/
drama.contactInit = function(lan) {
    var frm = $('#contactForm');
    $('input:first', frm).focus();
    
    //reset button
    $('#btnReset').click(function() {
        $('input, textarea', frm).val('');
        return false;
    });
}

/**
* Download Detail Page Initial
*/
drama.downloadDetailInit = function() {
    // carousel
    $('#viewport').carousel('#btnGoLeft', '#btnGoRight');
    $('#btnGoLeft, #btnGoRight').click(function() {
        $('div.shadow').hide();
        setTimeout(function() {
            $('div.shadow').show();
        }, 400);
    });
}

/**
* Download Page Initial
*/
drama.downloadInit = function(cate) {
    // category menu funciton
    if (cate != '') {
        $('.items li').each(function() {
            if ($(this).attr('id') == cate) {
                $('dd.classify div.text').html($(this).text());
            }
        });
    }     
        
    $('dd.classify')
        .click(function() {
            $(this).find('.items').toggle();
        })
    .find('.items')
        .hide()
        .find('li')
        .each(function() {
            $(this).click(function() {
                window.location = 'download.php?cate=' + $(this).attr('id');
            });
        });
    
    // product picture reflect
    $('.pic img').reflect({
        height: 0.2,
        opacity: 0.4
    });
}

/**
* download initial
*/
drama.formConfirm = function(lan) {
    switch (lan) {
        case 'tw': var msg = '確定要送出嗎？'; break;
        case 'es': var msg = ''; break;
        case 'en': var msg = 'Are you sure?'; break;
        case 'fr': var msg = ''; break;
        case 'ru': var msg = ''; break;
        default: var msg = 'Are you sure?';
    }
    if (!confirm(msg)) return false;
    return true;
}

/**
* Log
*/
drama.log = function (msg) {
    $('<div>')
        .css({width: '100%', height: 15, border: '1px solid blue', padding: 2, background: 'white', color: 'blue', opacity: '0.9'})
        .text(msg)
        .hide()
        .prependTo('body')
        .fadeIn();
}

$(function() {
    // 彈出式選單功能
    $('li.country').click(function() {
        $(this).find('ul.items').toggle();
    });
});