﻿
var ServiceSubTypes = {

    SubTypeList: "",
    SubTypeName_ALL: "",
    Name_MYGAMES: "",
    strLi_SubType: "",

    SelectedID: "",
    SelectedOriginalTextColor: "",
    SelectedOriginalBgColor: "",

    DefaultTextColor: "#707070",
    DefaultTextColorHover: "#ffffff",
    DefaultBgColor: "#E9E9E9",
    DefaultBgColorHover: "#669933",

    makeContainer: function() {
        strLi_SubType = "<a id=\"anchorSubtype#SubTypeID#\" href=\"#\" onclick=\"ServiceSubTypes.change(this, '#SubTypeID#', '#SubTypeName#'); return false;\">#SubTypeName#</a>";
    },

    buildContent: function() {

        var liElement;
        var strHTML;

        $.each(ServiceSubTypes.SubTypeList.Rows, function(key, item) {

            strHTML = strLi_SubType;
            strHTML = strHTML.replace(/#SubTypeID#/g, item.DataID);
            strHTML = strHTML.replace(/#SubTypeName#/g, item.ServiceSubtypeName);
            liElement = document.createElement('li');
            liElement.innerHTML = strHTML;
            ServiceSubTypes.append(liElement);
        });

    },

    append: function(objElement) {
        $("div.VerticalMenu > ul").append(objElement);
    },

    change: function(objThis, intSubTypeID, strSubTypeName) {

        //close popup
        disablePopup();

        intSubTypeID = parseInt(intSubTypeID);
        ServiceSubTypes.setClass(objThis);

        strURL = "";
        $.each(ServiceSubTypes.SubTypeList.Rows, function(key, item) {
            if (item.ServiceSubtypeName == strSubTypeName)
                strURL = item.ExtendURL;
        });

        if (PageType != "lite") {
            ServicePromotion.buildContent(intSubTypeID, strSubTypeName, strURL);    
        }
        Services.buildContent(strSubTypeName);
    },

    initClass: function() {
        //$("div.VerticalMenu > ul > li:first").addClass("Active");
        $("div.VerticalMenu > ul > li > a").removeAttr("style");
        ServiceSubTypes.SelectedID = "anchorSubtype" + ServiceSubTypes.SubTypeList.Rows[0]["DataID"];
        ServiceSubTypes.SelectedOriginalBgColor = (ServiceSubTypes.SubTypeList.Rows[0]["BackgroundColor"] == "") ? ServiceSubTypes.DefaultBgColor : ServiceSubTypes.SubTypeList.Rows[0]["BackgroundColor"];
        ServiceSubTypes.SelectedOriginalTextColor = (ServiceSubTypes.SubTypeList.Rows[0]["TextColor"] == "") ? ServiceSubTypes.DefaultTextColor : ServiceSubTypes.SubTypeList.Rows[0]["TextColor"];
        document.getElementById(ServiceSubTypes.SelectedID).style.backgroundColor = "#9DCF6A";
        document.getElementById(ServiceSubTypes.SelectedID).style.color = "#407B24";
    },

    setClass: function(objThis) {
        //$("div.VerticalMenu > ul > li").removeClass();
        if (document.getElementById(ServiceSubTypes.SelectedID)) {
            document.getElementById(ServiceSubTypes.SelectedID).style.backgroundColor = ServiceSubTypes.SelectedOriginalBgColor
            document.getElementById(ServiceSubTypes.SelectedID).style.color = ServiceSubTypes.SelectedOriginalTextColor
        }
        //$(objThis).parent().addClass("Active");
        ServiceSubTypes.SelectedID = objThis.id;
        ServiceSubTypes.SelectedOriginalBgColor = objThis.style.backgroundColor;
        ServiceSubTypes.SelectedOriginalTextColor = objThis.style.color;
        objThis.style.backgroundColor = "#9DCF6A";
        objThis.style.color = "#407B24";
    }

};


var ServicePromotion = {

    strULhtml_Default: "",
    strULhtml_IFrame: "",

    makeContainer: function() {
        strULhtml_Default = "<div class=\"LargeThumb\"><a href=\"#\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone_Promotion','#ServiceCode##ServiceRegion#_ImageLink');BeanFunBlock.StartGameByService('#ServiceCode#', '#ServiceRegion#');return false;\" title=\"" + StringTable["HINT_IMAGE_LINK"] + "\" target=\"_blank\"><img src=\"" + url_uploaded_image + "/" + web_short_name + "/game_zone/promotion/#PromotionImage#\" width=\"285\" height=\"140\"/></a></div>";
        strULhtml_Default += "<li>";
        strULhtml_Default += "  <h1>#PromotionService#</h1>";
        strULhtml_Default += "  <div class=\"Desc\">";
        strULhtml_Default += "    #PromotionText_220# &nbsp;";
        strULhtml_Default += "    <a href=\"##\" rel=\"Help\">" + StringTable["DESC_MORE"] + "</a>";
        strULhtml_Default += "  </div>";
        strULhtml_Default += "  <div class=\"Link\">";
        strULhtml_Default += "    <p name=\"LinkWebSite\"><a href=\"#WebSiteURL#\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone_Promotion','#ServiceCode##ServiceRegion#_TextLink');\" target=\"_blank\">" + StringTable["LINK_WEBSITE"] + "</a></p>";
        strULhtml_Default += "    <p name=\"LinkForum\"><a href=\"#ForumURL#\" target=\"_blank\">" + StringTable["LINK_FORUM"] + "</a></p>";
        strULhtml_Default += "    <p name=\"LinkDownload\"><a href=\"#DownloadURL#\" target=\"_blank\">" + StringTable["LINK_DOWNLOAD"] + "</a></p>";
        strULhtml_Default += "  </div>";
        strULhtml_Default += "  <div class=\"RightAlign\">";
        strULhtml_Default += "    <div class=\"StartButton\"><input type=\"button\" value=\"" + StringTable["BUTTON_START"] + "\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone_Promotion','#ServiceCode##ServiceRegion#_StartGame');BeanFunBlock.StartGameByService('#ServiceCode#', '#ServiceRegion#');\" /></div>";
        strULhtml_Default += "  </div>";
        strULhtml_Default += "</li>";
        strULhtml_Default += "<div id=\"divPopupPanel\" class=\"PopupContent\">";
        strULhtml_Default += "  <div id=\"divPopupClose\" class=\"PopupContentClose\">&nbsp;</div>";
        strULhtml_Default += "  <h1>#PromotionService#</h1>";
        strULhtml_Default += "  <div class=\"ContactArea\">#PromotionText#</div>";
        strULhtml_Default += "</div>";

        strULhtml_IFrame = "<iframe id=\"ifmPromotion\" src=\"#ExtendURL#\" scrolling=\"no\" frameborder=\"0\" width=\"100%\" height=\"200\"></iframe>";
    },

    buildContent: function(intSubTypeID, strSubtype, strURL) {

        $("div#divPromotion").empty();
        var ulElement;
        var strHTML;

        if (strURL != '' && strURL != null) {
            strHTML = strULhtml_IFrame;
            strHTML = strHTML.replace(/#ExtendURL#/g, strURL);
            ulElement = document.createElement('ul');
            ulElement.innerHTML = strHTML;

            ServicePromotion.append(ulElement);
        }
        else {
            var paraSubtype = "";
            if (strSubtype == ServiceSubTypes.SubTypeName_ALL || intSubTypeID == -1)
                paraSubtype = "ALL";
            else
                paraSubtype = intSubTypeID;

            $.ajax({
                cache: false,
                timeout: 20000,
                type: "POST",
                dataType: "json",
                data: ({ "strFunction": "getPromotions", "strSubtype": paraSubtype }),
                url: "../generic_handlers/gamezone.ashx", 
            
                error: function(objXMLHttpRequest, strTextStatus) {
                    alert("[gamezone.js] ServicePromotion (buildContent) Error: " + strTextStatus);
                },
                
                success: function(promotionData, strTextStatus) {
                    $("div#divPromotion").empty();

                    if (promotionData.Rows.length > 0) {
                        var randomInt;
                        var arrayPosition = new Array(); 
                        var iCount = 0;
                        var tmprandomInt = 0;
                        if (flagExp) {
                            for(var i = 0; i < promotionData.Rows.length; i++){
                                if (StrExpServices != "" &&
                                StrExpServices.indexOf(promotionData.Rows[i].ServiceCode + "_" + promotionData.Rows[i].ServiceRegion) > -1) {
                                    arrayPosition[iCount] = i;
                                    iCount++;
                                }
                            }
                            tmprandomInt = Math.floor(Math.random() * iCount);
                            randomInt = arrayPosition[tmprandomInt];
                        }
                        else if(intSubTypeID == -1)
                        {
                            for(var i = 0; i < promotionData.Rows.length; i++){
                                if (StrOpenedServices != "" &&
                                StrOpenedServices.indexOf(promotionData.Rows[i].ServiceCode + "_" + promotionData.Rows[i].ServiceRegion) > -1) {
                                    arrayPosition[iCount] = i;
                                    iCount++;
                                }
                            }
                            tmprandomInt = Math.floor(Math.random() * iCount);
                            randomInt = arrayPosition[tmprandomInt];
                        }
                        else {
                            randomInt = Math.floor(Math.random() * promotionData.Rows.length);
                        }
                        
                        if (randomInt != undefined || randomInt != null) {
                            strHTML = strULhtml_Default;
                            strHTML = strHTML.replace(/#PromotionImage#/g, promotionData.Rows[randomInt].PromotionFileName);
                            strHTML = strHTML.replace(/#PromotionService#/g, promotionData.Rows[randomInt].ServiceFamilyName);
                            strHTML = strHTML.replace(/#PromotionText_220#/g, promotionData.Rows[randomInt].PromotionText.sub(110));
                            strHTML = strHTML.replace(/#PromotionText#/g, promotionData.Rows[randomInt].PromotionText.replace(/\r\n/g, "<br />"));
                            strHTML = strHTML.replace(/#WebSiteURL#/g, promotionData.Rows[randomInt].ServiceWebsiteURL);
                            strHTML = strHTML.replace(/#ForumURL#/g, promotionData.Rows[randomInt].ServiceForumPageURL);
                            strHTML = strHTML.replace(/#ServiceCode#/g, promotionData.Rows[randomInt].ServiceCode);
                            strHTML = strHTML.replace(/#ServiceRegion#/g, promotionData.Rows[randomInt].ServiceRegion);
                            strHTML = strHTML.replace(/#DownloadURL#/g, promotionData.Rows[randomInt].ServiceDownloadURL);

                            ulElement = document.createElement('ul');
                            ulElement.setAttribute("class", "HighlightedGame");
                            ulElement.setAttribute("className", "HighlightedGame");
                            ulElement.innerHTML = strHTML;

                            if (promotionData.Rows[randomInt].ServiceWebsiteURL == null || promotionData.Rows[randomInt].ServiceWebsiteURL == "")
                                $(ulElement).children("li").children("div.Link").children("p[name = 'LinkWebSite']").remove();

                            if (promotionData.Rows[randomInt].ServiceForumPageURL == null || promotionData.Rows[randomInt].ServiceForumPageURL == "")
                                $(ulElement).children("li").children("div.Link").children("p[name = 'LinkForum']").remove();
                            
                            if (promotionData.Rows[randomInt].ServiceDownloadURL == null || promotionData.Rows[randomInt].ServiceDownloadURL == "")
                                $(ulElement).children("li").children("div.Link").children("p[name = 'LinkDownload']").remove();
    
                            ServicePromotion.append(ulElement);
                            initPopup();
                        }
                    }                
                }
            });
        }
    },

    append: function(objElement) {
        $("div#divPromotion").append(objElement);
    }

};


var Services = {

    ServiceList: "",
    strLi_Service_Left: "",
    strLi_Service_Right: "",

    makeContainer: function() {
        strLi_Service_Left = "<ul class=\"DetailsPanelLeft\">";
        strLi_Service_Left += "  <li class=\"GameThumb\">";
        strLi_Service_Left += "    <a href=\"#\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone','#ServiceCode##ServiceRegion#_ImageLink');BeanFunBlock.StartGameByService('#ServiceCode#', '#ServiceRegion#');return false;\" title=\"" + StringTable["HINT_IMAGE_LINK"] + "\" target=\"_blank\"><img src=\"" + url_uploaded_image + "/" + web_short_name + "/game_zone/#ServiceImage#\" /></a>";
        strLi_Service_Left += "    <div class=\"GameNameAreaHover\">";
        strLi_Service_Left += "      <div class=\"GameName\"><p class=\"Hover\">#ServiceName#</p></div>";
        strLi_Service_Left += "    </div>";
        strLi_Service_Left += "	 </li>";
        strLi_Service_Left += "	 <li class=\"Extra\">";
        strLi_Service_Left += "    <div class=\"StartButtonSmall\">";
        strLi_Service_Left += "      <input type=\"button\" value=\"" + StringTable["BUTTON_START"] + "\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone','#ServiceCode##ServiceRegion#_StartGame');BeanFunBlock.StartGameByService('#ServiceCode#', '#ServiceRegion#');\" />";
        strLi_Service_Left += "    </div>";
        strLi_Service_Left += "    <div class=\"Link\">";
        strLi_Service_Left += "      <a name=\"LinkWebSite\" href=\"#WebSiteURL#\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone','#ServiceCode##ServiceRegion#_TextLink');\" target=\"_blank\">" + StringTable["LINK_WEBSITE"] + "</a><br/>";
        strLi_Service_Left += "      <a name=\"LinkForum\" href=\"#ForumURL#\" target=\"_blank\">" + StringTable["LINK_FORUM"] + "</a>";
        strLi_Service_Left += "      <a name=\"LinkDownload\" href=\"#DownloadURL#\" target=\"_blank\">" + StringTable["LINK_DOWNLOAD"] + "</a>";
        strLi_Service_Left += "    </div>";
        strLi_Service_Left += "	 </li>";
        strLi_Service_Left += "</ul>";
        strLi_Service_Left += "<div class=\"GameThumb\">";
        strLi_Service_Left += "  <img src=\"" + url_uploaded_image + "/" + web_short_name + "/game_zone/#ServiceImage#\" />";
        strLi_Service_Left += "	 <div class=\"GameNameArea\">";
        strLi_Service_Left += "    <div class=\"GameName\"><p>#ServiceName#</p></div>";
        strLi_Service_Left += "	 </div>";
        strLi_Service_Left += "</div>";

        strLi_Service_Right = "<ul class=\"DetailsPanelRight\">";
        strLi_Service_Right += "  <li class=\"GameThumb\">";
        strLi_Service_Right += "    <a href=\"#\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone','#ServiceCode##ServiceRegion#_ImageLink');BeanFunBlock.StartGameByService('#ServiceCode#', '#ServiceRegion#');return false;\" title=\"" + StringTable["HINT_IMAGE_LINK"] + "\" target=\"_blank\"><img src=\"" + url_uploaded_image + "/" + web_short_name + "/game_zone/#ServiceImage#\" /></a>";
        strLi_Service_Right += "    <div class=\"GameNameAreaHover\">";
        strLi_Service_Right += "      <div class=\"GameName\"><p class=\"Hover\">#ServiceName#</p></div>";
        strLi_Service_Right += "    </div>";
        strLi_Service_Right += "  </li>";
        strLi_Service_Right += "  <li class=\"Extra\">";
        strLi_Service_Right += "    <div class=\"StartButtonSmall\">";
        strLi_Service_Right += "      <input type=\"button\" value=\"" + StringTable["BUTTON_START"] + "\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone','#ServiceCode##ServiceRegion#_StartGame');BeanFunBlock.StartGameByService('#ServiceCode#', '#ServiceRegion#');\" />";
        strLi_Service_Right += "    </div>";
        strLi_Service_Right += "    <div class=\"Link\">";
        strLi_Service_Right += "      <a name=\"LinkWebSite\" href=\"#WebSiteURL#\" onclick=\"BeanFunBlock.WebTrendCounter('DCSext.bf_GameZone','#ServiceCode##ServiceRegion#_TextLink');\" target=\"_blank\">" + StringTable["LINK_WEBSITE"] + "</a><br/>";
        strLi_Service_Right += "      <a name=\"LinkForum\" href=\"#ForumURL#\" target=\"_blank\">" + StringTable["LINK_FORUM"] + "</a>";
        strLi_Service_Right += "      <a name=\"LinkDownload\" href=\"#DownloadURL#\" target=\"_blank\">" + StringTable["LINK_DOWNLOAD"] + "</a>";
        strLi_Service_Right += "    </div>";
        strLi_Service_Right += "  </li>";
        strLi_Service_Right += "</ul>";
        strLi_Service_Right += "<div class=\"GameThumb\">";
        strLi_Service_Right += "  <img src=\"" + url_uploaded_image + "/" + web_short_name + "/game_zone/#ServiceImage#\" />";
        strLi_Service_Right += "  <div class=\"GameNameArea\">";
        strLi_Service_Right += "    <div class=\"GameName\"><p>#ServiceName#</p></div>";
        strLi_Service_Right += "  </div>";
        strLi_Service_Right += "</div>";
    },

    buildContent: function(typeName) {

        $("ul.GamesList").empty();
        var strHTML;
        var iCount = 1;

        $.each(this.ServiceList.Rows, function(key, item) {

            if (item.ServiceRank != 3 && item.ServiceRank != 4) {
                if ((iCount % 4) == 0)
                    strHTML = strLi_Service_Right;
                else
                    strHTML = strLi_Service_Left;
                        
                if (flagExp) {
                    if (StrExpServices != "" &&
                    StrExpServices.indexOf(item.ServiceCode + "_" + item.ServiceRegion) > -1) {
                        Services.append(item, strHTML);
                        iCount++;
                    }
                }
                else {
                    if (typeName == ServiceSubTypes.SubTypeName_ALL) {
                        Services.append(item, strHTML);
                        iCount++;
                    }
                    else if (item.ServiceSubtypeName == typeName) {
                        Services.append(item, strHTML);
                        iCount++;
                    }
                    else if (ServiceCategory[typeName] != null &&
                    ServiceCategory[typeName].indexOf(item.ServiceCode + "_" + item.ServiceRegion) > -1) {
                        //One game has multi game types
                        Services.append(item, strHTML);
                        iCount++;
                    }
                    else if (typeName == ServiceSubTypes.Name_MYGAMES && StrOpenedServices != "" &&
                    StrOpenedServices.indexOf(item.ServiceCode + "_" + item.ServiceRegion) > -1) {
                        Services.append(item, strHTML);
                        iCount++;
                    }
                }
            }

        });

        Services.setHover();

        if (iCount == 1) $("ul.GamesList").append("<div style=\"text-align:center;\">" + StringTable["DESC_NO_GAME"] + "</div>");
    },

    append: function(objItem, strHtml) {
        strHtml = strHtml.replace(/#ServiceName#/g, objItem.ServiceFamilyName);
        strHtml = strHtml.replace(/#ServiceImage#/g, objItem.ServiceLargeImageName);
        strHtml = strHtml.replace(/#WebSiteURL#/g, objItem.ServiceWebsiteURL);
        strHtml = strHtml.replace(/#ForumURL#/g, objItem.ServiceForumPageURL);
        strHtml = strHtml.replace(/#ServiceCode#/g, objItem.ServiceCode);
        strHtml = strHtml.replace(/#ServiceRegion#/g, objItem.ServiceRegion);
        strHtml = strHtml.replace(/#DownloadURL#/g, objItem.ServiceDownloadURL);

        var liElement = document.createElement('li');
        liElement.innerHTML = strHtml;

        if (objItem.ServiceWebsiteURL == null || objItem.ServiceWebsiteURL == "")
            $(liElement).children("ul").children("li.Extra").children("div.Link").children("a[name = 'LinkWebSite']").remove();

        if (objItem.ServiceForumPageURL == null || objItem.ServiceForumPageURL == "")
            $(liElement).children("ul").children("li.Extra").children("div.Link").children("a[name = 'LinkForum']").remove();

        if (objItem.ServiceDownloadURL == null || objItem.ServiceDownloadURL == "")
            $(liElement).children("ul").children("li.Extra").children("div.Link").children("a[name = 'LinkDownload']").remove();
            
        $("ul.GamesList").append(liElement);
    },

    setHover: function() {
        // Hover effect
        $('ul.GamesList li').hover(function() {
            $(this).children(".DetailsPanelLeft").show();
            $(this).children(".DetailsPanelRight").show();
        }, function() {
            $(this).children(".DetailsPanelLeft").hide();
            $(this).children(".DetailsPanelRight").hide();
        });
    }

};

var url_uploaded_image = "";
var web_short_name = "";
var StringTable = "";
var PageType = "";


function SlideTo(intHeight, intDuration) {
    $("#ifmPromotion").animate({ height: +intHeight + "px" }, intDuration);
}
