var switching = false;

function switchImageByDimension(element, dimensions, imageName, useEffects) {
    if ($(element) && !switching) {
        switching = true;
        if (useEffects) {
            new Effect.Opacity(element, {
                from: 1.0,
                to: 0.0,
                duration: 0.6,
                afterFinish: function() {
                    if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                        $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&d=' + dimensions;
                    else
                        $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&d=' + dimensions;

                    new Effect.Opacity(element, { delay: 0.3, from: 0.0, to: 1.0, duration: 0.3, afterFinish: function() { switching = false; } });
                } .bind(this)
            });
        }
        else {
            if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&d=' + dimensions;
            else
                $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&d=' + dimensions;

            switching = false;
        }
    }
    if ($('flashVideoPlayer'))
        $('flashVideoPlayer').remove();
}

function switchImageByWidth(element, width, imageName, useEffects) {
    if ($(element) && !switching) {
        switching = true;
        if (useEffects) {
            new Effect.Opacity(element, {
                from: 1.0,
                to: 0.0,
                duration: 0.6,
                afterFinish: function() {
                    if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                        $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&w=' + width;
                    else
                        $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&w=' + width;

                    new Effect.Opacity(element, { delay: 0.3, from: 0.0, to: 1.0, duration: 0.3, afterFinish: function() { switching = false; } });
                } .bind(this)
            });
        }
        else {
            if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&w=' + width;
            else
                $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&w=' + width;

            switching = false;
        }
    }
    if ($('flashVideoPlayer'))
        $('flashVideoPlayer').remove();
}

function switchImageByHeight(element, height, imageName, useEffects) {
    if ($(element) && !switching) {
        switching = true;
        if (useEffects) {
            new Effect.Opacity(element, {
                from: 1.0,
                to: 0.0,
                duration: 0.6,
                afterFinish: function() {
                    if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                        $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&h=' + height;
                    else
                        $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&h=' + height;

                    new Effect.Opacity(element, { delay: 0.3, from: 0.0, to: 1.0, duration: 0.3, afterFinish: function() { switching = false; } });
                } .bind(this)
            });
        }
        else {
            if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&h=' + height;
            else
                $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&h=' + height;

            switching = false;
        }
    }
    if ($('flashVideoPlayer'))
        $('flashVideoPlayer').remove();
}

function switchImageByWidthHeight(element, width, height, imageName, useEffects) {
    if ($(element) && !switching) {
        switching = true;
        if (useEffects) {
            new Effect.Opacity(element, {
                from: 1.0,
                to: 0.0,
                duration: 0.6,
                afterFinish: function() {
                    if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                        $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&w=' + width + '&h=' + height;
                    else
                        $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&w=' + width + '&h=' + height;

                    new Effect.Opacity(element, { delay: 0.3, from: 0.0, to: 1.0, duration: 0.3, afterFinish: function() { switching = false; } });
                } .bind(this)
            });
        }
        else {
            if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                $(element).src = '/getdynamicimage.aspx?path=' + escape(imageName) + '&w=' + width + '&h=' + height;
            else
                $(element).src = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value) + '&w=' + width + '&h=' + height;

            switching = false;
        }
    }
    if ($('flashVideoPlayer'))
        $('flashVideoPlayer').remove();
}

function switchPrice(element, cost) {
    if ($(element))
        $(element).innerHTML = '$' + formatCurrency(parseFloat(cost) + parseFloat($('originalPrice').value));
}

function showVideo(video) {

    var embed = new Element('embed', {
    'id': 'flashVideoPlayer',
    'style': 'position: absolute;',
    'height': 250,
    'width': 350,
    'flashvars': 'config={"videoFile":"' + video + '"}&',
    'pluginspage': 'http://www.adobe.com/go/getflashplayer',
    'type': 'application/x-shockwave-flash',
    'bgcolor': '#ffffff',
    'quality': 'high',
    'allowscriptaccess': 'always',
    'allowfullscreen': 'true',
    'src': '/FlowPlayerLP.swf'
    });
    $('ImageContainer').insert({ top: embed });
}


