var Section = new Array();

Section['paint'] = {
			 'start' : 1,
			 'num': 115,
			 'TNpath': 'Paint_Thumbnails/PAINT_',
			 'PTNpath': 'Paint_PreviewThumbnails/PNTGS_'};
Section['comm'] = {
			 'start' : 1,
			 'num': 33,
			 'TNpath': 'CMSNS_Thumbnails/CMSNS_',
			 'PTNpath': 'CMSNS_PreviewThumbnails/CMSNS_'};
Section['studio'] = {
			 'start' : 1,
			 'num': 45,
			 'TNpath': 'Studio_Thumbnails/Studio_',
			 'PTNpath': 'Studio_PreviewThumbnails/Studio_'};
Section['client'] = {
			 'start' : 1,
			 'num': 69,
			 'TNpath': 'Client_Thumbnails/CLNT_',
			 'PTNpath': 'Client_PreviewThumbnails/CLNT_'};
Section['press'] = {
			 'start' : 1,
			 'num': 37,
			 'TNpath': 'Press_Thumbnails/PRESS_',
			 'PTNpath': 'Press_PreviewThumbnails/PRESS_'};
			 

function fill(section) {
	var section_startnum = Section[section].start;
	var section_num = Section[section].num;
	var thumbnails = '';
	var TNpath = Section[section].TNpath;
	var PTNpath = Section[section].PTNpath;
	for (var i=section_num; i>=section_startnum; i--) {
		thumbnails += '<div class="thumb show"><img src="' + TNpath + 'TN_' + i + '.jpg" width="41" height="28" alt="" border="0" onMouseOver="update_preview(\'' + section + '\',' + i + ')" /></div>\n';
	}
	$(section+"_preview").innerHTML = '<img src="' + PTNpath + 'PTN_' + section_num + '.jpg" width="203" height="136" alt="" border="0" />';
	$(section+"_thumbs").innerHTML = thumbnails;
}

function update_preview(section, num) {
	var preview = section + "_preview";
	var PTNpath = Section[section].PTNpath;
	$(preview).innerHTML = '<img src="' + PTNpath + 'PTN_' + num + '.jpg" width="203" height="136" alt="" border="0" />';
}

var pop;

function gallery_init () {
	var gallery = DOM.getElementsByClassName(document, "show");
	Array.prototype.forEach.call(gallery, function (thumb) {

		// Display enlarged image
		DOM.addEventListener(thumb, "click", function (evt) {
			images = thumb.getElementsByTagName("img");

			Array.prototype.forEach.call(images, function (img) {
				var file_regex = /(.*\_)Thumbnails(.*\_)TN(_.*\.\w{3})/;
				var filename = img.src.replace(file_regex, "$1" + "PopUps" + "$2" + "PU" + "$3");
				if (pop) pop.hide();
				pop = display_ippu(filename, thumb);
			});

			Event.stop(evt);
            return false;
        });		
    });

}


function display_ippu(filename, ele) {
    var Popup = new IPPU();
    Popup.init();	

	var inner = document.createElement("div");
	DOM.addClassName(inner, "zoom");
	inner.innerHTML = "<div class='closeX' onclick='pop.hide()'><img src='img/x.gif' /></div>";
	inner.innerHTML += "<img src='" + filename + "' alt='Loading... ..  .  .'/>"
	Popup.setContentElement(inner);

    Popup.setTitlebar(false);
    Popup.setFadeIn(true);
    Popup.setFadeOut(true);
    Popup.setFadeSpeed(4);
    Popup.setDimensions("auto", "auto");
	Popup.setClickToClose(true);
	

	var dim;
    if (ele) {
        // pop up the box right under the element
        dim = DOM.getAbsoluteDimensions(ele);
        if (!dim) return;
    }

	var s = DOM.getWindowScroll(window);
    if (IPPU.isSafari()) {
    	Popup.setLocation(dim.absoluteLeft - 600, dim.absoluteTop - 200);
    } else {
        Popup.setLocation(dim.absoluteLeft - 600, s.top + 40);
    }
    //Popup.center()
	Popup.show();

	return Popup;
}

