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

JS_COMMUNITY.general = {
	// datamembers
 	debug: false,
 	
	init: function() {
		JS_COMMUNITY.general.hookModalBoxes();
	},
	
	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);

	},

	hookModalBoxes: function() {

		$('.modal-box-close').bind('click', function(evt) {
			evt.preventDefault();
			var id = $(this).attr('href');
			$(id).hide();
			JS_COMMUNITY.general.hookOverlay(false);
		});

		$('.modal-box-toggle').bind('click', function(evt) {
			var id = $(this).attr('href');
			var dataChunks = Array;
			if ($(this).attr('rel') != '') {
				var dataChunks = $(this).attr('rel').split(':');
			}
			JS_COMMUNITY.general.hookOverlay(true);

			for(i in dataChunks)
			{
				$('#'+ dataChunks[i].split('=')[0]).val(dataChunks[i].split('=')[1]);
			}

			JS_COMMUNITY.general.modalPositioning(id);

			$(id).fadeIn(500);
			evt.stopPropagation();
			evt.preventDefault();

			$(id + ' .modal-box-inner').bind('click', function(evt) { evt.stopPropagation(); });

			$(document.body).bind('click', function(evt) {
				$(id).fadeOut(500);
				JS_COMMUNITY.general.hookOverlay(false);
			});
			$(document.body).bind('keypress', function(evt) {
				if (evt.keyCode == 27) {
					$(id).fadeOut(500);
					JS_COMMUNITY.general.hookOverlay(false);
				}
			});
		});
	},
	
	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() {
		JS_COMMUNITY.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');
			}
		});
	},

	hookSortFilterDropdowns: function() {
		// filterByStat - filterByType
		$('#filterByStat, #filterByType').bind('change', function() {
			window.location = $(this).children('option:selected').attr('rel');
		});
	},
	
 	setExternalFavicons	: function() {
 		$('#externalLinksList a[href^="http://"]').each(function() {
			var faviconIMG = $('<img width="16" height="16" alt="' + $(this).attr('title') + '" title="' + $(this).attr('title') + '" class="externalLinkImage" />').prependTo($(this));
			var extImg = new Image();
			extImg.src = $(this).attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';
			extImg.onload = function() { faviconIMG.attr('src', extImg.src); }
			extImg.onerror = function() { faviconIMG.attr('src', '/modules/core/layout/images/externalLink.gif'); }
			extImg.onabort = function() { faviconIMG.attr('src', '/modules/core/layout/images/externalLink.gif'); }
 		});
	},

	// end of object
	_eoo: true
}


$(document).ready(function() {
	
	/* Add focus class when an input or textarea has focus */
	
	$('input, textarea').bind("focus", function(evt) {
		$(this).addClass("focus");
	});
	$('input, textarea').bind("blur", function(evt) {
		$(this).removeClass("focus");
	});

});


$(document).ready(function() { JS_COMMUNITY.general.init(); });
