/*
Javascript for BalmainRugby.com 

Uses jQuery 1.4.4
*/

$(function(){
	br.init();
});

var br = {

	init : function() {
	
		/*
		Global settings
		============================================================ 
		*/
		var settings = {
			extraShortDelay		: 500,
			shortDelay			: 1000,
			mediumDelay			: 3000,
			longDelay			: 8000,
			flickrThumbs		: ".social #flickr li a img"
		};

		/*
		// do this on DOMReady:
		============================================================ 
		*/

		// Tweet.js
		$.getScript( wp_templatePath + "/javascript/jquery.tweet.js", function(){
			$("body.home #content div.social .twitter .tweets").tweet({
				username: 					"balmainrugby",
				avatar_size: 				48,
				count: 						4,
				auto_join_text_default: 	"I said,",
				auto_join_text_ed: 			"I",
				auto_join_text_ing: 		"I was",
				auto_join_text_reply: 		"I replied to",
				auto_join_text_url: 		"I am checking out",
				loading_text: 				"Getting latest tweets...",
				template: 					"{avatar}{join}{text}{time}"
	        });
			$(".home #twitter").slideDown();
		});
		
		// Home page slideshow
		var homeSlideshow = $("body.home .slideshow");
		
		//create scrollable navigation
		$("<a/>", {
			"class": "prev browse left"
		}).appendTo(homeSlideshow);
		$("<a/>", {
			"class": "next browse right"
		}).appendTo(homeSlideshow);
		$("<div/>", {
			"class": "navi"
		}).appendTo(homeSlideshow);

		// resize slides to fit screen width
		resizeSlides("body.home .slideshow .slide");
		$(window).resize(function(){
			resizeSlides("body.home .slideshow .slide");
		});
		
		// init scrollable
		$(homeSlideshow)
			.scrollable({
				items: 			".slides",
				speed: 			1500,
				circular: 		true 
			})
			.navigator()
			.autoscroll(settings.longDelay);
		
		// position scrollable navigation
		$("body.home .slideshow .navi").css("margin-left", function(naviWidth){
			return "-" + parseInt(($(this).width() + parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right")))/2) + "px";
		});

		/*
		// functions
		============================================================ 
		*/
		
		function resizeSlides(slides) {
			$(slides)
				.width($(window).width())
				.parent()
					.fadeIn();
		}

		function initThumbnails(thumbnails) {
			var thumbs = $(thumbnails);
			if (!$.browser.msie){
				thumbs.hover(function() {
					$(this).parent().parent().siblings().fadeTo(settings.extraShortDelay, .35);
					$(this).parent().parent().fadeTo(settings.extraShortDelay, 1);
				}, function(){ });
				thumbs.parent().parent().parent().hover(function() {
				}, function() {
					$(this).children("li").fadeTo(settings.shortDelay, 1);
				});
			}
		}

		// fading hover button behaviour
		function initFadingHovers(fadingElement, fadeOpacity) {
			$(fadingElement)
				.css("opacity", fadeOpacity)
				.hover(function(){
						$(this).fadeTo("fast", 1);
					}, function() {
						$(this).fadeTo("fast", fadeOpacity);
					});
		}

		// create overlay elements - photo gallery overlays
		function createGalleryOverlay(overlayId) {
			var galleryOverlay = $('<div class="overlay" id=' + overlayId + '></div>');
			var galleryPrev = $('<a class="prev">Previous</a>');
			var galleryNext = $('<a class="next">Next</a>');
			var galleryInfo = $('<div class="info"></div>');
			var galleryProgress = $('<img class="progress" />');

			galleryPrev.appendTo(galleryOverlay);
			galleryNext.appendTo(galleryOverlay);
			galleryInfo.appendTo(galleryOverlay);
			galleryProgress
				.attr('src', wp_templatePath + '/images/ajax-loader.gif')
				.appendTo(galleryOverlay);
			galleryOverlay.insertAfter('#wrapper');
		}

	}
}
