window.Splendid = {};
window.Splendid.Constants = {};

var isIE6 = ($.browser.msie && parseInt($.browser.version)==6);

if(isIE6 && typeof(DD_belatedPNG)!='undefined') {
	var pngfixes = [
	  'img'
	  , '#newsletter_signup .background'
	  , '.splendid_tv .background'
	  , '.article_body .section li'
	];
	
	DD_belatedPNG.fix(pngfixes.join(','));
}

// repositions the navigation menu bar on the screen
function positionNavigation() {
	if( $(window).scrollTop() > window.Splendid.Constants.HeaderHeight) {
		$('#page_navigation').css({ position:'fixed', top:0, zIndex: 10, width: '100%'  });
	} else {
		$('#page_navigation').css({ position:'static'});
	}
}

function navigationOver() {
	$(this).addClass('active');
}

function navigationOut() {
	$(this).removeClass('active');
}

/*
 * Common Javascript
 */
$(document).ready(function() {
	
	// set useful constants
	window.Splendid.Constants.HeaderHeight = $('#header').height();
	
	// setup generic rollovers
	$('.rollover').hover(function(event) {
		this.src = this.src.replace("_off","_on");
	}, function(event) {
		this.src = this.src.replace("_on","_off");
	});
	
	// setup floating navigation
	var throttleScroll = _.throttle(positionNavigation, 20);
	$(window).scroll(throttleScroll);
	
	// setup the dropdown items in the navigation
	var dropdownsConfig = {
		over: navigationOver,
		out: navigationOut,
		interval: 10,
		timeout: 100
	}
	
	$('#page_navigation td').hoverIntent(dropdownsConfig);
	
	// [rb] fix positioning for IE < 8
	if($.browser.msie && parseInt($.browser.version)<8) {
		$('#page_navigation td .subnavigation').each(function(i,el){
			var ml = 0 - parseInt($(el).parent('td').width() / 2);
			$(el).css('margin-left',ml);
		});
	}
	
	// watermark any newsletter signup fields
	$('#newsletter_signup input[id="name"]').watermark('Name');
	$('#newsletter_signup input[id="email"]').watermark('Email');
	$('#newsletter_signup input[id="postcode"]').watermark('Postcode');
	
	// rollover effect on more link
	$('.article.about .introduction a').css({opacity:0.8}).hover(function(event) {
		$(this).css({opacity:1});
	}, function(event) {
		$(this).css({opacity:0.8});
	});
	
	$('div.article_body div.section').each(function(count, element) {
		var label = $(element).find('h3').html();
		
		var link = $(element).attr('id');
		// [rb] - 	I can be bothered with this cumbersome jQuery approach of manually adding id's
		// 			so if they have none generate a unique id to link to.
		if(link=='') {
			link = '_'+new Date().getTime()+count;
			$(element).attr('id',link);
		}
		if(label && link) {
			$('div.article_header div[class=section_navigation] ul').append('<li><a href="#' + link + '">' + label + '</a></li>');
		}
	});
	
	// do cufon
	Cufon.replace('div.article_header h1');
	
});

/*
 * Homepage
 */
window.Splendid.Homepage = function() {
	var my = {};
	
	my.initialize = function() {
		
		// launch twitter feed plugin
		$("#twitter_feed_output").tweet({
			username: "SplendidArtists",
			join_text: "auto",
			avatar_size: 32,
			count: 3,
			auto_join_text_default: "we said,",
			auto_join_text_ed: "we",
			auto_join_text_ing: "we were",
			auto_join_text_reply: "we replied to",
			auto_join_text_url: "we were checking out",
			loading_text: "loading tweets..."
		});
		
		// make mousing over slides also light up text
		$('.article.latest_news li').hover(function(event) {
			$(this).addClass('active');
		}, function(event) {
			$(this).removeClass('active');
		});
		
		// rollover effect on splendid tv
		$('.article.connect .splendid_tv a').css({opacity:0.8}).hover(function(event) {
			$(this).css({opacity:1});
		}, function(event) {
			$(this).css({opacity:0.8});
		});
		
		// setup splendid sliders
		
		$('.article.feature .slideshow').splendidSlider({
			animationSpeed: 1000,
			transitionInterval: 6000,
			visibleSlides: 1,
			pauseOnHover: false,
			autoScroll: true
		});
		$('.article.latest_news .slideshow').splendidSlider({visibleSlides: 3});
		
	};
	
	return my;
	
}();


/*
 * About Us
 */
window.Splendid.AboutUs = function() {
	var my = {};
	
	my.initialize = function() {
		
	};
	
	return my;
	
}();

/*
 * Blog
 */
window.Splendid.Blog = function() {
	var my = {};
	
	my.initialize = function() {
		
		// watermark search
		$('.article_header input[name=search_term]').watermark('Search the blog');
		
	};
	
	return my;
	
}();
