﻿
var MAX_SPONSOR_COUNT = 3;
var MAX_SPONSOR_HEIGHT = 90;
var MAX_SPONSOR_WIDTH = 200;

  window.addEvent('load',function(){
    GetSponsors();
  });
  
  
function GetSponsors(){


 var ajaxRequest = new Ajax('ajaxPages/getSponsors.ashx',
                  {method: 'get',                
                    onSuccess: SponsorsResults ,   
                    onFailure: SponsorsFailed
                  }
                ).request();   
}

function SponsorsResults(results){    

    var currentBlogObj =  eval ('(' + results + ')');                
    
    var list = document.createElement('ul');
   
    sponsorcount = currentBlogObj.Sponsors.Sponsor.length < MAX_SPONSOR_COUNT ? currentBlogObj.Sponsors.Sponsor.length :  MAX_SPONSOR_COUNT  ;
    
    
    randomIDs = buildArray(sponsorcount,currentBlogObj.Sponsors.Sponsor.length);
    
    
    
    for(var i = 0; i < randomIDs.length && i < MAX_SPONSOR_COUNT; i++){
    
        var li = document.createElement('li');
        var a = document.createElement('a');
        var img = document.createElement('img');
        
         img.id = 'sponsor' + i;
        
         a.appendChild(img);                 
         li.appendChild(a);
        
        
        img.alt = currentBlogObj.Sponsors.Sponsor[randomIDs[i]].Name;   
        
        if( currentBlogObj.Sponsors.Sponsor[randomIDs[i]].Img != null && currentBlogObj.Sponsors.Sponsor[randomIDs[i]].Img != "" ){
            img.src = currentBlogObj.Sponsors.Sponsor[randomIDs[i]].Img;                   
            
            if(navigator.appName == "Netscape"){
            
              resizeImage(img);
              
              var dif  = ((MAX_SPONSOR_HEIGHT - img.height)/2) -1 ;
              
              $(img).setStyle('margin-bottom',dif); 
              
              
              $(img).addEvent('load',function(){ 
                  resizeImage(this);
                  var dif  = ((MAX_SPONSOR_HEIGHT - this.height)/2) -1;
                  $(this).setStyle('margin-bottom',dif); 
                  
              });
            
            }
            
        }
        
        
        
        if(currentBlogObj.Sponsors.Sponsor[randomIDs[i]].SiteUrl != null && currentBlogObj.Sponsors.Sponsor[randomIDs[i]].SiteUrl != ""){
            a.href =  currentBlogObj.Sponsors.Sponsor[randomIDs[i]].SiteUrl;
        }else{
            a.href =  'http://www.comehometochico.com';
        }
        
        $('sponsorlogos').appendChild(li);
      //  list.appendChild(li);
    }    
    
    if(navigator.appName == "Microsoft Internet Explorer"){
      delayedResize();
    }
   

}

function SponsorsFailed(){
    
}

function delayedResize(){

  var sponsorlogos = $('sponsorlogos');

  for(var i = 1; i < sponsorlogos.childNodes.length; i++){
   
    var img = sponsorlogos.childNodes[i].firstChild.firstChild;  
    resizeImage(img);
    var dif  = ((MAX_SPONSOR_HEIGHT - img.height)/2) - 20 ;
    $(img).setStyle('margin-bottom',dif); 

  }

}



function resizeImage(image){
    
    var w = image.width;
    var h = image.height;
    
    if(image.height > MAX_SPONSOR_HEIGHT){

      var fa= 1-((h - MAX_SPONSOR_HEIGHT) / h);
      image.width=w * fa;
      image.height=h * fa;   
      
      if(navigator.appName == "Microsoft Internet Explorer"){
        return;
      }
         
    }
    
    w = image.width;
    h = image.height;

    if(image.width > MAX_SPONSOR_WIDTH){

      var f= 1-((w - MAX_SPONSOR_WIDTH) / w);
      image.width=w * f;
      image.height=h * f;

    }    
}



function buildArray(numOfItems, maxNum){
    theArray = new Array();
    y = 0;
    do {
        temp = getRand(maxNum);
       
        if (!testNum(temp, theArray))
        {
            theArray.push(temp);
            y++;
        }
       
    } while (y <= numOfItems);
    return theArray;
}

function getRand(maxNum){
    return (Math.floor(Math.random()*100)) % maxNum;
}
var counter = 25;
function testNum(testNum, theArray){
    testResult = 0;
    for (i=0; i <= theArray.length; i++){
        if (testNum == theArray[i])
        {
        testResult = 1;
        }
    }
    return testResult;
}