﻿var interval = 2.5; // delay between rotating images (in seconds)
var random_display = 2; // 0 = no, 2 = yes
interval *= 1000;

var large_file_count = 0;small_file_count = 0;welcome_file_count = 0;center_file_count = 0;image_index = 0;
var num_small_image_places  = 8, image_index_small = 0, image_index_center = 0, image_index_welcome = 0;
var number_of_images_large, number_of_images_small, number_of_images_welcome = 0, number_of_images_center;
var image_list_large, image_list_small, image_list_welcome, image_list_center;

image_list_large = new Array();
image_list_small = new Array();
image_list_welcome = new Array();
image_list_center = new Array();

var smallLocationsArray = new Array(num_small_image_places+1); // the LOCATIONS that contain the small images

// init our array
for (var b =0;b<smallLocationsArray.length;b++){
    smallLocationsArray[b] = 0; // we store the INDEX of image_list_small here - init to 0 
}

//productImage constructor
function productImage( productID, fileName )
{
	this.productID = productID;
	this.fileName = fileName;
	this.displayed = false;
}

function imageItem(image_location) {
	this.image_item = new Image();
	this.image_item.src = image_location;
}

function getImageItemLocation(imageObj) {
	return(imageObj.image_item.src)
}

function generate(x, y) {
	var range = y - x + 1;
	return Math.floor(Math.random() * range) + x;
}

function getNextImage(){

    if (random_display) {
	    
	    image_index = generate(0, number_of_images-1); 
	    
	   // do {image_index = generate(0, number_of_images-1)}
	    //    while(!isDisplayed(image_index));   
	 
		//image_index = generate(0, number_of_images-1); 
	}
	else {
		image_index = (image_index+1) % number_of_images;
	}
		
	var new_image = getImageItemLocation(image_list_large[image_index]);
	
	return(new_image);
}

////////////////////////////////////////////////////////////////////////
// generateSmallImageIndex - return the INDEX
////////////////////////////////////////////////////////////////////////
function generateSmallImageIndex(locationNum) {
    document.getElementById('test').innerHTML = "";
    // we don't display the same image loaded more than once.
	image_index_small = generate(0, number_of_images_small-1);
	
	///////////////////////////////////////////////////////////////////////////
	// loop until we find an image that isn't being displayed
	 do {
	    image_index_small = generate(0, number_of_images_small-1);
	   // document.getElementById('test').innerHTML += "image_index_small=" + image_index_small + "<br/>";
	   // document.getElementById('test').innerHTML += "image_list_small[image_index_small].displayed=" + image_list_small[image_index_small].displayed + "<p/>";
	    }
	        while(image_list_small[image_index_small].displayed);
	///////////////////////////////////////////////////////////////////////////    

	image_list_small[image_index_small].displayed = true; // set the new one to TRUE
	image_list_small[smallLocationsArray[locationNum]].displayed = false; // set the image being replaced to FALSE
	smallLocationsArray[locationNum] = image_index_small; // update the 
	
   // document.getElementById('test').innerHTML += "image_index_small=" + image_index_small + "<br/>";
    
	return(image_index_small);
}

function getNextImageWelcome(){

    if (random_display) {
	    
	    image_index_welcome = generate(0, number_of_images_welcome-1); 
	}
	else {
		image_index_welcome = (image_index_welcome+1) % number_of_images_welcome;
	}

	var new_image_welcome = getImageItemLocation(image_list_welcome[image_index_welcome]);

	return(new_image_welcome);
}

function getNextImageCenter(){

    if (random_display) {
	    
	    image_index_center = generate(0, number_of_images_center-1); 
	}
	else {
		image_index_center = (image_index_center+1) % number_of_images_center;
	}

	var new_image_center = getImageItemLocation(image_list_center[image_index_center]);

	return(new_image_center);
}

function rotateImage(place) {
     
	// large images
	var new_image = getNextImage();
	document.getElementById(place).setAttribute('src',new_image);
	
	document.getElementById("linkLarge").setAttribute('href','http://www.jennysplacega.com/products.asp?e_code=2&subcat_id=' + extractID2(new_image));
	
	/////////////////////////////
	// small product images	
    /////////////////////////////	
			
	var place_small, link_small, locationNum;
	
	locationNum = generate(1, num_small_image_places); // choose a random position (image) to replace
	
	var si_ix = generateSmallImageIndex(locationNum);
	
	place_small = "s_image" + locationNum; 
	
	link_small = "link" + locationNum; 

	document.getElementById(place_small).setAttribute('src',image_list_small[si_ix].fileName); //image_list_small[pix].fileName

	document.getElementById(link_small).setAttribute('href','http://www.jennysplacega.com/product_detail.asp?product_id=' + image_list_small[si_ix].productID);
	//alert(image_index_small);
	// welcome images
	var new_image_welcome = getNextImageWelcome();
	document.getElementById('welcome').setAttribute('src',new_image_welcome);
	
	// center images
	var new_image_center = getNextImageCenter();
	document.getElementById('center').setAttribute('src',new_image_center);	
	document.getElementById("linkCenter").setAttribute('href','http://www.jennysplacega.com/products.asp?e_code=2&subcat_id=' + extractID2(new_image_center));
	
	
	var recur_call = "rotateImage('"+place+"')";
	
	setTimeout(recur_call, interval);
}

function extractID(fileName) {
   
	//var array1 = fileName.split("=");
    var array1 = fileName.split(".");
    
	//var array2 = array1[array1.length-1].split(".");
	 
	return(array1[0]);
}

function extractID2(fileName) {
   
	var array1 = fileName.split("/");
    //var array1 = fileName.split(".");
    
	var array2 = array1[array1.length-1].split(".");
	 
	return(array2[array2.length-2]);
}

function extractID3(fileName) {
   
	var array1 = fileName.split("=");
    //var array1 = fileName.split(".");
    
	var array2 = array1[array1.length-1].split(".");
	 
	return(array2[array2.length-1]);
}

function loadImages(){

    var ix;
    var place_small, link_small, locationNum, pix;
    
    for (var x = 1; x < num_small_image_places+1; x++)
    {
        theImage = "s_image" + x;
	    // small product images	
	    pix = generateSmallImageIndex(x);
	    	
	    place_small = "s_image" + x; 
	
	    link_small = "link" + x; 

	    document.getElementById(place_small).setAttribute('src',image_list_small[pix].fileName);

	    document.getElementById(link_small).setAttribute('href','http://www.jennysplacega.com/product_detail.asp?product_id=' + image_list_small[pix].productID);
	       
    }

}

function initImages(){    
    image_index_small = num_small_image_places; //reset the index
    loadImages();
    rotateImage('rImage');
}


