//Set up global variables
var FS_ID = null;

///////////////////////////////////////////////////////////////////////////////////////
// FABRIC SEARCH FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////

// openFabricDiv(num)
// Opens the fabric search popup
// ars: num - the id number
function openFabricDiv(id,fabric_id,url){
	//Check id against the globale FS_ID to see if 
	//this 'take a look' box is already open
	if( id != FS_ID ){
		//Fade out previously open take a look box
		if( FS_ID != null ) {
			fabricDivClose(FS_ID);
		}
		
		//Set the fabric id
		$('fabricID').value = fabric_id;
		
		//Load fabric image into the pop up
		$('fsImg'+id).src = url;
		
		//Set position and display of the 'take a look' box
		$("fsBlock"+id).style.top = (getScrollY()+60) + "px";
		$("fsBlock"+id).style.left = (window.getWidth() / 2)-180 + "px";
		$("fsBlock"+id).style.display = "inline";
		
		//Set opacity of 'take a look' box to zero
		$("fsBlock"+id).set('opacity',"0.0");
		
		//Fade in 'take a look' box
		var myFx = new Fx.Tween("fsBlock"+id);
		myFx.set('tween', {duration: '2000'});
		myFx.start('opacity','0.0','1.0');
		
		//Reset global variable
		FS_ID = id
	}
}


function fabricDivClose(id){
	if( FS_ID == id ){
		FS_ID = null;	
	}
	
	//Fade out the fabric box
	var myFx = new Fx.Tween("fsBlock"+id);
	myFx.set('tween', {duration: '2000'});						 
	myFx.start('opacity','1.0','0.0');
	
	//Set fabric box display to none
	window.setTimeout( function () { $("fsBlock"+id).style.display = "none"; }, 1000);
}


function processFabricSearch(num){
	var fabricArgs = new2DArray(3,2);
	var length = itemAdded = errorBlock = "";
	validationErrors = new Array();

	//Get and validate fabric length
	length = $('fsLen'+num).value;
	if( !isNumeric(length) ){ 
		alert("INVALID LENGTH: Must be greater than zero and contain only numerical characters 0-9.");
	} else if( length < 3 ){ 
		alert("INVALID LENGTH: Length must be at least 3 yards."); 	
	} else {
		
		//Set up arguments for add to cart
		fabricArgs[0][0] = "item_id";
		fabricArgs[0][1] = $('itemID').value;
		fabricArgs[1][0] = "fabric_id";
		fabricArgs[1][1] = $('fabricID').value;
		fabricArgs[2][0] = "length";
		fabricArgs[2][1] = length;
		
		//run ajax to add item to cart
		itemAdded = runAJAXsync('curtains_ajax',fabricArgs);
		
		if( itemAdded != "Item Added to Cart" ){
			alert("ERROR: Unable to add your fabric to the cart.</h4> Refresh the page and try again if the problem persists call 800-791-2946.</center>");
		} else {
			$('fsButton'+num).innerHTML = "<img src='/images/graphics/fs-added-button.gif' alt='Added to Estimate' />";
		}
	}
}

// getColors()
// Executes an ajax call that add the given fabric to the sample request cart
// Args: id - the id of the fabric that is requested, handle - the id of the link that was clicked
function requestSampleSearch(fabric_id,num){
	args = new2DArray(2,2);
	args[0][0] = 'sample';
	args[0][1] = 'add';
	args[1][0] = 'fabric_id';
	args[1][1] = fabric_id;
	
	//Change link text
	$("fsReq"+num).innerHTML = "<b class='required'>Sample Requested</b>";
	
	var temp = runAJAXsync('fabrics_ajax',args);
	alert(temp);
}
///////////////////////////////////////////////////////////////////////////////////////
// END FABRIC SEARCH FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
// INITIALIZE FABRIC SEARCH FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////

function initFabricSearch(){
	
	//INITIALIZE STEPS LINKS AND STYLES LINKS
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	
	// loop through all anchor tags and set up onclick calls
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		
		if(anchor.getAttribute("name") && (anchor.getAttribute("name") == "fsLink")){
			anchor.onclick = function (){ openFabricDiv(this.getAttribute("rel"),this.getAttribute("rev"),this.getAttribute("href")); return false; }
		}else if(anchor.getAttribute("rev") && (anchor.getAttribute("rev") == "close")){
			anchor.onclick = function (){ fabricDivClose(this.getAttribute("rel")); return false; }
		}else if(anchor.getAttribute("rev") && (anchor.getAttribute("rev") == "fsSubmit")){
			anchor.onclick = function (){ processFabricSearch(this.getAttribute("rel")); return false; }
		}else if(anchor.getAttribute("name") && (anchor.getAttribute("name") == "fsRequest")){
			anchor.onclick = function (){ requestSampleSearch(this.getAttribute("rel"),this.getAttribute("rev")); return false; }
		}
	}	
}


addLoadEvent(initFabricSearch);
///////////////////////////////////////////////////////////////////////////////////////
// INITIALIZE FABRIC SEARCH FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////