﻿var Mackolik = Mackolik || {};
Mackolik.Fixture = function (sModuleName) {
    this.oContainer = sModuleName;
};

Mackolik.Fixture.prototype.init = function (id) {
    this.iSeasonId = id;
    //this.oSeason = new Mackolik.Season('fixtureSeasonCombo');
    //this.oSeason.initByLeague(id, this.getWeeks, this);
    
    var obj = this;
    /*
    YAHOO.util.Event.addListener('fixtureSeasonCombo', "change", 
    function(){
    obj.oSeason.iSeasonId = this.value;
    obj.getWeeks();
    });
    */
    this.getWeeks();

    YAHOO.util.Event.addListener('cboWeek', "change", 
        function () {
            obj.iWeek = this.value;
            obj.getMatches();
        }
    ); 
};

Mackolik.Fixture.prototype.getWeeks = function() {
    var url = APP_ROOT + '/AjaxHandlers/FixtureHandler.aspx?command=getWeeks&id=' + this.iSeasonId;
    var obj = this;
    var callback = new AjaxCallback(
        function(response) {
            obj.writeWeeks(response.responseText);
            obj.getMatches();
        }
    );
    var transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback);
};

Mackolik.Fixture.prototype.writeWeeks = function(data) {
    var oData = eval("(" + data+ ")");

    var oWrapper = document.getElementById('cboWeek');
    var bNotSelected = true;
    oWrapper.innerHTML = "";
    for (var i=0;i<oData.length; i++) {
        var option = document.createElement("option");
        option.value = oData[i][0];
        if (oData[i][3] != 1  && bNotSelected) {
            option.selected = true;
            bNotSelected = false;
        }
        if (bNotSelected && i==oData.length-1) {
            option.selected = true;
            bNotSelected = false;
        }
        option.innerHTML = oData[i][0] + " (" +  oData[i][1] + " - " + oData[i][2] + ")";
        oWrapper.appendChild(option);
    }
    this.iWeek = oWrapper.value;
};

Mackolik.Fixture.prototype.getMatches = function () {
    var url = APP_ROOT + '/AjaxHandlers/FixtureHandler.aspx?command=getMatches&id=' + this.iSeasonId + '&week=' + this.iWeek;
    var obj = this;
    var callback = new AjaxCallback(
        function(response) { 
            obj.writeFixture(eval("(" + response.responseText + ")"));
        }
    );
    var transaction = YAHOO.util.Connect.asyncRequest( 'GET', url, callback ); 
};

Mackolik.Fixture.prototype.writeFixture = function(data) {
var header = '<table width="100%" cellspacing=0 cellpadding=1 border=1 style="border-collapse:collapse;border-color:#cccccc;" class="rowData" id="resultsList">';
    var headerRow = '<tr class="data_header" height=18 align=center><td colspan=6>FİKSTÜR</td><td>Kod</td><td>1</td><td>0</td><td>2</td><td>1&amp;0</td><td>1&amp;2</td><td>2&amp;0</td><td>Alt</td><td>Üst</td></tr>';
    var columns = '<colgroup><col width=30 /><col width=30 /><col width=140 /><col width=40 /><col width=140 /><col width=20 /><col width=40 /><col width=30 span=8 /></colgroup>';
    var rowFormat = '<tr height=15 class="alt{13}"><td>{0}</td><td align=center>{1}</td><td align=right><a href="javascript:popTeam({14})">{2}</a></td><td align=center class="score">{16}</td><td align=left><a href="javascript:popTeam({15})">{3}</a></td><td align=right><a href="javascript:popComparison({17});"><img src="'+ICON_PATH+'compare.gif" /></a></td><td align=right>{4}</td><td align=right>{5}</td><td align=right>{6}</td><td align=right>{7}</td><td align=right>{8}</td><td align=right>{9}</td><td align=right>{10}</td><td align=right>{11}</td><td align=right>{12}</td></tr>';

    var sbFixture= new StringBuilder();
    sbFixture.append(header);
    sbFixture.append(columns);
    sbFixture.append(headerRow);
    
    function writeOdd (odd, condition, isFinished) {
        if(odd && condition && isFinished) {
            return "<b>" + odd.toFixed(2) + "</b>";
        }
        if(odd) {
            return odd.toFixed(2);
        }
        return "-";
    }
    
    for(var i=0;i<data.length;i++) {
        var odds = data[i];
        var matchScore;
        var matchStatus;
        var isFinished = false;

        if(odds[8] == 4 || odds[8] == 6 || odds[8] == 8 || odds[8] == 10) {
            isFinished = true;
        }
        
        matchStatus = odds[2];
        
        if(odds[8]==0) {
            matchScore = "<a href='javascript:popMatch(" + odds[0] + ")'>v</a>";
        } else if(odds[8] == 9) {
            matchScore = "<a href='javascript:popMatch(" + odds[0] + ")'>P - P</a>";
        } else if (isFinished){
            matchScore = "<a href='javascript:popMatch(" + odds[0] + ")'>" + odds[9] + " - " + odds[10] + "</a>";
        } else {
            matchScore = "<a href='javascript:popMatch(" + odds[0] + ")' style='color:#dd0000'>" + odds[9] + " - " + odds[10] + "</a>";
            matchStatus = "<span style='display:block;color:#dd0000'>" + odds[2] + "'</span>";
        }
        
        
        sbFixture.appendFormat(rowFormat, odds[1], matchStatus, odds[4], odds[6], odds[7]?odds[7]:'-', 
        writeOdd(odds[11], odds[9]>odds[10] , isFinished),
        writeOdd(odds[12], odds[9]==odds[10], isFinished),
        writeOdd(odds[13], odds[9]<odds[10], isFinished),
        writeOdd(odds[14], odds[9]>=odds[10], isFinished),
        writeOdd(odds[15], odds[9]!=odds[10], isFinished),
        writeOdd(odds[16], odds[9]<=odds[10], isFinished),
        writeOdd(odds[17], odds[9]+odds[10]<3, isFinished),
        writeOdd(odds[18], odds[9]+odds[10]>2, isFinished),
        i%2+1, odds[3], odds[5], matchScore, odds[0]);
    }
    sbFixture.append('</table>');
    document.getElementById(this.oContainer).innerHTML = sbFixture;
    var rows = document.getElementById(this.oContainer).firstChild.rows;
    
    for(var i=0;i<data.length;i++) {
        var odds = data[i];
        rows[i+1].team1 = odds[3];
        rows[i + 1].team2 = odds[4];
        YAHOO.util.Event.addListener(rows[i + 1], "mouseover", fixtureTabOver);
        YAHOO.util.Event.addListener(rows[i + 1], "mouseout", fixtureTabOut);
    }
};

function fixtureTabOver(e) {
    this.style.backgroundColor = Mackolik.Standing.HIGH_COLOR;
}
function fixtureTabOut(e) {
    this.style.backgroundColor = "";
}