var currentModal = false;
var currentPhoto = 1;
var currentProduct = 0;
var currentProductPhoto = 1;

window.onscroll = function(e) {
	if (currentModal) {
		bd_displayOverlay("update");
		bd_updateModal();
	}
};

window.onresize = function(e) {
	if (currentModal) {
		bd_displayOverlay("update");
		bd_updateModal();
	}
};

function bd_getNum(pNum) {
	var ret;
	var bool = false;
	
	if (pNum.indexOf("px") > -1) {
		bool = true;
		ret = parseInt(pNum.substring(0, pNum.indexOf("px")), 10);
	} else {
		ret = parseInt(pNum, 10);
	}
	
	return ret;
}

function bd_tweenExecute(pEl, pType, pStartValue, pValueDistance, pTime, pTimeEnd, pCallback) {
	var pCurrentTime = new Date().getTime();
	var pTimeRemaining = Math.max(0, pTimeEnd - pCurrentTime);
	var pCurrentMove;
	
	if (pType == "top" || pType == "right" || pType == "bottom" || pType == "left") {
		pCurrentMove = Math.round((pValueDistance - (Math.pow(pTimeRemaining, 3) / Math.pow(pTime, 3)) * pValueDistance) * 10) / 10;
	} else {
		pCurrentMove = Math.round((pValueDistance - (pTimeRemaining / pTime) * pValueDistance) * 10) / 10;
	}
	
	if (pTimeRemaining <= 0 || pValueDistance > 10) {
		pCurrentMove = parseInt(pCurrentMove, 10);
	}
	
	if (pType != "filter") {
		pEl.style[pType] = (pStartValue + pCurrentMove) + "px";
	} else {
		if (pType == "filter") {
			pEl.style[pType] = "alpha(opacity=" + ((pStartValue + pCurrentMove) * 100) + ")";
		} else {
			pEl.style[pType] = (pStartValue + pCurrentMove);
		}
	}
	
	if (pTimeRemaining > 0) {
		setTimeout(function () {
			bd_tweenExecute(pEl, pType, pStartValue, pValueDistance, pTime, pTimeEnd, pCallback);
		}, 10);
	} else {
		if (typeof(pCallback) == "function") {
			pCallback();
		}
	}
}

function bd_tween(pEl, pType, pNewValue, pTime, pCallback) {
	if (pTime === 0 || typeof(pTime) != "number") {
		pTime = 1;
	}
	
	var pStartValue = bd_getNum(pEl.style[pType]);
	var pValueDistance = pNewValue - pStartValue;
	var pTimeStart = new Date().getTime();
	var pTimeEnd = pTimeStart + pTime;
	
	bd_tweenExecute(pEl, pType, pStartValue, pValueDistance, pTime, pTimeEnd, pCallback);
}

