// gallery.js

function gbl(prop) {
	switch (prop) {
		case "LOADING_BAR": return '/vendor/'+VENDOR_DIR+'/images/config_load_bar.gif';	
	}
}

var images = new Array();
var __currentImage = -1;

function initGallery() {
	updateImages();
}

function clearImages() {
	__currentImage = -1;
	images = new Array();
	$("div[@id=thumbs]").html('<img src="'+gbl('LOADING_BAR')+'" border="0" />');
	$("span[@id=gallery_counters]").attr("style", "display:none");
}

function addImage(elem) {
	var img = {
		id: 				$(elem).attr('id'),
		name: 				$(elem).attr('model'),
		model_id: 			$(elem).attr('model_id'),
		blindshadetype:		$(elem).attr('blindshadetype'),
		color: 				$(elem).attr('swatch'),
		swatch_id: 			$(elem).attr('swatch_id'),
		swatch_oekey: 		$(elem).attr('swatch_oekey'),
		swatch_img: 		$(elem).attr('swatch_img'),
		swatch_thumb: 		$(elem).attr('swatch_thumb'),
		category_id:		$(elem).attr('category_id'),
		category: 			$(elem).attr('category'),
		category_rewrite: 	$(elem).attr('category_rewrite'),
		swatch_browsing: 	$(elem).attr('swatch_browsing'), 
		swatch_ordering: 	$(elem).attr('swatch_ordering'), 
		configure_print: 	$(elem).attr('configure_print'), 
		configure_order: 	$(elem).attr('configure_order')
	};
    var index = images.length;
    images[images.length] = img;
	$("div[@id=thumbs]").append('<img src="'+PRODUCT_IMAGES_URL+'/gallery/thumb/'+img['id']+'.jpg" id="thumb'+index+'" onclick="showImage('+index+');" height="50" width="70" border="0" alt="" />');
}

function updateArrows() {
	$("span[@id=prev_arrow]").attr("style", "display:"+(__currentImage<=0 ? "none" : "inline"));
	$("span[@id=next_arrow]").attr("style", "display:"+(__currentImage>=0 && images.length-1!=__currentImage || __currentImage<=0 && images.length>1 ? "inline" : "none"));
}	
function prevImage() {
	if (__currentImage > 0)
		showImage(__currentImage - 1);
	return false;
}
function nextImage() {
	if (__currentImage < images.length - 1 && __currentImage >= 0)
		showImage(__currentImage + 1);
	return false;
}

function updateImages() {
	var pid = $("select[@name=product]").val();
	if (!pid) pid = MODEL_CAT_ID;
	var rid = $("select[@name=room]").val();
	var bstype = $("select[@name=blindshadetype]").val();

	if (!pid || !rid) { return; }

	clearImages();

	$.ajax({
		url: '/ideas/gallery-list.php?type=getimages&pid='+escape(pid)+'&rid='+escape(rid)+'&bstype='+escape(bstype),
		type: 'POST',
		dataType: 'xml',
		timeout: 15000,
		error: function(robj, etype, eobj) {
		},
		success: function(xml) {
			$("div[@id=thumbs]").html('');
			images = new Array(); // clear the images array.
			$(xml).find('image').each(function() {
				addImage($(this)); // add each image that was returned.
			});
			updateArrows();
			if (images.length == 0) {
				$("div[@id=thumbs]").html('Sorry, no photos are available for the above selections.');
				$("span[@id=gallery_counters]").hide();
			} else {
				$("span[@id=gallery_total]").html(images.length);
				$("span[@id=gallery_counters]").attr("style", "display:inline");
			}
			if (__currentImage < 0) 
				showImage(0); // show initial image
		}
	});
}

function updateRooms() {
	var pid = $("select[@id=product]").val();

	if (!pid) { return; }

	$.ajax({
		url: '/ideas/gallery-list.php?type=getrooms&pid='+escape(pid),
		type: 'POST',
		dataType: 'xml',
		timeout: 15000,
		error: function(robj, etype, eobj) {
		},
		success: function(xml) {
			current_value = $("select[@id=room]").val();
			$("select[@id=room]").empty(); // empty all options
			$("select[@id=room]").append('<option value="none">All</option>');
			idx=1;
			keep_selected = 0;
			$(xml).find('room').each(function() {
				$("select[@id=room]").append('<option value="'+$(this).attr('id')+'">'+$(this).attr('name')+'</option>');
				if ($(this).attr('id') == current_value) {
					keep_selected = idx;	
				}
				idx++;
			});
			document.getElementById('room').selectedIndex = keep_selected;	// can't use jQuery here b/c of a bug
			updateImages();
		}
	});
}

