var hardwareItems = new hardware();

function hardware()
{
	this.page_loaded = 'N';
	this.ts_loc = '';
	this.catID = '';
	this.targetPage='';
	this.catArray= new Array();
	
	this.showHardwareItemPopup = function(accountID, hardwareID, hardware_index, item_index, browser)
	{
		if ($('hardwareItemPopupImage_'+hardware_index).innerHTML.indexOf('t_img') < 0){
			//alert('you are here');
			this.loadHardwarePopupImage(accountID, accountID, hardwareID, hardware_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);
		
		var imgObj = $('hardwareThumbIMG'+hardwareID);
		
//		if (distance == 0)
//			$('hardwareItemdiv'+hardware_index).style.left = '10px';
//		else if (distance == 1 || distance == 2)
//			$('hardwareItemdiv'+hardware_index).style.left = '-100px';
//		else 
//			$('hardwareItemdiv'+hardware_index).style.left = '-250px';
		
		switch(distance){
		case 0:
			popupLeft = '10px';
			break;
		case 1:
			popupLeft = '-100px';
			break;
		case 2:
			if(imgObj.width >= imgObj.height)
				popupLeft = '-'+(imgObj.width/2)+'px';
			if(imgObj.width < imgObj.height)
				popupLeft = '-'+(imgObj.width/2)+'px';;
			break;
		case 3:
			if(imgObj.width >= imgObj.height)
				popupLeft = '-'+(125+imgObj.width)+'px';
			if(imgObj.width < imgObj.height)
				popupLeft = '-'+(125+imgObj.width)+'px';
			break;
		default:
			popupLeft = '-135px';
			break;
		}
		$('hardwareItemdiv'+hardware_index).style.left = popupLeft;
		
		$('hardwareItem'+hardware_index).style.display = 'block';
	}
	
	this.showHardwarePopupNoStrip = function(accountID, showroomID, hardwareID, hardware_index, left_position)
	{
		if ($('hardwareItemPopupImage_'+hardware_index).innerHTML.indexOf('ts_img') < 0)
			this.loadHardwarePopupImage(accountID, showroomID, hardwareID, hardware_index);
		$('hardwareItemdiv'+hardwareID).style.left = left_position;
		$('hardwareItem'+hardwareID).style.display = 'block';
	}
	
	this.loadHardwarePopupImage = function(accountID, showroomID, hardwareID, hardware_index)
	{   //alert(this.page_loaded);
		if (this.page_loaded == 'N')
		{
			this.timer = setTimeout("hardwareItems.loadHardwarePopupImage('"+accountID+"', '"+showroomID+"', '"+hardwareID+"', '"+hardware_index+"')", 500);
			return;
		}
		
		window.clearTimeout(this.timer);
		
		new Ajax.Updater('hardwareItemPopupImage_'+hardware_index, 
						'/include/ajax/hardware_popup_image.php', 
						{
						parameters:'accountID='+accountID+
									'&showroomID='+showroomID+
									'&hardwareID='+hardwareID+
									'&ts_loc='+this.ts_loc+
									'&ss='+this.searchString+
									'&catID='+this.catID
						});
	}
	
	this.showHardwareDetailsPopup = function(hardwareID, left_position, top_position)
	{
		hardwareItems.hideAllSwatchDetailsPopups();
		$('hardwareItemdiv'+hardwareID).style.left = left_position;
		$('hardwareItemdiv'+hardwareID).style.top = top_position;
		$('hardware'+hardwareID).style.display = 'block';
		if ($('hardwareItemdiv'+hardwareID).innerHTML == '')
		{
			$('hardwareItemdiv'+hardwareID).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('hardwareItemdiv'+hardwareID, 
						'/include/ajax/hardware_details.php',
						{
							parameters:'hardwareID='+hardwareID+
										'&hide_collection_select=Y'
						});
		}
	}
	
	this.hideAllHardwareDetailsPopups = function()
	{
		var i = 0;
		while ($('hardwarevalue'+i))
		{
			var hardwareID = $('hardwarevalue'+i).value;
			$('hardware'+hardwareID).style.display = 'none';
			i++;
		}
	}
	

	this.hideHardwareItemPopup = function(hardware_index)
	{
		$('hardwareItem'+hardware_index).style.display = 'none';
	}
	

	// SEARCH //
	
	
	this.hardwareSearchKBCatChanged = function()
	{
		var kb_cat = $('kb_cat').options[$('kb_cat').selectedIndex].value;
		if (kb_cat != 'HARDWARE' && kb_cat != 'KIT')
		{
			$('kb_cat').selectedIndex = 0;
			alert('Coming soon');
			return;
		}
		this.hardwareSearchFiltersChanged();
	}
	
	this.hardwareSearchFiltersChanged = 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 hardwareSearchForm = Form.serialize('hardwareSearchForm');
		this.aggregateCategoryArray();
		window.location = this.targetPage+"?"+hardwareSearchForm+'&catArray='+this.catArray.join(",")+'&ts_sc='+ts_sc+'&ts_p='+page;
	}
	
	
	this.hardwareSearchCategoryChanged = function(selectObj)
	{
		if ($('ts_s'))
			$('ts_s').selectedIndex = 0;
		if ($('ts_sp'))
			$('ts_sp').selectedIndex = 0;
		
		
		selectID = selectObj.id;//the ID from the calling obj
		selectIDIndex = selectID.substring(selectID.length-1, selectID.length);//the number at the end is the index
		if(selectIDIndex == "t"){//doesn't have an index cause its the extra/new one. set it higher than the index will go
			selectIDIndex = 10;
		}else{
			if(selectIDIndex >0 && (selectObj.value == -1 || 
									selectObj.value == '-1' ||
									selectObj.value == '' )){
				selectIDIndex--;//pop it up a level if we just set the current filter to "all categories"
			}	
		}
		//this.aggregateCategoryArray(selectIDIndex);
		this.hardwareSearchFiltersChanged();
	}
	
	this.aggregateCategoryArray = function(){
		//alert ("stop at: " + stopIndex);
		idx=0;
		while($('ts_cat'+idx)){
			this.catArray.push($F('ts_cat'+idx));
			idx++;
		}
		if($('ts_cat')){
			this.catArray.push($F('ts_cat'));
		}
		
		if(this.catArray[this.catArray.length-1] == -1 || this.catArray[this.catArray.length-1] == '-1'){
			this.catArray.pop();
		}
		//alert($('ts_cat').getValue);
		//alert(this.catArray);
	}
	
	
	this.hardwareSwatchCountChanged = function(page, selectObj)
	{
		$('ts_sc').selectedIndex = selectObj.selectedIndex;
		$('ts_sc1').selectedIndex = selectObj.selectedIndex;
		this.hardwareSearchFiltersChanged(page);
	}
	
	
	this.hardwareShowroomChanged = function(shSelectObj)
	{
		window.location = shSelectObj.options[shSelectObj.selectedIndex].value;
		shSelectObj.selectedIndex = 0;
	}
	
	this.hardwareManufacturerChanged = function(mnSelectObj)
	{
		window.location = mnSelectObj.options[mnSelectObj.selectedIndex].value;
		mnSelectObj.selectedIndex = 0;
	}
	
	this.hardwareDesignerChanged = function(designerSelectObj)
	{
		var selectedVal = designerSelectObj.options[designerSelectObj.selectedIndex].value
		if (selectedVal == '' || selectedVal == '-1')
			window.location = this.targetPage+"?ts_d="+selectedVal;
		else
		{
			designerSelectObj.selectedIndex = 0;
			window.location = selectedVal;
		}
	}
	
	this.hardwareCollectionChanged = function(collectionSelectObj)
	{
		window.location = collectionSelectObj.options[collectionSelectObj.selectedIndex].value;
		collectionSelectObj.selectedIndex = 0;
	}
	
	this.searchHardwareLocation = function(locSelectObj, level)
	{
		var target_file = $('location_curpage').value;
		var loc = locSelectObj.options[locSelectObj.selectedIndex].value;
		if (loc == 114){
			window.open('http://nydc.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 256){
			window.open('http://adac.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 329){
			window.open('http://denverdesign.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 261){
			window.open('http://marketplacedc.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 390){
			window.open('http://www.lagunadesigncenter.com/products', '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 257){
			window.open('http://michigandesign.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 311){
			window.open('http://imsdesigncenter.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 357){
			window.open('http://seattledesigncenter.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 436){
			window.open('http://sfdesigncenter.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 331){
			window.open('http://ddbuilding.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 498){
			window.open('http://pacificdesigncenter.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 497){
			window.open('http://dcota.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		if (loc == 499){
			window.open('http://decorativecenter.designcentersearch.com/'+target_file, '_blank','');
			locSelectObj.selectedIndex = 0;
			return;
		}
		
		$('ts_loc').value = loc;
		$('ts_loc_level').value = level;
		this.hardwareSearchFiltersChanged();
	}
	
	this.hardwareClearSearch = function()
	{
		$('ts_mn').selectedIndex = 0;
		$('ts_d').selectedIndex = 0;
		$('ts_coll').selectedIndex = 0;
		$('ts_s').selectedIndex = 0;
		$('ts_sp').selectedIndex = 0;
		$('fs_sh').value = '';
		
		window.location = this.targetPage+"?"+Form.serialize('hardwareSearchForm');
	}
	
	
	this.goToSearchPage = function(page)
	{
		this.hardwareSearchFiltersChanged(page);
	}
	
	
	//SWATCH DETAILS
	this.showSwatchDetails = function(hardwareID)
	{
		new Ajax.Updater('hardwareDetailsContainer', 
						'/include/ajax/hardware_details.php', 
						{
							parameters:'hardwareID='+hardwareID
						});
	}
	
	
	//COLLECTION DETAILS
	this.viewAllCollection = function(hardwareCollectionID)
	{
		window.location = '/collection_details.php?tcID='+hardwareCollectionID;
	}

	this.collectionDetailsSwatchesCountChanged = function(hardwareCollectionID)
	{
		if ($('compare_on') && $('compare_on').checked)
		{
			window.location = "/collection_details.php?tcID="+hardwareCollectionID+'&'+Sortable.serialize("hardwareCollectionHardwareContainer", {name:'tIDs'})+
							'&sc='+$('tc').options[$('tc').selectedIndex].value+
							'&c=Y';
		}
		else
			window.location = "/collection_details.php?tcID="+hardwareCollectionID+'&tc='+
							$('tc').options[$('tc').selectedIndex].value;
	}
	
	this.showHardwareDetails = function(hardwareID, positionLeft, positionTop)
	{
		$('hardwareItemdiv'+hardwareID).style.left = positionLeft;
		$('hardwareItemdiv'+hardwareID).style.top = positionTop;
		$('hardwareItemdiv'+hardwareID).style.width = '950px';
		$('hardwareItemdiv'+hardwareID).style.height = '360px';
		
		$('hardwareItemdiv'+hardwareID).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('hardwareItemdiv'+hardwareID, 
						'/include/ajax/hardware_details.php',
						{
							parameters:'hardwareID='+hardwareID
						});
	}
	
	this.hardwareCollectionsChanged = function()
	{
			window.location = "hardware_details.php?tcID="+$('tcID').options[$('tcID').selectedIndex].value+'&ss='+hardwareItems.searchString;
	}
	
	this.hardwareCollectionChangedViewAll = function()
	{
			window.location = "hardware_collection_details.php?tcID="+$('tcID').options[$('tcID').selectedIndex].value+'&ss='+hardwareItems.searchString;
	}
	
	
	this.goToViewCollection = function(hardwareID)
	{
		window.open('/hardware_details.php?hardwareID='+hardwareID,'_blank','');
	}
	
	this.goToViewAll = function(fabricCollectionID)
	{
		window.open('hardware_collection_details.php?tcID='+hardwareCollectionID,'_blank','');	
	}
	
	
	
	this.compareHardwareChanged = function(hardwareCollectionID, c)
	{
		if ($('compare_on').checked == true)
		{
			if (c != 'Y')
			{
				window.location = "hardware_collection_details.php?tcID="+hardwareCollectionID+'&c=Y&tIDs='+$('tIDs').value;
			}
		}
		else
		{
			window.location = "hardware_collection_details.php?fcID="+hardwareCollectionID+'&'+Sortable.serialize("hardwareCollectionHardwareContainer", {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 = '/hardware.php?'+$('hardwareSearchForm').serialize()+'&'+$('FurnitureShopADAdvertisersForm').serialize()+'#adads';
	}
}


document.observe("dom:loaded", function() {
	  hardwareItems.page_loaded = "Y";
	});


