var activePopupId;
var activePopupShade;
var activePopupReposition = true;
var activeHintId;

function showPopup(popupId, shade) {
	hidePopup();
	activePopupId = popupId;
	activePopupShade = shade;
	var popupElement = $(popupId);
	if(popupElement) {
		if(shade) {
			showShade();
		}
		popupElement.setStyles({
			'top': 0,
			'left': 0
		})
		popupElement.removeClass('no-display');
		popupReposition();
	}
}

function showAjaxPopup(popupId, shade) {
	var popupElement = $(popupId);
	if(popupElement) {
		popupElement.empty();
		var headline = new Element('div', {
			'class': 'headline'
		}).inject(popupElement);
		new Element('a', {
			'href': '#',
			'class': 'btn-close',
			'text': 'close',
			'events': {
				'click': function(event) {
					event.stop();
					hidePopup();
				}
			}
		}).inject(headline);
		var holder = new Element('div', {
			'class': 'holder empty-holder'
		}).inject(popupElement);
		new Element('div', {
			'class': 'ajax-loader'
		}).inject(holder);
		showPopup(popupId, shade);
	}
}

function showHint(hintId, top, left) {
	hideHint();
	activeHintId = hintId;
	var hintElement = $(activeHintId);
	if(hintElement) {
		hintElement.removeClass('no-display');
		hintReposition(top, left);
	}
}

function showAjaxHint(hintId, top, left) {
	var hintElement = $(hintId);
	if(hintElement) {
		hintElement.empty();
		new Element('div', {
			'class': 't'
		}).inject(hintElement);
		var content = new Element('div', {
			'class': 'c empty-c'
		}).inject(hintElement);
		new Element('div', {
			'class': 'b'
		}).inject(hintElement);
		new Element('div', {
			'class': 'ajax-loader'
		}).inject(content);
		showHint(hintId, top, left);
	}
}

function showAjaxPopupHint(hintId, top, left) {
	var hintElement = $(hintId);
	if(hintElement) {
		hintElement.empty();
		new Element('div', {
			'class': 't'
		}).inject(hintElement);
		var content = new Element('div', {
			'class': 'center empty-holder'
		}).inject(hintElement);
		new Element('div', {
			'class': 'b'
		}).inject(hintElement);
		new Element('div', {
			'class': 'ajax-loader'
		}).inject(content);
		showHint(hintId, top, left);
	}
}

function hideHint() {
	if(activeHintId) {
		var hintElement = $(activeHintId);
		if(hintElement) {
			hintElement.addClass('no-display');
		}
	}
}

function hidePopup() {
	if(activePopupId) {
		var popupElement = $(activePopupId);
		if(popupElement) {
			popupElement.addClass('no-display');
			popupElement.setStyles({
				'top': 0,
				'left': 0
			});
			if(activePopupShade) {
				hideShade();
			}
			activePopupReposition = true;
		}
	}
}

function hintReposition(top, left) {
	if(activeHintId) {
		var hintElement = $(activeHintId);
		if(hintElement && !hintElement.hasClass('no-display')) {
			hintElement.setStyles({
				'top': top,
				'left': left
			})
		}
	}
}

function popupReposition() {
	if(activePopupId && activePopupReposition) {
		var popupElement = $(activePopupId);
		if(popupElement && !popupElement.hasClass('no-display')) {
			var popupCoords = popupElement.getCoordinates();
			var delta = getWidth() - popupCoords.width;
			if(delta > 0) {
				var left = Math.round(delta / 2);
			}
			else {
				var left = 0;
			}
			var delta = getHeight() - popupCoords.height;
			if(delta > 0) {
				var top = Math.round(delta / 2) + getScrollTop();
			}
			else {
				var top = getScrollTop();
			}
			//popupElement.setStyles({'left': left, 'top': top});
			popupElement.set('morph', {
				duration: 100
			})
			popupElement.morph({'left': left, 'top': top});
		}
	}
}

function initPopupClose(popupCloseId) {
	var popupCloseElement = $(popupCloseId);
	if(popupCloseElement) {
		popupCloseElement.addEvent('click', function(event) {
			event.stop();
			hidePopup();
		})
	}
}

function showShade() {
	var shadeElement = $('shade');
	if(shadeElement) {
		if(Browser.Engine.trident4) {
			$$('select').setStyle('display', 'none');
		}
		shadeElement.removeClass('no-display');
	}
}

function hideShade() {
	var shadeElement = $('shade');
	if(shadeElement) {
		if(Browser.Engine.trident4) {
			$$('select').setStyle('display', 'block');
		}
		shadeElement.addClass('no-display');
	}
}

window.addEvent('domready', function() {
	if (navigator.userAgent.indexOf('Safari') == -1 || navigator.userAgent.indexOf('Chrome') != -1){
		var shadeElement = $('shade');
		if(shadeElement) {
			shadeElement.addEvent('click', function() {
				hidePopup();
			})
		}
	}
})

window.addEvent('scroll', function() {
	//popupReposition();
})

window.addEvent('resize', function() {
	popupReposition();
})

function displayError (varError) {
		$('errorMessage').innerHTML=varError;
		showPopup('errorPopUp', false);
		initPopupClose('errorPopUpClose');
}

function displayEmailtoFriend (idProduct) {
		var theurl ="http://www.gretchy.com/emailtofriend.asp?idProduct="+idProduct+"&action=display";
		var req = new Request({url:theurl, 
				onFailure: function() {
					displayError("Failure (Accept Trade).");
				},
				onSuccess:function(results) {
					$('emailBody').value=results;
				}
			});
			
			req.send();
			
		showPopup('email-friend-popup', false);
		initPopupClose('email-friend-popup-close');
}

function displayFriendtoEmailBlog (blogID) {
		var theurl ="http://www.gretchy.com/emailtofriend.asp?blogID="+blogID+"&action=display";
		var req = new Request({url:theurl, 
				onFailure: function() {
					displayError("Failure (Accept Trade).");
				},
				onSuccess:function(results) {
					$('emailBody').value=results;
				}
			});
			
			req.send();
			
		showPopup('email-friend-popup', false);
		initPopupClose('email-friend-popup-close');
}