function updateProducts() {
	var rid = $("select[@id=room]").val();

	if (!rid) { return; }

	$.ajax({
		url: '/ideas/gallery-list.php?type=getproducts&rid='+escape(rid),
		type: 'POST',
		dataType: 'xml',
		timeout: 15000,
		error: function(robj, etype, eobj) {
		},
		success: function(xml) {
			
			current_value = $("select[@id=product]").val();
			$("select[@id=product]").empty(); // empty all options
			$("select[@id=product]").append('<option value="none">All</option>');
			idx=1;
			keep_selected = 0;
			$(xml).find('product').each(function() {
				$("select[@id=product]").append('<option value="'+$(this).attr('id')+'">'+$(this).attr('name')+'</option>');
				idd = $(this).attr('id');
				if ($(this).attr('id') == current_value) {
					keep_selected = idx;
				}
				idx++;
			});
			if (document.getElementById('product'))
				document.getElementById('product').selectedIndex = keep_selected;  // can't use jQuery here b/c of a bug
			updateImages();
		}
	});
}


function showImage(index) {
	var releasecover = false;

	if (index < 0) 								index = 0;
	if (index >= images.length) 				index = images.length - 1;
	if (index == __currentImage)  				return;
    if (__currentImage == -1 && index >= 0) 	releasecover = true;
   
	$("img[@id=thumb"+__currentImage+"]").attr("className", "");
	$("img[@id=thumb"+index+"]").attr("className", "selected");

	__currentImage = index;
    
	$("span[@id=swatch_link]").hide();
	$("span[@id=config_link]").hide();
	$("p[@id=swatch_oekey]").html('');
	
	// Replace the previous image with the loader bar.
	$("img[@id=main_image]").attr("src", gbl('LOADING_BAR'));
	$("div[@id=info]").html('<h2 class="avant-white">'+images[index].name+'</h2>'+
							'<strong>type:</strong> '+images[index].category+'<br/>'+
							'<strong>color:</strong> '+images[index].color+'<br/>'+
							(images[index].blindshadetype ? '<strong>blind & shade type:</strong> '+images[index].blindshadetype : ''));

	// Update the text with the sIFR rendering.
	if(typeof sIFR == "function") {
		sIFR.replaceElement(named({sSelector:"body .avant-white", sFlashSrc:"/vendor/default/fonts-sifr/avant_book.swf", sColor:"#ffffff", sBgColor:"#544d45",sWmode:"transparent"}));
	};

    var mimg = new Image();
    mimg.onload = function() { 
		$("img[@id=main_image]").attr("src", mimg.src); 
		$("a[@id=magnify_swatch]").attr('href', PRODUCT_IMAGES_URL+'/swatches/'+images[index].swatch_img);
		if (images[index].swatch_ordering == '1') {
			$("img[@id=swatch_corner]").attr('src','/images/product_corner_maroon.gif');
			$("img[@id=swatch_corner]").attr('width',90);
			$("img[@id=swatch_corner]").attr('height',90);
			$("span[@id=swatch_link]").show();
			$("a[@id=swatch_href]").attr('href','/products/'+images[index].category_rewrite+'/colors.php?mcid='+images[index].category_id);
		}
		else if (images[index].swatch_browsing == '1') {
			$("img[@id=swatch_corner]").attr('src','/images/product_corner_browse.gif');
			$("img[@id=swatch_corner]").attr('width',120);
			$("img[@id=swatch_corner]").attr('height',120);
			$("span[@id=swatch_link]").show();
			$("a[@id=swatch_href]").attr('href','/products/'+images[index].category_rewrite+'/colors.php?mcid='+images[index].category_id);
		}
		if (images[index].configure_order == '1') {
			$("span[@id=config_link]").show();
			$("a[@id=config_href]").attr('href','/store/configurator.php?mid='+images[index].model_id);
		}
	}
	mimg.src = PRODUCT_IMAGES_URL+'/gallery/full/' + images[index].id + '.jpg';
	$("img[@id=swatch_image]").attr("src", PRODUCT_IMAGES_URL+'/swatches/' + images[index].swatch_thumb); // this fires the "mimg.onload" function above
	$("p[@id=swatch_oekey]").html(images[index].swatch_oekey);
	$("div[@id=swatch]").show();
	$("span[@id=gallery_index]").html(index+1);
	updateArrows();
}
