function getItemsJQUERY(e)
{
	$("#divContent").html("<table cellspacing='0' cellpadding='0' border='0' style='width: 100%; height: 200px;'><tr><td align='right' style='width: 200px;'><img src='http://www.cmctraining.org/images/loading.gif'/></td><td align='left' valign='middle'>Laddar valda produkter ...</td></tr></table>");
	//$("#divContent").html("<table cellspacing='0' cellpadding='0' border='0' style='width: 100%; height: 200px;'><tr><td align='right' style='width: 200px;'></td><td align='left' valign='middle'>Laddar valda produkter ...</td></tr></table>");
	
	try
	{
		
		//if(e != '') e.preventDefault();
		
		// grab form data
		var strSearch = document.forms['frmJQUERYSearch'].s.value;
		strSearch = strSearch == 'Fritext sök' ? '' : strSearch;
		var strPFrom = document.forms['frmJQUERYSearch'].frmpfrom.value;//$("#pfrom").val();
		var strPTo = document.forms['frmJQUERYSearch'].frmpto.value;//$("#pto").val();
		var has_searched = document.forms['frmJQUERYSearch'].has_searched.value;
		var strName = document.forms['frmJQUERYSearch'].frmProdName.value;
		var strProdCountry = document.forms['frmJQUERYSearch'].frmProdCountry.value;
		var strDistrict = "";//document.forms['frmJQUERYSearch'].district.value;
		var strArtnr = "";//document.forms['frmJQUERYSearch'].artnr.value;
		var strImporter = document.forms['frmJQUERYSearch'].importer.value;
		var strGrape = document.forms['frmJQUERYSearch'].grape.value;
		var strVintage = document.forms['frmJQUERYSearch'].vintage.value;
		var strFairtrade = document.forms['frmJQUERYSearch'].frmFAIRTRADE.checked ? document.forms['frmJQUERYSearch'].frmFAIRTRADE.value : '';
		var strKRAV = document.forms['frmJQUERYSearch'].frmKRAV.checked ? document.forms['frmJQUERYSearch'].frmKRAV.value : '';
		var strSortby = document.forms['frmJQUERYSearch'].sortby.value;
		var strOrderby = document.forms['frmJQUERYSearch'].orderby.value;
		
		// grab stored search if no active search (default load)
		if(has_searched == 'false')
		{
			var stored = readCookie("jquery-search");
			
			if(strSearch == '' && stored != null && stored != '') 
			{
				strSearch = stored;
				
				// put it into search field
				document.forms['frmJQUERYSearch'].s.value = strSearch;
			}
		}
		
		// store this search
		//if(strSearch != 'Fritext sök') createCookie("jquery-search",strSearch,1);
		
		// check whether to make a PHP call (slow, first call) or to grab from JS (fast, subsequent loads)
		if(infoContainer.length == 0)
		{
			// send request
			
				// build GET querystring
				var tmpGet = "?data="+strSearch+"&";
				tmpGet 	  += "frmpfrom="+strPFrom+"&";
				tmpGet 	  += "frmpto="+strPTo+"&";
				tmpGet 	  += "frmName="+strName+"&";
				tmpGet 	  += "frmProdCountry="+strProdCountry+"&";
				tmpGet 	  += "district="+strDistrict+"&";
				tmpGet 	  += "artnr="+strArtnr+"&";
				tmpGet 	  += "importer="+strImporter+"&";
				tmpGet 	  += "grape="+strGrape+"&";
				tmpGet 	  += "vintage="+strVintage+"&";
				tmpGet 	  += "frmFAIRTRADE="+strFairtrade+"&";
				tmpGet 	  += "KRAV="+strKRAV+"&";
				tmpGet 	  += "sortby="+strSortby+"&";
				tmpGet 	  += "orderby="+strOrderby;
			
				// THIS PART IS LOADED IN tpl/guide.php
				
				// fake jquery, load in iframe instead
				//document.getElementById('iframejQueryFake').src = 'http://www.vinguiden.com/vinbutiken/jquery-get_items_fake.php'+tmpGet;
				
			/*$.post("http://www.vinguiden.com/vinbutiken/jquery-get_items.php", {data: strSearch, frmpfrom: strPFrom, frmpto: strPTo, frmName: strName, frmProdCountry: strProdCountry, district: strDistrict, artnr: strArtnr, importer: strImporter, grape: strGrape, vintage: strVintage, frmFAIRTRADE: strFairtrade, KRAV: strKRAV, sortby: strSortby, orderby: strOrderby}, function(xml) 
			{
				$("#divContent").animate({ height: 'hide', opacity: 'hide' }, 'fast');
				
				// format and output result
				$("#divContent").html(
					"" + $("item", xml).text()
				);
				
				$("#divContentShelf").html(
					"" + $("item_shelf", xml).text()
				);
				
				$("#divContent").show("slow");
			});*/
		}
		else
		{
			//$("#divContent").html("<table cellspacing='0' cellpadding='0' border='0' style='width: 100%; height: 200px;'><tr><td align='right' style='width: 200px;'><img src='http://www.cmctraining.org/images/loading.gif'/></td><td align='left' valign='middle'>Laddar valda produkter ...</td></tr></table>");
			//$("#divContent").hide("slow");
			document.getElementById('divContent').style.display = 'none';
			
			// JS call
			var distance = "";
			var distance = "";
			var word = "";
			var word2 = "";
			var words = new Array();
			var words2 = new Array();
			var myList = new Array(5000);
			var myList2 = new Array(5000);
			var myList3 = new Array(5000);
			var closest = '';
			var closest2 = '';
			
			var loop = 0;
			
			// shelf space
			try
			{
				var myCon2 = '';
				for(key in infoContainerShelf)
				{
						myVal = infoContainerShelf[key]['wpId'];
						
					     // print item
						myCon2 += buildItem(myVal, 'big');
							
				}
				
				$("#divContentShelf").show("slow");
				
				$("#divContentShelf").html(
						myCon2
				);
				
			}
			catch(e)
			{
				alert(e);
			}
			
			// no shortest distance found, yet
			var shortest = -1;
			var shortest2 = -1;
			
			var tmp_index = 0;
			
			// iterate all items in infoContainer
			for(key in infoContainer)
			{
				// grab word
				word = infoContainer[key]['wpName'];
				
				// calculate distance between input word, and current word
				distance = levenshtein(strSearch.toUpperCase(), word.toUpperCase());
				
				// get the distance for this item
				
					// find closest word within this item
						// break out words
						words2 = word.split(' ');
					
						// reset some vars
						distance2 = "";
						word2 = "";
						closest2 = '';
						shortest2 = -1;
						
						// iterate them to find the closest of them and use it as this distance
						for(key2 in words2)
						{
							// grab this word
							word2 = words2[key2];
							
							// calculate distance between input word, and current word
							distance2 = levenshtein(strSearch.toUpperCase(), word2.toUpperCase());
							
							// check for an exact match
							if(distance2 == 0)
							{
								// closest word is this one (exact match)
								closest2 = word2;
								shortest2 = distance2;
								
								// break out of the loop; we've found an exact match
								break;
							}
							
							// if this distance is less than the next found shortest
							// distance, OR if a next shortest word has not yet been found
							if (distance2 <= shortest2 || shortest2 < 0)
							{
								// set the closest match, and shortest distance
								closest2  = word2;
								shortest2 = distance2;
							}
						}
						
					// use closes values found within words to use for this entire item
					if(closest2 != '' && shortest2 != -1)
					{
						distance = shortest2;
						word = word;				// keep origanal word for this item, and not just the extracted word from this item that matched
					}
					
						
					// global (use closest word within this item to compare with overall)
						// check for an exact match
						if(distance == 0)
						{
							// closest word is this one (exact match)
							closest = word;
							shortest = distance;
							
							// break out of the loop; we've found an exact match
							break;
						}
						
						// if this distance is less than the next found shortest
						// distance, OR if a next shortest word has not yet been found
						if (distance <= shortest || shortest < 0)
						{
							// set the closest match, and shortest distance
							closest  = word;
							shortest = distance;
						}
						
						
				// store a list of words along with their distance to enable sorting later on
				//myList[distance+"-"+key] = word + " ["+distance+"]";
				//myList[(distance+1)+"-"+Math.random()] = key;
				
				// FILTER OUT UNRELEVANT ITEMS HERE
					
					// product name
					if(strName == '' || strName == word)
					{
						// COUNTRY
						cntr = infoContainer[key]['wpCountry'];
						cntr = ","+cntr+",";
						if(strProdCountry == '' || strProdCountry.indexOf(cntr) > -1)
						{
							// ARTICLE NR
							if(strArtnr == '' || strArtnr == infoContainer[key]['wpProductId'])
							{
								// IMPORTER
								if(strImporter == '' || strImporter == infoContainer[key]['wpImporter'])
								{
									// GRAPE
									grape = infoContainer[key]['wpGrape'];
									grape = grape.toUpperCase();
									if(strGrape == '' || grape.indexOf(strGrape.toUpperCase()) > -1)
									{
										// VINTAGE
										if(strVintage == '' || strVintage == infoContainer[key]['wpVintage'])
										{
											// FAIRTRADE
											if(strFairtrade == '' || (strFairtrade == '1' && infoContainer[key]['wpFAIRTRADE'] != ''))
											{
												// KRAV
												if(strKRAV == '' || (strKRAV == '1' && infoContainer[key]['wpKRAV'] != ''))
												{
													// PRICE
													if(parseInt(infoContainer[key]['wpPrice']) >= parseInt(strPFrom) && parseInt(infoContainer[key]['wpPrice']) <= parseInt(strPTo))
													{
														// DETERMINE WHAT TO SORT BY
														if(strSortby == 'votes') sortby = infoContainer[key]['votes'];
														else if(strSortby == 'price') sortby = infoContainer[key]['wpPrice'].replace(' kr', '');
														else if(strSortby == 'name') sortby = infoContainer[key]['wpName'];
														else if(strSortby == 'product_id') sortby = infoContainer[key]['wpProductId'];
														else if(strSortby == 'importer') sortby = infoContainer[key]['wpImporter'];
														else if(strSortby == 'whinetype') sortby = infoContainer[key]['wpProductType'];
														else if(strSortby == 'country') sortby = infoContainer[key]['wpCountry'];
														else if(strSortby == 'district') sortby = infoContainer[key]['wpDistrict'];
														else if(strSortby == 'vintage') sortby = infoContainer[key]['wpVintage'];
														else sortby = distance;
														myList[sortby+Math.random()] = key;
													}
												}
											}
										}
									}
								}
							}
						}
					}
			}
			
			
			
			//alert(strSearch + " [CLOSEST: "+closest+" SHORTEST: "+shortest+"]");
			
			// sort by key
			var array = myList;
			var keys = new Array();
			var numItems = 0;
			for(k in array)
			{
			     keys.push(k);
			     if(myList[k] != '') numItems++;
			}
			
			// check whether to sort a stirng or integer (javascript distinguishes between theese two)
			if(strSortby == 'name' || strSortby == 'importer' || strSortby == 'whinetype' || strSortby == 'country' || strSortby == 'district')
			{
				// string
				keys.sort();
			}
			else
			{
				// integer
				keys.sort(function(a,b){return a - b});
			}
			
			// ORDER BY
			if(strOrderby == 'DESC') keys.reverse();
			
			// determine whether to show thumbnails or biginfo
			var biginfo = numItems < 50 ? true : false;
			
			var myKey = "";		// distance
			var myVal = "";		// wpId
			var myCon = "";		// run time data container
			for (var i = 0; i < keys.length; i++)
			{
				myKey = keys[i];
				myVal = array[keys[i]];
				
			     // print item
				myCon += biginfo == true ? buildItem(myVal, 'big') : buildItem(myVal, 'small');
					
			}
			
			$("#divContent").show("slow");
			
			$("#divContent").html(
				myCon
			);
			
			

		}
	}
	catch(e)
	{
		//alert(e);
	}
}

