
var CDNBaseUrl = undefined;
var CDNSuffix = '.png';
var CDNDefaultPad = 'nopad';
var CDNHoverProductId = 217;
var CDNHeightHover = 150;
var CDNHeightHoverLrgW = 2000;
var CDNHeightHoverLrgH = 1342;
var CDNHeightHoverLrgPanelW = 2000;
var CDNHeightHoverLrgPanelH = 1342;
var CDNHeightMain = 350;
var CDNHeightPrint = 800;
var sCartUrl = '/cart';
var sMailingUrl = '/mailer-signup';

$(document).ready(function(){
	
	$('.msgok').each(coolReveal);
	$('.msgerror').each(coolReveal);

	$('A[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

});

function coolReveal() {
	$(this).hide();
	$(this).slideDown('slow');
}

var oMailingForm = undefined;
var oMailingPopper = undefined;
// toggle the "enter your email address" message on the mailing list email input field
function initializeMailingList() {
	oMailingForm = $('#mailingform');
	oMailingPopper = $('#mailingpopper');
	var oInput = $('#mailinglistemail');
	if (oInput.length > 0) {
		oInput.bind('focus', function(){
			oInput.css({ 'background-position' : '500px 50%'});
		});
		oInput.bind('blur', function(){
			if ((oInput.val()).length < 1) { 
				oInput.css({ 'background-position' : '2px 50%'});
			}
		});
	}
	if (oMailingForm.length > 0 && oMailingPopper.length > 0) {
		oMailingPopper.fancybox({
			'scrolling': 'auto',
			'width': 543,
			'height': 450,
			'autoScale': true,
			'overlayColor': '#000',
			'overlayOpacity': 0.9,
			'titleShow': false,
			'showCloseButton': true,
			'showNavArrows': false,
			'type': 'iframe',
			'padding': 4
		});		
		
		oMailingForm.submit( function(){
			MailingListSubmit();
			return false;
		});
	}
}

function MailingListSubmit(){
	var oMailingListEmail = $('#mailinglistemail');
	if(oMailingListEmail.length > 0){
		var sPostData = {
				'action' : 'mailer_signup',
				'mailer_email' : oMailingListEmail.val(),
				'async' : ''
		}
		
		$.ajax({
			url: sMailingUrl,
			//url: 'addtocart.html',
			
			type: 'post',
			
			data: sPostData,
			
			success: function(data, textStatus, XMLHttpRequest) {
				// could also do something with the "data" returned from the page you posted to
				oMailingPopper.trigger('click');
			},
			
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert('ERROR: The item could not be added to your cart. Please try again. If problems persist please contact us directly for assistance.\n'+errorThrown);
			}
		});		
	}
}
function MiniCartInitialize( sFSK ) {
	var form = $('#form');
	var action = $('#cart_action');
	var removeBtns = $('.removebutton');
	var checkoutBtns = $('.checkout');
	var closeLinks = $('.mini');
	if(removeBtns.length > 0){
		removeBtns.each(function(){
			$(this).click(function(){
				if(form.length > 0 && action.length > 0){
					action.val("clear");
					form.append('<input type="hidden" name="chk_remove_'+$(this).attr('rel')+'" value="on" checked />');
					form.submit();
				}
				return false;				
			});
			
		});
	}
	if(checkoutBtns.length > 0){
		checkoutBtns.each(function(){
			$(this).click(function(event){
				event.preventDefault();
				parent.location.href=secureSite+'cart?fsk='+sFSK;
				parent.$.fancybox.close();
			});
			
		});
	}
	if(closeLinks.length > 0){
		closeLinks.each(function(){
			$(this).click(function(event){
				event.preventDefault();
				$(parent.location).attr("href",$(this).attr("href"));
				parent.$.fancybox.close();
			});
			
		});
	}
}
function CartInitialize() {
	var form = $('#form');
	var action = $('#cart_action');
	var removeBtns = $('.removebutton');
	if(removeBtns.length > 0){
		removeBtns.each(function(){
			$(this).click(function(){
				if(form.length > 0 && action.length > 0){
					action.val("clear");
					form.append('<input type="hidden" name="chk_remove_'+$(this).attr('rel')+'" value="on" checked />');
					form.submit();
				}
				return false;				
			});
			
		});
	}
}


