
// **********************************
// jQuery code for PARTICIPANTS.ASPX
// **********************************

var restaurant_fixed = new Array();
var restaurant_items = new Array();

restaurant_fixed.push( { width:395, height:212, imagesrc:'LOGOruthschris.png', title:'Ruths Chris Steak House', url:'http://www.ruthschris.com' } );
restaurant_items.push( { width:395, height:75, imagesrc:'LOGObandera.png', title:'Bandera', url:'http://www.hillstone.com/#/restaurants/bandera' } );
restaurant_items.push( { width:259, height:75, imagesrc:'LOGObarriocantina.png', title:'Barrio Cantina &amp; Grill', url:'http://www.barriocantina.com' } );
restaurant_items.push( { width:143, height:75, imagesrc:'LOGOcafecarumba.png', title:'Cafe Carumba', url:'http://www.cafecarumba.com' } );
restaurant_items.push( { width:398, height:75, imagesrc:'LOGOchelseaskitchen.png', title:'Chelseas Kitchen', url:'http://www.chelseaskitchenaz.com' } );
restaurant_items.push( { width:113, height:75, imagesrc:'LOGOfranknlupes.png', title:'Frank &amp; Lupe&rsquo;s Old Mexico', url:'http://www.frankandlupes.com' } );
restaurant_items.push( { width:390, height:75, imagesrc:'LOGOhoustons.png', title:'Houstons', url:'http://www.houstons.com' } );
restaurant_items.push( { width:376, height:75, imagesrc:'LOGOilterrazzo.png', title:'Il Terrazzo at the Phoenician', url:'http://www.thephoenician.com/dining/il-terrazzo/il-terrazzo' } );
restaurant_items.push( { width:213, height:75, imagesrc:'LOGOnothingbundtcakes.png', title:'Nothing Bundt Cakes', url:'http://www.nothingbundtcakes.com' } );
restaurant_items.push( { width:114, height:75, imagesrc:'LOGOphoenixcitygrille.png', title:'Phoenix City Grille', url:'http://www.phoenixcitygrille.com' } );
restaurant_items.push( { width:131, height:75, imagesrc:'LOGOrasushi.png', title:'Ra Sushi', url:'http://www.rasushi.com' } );
restaurant_items.push( { width:166, height:75, imagesrc:'LOGOsierrabonita.png', title:'Sierra Bonita Grill', url:'http://www.sierrabonitagrill.com' } );
restaurant_items.push( { width:272, height:75, imagesrc:'LOGOthegrillatfairmontscottsdale.png', title:'The Grill at the Fairmont Scottsdale', url:'http://www.fairmont.com/scottsdale/GuestServices/Restaurants/TheGrill.htm' } );
restaurant_items.push( { width:107, height:75, imagesrc:'LOGOthemeltingpot.png', title:'The Melting Pot', url:'http://www.themeltingpot.com' } );
restaurant_items.push( { width:222, height:75, imagesrc:'LOGOunbacio.png', title:'Un Bacio Ristoranti', url:'http://www.unbacio.com' } );
restaurant_items.push( { width:149, height:75, imagesrc:'LOGOmortons.png', title:'Mortons - The Steakhouse', url:'http://www.mortons.com' } );
restaurant_items.push( { width:130, height:75, imagesrc:'LOGOgelatospot.png', title:'The Gelato Spot', url:'http://www.gelatospot.com' } );
restaurant_items.push( { width:171, height:75, imagesrc:'LOGOxoxobakery.png', title:'XOXO Bake Shop', url:'http://www.gelatospot.com' } );
restaurant_items.push( { width:190, height:75, imagesrc:'LOGOeddievs.png', title:'Eddie V&rsquo;s Edgewater Grille', url:'http://www.eddiev.com' } );
restaurant_items.push( { width:190, height:75, imagesrc:'LOGOfloschinese.png', title:'Flo&rsquo;s Chinese', url:'http://www.flosrestaurants.com' } );
restaurant_items.push( { width:97, height:75, imagesrc:'LOGOthemission.png', title:'The Mission', url:'http://www.themissionaz.com' } );
restaurant_items.push( { width:150, height:75, imagesrc:'LOGOmariamaria.png', title:'Maria Maria Cantina', url:'http://www.mariamariarestaurants.com' } );

