// JavaScript Document
var $j = jQuery.noConflict();
$j(document).ready(function() {
String.prototype.trim = function () {
  var sNew = this.replace(/^\s*/, ""); // strip whitespace from the beginning of the string
  sNew = sNew.replace(/\s*$/, ""); // strip whitespace from the end of the string
  return sNew; // return the mutated string
 }					
activeButtonsState = false;

var productTd = $j('table.product-catalog tr td:first-child');
var quoteAdded;
var quoteItem;
var quoteValue;
var quoteList = new Array;
var removeQuote = {};
var getQuoteLength = {};
var quoteMessage = {};

if ($j.cookie('equote')) {
quoteList = $j.cookie('equote').split(', ');
for (var x=0; x<=quoteList.length; x++)
  if (quoteList[x] != undefined)
	$j('div#quotes ul').append('<li><span>'+quoteList[x]+'</span> <a class="remove" href="#">[x]</a></li>');
}
productTd.addClass('catalog');
$j('td.ltgrey').removeClass('catalog');
$j('td.ltgreen').removeClass('catalog');
$j('td.dkgrn').removeClass('catalog');
$j('td.exception').removeClass('catalog');
$j('a#expand').click(function(){
					return false											 
		});
var catalogTd = $j('td.catalog');
var addHref = catalogTd.each(function(index) {
		catNumber = $j(this).html(); 
		//tmp = catNumber.split(' ');
		//catNumber = tmp[0];
		//if (tmp[1] != undefined)
			//extraTxt = ' <span>'+tmp[1]+'</span>';
		//else
		//	extraTxt = ' ';
		if($j(this).hasClass('discontinued')) {
		$j(this).html('<span class="catWrapper"><a class="tip" href="#" title="Item has been discontinued.">'+catNumber+'</a></span>');
		} else {
		$j(this).html('<span class="catWrapper"><a class="tip" href="#" title="Clicking a catalog number will add the item to your quote.">'+catNumber+'</a></span>');
		}
		// Tooltip Functions/////////////////////////////
	$j.extend($j.fn.Tooltip.defaults, {
				track: true,
				delay: 0,
				showURL: false,
				showBody: " - "
			});
	$j("a.tip").Tooltip();
});
var addMessage = $j(catalogTd).children('span').each(function(index) {
		$j(this).append('<span class="overlayMessage">Item '+$j(this).text()+' has been added to your quote</span>');																										
});
getQuoteLength = function() { //gets the number of quotes in the list
		qNum = quoteList.length;
};
quoteMessage = function() { //after getting the total number of quotes it then changes the message to display the number of quotes
		if (quoteList.length > 0) {
		$j('#message').html('You currently have <span>'+quoteList.length+'</span> items in your quote.');
		$j.cookie('equote', quoteList.join(', '), {path: '/'});
		$j("#quickquote-total span").text(quoteList.length);
		} 
		else {
			$j('#message').html('Click a Catalog Number to add to your Extec Quick Quote.');
			$j.cookie('equote', null, {path: '/'});
			$j("div#quickquote-total span").text("0");
			
			$j('a#expand').text('View Products');
			$j('div#quotes ul').slideUp('fast')	;
			$j('#show-process a').addClass('disabled');
			$j('#show-process a').css('opacity', '.30');
		}
		
		
		return false;
};
getQuoteLength();
if (quoteList.length > 0) {
	$j('#show-process a').removeClass('disabled');
		activeButtonsState = true;
		quoteMessage();
};

	$j('a#expand').click(function() {
		if (quoteList.length == 0)
			return false;
		
		if ($j(this).text() == 'View Products') {
			$j(this).text('Hide Products');
			$j('div#quotes ul').slideDown('fast');
		} else {
			$j(this).text('View Products');
			$j('div#quotes ul').slideUp('fast')	;
		}
		return false;
		
	});


var checkList = function(qNum) {
	for(x=0;x<=quoteList.length;x++) {
		if (qNum == quoteList[x] && qNum != null)
			return false;
	}
	return true;
}
var removeFromList = function(qNum) {
	tmpList = new Array;
	for(x=0;x<=quoteList.length;x++) {
	//	alert('checking '+quoteList[x]+' against removable item '+qNum);
		if (qNum != quoteList[x] && qNum != null && quoteList[x] != undefined) {
			tmpList.push(quoteList[x]);
	//		alert(quoteList[x]+' in temp list');
		}
	}
	quoteList = new Array();
	quoteList = tmpList;
	getQuoteLength();

	return false;
}
var addQuotes = function() {
		$j('div#quotes ul').append('<li><span>'+thisCat+'</span> <a class="remove" href="#">[x]</a></li>');
		$j('div#quotes ul li:last').show(function() {
			removeQuote(this);
		});
		$j('#show-process a').removeClass('disabled');
		$j('#show-process a').fadeTo(200, 1.0);
		quoteList.push(thisCat);
		getQuoteLength();
		quoteMessage();	
};
var processQuote = $j('a#process');
	processQuote.click(function() {
		if (quoteList.length > 0) {
			return true;
		} else {
			return false;
		}
	});
removeQuote = function(scope) {
		$j('a.remove', scope).click(function() {
					$j(this).parent('li').fadeOut(200, function() {
						removeFromList($j(this).children('span').text());
						//alert($j(this).children('span').text());
						$j(this).parent('li').remove();
						quoteMessage();
						return false;
				});				
					return false;
		});
				
};
removeQuote(this);

$j('td.catalog a').click(function() {	
	if($j(this).parent('span').parent('td').hasClass('discontinued')) {
		return false;													 
	}
	tmpCat = $j(this).text(); 
	tmpCat = tmpCat.trim().split(' ');
	thisCat = tmpCat[0];
	//thisCat = parseFloat(thisCat);
	if(!checkList(thisCat)) {
		//alert('You have already added this Catalog Number to your Quote List');
		var quoteAddMessage = $j(this).parent('span').children('span');
		quoteAddMessage.text('Item '+thisCat+' has already been added to your quote');
		quoteAddMessage.addClass('alert');
		quoteAddMessage.fadeIn(300, function() {
			quoteAddMessage.animate({opacity: 1.0}, 1000, function() {
				quoteAddMessage.fadeOut(500);		
			});
		});
		return false
	} else {
		activeButtonsState = true;
		var quoteAddMessage = $j(this).parent('span').children('span');
		quoteAddMessage.removeClass('alert');
		quoteAddMessage.text('Item '+thisCat+' has been added to your quote');
		quoteAddMessage.fadeIn(300, function() {
			quoteAddMessage.animate({opacity: 1.0}, 1000, function() {
				quoteAddMessage.fadeOut(500);	
			});
		});
		addQuotes();
		//removeQuote(this);
	};
return false
});

});