﻿var GlobalSelLeague = "";
var GlobalSelLeagueName = "";
var GlobalCodeString = "";
var GlobalCodeStringwithSelected = "";
var GlobalSelClub = 0;


jQuery.noConflict();

    function FillLeaguesForCLControl(gLeague,club)
    {
        if(gLeague != null)
            GlobalSelLeague = gLeague;
            
        if(club != null)
            GlobalSelClub=club;
        else 
            GlobalSelClub=0;
       
        var str1 = "Leagues' list loading ...";

        var str = "<select id=\"drpClLeague\" name=\"drpClLeague\" style=\"width:200px;\" >"
        str +="<option value=\"0\">" + str1 + "</option>";
        str += "</select>";
        
        jQuery("#plhClLeague").html(str);
        
        var tt = "{}";
       
       jQuery.ajax({
				type: "POST",
				data: tt,
				url: "/SurveyDropsService.asmx/LeaguesForCL",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: completeLeaguesDropForCLControl,
				failure: errorLeaguesDropForCLControl 
				});
    }

    function completeLeaguesDropForCLControl(val)
    {
       
        var arr = val.d.split(",");
        var selstr = "";
        var str = "";
        var strselleag = arr[0];
         
        str = "<select id=\"drpClLeague\" name=\"drpClLeague\" onchange=\"FillClubsForLeague(this.value);\" style=\"width:200px;\">"
          
          
            for(var i=0; i<arr.length; i=i+2)
            {
            
                if(GlobalSelLeague == arr[i])
                {
                    selstr = " selected ";
                    strselleag = arr[i];
                }
                else    
                    selstr = "";

                str +="<option value=\"" + arr[i] + "\" " + selstr + " >" + arr[i+1] + "</option>";
            
            }
        
        str += "</select>";
        
        jQuery("#plhClLeague").html(str);
        FillClubsForLeague(strselleag);
    
    }

    function errorLeaguesDropForCLControl(error)
    {
        alert("Error !!!" + error);
        var val = error;
        jQuery("#plhClLeague").html(val);

    }

//------------------------------------------------------------------------------------------

    function FillClubsForLeague(lpath)
    {
        jQuery("#lnewsheader").html("<h6>" + jQuery("#drpClLeague option:selected").text() + "</h6>");
        
        
        if(lpath == "")
            return;

        var tt = "{leaguepath:'" + lpath + "'}";
        
           jQuery.ajax({
				type: "POST",
				data: tt,
				url: "/SurveyDropsService.asmx/ClubsListForCl",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: completeClubsDropForCLControl,
				failure: errorClubsDropForCLControl
				});  
    }

    function completeClubsDropForCLControl(val)
    {
        
        var selstr = "";
        var str = "";
        var clubfound = 0;
        

        if(val.d != null && val.d != "")
        {
            var arr = val.d.split(",");
    
            for(var i=0; i<arr.length; i=i+3)
            {
               if (i > 0 && (i%15) == 0)
                   str +="</tr>";

               if (i==0 || (i%15) == 0)
                   str +="<tr>";
                   str +="<td height=\"68\" width=\"68\" align=\"center\" onclick=\"FillNewsForClub(this.id);\"  onmouseover=\"changeTDBG(this, 'over');\"  id=\"" + arr[i] + "\"  onmouseout=\"changeTDBG(this, 'out');\">";
                  if(arr[i+1].length > 0)
                    str += "<img src=\"/gmf/files/" + arr[i+1].substring(0,2) + "/" + arr[i+1] + ".png\" alt=\"" + arr[i+2] + "\" />";
                  else  
                    str += "<img src=\"/gmf/assets/png/blank.png\" alt=\"" + arr[i+2] + "\" />";
                  
                  str +="</td>";  
                  
                  if(arr[i] == GlobalSelClub)
                    clubfound = 1;
                  
            } 

            if(arr.length > 0)
                str +="</tr>";
        
            GlobalCodeString = str;
            
            if(clubfound == 1)
                FillNewsForClub(GlobalSelClub);
            else
                FillNewsForClub(arr[0]);
        }
        else
        {
            jQuery("#acccontainer").html("");
            PlayAccordion(0);
        }
    }

    function errorClubsDropForCLControl(error)
    {
        var val = error;
       GlobalCodeString = val;
    }
   
   //-----------------------------------------------------------------------------
    function FillNewsForClub(club)
    {
        GlobalCodeStringwithSelected=GlobalCodeString.replace("id=\"" +club + "\"  onmouseout=\"changeTDBG(this, 'out');\">","id=\"" +club + "\" style=\"background-image:url('/gmf/assets/png/premier_over.png');background-position:left top;background-repeat:no-repeat; \" onmouseout=\"changeTDBG(this, 'over');\">");
       
        if(club == "")
            return;

        var tt = "{varclub:'" + club + "'}";
        
           jQuery.ajax({
				type: "POST",
				data: tt,
				url: "/SurveyDropsService.asmx/NewsForClub",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: completeNewsForClub,
				failure: errorNewsForClub
				});  
    }

    function completeNewsForClub(val)
    {
        var arr = val.d.split("|");
        
        var str = "";
        var strnews = "";
        var outstr = "";

        if(arr.length > 4)
        {
           str +="<tr><td colspan=\"5\" align=\"left\" style=\"padding-left: 5px;\">"+arr[1]+"</td>";
           str += "</tr><tr><td colspan=\"2\" align=\"left\" style=\"padding-left: 5px;\">";

           if(arr[4].length > 0)
             str +="<img src=\"/gmf/files/" + arr[4].substring(0,2) + "/" + arr[4] + "\" alt=\"news picture\" /></td>";
           else  
             str += "<img src=\"/gmf/assets/spb_addon/noimage.jpg\" alt=\"news picture\" /></td>";

           str +="<td colspan=\"3\" align=\"left\" style=\"padding-left: 5px;\">"+arr[3]+"<span><a href=\""+arr[0]+"\">Read More</a></span>";
           str +="</td></tr>";
        }
        
        var j=1
        for(var i=5; i<arr.length; i=i+5)
        {
              j=j+1;
                strnews +="<h3 class=\"toggler\">";
                strnews += "<a href=\"#section"+j+"\" onfocus=\"this.blur();\">"+arr[i+1]+"</a></h3>";
                strnews += "<div class=\"accordion\"><h3><a href=\""+arr[i]+"\" style=\"color:#666666;\">" + arr[i+2] + "</a></h3><br />"
                strnews += " <p>"+arr[i+3]+"</p><br /></div>";
        } 
           
      
        outstr+="<h3 class=\"toggler first\" style=\"padding-top:5px;\"><a href=\"#section1\" onfocus=\"this.blur();\">Club List</a></h3>";
        outstr+="<div class=\"accordion\"><div id=\"team_tbl\" >";
        outstr+="<table cellpadding=\"5\" cellspacing=\"0\" width=\"340\" style=\"text-align:center;\">"
        outstr+=GlobalCodeStringwithSelected;
        outstr+="<tr><td height=\"20\"></td></tr>";
        outstr+=str;
        outstr+="<tr><td height=\"20\"></td></tr></table></div></div>";
        outstr+=strnews;
    
        jQuery("#acccontainer").html(outstr);
        
        PlayAccordion(0);
    }

    function errorNewsForClub(error)
    {
        var val = error;
        jQuery("#acccontainer").html(val);
    }
