
/** Promo Accordion **/
function sg_accordion() {
	
	var items = $('ul#accordion li');
	var links = $('a', items);
	var duration = 300;
	
	links.click(function() {
		
		var current = items.filter('.open');
		var h = $('div > p',$(this).parent('li')).innerHeight();
		
		current.find('div').animate({height:0},duration).end().removeClass('open');
		$(this).siblings('div').animate({height:h + 'px'},duration).parent('li').addClass('open');
		
		return false;
				
	});
	
}


/** Tab Nav z-indexes **/
function sg_secondaryNav() {
	
	var z = 20;
	
	$('#secondaryNav ul li').each(function() {
		$(this).css('z-index',z);
		z--;
	});
	
	$('#secondaryNav a').mouseover(
		function () { $('span',$(this)).removeAttr('style'); }
	);
	
}

/** Screw Features Header **/
function sg_screwsHeader() {
	
	var trg = $('img#screwFeature');
	
	var trigger = function(s) {
		
		if (s == '#') return false;
		
		trg.hide();
		
		$('<img>').load(function() {
			
			trg.attr('src',s).show();
			
		}).attr('src',s);
		
	}
	
	$('#screwspace #thumbs a').click(
		function() { trigger($(this).attr('href')); return false; }
	).mouseover(
		function() { trigger($(this).attr('href')); });
	
}

/** Gallery Stuff **/
function sg_gallery() {
	
	var wDetail = $('#galleryDetails');
	var wThumbs = $('#gallerySlider');
	var wList = $('#galleryList');
	var wScroll = $('#galleryScroller');
	
	// These should be based on math ratio of content length to scroller available height
	// console.log(outerH / innerH);
	var speed = 14;
	var increment = 10;
	var timer = null;
	var offset = 0;
	
	var maxY = 0;
	var minY = 0;
	var outerH = 0;
	var innerH = 0;
	
	var sliderInit = function() {
		
		wThumbs.css('height',wDetail.height() + 'px');
		wList.css('height',wThumbs.height()-68 + 'px');
		
		outerH = wList.height();
		innerH = wScroll.height();
		
		maxY = Math.round(outerH - innerH);
	
	}
	
	var slideUp = function() {
		
		if (offset >= minY) {	
			slideStop(); 
			return;
		}
		
		offset = Math.round(offset + increment);
		wScroll.css('top', offset + 'px');
	}
	
	var slideDown = function() {

		if (offset <= maxY) {
			slideStop();
			return;
		}
		
		offset = Math.round(offset - increment);
		wScroll.css('top', offset + 'px');
	}
	
	var slideStop = function() {
		clearInterval(timer);	
	}
	
	// Resize and initiate
	sliderInit();
	
	
	// Scroll Triggering
	$('#galleryUp').mousedown(function() {
		sliderInit();
		slideUp();
		timer = setInterval(function() { slideUp(); },speed);				   
	}).mouseup(function() { slideStop(); });
	
	$('#galleryDown').mousedown(function() {
		sliderInit();
		slideDown();
		timer = setInterval(function() { slideDown(); },speed);				   
	}).mouseup(function() { slideStop(); });
	
	
	$('#galleryUp, #galleryDown').click(function() { return false; });
	
	// Trigger each gallery ajax re-load
	$('a.thumb',wList).click(function() {
		
		var ref = $(this).attr('href');
		var loc = 'gallery/details/' + ref.replace('#','') + '.php';							  
		
		$.get(loc, function(data) {
		  wDetail.html(data);
		  $('#galleryImage').load(function() { sliderInit(); });
		});
		
		document.location.hash = ref;
		return false;
		
	});
	
	
	// If there's a hash, load the right page.
	var cs = document.location.hash;
	if (cs.length > 0) {
		
		$('a[href="' + cs + '"]').trigger('click');
		
	}
	
}


function sg_ie6() {
	
	if ($.browser.msie && $.browser.version < 7) {

		// Primary ie6 nav fixins
		$("#nav span.static").append('<img src="images/nav_side.png" width="20" height="135" />');
		
		// Secondary ie6 nav fixins
		$('#secondaryNav span').append('<img src="images/tabs_side.png" width="10" height="99" />');
	
	}
	
}


$(function() {
		   
	sg_ie6();
	
	$('table.fancy').each(function() {
		$('tbody tr:even', $(this)).addClass('even');
	});
		
});
