/*  */

var infoCartStatus = 'closed';

function clearSearch(searchField,act) {
	if (searchField.value == 'Search' && act == 'focus') {
		searchField.value = '';
	} else if (searchField.value == '' && act == 'blur') {
		searchField.value = 'Search';
	}
} //end function

function toggleMenu(menu) {
	if ($(menu + 'Options')) {
		if ($(menu + 'Options').style.display == 'none') {
			$(menu + 'Options').style.display = '';
			$(menu).removeClassName('plus');
			$(menu).addClassName('minus');
		} else {
			$(menu + 'Options').style.display = 'none';
			$(menu).removeClassName('minus');
			$(menu).addClassName('plus');
		} //end if-else
	}
} //end function

function toggleMore(more) {
	if ($(more + '_more')) {
		if ($(more + '_more').style.display == 'none') {
			$(more + '_more').style.display = '';
			$(more).innerHTML = '&laquo; Show Less'
		} else {
			$(more + '_more').style.display = 'none';
			$(more).innerHTML = 'Show More &raquo;'
		} //end if-else
	}
} //end function


function checkItem(menuItem) {
	
	var menu = '';
	//alert(menuItem.parentNode.id);
	try {
	menu = menuItem.parentNode.id;
	if (menu.split('_')[1] == 'more') { menu = menuItem.parentNode.parentNode.id; }
	} catch(e) { }
	
	
	//var refreshPage = loadResults ? refreshPage:false;
	//alert($('lft_' + menuItem.id + 'Options') + ' ' + menuItem.id);
	
	
	if ($(menuItem).hasClassName('checked')) {
		$(menuItem).removeClassName('checked');
		if ($('lft_' + menuItem.id + 'Options')) {
			$('lft_' + menuItem.id + 'Options').style.display='none';
			
			//if ($(menuItem).readAttribute('name') == 'subclass') {
				var desc = $('lft_' + menuItem.id + 'Options').descendants();
				for (var i=0;i<desc.length;i++) {
					//alert(desc[i].innerHTML);
					$(desc[i]).removeClassName('checked');
				}
			//} //end if	

		} //end if
	} else {
		$(menuItem).addClassName('checked');
		if ($('lft_' + menuItem.id + 'Options')) {
			$('lft_' + menuItem.id + 'Options').style.display='';
		} //end if
	} //end if-else
	
	//if ($(menuItem).readAttribute('name') == 'subclass') {
		loadResults(1,true,menu);
	/*} else {
		loadResults();
	}*/
} //end function


function loadResults(page,loadOptions,menu) {
	var page = page ? page : 1;
	var menu = menu ? menu : '';
	if (loadOptions == true) { var opts = true; } else { opts = false; }
	
	if ($('searchBox').value != 'Search') { var searchString = '&search=' + encodeURIComponent($('searchBox').value); } else { var searchString = ''; }
	
	if (searchString != '' && page == 1) {
		$('resultsArea').innerHTML = '<div class="searching">Searching.... Please wait....</div>'
	} else {
		$('resultsArea').innerHTML = '<div class="searching">Loading.... Please wait....</div>'
	}

	$('footerResults').style.display='none';

	var toggleStates = new Array();
	var toggles = $$('.toggle');
	for (var i=0;i<toggles.length;i++) {
		if (toggles[i].hasClassName('minus')) {
			toggleStates[toggleStates.length] =encodeURIComponent(toggles[i].readAttribute('id'));
		} //end if
	} //end for
	
	var params = '';
	var filters = $$('.menuOption');
	
	for (var i=0;i<filters.length;i++) {
		
		if (filters[i].hasClassName('checked')) {
			params += '&' + encodeURIComponent(filters[i].readAttribute('name')) + '=' + encodeURIComponent(filters[i].readAttribute('value'));
			//alert(filters[i].readAttribute('name') + '=' + filters[i].readAttribute('value'));
		} //end if
	} //end for
	
	new Ajax.Request('/includes/online_inventory.cfm', {
	  method: 'post',
	  postBody: 'ajaxload=true&page=' + page + '&menu=' + menu + '&parts=' + $('parts').value + '&loadopts=' + opts + '&toggle=' + toggleStates + '&class=' + mainClass + searchString + params,
	  onSuccess: function(transport) {
			var results = transport.responseText.split('|');
			$('results').innerHTML = results[0];
			$('results2').innerHTML = results[0];
			if (results[1] == 1) { $('footerResults').style.display='block'; } else { $('footerResults').style.display='none'; }
			if (opts == true) { $('filters').innerHTML = results[2]; results.shift(); }
			results.shift();
			results.shift();
			$('resultsArea').innerHTML = results;
		
			if (Prototype.Browser.IE == true) {
				var invRecs = $$('.invRec');
				for (var x=0;x<invRecs.length;x++) { 
					$(invRecs[x]).style.height = ($(invRecs[x]).getHeight()-28) + 'px';
				} //end for
			}//end if IE
	  }
	});
	
} //end function

