// Edit
var dispSec = 12;
var domid = "slide";
var dotid = "dot";
var dotcont = "rotdots";
var slidecont = "rotbanner";
var selctloc = "files/imgs/sel_slide.png";
var unselctloc = "files/imgs/dot_slide.png";

// Don't Edit
var slideCount;
var globalSeq = 2;
var myId;
var timeOut = (dispSec*1000)


$(document).ready(function() {
	
	// Fixes a list issue
	$('li').each(function(index) {
		var hasDisc = $(this).html();
		
		if (hasDisc.indexOf("• ") != -1) {
			$(this).html(hasDisc.substr(2));
		}
	});
	
	// Fixes weight on search results
	$("span.weight").each(function(index) {
		var cont = $(this).html();
		
		var subCont = cont.substr(1, (cont.length-2));
			//subCont = subCont.substr(subCont.length-(subCont.length-1));
		
		$(this).html(subCont);
	});
	
	// Reset the #body height to fit
	var bottomOffset = $("#body").offset().top + $("#body").height();
	var offsetDiff = $(document).height() - $("#body").offset().top;
	if (bottomOffset < ($(document).height()-156)) {
		$("#body").height(offsetDiff - 146)
	}
	
	// Slideshow
	if ($('#' + slidecont).length == 1) {
		setupSlideshow('#' + slidecont);
	}
	
	// Tweets
	$("#feed").tweet({
       avatar_size: 32,
       count: 4,
       username: "safeboatcouncil",
       template: "{text}"
    });

	$(this).find("a.tweet_action").click(function(ev) {
   		window.open(this.href, "Retweet", 'menubar=0,resizable=0,width=550,height=420,top=200,left=400');
   		ev.preventDefault();
	});
	
	// Overlay
	var myClose = $("#wearit").overlay({

		// some mask tweaks suitable for modal dialogs
		mask: {
			color: '#FFFFFF',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
});

function closeAll() {
	$("#wearit").overlay().close();
}

function setupSlideshow(container) {
	
	// Lets get the number of slides for today
	slideCount = $(container).children().length;
	
	// Add dot container
	$(container).parent().append('<div id="' + dotcont + '"></div>');
	
	// Add dots
	$("#rotdots").append('<img src="' + selctloc + '" id="' + dotid + '1" onclick="gotoSlide(1);" />'); // First one cause we start at 1
	
	for(i = 2; i <= slideCount; i++) {
		$("#rotdots").append('<img src="' + unselctloc + '" id="' + dotid + i + '" onclick="gotoSlide(' + i + ');" />'); // Lets to this... ;)
	}
	
	myId = window.setTimeout(seq(), timeOut);
}

function gotoSlide(slide) {
	
	clearTimeout(myId);
	
	var i1 = slide;
	var i2 = (globalSeq-1); // Holds the next frame not the current

	var selector1 = "#" + domid + i1;
	var selector2 = "#" + domid + i2;
	
	var nextdot = "#" + dotid + i1;
	var currdot = "#" + dotid + i2;
	
	$(selector1).fadeIn('slow', function() {
		$(selector2).fadeOut('slow');
	});
	
	$(nextdot).attr('src', 'files/imgs/sel_slide.png');
	$(currdot).attr('src', 'files/imgs/dot_slide.png');
	
	globalSeq = (slide+1); // Set to the next frame so we get back on track
	
	myId = setTimeout(seq(), timeOut);
}

function seq() {
	return (function() {
		
		var i1 = nextSlide();
		var i2 = prevSlide();
		
		var selector1 = "#" + domid + i1;
		var selector2 = "#" + domid + i2;
		
		var nextdot = "#" + dotid + i1;
		var currdot = "#" + dotid + i2;
		
		$(selector1).fadeIn('slow', function() {
			$(selector2).fadeOut('slow');
		});
		
		$(nextdot).attr('src', 'files/imgs/sel_slide.png');
		$(currdot).attr('src', 'files/imgs/dot_slide.png');
		
		globalSeq++;
		
		myId = setTimeout(seq(), timeOut);
	});
}

function nextSlide() {
	
	if(globalSeq == (slideCount+1)) {
		globalSeq = 1;
		return globalSeq;
	} else {
		return globalSeq;
	}
}

function prevSlide() {
	
	if(globalSeq == 1) {
		return (slideCount);
	} else {
		return (globalSeq-1);
	}
}