var beverage_fixed = new Array();
var beverage_items = new Array();

beverage_fixed.push( { width:200, height:255, imagesrc:'LOGOdeschutesbrewery.png', title:'Deschutes Brewery', url:'http://www.deschutesbrewery.com' } );
beverage_items.push( { width:220, height:90, imagesrc:'LOGOdeschutesbeer.png', title:'Deschutes Beers (Black Butte Porter, Inversion IPA, Mirror Pond)', url:'http://www.deschutesbrewery.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOboulderbeer.png', title:'Boulder Beer', url:'http://www.boulderbeer.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOdogfishhead.png', title:'Dogfish Head Craft Brewed Ales', url:'http://www.dogfish.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOfullsail.png', title:'Full Sail Brewing Company', url:'http://www.fullsailbrewing.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOoldworldbrewery.png', title:'Old World Brewery, Phoenix', url:'http://www.oldworldbrewery.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOuintabrewingcompany.png', title:'Uinta Brewing Company', url:'http://www.uintabrewing.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOsweetcarolina.png', title:'Sweet Carolina Sweet Tea Vodka', url:'http://www.whiterockdistilleries.com' } );
beverage_items.push( { width:220, height:80, imagesrc:'LOGOpinnaclevodka.png', title:'Pinnacle Vodka', url:'http://www.pinnaclevodka.com' } );
beverage_items.push( { width:220, height:60, imagesrc:'LOGOhangar1vodka.png', title:'Hangar 1 Vodka', url:'http://www.hangarone.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOfortycreek.png', title:'Forty Creek Whisky', url:'http://www.fortycreekwhisky.com' } );
beverage_items.push( { width:220, height:61, imagesrc:'LOGObuffalotrace.png', title:'Buffalo Trace Kentucky Bourbon Whisky', url:'http://www.buffalotrace.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOfamousgrouse.png', title:'The Famous Grouse Scotch Whisky', url:'http://www.thefamousgrouse.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOpenfolds.png', title:'Penfolds Wines', url:'http://www.penfolds.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOlunetta.png', title:'Lunetta Prosecco Wine', url:'http://www.lunettaprosecco.com' } );
beverage_items.push( { width:220, height:90, imagesrc:'LOGOfranciscoppola.png', title:'Francis Coppola Diamond Collection', url:'http://www.franciscoppolawinery.com' } );
beverage_items.push( { width:200, height:60, imagesrc:'LOGOtrincheroestates.png', title:'Trinchero Estates', url:'http://www.tfewines.com' } );
beverage_items.push( { width:100, height:87, imagesrc:'LOGOeaglerare.png', title:'Eagle Rare', url:'http://www.buffalotrace.com' } );
beverage_items.push( { width:200, height:76, imagesrc:'LOGOcavitcollection.png', title:'Cavit Collection', url:'http://www.cavitcollection.com' } );
beverage_items.push( { width:100, height:90, imagesrc:'LOGOjackrums.png', title:'Jack Flavored Rums', url:'http://www.jackrums.com' } );
beverage_items.push( { width:100, height:97, imagesrc:'LOGOpinkyellowglen.png', title:'Pink by Yellow Glen', url:'http://www.yellowglenusa.com' } );
beverage_items.push( { width:100, height:77, imagesrc:'LOGOtequilacruz.png', title:'Tequila Cruz', url:'http://www.cruztequila.com' } );
beverage_items.push( { width:200, height:49, imagesrc:'LOGOresqwater.png', title:'Hangover ResQwater', url:'http://www.resqwater.com' } );
beverage_items.push( { width:200, height:32, imagesrc:'LOGOrockstar.png', title:'Rockstar Energy Drink', url:'http://www.rockstar69.com' } );

