/* 
Promotions AJAX system
*/

// Function gatekeeps the search - will only perform search if:-
// 1. Search string is >=2 or < 1
// 2. if this is true then as long as the timer reaches 750 ms it will then run the search.  timer is reset at each call.

t = 0;
intime = false;
// set to predefined gibberish to make sure nobody actually types this into the search
currentsearchstring = "liahjefl8hnaifhknzckjbzekugfbesz"; 

function minchars(searchstring)
{	
	if (currentsearchstring == "liahjefl8hnaifhknzckjbzekugfbesz")
		currentsearchstring = searchstring;
	
	// Check to see if this is triggered by a change in content or just a defocus (fix for alt-tab triggering an onkeyup event in the browser)
	if (searchstring != currentsearchstring)
	{
		currentsearchstring = searchstring;
		if (searchstring.length >= 2 || searchstring.length < 1)
		{
			//s = searchstring;
			if (!intime) 
				clearTimeout(t);
			$('promoscroller').update("<br /><br /><p style='text-align:center;'><img src='images/ajax-loader.gif' alt='Searching'/><br />Searching</p>");
			t = setTimeout("searchByArtist('" + searchstring + "')",750);
		}
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}


// Main search by artist function.
function searchByArtist(searchstring,startindex)
{	
	var theJson = "";
   	var colMap = new Object();
	var elementArray = [];

  	new Ajax.Request("components/promotionSearch.cfc?method=searchByArtist&artist=" + searchstring + "&startindex=1", 
	{
  		method:'get',
  		onSuccess: function(transport)
		{
     		var theJson = transport.responseText.evalJSON();
			
			// No data returned?  No artists start with searchstring... display a message telling the user that
			if (!theJson.DATA.length) 
			{
				$('promoscroller').update("<p style=\'text-align:center;\'>Sorry, there are no artists in the system matching the search &quot;" + searchstring + "&quot;.<br />  Please try again.</p>");
			}
			else
			{
				$('promoscroller').update("<p style='text-align:center;'><img src='images/ajax-loader.gif' alt='Searching'/><br />Searching</p>");
			
				// get the columns as a map object 
				for(var i = 0; i < theJson.COLUMNS.length; i++) 
				{
	      			colMap[theJson.COLUMNS[i]] = i;      
	   			}

	   			var rowindex = 0;

				// clear the promo area
				$('promoscroller').update();

	   			// loop over returned data 
	   			for(var i = 0; i < theJson.DATA.length; i++) 
				{
					rowindex ++;

					// build the new elements ready to be filled with data - doing it this way as it's just faster 
					// than building it as a DOM object
	      			var pdiv = new Element('div', { 'class': 'promoelement', 'style' : 'display:block;', 'id' : 'promo' + i});
					var htmlString = "<a href=\"http://www.pushentertainment.com/" + theJson.DATA[i][colMap["PROMOTIONID"]] + "\" target=\"_blank\" >"
					+ "<img src=\"\../images/repository/" + theJson.DATA[i][colMap["PROMOTIONID"]] + "/" + theJson.DATA[i][colMap["PROMOTIONID"]] + "_info.jpg\"  class=\"promoimage\" onerror=\"this.src='images/noalbum.gif';\" /></a>"
					+ "<p class=\"promoartist\">" + theJson.DATA[i][colMap["ARTIST"]] + "&nbsp;</p>" 
					+ "<p class=\"promotitle\">" + String(theJson.DATA[i][colMap["TITLE"]]).substring(0,24) + "&nbsp;</p>" 
					+ "<p class=\"promotext\">" + theJson.DATA[i][colMap["SHORTDESCRIPTION"]] + "<br />"
					+ theJson.DATA[i][colMap['FORMATTEDSTARTDATE']] + "<br /><img src=\"images/red.gif\" class=\"redbar\" height=\"1\" width=\"147\"></p>";
					
					//if (rowindex+1 > theJson.DATA.length)
						//reached the end of data so give a More link option
						//htmlString = htmlString + "<br><div style=\"float:right;\"><a href=\"http://www.pushentertainment.com\">More</a></div>";

					// Build the element
					pdiv.update(htmlString);
	
					// Insert the promoelement into the promoscroller area
					Element.insert($('promoscroller'), { 'bottom': pdiv });
	
					// the break element to shuffle a row of 6 down 1
					var pbr = "<br style=\"clear:both;\">";

					// Shuffle down a row when 6 have been output
					if (rowindex == 6)
					{
						Element.insert($('promoscroller'), { 'bottom': pbr });
						rowindex = 0;
					}
				}		
			}
   		}
	});
}