﻿/// <reference path="../jquery-1.3.2-vsdoc2.js" />
var matrixCurrentPage = 1;
//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 (MatrixPages != null) {
    	if (MatrixPages.length < 1) {
            $("#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 (MatrixPages != null) {
		if (MatrixPages.length == 0) {
			$(document).ready(function () {
                $("#principalMatrix").hide();
            });
        }
        else {
        	Matrix = MatrixPages[matrixCurrentPage - 1];
            $('#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();
        });
    }
    if (MatrixPages != null) {
    	if (matrixCurrentPage == 1 || MatrixPages.length == 1) {
    		$("#matrix_seta_esq").addClass("inativo_esq").removeClass("ativo_esq");
    	}
    	else {
    		$("#matrix_seta_esq").addClass("ativo_esq").removeClass("inativo_esq");
    	}

    	if (MatrixPages.length == matrixCurrentPage) {
    		$("#matrix_seta_dir").addClass("inativo_dir").removeClass("ativo_dir");
    	}
    	else {
    		$("#matrix_seta_dir").addClass("ativo_dir").removeClass("inativo_dir");
    	}
    }
}


//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));
        
        $("#principalMatrix").show();
    }
}

function DisplayMatrix() {
    $(document).ready(function() {
        $("#principalMatrix").show("slow");
    });
}

function HideMatrixCombo() {
	if (MatrixPages.length > matrixCurrentPage) {
		$("#principalMatrix").hide();
		matrixCurrentPage++;
		PublishMatrix();
	}
}

function ShowMatrixCombo() {

	if (matrixCurrentPage > 1) {
		$("#principalMatrix").hide();
		matrixCurrentPage--;
		PublishMatrix();
	}
}

function ShowMatrixPage() {
    $(document).ready(function() {
        $("#principalMatrix").show();
    });
    GenerateMatrixPage();
}
