//call handler initialization function for banners
//setLoadFunctions();

//********************************************************************************
// ONLOAD - function that adds handlers/function calls to the onload event
//********************************************************************************
function setLoadFunctions()
{ 
  var prevonload = window.onload;
  
  window.onload = function()
  {
    //call anything already set (ie. in the body tag)
    if(prevonload) prevonload();   
  
    //*** add any functions to call onload here!!!
    //rotateImage('1') ;
    //rotateImage('2');
    //rotateImage('3');
  } 
}
//********************************************************************************


//********************************************************************************
// Small Banner Rotation Functionality 
//********************************************************************************

var interval = 2500; // delay between rotating images (sec * 1000)
var random_display = 1; // 0 = no, 1 = yes

var image_index = 0;
var url_index = 0;
var number_of_image;

//these arrays are to be populated with image information (done elsewhere)
image_list = new Array();
url_list = new Array();

function imageItem(image_location) 
{
	this.image_item = new Image();
	this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) 
{
	return(imageObj.image_item.src)
}
//randomizing function
function generate(x, y) 
{
	return Math.floor(Math.random() * (y - x + 1)) + x;
}
//choses another image in the rotation
function getNextImage() 
{
	var new_image;
	
	if (random_display) image_index = generate(0, number_of_image-1);
	else image_index = (image_index+1) % number_of_image;
	
	new_image = get_ImageItemLocation(image_list[image_index]);
	return(new_image);
}

//function called to start rotating an image
function rotateImage(place) 
{
	number_of_image = image_list.length;//check length here in case it changed (global var)
	var iPrefix = 'sImage';//append common prefix to images
	var uSuffix = 'Url'; //url also gets 'Url' suffix after prefix and before integer id
	var targetImg;
	var targetUrl;
	var recur_call = "rotateImage('"+place+"')";
	
	//var new_image = getNextImage();
	if (random_display) image_index = generate(0, number_of_image-1);
	else image_index = (image_index+1) % number_of_image;

	var new_image_src = get_ImageItemLocation(image_list[image_index]);

	targetImg = document.getElementById(iPrefix+place);
	if(targetImg) targetImg.src = new_image_src;
	
	targetUrl = document.getElementById(iPrefix+uSuffix+place);
	if(targetUrl) targetUrl.href = "http://www.window-fashions.com/" + url_list[image_index];	
	
	setTimeout(recur_call, interval);
}
//********************************************************************************

//********************************************************************************
// AJAX MULTIMEDIA BANNERS - functions used to rotate/reload div content using AJAX methods 
//********************************************************************************

// the urls corresponding to the divs to be updated, ids must match ending in div name
var tPrefix = ""; // should work on dev and live in ie and firefox
//if(true) tPrefix = "http://www.siriusweblabs.com/staging/window-fashions_com/";//for staging
//tPrefix = "http://eros/windowfashions_com/";// dev (if necessary)


var tArray = new Array();
var dArray = new Array();//track initial delays
var flInt = 10000; //duration (5000 = 5 sec)

//update the content based on AJAX results
function changeSource(dId, newSource)
{
  var targetName = "bannerDiv"+dId;
  var target;

  target = document.getElementById(targetName);  
  if(target && newSource) target.innerHTML = newSource;
}

//custom function for requesting info using AJAX
function refreshBannerAjax(dId)
{  
  //alert('refreshing');
  var requestUrl = "";
  var AjaxObj; // XML/AJAX object banner rotation
  var AjaxResult = "";
 
  if (window.XMLHttpRequest)  AjaxObj = new XMLHttpRequest(); // Firefox, Safari, ...    
  else if (window.ActiveXObject) AjaxObj = new ActiveXObject("Microsoft.XMLHTTP");// ActiveX version for IE 
  
  if(AjaxObj)
  {
    //alert('got ajax obj');	
	//path:  "" + "includes/bannerShow.cfm?uid=1"
	//
    requestUrl = tPrefix+tArray[dId];
	//alert(requestUrl);
	AjaxObj.onreadystatechange = function()
	{
	  try
	  {
		  //transfer completed (0,1,2,3,4 with 4 being complete)
		  if(AjaxObj.readyState == 4)
		  {
			//success (404,305,200... with 200 being success)
			if(AjaxObj.status == 200)
			{
			  AjaxResult = AjaxObj.responseText;			  
			  //call custom function to update the target
			  changeSource(dId, AjaxResult);
			}
			else
			{
				//alert('AJAX comm error.. '+AjaxObj.status);
				AjaxResult = "";				
			}
		  }
	  }
	  catch(e)
	  {
		//alert('Caught Exception: ' + e.description);
	  }
	};
	//define the command / initialize request // note that GET may be cached by IE
    AjaxObj.open("POST", requestUrl,  true);
  
    //send to server (need some data or firefox will give status 411 and not send)
    AjaxObj.send('test=1');
  }  
  
  // handle delaying first time, second for each id
  if(!dArray[dId] || dArray[dId].length <=0)
  {
	dArray[dId]=dId;
	setTimeout("refreshBannerAjax("+dId+")", flInt+(dId*2000)-2000);
  }
  else setTimeout("refreshBannerAjax("+dId+")", flInt);
}
//********************************************************************************