function addItem(partNo) {
	$('infoCart').style.height='20px';
	$('infoCart').style.width='150px';
	$('infoCart').style.overflow='hidden';
	infoCartStatus = 'closed';
	$('rec' + partNo).style.backgroundColor='#FFFF99';
	var dur = (Math.ceil($('rec' + partNo).positionedOffset()[1]/300)+1)/10;
	var addFinished = false;
	var animFinished = false;
	var countInfo = '';
	
	new Ajax.Request('/includes/online_inventory.cfm', {
	  method: 'post',
	  postBody: 'ajaxload=true&addItem=' + partNo,
	  onSuccess: function(transport) {
		  addFinished = true;
		  var results = transport.responseText.split('|');
		  countInfo = results[0];
		  results.shift();
		  $('infoCartItems').innerHTML = results;
		  if (animFinished == true) { $('infoCartCount').innerHTML = countInfo; }
	  }
	});

	Effect.Pulsate('rec' + partNo, { pulses: 2, duration: 0.5, from: 0.3, afterFinish: function() {
	$('button_' + partNo).src='/images/remove_item.gif';
	$('button_' + partNo).width='94';
	$('button_' + partNo).onclick = function() { removeItem(partNo); };
	$('rec' + partNo).style.backgroundColor='#eeeeee';
	$('clone').clonePosition($('rec' + partNo));
	$('clone').style.display='block';
	$('clone').morph('width:15px; height:20px; top:-30px; left:310px;', { duration: dur, afterFinish: function() {
		animFinished = true;
		if (addFinished == false) { $('infoCartCount').innerHTML = 'Adding Item...'; } else { $('infoCartCount').innerHTML = countInfo; }
		$('clone').style.display='none';	
	}});
	} });

}

function removeItem(partNo,forceOpen) {
	if (forceOpen == true) { } else {
		$('infoCart').style.height='20px';
		$('infoCart').style.width='150px';
		$('infoCart').style.overflow='hidden';
		infoCartStatus = 'closed';
	}
	
	new Ajax.Request('/includes/online_inventory.cfm', {
	  method: 'post',
	  postBody: 'ajaxload=true&removeItem=' + partNo,
	  onSuccess: function(transport) {
		  var results = transport.responseText.split('|');
		  $('infoCartCount').innerHTML = results[0];
		  results.shift();
		  $('infoCartItems').innerHTML = results;
	  }
	});	
	
	if ($('rec' + partNo)) {
		if (forceOpen == true) {
			$('button_' + partNo).onclick = function() { addItem(partNo); };
			$('button_' + partNo).src='/images/select_item.gif';
			$('button_' + partNo).width='83';
			$('rec' + partNo).style.backgroundColor='';			
		} else {
			Effect.Pulsate('rec' + partNo, { pulses: 2, duration: 0.5, from: 0.3, afterFinish: function() {
				$('button_' + partNo).onclick = function() { addItem(partNo); };
				$('button_' + partNo).src='/images/select_item.gif';
				$('button_' + partNo).width='83';
				$('rec' + partNo).style.backgroundColor='';
			} });
		} //end if
	} //end if
}


