﻿
//jQuery onload
$(function() {
    PublicContestClient.init();
    $("#filters .toggler").click();
});

var PublicContestClient = {
    pageIndex: 0,
    pageSize: 15,
    totalCount: 0,
    sortDirection: "DESC",
    sortField: "AdsInQueue",
    totalPages: 0,
    hasPreviousPage: false,
    hasNextPage: false,
    userType: "",

    init: function() {
        $.post("/Company/GetRecentPageIndex", null,
               function(data) { PublicContestClient.getRecentPageIndex_callback(data); }, "json");    
    },

    getRecentPageIndex_callback: function(response) {
        if (response != "0")
            PublicContestClient.getPublicContestList(response);
    },

    filter: function() {
        this.getPublicContestList(0);
    },

    cancelfilter: function() {
        $("#chkAvgDur1").attr('checked', true);
        $("#chkAvgDur2").attr('checked', true);
        $("#chkAvgDur3").attr('checked', true);
        $("#chkHealthGood").attr('checked', true);
        $("#chkHealthMedium").attr('checked', true);
        $("#chkHealthPoor").attr('checked', true);
        $("#chkPrize1").attr('checked', true);
        $("#chkPrize2").is('checked', true);
        $("#txtKeywords").val("Keywords Required");
        this.getPublicContestList(0);
    },

    sort: function(domid, field) {
        var src = $('#' + domid).attr("src");
        $(":image").attr("src", "#");
        $(":image").hide();

        if (field == this.sortField) {
            if (this.sortDirection == "ASC")
                this.sortDirection = "DESC";
            else
                this.sortDirection = "ASC";
        }
        else {
            this.sortDirection = "ASC";
        }
        this.sortField = field;

        $('#' + domid).show();
        if (src == "#")
            $('#' + domid).attr("src", "/content/images/arrow-2.gif");
        else if (src == "/content/images/arrow-2.gif")
            $('#' + domid).attr("src", "/content/images/arrow-2-1.gif");
        else
            $('#' + domid).attr("src", "/content/images/arrow-2.gif");

        this.getPublicContestList(this.pageIndex);
    },

    setPagination: function(totalcount, totalpages, haspreviouspage, hasnextpage, pageindex, pagesize, sortdirection, sortfield) {
        if (pageindex != null) { this.pageIndex = pageindex; }
        if (pagesize != null) { this.pageSize = pagesize; }
        if (sortdirection != null) { this.sortDirection = sortdirection; }
        if (sortfield != null) { this.sortField = sortfield; }
        if (totalcount != null) { this.totalCount = totalcount; }
        if (totalpages != null) { this.totalPages = totalpages; }
        if (haspreviouspage != null) { this.hasPreviousPage = haspreviouspage; }
        if (hasnextpage != null) { this.hasNextPage = hasnextpage; }
    },

    getRow: function(obj, last, count, iswriter, isadvertiser) {
        var sb = "";
        if (last) {
            sb = "<tr id='trbody' class='last'> ";
        }
        else {
            sb = "<tr id='trbody'> ";
        }

        sb += "<td class='a-left'>";

        if (obj.ShowContest) {
            if (iswriter) {
                sb += "<a href='/Writer/ContestDetails/contesthistory/" + obj.AdGroupCode + "/0'>" + obj.AdGroupName + "</a>";
            }
            else {
                sb += "<a href='/Company/ContestDetails/contesthistory/" + obj.AdGroupCode + "/0'>" + obj.AdGroupName + "</a>";
            }
        }
        else {
            sb += "<a href='#TB_inline?height=180&width=400&inlineId=popContestDetailSignUp&modal=true' class='thickbox'>" + obj.AdGroupName + "</a>";
        }

        sb += "</td>";

        sb += "<td class='i'>&nbsp;</td>";

        sb += "<td class='a-left'>";
        sb += obj.AdvertiserName;
        sb += "</td>";

        sb += "<td class='a-left'>";
        sb += obj.AdMarketName;
        sb += "</td>";

        sb += "<td class='a-left'>";
        sb += obj.Industry;
        sb += "</td>";

        sb += "<td class='a-center'>";
        sb += obj.AvgDuration;
        sb += "</td>";

        sb += "<td class='a-right last'  style='display:none'>$";
        sb += obj.PrizeOffer;
        sb += "</td>";

        sb += "</tr>";
        return sb;
    },

    getPublicContestList: function(pageindex) {
        if (pageindex != null) {
            this.pageIndex = pageindex;
        }

        $('#PublicContestBoxAjax').removeClass("nodisplay"); //the ajax box
        $('#PublicContestBox').addClass("ajaxContainer"); //the container you want to be gray   

        var industryList = '';
        $('#IndustryList :selected').each(function(i, selected) {
            industryList += $(selected).val() + ",";
        });

        //$.post(url, data, callback, "json");
        $.post("/PublicContest/GetPublicContestList",
           {
               UserType: this.userType,
               IsAverageDuration1: $("#chkAvgDur1").is(':checked'),
               IsAverageDuration2: $("#chkAvgDur2").is(':checked'),
               IsAverageDuration3: $("#chkAvgDur3").is(':checked'),
               IsHealthGood: $("#chkHealthGood").is(':checked'),
               IsHealthMedium: $("#chkHealthMedium").is(':checked'),
               IsHealthPoor: $("#chkHealthPoor").is(':checked'),
               IsPrize1: $("#chkPrize1").is(':checked'),
               IsPrize2: $("#chkPrize2").is(':checked'),
               GroupName: $("#txtKeywords").val(),
               PageIndex: this.pageIndex,
               PageSize: this.pageSize,
               SortDirection: this.sortDirection,
               SortField: this.sortField,
               IndustryIds: industryList
           },
           function(data) { PublicContestClient.getPublicContestList_callback(data); }, "json");
    },

    getPublicContestList_callback: function(response) {
        this.setPagination(response.TotalCount, response.TotalPages, response.HasPreviousPage, response.HasNextPage);
        PagingControl.setPagination("Top", this.pageIndex, this.pageSize, this.totalCount, this.totalPages, "PublicContestClient.getPublicContestList", "Bot");

        var last = false;
        var bodycontent = '';

        for (var i = 0; i < response.PageSize; i++) {
            last = (response.TotalPages == i + 1) ? true : false;
            var contest = response.PublicContestList[i];
            if (contest == null) continue;
            bodycontent += this.getRow(contest, last, i, response.IsWriter, response.IsAdvertiser);
        }

        $("#tbody").html(bodycontent);
        $('#PublicContestBoxAjax').addClass("nodisplay"); //the ajax box
        $('#PublicContestBox').removeClass("ajaxContainer"); //the container you want to be gray        
        tb_init('a.thickbox, area.thickbox, input.thickbox');
    }

}
