/* The index of the array is the PF PID. The value is the Wine.com PID */

var wineProductsBaseURL = "http://www.wine.com/v6/giftcenter/proflowersproduct.aspx?product_id=";
var wineProductsNoProdURL = "http://www.wine.com/v6/giftcenter/proflowers.aspx";
var RedEwineProductsBaseURL = "http://www.wine.com/v6/giftcenter/Red-Envelope-Wine-Gifts-Product.aspx?product_id=";
var RedEwineProductsNoProdURL = "http://www.wine.com/v6/giftcenter/Red-Envelope-Wine-Gifts.aspx";
var wineProductsRefCode = "&S=";
var wineProductsRefCodeNoProd = "?S=";
var wineProductsCatID = "&CID=";
var wineProductsOmRef = "&pvref=";
var wineProductsOmCat = "&pvcatid=";
var wineProductsOmBrand = "&pvcobrand=";
var wineProductsOmPid = "&pvpid=";
var wineProductsOmSort = "&pvsort=";

var gcQueryPartner = "&p=";
var gcQueryLoc = "&l=";
var gcQueryRef = "&r=";
var gcQueryCat = "&c=";
var giftCardMagicNumber = "$10.00";

var floristExpressBaseURL = "http://www.floristexpress.net/products/"

var giftCardProducts = [];
/* Testing example below for gift cards. */
/*
giftCardProducts[41648] = "41648";
giftCardProducts[40805] = "40805"; 
giftCardProducts[19973857] = "19973857";
*/


var wineProductsArray = [];
/* Testing example below for wine products. */

/*
wineProductsArray[30000122] = "96526";
wineProductsArray[40735] = "93459";
wineProductsArray[21322193] = "93459";
*/

/* Main set of productIDs */
wineProductsArray[30000036] = "96542";
wineProductsArray[30000083] = "96537";
wineProductsArray[30007268] = "96528";
wineProductsArray[30000037] = "96529";
wineProductsArray[30000039] = "96533";
wineProductsArray[30000085] = "96543";
wineProductsArray[30000040] = "96541";
wineProductsArray[30000087] = "96538";
wineProductsArray[30000041] = "96530";
wineProductsArray[30000042] = "96527";
wineProductsArray[30000043] = "96539";
wineProductsArray[30000090] = "96545";
wineProductsArray[30000044] = "96534";
wineProductsArray[30000045] = "96531";
wineProductsArray[30000088] = "96536";
wineProductsArray[30000046] = "96526";
wineProductsArray[30000058] = "96532";
wineProductsArray[30000089] = "96535";
wineProductsArray[30002080] = "97564";
wineProductsArray[30002258] = "97735";
wineProductsArray[30002259] = "97736";
wineProductsArray[30004358] = "99883";
wineProductsArray[30004359] = "99883";
wineProductsArray[30003686] = "98999";
wineProductsArray[30005499] = "100144";
wineProductsArray[30005500] = "100144";

wineProductsArray[30043908] = "107072";
wineProductsArray[30043909] = "107073";
wineProductsArray[30043912] = "107074";
wineProductsArray[30043913] = "107075";
wineProductsArray[30043915] = "107076";
wineProductsArray[30043910] = "107077";
wineProductsArray[30043916] = "107078";
wineProductsArray[30043911] = "107079";

wineProductsArray[30058344] = "111984";
wineProductsArray[30058184] = "111632";
wineProductsArray[30058087] = "111638";
wineProductsArray[30059136] = "112199";
wineProductsArray[30059915] = "112705";
wineProductsArray[30059916] = "112704";
wineProductsArray[30059917] = "112708";
wineProductsArray[30059918] = "112707";
wineProductsArray[30059912] = "112709";

var floristExpressProductsArray = [];

/* Testing example below for florist express. */
/*JUN SiteTests -PIR 18625 FLE products on PF- SM */

floristExpressProductsArray[30003819] = "1_dozen_long_stemmed_red_roses.htm?" + "refcode=" + getGroup() + "&pfc=1";

/* mother's day 2010 test - RD */

floristExpressProductsArray[30008774] = "one_dozen_red_roses.htm?pfc=1&refcode=MGP";