function toggleInfoCart() {
	if (infoCartStatus == 'closed') {
		infoCartStatus = 'anim';
		
		var comments = $$('.itemComment');
		for (var x=0;x<comments.length;x++) {
			if ($(comments[x]).style.display != 'none') { autoGrow($(comments[x]).id.split('_')[1],1); }
		}
		
		$('infoCart').morph('width:250px; height:' + (parseInt($('infoCartItems').getHeight())+parseInt($('infoCartCount').getHeight())) + 'px', { duration:0.5, afterFinish: function() { 
			infoCartStatus = 'open'; 
			$('infoCart').style.overflow='visible';
			$('infoCart').style.height='auto';
		}});
	} else if (infoCartStatus == 'open') {
		infoCartStatus = 'anim';
		$('infoCart').style.height=$('infoCartItems').getHeight() + 'px';
		$('infoCart').style.overflow='hidden';
		$('infoCart').morph('width:150px; height:20px', { duration:0.5, afterFinish: function() { infoCartStatus = 'closed'; }});	
	} //end if else
}

function toggleComment(button,partno) {
	if ($('comment_' + partno).style.display == 'none') {
		$(button).innerHTML = 'Hide Comment';
		$('comment_' + partno).style.display = '';
	} else {
		if ($('comment_' + partno).value != '') { 
			$(button).innerHTML = 'Show Comment';
		} else {
			$(button).innerHTML = 'Add Comment';
		}
		$('comment_' + partno).style.display = 'none';
	} //end if else
}

function toggleDesc(partno) {
	if ($('long_' + partno).style.display == 'none') {
		$('expand_' + partno).innerHTML = 'Less';
		$('long_' + partno).style.display = '';
		$('short_' + partno).style.display = 'none';
	} else {
		$('expand_' + partno).innerHTML = 'More';
		$('long_' + partno).style.display = 'none';
		$('short_' + partno).style.display = '';
	} //end if else	
}


function autoGrow(partno,anim,minHeight) {
	var noAnim = anim ? anim:0;
	var minHgt = minHeight ? minHeight:15;

	if ($('textarea_holder')) {
		$('textarea_holder').style.width=($('comment_' + partno).getWidth()-5) + 'px';
		var html = $('comment_' + partno).value; /*.replace(/(<|>)/g, '');*/
		html = html.replace(/\n/g, '<br>');
		$('textarea_holder').innerHTML = html;
		if (html == '') {
			$('comment_' + partno).style.height=minHgt+ 'px';
		} else {
			var textHeight = $('textarea_holder').getHeight()+15;
			if (textHeight < minHgt) { textHeight = minHgt; }
			if (noAnim == 1) { $('comment_' + partno).style.height=(textHeight)+ 'px'; }
			else { $('comment_' + partno).morph('height:' + (textHeight)+ 'px', { duration:0.3 }); }
		}
		/*$('saveComment_' + partno).style.display='block';*/
	}
}

function saveComment(cartItemId,partno) {
	new Ajax.Request('/includes/online_inventory.cfm', {
	  method: 'post',
	  postBody: 'ajaxload=true&saveComment=' + cartItemId + '&comment=' + encodeURIComponent($('comment_' + partno).value),
	  onSuccess: function(transport) {
	 }
	});	

}

function enlargeItem(partNo) {

	lightbox(partNo);

} //end function


function lightbox(partNo) {
	if (!$('lightboxCover')) {
		try { 
			Element.insert($('body'),{bottom:'<div id="lightboxCover" style="z-index:10000;position:absolute;top:0px;left:0px;overflow:hidden;background-color:transparent;opacity:0.6;filter:alpha(opacity=60);text-align:center;display:none;">&nbsp;</div>'}); 
			Element.insert($('body'),{bottom:'<div align="center" style="z-index:10000;position:absolute;top:0px;width:100%;display:none;" id="lightboxWrapper"><div id="itemContainer" style="border:solid 4px #196392; width: 700px; height: 600px; background-color:#ffffff;"></div></div>'}); 
			Event.observe(window, 'resize', function() { positionLightbox(); });
			Event.observe(window, 'scroll', function() { positionLightbox(); });
		} catch(e) { alert(e.description); }
	}

	$('itemContainer').innerHTML = '<table cellspacing="0" cellpadding="0" border="0" width="100%" height="600"><tr><td align="center">Loading...<br><br><a href="#" onclick="closelightbox();return false;"><b>CLOSE</b></a></td></tr></table>';
	
	$('lightboxCover').style.display='block';
	//Effect.Appear('lightboxCover', { duration: 0.5, from:0.0, to:0.6, afterFinish: function() { }});
	Effect.Appear('lightboxWrapper', { duration: 0.5, from:0.0, to:1.0 });	
	//$('body').style.overflow='hidden';
	//$('html').style.overflow='hidden';
	positionLightbox();
	
	new Ajax.Request('/includes/online_inventory.cfm?' + Math.random() * 999999, {
		method:'post',
		postBody:'ajaxload=true&partNo=' + partNo,  
		onSuccess: function(transport) { 
			$('itemContainer').innerHTML = transport.responseText;
			
			var imgDims = [$('itemImage').width,$('itemImage').height];
			if (imgDims[0] >= imgDims[1]) {
				$('itemImage').width = 590;
			} else {				
				$('itemImage').height = 590;
			}
	
		},
		onException: function(req,exception) { alert('Error: ' + exception.description); closelightbox(); return true; },
		onFailure: function(transport) { alert('Error'); closelightbox(); } 
	});	
	
	//setTimeout("if($('lightboxItem').style.display=='none') { $('lightboxImg').src='/images/portImages/159full.jpg'; }",2000);
} //end function

