if (typeof(FoxyDomain) == 'undefined') {
	var FoxyDomain = "stairbox.foxycart.com";
}

var popOutMargins = 200;
var windowWidth;
var windowHeight;	

$(document).ready(function(){
	// don't load the cart if we're on the cart
	if (location.href.substring(location.href.lastIndexOf('/')+1,location.href.lastIndexOf('/')+5) != "cart") {
		fc_UpdateCart(FoxyDomain); 
		// overide the default submit for Foxy Cart forms
		$('form.foxycart').submit(function(){ 
			MyFoxyData = $(this).serialize(false);
			MyFoxyID = $(this).attr('id');
			if (MyFoxyData.match("cart=checkout") || MyFoxyData.match("redirect=")) {
				location.href = "https://" + FoxyDomain + "/cart.php?" + MyFoxyData + fc_AddSession();
				fc_UpdateCart(FoxyDomain);
			}
			if(location.href.substring(location.href.lastIndexOf('/')-11).match(/stair_parts/)){
				popOut('cart', "https://" + FoxyDomain + "/cart.php?" + MyFoxyData + fc_AddSession(), 'hidden');
				fc_UpdateCart(FoxyDomain);
				return false;
			}
			else{
				popOut('cart', "https://" + FoxyDomain + "/cart.php?" + MyFoxyData + fc_AddSession());
				//popOut('cart', "https://" + FoxyDomain + "/cart.php?" + MyFoxyData + fc_AddSession(), "hidden");
				//popOut('cart');
			}
			return false;
		});
	}
	
	
	//popOut triggers below
	$(".popOut").click(function () {popOut('2D');});
	$(".popOutCart").click(function () {popOut('cart');});
	$(".popBackCart").click(function () {popBack('cart');});
	$(".popBack").click(function () {popBack('2D');});
	//canvas sizes
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	$('#securityCanvas').attr("width", windowWidth - popOutMargins);
	$('#securityCanvas').attr("height", windowHeight - popOutMargins);
	$('#popOutCanvas').attr("width", windowWidth - popOutMargins);
	$('#popOutCanvas').attr("height", windowHeight - popOutMargins);
	$('#popOut, #popOut3D, #popOutCart').width(windowWidth - popOutMargins);
	$('#popOut, #popOut3D, #popOutCart').height(windowHeight - popOutMargins);
	$('#popOut, #popOut3D, #popOutCart').css('position', 'absolute');
	$('#popOut, #popOut3D, #popOutCart').css('top', -windowHeight + 'px');
	$('#popOut, #popOut3D, #popOutCart').css('left', popOutMargins / 2 + 'px');
	$('#overlay').width(windowWidth);
	$('#overlay').height(windowHeight);
	$('#overlay').hide();
	$('#overlay').css('opacity', 0);
	
	
});



/*start of stairbox own code mostly popouts*/ 
function popOut (which, url, pop) {
	if(which == '2D'){
		animatedPopOut = $('#popOut');
	}
	if(which == 'cart'){
		animatedPopOut = $('#popOutCart');
		if(url == null){
			url = 'https://stairbox.foxycart.com/cart?cart=view';
		}
		$('#cartIframe').html("<iframe name=\"iframecart\" width='" + (windowWidth - popOutMargins) + "' height='" + (windowHeight - popOutMargins) + " style='position:relative; z-index:2;' src='" + url + "'></iframe>");
		if(pop == 'hidden'){
			return;
		}
	}
	if(which == '3D'){
		animatedPopOut = $('#popOut3D');
	}
	animatedPopOut.expose({
		api: true,
		color: "#2f4f4f",
		onLoad: function() {
			//go ten pixels too far for "bounce" effect
			animatedPopOut.animate({
				'top': popOutMargins / 2 + 12
			}, 1500 );
			//bounce back up
			animatedPopOut.animate({
				'top': popOutMargins / 2
			}, 300 );
		},
		onBeforeClose: function() {
			//bounce down 10 px
			animatedPopOut.animate({
				'top': popOutMargins/2 + 12
			}, 150 );
			//dissapear
			animatedPopOut.animate({
				'top': -windowHeight
			}, 1500 );
		}
	}).load();
}
function popBack(which){
	if(which == '2D'){
		animatedPopOut = $('#popOut');
	}
	if(which == 'cart'){
		animatedPopOut = $('#popOutCart');
		fc_UpdateCart(FoxyDomain);
	}
	if(which == '3D'){
		animatedPopOut = $('#popOut3D');
	}
	animatedPopOut.expose({api:true}).close();
}
/*
function popOut (which, url, pop) {
	if(which == '2D'){
		animatedPopOut = $('#popOut');
	}
	if(which == 'cart'){
		animatedPopOut = $('#popOutCart');
		if(url == null){
			url = 'https://stairbox.foxycart.com/cart?cart=view';
		}
		$('#cartIframe').html("<iframe name=\"iframecart\" width='" + (windowWidth - popOutMargins) + "' height='" + (windowHeight - popOutMargins) + " style='position:relative; z-index:2;' src='" + url + "'></iframe>");
		if(pop == 'hidden'){
			return;
		}
	}
	if(which == '3D'){
		animatedPopOut = $('#popOut3D');
	}
	animatedPopOut.expose({closeOnEsc: false, closeOnClick: false,api: true}).load();
	//go ten pixels too far for "bounce" effect
	animatedPopOut.animate({
		'top': popOutMargins / 2 + 12
	}, 1500 );
	//bounce back up
	animatedPopOut.animate({
		'top': popOutMargins / 2
	}, 300 );
	
}
function popBack (which) {
	if(which == '2D'){
		animatedPopOut = $('#popOut');
	}
	if(which == 'cart'){
		animatedPopOut = $('#popOutCart');
		fc_UpdateCart(FoxyDomain);
	}
	if(which == '3D'){
		animatedPopOut = $('#popOut3D');
	}
	//bounce down 10 px
	animatedPopOut.animate({
		'top': popOutMargins/2 + 12
	}, 150 );
	//dissapear
	animatedPopOut.animate({
		'top': -windowHeight
	}, 1500 );
	animatedPopOut.expose({api:true}).close();
}
*/
/*end of stairbox own code*/


