﻿/* Availability Messaging Functions */
function skuSelector1_SelectedIndexChangedHandler(e) {
	var availMsg = (e.get_availabilityMessage() != null && e.get_availabilityMessage() != "") ? e.get_availabilityMessage() : "&nbsp;";
	$get("divAvailability").innerHTML = availMsg;

	var qtyMsg = "&nbsp;";

	var btnAdd = $("#addToCartDiv");
	var availQty = e.get_quantityAvailable();
	if (availQty != null && availQty != "") {
	    if (availQty < 25 && availQty != 0) {
			qtyMsg = "<span class='fewLeft'>Order now – only " + availQty + " left</span>";
		} else {
			qtyMsg = "Plenty in stock";
		}

		//if  out of stock
		if (availQty == 0) {
			//hide add to cart button
		    //btnAdd.hide();
		    qtyMsg = "This item is currently out of stock";
		} else {
			//show add to cart button
			//btnAdd.show();
		}
	} else {
		//hide add to cart button
        //btnAdd.hide();
	}
	$get("spanNumAvail").innerHTML = qtyMsg;


	//see if we can adjust the main image with a sku based image (just incase there sku images like for chairs)
	if (e.get_selectedSku() != null) {
	    var largeImageUrl = getFriendlyImageUrl("assets/images/swatches/") + pfid + "_" + e.get_selectedSku() + "_lg.jpg";
	    $.ajax({ url: largeImageUrl, type: 'HEAD', async: false, success: function (data) {
	        $("#MainImage").attr("src", largeImageUrl);
	    }
	    });
	}
}

function imageAttributeSelector1_SelectedIndexChanged(e) {
    //alert("in here");
    //window.alert(e.get_selectedValue());
    //e.get_attributeLabel()


    if (e.get_selectedControl() != null) {
        //if (e._selectedValue.toString() != "," && e._selectedValue.toString() != "null,null") {
        //$("#" + e.get_selectedControl().get_element().offsetParent.offsetParent.id).parent().parent().parent().find("span#optionAttrLabelValue").html(e._selectedValue.toString().replace("null", "").replace(",", ""));
        //}
        $(e.get_selectedControl().get_element().parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).find("span#optionAttrLabelValue").html("You have selected "+e._selectedValue.toString().replace("null", "").replace(",", ""));
    }
    else {
        //("#" + e.get_selectedControl().get_element().offsetParent.offsetParent.id).parent().parent().parent().find("span#optionAttrLabelValue").html(e._selectedValue.toString().replace("null", "").replace(",", ""));
        //$("#" + e.get_element().id).parent().parent().find("span#optionAttrLabelValue").html("<b>Please Select Below</b>");
        $(e.get_element().parentNode.parentNode).find("span#optionAttrLabelValue").html("Please Select Below");
    }


    var selectedControl = e.get_selectedControl();
    if (selectedControl != null) {
        if (e.get_attributeLabel().toLowerCase() == 'color' || e.get_attributeLabel().toLowerCase() == 'attribute') {
            var smallImageUrl = getFriendlyImageUrl(selectedControl.get_smallImageUrl().replace(/.*assets/i, "assets"));
            var largeImageUrl = getFriendlyImageUrl(selectedControl.get_largeImageUrl().replace(/.*assets/i, "assets"));
            var jumboImageUrl = getFriendlyImageUrl(selectedControl.get_jumboImageUrl().replace(/.*assets/i, "assets"));

            //check to make sure image exists, if it doesnt, don't do anything
            $.ajax({ url: selectedControl.get_largeImageUrl(), type: 'HEAD', async: false, success: function (data) {
                $("#MainImage").attr("src", largeImageUrl);
                //$("#zoom").attr("href", jumboImageUrl);
                /*var parentZoom = $("#zoom").parent();
                $("#zoom").remove();
                var innerZoom = "<a href='" + jumboImageUrl + "' id='zoom'>ENLARGE</a>";
                parentZoom.html(innerZoom);
                $("#zoom").fancyzoom();*/
            }
            });
        }
    }
}

function imageAttributeSelector1_OnMouseOver(e) {

}

/* Swatch Functions */
//this function will cause the main image to swap when a swatch is rolled over.
//you must add the following to the swatch control declaration:
//  OnClientThumbNailMouseOver="SwatchOver"

//var swatchUrl = '<%# ResolveUrl("~/assets/images") %>';

function SwatchOver(sender, eventArgs) {
	var mainImage = document.getElementById("MainImage");
	swatchPath = sender.get_swatchImagePath();
	mainImage.src = swatchPath + sender._swatches[eventArgs.index].Image;
}

//this function will cause the main image to swap when a swatch is rolled off.
//you must add the following to the swatch control declaration:
//  OnClientThumbnailMouseOut="SwatchOut" 
function SwatchOut(sender, eventArgs) {
	var mainImage = document.getElementById("MainImage");
	mainImage.src = mainImageSrc;
}

//this function will cause the main image to swap when a swatch is clicked.
//you must add the following to the swatch control declaration:
//  OnClientSelectedIndexChanged="SwatchClick"
function SwatchClick(sender) {
	var mainImage = document.getElementById("MainImage");
	swatchPath = sender.get_swatchImagePath();
	mainImage.src = swatchPath + sender.get_selectedImage();
}

/* Alternates */
//this function will cause the main image to swap when an alternate is rolled over.
//typically used in onmouseover and onmouseout to show the alternate and then change 
//back to the main image
   function AlternateOver(imagePath) {

   	var mainImage = $("#MainImage");
   	mainImage.attr('src', imagePath);
   }


