var promotions;
var currentIndex;
var timer;
var paused;
var pageName;
var galleryName;

function loadPromotions(marketId)
{
    if (marketId == null) 
    {
        marketId = 0;
    }
    
    if (marketId == 0)
    {
        pageName = "home";
        galleryName = "myGallery";
    }
    else
    {
        pageName = "market";
        galleryName = "myGalleryMkt";
    }
    
    var xmlHttp;
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
          try
          {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch (e)
          {
                try
                {
                  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                  alert("Your browser does not support AJAX!");
                  return false;
                }
           }
      }
      
      xmlHttp.onreadystatechange=function()
      {
        if(xmlHttp.readyState==4)
        {
         
             promotions = eval('(' + xmlHttp.responseText + ')');
             setPromotions();
             startGallery();
             
            
              //BELOW - used for the test widget            
              
             //default index
             //currentIndex = -1;  
             //moveIndex(true);
             
             //set timer
             //start();          
        }
      }
      
      xmlHttp.open("GET","/handlers/promotionshandler.ashx?marketId=" + marketId,true);
      xmlHttp.send(null);
}

function startGallery() 
{
    var myGallery = new gallery($(galleryName), {
        timed: true,
        showArrows: true,
        showInfopane: true,
        showCarousel: false,
        embedLinks: true,
        delay: 9000,
        page: pageName
    });
    document.gallery = myGallery;
}

function setPromotions()
{
    var html = "";
    var image = "";
    
    for(var i = 0; i < promotions.length; i++)
    {
        image = (pageName == "home") ? promotions[i].Image : promotions[i].ShortImage;
    
        if (promotions[i].TitleFrame == true)
        {
            html += "<div class=\"imageElement\">";
            html += "<h3><div class=\"slideHtTop\">&nbsp;</div>";
            html += "<a href=\"#\">The Perfect Place to Plan<div class=\"slideHtMid\">&nbsp;</div>Your Wedding...</a></h3>";
            html += "<p><a href=\"#\">&nbsp;</a></p>";
            html += "<a href=\"#\" title=\"The Perfect Place to Plan Your Wedding...\" class=\"open\"></a>";
            html += "<img src=\"" + image + "\" class=\"full\" alt=\"The Perfect Place to Plan Your Wedding...\" title=\"The Perfect Place to Plan Your Wedding...\" />";
            html += "</div>";
        }
        else
        { 
            html += "<div class=\"imageElement\">";
            html += "<h3><a href=\"" + promotions[i].Url + "\">" + promotions[i].Title + "</a></h3>";
            html += "<p><a href=\"" + promotions[i].Url + "\">" + promotions[i].Description + "</p>";
            html += "<a href=\"" + promotions[i].Url + "\" title=\"" + promotions[i].UrlText + "\" class=\"open\"></a>";
            html += "<img src=\"" + image + "\" class=\"full\" title=\"" + promotions[i].UrlText + "\" alt=\"" + promotions[i].Title + "\"/> ";
            html += "</div>";				    
        }
    }
    
    document.getElementById(galleryName).innerHTML += html;
}


//used for the test widget
function start()
{
    timer = window.setInterval("moveIndex(true)", 5000); 
}

//used for the test widget
function pause()
{
    if (paused)
    {
        start();
        paused = false;
    }
    else
    {
        window.clearInterval(timer);
        paused = true;
    }
}

//used for the test widget
function moveIndex(forward)
{
    var html = "";
    setIndex(forward);
    
    html+= "<div><center><font color=\"white\"><h2>";
    html+= promotions[currentIndex].Title + "</h2><br/>" + promotions[currentIndex].Description + "<br/><a href=\"" + promotions[currentIndex].Url + "\">" + promotions[currentIndex].UrlText + "</a><br/><br/>";
    html+= "<img src=\"/" + promotions[currentIndex].Image + "\">";
    html+= "</font></center></div>";
   
    document.getElementById("thisDiv").innerHTML = html;
}

//used for the test widget
function setIndex(forward)
{
    if (forward)
    {
        if (currentIndex + 1 >=  promotions.length) currentIndex = 0;
        else currentIndex = currentIndex + 1; 
    }
    else 
    {
        if (currentIndex - 1 < 0) currentIndex = promotions.length -1;
        else currentIndex = currentIndex - 1;
    }
}