function preload(items)
{
	for (var i = 0; i < items.length; i++)
	{
		var myImageSrc = "images/" + items[i].imagesrc;
		var myImage = $("<img />").attr("src", myImageSrc);
	}
}

function getItemHTML(item, i, type)
{
	return '<div id="' + type + '_' + i + '" class="' + type + '"><a href="' + item.url + '" title="' + item.title + '" target="' + type + 's" class="image"><img src="images/' + item.imagesrc + '" alt="' + item.title + '" /></a></div>';
}

$(document).ready(function() {
	
	var debugString = "";

	// Shuffle the restaurant images
	restaurant_items = $.shuffle(restaurant_items);

	var restaurants_adjusted = new Array();
	var maxWidth = $("#restaurants").innerWidth();
	var adjustedIndex = 0;
	var currentWidth = 0;
	var currentHeight = 0;
	var imageWidth = 0;
	var imageHeight = 0;
	var imageHorzMargin = 25;
	var imageVertMargin = 35;

	//debugString = '<br>MaxWidth:' + maxWidth;

	while (restaurant_items.length > 0)
	{
		imageHeight = 0;

		//debugString = debugString + '<br><hr>';

		for (var i = 0; i < restaurant_items.length; i++)
		{
			//debugString = debugString + '<br>[INFO1] ' + restaurant_items.length + ' i:' + i + ' n:"' + restaurant_items[i].title + '" cw:' + currentWidth + ' iw:' + restaurant_items[i].width + ' ih:' + restaurant_items[i].height;

			if ((restaurant_items[i].width + imageHorzMargin + currentWidth) <= maxWidth)
			{
				restaurants_adjusted[adjustedIndex] = restaurant_items[i];
				imageWidth = restaurant_items[i].width;
				if (restaurant_items[i].height > imageHeight)
					imageHeight = restaurant_items[i].height;
				currentWidth = currentWidth + imageHorzMargin + imageWidth;

				//debugString = debugString + '<br><b>[IMG1] n:"' + restaurant_items[i].title + '" cw:' + currentWidth + ' ai:' + (adjustedIndex + 1) + '</b>';
				restaurant_items.splice(i, 1);
				adjustedIndex = adjustedIndex + 1;
			}
		}

		//debugString = debugString + '<br>';

		for (var i = restaurant_items.length - 1; i >= 0; i--)
		{
			//debugString = debugString + '<br>[INFO2] ' + restaurant_items.length + ' i:' + i + ' n:' + restaurant_items[i].title + ' cw:' + currentWidth + ' iw:' + restaurant_items[i].width + ' ih:' + restaurant_items[i].height;

			if ((restaurant_items[i].width + imageHorzMargin + currentWidth) <= maxWidth)
			{
				restaurants_adjusted[adjustedIndex] = restaurant_items[i];
				imageWidth = restaurant_items[i].width;
				if (restaurant_items[i].height > imageHeight)
					imageHeight = restaurant_items[i].height;
				currentWidth = currentWidth + imageHorzMargin + imageWidth;

				//debugString = debugString + '<br><b>[IMG2] n:"' + restaurant_items[i].title + '" cw:' + currentWidth + ' ai:' + (adjustedIndex + 1) + '</b>';
				restaurant_items.splice(i, 1);
				adjustedIndex = adjustedIndex + 1;
			}
		}

		currentWidth = 0;
		currentHeight = currentHeight + imageVertMargin + imageHeight;
	}

	// Add the fixed items to the top of the array
	for (var i = 0; i < restaurant_fixed.length; i++)
	{
		restaurants_adjusted.splice(i, 0, restaurant_fixed[i]);
		currentHeight = currentHeight + imageVertMargin + restaurant_fixed[i].height;
	}

	// Set the height of the restaurant participants DIV
	$("#restaurants").height(currentHeight);

	// Preload the restaurant/beverage images
	preload(restaurants_adjusted);



	// Shuffle the beverage images
	beverage_items = $.shuffle(beverage_items);

	var beverages_adjusted = new Array();
	maxWidth = $("#beverages").innerWidth();
	adjustedIndex = 0;
	currentWidth = 0;
	currentHeight = 0;
	imageWidth = 0;
	imageHeight = 0;
	imageHorzMargin = 20;
	imageVertMargin = 25;

	//debugString = '<br>MaxWidth:' + maxWidth;

	while (beverage_items.length > 0)
	{
		imageHeight = 0;

		//debugString = debugString + '<br><hr>';

		for (var i = 0; i < beverage_items.length; i++)
		{
			//debugString = debugString + '<br>[INFO1] ' + beverage_items.length + ' i:' + i + ' n:"' + beverage_items[i].title + '" cw:' + currentWidth + ' iw:' + beverage_items[i].width + ' ih:' + beverage_items[i].height;

			if ((beverage_items[i].width + imageHorzMargin + currentWidth) <= maxWidth)
			{
				beverages_adjusted[adjustedIndex] = beverage_items[i];
				imageWidth = beverage_items[i].width;
				if (beverage_items[i].height > imageHeight)
					imageHeight = beverage_items[i].height;
				currentWidth = currentWidth + imageHorzMargin + imageWidth;

				//debugString = debugString + '<br><b>[IMG1] n:"' + beverage_items[i].title + '" cw:' + currentWidth + ' ai:' + (adjustedIndex + 1) + '</b>';
				beverage_items.splice(i, 1);
				adjustedIndex = adjustedIndex + 1;
			}
		}

		//debugString = debugString + '<br>';

		for (var i = beverage_items.length - 1; i >= 0; i--)
		{
			//debugString = debugString + '<br>[INFO2] ' + beverage_items.length + ' i:' + i + ' n:' + beverage_items[i].title + ' cw:' + currentWidth + ' iw:' + beverage_items[i].width + ' ih:' + beverage_items[i].height;

			if ((beverage_items[i].width + imageHorzMargin + currentWidth) <= maxWidth)
			{
				beverages_adjusted[adjustedIndex] = beverage_items[i];
				imageWidth = beverage_items[i].width;
				if (beverage_items[i].height > imageHeight)
					imageHeight = beverage_items[i].height;
				currentWidth = currentWidth + imageHorzMargin + imageWidth;

				//debugString = debugString + '<br><b>[IMG2] n:"' + beverage_items[i].title + '" cw:' + currentWidth + ' ai:' + (adjustedIndex + 1) + '</b>';
				beverage_items.splice(i, 1);
				adjustedIndex = adjustedIndex + 1;
			}
		}

		currentWidth = 0;
		currentHeight = currentHeight + imageVertMargin + imageHeight;
	}

	// Add the fixed items to the top of the array
	for (var i = 0; i < beverage_fixed.length; i++)
	{
		beverages_adjusted.splice(i, 0, beverage_fixed[i]);
		currentHeight = currentHeight + imageVertMargin + beverage_fixed[i].height;
	}

	// Set the height of the beverage participants DIV
	$("#beverages").height(currentHeight);

	preload(beverages_adjusted);

	// Add the restaurant/beverage images to the page
	for (var i = 0; i < restaurants_adjusted.length; i++)
	{
		var item = getItemHTML(restaurants_adjusted[i], i+1, 'restaurant');
		$("#restaurants").append(item);
	}
	for (var i = 0; i < beverages_adjusted.length; i++)
	{
		var item = getItemHTML(beverages_adjusted[i], i+1, 'beverage');
		$("#beverages").append(item);
	}

	//$("#debug").append(debugString);

});