function bd_windowSize() {
	var winWidth = 0,
		winHeight = 0,
		x = 0,
		y = 0;
	
	if (typeof window.innerWidth == "number") {
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}
	
	if (self.pageYOffset) {
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return [winWidth, winHeight, x, y];
}

function bd_displayOverlay(pState) {
	var winSize = bd_windowSize();
	
	if (pState == "on") {
		try {
			document.getElementById("modal_overlay").style.width = winSize[0] + "px";
			document.getElementById("modal_overlay").style.height = winSize[1] + "px";
			document.getElementById("modal_overlay").style.display = "block";
		} catch (e1) {}
	} else if (pState == "off") {
		try {
			document.getElementById("modal_overlay").style.display = "none";
		} catch (e2) {}
	} else {
		try {
			document.getElementById("modal_overlay").style.width = winSize[0] + "px";
			document.getElementById("modal_overlay").style.height = winSize[1] + "px";
		} catch (e3) {}
	}
}

function bd_showModal(pType) {
	currentModal = pType;
	
	var winSize = bd_windowSize();
	
	bd_displayOverlay("on");
	
	try {
		document.getElementById("modal_" + currentModal).style.display = "block";
		
		var offsetLeft = (winSize[0] / 2) - (document.getElementById("modal_" + currentModal).offsetWidth / 2);
		var offsetTop = (winSize[1] / 2) - (document.getElementById("modal_" + currentModal).offsetHeight / 2);
		
		if (offsetLeft <= 0) {
			offsetLeft = 20;
		}
		
		if (offsetTop <= 0) {
			offsetTop = 20;
		}
		
		document.getElementById("modal_" + currentModal).style.left = Math.round(offsetLeft) + "px";
		document.getElementById("modal_" + currentModal).style.top = (Math.round(offsetTop) + winSize[3]) + "px";
	} catch (e) {}
}

function bd_updateModal() {
	var winSize = bd_windowSize();
	
	try {
		var offsetLeft = (winSize[0] / 2) - (document.getElementById("modal_" + currentModal).offsetWidth / 2);
		var offsetTop = (winSize[1] / 2) - (document.getElementById("modal_" + currentModal).offsetHeight / 2);
		
		if (offsetLeft <= 0) {
			offsetLeft = 20;
		}
		
		if (offsetTop <= 0) {
			offsetTop = 20;
		}
		
		document.getElementById("modal_" + currentModal).style.left = Math.round(offsetLeft) + "px";
		document.getElementById("modal_" + currentModal).style.top = (Math.round(offsetTop) + winSize[3]) + "px";
		document.getElementById("modal_" + currentModal).style.display = "block";
	} catch (e1) {}
}

function bd_closeModal() {
	bd_displayOverlay("off");
	
	try {
		document.getElementById("modal_" + currentModal).style.display = "none";
	} catch (e) {}
	
	currentModal = false;
}

function bd_imageHover(pEl, pState, pSrc) {
	if(pState == 0) {
		pEl.src = "http://files.brettdetar.com/assets/img/" + pSrc + ".png";
	} else {
		pEl.src = "http://files.brettdetar.com/assets/img/" + pSrc + "_hover.png";
	}
}

function bd_photoModal(pDir) {
	if (pDir == 0) {
		currentPhoto = currentPhoto - 1;
	} else {
		currentPhoto = currentPhoto + 1;
	}
	
	document.getElementById("modal_photo_image").src = "http://www.brettdetar.com/assets/photo/" + currentPhoto + ".jpg";
	
	if (currentPhoto == 1) {
		document.getElementById("modal_photo_prev").style.visibility = "hidden";
	} else {
		document.getElementById("modal_photo_prev").style.visibility = "visible";
	}
	
	if (currentPhoto == 10) {
		document.getElementById("modal_photo_next").style.visibility = "hidden";
	} else {
		document.getElementById("modal_photo_next").style.visibility = "visible";
	}
}

function bd_openProduct(pId) {
	var pParent = document.getElementById("product_info");
	var pHtml = "";
	var pProduct = false;
	
	for(var i in products) {
		if(products[i].id == pId) {
			currentProduct = products[i];
		}
	}
	
	if(currentProduct == false) { return; }
	
	pHtml += "<h1>" + currentProduct.name + "</h1><h4>" + currentProduct.cost + "</h4>";
	pHtml += currentProduct.description;
	
	if(currentProduct.note.length > 0) {
		pHtml += "<p class='note'>" + currentProduct.note + "</p>";
	}
	
	if(currentProduct.button.length > 0) {
		pHtml += "<div class='buy'>";
		pHtml += currentProduct.button;
		pHtml += "</div>";
	}
	
	pParent.innerHTML = pHtml;
	
	currentProductPhoto = 1;
	
	document.getElementById("modal_product_image").src = "http://www.brettdetar.com/assets/product/" + currentProduct.id + "_" + currentProductPhoto + ".png";
	document.getElementById("modal_product_prev").style.visibility = "hidden";
	
	if(currentProduct.photos > 0) {
		document.getElementById("product_info").style.width = "400px";
		document.getElementById("product_image").style.display = "block";
	} else {
		document.getElementById("product_info").style.width = "900px";
		document.getElementById("product_image").style.display = "none";
	}
	
	if(currentProduct.photos > 1) {
		document.getElementById("modal_product_next").style.visibility = "visible";
	} else {
		document.getElementById("modal_product_next").style.visibility = "hidden";
	}
	
	bd_showModal("product");
	TSPurchase.initializeBuyButtons();
}

function bd_productPhoto(pDir) {
	if (pDir == 0) {
		currentProductPhoto = currentProductPhoto - 1;
	} else {
		currentProductPhoto = currentProductPhoto + 1;
	}
	
	document.getElementById("modal_product_image").src = "http://www.brettdetar.com/assets/product/" + currentProduct.id + "_" + currentProductPhoto + ".png";
	
	if (currentProductPhoto == 1) {
		document.getElementById("modal_product_prev").style.visibility = "hidden";
	} else {
		document.getElementById("modal_product_prev").style.visibility = "visible";
	}
	
	if (currentProductPhoto == currentProduct.photos) {
		document.getElementById("modal_product_next").style.visibility = "hidden";
	} else {
		document.getElementById("modal_product_next").style.visibility = "visible";
	}
}

function rawurlencode(pString) {
	pString = (pString + "").toString();
	return encodeURIComponent(pString).replace(/!/g, '%21').replace(/\$/g, '%24').replace(/&/g, '%26').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/\+/g, '%2B').replace(/,/g, '%2C').replace(/\//g, '%2F').replace(/:/g, '%3A').replace(/;/g, '%3B').replace(/=/g, '%3D').replace(/\?/g, '%3F').replace(/@/g, '%40').replace(/~/g, '%7E')
}
