/*
Sony Style Login/Regisration page Javascript
AUTHOR: Jonathan Cheung
===================================================================================================================================================================
// !  * Registration page javascript logic.
* 	PRE-CONDITIONS:
*
* 	Required JS files: prototype.js, validation.js, ss_core_omniture.js
*
// ===================================================================================================================================================================
*/
jQuery.noConflict();
Event.observe(window, 'load', function (){
	$$('a.popupIconButton').each(function(e){
		Event.observe($(e), 'mouseover', function(e){
			$($(this).readAttribute('href').substring(1)).addClassName('show');
			if (Prototype.Browser.IE)
				$$('select').each(function(e){e.hide()});
		});
		Event.observe($(e), 'mouseout', function(e){
			if (
				(Position.within(Event.element(e), Event.pointerX(e), Event.pointerY(e))  == false)
			){
				$($(this).readAttribute('href').substring(1)).removeClassName('show');
			if (Prototype.Browser.IE)
				$$('select').each(function(e){e.show()});
			}
		});
	});

	/** BEGIN COLOR COLLAPSE STUFF **/

	// Initialize our Vanity and Model Color:

	ourDefaultSKU = $$('.colorChipLink.activeChip').invoke('readAttribute', 'id');
	if ($(ourDefaultSKU[0]))
	{
	ourCurrentVanityName = $(ourDefaultSKU[0]).innerHTML;

	productDetailColorCollapse(ourDefaultSKU[0]);
	}

	$$('a.colorChipLink').invoke('observe', 'mouseover', function(evt){

		$('currentVanityName').innerHTML = $(this).innerHTML;

	});// end ColorChip Hover Event Listener

	$$('a.colorChipLink').invoke('observe', 'mouseout', function(evt){

		$('currentVanityName').innerHTML = ourCurrentVanityName;

	});// end ColorChip MouseOut Event Listener


	/*  COLOR COLLAPSE - NON FLASH CONTENT EVENT LISTENER:								   !!! READ ME !!!
	 *	--------------------------------------------------------------------------------------------------
	 */
	$$('a.colorChipLink').invoke('observe', 'click', function(evt){


		// We need to highlight the chip we want to outline while removing the outline on the previous active chip
		$$('a.colorChipLink').invoke('removeClassName', 'activeChip');
		$(this).addClassName('activeChip');

		ourSKU = $(this).readAttribute('id');

		productDetailColorCollapse(ourSKU);
		setProductDisplayUrl(ourSKU);


	});//end colorChipLink Event Listener

	jQuery(".jqzoom").jqzoom({
		zoomWidth: 186,
		zoomHeight: 185,
		xOffset: 0,
		yOffset: -1,
		position: "right",
		showPreload: true,
		title: false,
		lens:true
	});




	// initiate wishlist links
	initAddToWishListAjax();

	$$('.wishlistLink').each(function(e){
		Event.observe(e, 'click', function(evt){

			var element = Event.element(evt)
			var elementId = element.id;
			elementId = elementId.replace('wishlist_','');

			addItemToWishlist(element);
			ajaxEngine.sendRequest('SYAddToWishListAjax', {method:'post', parameters: getAddToWishListAjaxParams(elementId)});

			Event.stop(evt);
		});
	});


	// initiate pricing
	if (isUserInEpp())
	{
		initPricingAjax();

		var counter = 0;
		var priceArray = new Array();
		$$('.priceFinancingGroup').each(function(element){

				var elementId = element.id;
				elementId = elementId.replace('SKU_','');
				elementId = elementId.replace('PriceAndFinancingOptions','');

				priceArray.push("catEntryId_" + counter + "=" + elementId);

				++counter;
		});

		if (counter > 0)
		{
			var productIdsForPrice = priceArray.join("&");
			ajaxEngine.sendRequest('SYGetContractUnitPrices', {method:'post', parameters: productIdsForPrice + "&" + baseUrlParameters});
		}
	}

		// initiate product buttons
	initProductButtonsAjax();

	var counter = 0;
	var productArray = new Array();
	$$('.CTAButton').each(function(element){

         if(!element.hasClassName('CTACTOButton')){
			var elementId = element.id;
			elementId = elementId.replace('SKU_','');
			elementId = elementId.replace('CTAButton','');

			productArray.push("catEntryId_" + counter + "=" + elementId);

			++counter;
			}
	});
	if (counter > 0)
	{
		var productIdsForButtons = productArray.join("&");
		ajaxEngine.sendRequest('SYGetProductButtonMessageFromCacheAjax', {method:'post', parameters: productIdsForButtons + "&URL=SYGetSKUButtonMessageAjaxResponseView&" + baseUrlParameters});
	}


		// ADD TO CART AND WISHLIST OMNITURE ADDITIONS
		$$('.ctaGrouping .produtDetailCTALink').each(function(addToCart, index){
			// GO THROUGH EACH SKU'S ADD TO CART AND WISHLIST
			if(addToCart && addToCart.tagName=='A'){
				Event.observe(addToCart, 'click', function(e){
					trackAddToShoppingCart($$('.productSku')[index].innerHTML);
					trackSearchAddToCart('Search Product Results: ' + $$('.productSku')[index].innerHTML);
				});
			}
		});

		$$('.ctaGrouping .wishlistLink').each(function(addToWishlist, index){
			// GO THROUGH EACH SKU'S ADD TO CART AND WISHLIST
			if(addToWishlist && addToWishlist.tagName=='A'){
				Event.observe(addToWishlist, 'click', function(e){
					trackAddToWishList($$('.productSku')[index].innerHTML);
				});
			}
		});


});

