boots.productquickview = {

	c: {
		overlay: {
			links: '.quickViewLink',
			loadingId: 'loading',
			loading: false,
			overlay: '#overlay',
			dialogue: '#dialogue',
			dialogueOffset: 0,
			dialogueMargin: 20
		}
	},
	
	init: function() {
		var t = this;
		
		$(".quickViewLink").each(function(){
			$(this).click(function(){
				t._populateOverlay($(this));
				return false;
			});
		});
	},

	_getScrollPosition: function() {
		return {
			top: window.pageYOffset || document.documentElement.scrollTop || 0,
			left: window.pageXOffset || document.documentElement.scrollLeft || 0
		}
	},
	_populateOverlay: function(e) {
		var t = this,
			d = $(t.c.overlay.dialogue),
			u = e.attr('rel');
		
		// reset dialogue setting
		t.c.overlay.dialogueOffset = 0;
		d.css({
			height: 'auto',
			overflow: 'visible',
			marginTop: 0,
			top: 0,
			position: 'absolute'
		});
		
		if(!t.c.overlay.loading) {
			t.c.overlay.loading = true;
			t._loading(e);
			
			// make an ajax call to quickview.jsp
			$.ajax({
				url: u,
				dataType: 'html',
				success: function(data, status, xhr) {
					t.c.overlay.loading = false;
					t._loading();
				
					Adoro.Dialogue.setHTML(data);
					boots.TabSetCollection.init();
					Adoro.Dialogue.showDialogue({
						overlayOpacity: "0.8"
					});
					
					t._getCoremetricsData(data, u);
					t.c.overlay.dialogueOffset = d.position().top - t._getScrollPosition().top;
					
					// if not IE6, set position to fixed
					if(!($.browser.msie && parseInt($.browser.version) === 6)) {
						d.css({
							top: t.c.overlay.dialogueOffset + 'px',
							position: 'fixed'
						});
					}
	
					// if dialogue height > window height, then cut height of dialogue
					if(d.outerHeight() > $(window).height()) {
						d.css({
							height: ($(window).height() - (t.c.overlay.dialogueMargin * 2)) + 'px',
							marginTop: t.c.overlay.dialogueMargin + 'px',
							overflow: 'auto'
						});
					}
					boots.promotion.threeForTwo.updateLinksWithCookieDetails(true);
					boots.product.listgridview.setupAllActionsForALoadedQuickView();
				},
				error: function(xhr, status, err) {
					t.c.overlay.loading = false;
					t._loading();
					alert('Error getting product details');
				}
			});
		}
	},
	_getCoremetricsData: function (data, u){
		var cmdisabled = $('#cmdisabled').val();
		if (cmdisabled == undefined) {
			var viewType = $('#viewType').val();
			var sort = $('#sortBy :selected').text();
			var pageFormat = $('#pageFormat').val();
			var category = $('#cmCategory').val();
			
			//get the page format and the channel from the page view attributes.
			var exploreAtts = $('#exploreAttributes').val();

			exploreAtts = exploreAtts.replace("/-_-/g", "|");
			
			var exploreAttsArray = exploreAtts.split("|");
			
			var destinationURL = "" + u + "";
			var referringURL = "" + window.location + "";

			
			var myObj = eval("("+ $("#productDetailsQuickView .coremetricsData").attr("rel") +")");
	
			cmCreateManualProductviewTag(myObj["coremetricsPageName"], myObj["coremetricsProductId"], myObj["coremetricsProductName"], 
				myObj["coremetricsProductCategory"], myObj["store_id"] , "Y", "0", 
				myObj["outOfStock"] + '-_-' + myObj["reviewCount"] + '-_-' + myObj["averageReviewValue"] + '-_-' + myObj["crossSellHostId"] + 
				'-_-' + myObj["quickViewCategory"] + '-_-' + sort + '-_-' + exploreAttsArray[6] + '-_-' + exploreAttsArray[7] + '-_-' + myObj["viewType"] + 
				'-_-' + myObj["refinement"] + '-_-' + myObj["linkType"], destinationURL, referringURL);
			//cmCreateProductviewTag(<page name>, <product id>, <product name>, <category id>, <store id> , "Y", "0", <explore attributes>, <destination URL>, <referring URL>);
		}
	},
	_positionOverlay: function() {
		var t = this,
			o = $(t.c.overlay.overlay);

		// position the dialogue if it's IE6
		if(o.is(':visible') && ($.browser.msie && parseInt($.browser.version) === 6)) {
			$(t.c.overlay.dialogue).stop().animate({
				top: (t._getScrollPosition().top + t.c.overlay.dialogueOffset) + 'px'
			});
		}
	},
	
	_loading: function(el) {
		var t = this;
		if(t.c.overlay.loading) {
			var loading = $(document.createElement('div'))
							.attr('id', t.c.overlay.loadingId)
							.css({
								left: (el.offset().left + el.outerWidth()) + 'px',
								top: el.offset().top + 'px'
							});
							
			$('body').append(loading);
		} else {
			$('#' + t.c.overlay.loadingId).remove();
		}
	}
};

$(function() {
	boots.productquickview.init();
});