function closelightbox() {
	if ($('lightboxCover')) {
		Effect.Fade('lightboxWrapper', { duration: 0.3, from:1.0, to:0.0, afterFinish: function() {
			$('lightboxCover').style.display='none';
			$('itemContainer').innerHTML = '';
		}});
		/*Effect.Fade('lightboxCover', { duration: 0.3, from:0.6, to:0.0, afterFinish: function() { 
			$('body').style.overflow='visible';
			$('html').style.overflow='visible';
			$('itemContainer').innerHTML = '';
		} });*/
	}
} //end function

function positionLightbox(resize) {
	if ($('lightboxCover')) {
		var dimensions = screenSize();
		//$('lightboxCover').style.width=dimensions[0] + 'px';
		$('lightboxCover').style.width='100%';
		$('lightboxCover').style.height=dimensions[1] + 'px';
		var wrapperSpacer = (dimensions[1] - 600)/2;
		
		if (document.documentElement.scrollTop == 0) {
			$('lightboxCover').style.top = document.body.scrollTop + 'px';
			$('lightboxWrapper').style.top = Math.max(document.body.scrollTop + wrapperSpacer,document.body.scrollTop) + 'px';
		} else {			
			$('lightboxCover').style.top = document.documentElement.scrollTop + 'px';
			$('lightboxWrapper').style.top = Math.max(document.documentElement.scrollTop + wrapperSpacer,document.documentElement.scrollTop) + 'px';
		}
	} //end if
}

function loadImage(img) {
	$('itemImage').src = img.src;
	$('itemImage').removeAttribute("width");
	$('itemImage').removeAttribute("height");
	
	var imgDims = [$('itemImage').width,$('itemImage').height];
	if (imgDims[0] >= imgDims[1]) {
		$('itemImage').width = 590;
	} else {				
		$('itemImage').height = 590;
	}

	var borders = $$('.itemImg');
	for (var x=0;x<borders.length;x++) {
		$(borders[x]).style.borderColor='';
	} //end for
	$(img).style.borderColor='red';
}

function enterDetails() {
	window.location='default.cfm?submit=' + Math.ceil(Math.random() * 9999999);
}


function screenSize() {
	if (typeof( window.innerWidth ) == 'number') {
	//Non-IE
		var screenWidth = window.innerWidth;
		var screenHeight = window.innerHeight;
	} else {
		if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			var screenWidth = document.documentElement.clientWidth;
			var screenHeight = document.documentElement.clientHeight;
		} else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				var screenWidth = document.body.clientWidth;
				var screenHeight = document.body.clientHeight;
			} //end if
		} // end if-else
	} //end if-else
	
	return new Array(screenWidth,screenHeight);
} //end function


function valEmail(email) {
	invalidChars = " /:,;"
	if (email == "Required") {
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) != -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}

	return true
}


function clearFilters() {

	var filters = $$('.menuOption');
	
	for (var i=0;i<filters.length;i++) {
		if (filters[i].readAttribute('name') != 'subclass' && filters[i].hasClassName('checked')) {
			filters[i].removeClassName('checked');
		} //end if
	} //end for
	loadResults(1,true);
}