var tiles = new tiles();

function tiles()
{
	this.page_loaded = 'N';
	this.ts_loc = '';
	
	this.showTilePopup = function(accountID, tileID, tile_index, item_index, browser)
	{
		if ($('tilePopupImage_'+tile_index).innerHTML.indexOf('ts_img') < 0)
			tiles.loadTilePopupImage(accountID, accountID, tileID, tile_index);
			
		var browser_index = browser.itemIndex;
		if (browser_index < 0)
			browser_index = browser.itemArray.length + browser_index;
		var distance = 0;
		if (item_index >= (browser_index%browser.itemArray.length))
			distance = item_index - (browser_index%browser.itemArray.length);
		else 
			distance = browser.itemArray.length - ((browser_index%browser.itemArray.length) - item_index);

		if (distance < 2)
			$('tilediv'+tile_index).style.left = '60px';
		else 
			$('tilediv'+tile_index).style.left = '-135px';
		
		$('tile'+tile_index).style.display = 'block';
	}
	
	this.showTilePopupNoStrip = function(accountID, showroomID, tileID, tile_index, left_position)
	{
		if ($('tilePopupImage_'+tile_index).innerHTML.indexOf('ts_img') < 0)
			tiles.loadTilePopupImage(accountID, showroomID, tileID, tile_index);
		$('tilediv'+tileID).style.left = left_position;
		$('tile'+tileID).style.display = 'block';
	}
	
	this.loadTilePopupImage = function(accountID, showroomID, tileID, tile_index)
	{
		if (tiles.page_loaded == 'N')
		{
			tiles.timer = setTimeout("tiles.loadTilePopupImage('"+accountID+"', '"+showroomID+"', '"+tileID+"', '"+tile_index+"')", 500);
			return;
		}
		
		new Ajax.Updater('tilePopupImage_'+tile_index, 
						'/common/libraries/functionality/design_center/tiles/includes/ajax/tile_popup_image.php', 
						{
						parameters:'accountID='+accountID+
									'&showroomID='+showroomID+
									'&portalID='+tiles.portalID+
									'&tileID='+tileID+
									'&ts_loc='+tiles.ts_loc+
									'&ss='+tiles.searchString
						});
	}
	
	this.showTileDetailsPopup = function(tileID, left_position, top_position)
	{
		tiles.hideAllSwatchDetailsPopups();
		$('tilediv'+tileID).style.left = left_position;
		$('tilediv'+tileID).style.top = top_position;
		$('tile'+tileID).style.display = 'block';
		if ($('tilediv'+tileID).innerHTML == '')
		{
			$('tilediv'+tileID).innerHTML = "<table cellpadding='0' cellspacing='0' width='100%' height='100%'><tr><td align='center'><img src='/artwork/indicator.gif' border='0'/></td></tr></table>";
				
			new Ajax.Updater('tilediv'+tileID, 
						'/includes/ajax/tile_details.php',
						{
							parameters:'tileID='+tileID+
										'&hide_collection_select=Y'
						});
		}
	}
	
	this.hideAllTileDetailsPopups = function()
	{
		var i = 0;
		while ($('tilevalue'+i))
		{
			var tileID = $('tilevalue'+i).value;
			$('tile'+tileID).style.display = 'none';
			i++;
		}
	}
	

	this.hideTilePopup = function(tile_index)
	{
		$('tile'+tile_index).style.display = 'none';
	}
	

	// SEARCH //
	
	
	this.tilesSearchKBCatChanged = function()
	{
		if ($('ts_kb_cat').options[$('ts_kb_cat').selectedIndex].value != 'TILE')
		{
			$('ts_kb_cat').selectedIndex = 0;
			alert('Coming soon');
			return;
		}
		tiles.tilesSearchFiltersChanged();
	}
	
	this.tilesSearchFiltersChanged = function(page)
	{
		if (!page)
			page = 1;
		var ts_sc = '';
		if ($('ts_sc'))
		{
			ts_sc = $('ts_sc').options[$('ts_sc').selectedIndex].value;
		}
		else if ($('ts_a_sc'))
		{
			ts_sc = $('ts_a_sc').options[$('ts_a_sc').selectedIndex].value;
		}
		else 
			ts_sc = '';
		var tileSearchForm = Form.serialize('tilesSearchForm');
		window.location = "tiles.php?"+tileSearchForm+'&ts_sc='+ts_sc+'&ts_p='+page;
	}
	
	this.tilesSwatchCountChanged = function(page, selectObj)
	{
		$('ts_sc').selectedIndex = selectObj.selectedIndex;
		$('ts_sc1').selectedIndex = selectObj.selectedIndex;
		tiles.tilesSearchFiltersChanged(page);
	}
	
	
	this.tilesShowroomChanged = function(shSelectObj)
	{
		window.location = shSelectObj.options[shSelectObj.selectedIndex].value;
		shSelectObj.selectedIndex = 0;
	}
	
	this.tilesManufacturerChanged = function(mnSelectObj)
	{
		window.location = mnSelectObj.options[mnSelectObj.selectedIndex].value;
		mnSelectObj.selectedIndex = 0;
	}
	
	this.tilesDesignerChanged = function(designerSelectObj)
	{
		var selectedVal = designerSelectObj.options[designerSelectObj.selectedIndex].value
		if (selectedVal == '' || selectedVal == '-1')
			window.location = "tiles.php?ts_d="+selectedVal;
		else
		{
			designerSelectObj.selectedIndex = 0;
			window.location = selectedVal;
		}
	}
	
	this.tilesCollectionChanged = function(collectionSelectObj)
	{
		window.location = collectionSelectObj.options[collectionSelectObj.selectedIndex].value;
		collectionSelectObj.selectedIndex = 0;
	}
	
	this.searchTilesLocation = function(locSelectObj, level)
	{
		var loc = locSelectObj.options[locSelectObj.selectedIndex].value;
		if (loc == 114)
		{
			window.open('http://nydc.com/tiles.php', '_blank','directories=0,menubar=0,scrollbars,resizable');
			return;
		}
		
		if (loc == 256)
		{
			window.open('http://adac.designcentersearch.com/tiles.php', '_blank','directories=0,menubar=0,scrollbars,resizable');
			locSelectObj.selectedIndex = 0;
			return;
		}
		
		if (loc == 261)
		{
			window.open('http://marketplacedc.designcentersearch.com/tiles.php', '_blank','directories=0,menubar=0,scrollbars,resizable');
			locSelectObj.selectedIndex = 0;
			return;
		}
		
		if (loc == 257)
		{
			window.open('http://michigandesign.designcentersearch.com/tiles.php?ba=1', '_blank','directories=0,menubar=0,scrollbars,resizable');
			locSelectObj.selectedIndex = 0;
			return;
		}
		
		$('ts_loc').value = loc;
		$('ts_loc_level').value = level;
		tiles.tilesSearchFiltersChanged();
	}
	
	this.tilesClearSearch = function()
	{
		$('ts_mn').selectedIndex = 0;
		$('ts_d').selectedIndex = 0;
		$('ts_coll').selectedIndex = 0;
		$('ts_cat').selectedIndex = 0;
		$('ts_s').selectedIndex = 0;
		$('ts_f').selectedIndex = 0;
		$('ts_u').selectedIndex = 0;
		$('ts_c').selectedIndex = 0;
		$('ts_m').selectedIndex = 0;
		$('ts_loc').value = '';
		
		window.location = "tiles.php?"+Form.serialize('tilesSearchForm');
	}
	
	
	this.goToSearchPage = function(page)
	{
		tiles.tilesSearchFiltersChanged(page);
	}
	
	
	//SWATCH DETAILS
	this.showSwatchDetails = function(tileID)
	{
		new Ajax.Updater('tileDetailsContainer', 
						'/includes/ajax/tile_details.php', 
						{
							parameters:'tileID='+tileID
						});
	}
	
	
	//COLLECTION DETAILS
	this.viewAllCollection = function(tileCollectionID)
	{
		window.location = '/collection_details.php?tcID='+tileCollectionID;
	}

	this.collectionDetailsSwatchesCountChanged = function(tileCollectionID)
	{
		if ($('compare_on') && $('compare_on').checked)
		{
			window.location = "/collection_details.php?tcID="+tileCollectionID+'&'+Sortable.serialize("tileCollectionTilesContainer", {name:'tIDs'})+
							'&sc='+$('tc').options[$('tc').selectedIndex].value+
							'&c=Y';
		}
		else
			window.location = "/collection_details.php?tcID="+tileCollectionID+'&tc='+
							$('tc').options[$('tc').selectedIndex].value;
	}
	
	this.showTileDetails = function(tileID, positionLeft, positionTop)
	{
		$('tilediv'+tileID).style.left = positionLeft;
		$('tilediv'+tileID).style.top = positionTop;
		$('tilediv'+tileID).style.width = '950px';
		$('tilediv'+tileID).style.height = '360px';
		
		$('tilediv'+tileID).innerHTML = "<table cellpadding='0' cellspacing='0' width='100%' height='100%'><tr><td><img src='/artwork/indicator.gif' border='0'/></td></tr></table>";
		
		new Ajax.Updater('tilediv'+tileID, 
						'/includes/ajax/tile_details.php',
						{
							parameters:'tileID='+tileID
						});
	}
	
	this.tileCollectionChanged = function()
	{
			window.location = "tile_details.php?tcID="+$('tcID').options[$('tcID').selectedIndex].value+'&ss='+tiles.searchString;
	}
	
	this.tileCollectionChangedViewAll = function()
	{
			window.location = "tile_collection_details.php?tcID="+$('tcID').options[$('tcID').selectedIndex].value+'&ss='+tiles.searchString;
	}
	
	
	this.goToViewCollection = function(tileID)
	{
		window.open('/tile_details.php?tileID='+tileID,'_blank','directories=0,menubar=0,scrollbars,resizable');
	}
	
	this.goToViewAll = function(fabricCollectionID)
	{
		window.open('tile_collection_details.php?tcID='+tileCollectionID,'_blank','directories=0,menubar=0,scrollbars,resizable');	
	}
	
	
	
	this.compareTilesChanged = function(tileCollectionID, c)
	{
		if ($('compare_on').checked == true)
		{
			if (c != 'Y')
			{
				window.location = "tile_collection_details.php?tcID="+tileCollectionID+'&c=Y&tIDs='+$('tIDs').value;
			}
		}
		else
		{
			window.location = "tile_collection_details.php?fcID="+tileCollectionID+'&'+Sortable.serialize("tileCollectionTilesContainer", {name:'tIDs'});;	
		}
		
	}
	
	
	this.changeShopAD = function()
	{
		$('sac').value = $('shop_ad_cat').options[$('shop_ad_cat').selectedIndex].value;
		$('sab').value = $('shop_ad_brand').options[$('shop_ad_brand').selectedIndex].value;
		window.location = '/main/tiles.php?'+$('tilesSearchForm').serialize()+'&'+$('FurnitureShopADAdvertisersForm').serialize()+'#adads';
	}
}


