﻿/// <reference path="../jquery-1.3.2-vsdoc2.js" />

//Carrega matrix junto com os resultados
function GenerateMatrix() {
    
    PublishMatrix();
    $("#moreMatrix").hide();
    $("#Ativo").addClass("bt-ativo").removeClass("bt-inativo");
    $("#Inativo").addClass("bt-inativo").removeClass("bt-ativo");
    if (MatrixPage != null) {
        if (MatrixPage.length <= 0) {
            $("#moreMatrix").hide();
        }
        else {
            $("#moreMatrix").show();
        }
    }
}

//Carrega dados da matrix paginada
function GenerateMatrixPage() {
    if (MatrixPage != null && MatrixPage.length > 0) {
        $('#divMatrix').empty();
        for (var i = 0; i <= MatrixPage.length - 1; i++) {
            PublishGridLineMatrix(i, MatrixPage[i]);
        }
    }
    else {
        $(document).ready(function() {
            $("#principalMatrix").hide();
        });
    }
}

//Carrega dados da Matrix
function PublishMatrix() {
    if (Matrix != null) {
        if (Matrix.length == 0) {
            $(document).ready(function() {
                $("#principalMatrix").hide();
            });
        }
        else {
            $('#divMatrix').empty();
//Carrega Melhor Preço(Titulo lateral)
            var amountPriceAll = jLinq.from(Matrix)
            .where(function(r){return r.UrlLogo==" ";})
            .select();
            for (var i = 0; i <= amountPriceAll.length - 1; i++) {
                PublishGridLineMatrix(i, amountPriceAll[i]);
            }
            
            //Carrega Melhor Preço do Menor ao Maior Valor Cias
            amountPriceAll = jLinq.from(Matrix)
            .each(function(rec){ 
                if (rec.DirectPrice<rec.OneStopPrice && rec.DirectPrice>0 && rec.OneStopPrice>0)
                    if (rec.DirectPrice<rec.TwoOrMoreStopPrice && rec.TwoOrMoreStopPrice>0)
                        rec.precocombo=rec.DirectPrice
                    else if (rec.TwoOrMoreStopPrice>0)
                        rec.precocombo=rec.TwoOrMoreStopPrice
                    else
                        rec.precocombo=rec.DirectPrice
                else if (rec.DirectPrice> 0 && rec.OneStopPrice>0) 
                        if (rec.OneStopPrice < rec.TwoOrMoreStopPrice && rec.TwoOrMoreStopPrice>0)
                            rec.precocombo=rec.OneStopPrice
                        else if (rec.TwoOrMoreStopPrice>0)
                            rec.precocombo=rec.TwoOrMoreStopPrice
                        else
                            rec.precocombo=rec.OneStopPrice
                else if (rec.DirectPrice > 0)
                    if (rec.DirectPrice < rec.TwoOrMoreStopPrice && rec.TwoOrMoreStopPrice>0)
                        rec.precocombo=rec.DirectPrice
                    else if (rec.TwoOrMoreStopPrice>0)
                        rec.precocombo=rec.TwoOrMoreStopPrice
                    else
                        rec.precocombo=rec.DirectPrice
                else if (rec.OneStopPrice > 0)
                     if (rec.OneStopPrice < rec.TwoOrMoreStopPrice && rec.TwoOrMoreStopPrice>0)
                            rec.precocombo=rec.OneStopPrice
                    else if (rec.TwoOrMoreStopPrice>0 )
                        rec.precocombo=rec.TwoOrMoreStopPrice                            
                    else
                        rec.precocombo=rec.OneStopPrice
                else if (rec.TwoOrMoreStopPrice > 0)    
                        rec.precocombo=rec.TwoOrMoreStopPrice                          
                ;})
            .where(function(r){return r.UrlLogo!=" ";})
            .orderBy("precocombo")
            .select();
            for (var i = 0; i <= amountPriceAll.length - 1; i++) {
                PublishGridLineMatrix(i, amountPriceAll[i]);
            }
        }
    }
    else {
        $(document).ready(function() {
            $("#principalMatrix").hide();
        });
    }
}


