﻿var viewport, header, leftNav, filmstrip, tarif_count, tarifdiv_width, moveCount, currentMove;

function SetComparisonCriteriaClass(target, classname) {
    //Sets Criteria Class isBetter/isWorse
    target.removeClass("isBetter");
    target.removeClass("isWorse");
    target.addClass(classname);
}

function UpdatePosition() {
    tarifdiv_width = $(".comparisonTarifDiv:first").width();
    var movePos = -1 * (tarifdiv_width * currentMove);
    filmstrip.animate({ left: movePos + "px" }, 250);
}

function EvaluateComparison() {
    var TopClass = "isBetter";
    var DefeatClass = "isWorse";
    var isNA = "isNA";
    //clog("=====Comparator Function====="); 
    //clog("Tarif_Count: " + tarif_count);
    //clog("CurrentMove: " + currentMove);

    var tarifdivs = $(".comparisonTarifDiv.eval");
    var targets = new Array();
    var allcritdivs = new Array();

    //retrieves tarif divs concerned
    var i = currentMove;
    var index = 0;
    while (i < tarif_count && index < 3) {
        targets.push(tarifdivs[i]);
        jQuery.each($(tarifdivs[i]).find(".SingleCriteria"), function () {
            allcritdivs.push($(this));
        });
        index++; i++;
    }
    //clog(targets);
    //clog("Found Tarifs for Comparison: " + targets.length);

    //Compares Criteria
    var criteriaIndex = 0;
    var topCritComparators = new Array();
    jQuery.each(targets, function () {
        //clog("Searching Criteria");
        var critdivs = $(this).find(".SingleCriteria");
        //clog(critdivs);
        //clog("Found " + critdivs.length + " Criteria for " + $(this).id);

        var criteriaIndex = 0;
        jQuery.each(critdivs, function () {
            
            if ($(this).text() == "NA" || $(this).text() == "-1") {
                SetComparisonCriteriaClass($(this), isNA);
            }
            else if (topCritComparators[criteriaIndex]) {
                var topCrit = $(topCritComparators[criteriaIndex]);
                var currCrit = $(this);
                var topCritValue = parseInt(topCrit.text());
                var currCritValue = parseInt(currCrit.text());
                //clog("TopCrit: " + topCritValue + " CurrCrit:" + currCritValue);

                if (currCritValue < topCritValue) {
                    //clog("CurrCrit better than topCrit");
                    topCritComparators[criteriaIndex] = $(currCrit);

                    var defeatIndex = criteriaIndex;
                    while (defeatIndex < allcritdivs.length) {
                        SetComparisonCriteriaClass($(allcritdivs[defeatIndex]).parent(), DefeatClass);
                        defeatIndex += critdivs.length;
                    }

                    //SetComparisonCriteriaClass(topCrit.parent(), DefeatClass);
                    SetComparisonCriteriaClass(currCrit.parent(), TopClass);
                }
                else if (currCritValue > topCritValue) {
                    //clog("CurrCrit worse than topCrit");
                    SetComparisonCriteriaClass(currCrit.parent(), DefeatClass);
                }
                else {
                    //clog("CurrCrit same as topCrit");
                    SetComparisonCriteriaClass(currCrit.parent(), TopClass);
                    SetComparisonCriteriaClass(topCrit.parent(), TopClass);
                }
            }
            else {
                //clog("Setting CurrCrit to top Crit Comparator:" + $(this).text());
                topCritComparators[criteriaIndex] = $(this);
                SetComparisonCriteriaClass($(this).parent(), TopClass);
            }
            criteriaIndex++;
        });
    });

}

function InitializeComparison() {
    try {
        viewport = $(".comparisonViewport");
        header = $(".comparisonHeader");
        leftNav = $(".viewportLeftNav");
        rightNav = $(".viewportRightNav");
        filmstrip = $(".comparisonFilmstrip");

        ResetComparison();

        EvaluateComparison();

        $('.jtooltip_hover').tooltip({
            delay: 0,
            track: true,
            /*fade: 250,*/
            opacity: 1,
            top: 0,
            left: 15,
            fixPNG: true,
            showURL: false,
            bodyHandler: function () {
                return $(this).find(".infoText").html();
            }
        });

        leftNav.unbind();
        leftNav.bind("click", function () {
            if (currentMove > 0) {
                currentMove--;
                if (currentMove == 0)
                    $(this).fadeOut();
                rightNav.fadeIn();
                UpdatePosition();
                EvaluateComparison();
            }
        });

        rightNav.unbind();
        rightNav.bind("click", function () {
            if (currentMove < moveCount) {
                currentMove++;
                if (currentMove == moveCount)
                    $(this).fadeOut();
                leftNav.fadeIn();
                UpdatePosition();
                EvaluateComparison();
            }
        });

        $(".closeButton").each(function () {
            $(this).unbind();
            $(this).bind("click", function () {
                if (tarif_count > 3) {
                    $(this).parent().parent().removeClass("eval").fadeOut(250, EvaluateComparison);
                    moveCount--;
                    tarif_count--;

                    if (currentMove > moveCount) {
                        currentMove--;
                        UpdatePosition();
                    }

                    //EvaluateComparison();
                }

                //check if there are 3 tarifs left
                if (tarif_count <= 3) {
                    $(".closeButton").fadeOut();
                    leftNav.fadeOut();
                    rightNav.fadeOut();
                }
            });
        });    
    }
    catch (ex) {
        return;
    }
}

function ToggleBar(className) {
    $("." + className + " .section_toggler").toggle();
    $("." + className + " .criteriaDiv").toggle();
}

function ResetComparison() {
    $(".comparisonTarifDiv").addClass("eval").show();
    tarif_count = $(".comparisonTarifDiv").length;
    moveCount = tarif_count - 3;
    currentMove = 0;
    UpdatePosition();

    leftNav.hide();
    if (tarif_count <= 3) {
        rightNav.hide();
        $(".closeButton").hide();
    } else {
        rightNav.show();
        $(".closeButton").show();
    }
}
