// Start
$(function() {
    // --------- No Right Click --------- //
    //$(document).bind('contextmenu', function(e) {
    //	return false;
    //});
    $("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'facebook'
    });
    $('#marquee').marquee();
    // Tab
    $('#tabs').tabs().tabs('rotate', 5000, true).find('.ui-tabs-nav').sortable({
        axis : 'x'
    });
    $(".news_flip").newsFade({
      auto:true
    });

    // Dialog Start
    $('#ChangePasswordDialog').dialog( {
        autoOpen : false,
        width : 400,
        modal : true
    });
    $('#ChangePasswordDialogLink').click(function() {
        $('#ChangePasswordDialog').dialog('open');
        return false;
    });

    $('#ColumnDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#ColumnDialogLink').click(function() {
        $('#ColumnDialog').dialog('open');
        return false;
    });

    $('#UserDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#UserDialogLink').click(function() {
        $('#UserDialog').dialog('open');
        return false;
    });

    $('#LinkDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#LinkDialogLink').click(function() {
        $('#LinkDialog').dialog('open');
        return false;
    });

    $('#KhaoxunDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#KhaoxunDialogLink').click(function() {
        $('#KhaoxunDialog').dialog('open');
        return false;
    });

    $('#AdvertiseTextDialog').dialog( {
        autoOpen : false,
        width : 750,
        modal : true
    });
    $('#AdvertiseTextDialogLink').click(function() {
        $('#AdvertiseTextDialog').dialog('open');
        return false;
    });

    $('#PollDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#PollDialogLink').click(function() {
        $('#PollDialog').dialog('open');
        return false;
    });

    $('#AnswerDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#AnswerDialogLink').click(function() {
        $('#AnswerDialog').dialog('open');
        return false;
    });

    $('#EventDialog').dialog( {
        autoOpen : false,
        width : 500,
        modal : true
    });
    $('#EventDialogLink').click(function() {
        $('#EventDialog').dialog('open');
        return false;
    });

    $('#StatusDialog').dialog({
        modal: true,
        buttons: {
            'ຕົກລົງ': function() {
                $(this).dialog('close');
            }
        }
    });
    // Dialog End
    
    $(".slidetabs").tabs(".images > div", {
        // enable "cross-fading" effect
        effect: 'fade',
        fadeOutSpeed: "slow",
        // start from the beginning after the last tab
        rotate: true
    // use the slideshow plugin. It accepts its own configuration
    }).slideshow();
    
    $("#slider").easySlider({
        auto: true,
        controlsShow: false,
        pause: 4000,
        continuous: true
    });

    // Form Start
    $('#frmLogin').validate();
    $('#frmComment').validate();
    $('#frmFeedback').validate();
    $('#frmColumnInsert').validate();
    $('#frmColumnUpdate').validate();
    $('#frmKhaoInsert').validate();
    $('#frmKhaoUpdate').validate();
    $('#frmKhaoxunInsert').validate();
    $('#frmKhaoxunUpdate').validate();
    $('#frmUserInsert').validate();
    $('#frmUserUpdate').validate();
    $('#frmLinkInsert').validate();
    $('#frmLinkUpdate').validate();
    $('#frmPollInsert').validate();
    $('#frmPollUpdate').validate();
    $('#frmAnswerInsert').validate();
    $('#frmAnswerUpdate').validate();
    $('#frmAdvertiseTextInsert').validate();
    $('#frmAdvertiseTextUpdate').validate();
    $('#frmEventInsert').validate();
    $('#frmEventUpdate').validate();
    $('#frmChangePassword').validate({
        rules: {
            OldPassword: {
                required: true,
                minlength: 2
            },
            NewPassword1: {
                required: true
            },
            NewPassword2: {
                required: true,
                equalTo: '#NewPassword1'
            }
        }
    });
    // Form End

    // Pretty Date
    $(".datetime").prettyDate();

    // Button Start
    $('#radio').buttonset();
    $('#check').button();
    $('#format').buttonset();
    $('.uiButton').button();
    $('#datepicker').datepicker( {
        showOn : 'button',
        buttonImage : '/vtmdata/images/date.png',
        buttonImageOnly : true,
        dateFormat : 'yy-mm-dd',
        showAnim : 'explode'
    });
    // Button End

    $('#slider').s3Slider({
        timeOut: 5000
    });

    function smartColumns() { //Create a function that calculates the smart columns
        //Reset column size to a 100% once view port has been adjusted
        $("ul.column").css({
            'width' : "100%"
        });

        var colWrap = $("ul.column").width(); //Get the width of row
        var colNum = Math.floor(colWrap / 200); //Find how many columns of 200px can fit per row / then round it down to a whole number
        var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

        $("ul.column").css({
            'width' : colWrap
        }); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
        $("ul.column li").css({
            'width' : colFixed
        }); //Set exact width of the re-adjusted column
    }

    smartColumns();//Execute the function when page loads

    $(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
        smartColumns();
    });
});

function CheckAll(formName){
    for ( var i = 0; i < document.forms[formName].elements.length; i++) {
        if (document.forms[formName].elements[i].type == 'checkbox') {
            document.forms[formName].elements[i].checked = !(document.forms[formName].elements[i].checked);
        }
    }
}
