if (typeof SHOOT == 'undefined') SHOOT = {};

// ------------------------------------------
// Animated news banner
// ------------------------------------------
SHOOT.banner = {

	t: null, // timer

	d: 1500, // start delay

	s: 50, // animation speed (low = slow)

	l: 0, // left position of banner container

	init: function(){

		// resize the banner container to width of copy and add a duplicate of that copy
		$("#banner-copy1 li").each(function(){
			if($(this).hasClass("bd")) $(this).append(' • ');
			SHOOT.banner.l += $(this).outerWidth(true);
		});

		$("#banner-window").append('<ul id="banner-copy2" class="banner">'+$("#banner-copy1").html()+'</ul>');
		$("ul.banner").width(this.l);
		$("#banner-window").width( (this.l * 2) ) ;

		// start the animation after a civilised pause
		this.t = setTimeout("SHOOT.banner.reset()",this.d);
	},

	// start animation
	animate: function() {
		$("#banner-window").animate({
			"left": "-"+this.l+"px"
		}, this.a, 'linear', function(){
			SHOOT.banner.reset()
		});
	},

	// pause the animation
	pause: function() {
		$("#banner-window").stop();
	},

	resume: function() {

		// recalucalate animation duration
		pos = this.l - (parseInt($("#banner-window").css("left")) * -1);
		this.a = Math.ceil(pos / this.s) * 1000;

		// and start again
		this.animate();
	},

	reset: function() {

		// ensure we're at the beginning
		this.move(0);

		// calucalate animation duration
		this.a = Math.ceil(this.l / this.s) * 1000;

		// go
		this.animate();
	},

	move: function(pos) {
		$("#banner-window").css({
			left: pos+"px"
		});
	}

}

// ------------------------------------------
// Message display
// ------------------------------------------
SHOOT.messenger = function(msg) {
	$.growlUI(msg);
}

// ------------------------------------------
// Rating class
// ------------------------------------------
SHOOT.rating = {

	// entry rating
	erate: function(path) {

		bits = path.split('/');

		$.ajax({
			type: "GET",
			url: path,
			success: function(msg){
				if(msg.substring(0,1) != '1') {
					SHOOT.messenger(msg);
					return;
				}
				SHOOT.messenger('Thanks for rating');
				$("#rating_score").load("/site/rating_score/"+bits[3]);
			}
		});

	},

	// comment rating
	crate: function(el) {

		this.frm = el.closest("form");
		r = (el.text() == "Down") ? -1 : 1;

		// set the rating value
		this.frm.find("input[name='rating']").val(r);

		// submit the request
		var r = false;
		$.ajax({
			type: "POST",
			url: "/",
			data: this.frm.serialize(),
			success: function(msg){
				r = msg.substring(0,1);
				if(r == '2') {
					SHOOT.messenger("You can only change your mind 3 times");
					return;
				} else if(r != '1') {
					SHOOT.messenger("Could not apply your rating");
					return;
				}

				// submit rating
				cid = SHOOT.rating.frm.find("input[name='comment_id']").val();
				$("#crating_score"+cid).html(msg.substring(2));
				SHOOT.messenger("Thanks for rating");
			}
		});
	}

}

// ------------------------------------------
// Calendar class
// ------------------------------------------
SHOOT.calendar = {

	load: function(type) {
		$("#sn-calendar").load('/news/calendar/'+type);
	},

	nav: function(h) {
		$("#sn-calendar").block().load(h,function(){$("#sn-calendar").unblock()})
	}
}


