//Product Page functions for the Coverfinder

var coverPicked = false;
var ImagePath = '';
var VendorID = '';
var Material_99_Exists = 0;
var BubbleSemaphore = 0;
var BubbleStatus = 0;
var CoverHasStraps = 0;
var UserAddedStraps;
var ShowStrappedPreviewImage = 0;
var TextContainerStatus = 0;
var CurrentVisibleUpgradeText = '';
var NextUpgradeText = '';
var CurrentImageStraps = '';
var CurrentImageNoStraps = '';


function Init(SetImagePath, Vendor)
{
	ImagePath = SetImagePath;
	VendorID = Vendor;
	document.productForm.color_id.value = 'NONE';
	return true;
}

function ShowUpgrade(FloatWindowName)
{
	BubbleSemaphore = 1;
	if (BubbleStatus == 1 && CurrentVisibleUpgradeText != FloatWindowName){
		FadeOutUpgrade();
	}
	
	if (BubbleStatus == 0){
		ShowBubble();
	}
	
	if (CurrentVisibleUpgradeText != FloatWindowName){
		NextUpgradeText = FloatWindowName;
		setTimeout("SwitchUpgradeText();", 180);
	}

	return true;
}

function AllowBubbleHide(TransitionTime)
{
	BubbleSemaphore = 0; 
	setTimeout('HideUpgrade()', TransitionTime);	
}

function ShowBubble()
{
	var BubbleTarget = $('UpgradeBubbleContainer');
	BubbleTarget.removeClass('UpgradeBubbleHidden');
	var myFx = new Fx.Style(BubbleTarget, 'opacity', {duration:600, transition: Fx.Transitions.Cubic.easeOut}).start(0,1);
	BubbleStatus = 1;	
	return true;	
}

function FadeBubble(TransitionTime)
{
	var BubbleTarget = $('UpgradeBubbleContainer');
	var myFx = new Fx.Style(BubbleTarget, 'opacity', {duration:TransitionTime, transition: Fx.Transitions.Cubic.easeOut}).start(1,0);
	BubbleStatus = 0;
	setTimeout("HideBubble();", TransitionTime);
}

function HideBubble()
{
	var BubbleTarget = $('UpgradeBubbleContainer');
	BubbleTarget.addClass('UpgradeBubbleHidden');
	BubbleStatus = 0;
	return true;
}

function HideUpgrade(FloatWindowName)
{
	if (BubbleSemaphore == 1 || BubbleStatus == 0){
		return false;	
	}
	else if (BubbleSemaphore == 0 && BubbleStatus == 1){
		FadeBubble(2000);	
		return true;
	}
}

function SwitchUpgradeText()
{
	var NewBubbleContents = document.getElementById(NextUpgradeText);
	ActiveUpgradeTextContainerDOM = document.getElementById('UpgradeTextContainer');
	ActiveUpgradeTextContainerDOM.innerHTML = NewBubbleContents.innerHTML;
	CurrentVisibleUpgradeText = NextUpgradeText;
	
	FadeInUpgrade();
	
	return true;
}


function FadeOutUpgrade()
{
	if (BubbleStatus == 1 && TextContainerStatus == 1){
		var ElementFader = $('UpgradeTextContainer');
		var myFx = new Fx.Style(ElementFader, 'opacity', {duration:200}).start(1,0);
		TextContainerStatus = 0;	
	}	
	
	return true;	
}

function FadeInUpgrade()
{
	if (BubbleStatus == 0){
		ShowBubble();
	}
	
	if (BubbleStatus == 1 && TextContainerStatus == 0){
		var ElementFader = $('UpgradeTextContainer');
		var myFx = new Fx.Style(ElementFader, 'opacity', {duration:200}).start(0,1);
		TextContainerStatus = 1;	
	}	
	
	return true;	
}

function ShowColorPreview(image, strap_image)
{
	if (image == 'NULL' && strap_image == 'NULL'){
		image = CurrentImageNoStraps;
		strap_image = CurrentImageStraps;
	}
	else{
		CurrentImageStraps = strap_image;
		CurrentImageNoStraps = image;
	}	
	
	if (ShowStrappedPreviewImage == 1){
		document.images.PreviewImage.src = ImagePath+'/colors/'+strap_image;
	}
	else{
		document.images.PreviewImage.src = ImagePath+'/colors/'+image;
	}
	
	return 1;
}

function SwitchInfo(material_name, color_name, price, image, strap_image, prod_opt_id, color_id, w_length, material_id)
{
	var CurrentWidget = document.getElementById('ColorName');
	CurrentWidget.innerHTML = color_name;
	
	if (coverPicked == false){
		var CurrentWidget = document.getElementById('DefaultTextContainer');
		CurrentWidget.className = 'UpgradeOptionHidden';
		
		CurrentWidget = document.getElementById('PreviewTextContainer');
		CurrentWidget.className = 'UpgradeOptionVisible';
	}

	document.productForm.prod_opt_id.value = prod_opt_id;
	document.productForm.color_id.value = color_id;
	
	var poRef = document.getElementById('prod_opt_id_option');
	
	poRef.name = 'prod_opt_id_'+prod_opt_id+'_option';
	poRef.value = document.productForm.color_id.value;
	
	var poRefStrap = document.getElementById('prod_opt_id_strap');
	
	var CurrentWidget = document.getElementById('FabricName');
	CurrentWidget.innerHTML = material_name;
	
	CurrentWidget = document.getElementById('PriceName');
	CurrentWidget.innerHTML = '$' + price;
	
	CurrentWidget = document.getElementById('PreviewText');
	CurrentWidget.className = 'Warranty' + w_length;
	
	if ((material_id != CurrentMaterial) && (MaterialOptionsOnPage.length != 0)){
		ShowMaterialUpgrades(material_id);
	}
	else{
		document.productForm.prod_opt_id.value = prod_opt_id;			
	}
	
	ShowColorPreview(image, strap_image);

	CurrentMaterial = material_id;
	coverPicked = true;
	return false;
}

