var eslRevertTimeout = null;
var eslRevertDelay = .1; // seconds
eslRevertDelay = eslRevertDelay * 1000; // convert to miliseconds

function initExpressShopLink( options ) {
	if(typeof options == 'undefined') options = {};
	if(typeof options.prodId != 'undefined') options.productId = options.prodId;
	
	var prodLink = $(options.prodLink);
	
	prodLink.bind('mouseover', function() {
		showExpressShopLink(options);
		clearEslRevertTimeout();  // make sure box doesn't revert back
	});
	prodLink.bind('mouseout', {'productId': options.productId}, setEslRevertTimeout); // to revert back
}

function showExpressShopLink( options ) {
	if(typeof options == 'undefined') options = {};
	if(typeof options.prodId != 'undefined') options.productId = options.prodId;
	
	var prodLink = options.prodLink;
	var catId = options.catId;
	var productId = options.productId;
	
	if(typeof options.useAnimation == 'undefined') options.useAnimation = true;
	if(typeof options.showId == 'undefined') options.showId = '';
	if(typeof options.position == 'undefined') options.position = true;
	if(typeof options.left == 'undefined') options.left = false;
	if(typeof options.top == 'undefined') options.top = false;
	if(typeof options.noCartDD == 'undefined') options.noCartDD = false;
	if(typeof options.positionToOffsetParent == 'undefined') options.positionToOffsetParent = true;
	
	var leftMargin = 5;
	var topMargin = 5;
	
	var esLinkId = 'esl-'+productId;
	options.esLink = esLinkId;
	var esContent = 'esc-'+productId;
	options.esContent = esContent;
	
	var esLink = $('#' + esLinkId);
	if(!!esLink.length) {
		esLink.remove(); // remove link if already in page.  need to always create again because could be more than one of the same item in cross sells
	}
	
	$(prodLink).after('<a id="'+esLinkId+'" style="display: none;" class="esLink" href="#'+esContent+'">Quick&nbsp;View</a>');
	esLink = $('#' + esLinkId);
	esLink.bind('click', options, showExpressShop);
	esLink.mouseover(clearEslRevertTimeout);
	
	var pos = (options.positionToOffsetParent) ? $(prodLink).position() : $(prodLink).offset();
	
	if(options.position) esLink.css({left:(pos.left+leftMargin)+'px', top:(pos.top+topMargin)+'px'}); // if you are letting js position for you
	else{
		if(options.left !== false) esLink.css({left:options.left}); // if you are manually setting the left value
		if(options.top !== false) esLink.css({top:options.top}); // if you are manually setting the top value
	}
	
	$('.esLink').hide(); // hide others
	esLink.show();
}

function hideExpressShopLink( productId ) {
	$('#esl-' + productId).hide();
}

function setEslRevertTimeout( event ) {
	eslRevertTimeout = window.setTimeout('hideExpressShopLink("'+event.data.productId+'")', eslRevertDelay);
}
function clearEslRevertTimeout() {
	clearTimeout(eslRevertTimeout);
}

function showExpressShop( event ) {
	var options = {};
	if (typeof event == 'string') {
		//// Legacy compatibility ////
		options = arguments[2];
		if (!options) options = {};
		options.catId = arguments[0];
		options.productId = arguments[1];
	} else {
		event.preventDefault();
		options = event.data;
	}
	if(typeof options.prodId != 'undefined') options.productId = options.prodId;
	
	setTimeout(function() {
		
		var paramData = { 'catId': options.catId, 'productId': options.productId };
		if(typeof options.showId != 'undefined') paramData.showId = options.showId;
		if(typeof options.noCartDD != 'undefined') if(options.noCartDD) paramData.noCartDD = options.noCartDD;

		var useHeight = 580,
		closeLink = '#checkoutModalCloseLink',
		editingProduct = false;
		
		if(typeof options.editId != 'undefined'){
			paramData.editId = options.editId;
			useHeight = 533;
			editingProduct = true;
		}
		
		if(typeof options.initialSize != 'undefined') paramData.initialSize = options.initialSize;
		if(typeof options.from != 'undefined') paramData.from = options.from;
		
		var qvModal = new Modal({
			id: 'qvModal',
			height: useHeight,
			width: 670,
			quickView: true,
			closeSelector: closeLink,
			useAjax: true,
			url: '/' + jsContextRoot + '/browse/quickView.jsp',
			ajaxData: paramData,
			ajaxCb: function() {
				if (editingProduct) {
					qvModal.getModalDOM().addClass('editCartModal');
				}
				
				if (qvModal.getModalDOM().find('#content').hasClass('bookmark')) { // Test for bookmark page and transform size
					qvModal.transform({
						height: 200,
						width: 670
					});
				}
			}
		});
		qvModal.open();
		
	}, 0);
}

function quickViewAJAXError( XMLHttpRequest, textStatus, errorThrown ) {
	alert("Error making AJAX request:" + textStatus); // to do: handle properly
	hideExpressShop();
}

function qvSwapTabs( newTab ) {
	var parent = $('ul.qvTabs');
	parent.children('li').removeClass('selected');
	parent.children('li.'+newTab).addClass('selected');
	parent.nextAll('.qvTabContent').hide();
	parent.nextAll('.product_'+newTab).show();
}
