/*------------------------------------------------------------------
[Global Javascript]

Copyright: Adam Foster, Codefor 2010
Client: IGM
Project: The Indie Games Magazine
Created by: Adam Foster
Date created: 16 - 04 - 10
-------------------------------------------------------------------*/

$j = jQuery.noConflict();

/* When dom is ready ----------------------------------------------*/

$j(document).ready(function(){
	init();
	hoverStates();
	inputValueReplace();
	postScroller();
});

/* Init -----------------------------------------------------------*/

function init(){
	$j('body').addClass('js');
	$j('a[rel="external"]').attr("target", "_blank");	
};

/* Nav hover states -----------------------------------------------*/

function hoverStates() {
	
	$j('#nav li:not(.current_page_item):not(.current_page_parent)').find('span')
		.css({ 'display':'block', 'opacity':0})
		.end()
		.hover(
		function(){
			$j(this).find('span').stop().animate({opacity:"1"}, 150);
		},
		function(){
			$j(this).find('span').stop().animate({opacity:"0"}, 250);
		}
	);

};

/* Input replace --------------------------------------------------*/

function inputValueReplace() {
  
  $j('input.valueReplace').each(function() { var o = new valueReplace($j(this)); });
	function valueReplace(e) {
		var o = {
		  init : function() {
				o.field = $j(e); 
				o.isSet = false;
				o.defaultValue = o.field.val();
				o.monitor();
			},
			monitor : function() {
				o.field.focus(function(){
					if( $j(this)[0].value == o.defaultValue) { $j(this)[0].value = ''; }
				});
				o.field.blur(function(){
					if($j(this)[0].value == '') { $j(this)[0].value = o.defaultValue;}
				});
			}
		};
		o.init();
	};

}; 

/* News scroller --------------------------------------------------*/

function postScroller() {
	
	$j('#news-pag li').each(function() { var o = new scrollMe($j(this)); });
	function scrollMe(e) {
		var o = {
		  init : function() {
				o.trigger = $j(e).children('a'); 
				o.monitor();
			},
			monitor : function() {
				o.trigger.click(function(e){
					e.preventDefault();
					$j('#postScollerContent').stop().animate({'top': - parseInt(o.trigger.attr('rel')) * 402}, 600);
					$j('#news-pag').children('.selected').removeClass('selected');
					o.trigger.parent('li').addClass('selected');
				});
			}
		};
		o.init();
	};
	
};

/* home feature --------------------------------------------------*/

function homeFeature(){
	
	$j('#featureBox').each(function() { var o = new featureBox($j(this)); });
	function featureBox(e) {
		var o = {
		  init : function() {
				o.box = $j(e); 
				o.box.append("<ul id='featureThumbs'></ul>");
				o.features = o.box.children('.featurePost');
				o.thumbBox = $j('#featureThumbs', o.box);
				o.setup();
				o.monitor();
			},
			setup : function() {
				o.features.each(function(){
					tHTML = "<li><a class='thumb' href='#" + $j(this).attr('id') + "'><img width='151' height='84' src='" + $j(this).find('a').attr('rel') + "'/>";
					if($j(this).find('span').hasClass('insiderFeature')) tHTML += "<span class='insider'></span>";
					tHTML += "<span class='hover'></span></a></li>";
					o.thumbBox.append(tHTML);						 
				});
				o.thumbBox.children('li:first').addClass('selected');
				o.box.children('#feature3').show().addClass('selected');
			},
			monitor : function() {
				o.thumbBox.children('li').click(function(e){
					e.preventDefault();
					$j('.featurePost.selected', o.box).fadeOut('slow').removeClass('selected');
					$j($j(this).children('a').attr('href')).fadeIn('slow').addClass('selected');
					$j('.selected', o.thumbBox).removeClass('selected').find('.hover').fadeOut();
					$j(this).addClass('selected');
				}).hover( function(){
					$j(this).find('.hover').stop().fadeTo("slow", 1);	
					}, function(){
					if( !$j(this).hasClass('selected') ) $j(this).find('.hover').stop().fadeTo("slow", 0.0);	
				});
				o.box.hover( function(){
					o.features.find('.featureOverlay').stop().animate({'bottom': 0}, 250);	
					}, function(){
					o.features.find('.featureOverlay').stop().animate({'bottom': -60}, 400);	
				});
			}
		};
		o.init();	
	};
	
};