function changeQVThumbnailImage(thumbnail, zoomedImage){
	var currentSrc = thumbnail.firstChild.src;
	var newSrc = currentSrc.replace("31x31", "218x144");
	document.getElementById('quickViewMainImageSpot').src = newSrc;
	document.getElementById('jqMainImage').href=zoomedImage;
}

/*
	Setup markup for Price section. May be different for each page.
*/
function displayPriceSection(productId, priceObject)
{
	var origPrice = priceObject.orgPrice;
	var price = priceObject.price;
	var finance = priceObject.finance;

	var newHtml = '';

	var element = $('SKU_' + productId + 'PriceAndFinancingOptions');
	if (element)
	{
		if (origPrice)
		{
			var dollarIdx = origPrice.indexOf("$");
			var decimalIdx = origPrice.indexOf(".");

			newHtml += '<div class="originalPricing">';
			newHtml += '<p class="productPrice">';
			newHtml += '<span class="strikeThrough">';
			newHtml += '<span class="productCurrency">$</span>';
			newHtml += '<span class="productDollars">';
			newHtml += origPrice.substring(dollarIdx+1,decimalIdx);
			newHtml += '</span>';
			newHtml += '<span class="productCents">';
			newHtml += origPrice.substr(decimalIdx);
			newHtml += '</span>';
			newHtml += '<span class="clearfix"></span>';
			newHtml += '</span>';
			newHtml += '<span class="clearfix"></span>';
			newHtml += '</p>';
			newHtml += '</div>';
		}

		if (price)
		{
			var dollarIdx = price.indexOf("$");
			var decimalIdx = price.indexOf(".");

			newHtml += '<p class="productPrice">';
			newHtml += '<span class="productCurrency">$</span>';
			newHtml += '<span class="productDollars">'
			newHtml += price.substring(dollarIdx+1,decimalIdx);
			newHtml += '</span>';
			newHtml += '<span class="productCents">'
			newHtml += price.substr(decimalIdx);
			newHtml += '</span></p>';
		}
		if (finance)
		{
			newHtml += '<p class="productFinancing">';
			newHtml += finance;
			newHtml += '</p>';
			/*
			var dollarIdx = finance.indexOf("$");
			var slashIdx = finance.indexOf("/");

			newHtml += '<p class="productFinancing">As low as ';
			newHtml += '<span class="monthlyRate">';
			newHtml += finance.substring(dollarIdx,slashIdx);
			newHtml += '</span>/month**</p>';
			*/
		}

		element.innerHTML = newHtml;
	}
}

