$(function () {
    //=== Set body class based on device orientation ===
//    if (Math.abs(window.orientation) == 90) {
//        landscape()
//    }
//    else if (window.orientation == 0 || window.orientation == 180) {
//        portrait();
//    }

    //=== Event for change in device orientation ===
//    window.addEventListener("orientationchange", function () {
//        if (Math.abs(window.orientation) == 90) {
//            landscape()
//        }
//        if (window.orientation == 0 || window.orientation == 180) {
//            portrait();
//        }
//    }, false);

    //===iPad Sliding Menu ===

    $(document).on('click', '#slide-open', function () {
        $('.slide-menu-left').show();
        $('.slide-menu-left, .slide-menu-left .footer-left').animate({
            width: '265px'
        });
        $('.slide-menu-right, .slide-menu-right .footer-right, #TableContainer table.tablesorter, .tablesorter-scroller-table').animate({
//            width: ''
        }, function () {
            $.each($("#ListingsTable tbody tr:visible:first td"), function (index, item) {
                $(".tablesorter-scroller-header table thead .tablesorter-filter-row td:eq(" + index + ")  input").width($(item).outerWidth() - 8);
            });
            $(".tablesorter-scroller-table").css("width", $("#ListingsTable").outerWidth());

        });
        $('.footer-right li a').animate({
            padding: '0 62px'
        });

        $(this).attr('id', 'slide-close');
    });

    $(document).on('click', '#slide-close', function () {
        $('.slide-menu-left, .footer-left').animate({
            width: '0'
        }, function () {
            $('.slide-menu-left').hide();
        });
        $('.slide-menu-right, .slide-menu-right .footer-right, #TableContainer table.tablesorter, .tablesorter-scroller-table').animate({
//            width: ''
        }, function () {
            $.each($("#ListingsTable tbody tr:visible:first td"), function (index, item) {
                $(".tablesorter-scroller-header table thead .tablesorter-filter-row td:eq(" + index + ")  input").width($(item).outerWidth() - 8);
            });
            $(".tablesorter-scroller-table").css("width", $("#ListingsTable").outerWidth());

        });
        $('.footer-right li a').animate({
            padding: '0 88px'
        });

        $(this).attr('id', 'slide-open');
    });

    $("#accountInfo,.companies-wrapper,#metaFields").on('click', '.checkbox-single div', function () {
        if (!$(this).next().prop("disabled")) {
            console.log("Not Disabled");
            if ($(this).hasClass('checkbox-false')) {
                console.log("false");
                $(this).next().val(true);
                $(this).attr('class', 'checkbox-true');
            }
            else {
                console.log("true");
                $(this).next().val(false);
                $(this).attr('class', 'checkbox-false');
            }
        }
        console.log("Disabled");
    });

    $("#searchForm").submit(function () {
        Search(this);
        return false;
    });

});


//=== Update layout for vertical orientation ===
function portrait() {
    $("body").attr("class", "portrait");

    //=== If left menu is on screen ===
    $('.slide-menu-right, .slide-menu-right .footer-right').animate({
//        width: ''
    });

    // === If left menu is hidden ===
    if ($(".slide-menu-left").css("display") == "block") {
        $('.slide-menu-left, .slide-menu-left .footer-left').animate({
            width: '350px'
        });
    }
}

//=== Update layout for horizontal orientation ===
function landscape() {
    $("body").attr("class", "landscape");

    //=== If left menu is on screen ===
    if ($(".slide-menu-left").css("display") == "block") {
        $('.slide-menu-left, .slide-menu-left .footer-left').animate({
            width: '265px'
        });
        $('.slide-menu-right, .slide-menu-right .footer-right').animate({
            width: '759px'
        });
    }

    // === If left menu is hidden
    else {
        $('.slide-menu-right, .slide-menu-right .footer-right').animate({
            width: '1024px'
        });
        $('#slide-close').attr('id', 'slide-open');
    }
}

function FormatDate(date) {
    var d = new Date(date);
    // JS months are 0 based. 0=January, 11=December
    return (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear();
}

function FormatDateTime(datetime) {
    var d = new Date(datetime);

    var ap = "";

    var hour = d.getHours();
    if (hour < 12) {
        ap = "AM";
    }
    else {
        ap = "PM";
    }
    if (hour == 0) {
        hour = 12;
    }
    if (hour > 12) {
        hour = hour - 12;
    }

    var min = d.getMinutes();

    min = min + "";

    if (min.length == 1) {
        min = "0" + min;
    }

    return (d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear() + ' ' + hour + ':' + min + ' ' + ap;
}

function FormatPhone(phone) {
    //    number = number.replace(/[^\d]/g, "").replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
    //    return number;
    if (phone == 0) {
        return '';
    } else {
        var regExp = /\(|\)|\-|\s/g;
        phone = phone.toString().replace(regExp, "");
        return '(' + phone.toString().substr(0, 3) + ') ' + phone.toString().substr(3, 3) + '-' + phone.toString().substr(6);
    }
}

Number.prototype.formatMoney = function (c, d, t) {
    var n = this,
        c = isNaN(c = Math.abs(c)) ? 2 : c,
        d = d == undefined ? "." : d,
        t = t == undefined ? "," : t,
        s = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function TrimValue(field) {
    if (typeof (field) != "undefined" && field != null) {
        var trimmed = field.toString().trim();
        return trimmed.length < 2 ? trimmed.replace("0", "") : trimmed;
    }
    else
        return "";
}

function QueueSort(queueId, sort, filter) {
    this.queueId = queueId;
    this.sort = sort;
    this.filter = filter;
}

//var specs = [];
//specs[0] = this.SPECID;
//specs[1] = this.DESCRIPTION;
//specs[2] = this.PARENTSPECID;
//specs[3] = id;
//specs[4] = this.ORDERBY;
//specs[5] = triggerDD;
function Spec(specId, description, parentSpecId, elementId, orderBy, triggerDD, active) {
    this.SPECID = specId;
    this.DESCRIPTION = description;
    this.ParentSpecId = parentSpecId;
    this.ElementId = elementId;
    this.ORDERBY = orderBy;
    this.TriggerElementId = triggerDD;
    this.ACTIVE = active;
}