if(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend = {
	// datamembers
	debug: false,
	// init, something like a constructor
	init: function() {
		jsFrontend.forms.init();
		jsFrontend.general.init();
		jsFrontend.advertisement.init();
	},
	// end
	_eoo: true
}

jsFrontend.forms = {
	// datamembers
	buttonHTML: '<a href="#" class="button buttonSubmit"><b>&nbsp;</b><span>{value}</span><i>&nbsp;</i></a>',
	// init, something like a constructor
	init: function() {
		jsFrontend.forms.submitWithLink();
	},
	// load form stuff
	submitWithLink: function() {
		if($('.submitWithLink').length > 0) {
			$('form.submitWithLink').each(function() {
				// get id
				var formId = $(this).attr('id');
				
				// validate id
				if(formId != '') {
					// loop every button to be replaced
					$('form#'+ formId + '.submitWithLink input:submit').each(function() { 
						$(this).after(jsFrontend.forms.buttonHTML.replace('{value}', $(this).val()))
								.css({position:'absolute', top:'-2000px'})
								.attr('tabindex', -1); 
					});

					// add onclick event for button (button can't have the name submit)
					$('form#'+ formId + ' a.buttonSubmit').bind('click', function(evt) {
						evt.preventDefault();
						$('form#'+ formId).submit();
					});
				}
			});
		}
	},
	// end
	_eoo: true		
}

jsFrontend.general = {
	// datamembers
 	debug: false,
 	
	init: function() {
		if($('#humanized-login-box').length > 0) {
//			jsFrontend.general.hookOverlay(true);
			$('#humanized-login-box').show();
			jsFrontend.general.modalPositioning('#humanized-login-box');

			$('#humanized-login-box').fadeIn(500);

			$('#humanized-login-box .modal-box-inner').bind('click', function(evt) { evt.stopPropagation(); });

			$(document.body).bind('click', function(evt) {
				$('#humanized-login-box').fadeOut(500);
//				jsFrontend.general.hookOverlay(false);
			});
			$(document.body).bind('keypress', function(evt) {
				if (evt.keyCode == 27) {
					$('#humanized-login-box').fadeOut(500);
//					jsFrontend.general.hookOverlay(false);
				}
			});
		}
	},
	
	hookOverlay : function(show) {

		if($('#overlay').length == 0) $('#container').prepend('<div id="overlay">&nbsp;</div>');
		$('#overlay').height($(document).height());

		// Persistent overlay opacity for IE6
		$('#overlay').css("filter", "alpha(opacity=40)");
		
		if(show) $('#overlay').fadeIn(500);
		else $('#overlay').fadeOut(250);

	},

	modalPositioning: function(element) {

		// Box positioning code
		
		// Explicitly declare the width and height of modal boxes
		$(element).css('width', '');
		$(element).css('height', '');
		var modalWidth = $(element).width();
		$(element).css('width', modalWidth);
		var modalHeight = $(element).height();
		$(element).css('height', modalHeight);

		// little hack to position the modalbox for IE
		jQuery.browser.msie6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object");
		if(jQuery.browser.msie6 == true) {
			// we add an offset to position the modalbox
			$(element).css('top',  ($(window).height() - $(element).height()) / 2 + parseInt(document.documentElement.scrollTop));

			// Since the modal boxes are positioned ABSOLUTE the left value has to be calculated from the width of the first RELATIVE element
			// Assuming the first relative element is #container and has 940px width
			$(element).css('left', $('#container').width() / 2 - $(element).width() / 2);

		} else {
			$(element).css('top',  ($(window).height() - $(element).height()) / 2);
			$(element).css('left', ($(window).width() - $(element).width()) / 2);
		}
	},

	hookHumanizedBoxes: function() {
		jsFrontend.general.modalPositioning('.humanized-box');

		$('#cart-continue-again').bind('click', function(evt) {
			evt.preventDefault();
			$('.humanized-box').fadeOut(750);
			// unbind all removal hooks
			$('#cart-continue-again').unbind('click');
			$(document).unbind('keypress');
			$(document).unbind('click');
		});
		$(document).bind('keypress', function(evt) {
			$('.humanized-box').fadeOut(750);
			// unbind all removal hooks
			$('#cart-continue-again').unbind('click');
			$(document).unbind('keypress');
			$(document).unbind('click');
		});
		$(document).bind('click', function(evt) {
			// check if we clicked inside humanized box
			var outsideHumanized = false;
			$('.humanized-box').each(function() {
				if (evt.clientX < $(this).offset().left
					|| evt.clientY < $(this).offset().top
					|| evt.clientX > $(this).offset().left + $(this).width()
					|| evt.clientY > $(this).offset().top + $(this).height()) outsideHumanized = true;
			});
			if (outsideHumanized)
			{
				$('.humanized-box').fadeOut(750);
				// unbind all removal hooks
				$('#cart-continue-again').unbind('click');
				$(document).unbind('keypress');
				$(document).unbind('click');
			}
		});
	},

	// end of object
	_eoo: true
}

jsFrontend.advertisement =
{
	init: function()
	{
		if($('#advertisement').length > 0 && $('#advertisementOverlay').length > 0)
		{
			
			var $advertisement = $('#advertisement'),
				$advertisementInner = $('#advertisement .inner'),
				$advertisementContent = $('#advertisementContent'),
				windowWidth = $(window).width(),
				windowHeight = $(window).height(),
				left = windowWidth / 2 - $advertisement.outerWidth() / 2,
				top = windowHeight / 2 - $advertisement.outerHeight() / 2;
			
			$advertisement.show();
			$advertisement.fadeIn(500);
			
			$advertisement.css({'marginLeft' : left, 
								'marginTop' : top})
			
			$(document.body).bind('click', function(evt) {
				$('#advertisementOverlay').fadeOut(500);
			});
			$(document.body).bind('keypress', function(evt) {
				if (evt.keyCode == 27) {
					$('#advertisementOverlay').fadeOut(500);
				}
			});
			
			$('a#advertisementClose, a#advertisementGoToSite').bind('click', function ()
			{
				$('#advertisementOverlay').fadeOut(500);
			});
		}
	},
	
	// end of object
	_eoo: true
}

$(document).ready(function() { 
	jsFrontend.init(); 
});