function displayProductButtonSection(productId, buttonObject)
{
	var newHtml = '';
	var buttonType = buttonObject.buttonType;
	var element = $('SKU_' + productId + 'CTAButton');
	var elementPrice = $('SKU_' + productId + 'PriceAndFinancingOptions');
	
	if (element)
	{
		if (buttonType == 'end_of_life')
		{
			element.removeClassName('CTAButton');
			elementPrice.innerHTML = "No Longer Available";
			if(!elementPrice.hasClassName('prodAvailibilityMsg'))
				elementPrice.addClassName('prodAvailibilityMsg');
			if($('productDisplayHrefViewNow')) $('productDisplayHrefViewNow').href=$('productDisplayHrefViewNow').readAttribute('data-accessory-href');
				$('productDisplayHrefViewNow').innerHTML="FIND ACCESSORIES";
			
		}else if (element.hasClassName('productDetailPrimaryCTA'))
		{
			var anchorElement =  $('SKU_' + productId + 'ProductDetailCTAText');

			if (anchorElement)
			{
				var urlLink = buttonObject.buttonLink;
				if (urlLink == "")
				{
					urlLink="javascript:void(0);";
				}
				anchorElement.href = urlLink;
				anchorElement.innerHTML = buttonObject.buttonText;
			}
			/*
			newHtml += '<a href="';
			newHtml += urlLink;
			newHtml += '"';
			newHtml += 'class="produtDetailCTALink" rel="">';
			newHtml +=
			newHtml += '</a>';
			*/
		}

		//element.innerHTML = newHtml;
	}

	newHtml = '';
	var inventoryElement = $('SKU_' + productId + 'InventoryStatus');
	if (inventoryElement)
	{
		if (inventoryElement.hasClassName('inventoryStatusGroup'))
		{
			newHtml += '<p id="productInventoryStatus">';
			newHtml += buttonObject.statusMessage;
			newHtml += '</p>';
			newHtml += '<p id="estimatedShipDate">';
			newHtml += buttonObject.shipDateMessage;
			newHtml += '</p>';
		}

		inventoryElement.innerHTML = newHtml;
	}

}

function setProductDisplayUrl(ourSKU)
{
if(ourSKU && ($('productDisplayHrefViewNow') || $('productDisplayHrefDescription')))
	{
		var catId=ourSKU.replace('SKU_','');
		var purl=$('productDisplayHrefViewNow').readAttribute('product-display-href');

		var string='';
		if(purl.indexOf('productId=')!=-1){
           string='productId=';
		}else if(purl.indexOf('categoryId=')!=-1){
			string='categoryId=';
		}else if(purl.indexOf('LBomId=')!=-1){
			string='LBomId=';
		}
		var start=purl.indexOf(string);
		if(start!=-1){
			var end=purl.indexOf('&',start);
			if(end==-1) end=purl.length;
			purl=purl.replace(purl.substring(start,end),string+catId);
		}

		if($('productDisplayHrefViewNow')) $('productDisplayHrefViewNow').href=purl;
		if($('productDisplayHrefDescription')) $('productDisplayHrefDescription').href=purl;
		$('productDisplayHrefViewNow').innerHTML="VIEW DETAILS";

		var element = $('SKU_' + catId + 'PriceAndFinancingOptions');
		if(element.hasClassName('prodAvailibilityMsg')){
			purl=$('productDisplayHrefViewNow').readAttribute('data-accessory-href');
			string='';
			if(purl.indexOf('productId=')!=-1){
	           string='productId=';
			}else if(purl.indexOf('categoryId=')!=-1){
				string='categoryId=';
			}else if(purl.indexOf('LBomId=')!=-1){
				string='LBomId=';
			}
			start=purl.indexOf(string);
			if(start!=-1){
				end=purl.indexOf('&',start);
				if(end==-1) end=purl.length;
				purl=purl.replace(purl.substring(start,end),string+catId);
			}
	
			if($('productDisplayHrefViewNow')) $('productDisplayHrefViewNow').href=purl;
			$('productDisplayHrefViewNow').innerHTML="FIND ACCESSORIES";
		}
	}
}
