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) $('body').prepend('<div id="overlay"></div>');

		$('#overlay').height($(document).height());
		
		// Persistent overlay opacity for IE6
		$('#overlay').css("filter", "alpha(opacity=40)");

		if(show) $('#overlay').fadeIn(500);
		else $('#overlay').hide();
	},

	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]);
			}
			
			$(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);
				}
			});
		});
	},
	
	hookHumanizedBoxes: function() {
		$(document).bind('mousemove keypress', function(evt) {
			$('.humanizedBox').fadeOut(750);
			$(document).unbind('mousemove keypress');
		});
	},

	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() {
	
	/*
		Browse articles
		Enhance search box
	*/
	
	$('#searchTerm').click(function(evt) {
		$(this).select();
	});

	/*
		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");
	});

	/*
		Widgets
	*/
		/* Widget mute/unmute */
		/* Enhance widget links w/ bigger click area on any page except home */
		
		if ( $('body').attr("id") != 'home') { 
		
			$('#widgets li')
				.bind("mouseover focus", function(evt) {
					$(this).removeClass('mute');
				})
				.bind("mouseout blur", function(evt) {
					$(this).addClass('mute');
				})
				.bind("click", function(evt) {
					var link = $(this).find("a").attr('href');
					window.location = link;
				});
		
		} else {
			$('#widgets li').removeClass('mute');
			
			$('#widgets div div').bind("click", function(evt) {
				var link = $(this).parent().find("a").attr('href');
				window.location = link;
			});
			
		}
	
	/*
		Hover class for articles
	*/
	$('#articles li').bind("mouseover focus", function(evt) {
			$(this).addClass('hover');
		})
		.bind("mouseout blur", function(evt) {
			$(this).removeClass('hover');
		});

	/*
		CSS3
		@todo move this to IE6 and IE7 only, performance hit
		Or fix it server side ( = better)
	*/

	$('.listView li:last-child').addClass("last-child");
	$('table.datagrid #codeRow td:last-child').addClass('last-child');
	$('.filterSelection ul li:last-child a').css("border","none");
	
	$('.lightBoxThumbs li:first-child').addClass("first-child");
	$('#container .lightBoxThumbs li:first-child').addClass("first-child");
	$('#templates li:last-child').addClass('last-child');
	
	/*
		Fix modal boxes height
		Crappy scripting by Johan
		@todo Find a way to do this for everything at once...
	*/
	
	bodyHeight = $('body').height();
	
	/* 
		Since IE6 uses position absolute instead of position fixed for the modal box,
		we depend on the window height instead of the body height
		
		This is not perfect but there doesn't seem to be any development time left :X
	*/
	
	// This uses feature detection instead of the old browser sniffing
	if(jQuery.support.leadingWhitespace == false) {
		bodyHeight = $(window).height() + document.body.scrollTop;
	}

	modalHeightAdd = $('#addToCart').height();
	topValue = (bodyHeight - modalHeightAdd)/2;
	$('#addToCart').css('top', topValue);

	modalHeightLb = $('#lightbox').height();
	topValue2 = (bodyHeight - modalHeightLb)/2;
	$('#lightbox').css('top', topValue2);

	sendToFriendHeight = $('#sendToFriend').height();
	topValue3 = (bodyHeight - sendToFriendHeight)/2;
	$('#sendToFriend').css('top', topValue3);
	
	humanizedSuccesBoxHeight = $('#humanizedSuccesBox').height();
	topValue4 = (bodyHeight - humanizedSuccesBoxHeight)/2;
	$('#humanizedSuccesBox').css('top', topValue4);

	sendPrivateMessageHeight = $('#sendPrivateMessage').height();
	topValue5 = (bodyHeight - sendPrivateMessageHeight)/2;
	$('#sendPrivateMessage').css('top', topValue5);

	sendPrivateMessageHeight = $('#sendPrivateMessage').height();
	topValue5 = (bodyHeight - sendPrivateMessageHeight)/2;
	$('#sendPrivateMessage').css('top', topValue5);

	saveWishlistHeight = $('#saveWishlist').height();
	topValue6 = (bodyHeight - saveWishlistHeight)/2;
	$('#saveWishlist').css('top', topValue6);

	retrieveWishlistHeight = $('#retrieveWishlist').height();
	topValue7 = (bodyHeight - saveWishlistHeight)/2;
	$('#retrieveWishlist').css('top', topValue7);

	// Fix the a/img problem
	$("#widgets img, #howTo img, #content img").each(function() {
		var parentTag = $(this).parent().get(0).tagName;
		if (parentTag == 'A') {
			$(this).parent().addClass('noborder');
		};
	});
});


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