//Parametros da Matrix
function PublishGridLineMatrix(i, mat) {

    if (mat.UrlLogo != '') {
        
        directprice = mat.DirectPrice != 0 ? Math.round(Number(mat.DirectPrice.toString().replace(",", "."))) : 99999999;
        onestopprice = mat.OneStopPrice != 0 ? Math.round(Number(mat.OneStopPrice.toString().replace(",", "."))) : 99999999;
        twoormorestopprice = mat.TwoOrMoreStopPrice != 0 ? Math.round(Number(mat.TwoOrMoreStopPrice.toString().replace(",", "."))) : 99999999;

        if (directprice == 99999999)
            strdirectprice = '&nbsp;';
        else if (directprice < onestopprice && directprice < twoormorestopprice)
            strdirectprice = '<b><a onclick=FilterMatrix(0,\'' + mat.MainAirlineCode + '\',' + directprice + ')>' + mat.CurrencyCode + "&nbsp;" + directprice + '</a></b>'
        else
            strdirectprice = '<a onclick=FilterMatrix(0,\'' + mat.MainAirlineCode + '\',' + directprice + ')>' + mat.CurrencyCode + "&nbsp;" + directprice + '</a>'

        if (onestopprice == 99999999)
            stronestopprice = '&nbsp;';
        else if (onestopprice < directprice && onestopprice < twoormorestopprice)
            stronestopprice = '<b><a onclick=FilterMatrix(1,\'' + mat.MainAirlineCode + '\',' + onestopprice + ')>' + mat.CurrencyCode + "&nbsp;" + onestopprice + '</a></b>'
        else
            stronestopprice = '<a onclick=FilterMatrix(1,\'' + mat.MainAirlineCode + '\',' + onestopprice + ')>' + mat.CurrencyCode + "&nbsp;" + onestopprice + '</a>'

        if (twoormorestopprice == 99999999)
            strtwoormorestopprice = '&nbsp;';
        else if (twoormorestopprice < directprice && twoormorestopprice < onestopprice)
            strtwoormorestopprice = '<b><a onclick=FilterMatrix(2,\'' + mat.MainAirlineCode + '\',' + twoormorestopprice + ')>' + mat.CurrencyCode + "&nbsp;" + twoormorestopprice + '</a></b>'
        else
            strtwoormorestopprice = '<a onclick=FilterMatrix(2,\'' + mat.MainAirlineCode + '\',' + twoormorestopprice + ')>' + mat.CurrencyCode + "&nbsp;" + twoormorestopprice + '</a>'
        
        var values = {
            Index: i,
            aircompanycode1: mat.MainAirlineCode,
            aircompany1: mat.MainAirlineName,
            urllogo1: mat.UrlLogo == ' ' ? 'style="display:none"' : "src='" + mat.UrlLogo + "'",
            directpricecurrencycode1: "",
            onestoppricecurrencycode1: "",
            twoormorestoppricecurrencycode1: "",
            directprice1: strdirectprice,
            onestopprice1: stronestopprice,
            twoormorestopprice1: strtwoormorestopprice
        };
        $('#divMatrix').append($.tmpl(templateMatrix, values));
        DisplayMatrix();
    }
}

function DisplayMatrix() {
    $(document).ready(function() {
        $("#principalMatrix").show("slow");
    });
}

function HideMatrix() {
    $(document).ready(function() {
        $("#principalMatrix").hide();
    });
    ShowMatrixPage();
    $("#Ativo").addClass("bt-inativo").removeClass("bt-ativo");
    $("#Inativo").addClass("bt-ativo").removeClass("bt-inativo");

}

function ShowMatrix() {
    $(document).ready(function() {
        $("#principalMatrix").show("slow");
    });
    GenerateMatrix();
    $("#Inativo").addClass("bt-inativo").removeClass("bt-ativo");
    $("#Ativo").addClass("bt-ativo").removeClass("bt-inativo");

}

function ShowMatrixPage() {
    $(document).ready(function() {
        $("#principalMatrix").show();
    });
    GenerateMatrixPage();
}