var fabrics = new fabrics();

function fabrics()
{
	this.page_loaded = 'N';
	this.fs_loc = '';
	
	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;
		}
		
		new Ajax.Updater('textilePopupImage_'+fabricSwatchID, 
						'/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, 
						'/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;
		var fs_sc = '';
		if ($('fs_sc'))
		{
			fs_sc = $('fs_sc').options[$('fs_sc').selectedIndex].value;
		}
		else if ($('fs_a_sc'))
		{
			fs_sc = $('fs_a_sc').options[$('fs_a_sc').selectedIndex].value;
		}
		else 
			fs_sc = '';
		var fabricSearchForm = Form.serialize('fabricsSearchForm');
		window.location = "fabrics.php?"+fabricSearchForm+'&fs_sc='+fs_sc+'&fs_p='+page;
	}
	
	this.fabricsSwatchCountChanged = function(page, selectObj)
	{
		$('fs_sc').selectedIndex = selectObj.selectedIndex;
		$('fs_sc1').selectedIndex = selectObj.selectedIndex;
		fabrics.fabricsSearchFiltersChanged(page);
	}
	
	
	this.fabricsShowroomChanged = function(shSelectObj)
	{
		window.location = shSelectObj.options[shSelectObj.selectedIndex].value;
		shSelectObj.selectedIndex = 0;
	}
	
	this.fabricsManufacturerChanged = function(mnSelectObj)
	{
		window.location = mnSelectObj.options[mnSelectObj.selectedIndex].value;
		mnSelectObj.selectedIndex = 0;
	}
	
	this.fabricsDesignerChanged = function(designerSelectObj)
	{
		window.location = designerSelectObj.options[designerSelectObj.selectedIndex].value;
		designerSelectObj.selectedIndex = 0;
	}
	
	this.fabricsCollectionChanged = function(collectionSelectObj)
	{
		window.location = collectionSelectObj.options[collectionSelectObj.selectedIndex].value;
		collectionSelectObj.selectedIndex = 0;
	}
	
	this.searchFabricsLocation = function(locSelectObj, level)
	{
		$('fs_loc').value = locSelectObj.options[locSelectObj.selectedIndex].value;
		$('fs_loc_level').value = level;
		fabrics.fabricsSearchFiltersChanged();
	}
	
	this.fabricsClearSearch = function(fabricUseIDs)
	{
		$('fs_mn').selectedIndex = 0;
		$('fs_d').selectedIndex = 0;
		$('fs_coll').selectedIndex = 0;
		$('fs_s').selectedIndex = 0;
		$('fs_c').selectedIndex = 0;
		$('fs_m').selectedIndex = 0;
		$('fs_loc').value = '';
		
		window.location = "fabrics.php?"+Form.serialize('fabricsSearchForm');
	}
	
	this.fabricsAdvancedSearch = function(fs_a, page)
	{
		if (!page)
			page = 1;
		
		if ($('fabricsDetailedSearchFilters'))
		{	
			if ($('fabricsDetailedSearchFilters').style.display == 'block')
				window.location = "fabrics.php?"+Form.serialize('fabricsAdvancedSearchForm')+'&fs_a_p='+page;
			else
			{
				if (fs_a == 'Y')
				{
					$('fabricsDetailedSearchFilters').style.display = 'block';
				}
				else
				{	
					if ($('fs_sc'))
						window.location = "fabrics.php?"+Form.serialize('fabricsAdvancedSearchForm')+'&fs_sc='+$('fs_sc').options[$('fs_sc').selectedIndex].value+'&fs_a_p='+page;
					else
						window.location = "fabrics.php?"+Form.serialize('fabricsAdvancedSearchForm')+'&fs_a_p='+page;
				}
			}
		}
		else
		{
			//coming from the featured fabrics for instance
			window.location = "fabrics.php?fs_a=Y";
		}
	}
	
	this.fabricsClearAdvancedSearch = function(fabricUseIDs)
	{
		$('fs_a_mn').selectedIndex = 0;
		$('fs_a_d').selectedIndex = 0;
		$('fs_a_coll').selectedIndex = 0;
		$('fs_a_s').selectedIndex = 0;
		$('fs_a_s_t').selectedIndex = 0;
		$('fs_a_ct').selectedIndex = 0;
		$('fs_a_c').selectedIndex = 0;
		$('fs_a_c2').selectedIndex = 0;
		$('fs_a_m').selectedIndex = 0;
		$('fs_a_m_o').selectedIndex = 0;
		$('fs_a_tr').checked = false;
		$('fs_a_mod').checked = false;
		$('fs_a_ef').checked = false;
		$('fs_a_rr').checked = false;
		$('fs_a_fr').checked = false;
		
		var fabricUseIDsArr = fabricUseIDs.split(":");
		var i;
		for (i = 0; i < fabricUseIDsArr.length; i++)
		{
			if ($('fs_a_u'+fabricUseIDsArr[i]))
				$('fs_a_u'+fabricUseIDsArr[i]).checked = false;
		}
		
		window.location = "fabrics.php?"+Form.serialize('fabricsAdvancedSearchForm');
	}
	
	
	this.goToAdvancedSearchPage = function(page)
	{
		fabrics.fabricsAdvancedSearch('N', page);
	}
	
	this.goToSearchPage = function(page)
	{
		fabrics.fabricsSearchFiltersChanged(page);
	}
	
	
	//SWATCH DETAILS
	this.showSwatchDetails = function(fabricSwatchID)
	{
		new Ajax.Updater('fabricSwatchDetailsContainer', 
						'/includes/ajax/fabric_swatch_details.php', 
						{
							parameters:'fabricSwatchID='+fabricSwatchID
						});
	}
	
	
	//COLLECTION DETAILS
	this.viewAllCollection = function(fabricCollectionID)
	{
		window.location = '/collection_details.php?fcID='+fabricCollectionID;
	}

	this.collectionDetailsSwatchesCountChanged = function(fabricCollectionID)
	{
		if ($('compare_on') && $('compare_on').checked)
		{
			window.location = "/collection_details.php?fcID="+fabricCollectionID+'&'+Sortable.serialize("fabricCollectionSwatchesContainer", {name:'fsIDs'})+
							'&sc='+$('sc').options[$('sc').selectedIndex].value+
							'&c=Y';
		}
		else
			window.location = "/collection_details.php?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, 
						'/includes/ajax/fabric_swatch_details.php',
						{
							parameters:'fabricSwatchID='+fabricSwatchID
						});
	}
	
	this.fabricCollectionChanged = function()
	{
			window.location = "swatch_details.php?fcID="+$('fcID').options[$('fcID').selectedIndex].value+'&ss='+fabrics.searchString;
	}
	
	this.fabricCollectionChangedViewAll = function()
	{
			window.location = "collection_details.php?fcID="+$('fcID').options[$('fcID').selectedIndex].value+'&ss='+fabrics.searchString;
	}
	
	
	this.goToViewCollection = function(fabricSwatchID)
	{
		window.open('/swatch_details.php?swatchID='+fabricSwatchID,'_blank','directories=0,menubar=0,scrollbars,resizable');
	}
	
	this.goToViewAll = function(fabricCollectionID)
	{
		window.open('collection_details.php?fcID='+fabricCollectionID,'_blank','directories=0,menubar=0,scrollbars,resizable');	
	}
	
	
	
	this.compareSwatchesChanged = function(fabricCollectionID, c)
	{
		if ($('compare_on').checked == true)
		{
			if (c != 'Y')
			{
				window.location = "collection_details.php?fcID="+fabricCollectionID+'&c=Y&fsIDs='+$('fsIDs').value;
			}
		}
		else
		{
			window.location = "collection_details.php?fcID="+fabricCollectionID+'&'+Sortable.serialize("fabricCollectionSwatchesContainer", {name:'fsIDs'});;	
		}
		
	}
}


