﻿function CCollection() {
    this.lsize = 0;
}

CCollection.prototype.Add = function(newItem) {
    /* --adds a new item to the collection-- */
    if (newItem == null) return;
    this.lsize++;
    this[(this.lsize - 1)] = newItem;
}

CCollection.prototype.Remove = function(index) {
    /* --removes the item at the specified index-- */
    if (index < 0 || index > this.length - 1) return;
    this[index] = null;

    /* --reindex collection-- */
    for (var i = index; i <= lsize; i++)
        this[i] = this[i + 1];

    this.lsize--;
}
CCollection.prototype.isEmpty = function () { return this.lsize == 0 }     /* --returns boolean if collection is/isn't empty-- */
CCollection.prototype.Size = function() { return this.lsize }
CCollection.prototype.Clear = function() {
    /* --clears the collection-- */
    for (var i = 0; i < lsize; i++)
        this[i] = null;

    this.lsize = 0;
}

CCollection.prototype.Price = function() {
	var price = 0;

	
    for (var i = 0; i < this.lsize; i++){
        var p = this[i].Price;
		

		if (p != undefined && !isNaN(p))
		{
			price += p;
		}
	}

    return price;
}


CCollection.prototype.UpdateSummary = function () {

	for (var i = 0; i < this.lsize; i++) {
		var modifier = this[i];

		var text = modifier.Text;

		if (modifier.SummaryLabel != undefined) {

			if (text != undefined && text != null && modifier.SummaryValueLabel != undefined) {
				
				modifier.SummaryLabel.show();
				
				modifier.SummaryValueLabel.html(text);

			}
			else {
				modifier.SummaryLabel.hide();
			}
		}
	}

	var lblprice = $("#product-price");


	$.ajax({
		type: "POST",
		url: "/BvModules/ProductTemplates/CanvasPop/GetPrice.aspx",
		data: "productid=" + this.ProductId + "&variantprice=" + this.Price(),
		async: false,
		dataType: "text",
		success: function (result) {
			lblprice.html(result);

		}

	});
}

var i = 0;

var ProductModifiersCollection = new CCollection();

function ProductModifierContainer() {
    this.containerId = null;
    ProductModifiersCollection.Add(this);
}

ProductModifierContainer.prototype.Debug = function(msg){
    alert(msg);
}

ProductModifierContainer.prototype.SetContainerId = function(c) {
    this.containerId = c;
}

ProductModifierContainer.prototype.RequiresSizeSelection = function(){
    try{
        if (this.containerId != null) {
            if ($('#'+ this.containerId).hasClass('.RequiresSizeSelection')){
                return true;
            } else {return false;}
        }
    } catch (ex){
        alert(ex);
    }
}


ProductModifierContainer.prototype.bindContainerPopup = function() {
    try{
        if (this.containerId != null) {
            $('#'+ this.containerId).find('.ContainerPopupTrigger').fancybox();
        }
    } catch (ex){
        alert(ex);
    }
}

ProductModifierContainer.prototype.bindOptionPopup = function() {
    try {
        if (this.containerId != null) {
            $('#' + this.containerId).find(' .imageRadioButtonHelpLink').fancybox({ frameWidth: 700, frameHeight: 550 });
        }
    } catch (ex) {
        alert(ex);
    }
}

ProductModifierContainer.prototype.show = function() {
    try {
        if (this.containerId != null) {
            if ($('#' + this.containerId).is(":hidden")) {
                $('#' + this.containerId).slideDown();
            }


            if (this.containerId === 'CanvasPopEffectModifier') {

                $('#' + this.containerId).find('img').thumbPopup({
                    imgSmallFlag: "_thumb",
                    imgLargeFlag: "_large"
                });
            }


            if (this.containerId === 'ProductFrameModifier') {

                $('#' + this.containerId).find('img').thumbPopup({
                    imgSmallFlag: "_thumb",
                    imgLargeFlag: "_large"
                });
            }
        }
    } catch (ex) {
        alert(ex);
    }
}

ProductModifierContainer.prototype.hide = function() {
    try {
        if (this.containerId != null) {
            if (!($('#' + this.containerId).is(":hidden"))) {
                $('#' + this.containerId).slideUp();
            }
        }
    } catch (ex) {
        alert(ex);
    }
}

ProductModifierContainer.prototype.UpdateSelection = function(price, text) {
    this.Price = price;
	this.Text = text;	
}



function Anthem_Error(result) {
    alert('Anthem_Error was invoked with the following error message: ' + result.error);
}

// to ensure all lightbox popups are bound properlt, add each container in its "View" user control into the list
// using and call this method in Controls/VariantDisplay control AFTER all modifiers and inputs have been added or updated.
function BindPopups() {
    for (var i = 0; i < ProductModifiersCollection.Size(); i++) {
        var modifier = ProductModifiersCollection[i];
        modifier.bindContainerPopup();
        modifier.bindOptionPopup();
    }

   /* $('#CanvasPopEffectOptions img').thumbPopup({
        imgSmallFlag: "_thumb",
        imgLargeFlag: "_large"
    });*/


}

function SizeChanged(validSize) {
    for (var i = 0; i < ProductModifiersCollection.Size(); i++){
        var modifier = ProductModifiersCollection[i];
        if (modifier.RequiresSizeSelection()) {
            if (validSize) {
                modifier.show();
            } else {
                modifier.hide();
            }
        }
//        modifier.bindContainerPopup();
//        modifier.bindOptionPopup();
    }
    if (validSize) {
        disableEdgeOptions();
    }
    
}


function disableEdgeOptions() {
    disabled = ($(".DisableEdgeOptions input:checked").length > 0);


    if (disabled) {

        //$('#CanvasPopEdgeOptions').find('input').removeAttr('checked');
        if (!$('#CanvasPopEdgeOptions').find('input:first').is(':checked')) {
            $('#CanvasPopEdgeOptions').find('input:first').trigger('click');
        }
        
        
        $('#CanvasPopEdgeOptions').find('input').attr('disabled', 'disabled');

        if (!($('#CanvasPopEdgeOptions').is(":hidden"))) {
            $('#CanvasPopEdgeOptions').slideUp();
            //Anthem_FireCallBackEvent(this, event, 'ctl00$MainContentHolder$VariantsDisplay$modifierTemplate3$FrameModifierList', '3', false, '', '', '', true, null, tiggerFrameOptionImageRadioButtonLightBox, null, true, true);
        }
    } else {
        $('#CanvasPopEdgeOptions').find('input').removeAttr('disabled');
        if (($('#CanvasPopEdgeOptions').is(":hidden"))) {
            $('#CanvasPopEdgeOptions').slideDown();
        }
    }
}