// toggle the background instructions for the zip code input in the cart tallies table
function CartTalliesInitialize() {
	var oInput = $('#carttallieszip');
	if (oInput.length > 0) {
		oInput.bind('focus', function(){
			oInput.addClass('carttalliesfocus');
		});
		oInput.bind('blur', function(){
			if ((oInput.val()).length < 1) { 
				oInput.removeClass('carttalliesfocus');
			}
		});
		
		if ((oInput.val()).length > 0) { 
			oInput.addClass('carttalliesfocus');
		}
	}

	var oInputPromo = $('.carttalliescoupons_promoinput');
	if (oInputPromo.length > 0) {
		oInputPromo.bind('focus', function(){
			oInputPromo.addClass('carttalliescouponsfocus');
		});
		oInputPromo.bind('blur', function(){
			if ((oInputPromo.val()).length < 1) { 
				oInputPromo.removeClass('carttalliescouponsfocus');
			}
		});
		if ((oInputPromo.val()).length > 0) { 
			oInputPromo.addClass('carttalliescouponsfocus');
		}
	}
		
	var oInputPin = $('.carttalliescoupons_pininput');
	if (oInputPin.length > 0) {
		oInputPin.bind('focus', function(){
			oInputPin.addClass('carttalliescouponsfocus');
		});
		oInputPin.bind('blur', function(){
			if ((oInputPin.val()).length < 1) { 
				oInputPin.removeClass('carttalliescouponsfocus');
			}
		});
		if ((oInputPin.val()).length > 0) { 
			oInputPin.addClass('carttalliescouponsfocus');
		}
	}	
	var oInputPhrase = $('.carttalliescoupons_phraseinput');
	if (oInputPhrase.length > 0) {
		oInputPhrase.bind('focus', function(){
			oInputPhrase.addClass('carttalliescouponsfocus');
		});
		oInputPhrase.bind('blur', function(){
			if ((oInputPhrase.val()).length < 1) { 
				oInputPhrase.removeClass('carttalliescouponsfocus');
			}
		});
		if ((oInputPhrase.val()).length > 0) { 
			oInputPhrase.addClass('carttalliescouponsfocus');
		}
	}	
}




// featured slider

var oFeaturedListItems = undefined;
var iFeaturedListItemCount = 0;
var iFeaturedListCurrentItem = 1;
var oFeaturedListTimer = undefined;
var iFEATURED_ITEMS_ADVANCE_DELAY = 5000;
var iFEATURED_ITEMS_ADVANCE_DELAY_AFTER_CLICK = 10000;
var iFeaturedListDelay = iFEATURED_ITEMS_ADVANCE_DELAY;
var iFEATURED_ITEMS_FADE_SPEED = 500;
var bClickLock = false;

function FeaturedItemsHandleClick(oThis, oEvent, iDirection) {
	oEvent.preventDefault();
	
	if (!bClickLock) {
		bClickLock = true;
		
		clearTimeout(oFeaturedListTimer);
		iFeaturedListDelay = iFEATURED_ITEMS_ADVANCE_DELAY_AFTER_CLICK;
		
		oThis.blur();
		FeaturedItemsAdvance(iDirection);
	}
}

function FeaturedItemsAdvance(iDirection) {
	var sMarginLeftSign = '-';
	if (iDirection == -1) {
		sMarginLeftSign = '';
	}
	oFeaturedListItems.eq((iFeaturedListCurrentItem-1)).animate({opacity:'0', 'margin-left':(sMarginLeftSign+'30px')}, (iFEATURED_ITEMS_FADE_SPEED/2), function() {
		$(this).css({display:'none'});
		iFeaturedListCurrentItem = iFeaturedListCurrentItem + iDirection;
		if (iFeaturedListCurrentItem < 1) {
			iFeaturedListCurrentItem = iFeaturedListItemCount;
		} else if (iFeaturedListCurrentItem > iFeaturedListItemCount) {
			iFeaturedListCurrentItem = 1;
		}
		FeaturedItemsShowItem(iDirection);
	});
}

function FeaturedItemsShowItem(iDirection) {
	var sMarginLeftSign = '';
	if (iDirection == -1) {
		sMarginLeftSign = '-';
	}
	oFeaturedListItems.eq((iFeaturedListCurrentItem-1)).css({opacity:'0', display:'block', 'margin-left':(sMarginLeftSign+'30px')}).animate({opacity:'1', 'margin-left':'0px'}, iFEATURED_ITEMS_FADE_SPEED, function() {
		FeaturedItemsQueueNext();
	});	
}

function FeaturedItemsQueueNext() {
	oFeaturedListTimer = setTimeout(FeaturedItemsAutoAdvance, iFeaturedListDelay);
	if (iFeaturedListDelay == iFEATURED_ITEMS_ADVANCE_DELAY_AFTER_CLICK) {
		iFeaturedListDelay = iFEATURED_ITEMS_ADVANCE_DELAY;
	}
	bClickLock = false;
}

function FeaturedItemsAutoAdvance() {
	FeaturedItemsAdvance(1);
}