function sortfunction(a, b){
return (a - b) //causes an array to be sorted numerically and ascending
}

// function to build item (thumbnail || thumbnail + info)
function buildItem(wpid, strSize)
{
	// declare return var
	var strReturn = '';
	
	// calculate padding (to make item be vertical-align: bottom)
	var padd_top = infoContainer[wpid]['maxSmallH'] < 100 ? (100-infoContainer[wpid]['maxSmallH']) : '';

	// build it
	if(strSize == 'small')
	{
		strReturn += "<div onclick='loadInfo(\""+wpid+"\");' title='"+infoContainer[wpid]['wpName'].replace('"', '\"')+"' style='padding: 0 5px 5px 0; border: 0px solid red; float: left;'>";
		strReturn += "	<div style='width: "+infoContainer[wpid]['maxSmallW']+"px; height: 100px; overflow: hidden;'>";
		strReturn += "     <div style='padding: "+(padd_top != '' ? padd_top : '0')+"px 0 0 0;'>";
		strReturn += '		<img src="'+infoContainer[wpid]['wppName4']+'" style="cursor: pointer; width: '+infoContainer[wpid]['maxSmallW']+'px; height: '+infoContainer[wpid]['maxSmallH']+'px;">';
		strReturn += '	   </div>';
		strReturn += '	</div>';
		strReturn += '</div>';
	}
	else
	{
		strReturn += "<div onclick='loadInfo("+wpid+");' title='"+infoContainer[wpid]['wpName'].replace('"', '\"')+"' style='padding: 0 5px 5px 0; border: 0px solid red; float: left;'>";
		strReturn += "	<div style='width: 250px; overflow: hidden;'>";
		strReturn += '		<table cellspacing="0" cellpadding="0" border="0" width="250" height="250">';
		strReturn += '			<tr>';
		strReturn += '				<td valign="top">';
		strReturn += '					<div style="width: 80px; overflow: hidden;">';
		strReturn += '						<img src="'+infoContainer[wpid]['wppName']+'" style="cursor: pointer; width: '+infoContainer[wpid]['maxBigW']+'px; height: '+infoContainer[wpid]['maxBigH']+'px;">';
		strReturn += '					</div>';
		strReturn += '				</td>';
		strReturn += '				<td valign="top">';
		strReturn += '					<table cellspacing="0" cellpadding="0" border="0" width="200">';
		strReturn += '						<tr>';
		strReturn += '							<td style="width: 170px;">';
		strReturn += '								<span style="font-family: georgia; font-size: 12px;">';
		strReturn += '									'+infoContainer[wpid]['wpName'];
		strReturn += '								</span>';
		strReturn += '								'+ (infoContainer[wpid]['wpFAIRTRADE'] == '1' ? '<div style="padding: 0 0 5px 0;"><img src="http://www.vinguiden.com/vinbutiken/inc/gfx/flags/fairtrade.jpg" style="width: 27px; height: 18px;" alt="Rättvisemärkt"></div>' : '');
		strReturn += '								'+ (infoContainer[wpid]['KRAV'] == '1' ? '<div style="padding: 0 0 5px 0;"><img src="http://www.vinguiden.com/vinbutiken/inc/gfx/flags/krav.jpg" style="width: 27px; height: 18px;" alt="KRAV"></div>' : '');
		strReturn += '								<table cellspacing="0" cellpadding="0" border="0">';
		strReturn += '									<tr>';
		strReturn += '										<td colspan="2" style="font-size: 11px; font-family: arial;">';
		strReturn += '											<b>Art. nr</b>: '+infoContainer[wpid]['wpProductId']+'<br/>';
		strReturn += '											<b>Pris</b>: '+infoContainer[wpid]['wpPrice']+'<br/>';
		strReturn += '											<b>Lagersaldo</b>: '+(infoContainer[wpid]['wpInStock'] == -1 ? 'Finns i lager' : infoContainer[wpid]['wpInStock'])+'<br/>';
		strReturn += '											'+(infoContainer[wpid]['wpProducer'] == 'NULL' ? '' : '<b>Producent</b>: '+infoContainer[wpid]['wpProducer'])+'<br/>';
		strReturn += '											<b>Land</b>: '+infoContainer[wpid]['wpCountry']+'<br/>';
		strReturn += '											<b>Distrikt</b>: '+infoContainer[wpid]['wpDistrict']+'<br/>';
		strReturn += '											<b>Import&ouml;r</b>: '+infoContainer[wpid]['wpImporter']+'<br/>';
		strReturn += '											<b>Druva</b>: '+infoContainer[wpid]['wpGrape']+'<br/>';
		strReturn += '											<b>Årgång</b>: '+infoContainer[wpid]['wpVintage']+'<br/>';
		strReturn += '											<!--<b>Antal sålda</b>: '+infoContainer[wpid]['wpQuantity']+'<br/>-->';
		strReturn += '										</td>';
		strReturn += '									</tr>';
		strReturn += '									<tr>';
		strReturn += '										<td colspan="2">&nbsp;</td>';
		strReturn += '									</tr>';
		strReturn += '								</table>';
		strReturn += '								<br/>';
		strReturn += '								<br/>';
		strReturn += '							</td>';
		strReturn += '						</tr>';
		strReturn += '					</table>';
		strReturn += '				</td>';
		strReturn += '			</tr>';
		strReturn += '		</table>';
		strReturn += '	</div>';
		strReturn += '</div>';
	}
	// return it
	return strReturn;
}
		
		
$(document).ready(function()
{
	try
	{
		function reloadItems(e)
		{
			//$("#divContent").hide("slow");
			
			getItemsJQUERY(e);
		}
		
		$("#frmSubmit").click(function(e)
		{
			document.forms['frmJQUERYSearch'].has_searched.value = 'true';
			reloadItems(e);
		});
	
		reloadItems('');
		
	}
	catch(e)
	{
		//alert(e);
	}
 });
