function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function nl2br (str, is_xhtml) {
    var breakTag = '';
    breakTag = '<br/>';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}
function removeLightBox() {
	$('a.extra').remove();
	$('div.main_img #options').remove();
	if ($('a#lightBoxWrap').length) {
		//unwrap it..
		var html = $('a#lightBoxWrap').html();
		var parent = $('a#lightBoxWrap').parent();
		$('a#lightBoxWrap').remove();
		parent.prepend(html);
	}
}
function setOptions(lamp) {
	if (lamp.options.option) {
		//add the 'options' div
		$('div.main_img').append('<div id="options"></div>');
		//wrap the main image
		var option = lamp.options.option.length ? lamp.options.option[0] : lamp.options.option;
		$('#big_img').wrap('<a href="'+option.image+'" title="'+option.name+'" class="lightBox" id="lightBoxWrap"></a>');
		//now do the rest
		if (lamp.options.option.length) {
			for (var i=0; i<lamp.options.option.length; i++) {
				if (i != 0) {
					var option = lamp.options.option[i];
					$('div.main_img').append('<a href="'+option.image+'" title="'+option.name+'" class="lightBox extra" style="display:none;">'+option.name+'</a>');
				}
			}
		}
		//initialize the options -- get the path to the images..
		var img = option.image;
		var path = img.split('/');
		var lbImagePath = '';
		for (var i=0; i<path.length; i++) {
			if (path[i] != 'img') {
				lbImagePath += path[i]+'/';
			} else {
				break;
			}
		}
		//now, imagePath is a path to the assets folder
		lbImagePath += 'js/lightbox/images';
		$('a.lightBox').lightBox({
			overlayBgColor: '#000',
			imageLoading: lbImagePath+'/loading.gif',
			imageBtnClose: lbImagePath+'/close.gif',
			imageBtnPrev: lbImagePath+'/prev.gif',
			imageBtnNext: lbImagePath+'/next.gif',
			imageBlank: lbImagePath+'/blank.gif'
		});
	}
}

$(function() {
	//load the images from the json object
	var imagesLoaded = 0;
	if (!lamps) {
		return false;
	}
	var ll = lamps.length;
	var images = [];
	for (var i=0; i<ll; i++) {
		var lamp = lamps[i];
		if (lamp) {
			var imageSrc = imageDir+lamp.image;
			var img = new Image();
			images.push(img);
			images[i].onload = function() {
				imagesLoaded++;
			};
			img.src = imageSrc;
			if (i == 0) {
				//set the options
				setOptions(lamp);
			}
		}
	}
	$('a.lamp').unbind('click').click(function() {
		//remove any lightBox links that have been added
		removeLightBox();
		//now go on...
		if (imagesLoaded >= images.length) {
			var thisId = $(this).attr('id');
			var lamp = lamps[thisId];
			//set everything up...
			var thisImage = lamp.image;
			var imgSrc = imageDir+thisImage;
			$('div.main_img img').attr('src', imgSrc);
			//now do the info
			var sku = typeof(lamp.sku) == 'string' && lamp.sku ? '<span class="sku">'+lamp.sku+"</span>" : '';	
			var name = lamp.name ? '<span class="name">'+lamp.name+"</span>" : '';
			var description = typeof(lamp.description) == 'string' && lamp.description ? nl2br(trim(lamp.description)) : '';
			var specs = typeof(lamp.specs) == 'string' && lamp.specs ? nl2br(trim(lamp.specs)) : '';
			var links = typeof(lamp.links) == 'string' ? lamp.links : '';

			setOptions(lamp);

			var info = sku ? sku+' ' : '';
			info += name ? name : '';
			if (info) {
				info += '<br/>';
			}
			info += description ? '<span class="description">'+description+'</span><br/>' : '';
			info += specs ? '<span class="specs">'+specs+'</span>' : '';
			info += links ? '<br/><br/><span class="links">'+links+'</span>' : '';
			$('p#img_caption').html(info);
		}
		return false;
	});
});