/* mother's day 2011 */
floristExpressProductsArray[30052846] = "birthday_brilliance.htm?pfc=1&refcode=BIR";
floristExpressProductsArray[30052847] = "a_beary_happy_birthday.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052848] = "happy_birthday_garden.htm?pfc=1&refcode=BIR";
floristExpressProductsArray[30052877] = "lavender_rose_tulip__hydrangea_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052878] = "green_envy_armband_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052879] = "rosebud_elite_boutonniere.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052880] = "orchids_galore_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052881] = "orchid_frenzy_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052883] = "loves_on_fire_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052885] = "loves_flowing_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052886] = "orange_alstroemeria_boutonniere.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052887] = "red_dazzle_clutch_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052888] = "orange_rose_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052889] = "orchids_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052890] = "gerbera_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052891] = "sweetest_love_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052892] = "forever_yours_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052893] = "blushing_beauty_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052894] = "romantic_rose_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052895] = "romantic_garden_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052896] = "modern_boutonnierre.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052899] = "rockin_red_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052900] = "rose_rhapsody_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052914] = "roses_n_wires_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052917] = "purple_reigns_shoulder_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052919] = "studded_carnation_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052920] = "purple_reigns_wrist_corsage.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052922] = "burished_calla_boutonniere.htm?pfc=1&refcode=FEC";
floristExpressProductsArray[30052845] = "forever_missing_you.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052923] = "in_his_honor_pedestal_arrangement.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052924] = "eternal_paradise.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052926] = "her_garden_of_glory.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052927] = "in_remembrance.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052929] = "eternal_garden_basket.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052931] = "crossing_over_lid_decoration.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052932] = "always_remembered_garden_basket.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052933] = "heaven_on_earth.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052934] = "a_beautiful_tribute_casket_spray.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052936] = "a_tribute_heart.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052938] = "classic_white_bouquet.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052940] = "green__white_spray.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052942] = "personal_tribute_basket.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052943] = "sympathy_basket.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30052944] = "fruits__flowers.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052948] = "fruit_sweets_and_treats_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052949] = "deluxe_all_fruit_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052950] = "fresh_fruit_and_candy_gift_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052951] = "afternoon_delights_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052953] = "fruit__flower_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052954] = "fruit__kalanchoe_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052955] = "party_on_gourmet_basket.htm?pfc=1&refcode=PGB";
floristExpressProductsArray[30052956] = "garden_of_love.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30052958] = "loves_burning_bright.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30052959] = "ode_to_orchids.htm?pfc=1&refcode=POE";
floristExpressProductsArray[30052960] = "oriental_dreams.htm?pfc=1&refcode=POE";
floristExpressProductsArray[30052961] = "pure_perfection.htm?pfc=1&refcode=POE";
floristExpressProductsArray[30052962] = "contemporary_anthurium_garden.htm?pfc=1&refcode=POE";
floristExpressProductsArray[30052963] = "cyclamen_surprise.htm?pfc=1&refcode=PPL";
floristExpressProductsArray[30052964] = "plant_paradise.htm?pfc=1&refcode=PPL";
floristExpressProductsArray[30052965] = "get_up_and_grow.htm?pfc=1&refcode=PPL";
floristExpressProductsArray[30052966] = "vibrant_violets.htm?pfc=1&refcode=PPL";
floristExpressProductsArray[30052830] = "a_sprinkling_of_springtime_-_gerbera_daisy.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052831] = "lily_la_rose_2.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052967] = "salute_to_spring.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052968] = "daffodil_delight.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052969] = "bearing_my_heart.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052970] = "lily_dreams.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052971] = "youre_the_best.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052947] = "opulent_orange_roses.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052946] = "thinking_of_you.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052945] = "iris_fantasy.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052941] = "perfect_in_purple.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052939] = "simply_succulents.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052937] = "amazing_azalea.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052935] = "sweet_serenade.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052928] = "rose__lily_cube_bouquet.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052915] = "plants_a-plenty.htm?pfc=1&refcode=SDY";
floristExpressProductsArray[30052925] = "roses_n_rapture.htm?pfc=1&refcode=PVD";