var aValues = new Array();
function setOption(value, switchImage, setSwatch) {
    if (value.length > 0) {
        var sOption = value.split('=')[0];
        var sValue = value.split('=')[1];

        var bReplaced = false;

        for (var i = 0; i < aValues.length; i++) {
            if (aValues[i].split('=')[0] == sOption) {
                aValues[i] = value;
                bReplaced = true;
            }
        }

        if (!bReplaced) {
            aValues[aValues.length] = value;
        }

        validateOptions(aValues.join(';'));

        if (switchImage) {
            $('ImageManipulators').getElementsBySelector('input').each(function(value, element) {
                if (element.value.split(';')[0] == value) {
                    var image = element.value.split(';')[1];
                    if (image.length > 0 && image.indexOf('noimage') < 0)
                        changeImage(image);
                }
            } .bind(this, sValue));
        }

        if (setSwatch) {
            $$('div.ColorBlock')[0].getElementsBySelector('ul li a').each(function(value, element) {
                element.removeClassName('selected');
                if (element.title == value)
                    element.addClassName('selected');
            } .bind(this, sValue));
            $('ProductOptionSwatchSelection').innerHTML = '<span class="ProductColorSwatchName">' + sValue + '</span>';
        }
    }
}

function validateOptions(value) {
    var iCount = value.split(';').length;
    if (iCount == $('OptionCount').value) {
        this.isValid = false;
        $('ProductOptionValues').getElementsBySelector('input').each(function(value, element) {
            //  check if value exists
            if (value.split(';').uniq().sort().join(';') == element.value.split(';').uniq().sort().join(';')) {
                //  check if value extra exists
                if ($(element.identify() + '_extra')) {
                    //  change price
                    changePrice(parseFloat($(element.identify() + '_extra').value.split(';')[0]));
                    //  change image
                    changeImage($(element.identify() + '_extra').value.split(';')[1]);
                    //  change stock status
                    changeStockStatus();
                    //  change options
                    changeOptions(getOptionValue(value));
                    this.isValid = true;
                }
            }
            else {
                //  TODO:  awaiting stock status logic
            }
        } .bind(this, value));

        if (!this.isValid) {
            //  set defaults
            changePrice(0);
            changeImage($('DefaultImage').value);
            changeStockStatus();
            changeOptions('');
        }
    }
}

function validateOptions2() {
    if ($('OptionCount') && $('options')) {
        //  If the product has no options
        if (parseInt($('OptionCount').value) == 0) {
            return true;
        }
        else {
            //  If the product has options, and at least one is filled in
            if ($('options').value.length > 0 && aValues.length == parseInt($('OptionCount').value)) {
                return true;
            }
        }
    }
    alert("This product has options.  Please select options, then add this product to your bag.");
    return false;
}

function changePrice(offset) {
    if ($('OriginalPrice') && $('OriginalPriceLabel'))
        $('OriginalPriceLabel').innerHTML = '$' + formatCurrency(parseFloat($('OriginalPrice').value) + offset);

    if ($('SalePrice') && $('SalePriceLabel'))
        $('SalePriceLabel').innerHTML = '$' + formatCurrency(parseFloat($('SalePrice').value) + offset);
}

function changeImage(image) {
    if ($('imgMain'))
        $('imgMain').src = '/getdynamicimage.aspx?path=' + image + '&w=250&h=250';
}

function changeStockStatus() {
    //  TODO:  awaiting stock status logic
}

function changeOptions(value) {
    if ($('options'))
        $('options').value = value;
}

function formatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function validateForm() {
    if ($('OptionCount') && $('options')) {
        if ($('options').value.split(';').length == parseInt($('OptionCount').value))
            document.form2.submit();
    }
}

function getOptionValue(value) {
    this.optionValue = '';

    if ($('ProductOptionValues')) {
        $('ProductOptionValues').getElementsBySelector('input').each(function(value, element) {
            if (value.split(';').uniq().sort().join(';') == element.value.split(';').uniq().sort().join(';')) {
                if ($('OptionGroupID')) {
                    this.optionValue = $('OptionGroupID').value + ';' + element.identify();
                    return;
                }
            }
        } .bind(this, value));
    }

    return this.optionValue;
}

var slideShowOn = false;
var slideShowIndex = 0;

function toggleSlideShow() {

    this.i = 0;

    $$('.slideshowImage').each(function(element) {
        if (this.i == 0)
            switchImageByWidthHeight('imgMain', 250, 250, element.value, true);
        else
            switchImageByWidthHeight.delay(this.i * 5, 'imgMain', 250, 250, element.value, true);

        this.i = this.i + 1;
    } .bind(this));
}