$(function() {

	// ------------------------------------------
	// Interaction handlers
	// ------------------------------------------
	isIE = $.browser.msie;
	isIE6 = (isIE && $.browser.version < 7);

	// news banner
	SHOOT.banner.init();

	$("#banner-window").hover(
		function(){ SHOOT.banner.pause() },
		function(){ SHOOT.banner.resume() }
	)

	// external Links
	$("a[rel='external']").click(function(){
		window.open($(this).attr("href"));
		return false;
	})

	// hover states for block elements
	$("li.size1").hover(
		function(){ $(this).addClass("size1-over"); },
		function(){ $(this).removeClass("size1-over"); }
	);

	$("p.size2").hover(
		function(){ $(this).addClass("size2-over"); },
		function(){ $(this).removeClass("size2-over"); }
	);

	$("p.size3").hover(
		function(){ $(this).addClass("size3-over"); },
		function(){  $(this).removeClass("size3-over"); }
	);

	$("p.size4").hover(
		function(){ $(this).addClass("size4-over"); },
		function(){ $(this).removeClass("size4-over"); }
	);

	// mouseover for anchors
	$("li.abutton a, p.abutton a, p.abutton button").mouseover(
		function(){

			if(isIE6) return;

			var a = $(this);
			d = a.width();
			if(isIE) {
				s = 0;
				d  =(a.parent().hasClass("size1")) ? d - 30 : d - 20;
				v = 200;
			} else {
				s = -30;
				v = 300;
			}

			$(this).css({
				backgroundPosition:s+"px center"
			}).animate(
			{
				backgroundPosition: d+"px center"
				},
			v,
			"linear",
			function(){
				a.css( {
					backgroundPosition:"-30px center"
				} );
			}
			);
		}
		);


	// add to favourites
	$("a.favourites-add").click( function(){
		$.get($(this).attr("href"), function(data){
			SHOOT.messenger(data);
		});
		return false;
	});

	// entry rating
	$("a.rate-interactive").click(function(){
		SHOOT.rating.erate($(this).attr("href"));
		return false;
	}).hover(
		function(){$(this).addClass("rate-over")},
		function(){$(this).removeClass("rate-over")}
	);

	// comment rating
	$(".crate-interactive").click(function(){
		SHOOT.rating.crate($(this));
	}).hover(
		function(){$(this).addClass("crate-over")},
		function(){$(this).removeClass("crate-over")}
	);

	// calendar
	$("#sn-calendar a.calendarMonthLinks").live("click",function(e){
		e.preventDefault();
		SHOOT.calendar.nav($(this).attr("href"));
	})


	// ------------------------------------------
	// sifr
	// ------------------------------------------
	if(typeof sIFR == "function"){

		$("#content h1, h2.hd").each(function(){
			$(this).html('<span>'+$(this).text()+'</span>');
		})

		sIFR.replaceElement(named({
			sSelector:"#content h1 span",
			sWmode: "transparent",
			sFlashSrc:"/assets/flash/capture_it.swf",
			sColor:"#FFFFFF",
			sLinkColor:"#000000"
		}));

		sIFR.replaceElement(named({
			sSelector:"h2.hd span",
			sWmode: "transparent",
			sFlashSrc:"/assets/flash/capture_it.swf",
			sColor:"#FFFFFF",
			sLinkColor:"#000000"
		}));
	};

	// ------------------------------------------
	// Content box header shadows
	// ------------------------------------------
	$("#content-wrap div.cb-tw, #navcolumn div.sn-stretch").each(function(){
		$(this).prepend('<div class="cb-shad"></div>');
	})

	// ------------------------------------------
	// curvy corners
	// ------------------------------------------
	if (isIE) {

		$("#content-wrap, .floating-text").corner({
			tl: { radius: 20 },
			tr: { radius: 20 },
			bl: { radius: 20 },
			br: { radius: 20 }
		}).before('<div class="kludge"></div>').after('<div class="kludge"></div>'); // !!! beats header and footer markup !!!

	}

	// ------------------------------------------
	// Shootube thumbs
	// ------------------------------------------
	$("#content-pad div.st-thumb img:not(.no-overlay)").each(function(){
		$(this).css({background: "#000 url("+$(this).attr("src")+") center center no-repeat"}).attr({src:"/assets/images/shootube_theme/play-button.png"});
	})

})
