$(document).ready(function() {
	// initiliase:
	initialiseAll();
});

function productDropped(droppedWhere, productObject) {
//alert(droppedWhere);
	// called when a draggable is dropped over a suitable target
	thisProductsId     = productObject.attr("id");
	thisProductsPrefix = thisProductsId.substr(0, 3);
//alert(thisProductsId);
//alert(droppedWhere);

	// DO NOTHING IF DRAGGED FROM CHANGING ROOM AND DROPPED BACK TO CHANGING ROOM
	if(droppedWhere == "changing" && thisProductsPrefix == "chr") { return false; }
	if(droppedWhere == "mixmatch" && thisProductsPrefix == "mxm") { return false; }

	pv_id = thisProductsId.substr(3);

	// REMOVE FROM CHANGING ROOM
	if(thisProductsPrefix == "mxm" && droppedWhere == "outside") {
//		$(productObject).hide();
		removeFromMixMatch(pv_id);
		return false;
	}
	// ADD TO MIX'N'MATCH MANEQUIN
	if(droppedWhere == "mixmatch") {
//		$(productObject).hide();
		$("#changingRmWorking").show();
		//addToChangingRoom(pv_id);
		addToMixMatch(pv_id);
		return false;
	}
	// REMOVE FROM CHANGING ROOM
	if(thisProductsPrefix=="chr" && droppedWhere!="changing" && droppedWhere!="mixmatch") {
//		$(productObject).hide();
		removeFromChangingRoom(pv_id);
		return false;
	}
	// ADD TO CHANGING ROOM
	if(droppedWhere == "changing") {
//		$(productObject).hide();
		$("#changingRmWorking").show();
		addToChangingRoom(pv_id);
		return false;
	}
	// ADD TO BASKET + create modal to check size required:
	if(droppedWhere == "basket") {
		eval("thisProductName = window.document.getElementById('prodname_" + pv_id + "').value;");
		// list of available sizes to choose from
		eval("thisProductSizesList = window.document.getElementById('prodsizelist_" + pv_id + "').value;");
		thisFullSizeList = thisProductSizesList.split('__');
		thisFullSizeList.push("(cancel)");
		$(productObject).hide();
		
		Boxy.ask("Please choose which size you'd like of \"" + thisProductName + "\"", thisFullSizeList, 
			function(r) {
				if (r != "(cancel)") {
					addToBasket(pv_id, r);
				} else {
					// would be nice if the product animated as it reverted back to carousel
					// http://groups.google.com/group/jquery-ui/browse_thread/thread/d25b97f71e4ef956/
				}
			},
			{ title: "which size?" }
		);
	}
//	initialiseAll();
	return false;
}

// BASKET FUNCTIONALITY
function addToBasket(pv_id, size) {
	qty = 1;
	bsk_msg = '';
	$.ajax({
		type: 'GET',
		url: 'ajax-actions?action=add_to_basket&pv_id=' + pv_id + '&qty=' + qty + '&size=' + size,
		success: function(bsk_msg) {
			if(bsk_msg != '') {
				reloadBasketSummary();
				reloadBasketHeaderSummary();
			}
		}
	});
	return false;
}

function reloadBasketSummary() {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=basket_summary',
		success: function(bsk_msg){
			$('#basketPanel').html(bsk_msg);
		}
	});
	return false;
}

function reloadBasketHeaderSummary() {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=basket_header_summary',
		success: function(bsk_header_msg){
			$('#headerBasket').html(bsk_header_msg);
			$('#headerBasket').addClass('basketVisible');
		}
	});
	return false;
}

// CHANGING ROOM FUNCTIONALITY
function addToChangingRoom(pv_id) {
	chr_msg = '';
	$.ajax({
		type: 'GET',
		url: 'ajax-actions?action=add_to_changing_room&pv_id=' + pv_id, 
		success: function(atchr_msg) {
//alert("addToChangingRoom: "+atchr_msg);
			if(atchr_msg != '') {
				reloadChangingRoomList();
			}
			$("#changingRmWorking").hide();
		}
	});
	return false;
}

function removeFromChangingRoom(pv_id) {
	$.ajax({
		type: 'GET',
		url: 'ajax-actions?action=remove_from_changing_room&pv_id=' + pv_id, 
		success: function(rfchr_msg) {
//alert("removeFromChangingRoom: "+rfchr_msg);
			if(rfchr_msg != '') {
				removeFromMixMatch(pv_id);
				reloadChangingRoomList();
			}
		}
	});
	return false;
}

function reloadChangingRoomList() {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=changing_room_summary',
		success: function(rchr_msg){
//alert("reloadChangingRoomList: "+rchr_msg);
			// send up-to-date content to changing room
			$('#changingPanelContents').html(rchr_msg);
			// re-initialise the scroll bar:
			$('#allProductsAdded').jScrollPane({
				showArrows: true, 
				scrollbarWidth: 16, 
				arrowSize: 18, 
				containerWidth: 97, 
				containerHeight: 250
			});
			// this tooltip needs re-initialising now:
			$('#changingRoomHelp').pluginTooltip({
				track: true,
				delay: 0,
				showURL: false,
				fixPNG: true,
				extraClass: "smallToolTip",
				left: 22,
				top: -10
			});
			reloadShoppingAssistant();
		}
	});
	return false;
}

