var fabrics = new fabrics();

function fabrics()
{
	this.page_loaded = 'N';
	
	this.showSwatchPopup = function(accountID, fabricSwatchID, swatch_index, item_index, browser)
	{
		if ($('textilePopupImage_'+fabricSwatchID).innerHTML.indexOf('fs_img') < 0)
			fabrics.loadSwatchPopupImage(accountID, fabricSwatchID);
			
		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)
			$('swatchdiv'+swatch_index).style.left = '60px';
		else 
			$('swatchdiv'+swatch_index).style.left = '-155px';
		
		$('swatch'+swatch_index).style.display = 'block';
	}
	
	this.showSwatchPopupNoStrip = function(accountID, fabricSwatchID, left_position)
	{
		if ($('textilePopupImage_'+fabricSwatchID).innerHTML.indexOf('fs_img') < 0)
			fabrics.loadSwatchPopupImage(accountID, fabricSwatchID);
		$('swatchdiv'+fabricSwatchID).style.left = left_position;
		$('swatch'+fabricSwatchID).style.display = 'block';
	}
	
	this.loadSwatchPopupImage = function(accountID, fabricSwatchID)
	{
		if (fabrics.page_loaded == 'N')
		{
			fabrics.timer = setTimeout("fabrics.loadSwatchPopupImage('"+accountID+"', '"+fabricSwatchID+"')", 500);
			return;
		}
		
		window.clearTimeout(fabrics.timer);
		
		new Ajax.Updater('textilePopupImage_'+fabricSwatchID, 
						'/account/includes/ajax/fabric_swatch_popup_image.php', 
						{
						parameters:'accountID='+accountID+
									'&fabricSwatchID='+fabricSwatchID+
									'&fs_loc='+fabrics.fs_loc+
									'&ss='+fabrics.searchString
						});
	}
	
	this.showSwatchDetailsPopup = function(fabricSwatchID, left_position, top_position)
	{
		fabrics.hideAllSwatchDetailsPopups();
		$('swatchdiv'+fabricSwatchID).style.left = left_position;
		$('swatchdiv'+fabricSwatchID).style.top = top_position;
		$('swatch'+fabricSwatchID).style.display = 'block';
		if ($('swatchdiv'+fabricSwatchID).innerHTML == '')
		{
			$('swatchdiv'+fabricSwatchID).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('swatchdiv'+fabricSwatchID, 
						'/account/includes/ajax/fabric_swatch_details.php',
						{
							parameters:'fabricSwatchID='+fabricSwatchID+
										'&hide_collection_select=Y'
						});
		}
	}
	
	this.hideAllSwatchDetailsPopups = function()
	{
		var i = 0;
		while ($('swatchvalue'+i))
		{
			var fabricSwatchID = $('swatchvalue'+i).value;
			$('swatch'+fabricSwatchID).style.display = 'none';
			i++;
		}
	}
	

	this.hideSwatchPopup = function(swatch_index)
	{
		$('swatch'+swatch_index).style.display = 'none';
	}
	

	// SEARCH //
	
	this.fabricsSearchFiltersChanged = function(page)
	{
		if (!page)
			page = 1;
		$('fs_p').value = page;
		$('StorefrontSearchForm').submit();
	}
	
	this.fabricsManufacturerChanged = function()
	{
		var selectedMN = '';
		if ($('fs_mn').options[$('fs_mn').selectedIndex].value == 'FEATURED_COLLECTIONS')
			window.location = '/main/fabrics_features.php';
		else
			fabrics.fabricsSearchFiltersChanged();
	}
	
	this.fabricsClearSearch = function(fabricUseIDs)
	{
		if ($('fs_mn'))
			$('fs_mn').selectedIndex = 0;
		if ($('fs_d'))
			$('fs_d').selectedIndex = 0;
		$('fs_coll').selectedIndex = 0;
		$('fs_s').selectedIndex = 0;
		$('fs_c').selectedIndex = 0;
		$('fs_m').selectedIndex = 0;
		$('fs_key').value = '';
		
		//window.location = "/www/textiles.php?"+Form.serialize('StorefrontSearchForm');
		fabrics.fabricsSearchFiltersChanged();
	}
	
	
	
	
	this.goToSearchPage = function(page)
	{
		fabrics.fabricsSearchFiltersChanged(page);
	}
	
	this.fabricSwatchSizeChanged = function(selectObj)
	{
		$('fs_sc').value = selectObj.options[selectObj.selectedIndex].value;
		fabrics.fabricsSearchFiltersChanged();
	}
	
	//SWATCH DETAILS
	this.showSwatchDetails = function(fabricSwatchID, showroomAccountID)
	{
		new Ajax.Updater('fabricSwatchDetailsContainer', 
						'/account/includes/ajax/fabric_swatch_details.php', 
						{
							parameters:'fabricSwatchID='+fabricSwatchID+
										'&showroomAccountID='+showroomAccountID+
										'&ss='+fabrics.searchString
						});
	}
	
	
	//COLLECTION DETAILS
	this.viewAllCollection = function(fabricCollectionID)
	{
		window.location = '/account/collection_details.php?fcID='+fabricCollectionID;
	}

	this.collectionDetailsSwatchesCountChanged = function(AID, fabricCollectionID, link_prefix)
	{
		if ($('compare_on') && $('compare_on').checked)
		{
			window.location = "/account/collection_details.php?AID="+AID+"&fcID="+fabricCollectionID+'&'+Sortable.serialize("fabricCollectionSwatchesContainer", {name:'fsIDs'})+
							'&sc='+$('sc').options[$('sc').selectedIndex].value+
							'&c=Y';
		}
		else
			window.location = "/account/collection_details.php?AID="+AID+"&fcID="+fabricCollectionID+'&sc='+
							$('sc').options[$('sc').selectedIndex].value;
	}
	
	this.showFabricSwatchDetails = function(fabricSwatchID, positionLeft, positionTop)
	{
		$('swatchdiv'+fabricSwatchID).style.left = positionLeft;
		$('swatchdiv'+fabricSwatchID).style.top = positionTop;
		$('swatchdiv'+fabricSwatchID).style.width = '950px';
		$('swatchdiv'+fabricSwatchID).style.height = '360px';
		
		$('swatchdiv'+fabricSwatchID).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('swatchdiv'+fabricSwatchID, 
						'/account/includes/ajax/fabric_swatch_details.php',
						{
							parameters:'fabricSwatchID='+fabricSwatchID
						});
	}
	
	this.fabricCollectionChanged = function()
	{
			//window.location = "/account/swatch_details.php?fcID="+$('fcID').options[$('fcID').selectedIndex].value;
			$('FabricCollectionSearchForm').submit();
	}
	
	this.fabricCollectionChangedViewAll = function(AID)
	{
			window.location = "/account/collection_details.php?AID="+AID+"&fcID="+$('fcID').options[$('fcID').selectedIndex].value;
	}
	
	
	this.goToViewCollection = function(fabricSwatchID)
	{
		window.open('/account/swatch_details.php?swatchID='+fabricSwatchID,'_blank','directories=0,menubar=0,scrollbars,resizable');
	}
	
	this.goToViewAll = function(fabricCollectionID)
	{
		window.open('/account/collection_details.php?fcID='+fabricCollectionID,'_blank','directories=0,menubar=0,scrollbars,resizable');	
	}
	
	
	
	this.compareSwatchesChanged = function(AID, fabricCollectionID, c)
	{
		$('compareSwatchesForm').submit();
	}
}



