(function() {
	if ((typeof(jDisplayChangeTitleOverlay) != "undefined")
		&& (jDisplayChangeTitleOverlay == true)) {
		$.get('/content/changetitle/changeTitle.html', function(data) {
			displayChangeTitleOverlay(data)
		});
	}
})();

(function() {
    var addThisContent = $('#addThisContent');
	if (addThisContent.length > 0) {
		if (document.location.href.indexOf('disable3rdParty=add-this') > -1) {
			return;
		}
		addThisContent.load("/shop/3rdparty/get-add-this.action");
	}
})();


/* ----------------------------------------------------------------------------*/

/* Global location for methods required when adding warranties to products.
 * Moved here to avoid duplication and a new javascript file.
 */
function updateBagItems( numberOfBasketItems ) {
	//To set the contents of a div...below
	$('#numBagItemsSection').html(numberOfBasketItems);			
}

function handleCloseWarranty() {
	displayAddToBagMessage( currentAddToBagReturnData );
}

/* -------------------------------- Minibag Functions -------------------------*/

/*
 * The jQuery below generates the qTip used by the minibag. Coremetrics tags are also included
 * below.
 */
(function () {
	if (activateMinibag) {
		var target = '#gotoCheckOutContainerLink';		
		$(target).qtip({ 
			content: {
				url: '/shop/minibag/show.action',
				// IE will cache the ajax request unless POST is used.
				method: 'post'
			},		
			position: {
				corner: {
					target: 'bottomLeft',
					tooltip: 'topRight'
				},
				adjust: {
					x: 195, y: -25
				}			
			},
			show: {
				delay: 300
			},
			hide: {		
				fixed: true,
				when: 'mouseout', 
				delay: 100
			},
			style: {
				background: 'transparent',
				border: {
					width: 0,
					radius: 0,
					colour: '#181818'
				},
				width: calculateWidth()
			},
			api: {
				beforeShow: function() {		
					this.loadContent(this.options.content.url,this.options.content.data,this.options.content.method);
				},
				onShow: function() {
					PAGEVIEWTAG('MINIBAG', titleUID + '-' + 'MINIBAG');
				}
			}
		});
		
		if (document.getElementById("noMinibag")) {
			$(target).qtip('destroy');
			$(target).removeData('qtip');
		}
	}		
})();

function calculateWidth() {
	if($.browser.msie){
		return 527;
	} return 531;
}

function miniBagCMEvent(e, name) {
	MANUAL_LINK_CLICK_EX(e.target.href, titleUID + ' - MINBAG - ' + name, titleUID + ' - MINIBAG');
}

(function() {
	/* View minibag item */
	$("a[name='minibagView']").live('click', function(e) {		
		miniBagCMEvent(e, 'VIEW BAG ITEM');
	});
	
	/* Remove minibag item */
	$("a[name='minibagRemove']").live('click', function(e) {
		miniBagCMEvent(e, 'REMOVE BAG ITEM');
	});
	
	/* View recently viewed item */
	$("a[name='recentView']").live('click', function(e) {
		miniBagCMEvent(e, 'VIEW RECENT ITEM');
	});

	/* Continue to checkout */
	$("a[name='minibagCheckout']").live('click', function(e) {
		miniBagCMEvent(e, 'CHECKOUT');
	});	
})();

var itemRemoved;
var numberItemsInBasket;
var deleteOptions;
var refreshOptions;

function ajaxMinibagDelete(lineitem, lpUid) {
	deleteOptions = {
		url : '/shop/minibag/json/minibagRemoveItem.action?lineitem='+lineitem+'&pdLpUid='+lpUid,
		handler: handleAjaxMinibagDelete
	};
	submitMinibagAjax(deleteOptions);
}

function handleAjaxMinibagDelete(returnObj) {
	itemRemoved = returnObj.itemRemoved;
	numberItemsInBasket = returnObj.numberItemsInBasket;
	if (itemRemoved) {
		$('#numBagItemsSection').html(numberItemsInBasket);
		$('#shoppingBagSize').html(numberItemsInBasket);
		
		refreshOptions = {
			url: '/shop/minibag/show.action',
			dataType: 'html',
			handler: handleAjaxMinibagRefresh				
		};
		submitMinibagAjax(refreshOptions);
	}
}

function handleAjaxMinibagRefresh(returnObj) {
	$('#minibagOuter').html(returnObj);
	$('#qtip-0').width(calculateWidth());
}

/* -------------------------------- Utility Functions -------------------------*/

/**
 * Utility function for submitting AJAX requests. 
 *
 * options = { url : url of AJAX request, 
 *	 handler: function to handle AJAX callback, 
 *	 formId: optiiona, used to sumbit form data along with AJAX request, 
 *	 dataType: data type for expected data, defualts to JSON
 * }
*/
function submitMinibagAjax(options) {
	var defaults = {dataType: 'json'};
	var options = $.extend({}, defaults, options); 
	
	performAjaxRequest(options.url,null,null,options.dataType,null,
		options.handler,null,'POST',null);
}