function FeaturedItemsInitialize() {
	oFeaturedListItems = $('.slidergroups .slidergroup');
	iFeaturedListItemCount = oFeaturedListItems.length;
	if (iFeaturedListItemCount > 0) {
		$('#slideprev').click(function(event) {
			FeaturedItemsHandleClick(this, event, -1);
		});
		
		$('#slidenext').click(function(event) {
			FeaturedItemsHandleClick(this, event, 1);
		});
		
		oFeaturedListItems.eq(0).fadeIn(iFEATURED_ITEMS_FADE_SPEED, function() {
			FeaturedItemsQueueNext();
		});
	}
}




// tabs

var oTabsMenuItems = undefined;
var oTabsContentItems = undefined;

function TabsHandleClick(e, oThis) {
	e.preventDefault();
	TabsCloseAll(oThis);
	oThis.addClass('on');
	oTabsContentItems.eq(oThis.index()).addClass('on');
	oThis.removeClass('off');
	oTabsContentItems.eq(oThis.index()).removeClass('off');
}

function TabsCloseAll(oThis) {
	oTabsMenuItems.addClass('off');
	oTabsMenuItems.removeClass('on');
	oTabsContentItems.addClass('off');
	oTabsContentItems.removeClass('on');
	
}

function TabsInitialize() {
	oTabsMenuItems = $('.tabsmenu div');
	if (oTabsMenuItems.length > 0) {
		oTabsContentItems = $('.tabsmain div.tabscontent');
		if (oTabsContentItems.length > 0) {
			TabsCloseAll();
			oTabsMenuItems.first().addClass('on');
			oTabsContentItems.first().addClass('on');
			oTabsMenuItems.first().removeClass('off');
			oTabsContentItems.first().removeClass('off');
			oTabsMenuItems.click(function(e) {
				TabsHandleClick(e, $(this));
			});
		}		
	}
}




// furniture grid

var bDidInitializeFurnitureGridYet = false;

function FurnitureGridInitialize() {
	if (!bDidInitializeFurnitureGridYet) {
		bDidInitializeFurnitureGridYet = true;
		$('.furnituregrid_item a img.inset').hover(function() {
				var oItem = $(this).parent().parent().find('div.furnituregrid_item_info');
				if (oItem.length > 0) {
					oItem.addClass('furnituregrid_item_info_hover');
				}
			}, function() {
				var oItem = $(this).parent().parent().find('div.furnituregrid_item_info');
				if (oItem.length > 0) {
					oItem.removeClass('furnituregrid_item_info_hover');
				}
		});
	}
}






// panel menu

var oPMLinks = undefined;
var oPMLinkSubs = undefined;

function PanelMenuHandleLinkClick(oEvent, oThis) {
	oEvent.preventDefault();
	return;
	oThis = $(oThis);
	var oNearestSub = oThis.next('.panelmenusub');
	if (oThis.hasClass('on')) {
		if (oNearestSub.length > 0) {
			oNearestSub.slideUp(400);
			oThis.removeClass('on');
		}else{
			$(document.location).attr("href",oThis.attr("href"));
		}
	} else {
		oPMLinkSubs.slideUp(400);
		oPMLinks.removeClass('on');
		if (oNearestSub.length > 0) {
			oNearestSub.slideDown(400);
			oThis.addClass('on');
		}else{
			$(document.location).attr("href",oThis.attr("href"));
		}
	}
}

function PanelMenuInitialize() {
	oPMLinks = $('.panelmenu a.link');
	oPMLinkSubs = $('.panelmenusub');
	oPMLinks.click(function(event) {
		PanelMenuHandleLinkClick(event, this);
	});
}






// build a chair

var iBACProductBgId = -1;
var sBACProductSKU = '';
var fBACProductPrice = 0.0;
var sBACPanelSKU = '';
var sBACFSK = '';

var iBACChoicePanelOverlayId = -1; 
var iBACChoicePanelCalloutId = -1;

var bBACSeeingActualPanels = false;
var bBACPanelPage = false;

var iHoverW = 0;
var iHoverH = 0;
var iHoverLrgAreaW = 0;
var iHoverLrgAreaH = 0;
var iHoverLrgSwayW = 0;
var iHoverLrgSwayH = 0;
						
var oBACImgMain = undefined;
var oBACImgHover = undefined;
var oBACImgHoverLrg = undefined;
var oBACHoverOffset = undefined;						
var oBACThumbs = undefined;
var oBACColorChoices = undefined;
var oBACHoverMsg = undefined;
var oBACHoverImg = undefined;
var oBACPanelFootLinks = undefined;
var oBACSearchTextInput = undefined;
var oBACPanelGridLinks = undefined;
var oBACCartForm = undefined;
var oBACMiniCartPopper = undefined;
var oBACPrintLink = undefined;
var oBACPrintImg = undefined;

