
// Create a namespace.
Nirva = {
    rotation_images: [],
    rotation_counter: 0,
    blank_img: 'images/blank-black.png',
    collection_items: null,
    collection_name: null
};

Nirva.setCollectionName = function(name) {
    this.collection_name = name;
}

Nirva.setCollectionItems = function(collection) {
    if (typeof collection != 'object') {
	throw Error('Invalid collection data.');
    }

    this.collection_items = collection;
}

Nirva.NewWindow = function(url, w, h, winname) {

    if (!winname) {
	winname = 'menuitem';
    }

    LeftPosition = 100;
    TopPosition  = 100;

    settings  = 'width=' + w + ',height=' + h;
    settings += ',top=' + TopPosition + ',left=' + LeftPosition;
    settings += ',scrollbars=yes,location=no,directories=no';
    settings += ',status=no,menubar=no,toolbar=no,resizable=yes';

    win = window.open(url, winname, settings, 1);
    
    if (win.focus) {
        win.focus();
    }

    return win;
}

function NewPressWindow(image, width, height) {

    if (! width) {
	width = 500;
    }
    if (! height) {
	height = 500;
    }

    var url = 'press_window.html?' + image;

    var win = Nirva.NewWindow(url, width, height, 'press');

    if (win) {
	if (win.focus) {
	    win.focus();
	}
	win.resizeTo(width, height);
    }
}

// Load main image into designated image tag.
// Add some margin space to the left if asked to.
// margin-left style values must match that image.mainimage.
function load_main_image(collection, image_name, image_name1, image_name2, text_name_top, text_name_bottom, more_margin) {
    var image_main   = document.getElementById('mainimage');
    var instructions = document.getElementById('instructions');
    var disp_table   = document.getElementById('displaytable');

    // Clear the board.
    Nirva.clearBoard();

    // Sanity check
    if (! image_main) {
	return false;
    }

    // undisplay any image that may be there.
    // ???!!!
    //image_main.style.display != 'block';

    if (instructions && instructions.style.display != 'none') {
	instructions.style.display = 'none';
    }

    if (disp_table && disp_table.style.display == 'none') {
	disp_table.style.display = 'block';
    }

    // I don't think we need this.
    var root_url = "/";
    root_url = '';

    if (more_margin) {
	image_main.style.marginLeft = '4em';
    } else {
	image_main.style.marginLeft = '1em';
    }

    // Load image and reveal
    image_main.src = root_url + collection + "/" + image_name;
    image_main.style.display = 'block';

    // Do the second and third images
    if (image_name1) {
	var image_sub1 = document.getElementById('subimage1');
	if (image_sub1) {
	    image_sub1.src = root_url + collection + "/" + image_name1;
	    image_sub1.style.display = 'block';
	}
    }
    if (image_name2) {
	var image_sub2 = document.getElementById('subimage2');
	if (image_sub2) {
	    image_sub2.src = root_url + collection + "/" + image_name2;
	    image_sub2.style.display = 'block';
	}
    }

    if (text_name_top) {
	var desc_top = document.getElementById('desc_top');
	if (desc_top) {
	    desc_top.innerHTML = descriptions[text_name_top];
	}
    }

    if (text_name_bottom) {
	var desc_bottom = document.getElementById('desc_bottom');
	if (desc_bottom) {
	    desc_bottom.innerHTML = descriptions[text_name_bottom];
	}
    }
	
    return;
}

// Load main image into designated image tag.
// Add some margin space to the left if asked to.
// margin-left style values must match that image.mainimage.
Nirva.displayItem = function(itemName, more_margin) {
    if (! itemName || ! Nirva.collection_items || ! Nirva.collection_items[itemName]) {
	return;
    }

    var image_main   = document.getElementById('mainimage');
    var instructions = document.getElementById('instructions');
    var disp_table   = document.getElementById('displaytable');

    // Sanity check. If main image doesn't exist the page is invalid.
    if (! image_main) {
	return false;
    }

    // Clear the board.
    Nirva.clearBoard();

    if (instructions && instructions.style.display != 'none') {
	instructions.style.display = 'none';
    }

    if (disp_table && disp_table.style.display == 'none') {
	disp_table.style.display = 'block';
    }

    // I don't think we need this.
    var root_url = "/";
    root_url = '';

    if (more_margin) {
	image_main.style.marginLeft = '4em';
    } else {
	image_main.style.marginLeft = '1em';
    }

    var item = Nirva.collection_items[itemName];

    // Load image and reveal
    image_main.src = root_url + Nirva.collection_name + "/" + item.image1;
    image_main.style.display = 'block';

    // Do the second and third images
    if (item.image2) {
	var image_sub1 = document.getElementById('subimage1');
	if (image_sub1) {
	    image_sub1.src = root_url + Nirva.collection_name + "/" + item.image2;
	    image_sub1.style.display = 'block';
	}
    }
    if (item.image3) {
	var image_sub2 = document.getElementById('subimage2');
	if (image_sub2) {
	    image_sub2.src = root_url + Nirva.collection_name + "/" + item.image3;
	    image_sub2.style.display = 'block';
	}
    }

    // Display the text.
    if (item.text_top) {
	var desc_top = document.getElementById('desc_top');
	if (desc_top) {
	    desc_top.innerHTML = item.text_top;
	}
    }

    if (item.text_bottom) {
	var desc_bottom = document.getElementById('desc_bottom');
	if (desc_bottom) {
	    desc_bottom.innerHTML = item.text_bottom;
	}
    }
	
    return;    
}

