/*
 * Main js file for Promo Carousel yui control. This gets displayed
 * in the biz page of those businesses that have a promo running.
 */

/******************************************************************************/

var fmtItem = function(imgUrl, url, title, cats) {
  var innerHTML = 
  '<a href="' + 
  url + 
  '"><img src="' + 
  imgUrl +
  '" width="' +
  75 +
  '" height="' +
  75+
  '"/>' + 
  title + 
  '</a>' +
  '<br/>' + cats;

  return innerHTML;
}

var xmlHttpCarousel;

function createXMLHttpRequestCarousel() {

  xmlHttpCarousel = null;

  if(window.ActiveXObject) {
    xmlHttpCarousel = new ActiveXObject("Microsoft.XMLHTTP");
  } else if(window.XMLHttpRequest) {
    xmlHttpCarousel = new XMLHttpRequest();
  }  

  // handleCarouselResponse is a method in your javascript code.
  xmlHttpCarousel.onreadystatechange = handleCarouselResponse;
  

}

function handleCarouselResponse() {
  if(xmlHttpCarousel.readyState == 4) { // the state when the request has completed
    if (xmlHttpCarousel.status == 200) { // the HTTP status code returned from the server
      var jsonData = xmlHttpCarousel.responseText;
      //alert(jsonData);
      var myJSONObject = eval('(' + jsonData + ')');
      for (var i = 0; i < myJSONObject.length; ++i) {
        var str = myJSONObject[i].bizImgUrl;
        if (str.match('placeholder_smal.gif')) continue;
        var citem = fmtItem(myJSONObject[i].bizImgUrl, 
                          myJSONObject[i].bizUrl, 
                          myJSONObject[i].bizName,
                          myJSONObject[i].bizCats);
        carousel.addItem(citem);
      }
      carousel.scrollForward();
    }
  }
}

var makeAjaxRequest = function() 
{

  // If you change the itemCount, you need to modify the
  // handleCarouselResponse too!

  xmlHttpCarousel.open(
  "GET", 
  "/ajaxhandler.do?dispatch=getCarouselDataJSON&itemCount=32");
  
  xmlHttpCarousel.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
  xmlHttpCarousel.send(null); 

}

/***********************************************************************************/

var carousel;


function showCarouselExample() {
  var i, spotlight;
  if (!carousel) {
    //alert("carousel not found");
    carousel = new YAHOO.widget.Carousel("container", {
      numItems: 500,
      scrollIncrement: 30,
      isCircular: true,
      animation: {speed: 180, effect: YAHOO.util.Easing.easeNone},
      numVisible: 5
    });

  }

  carousel.render();
  carousel.show();
  createXMLHttpRequestCarousel();
  makeAjaxRequest();

}  

YAHOO.util.Event.onContentReady("container", function (ev) { 
  var browserName=navigator.appName;
  if (browserName == 'Netscape')
    document.getElementById('container').style.left = '0px';
  else
    document.getElementById('container').style.left = '50px';

  showCarouselExample(); 
  setInterval(showCarouselExample, 120000);
});

/*************************** EOF **************************/