function BACSelectPanel(iPanelId, iCalloutId) {
	iBACChoicePanelOverlayId = iPanelId;
	iBACChoicePanelCalloutId = iCalloutId;
	BACUpdateMainPic();
}

function BACUpdateMainPic() {
	if (iBACProductBgId > 0) {
		oBACImgMain.css({ 'background-image': 'url('+GetCDNUrl(iBACProductBgId, iBACChoicePanelOverlayId, CDNHeightMain, CDNDefaultPad)+')' });
		if (iBACChoicePanelOverlayId !== -1 && iBACChoicePanelOverlayId > 0) {
			oBACHoverMsg.fadeIn(500);
			//oBACHoverImg
		} else {
			oBACHoverMsg.fadeOut(500);
			//oBACHoverImg
		}
		BACUpdatePrintPic(); // always also update the large print image
	}
}

function BACUpdateHoverPic() {
	if (oBACImgHover.length > 0 && oBACImgHoverLrg.length > 0) {
		oBACImgHover.css({ 'background-image': 'url('+GetCDNUrl(iBACChoicePanelCalloutId, -1, CDNHeightHover, CDNDefaultPad)+')' });
		oBACImgHoverLrg.css({ 'background-image': 'url('+GetCDNUrl(iBACChoicePanelCalloutId, -1, CDNHeightHoverLrgH, CDNDefaultPad)+')' });
	}
}

function BACUpdatePrintPic() {
	if (oBACPrintImg.length > 0) {
		oBACPrintImg.attr('src', GetCDNUrl(iBACProductBgId, iBACChoicePanelOverlayId, CDNHeightPrint, CDNDefaultPad))
	}else{
		alert("Large Image Not Loaded");
	}
}

function BACHandleClickOnThumb(oEvent, oThis) {
	oEvent.preventDefault();
	oThis = $(oThis);
	var iClickedBgId = parseInt(oThis.attr('rel'));
	if (iClickedBgId !== iBACProductBgId) {
		iBACProductBgId = iClickedBgId;
		BACUpdateMainPic();
	}
}