/*Vday 2010*/
floristExpressProductsArray[30048597] = "green__pink_arrangement.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048598] = "an_exceptional_life_standing_spray.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048599] = "expression_of_sympathy.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048600] = "eternal_memories.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048602] = "memorable_arrangement.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048603] = "paying_tribute.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048604] = "in_comfort_and_peace.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048606] = "standing_cross.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048607] = "memorable_tribute.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048608] = "honor_a_memory_arrangement.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048609] = "pink__purple_expressions.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048610] = "eternal_grace.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048611] = "yellow__lavender_sympathy_vase.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048612] = "white_daisy_basket.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048613] = "yellow_rose__alstroemeria_pillow_cross.htm?pfc=1&refcode=FNR";
floristExpressProductsArray[30048614] = "classic_bud_vase_roses.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30048616] = "garden_of_love_2.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30048618] = "cheery__bright.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30048619] = "with_many_thanks.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30048621] = "springtime_surprise.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30048622] = "joyful_bloom.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30048623] = "blushing_beauty_2.htm?pfc=1&refcode=PMD";
floristExpressProductsArray[30048624] = "its_your_happy_birthday_cake.htm?pfc=1&refcode=PBD";
floristExpressProductsArray[30048625] = "its_your_birthday.htm?pfc=1&refcode=PBD";
floristExpressProductsArray[30048626] = "snappy_happy_birthday.htm?pfc=1&refcode=PBD";

floristExpressProductsArray[30052930] = "my_heart_is_yours.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30052957] = "sweet_delights.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30048615] = "sheer_pink_delight.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30048617] = "rhapsody_in_red.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30062985] = "captivating_kalanchoe.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064286] = "12_dozen_premium_red_roses.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064285] = "Radiant_Red_Tulips.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064284] = "Radiant_Peruvian_Lilies.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064283] = "Love_Bouquet_2.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064282] = "1_dozen_medium_stem_mixed_roses.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064281] = "bearing_my_heart.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064280] = "Rose_Elegance_Premium_Dozen_Roses-Red.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064279] = "European_Romance_Bouquet.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064278] = "Youre_In_My_Heart_2.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064277] = "Rose_Garden_Bouquet.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064276] = "Youre_In_My_Heart_3.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064275] = "Multi-color_Tulip_Bouquet.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064274] = "Classic_Bud_Vase_Roses_2.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064273] = "Ever-Budding_Romance_2.htm?pfc=1&refcode=PVD";
floristExpressProductsArray[30064272] = "My_Rosy_Valentine_2.htm?pfc=1&refcode=PVD";

var prevPID;

var brand = getBrand(location);
var group = getGroup();
var ref = (GetQueryStringParam("ref") == false) ? "NoRef" : GetQueryStringParam("ref");
var loc = "ProdLink";

function findExternalProducts() {
	if (document.getElementsByTagName) {
		var links = document.getElementsByTagName("a");
		var numFound = 0;
		var productPID;
		var numLinks = links.length;
		for (var i = 0; i < numLinks; i++) {
			productPID = getPID(links[i].href);

			if (productPID != "") {
				if (!doWineProducts(productPID, links[i])) {
					if (!doGiftCardProducts(productPID, links[i])) {
						doFloristExpressProducts(productPID, links[i]);
					}
				}
			}
		}
	}
}

function doWineProducts(productPID, link) {
	if (wineProductsArray[productPID]) {
		if (brand == "RED") {
			wineProductsBaseURL = RedEwineProductsBaseURL;
		}

		var pos = getViewPos(link);
		link.href = wineProductsBaseURL + wineProductsArray[productPID] + wineProductsRefCode + brand + "_" + loc + "_" + group + "_" + ref + wineProductsCatID + brand +
						"_" + loc + "_" + group + "_" + ref + wineProductsOmRef + ref + wineProductsOmCat + group + wineProductsOmBrand + brand + wineProductsOmSort + pos + wineProductsOmPid + productPID;
		
		return true;
	}
}