function ShowMaterialUpgrades(Material_id)
{
	if (MaterialOptionsOnPage.length == 0){
		return true;
	}
	
	var StrapOptionPattern = new RegExp('strap', 'i');
	
	for (UpgradeIndex = 0; UpgradeIndex < MaterialsOnPage.length; UpgradeIndex++){
		var CurrentUpgradeRow = document.getElementById('UpgradeOptionRow_Material_' + MaterialsOnPage[UpgradeIndex]);
		var IncludedStrapsOption;
		
		if (VendorID == 704){
			IncludedStrapsOption = document.getElementById('UpgradeCheckBox_' + MaterialsOnPage[UpgradeIndex] + '__straps_704');
			if (IncludedStrapsOption){						
				if (Material_id == 13){
					IncludedStrapsOption.value = 0;
				}
				else{
					IncludedStrapsOption.value = 1;
				}
			}
		}
		
		if (CurrentUpgradeRow)
		{
			if (MaterialsOnPage[UpgradeIndex] == Material_id){
				CurrentUpgradeRow.className = 'UpgradeOptionVisible';
			}
			else{
				CurrentUpgradeRow.className = 'UpgradeOptionHidden';
			}
		}
	}
	
	var GenericUpgrades = document.getElementById('UpgradeOptionRow_Material_99');
	if (GenericUpgrades){
		GenericUpgrades.className = 'UpgradeOptionVisible';
	}

	var UpgradeHeader = document.getElementById('UpgradeHeader');
	UpgradeHeader.className = 'UpgradeOptionVisible';
	
	return true;
}

function UpgradeCheckBoxSwitch(Material_id, type_id)
{
	if (MaterialOptionsOnPage.length == 0)
	{
		return true;
	}
	
	var StrapOptionPattern = new RegExp('strap', 'i');
	
	for (OptionIndex = 0; OptionIndex < MaterialOptionsOnPage.length; OptionIndex++){
		if (MaterialOptionsOnPage[OptionIndex] == Material_id + '__' + type_id){
			var CurrentOptionRow = document.getElementById('UpgradeCheckBox_' + MaterialOptionsOnPage[OptionIndex]);
			
			if (CurrentOptionRow.disabled == false){
				CurrentOptionRow.checked = !(CurrentOptionRow.checked);
			}
			
			if (StrapOptionPattern.test(MaterialOptionsOnPage[OptionIndex])){
				if (CurrentOptionRow.checked == true || UserAddedStraps == 1){
					CoverHasStraps = 1;
					ShowStrappedPreviewImage = 1;
				}
				else{
					CoverHasStraps = 0;	
					ShowStrappedPreviewImage = 0;
				}
				ShowColorPreview('NULL','NULL');
			}
			return true;
		}
	}
}

function ValidateUpgradeSelection(Material_id, type_id, prod_opt_id)
{
	if (MaterialOptionsOnPage.length == 0 || Material_id == 99){
		return true;
	}
	
	var MaterialUpgradeSelected = 0;
	var BootFrameSelected = 0;
	var MaterialBoundOptionPattern = new RegExp(Material_id + '__', 'i');
	var GenericOptionPattern = new RegExp('99__', 'i');
	var StrapOptionPattern = new RegExp(Material_id + '__strap', 'i');
	
	for (OptionIndex = 0; OptionIndex < MaterialOptionsOnPage.length; OptionIndex++)
	{
		var CurrentOptionRow = document.getElementById('UpgradeCheckBox_' + MaterialOptionsOnPage[OptionIndex]);		
		if (GenericOptionPattern.test(MaterialOptionsOnPage[OptionIndex])){
			Material_99_Exists = 1;
		}
		if (!(GenericOptionPattern.test(MaterialOptionsOnPage[OptionIndex]))){
			if (MaterialBoundOptionPattern.test(MaterialOptionsOnPage[OptionIndex])){
				if (MaterialOptionsOnPage[OptionIndex] == Material_id + '__' + type_id){
					if (CurrentOptionRow.checked == true){
						MaterialUpgradeSelected = 1;
					}
				}   
				else{   
					CurrentOptionRow.checked = false;
				}
			}
			else{
				if (CurrentOptionRow){
					CurrentOptionRow.checked = false;
				}
			}
			
			if (StrapOptionPattern.test(MaterialOptionsOnPage[OptionIndex])){
				if (CurrentOptionRow.checked == true){
					UserAddedStraps = 1;
					ShowStrappedPreviewImage = 1;
				}
				else{
					ShowStrappedPreviewImage = 0;
				}
 				ShowColorPreview('NULL','NULL');
			}
		}
	}
	return true;	
}


function ValidateForm()
{
	if (document.productForm.prod_opt_id.value == 'NONE' || document.productForm.color_id.value == 'NONE'){
		alert('You have not chosen a fabric or a color yet. Please click on the colored boxes under the fabric titles to change your color and fabric selection.');
		return false;
	}
	else{
		SaveTracking(document.productForm.cart_id.value);	
		document.productForm.submit();
		return true;
	}
}