function BACHandleClickOnColor(oEvent, oThis) {
	oEvent.preventDefault();
	oThis = $(oThis);
	var sClickedColorName = oThis.find('strong').text();
	var sClickedId = oThis.attr('id');
	var aClicked = sClickedId.split('_');
	var sClickedCode = aClicked[1];
	var fClickedCharge = parseFloat(aClicked[2]);
	
	if (sClickedColorName !== oBACChoiceColor['name']) {
		var oName = $("#product_name"); 
		var name = oName.html();
		if(sCurrentColor['name'] != oBACChoiceColor['name']){
			name = name.substring(oBACChoiceColor['name'].length,(name.length));
		}
		
		if(sClickedColorName != sCurrentColor['name']){
			name = sClickedColorName + " "+ name;
		}
		
		oName.html(name);			

		var oPrice = $("#product_price"); 
		var price = oPrice.html();
		price = parseFloat(price.substring(1,(price.length-1)).replace(',',''));
		price -= oBACChoiceColor['charge'];

		oBACChoiceColor['code'] = sClickedCode;
		oBACChoiceColor['name'] = sClickedColorName;
		oBACChoiceColor['charge'] = fClickedCharge;
		
		
		price += oBACChoiceColor['charge'];
		oPrice.html("$"+(addCommas(price.toFixed(2))));
		
		sBACProductSKU = sBACProductSKU.substring(0, sBACProductSKU.length-sClickedCode.length) + sClickedCode;
		
		oBACColorChoices.removeClass('on');
		oThis.addClass('on');
		
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function BACHandleClickOnPanelFootLink(oEvent, oThis) {
	oEvent.preventDefault();
	oThis = $(oThis);
	var iClickedPanelId = parseInt(oThis.attr('rel'));
	if (iClickedPanelId !== iBACChoicePanelOverlayId) {
		oBACPanelFootLinks.removeClass('on');
		oThis.addClass('on');
	}
	BACLoadNewPanel(iClickedPanelId);
}

function BACHandleClickOnPanelGridLink(oEvent, oThis) {
	if (bBACSeeingActualPanels) {
		oEvent.preventDefault();
		oThis = $(oThis);
		var iClickedPanelId = parseInt(oThis.attr('rel'));
		if (iClickedPanelId !== iBACChoicePanelOverlayId) {
			oBACPanelFootLinks.removeClass('on');
			oThis.addClass('on');
		}
		BACLoadNewPanel(iClickedPanelId);
	}
}

function BACLoadNewPanel(iClickedPanelId) {
	if (iClickedPanelId !== iBACChoicePanelOverlayId) {
		iBACChoicePanelOverlayId = iClickedPanelId;
		BACUpdateMainPic();
		BACUpdateHoverPic();
	}
}

function GetCDNUrl(iBgId, iOverlayId, iHeight, sPad) {
	if(CDNBaseUrl == undefined) return '';
	return CDNBaseUrl + iBgId + '_' + iOverlayId + '_' + iHeight + '_' + sPad + CDNSuffix;
}

function BACHandleFormSubmit( forceSku , forcePanel) {
	var sku = sBACProductSKU;
	var pSku = sBACPanelSKU;
	if(forceSku != undefined && forceSku.length > 0){
		sku = forceSku;
	}
	if(forcePanel == ''){
		pSku = undefined;
	}
	
	var skus = [];
	skus[0] = sku;
	if(pSku != undefined  && pSku!=''){
		skus[1] = pSku;
	}
	
	var sPostData = {
			'fsk' : sBACFSK,
			'action' : 'cart',
			'cart_action' : 'addTo',
			'part_id[]' : skus,
			'json' : ''
	}
	sPostData['qty_'+skus[0]] = 1;
	
	if(oBACChoiceColor['code'] != sCurrentColor['code']){
		sPostData['color_'+skus[0]] = oBACChoiceColor['code'];		
	}
	
	if(pSku != undefined && skus[1]!=''){
		if(oBACChoiceColor['code'] != sCurrentColor['code']){
			sPostData['color_'+skus[1]] = oBACChoiceColor['code'];			
		}
		sPostData['qty_'+skus[1]] = 1;		
	}
	
	$.ajax({
		url: sCartUrl,
		//url: 'addtocart.html',
		
		type: 'post',
		
		data: sPostData,
		
		success: function(data, textStatus, XMLHttpRequest) {
    		// could also do something with the "data" returned from the page you posted to
			if( data['result'] == "sucess"){
				CartInfoUpdate( data['cart_info']['qty'] , data['cart_info']['total'] );				
			}
    		oBACMiniCartPopper.trigger('click');
  		},
  		
  		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('ERROR: The item could not be added to your cart. Please try again. If problems persist please contact us directly for assistance.\n'+errorThrown);
		},
		
		dataType: "json"
	});
}

function BACHandlePrintClick(event) {
	event.preventDefault();
	window.print();
}

function BACHandleZoomHoverIn() {
	if (iBACChoicePanelCalloutId !== -1 || bBACPanelPage) {
		oBACImgHoverLrg.show();
		if (!bBACPanelPage) {
			oBACHoverMsg.css({ 'color': '#fff' });
		}
	}
}

function BACHandleZoomHoverOut() {
	oBACImgHoverLrg.hide();
	if (!bBACPanelPage) {
		oBACHoverMsg.css({ 'color': '#25255a' });
	}
}

function BACHandleZoomHoverMousemove(event, oThis) {
	if (iBACChoicePanelCalloutId !== -1 || bBACPanelPage) {
		oThis = $(oThis);
		
		var XOffset = event.pageX - (oBACHoverOffset.left - 8);
		var YOffset = event.pageY - oBACHoverOffset.top;
		
		var XRatio = (XOffset / iHoverW).toFixed(2);
		var YRatio = (YOffset / iHoverH).toFixed(2);
		
		if (XRatio < 0) {
			XRatio = 0;
		} else if (XRatio > 1) {
			XRatio = 1;
		}
		
		if (YRatio < 0) {
			YRatio = 0;
		} else if (YRatio > 1) {
			YRatio = 1;
		}
		
		var iXLrg = parseInt(iHoverLrgSwayW * XRatio);
		var iYLrg = parseInt(iHoverLrgSwayH * YRatio);
		
		if (iXLrg < iHoverLrgSwayW) {
			iXLrg = HoverLrgSwayW;
		}
		
		if (iYLrg < iHoverLrgSwayH) {
			iYLrg = iHoverLrgSwayH;
		}

		oBACImgHoverLrg.css({ 'left': iXLrg+'px', 'top': iYLrg+'px' });
	}
}

var oBACChoiceColor = {
		'code' : 'BLK' ,
		'name' : 'Black',
		'charge' : 0.0,
}; 

function BACInitialize(iProductBgId, fPrice, sSKU, sPanelSKU, iPanelOverlayId, iPanelCalloutId, oColor, sFSK, cdnUrl ) {
	iBACProductBgId = iProductBgId;
	sBACProductSKU = sSKU;
	fBACProductPrice = fPrice;
	sBACPanelSKU = sPanelSKU;
	iBACChoicePanelOverlayId = iPanelOverlayId;
	iBACChoicePanelCalloutId = iPanelCalloutId;
	
	
	oBACChoiceColor['code'] = oColor['code'] ;
	oBACChoiceColor['name'] = oColor['name'] ;
	oBACChoiceColor['charge'] = oColor['charge'] ;
	
	sBACFSK = sFSK;
	CDNBaseUrl = cdnUrl;
	
	oBACImgMain = $('#proddetails_pics_main');
	oBACImgHover = $('#proddetails_pics_hover_img');
	oBACImgHoverLrg = $('#proddetails_pics_hover_lrg');
	oBACThumbs = $('.proddetails_pics_details_thumbs_pic');
	oBACColorChoices = $('.colorchoice');
	oBACHoverMsg = $('#proddetails_pics_hover_msg');
	oBACHoverImg = $('#proddetails_pics_hover_img');
	oBACPanelFootLinks = $('.panelmenufoot a');
	oBACSearchTextInput = $('#panel_search_textinput');
	oBACPanelGridLinks = ''; //$('.panelgrid_results_item a');
	oAddToCarts = $('a.add');
	oPanelGroups = $('.panelgroup');
	oPanelPoolSpecialPopup = $('#panelpoolpopup');
	oPanelFeatureSelect = $('#featured_select');
	oPanelFeatureQty = $('#featured_qty');
	oPanelFeatureSku = $('#featured_sku');

	oBACCartForm = $('#frmAddToCart');
	oBACMiniCartPopper = $('#minicartpopper');
	oBACPrintLink = $('.prodtools_print');
	oBACPrintImg = $('#printimage');
	
	var oSeeingActualPanels = $('.panelgrid_results_seeing_actual_panels');
	if (oSeeingActualPanels.length > 0) {
		bBACSeeingActualPanels = true;
	}

	if (oBACImgHover.length > 0) {
		oBACHoverOffset = oBACImgHover.offset();
		iHoverW = oBACImgHover.width();	
		iHoverH = oBACImgHover.height();
		oBACImgHover.hover(function() {
			BACHandleZoomHoverIn();
		}, function() {
			BACHandleZoomHoverOut();
		});
		oBACImgHover.mousemove(function(event) {
			BACHandleZoomHoverMousemove(event, this);
		});
	}

	if (oBACImgMain.length > 0) {
		iHoverLrgAreaW = oBACImgMain.width();	
		iHoverLrgAreaH = oBACImgMain.height();
		iHoverLrgSwayW = -1 * (CDNHeightHoverLrgW - iHoverLrgAreaW);
		iHoverLrgSwayH = -1 * (CDNHeightHoverLrgH - iHoverLrgAreaH);

		BACUpdateMainPic();
		oBACThumbs.click(function(event) {
			BACHandleClickOnThumb(event, this);
		});
		oBACColorChoices.click(function(event) {
			BACHandleClickOnColor(event, this);
		});
		
		if (oBACPanelFootLinks.length > 0) {
			oBACPanelFootLinks.click(function(event) {
				//BACHandleClickOnPanelFootLink(event, this);
			});
		}
		
		if (oBACSearchTextInput.length > 0) {
			if ($.trim(oBACSearchTextInput.val()) !== '') {
				oBACSearchTextInput.addClass('panel_search_textinput_focus');
			}
			oBACSearchTextInput.bind('focus', function() {
				$(this).addClass('panel_search_textinput_focus');
			});
			oBACSearchTextInput.bind('blur', function() {
				var sText = $.trim($(this).val());
				if (sText == '') {
					$(this).val('');
					$(this).removeClass('panel_search_textinput_focus');
				}
			});
		}
		
		if (oBACPanelGridLinks.length > 0) {
			oBACPanelGridLinks.click(function(event) {
				BACHandleClickOnPanelGridLink(event, this);
			});
		}
		
		if (oBACCartForm.length > 0 && oBACMiniCartPopper.length > 0) {
			oBACMiniCartPopper.fancybox({
				'scrolling': 'auto',
				'width': 650,
				'height': 980,
				'autoScale': true,
				'overlayColor': '#000',
				'overlayOpacity': 0.9,
				'titleShow': false,
				'showCloseButton': true,
				'showNavArrows': false,
				'type': 'iframe',
				'padding': 4,
				'onClosed': function(){
					CartInfoQuery();
				}
			});
			oBACCartForm.submit(function() {
				BACHandleFormSubmit();
				return false;
			});
			if( oAddToCarts.length > 0 ){
				oAddToCarts.each(function(){
					$(this).click(function(){
						var sku = $(this).attr("rel");
						BACHandleFormSubmit( sku , '');
						return false;
					});
				});
			}
		}
		
		if (oBACPrintLink.length > 0) {
			oBACPrintLink.click(function(event) {
				BACHandlePrintClick(event);
			});
		}
		if(oPanelFeatureSelect.length > 0){
			oPanelFeatureSelect.bind("change",function(){
				var sku = $.trim($(this).val());
				var item = featured_group[sku];
//				oBACImgHover.html(
//						'<img src="'+item["thumbSrc"]+'" alt="" />'
//				);
				oBACImgHover.css("background","url("+item["thumbSrc"]+") no-repeat center center");
				oBACImgMain.css("background","url("+item["midSrc"]+") no-repeat center center");
				oBACImgHoverLrg.css("background","url("+item["fullSrc"]+") no-repeat center");
				oPanelFeatureQty.attr("name","qty_"+sku);
				oBACPrintImg.attr('src',item["printSrc"]);
//				oPanelFeatureSku.html(sku);
				sBACPanelSKU = sku;
			});
		}
		if(oPanelGroups.length > 0){
			oPanelGroups.each(function(){
				$(this).bind("change",function(){
					var part_id = $(this).attr("rel");
					var sku = $(this).val();
					var ary = eval('src_'+part_id);
					$("#addbtn_"+part_id).attr("rel",sku);
					$("#img_"+part_id).attr("src",ary[sku]['src']);
//					$("#link_"+part_id).attr("href",ary[sku]['url_name']);
				});
			});
		}
	}
	if (oPanelPoolSpecialPopup.length > 0 ) {
		oPanelPoolSpecialPopup.fancybox({
			'scrolling': 'auto',
			'width': 565,
			'height': 980,
			'autoScale': true,
			'overlayColor': '#000',
			'overlayOpacity': 0.9,
			'titleShow': false,
			'showCloseButton': true,
			'showNavArrows': false,
			'type': 'iframe',
			'padding': 4,
			'onClosed': function(){
				CartInfoQuery();
			}
		});
	}
}

var oPanelFeatureSelect = undefined;
var oPanelFeatureQty = undefined;
var oPanelFeatureSku = undefined;
var oAddToCarts = undefined;
var oPanelGroups = undefined;
var oPanelSpecialPopup = undefined;
var oPanelPoolSpecialPopup = undefined;
function PanelPageInitialize(iProductBgId, sSKU, sFSK) {
	iBACProductBgId = iProductBgId;
	sBACProductSKU = sSKU;
	sBACFSK = sFSK;
	
	bBACPanelPage = true;
	
	oBACImgMain = $('#proddetails_pics_main');
	oBACImgHover = $('#proddetails_pics_hover_img');
	oBACImgHoverLrg = $('#proddetails_pics_hover_lrg');
	oBACSearchTextInput = $('#panel_search_textinput');
	oBACCartForm = $('#frmAddToCart');
	oBACMiniCartPopper = $('#minicartpopper');
	oPanelFeatureSelect = $('#featured_select');
	oPanelFeatureQty = $('#featured_qty');
	oPanelFeatureSku = $('#featured_sku');
	oPanelSpecialPopup = $('#panelpopup');
	oPanelPoolSpecialPopup = $('#panelpoolpopup');
	oAddToCarts = $('.add');
	oPanelGroups = $('.panelgroup');
		
	var oSeeingActualPanels = $('.panelgrid_results_seeing_actual_panels');
	if (oSeeingActualPanels.length > 0) {
		bBACSeeingActualPanels = true;
	}
	
	if (oBACImgHover.length > 0) {
		oBACHoverOffset = oBACImgHover.offset();
		iHoverW = oBACImgHover.width();	
		iHoverH = oBACImgHover.height();
		oBACImgHover.hover(function() {
			BACHandleZoomHoverIn();
		}, function() {
			BACHandleZoomHoverOut();
		});
		oBACImgHover.mousemove(function(event) {
			BACHandleZoomHoverMousemove(event, this);
		});
		//oBACImgHoverLrg.css({ 'width': CDNHeightHoverLrgPanelW+'px', 'height': CDNHeightHoverLrgPanelH+'px' });
	}
	
	if (oBACImgMain.length > 0) {
		iHoverLrgAreaW = oBACImgMain.width();	
		iHoverLrgAreaH = oBACImgMain.height();
		iHoverLrgSwayW = -1 * (CDNHeightHoverLrgPanelW - iHoverLrgAreaW);
		iHoverLrgSwayH = -1 * (CDNHeightHoverLrgPanelH - iHoverLrgAreaH);
		
		if (oPanelFeatureSelect.length > 0) {
			oPanelFeatureSelect.bind('change', function(){
				var sku = $.trim($(this).val());
				var item = featured_group[sku];
				oBACImgHover.html(
						'<img src="'+item["thumbSrc"]+'" alt="" />'
				);
				oBACImgMain.css("background","url("+item["midSrc"]+") no-repeat center");
				oBACImgHoverLrg.css("background","url("+item["fullSrc"]+") no-repeat center");
				oPanelFeatureQty.attr("name","qty_"+sku);
				oPanelFeatureSku.html(sku);
				sBACProductSKU = sku;
			});
			
		}
		if (oBACSearchTextInput.length > 0) {
			if ($.trim(oBACSearchTextInput.val()) !== '') {
				oBACSearchTextInput.addClass('panel_search_textinput_focus');
			}
			oBACSearchTextInput.bind('focus', function() {
				$(this).addClass('panel_search_textinput_focus');
			});
			oBACSearchTextInput.bind('blur', function() {
				var sText = $.trim($(this).val());
				if (sText == '') {
					$(this).val('');
					$(this).removeClass('panel_search_textinput_focus');
				}
			});
		}
		
		if (oPanelSpecialPopup.length > 0 ) {
			oPanelSpecialPopup.fancybox({
				'scrolling': 'auto',
				'width': 565,
				'height': 980,
				'autoScale': true,
				'overlayColor': '#000',
				'overlayOpacity': 0.9,
				'titleShow': false,
				'showCloseButton': true,
				'showNavArrows': false,
				'type': 'iframe',
				'padding': 4,
				'onClosed': function(){
					CartInfoQuery();
				}
			});
		}
		if (oPanelPoolSpecialPopup.length > 0 ) {
			oPanelPoolSpecialPopup.fancybox({
				'scrolling': 'auto',
				'width': 565,
				'height': 980,
				'autoScale': true,
				'overlayColor': '#000',
				'overlayOpacity': 0.9,
				'titleShow': false,
				'showCloseButton': true,
				'showNavArrows': false,
				'type': 'iframe',
				'padding': 4,
				'onClosed': function(){
					CartInfoQuery();
				}
			});
		}
		if (oBACCartForm.length > 0 && oBACMiniCartPopper.length > 0) {
			oBACMiniCartPopper.fancybox({
				'scrolling': 'auto',
				'width': 650,
				'height': 980,
				'autoScale': true,
				'overlayColor': '#000',
				'overlayOpacity': 0.9,
				'titleShow': false,
				'showCloseButton': true,
				'showNavArrows': false,
				'type': 'iframe',
				'padding': 4,
				'onClosed': function(){
								CartInfoQuery();
							}
			});
			oBACCartForm.submit(function() {
				BACHandleFormSubmit();
				return false;
			});
			if( oAddToCarts.length > 0 ){
				oAddToCarts.each(function(){
					$(this).click(function(){
						var sku = $(this).attr("rel");
						BACHandleFormSubmit( sku );
						return false;
					});
				});
			}
		}
		
	}

	if(oPanelGroups.length > 0){
		oPanelGroups.each(function(){
			$(this).bind("change",function(){
				var part_id = $(this).attr("rel");
				var sku = $(this).val();
				var ary = eval('src_'+part_id);
				$("#addbtn_"+part_id).attr("rel",sku);
				$("#img_"+part_id).attr("src",ary[sku]['src']);
			});
		});
	}
					
}

function SpecialsInitialize( ){
	var oPopUpLinks = $('.popup_link');
	oPopUpLinks.each(function(){
		$(this).fancybox({
			'scrolling': 'auto',
			'width': 655,
			'height': 980,
			'autoScale': true,
			'overlayColor': '#000',
			'overlayOpacity': 0.9,
			'titleShow': false,
			'showCloseButton': true,
			'showNavArrows': false,
			'type': 'iframe',
			'padding': 4
		});
	});
}

var oCartInfoQty;
var oCartInfoTotal;

var sCartInfoQty = undefined;
var sCartInfoTotal = undefined;
function CartInfoInitialize(){
	oCartInfoQty = $('#cartInfoQty');
	oCartInfoTotal = $('#cartInfoTotal');
}

function CartInfoUpdate( cartInfoQty , cartInfoTotal ){
	if(cartInfoQty != undefined && cartInfoQty.length > 0){
		sCartInfoQty = cartInfoQty;
	}
	if(cartInfoTotal != undefined && cartInfoTotal.length > 0){
		sCartInfoTotal = cartInfoTotal;
	}
	
	if(sCartInfoQty != undefined && sCartInfoTotal != undefined){
		oCartInfoQty.html(sCartInfoQty);
		oCartInfoTotal.html(sCartInfoTotal);
	}
}

function CartInfoQuery(){
	var sPostData = {
			'fsk' : sBACFSK,
			'action' : 'cart',
			'cart_action' : 'addTo',
			'json' : ''
	}
	
	$.ajax({
		url: sCartUrl,
		type: 'post',
		data: sPostData,
		success: function(data, textStatus, XMLHttpRequest) {
			if( data['result'] == "sucess"){
				CartInfoUpdate( data['cart_info']['qty'] , data['cart_info']['total'] );				
			}
  		},
  		error: function(XMLHttpRequest, textStatus, errorThrown) {},
		dataType: "json"
	});	
}

