var strProduct="All";
var casestudyArr = new Array();
function casestudy(LogoPath, Name, ProductName,FullParagraph,RepName,RepTitle,Url,PdfPath) 
{    
    this.LogoPath = LogoPath;    
    this.Name = Name;    
    this.ProductName = ProductName;    
    this.FullParagraph = FullParagraph;    
    this.RepName = RepName;    
    this.RepTitle = RepTitle;    
    this.Url = Url;    
    this.PdfPath = PdfPath;
}
var casestudylist=new Array();
$ (document).ready(function()
{    	
    var stringParts=location.href.split("=");
    if(location.href.indexOf("csfilter")!=-1 && stringParts[1]!=null)
        filter(unescape(stringParts[1]));
    else
        filter("All");
    
});
String.prototype.trim = function() 
{
    a = this.replace(/^\s+/, '');return a.replace(/\s+$/, '');
};
function sortByName(a, b) 
{   
    var x = a.Name.trim().toLowerCase();   
    var y = b.Name.trim().toLowerCase();   
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function filter(Product)
{	
    strProduct=Product;	
    ShowPage(1);
}
function ShowPage(Page)
{	
    var itrno=0;	
    var PageNumbers="";	
    var PageCount=0;
    $ ("#gridContainer1").empty();	
    $ ("#pagination").empty();
    $ ("#divproducttitle").empty();
    if(strProduct!="All")
        $ ("#divproducttitle").append(strProduct+" -");
    var strdivcontent="";	
    casestudylist=[];	
    var xmlpath="/insight/casestudies/Case_Studies_Data.xml";
    var pdfpath="/insight/pdfs/";
    var casestudynotfound="<br/><center><p>No Case Study Found.</p></center>";
   
    $.get(xmlpath,{},function(xml)	
    {		
        $ ('CaseStudy',xml).each(function(i)		
        {	
            if(strProduct=="All")			
            {				
                casestudylist[itrno] = new casestudy($ (this).find("LogoPath").text(), $ (this).find("Name").text(), $ (this).find("ProductName").text(),$ (this).find("FullParagraph").text(),$ (this).find("RepName").text(),$ (this).find("RepTitle").text(),$ (this).find("Url").text(),$ (this).find("PdfPath").text());				
                itrno++;				
                if(itrno==1||itrno==((PageCount*8)+1))					
                    PageCount++;			
            }			
            else if($ (this).find("ProductName").text().trim()==strProduct || (strProduct=="DoubleClick Studio" && ($ (this).find("ProductName").text().trim()=="DoubleClick Rich Media")))			
            {								
                casestudylist[itrno] = new casestudy($ (this).find("LogoPath").text(), $ (this).find("Name").text(), $ (this).find("ProductName").text(),$ (this).find("FullParagraph").text(),$ (this).find("RepName").text(),$ (this).find("RepTitle").text(),$ (this).find("Url").text(),$ (this).find("PdfPath").text());				
                itrno++;				
                if(itrno==1||itrno==((PageCount*8)+1))					
                    PageCount++;			
            }					
        });		
        casestudylist.sort(sortByName);		
        if(Page==1)            	
        {            		
            for (var i = 0; i < 8; i++)			
            {				
                if(casestudylist[i]!=null)				
                {	
                    strdivcontent+="<table><tr><td class='CaseStudyTableCell' width='140px'>";				
                    strdivcontent+="<img width='140' src='images/"; 					
                    strdivcontent+=casestudylist[i].LogoPath.trim();					
                    strdivcontent+="'/></td><td class='CaseStudyTableCell'><h5 class='client'>";					
                    strdivcontent+=casestudylist[i].Name;	
                    strdivcontent+="</h5><h5>";				
                    strdivcontent+=casestudylist[i].ProductName;
                    strdivcontent+="</h5><p>";						
                    strdivcontent+=casestudylist[i].FullParagraph;
                    strdivcontent+="</p><h5>";
                    strdivcontent+=casestudylist[i].RepName;
                    strdivcontent+="</h5><p>";
                    strdivcontent+=casestudylist[i].RepTitle;
                    strdivcontent+="</p>";	
                    if(casestudylist[i].PdfPath.trim()!="")
                    {
                        strdivcontent+="<a class='download' target='_blank' href='";	
                        strdivcontent+=pdfpath;	
                        strdivcontent+=casestudylist[i].PdfPath.trim();					
                        strdivcontent+="'>";
                        strdivcontent+="Download the case study (pdf)</a>"
                    }
                    strdivcontent+="</td></tr></table><div class='list_divider' style='width:98%'/>";					
                    $ ("#gridContainer1").append(strdivcontent);					
                    strdivcontent="";				
                }			
            }		
        }		
        else		
        {			
            for (var i = ((Page-1)*8); i <= (((Page-1)*8)+7); i++)			
            {				
                if(casestudylist[i]!=null)				
                {					
                    strdivcontent+="<table><tr><td class='CaseStudyTableCell' width='140px'>";				
                    strdivcontent+="<img width='140' src='images/"; 					
                    strdivcontent+=casestudylist[i].LogoPath.trim();					
                    strdivcontent+="'/></td><td class='CaseStudyTableCell'><h5 class='client'>";					
                    strdivcontent+=casestudylist[i].Name;	
                    strdivcontent+="</h5><h5>";				
                    strdivcontent+=casestudylist[i].ProductName;
                    strdivcontent+="</h5><p>";						
                    strdivcontent+=casestudylist[i].FullParagraph;
                    strdivcontent+="</p><h5>";
                    strdivcontent+=casestudylist[i].RepName;
                    strdivcontent+="</h5><p>";
                    strdivcontent+=casestudylist[i].RepTitle;
                    strdivcontent+="</p>";	
                    if(casestudylist[i].PdfPath.trim()!="")
                    {
                        strdivcontent+="<a class='download' target='_blank' href='";	
                        strdivcontent+=pdfpath;	
                        strdivcontent+=casestudylist[i].PdfPath.trim();					
                        strdivcontent+="'>";
                        strdivcontent+="Download the case study (pdf)</a>"
                    }
                    strdivcontent+="</td></tr></table><div class='list_divider' style='width:98%'/>";					
                    $ ("#gridContainer1").append(strdivcontent);					
                    strdivcontent="";							
                }			
            }		
        }		
        if(PageCount>1)        	
        {     
            document.getElementById("tdpaging").className="pager";    		
            if(Page==1)				
                PageNumbers+="1 of <a onclick='ShowPage(" + PageCount + ");'>" +PageCount+"</a> <a  onclick='ShowPage(2);'>&nbsp;&gt&gt</a>";			
            else if(PageCount>Page)				
                PageNumbers+="<a onclick='ShowPage("+(Page-1)+");'>&lt&lt&nbsp;</a> "+Page+" of <a onclick='ShowPage(" + PageCount + ");'>" +PageCount+"</a> <a  onclick='ShowPage("+(Page+1)+");'>&nbsp;&gt&gt</a>";			
            else				
                PageNumbers+="<a onclick='ShowPage("+(Page-1)+");'>&lt&lt&nbsp;</a> "+Page+" of <a onclick='ShowPage(" + PageCount + ");'>" +PageCount+"</a>";			
            $ ("#pagination").append(PageNumbers);                	
        } 
        else if(PageCount==1)	
            document.getElementById("tdpaging").className="pagerblank"; 
        else
        {
            document.getElementById("tdpaging").className="pagerblank"; 
            $ ("#gridContainer1").append(casestudynotfound);	
        }
                
   });
}