Event.observe(window, 'load', function() {
    if ($('FeaturedCarousel')) {
        new Carousel('FeaturedCarousel', 787);
    }

    if ($('BestSellingCarousel')) {
        new Carousel('BestSellingCarousel', 787);
    }

    var bloobox = new Bloobox();
});

function ValidateFeedBackForm() {

	if (document.fFeed.QualityofProducts.value.length != 1) {
		alert('Please Enter a Valid Value for Quality of Products');
		document.fFeed['QualityofProducts'].focus();
		return(false);
	}
	
	if (document.fFeed.DiversityofSelection.value.length != 1) {
		alert('Please Enter a Valid Value for rating the diversity of our Products');
		document.fFeed['DiversityofSelection'].focus();
		return(false);
	}
	
	if (document.fFeed.AbilitytoFind.value.length != 1) {
		alert('Please Enter a Valid Value for your ability to find our Products');
		document.fFeed['AbilitytoFind'].focus();
		return(false);
	}

	if (document.fFeed.ProductDescriptions.value.length != 1) {

		alert('Please Enter a Valid Value for Products Description Quality');
		document.fFeed['ProductDescriptions'].focus();
		return(false);
	}
	
	if (document.fFeed.CustomerService.value.length != 1) {
		alert('Please Enter a Valid Value for Customer Service');
		document.fFeed['CustomerService'].focus();
		return(false);
	}

	if (document.fFeed.Shipping.value.length != 1) {
		alert('Please Enter a Valid Value for Quality of Shipping');
		document.fFeed['Shipping'].focus();
		return(false);
	}
	
	if (document.fFeed.Rate.value.length != 1) {
		alert('Please Enter a Valid Value for Rating your Experience');
		document.fFeed['Rate'].focus();
		return(false);
	}
	
	if (document.fFeed.willReturn.value.length != 1) {
			alert('Please Enter a Valid Value to indicate if you are likely to return');
			document.fFeed['willReturn'].focus();
			return(false);
	}
	
	if (document.fFeed.other.value.length < 1) {
		//check all of the checkboxes:
		var blnChecked=false;
		if (document.fFeed.friend.checked) {blnChecked=true;}
		if (document.fFeed.blog.checked) {blnChecked=true;}
		if (document.fFeed.print.checked) {blnChecked=true;}
		if (document.fFeed.facebook.checked) {blnChecked=true;}
		if (document.fFeed.twitter.checked) {blnChecked=true;}
		if (document.fFeed.searchengine.checked) {blnChecked=true;}
		if (document.fFeed.other.checked) {blnChecked=true;}

		if (blnChecked==false) {
			alert('Enter at least one value for how you found Bambeco.');
			return(false);
		}	
		
	}
	
	if (document.fFeed.signup.checked == true) {
		if (document.fFeed.email.value == "") {
			alert('Enter at least one value for how you found Bambeco.');
			return(false);
		}
	}
	
	
	
	return(true);	

	
}

function toggleQuestions() {
    var ele = document.getElementById("ProductMoreQuestions");
	if(ele.style.display == "block") {
    		ele.style.display = "none";
  	}
	else {
		ele.style.display = "block";

	}
} 

function swapZIndex(over)
{
	if(over)
	{
		$('ImageContainer').style.zIndex = 1;
	}
	else
	{
		$('ImageContainer').style.zIndex = -1;
	}
}

function switchAnchorImageByWidthHeight(element, width, height, imageName, useEffects) {
    if ($(element) && !switching) {
        switching = true;
        if (useEffects) {
            new Effect.Opacity(element, {
                from: 1.0,
                to: 0.0,
                duration: 0.6,
                afterFinish: function() {
                    if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                        $(element).href = '/getdynamicimage.aspx?path=' + escape(imageName);// + '&w=' + width + '&h=' + height;
                    else
                        $(element).href = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value);// + '&w=' + width + '&h=' + height;

                    new Effect.Opacity(element, { delay: 0.3, from: 0.0, to: 1.0, duration: 0.3, afterFinish: function() { switching = false; } });
                } .bind(this)
            });
        }
        else {
            if (imageName.length > 0 && imageName.indexOf('noimage') == -1)
                $(element).href = '/getdynamicimage.aspx?path=' + escape(imageName);// + '&w=' + width + '&h=' + height;
            else
                $(element).href = '/getdynamicimage.aspx?path=' + escape($('productOptionMainImage').value);// + '&w=' + width + '&h=' + height;

            switching = false;
        }
    }
    if ($('flashVideoPlayer'))
        $('flashVideoPlayer').remove();
}