function fc_FoxyJSON(json) {
	// make this json object available outside the script
	fc_json = json;
	// hide #fc_minicart if product_count = 0. css() rather than hide() for jQuery+Safari
	(json.product_count > 0) ? jQuery("#fc_minicart").show() : jQuery("#fc_minicart").css("display","none");
	// update values
	jQuery("#fc_quantity").html("" + json.product_count);
	jQuery("#fc_total_price").html("" + fc_CurrencyFormatted(json.total_price));
	
	// if they defined fc_BuildFoxyCart(), Build the Cart
	if (typeof(fc_BuildFoxyCart) == 'function') { 
		fc_BuildFoxyCart();
	}
};
function fc_UpdateCart(StoreDomain) {
	jQuery.getScript('https://' + StoreDomain + '/cart.php?cart=get&callback=fc_FoxyJSON&noCacheIE=' + (new Date()).getTime() + fc_AddSession());
};

function fc_AddSession() {
	sessionName = "fcsid";
	sessionString = "";
	fc_regex = /#fcsid=([A-Za-z0-9]*)/;
	if (fc_regex.test(window.location.href)) { // check for the session in the URL, overwrite any existing session
		fc_match = fc_regex.exec(window.location.href);
		// reset the cookie
		sessionString = fc_match[1];
		fc_CookieSet(sessionName, sessionString);
	} else if (document.cookie.indexOf(sessionName + "=") > -1) { // retrieve the session from the cookie
		c_start=document.cookie.indexOf(sessionName + "=");
		if (c_start != -1) { 
			c_start = c_start + sessionName.length + 1; 
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			sessionString = unescape(document.cookie.substring(c_start,c_end));
		}
	} else if (fc_json) { // retrieve the session from the JSON, set a cookie
		// set a cookie for next time
		sessionString = fc_json.session_id;
		fc_CookieSet(sessionName, sessionString);
	}
	if (sessionString != "") {
		return "&fcsid=" + sessionString;
	} else {
		return "";
	}
};
function fc_CookieSet(sessionName, sessionString) {
	// get this domain with no subdomain
	domain = window.location.href.split('/');
	domain = domain[2];
	pos1 = domain.lastIndexOf(".");
	pos2 = domain.lastIndexOf(".",pos1-1);
	if (pos2 > 1) {
	    domain = domain.substr(pos2);
	} else {
	    domain = "." + domain;
	}
	document.cookie = sessionName + "=" + escape(sessionString) + ";path=/;domain="+domain;
	return true;
}
function fc_CookieFix() {
	jQuery('a:not([href*=fcsid])').filter('[href*=' + FoxyDomain + ']').each(function(){
		jQuery(this).attr('href', jQuery(this).attr('href') + fc_AddSession());
	});
}
function fc_CurrencyFormatted(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;
}


var fc_json; // your JSON object including your current cart data
var fc_FoxyCart; // string to hold your cart
var fc_scriptCounter = 0; // counter used when adding dynamic script tags to update the json object 