function doGiftCardProducts(productPID, link) {
	var result = false;

	/* Check for gift card products. */
	if (giftCardProducts[productPID]) {
		result = true;

		/* Helps us track where we are. */
		if (prevPID == "" | prevPID != productPID) {
			prevPID = productPID;
			numFound = 0;
		}
		numFound++;
		if (brand.toUpperCase().match("RED") != null) {
			link.href = redEGiftCardsBaseURL;
		}
		else {
			link.href = giftCardsBaseURL;
		}

		link.target = "_blank";
		link.href += gcQueryPartner + brand + gcQueryLoc + loc + gcQueryCat + group + gcQueryRef + ref;

		if (numFound == 1) {
			/* Adjust strikethrough pricing styles - getElement method. */

			eltemp = link.nextSibling;

			if (eltemp != null) {
				/* Adjust strikethrough pricing styles - navigation method. */
				el = link.nextSibling.nextSibling;

				if (el != null && el.hasChildNodes) {
					el = el.firstChild;
				}

				if (el != null) {
					if (el.innerHTML == giftCardMagicNumber) {
						el.className = 'productPrice';
						el.style.textDecoration = 'none';
						el.innerHTML = el.innerHTML + " - ";
					}
				}
			}

		}
	}

	return result;
}

function doFloristExpressProducts(productPID, link) {
	if (floristExpressProductsArray[productPID]) {
		link.href = floristExpressBaseURL + floristExpressProductsArray[productPID];
		return true;

	}
}

function getViewPos(url) {
	name = "viewpos";
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(url);
	if (results == null)
		return "";
	else
		return results[1];
}

function getPID(url) {
	var regExPattern = /-\d+\?/; /* find one or more occurences of a digit [ \d+ ] prefixed by [ - ] and suffixed by [ \? ] */

	var result = url.match(regExPattern);
	if (result != null) {
		var returnValue = result[0];
		returnValue = returnValue.replace("-", "");
		returnValue = returnValue.replace("?", "");

		if (parseInt(returnValue)) {
			return returnValue;
		}
	}
	else {
		if (GetQueryStringParam("pid") != false)
			return GetQueryStringParam("pid");
	}
	return ("");
}

function getBrand(url) {
	loc = url.hostname;

	if (loc.toUpperCase().match("REDENVELOPE") != null) {
		return "RED";
	}
	else if (loc.toUpperCase().match("PROFLOWERS") != null) {
		return "PFC";
	}
	else if (loc.toUpperCase().match("CHERRYMOONFARMS") != null) {
		return "CMF";
	}
	else if (loc.toUpperCase().match("SECRESTSPOON") != null) {
		return "SSS";
	}

	return "NoBrand";
}

function getGroup() {
	var grp = (location.pathname.lastIndexOf("-") != -1) ? location.pathname.substring(location.pathname.lastIndexOf("-") + 1, location.pathname.length) : "NoGroup";
	if (grp == "NoGroup") grp = (GetQueryStringParam("catid") != false) ? GetQueryStringParam("catid") : "ProductSearch";
	if (grp == "NoGroup") grp = (GetQueryStringParam("pageid") != false) ? GetQueryStringParam("pageid") : "NoGroup";
	if (grp != "NoGroup") grp += (GetQueryStringParam("code") != false) ? "_" + GetQueryStringParam("code") : "";
	return grp;
}

function openCardURL(linkLoc) {
	var cardURL = (brand.toUpperCase().match("RED") != null) ? redEGiftCardsBaseURL : giftCardsBaseURL;
	cardURL += gcQueryPartner + brand + gcQueryLoc + linkLoc + gcQueryCat + group + gcQueryRef + ref;
	if (GetQueryStringParam("test") == "true") alert(cardURL);
	window.open(cardURL, "_blank")
}

function openWineURL(linkLoc) {
	if (brand == "RED") {
		wineProductsNoProdURL = RedEwineProductsNoProdURL;
	}

	var wineURL = wineProductsNoProdURL + wineProductsRefCodeNoProd + brand + "_" + linkLoc + "_" + group + "_" + ref + wineProductsCatID + brand + "_" + linkLoc + "_" +
		group + "_" + ref + wineProductsOmRef + ref + wineProductsOmCat + linkLoc + wineProductsOmBrand + brand + wineProductsOmSort + wineProductsOmPid;
	if (GetQueryStringParam("test") == "true") alert(wineURL);
	window.open(wineURL, "_blank");
}

