﻿$(document).ready(function() {
	//disabled - empty basket not needed
	//$("#EMPTY_BASKET").click(function() {
	//	saveToCookieBasket("RESET", "RESET");
	//	alert("Basket emptied");
	//});

});

//**********************************************************************************
//** get the necessary data from the jQO and does a pretty effect then saves to cookie
function actionAddToBasket(jQO) {
	//save to cookie
	if (!saveToCookieBasket(jQO.attr("alt_ID"), jQO.attr("alt_TYPE"))) {
		// if fails to add do nothing and return
		return;
	}

	imgID = jQO.attr("id") + "_ImgToBasket";

	//get image id that conatins source for effect's image
	imgThumb = jQO.attr("alt_IMG");
	idFromPos = jQO.attr("alt_EL"); //the element the is the FROM position
	
	//removes if found;
	$("#" + imgID).remove()

	var startX = $("#" + idFromPos).offset().left;
	var startY = $("#" + idFromPos).offset().top;

	var stopX = $("[alt_id='dvBrochureTarget']").offset().left;
	var stopY = $("[alt_id='dvBrochureTarget']").offset().top;

	$("body").append("<img id='" + imgID + "' style='border: solid 0px black; background-color:white; z-index: 900; width: 75px; height: 75px; position: absolute; left: " + startX + "; top: " + startY + ";' src='" + imgThumb + "' >");

	//this is needed cause firefox will not except the start position in the style attibute.
	$("#" + imgID).css("left", startX);
	$("#" + imgID).css("top", startY);

	$("#" + imgID).animate({
		"width": "10px",
		"height": "10px",
		"top": stopY,
		"left": stopX
	}, 1000, false, function() { $(this).remove(); });

}

//**********************************************************************************
//** get the necessary data from the jQO and does a pretty effect then saves to cookie
function actionAddToBasket_LightBox() {
	//save to cookie
	//saveToCookieBasket(jQO.attr("alt_ID"), jQO.attr("alt_TYPE"));

	//$("#TB_iframeContent").effect("transfer", { to: "#dvBrochureTarget" }, 350);
	//$(".ui-effects-transfer").css("zindex", "2000");
	jQO = $("#TB_iframeContent");
	divID = jQO.attr("id") + "_ImgToBasket";

	idFromPos = "TB_iframeContent"; //the element the is the FROM position

	//removes if found;
	$("#" + divID).remove()

	var startX = $("#" + idFromPos).offset().left;
	var startY = $("#" + idFromPos).offset().top;

	var stopX = $("#dvBrochureTarget").offset().left;
	var stopY = $("#dvBrochureTarget").offset().top;

	$("body").append("<div id='" + divID + "' style='border: solid 0px black; background-color:white; z-index: 1001; width: 850px; height: 390px; position: absolute; left: " + startX + "; top: " + startY + ";'></div>");

	//this is needed cause firefox will not except the start position in the style attibute.
	$("#" + divID).css("left", startX);
	$("#" + divID).css("top", startY);

	$("#" + divID).animate({
		"width": "10px",
		"height": "10px",
		"top": stopY,
		"left": stopX
	}, 1000, false, function() { $(this).remove(); });

}

function saveToCookieBasket(itemId, itemType, bSupressErrorMsgs) {
	var c_name = "basket";
	var maxItems = getCookieBasketMax();

	if (bSupressErrorMsgs == undefined) { bSupressErrorMsgs == false; };
	
	if (itemId == "RESET") {
		setCookie(c_name, "");
		return true;
	}

	try {
		var item_count = getCookieBasketCount(c_name);

		if (item_count >= maxItems) {
			if (!bSupressErrorMsgs) {
				alert("Maximum of " + maxItems + " items permitted in Brochure Builder.");
			}
			return false;
		}

		//check for dupes
		var new_item = itemId + "|" + itemType;
		var item_list = getCookie(c_name);
		//alert(itemId + "--" + item_list);
		var items = item_list.split(",");
		var i = $.inArray(new_item, items);

		if (i == -1) {
			//rewrite cookie value
			if (item_count == 0 || item_list == "") {
				items_list = new_item;

			} else {
				// append/prepend to array
				items.push(new_item);
				items_list = items.join(",");

			}

			setCookie(c_name, items_list);
			

		} else {
			//do nothing if already there
			//if we ever need a remove function then use array_name.splice(value)

		}

		return true;
		
	}
	catch (err) {
		alert(err);
		return false;
	}

}

//*************************************************
//** This get the number of items allowed for the basket

function getCookieBasketMax() {

	return 10;

}

//*************************************************
//** This get the number of items for the basket from the cookies
function getCookieBasketCount(c_name) {

	c_value = getCookie(c_name);

	if (c_value == '') { return 0; };

	return c_value.split(",").length;

}

//**********************************************************************************
//** NOT TESTED!!!!! get the necessary data from the jQO and does a pretty effect then saves to cookie
function actionAddToBasket_OutlineOnly(jQO) {
	//save to cookie
	saveToCookieBasket(jQO.attr("alt_ID"), jQO.attr("alt_TYPE"));

	olID = jQO.attr("id") + "_OLToBasket";

	//the element the is the FROM position
	idFromPos = jQO.attr("alt_EL");

	//removes if found;
	$("#" + olID).remove()

	var startX = $("#" + idFromPos).offset().left;
	var startY = $("#" + idFromPos).offset().top;
	var startW = $("#" + idFromPos).width;
	var startH = $("#" + idFromPos).height;

	var stopX = $("#dvBrochureTarget").offset().left;
	var stopY = $("#dvBrochureTarget").offset().top;

	var appHTML = "<div id='" + olID + "' "
	appHTML += " style='border: solid 3px #F37521; ";
	appHTML += " z-index: 900; ";
	appHTML += " position: absolute; ";
	appHTML += " left: " + startX + "; ";
	appHTML += " top: " + startY + "; ";
	appHTML += " width: " + startW + "; ";
	appHTML += " height: " + startH + "; ";
	appHTML += " ' >";
	appHTML += " </div>";

	$("body").append(appHTML);

	//this is needed cause firefox will not except the start position in the style attibute.
	$("#" + imgID).css("left", startX);
	$("#" + imgID).css("top", startY);

	$("#" + imgID).animate({
		"width": "10px",
		"height": "10px",
		"top": stopY,
		"left": stopX
	}, 1000, false, function() { $(this).remove(); });

}