function addToMixMatch(pv_id) {
	mxm_msg = '';
	$.ajax({
		type: 'GET',
		url: 'ajax-actions?action=add_to_mix_match&pv_id=' + pv_id, 
		success: function(mxm_msg) {
//alert("addToMixMatch: "+mxm_msg);
			if(mxm_msg != '') {
				reloadMixMatchList();
				reloadChangingRoomList();
			}
			$("#changingRmWorking").hide();
		}
	});
	return false;
}

function removeFromMixMatch(pv_id) {
	mxm_msg = '';
//	$('#mxm'+pv_id).hide();
	$.ajax({
		type: 'GET',
		url: 'ajax-actions?action=remove_from_mix_match&pv_id=' + pv_id, 
		success: function(rfmm_msg) {
//alert("removeFromMixMatch: "+rfmm_msg);
			//$('#mxm'+pv_id).hide();
			if(rfmm_msg != '') {
				reloadMixMatchList();
			}
		}
	});
	return false;
}

function reloadMixMatchList() {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=changing_room_mixmatch_summary',
		success: function(rmm_msg){
//alert("reloadMixMatchList: "+rmm_msg);
			// send up-to-date content to changing room
			$('#mixMatch').html(rmm_msg);
		}
	});
	return false;
}

// SHOPPING ASSISTANT FUNCTIONALITY
function reloadShoppingAssistant() {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=shopping_assistant_summary',
		success: function(rsa_msg){
			$('#shoppingAssistantContent').html(rsa_msg);
		}
	});
	return false;
}

// PRODUCT ZOOM FUNCTIONALITY
function reloadProductPictureZoom(prod_id) {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=product_picture_zoom&pv_id=' + prod_id,
		success: function(ppz_msg){
			$('#zoomPhoto'+prod_id).html(ppz_msg);
			//$("#zoom"+prod_id).remove();
			$("#zoom"+prod_id).jqzoom();
		}
	});
	return false;
}

// HORIZONTAL FILTERS FUNCTIONALITY
function reloadHorizontalFilters(cntxt) {
	var obj;
	obj = document.getElementById('horizontalFilterBrands');
	brand_id = obj.options[obj.selectedIndex].value;
	obj = document.getElementById('horizontalFilterCategories');
	cat_id = obj.options[obj.selectedIndex].value;
	obj = document.getElementById('horizontalFilterColours');
	colour_id = obj.options[obj.selectedIndex].value;
	obj = document.getElementById('horizontalFilterSizes');
	size_id = obj.options[obj.selectedIndex].value;

	add_url = '';
	price_order = "asc";
	if(cntxt == "desc") {
		price_order = cntxt;
		add_url = '&horizontalFilterOrderPrice=' +price_order;
	}
	add_url_1 = '';
	gender = "asc";
	if(cntxt == "male" || cntxt == "female") {
		gender = cntxt;
		add_url_1 = '&horizontalFilterGender=' +gender;
	}

	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=reload_horizontal_filter&&horizontalFilterBrands=' +brand_id+ '&horizontalFilterCategories=' +cat_id+ '&horizontalFilterColours=' +colour_id+ '&horizontalFilterSizes=' +size_id+add_url+add_url_1,
		success: function(rhf_msg){
			$('#horizontalFiltersWrapper').html(rhf_msg);
			reloadCarouselContent(brand_id, cat_id, colour_id, size_id, price_order, gender);
		}
	});
	return false;
}

// CAROUSEL BACKGROUND FUNCTIONALITY
function reloadCarouselContent(brand_id, cat_id, colour_id, size_id, price_order, gender) {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=carousel_content&brand_id=' +brand_id+ '&cat_id=' +cat_id+ '&colour_id=' +colour_id+ '&size_id=' +size_id+ '&price_order=' +price_order+ '&gender=' +gender,
		success: function(rcc_msg){
			$('#carouselWrapper').html(rcc_msg);
			instanceOne = new ImageFlow();
			instanceOne.init({ ImageFlowID:'dropShopCarousel' });
		}
	});

	return false;
}

function initialiseAll() {
	// set changing room to be droppable
	$("#changingPanelContents").droppable({
		activeClass: 'ui-state-highlight',
		drop: function(event, ui) {
			productDropped("changing", ui.draggable);
			// dropped object is referenced by ui.draggable
			// this object is referenced by $(this)
			// eg $(this).addClass('ui-state-highlight').find('p').html('Dropped!');
		}
	});
	// set manequin area to be droppable
	$("#mixMatch").droppable({
		activeClass: 'ui-state-highlight',
		drop: function(event, ui) {
			productDropped("mixmatch", ui.draggable);
		}
	});
	// set basket to be droppable
	$("#basketPanel").droppable({
		activeClass: 'ui-state-highlight',
		drop: function(event, ui) {
			productDropped("basket", ui.draggable);
		}
	});
	// set up modal boxes:
/*
	$('.modalpopup').boxy({
		title: "Product Details",
		clickToFront: true,
		closeable: true
	});
*/
	// add tooltip behaviours:
	$('.helpPrompt').pluginTooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		extraClass: "smallToolTip", 
		left: 22,
		top: -10
	});
	$('.imageWithTooltip').pluginTooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		showBody: " | ",
		left: 12,
		top: -30
	});
	$("#primaryBestSeller").draggable({ 
		revert: true, 
		containment: '#wrapper', 
		distance: 20
	});
	// style the scrollbar in the changing room:
	$('#allProductsAdded').jScrollPane({showArrows: true, scrollbarWidth: 16, arrowSize: 18, containerWidth: 97, containerHeight: 250});
	// hide ajax progress displays:
	$('.ajaxWorking').hide();
	// set up defaults:
	modalPopupMode = "zoom";
}