// JavaScript Document
function getWines(specials) {
	if (specials.length == 0) {
		return;
	}
	var url = $(specials[0]).find('.wine-tag a').attr('href');
	if (typeof window.wines == 'undefined') window.wines = {};
	if (typeof window.wines[url] != 'object') {
		$.get(url, function (data) {
			data = data.replace(/[\s\S]*<div id="json">(.*?)<\/div>[\s\S]*/igm, "$1");
			if (data.length != 0) {
				data = data.replace(/"/g,'\\"').replace(/\{'/g,'{"').replace(/'\}/g,'"}').replace(/'([:,])'/g,'"$1"').replace("\n","").replace(/<br ?\/?>/g, " ");
				try {
					wine = $.parseJSON(data);
				} catch (ex) {
					wine = {'tag_name': 'Wine not found','tag_region': '','tag_small description': '','tag_image': '','tag_product code': '','tag_price': '','tag_rrp': '','tag_save': '','tag_was': ''};
				}
				
				window.wines[url] = wine;
				
				var imgPlaceholder = $(specials[0]).find("img.placeholder")[0];
				var html = specials[0].innerHTML;
				
				for(var tag in wine) {
					var rg = new RegExp('\\['+tag+'\\]', "g");
					
					if (tag == 'tag_image' && imgPlaceholder) {
						wine[tag] = wine[tag].replace(/(.*)<img.*src=\"([^\"]+?)\"[^\/]*\/>(.*)/, '$1<img src="/Utilities/ShowThumbnail.aspx?USM=1'+"&amp;W="+imgPlaceholder.clientWidth+"&amp;H="+imgPlaceholder.clientHeight+'&amp;R=1&amp;Img=$2" />$3');
					}
					
					html = html.replace(rg, wine[tag]);
				}
				specials[0].innerHTML = html;
				specials[0].style.visibility = "visible";
			}
			getWines(specials.not(specials[0]));
		},'html');
	} else {
		var html = specials[0].innerHTML;
		var wine = window.wines[url];
		for(var tag in wine) {
			var rg = new RegExp('\\['+tag+'\\]', "g");
			html = html.replace(rg, wine[tag]);
		}
		specials[0].innerHTML = html;
		specials[0].style.visibility = "visible";
		getWines(specials.not(specials[0]));
	}
}