// Find the default display item from the collection and click() it!
// The default item is an image tag with id of 'default_collection_id'.
Nirva.displayDefaultItem = function() {
    var first_item = document.getElementById('default_collection');

    if (first_item && first_item.onclick) {
	first_item.onclick();
    }
}

// Clear existing elements in preperation for updating it.
Nirva.clearBoard = function() {
    // Clear descriptions before iamges so text won't shift to left.
    var desc_top    = document.getElementById('desc_top');
    var desc_bottom = document.getElementById('desc_bottom');
    if (desc_top)
	desc_top.innerHTML = '';
    if (desc_bottom)
	desc_bottom.innerHTML = '';

    // Clear images.
    // Setting src to '' causes IE to display a broken image icon.
    var image_main = document.getElementById('mainimage');
    if (image_main) {
	image_main.src = Nirva.blank_img;
    }

    var image_sub1 = document.getElementById('subimage1');
    if (image_sub1) {
	image_sub1.src = Nirva.blank_img;
    }

    var image_sub2 = document.getElementById('subimage2');    
    if (image_sub2) {
	image_sub2.src = Nirva.blank_img;
    }
}

// Change the purchase image.
// Requires Nirva.purchase_images and Nirva.purchase_image_id to be set up on the calling page.
// Nirva.purchase_images:   array of image paths. Must match order of select items (color, style) if used with paypal.
// Nirva.purchase_image_id: id of the image element which holds the purchase image.
// Currently these functions work with only one purchase item on a page.
function load_purchase_image() {
    var select_elem = document.getElementById(Nirva.purchase_selector_id);
    if (! select_elem) {
        return false;
    }

    var display_image = document.getElementById(Nirva.purchase_image_id);
    if (! display_image) {
        return false;
    }

    // This may or may not exist.
    var text_div = document.getElementById(Nirva.purchase_text_id);

    //display_image.src = '';	// clear the image.
    display_image.style.display = 'none';	// clear the image.
    var image_src = Nirva.purchase_images[select_elem.selectedIndex];
    var src_re    = /__TEXT_(\d+)/;
    var text_match;

    if (text_match = src_re.exec(image_src)){
	if (! text_div) {
	    return false;
	}

	display_image.style.display = 'none';
	text_div.style.display      = 'block';
	text_div.innerHTML          = Nirva.purchase_text[text_match[1]];
    } else {
	if (text_div) {
	    text_div.style.display = 'none';
	}

	display_image.src = image_src;
	display_image.style.display = 'inline';
    }

    return true;
}

// Load the purchase image based on the thumbnail that's clicked on.
// Set the index of the select elem to select_index and call load_purchase_image().
function load_from_thumbnail(select_index) {
    if (Nirva.purchase_image_id == undefined || select_index == undefined) {
        return false;
    }

    var select_elem = document.getElementById(Nirva.purchase_selector_id);
    if (! select_elem) {
	return false;
    }
    select_elem.selectedIndex = select_index;

    var ret = load_purchase_image();

    return true;
}

// Set the rotation image list.
Nirva.setRotationImages = function(images) {
    if (images instanceof Array) {
	Nirva.rotation_images = images;
    } else if (arguments.length) {
	Nirva.rotation_images = arguments;
    }
}

// Rotate the intro main image from a list in Nirva.intro_images array.
Nirva.rotateIntroImage = function() {
    if (! Nirva.rotation_images || Nirva.rotation_images.length == 0) {
        return;
    }

    document.getElementById('intro_image').src = Nirva.rotation_images[Nirva.rotation_counter];

    if (Nirva.rotation_counter == (Nirva.rotation_images.length - 1)) {
        Nirva.rotation_counter = 0;
    } else {
        Nirva.rotation_counter++;
    }
}
