function show_main_slider(){
	$("#autoslides").fadeIn();
	$("#productslides").hide();
}

function show_alt_slider(selector){
	$("#autoslides").hide();  // hide and fade wont effect if already faded in
	$("#productslides").fadeIn().children('ul').slideTo(selector); 

}

function play_slider(){
	$('#playpause').attr("class", "pause");
	$('#autoslides ul').cycle('resume');
	$('#playpause').data('paused', false);
}
function pause_slider(){
	$('#playpause').attr("class", "play");
	$('#autoslides ul').cycle('pause');
	$('#playpause').data('paused', true);
}
function slider_paused(){
	return $('#playpause').data('paused');
}

// used on a ul with multiple li, where only one can be seen at a time
// assumes:
// - firstli is currently displayed
// li items are width = 100% and all displayed
// ul has overflow to hidden
$.fn.slideTo = function(target_selector){
	$activating_slide = $(target_selector);
	$current_slide =  this.children('li:first');

	if ($activating_slide[0] == $current_slide[0]){
		return
	}

	this.css({position: 'relative'});
	width = this.width() + 'px';

	$current_slide
	.css({position: 'absolute', top: '0', left: 0})
	.show() // no need to hide later, will be off screen
	.animate({left: width}, 500);

	$activating_slide
	.css({zIndex: 1, position: 'absolute', top: '0', left: '-' + width})
	.remove().prependTo(this)           //set to first element, for next cycle
	.show()
	.animate({left: '0'}, 500, function(){
		$(this).css({zIndex: 0});
	})
}

$(function($){
	// init pause state
	$('#playpause').data('paused', false);

	// main slider
	$("#autoslides ul").cycle({
		fx: 'scrollRight',
		speed: 500,
		pager:   '#currentslide',
		cleartypeNoBg: true,
		timeout: 8000
	});

	$("#playpausecurrent a").click(function(){
		show_main_slider();
		$("#slidedeck li a").removeClass('current');
	});

	// play/pause
	$('#playpause').bind("click", function() { //note: todo: data?
		slider_paused() ? play_slider() : pause_slider();
	});

	// only shown on click
	$("#productslides").hide();

	//manage heading clicks
	$("#slidedeck li a").click(function(){
		$("#slidedeck li a").removeClass('current');
		selector = $(this).addClass('current').attr('href');		
		pause_slider();
		show_alt_slider(selector);
		return false;
	});

	// later on
	$(".tab_content").hide(); 
	$("ul.tabs li:first").addClass("active").show(); 
	$(".tab_content:first").show(); 

	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active");
		$(this).addClass("active");
		$(".tab_content").hide(); 
		var activeTab = $(this).find("a").attr("href"); 
		$(activeTab).fadeIn(); 
		return false;
	});

	$('#overviewslides').cycle({
		fx: 'scrollRight',
		speed: 500,
		pager:   '#currentslide',
		cleartypeNoBg: true,

		timeout: 8000
	});

	$('.anotherslider > ul').cycle({
		fx: 'scrollRight',
		speed: 500,
		prev:   '.anotherslider > .larrow-box', 
		next:   '.anotherslider > .rarrow-box',
		cleartypeNoBg: true,
		timeout: 0
	});

	$('.portboxcontent > .hidden').hide();
	$('.portboxcontent > .moreLink').click(function() {
		$(this).next('.hidden').slideToggle('fast');
		$(this).hide();

		return false
	});

	$('div.fullfaq li> h6').hide();
	$('div.fullfaq li> h2').click(function() {
		$(this).addClass("current");
		$(this).next('h6').slideToggle('fast')
		.siblings('h6:visible').slideUp('fast')
	});


	// nav drop menus	


	var down1top = $(".down1").css("top");	// get initial negative offset	
	$(".drop:eq(0), .down1").hover(function(){
		$(".down1").animate({top: '60px'}, {queue: false});
	}, function(){
		$(".down1").animate({top: down1top}, {queue: false});
	});
	
	var down2top = $(".down2").css("top");
	$(".drop:eq(1), .down2").hover(function(){
		$(".down2").animate({top: '60px'}, {queue: false});
	}, function(){
		$(".down2").animate({top: down2top}, {queue: false});
	});
	
	var down3top = $(".down3").css("top");
	$(".drop:eq(1), .down3").hover(function(){
		$(".down3").animate({top: '42px'}, {queue: false});
	}, function(){
		$(".down3").animate({top: down3top}, {queue: false});
	});

	var down4top = $(".down4").css("top");
	$(".drop:eq(0), .down4").hover(function(){
		$(".down4").animate({top: '42px'}, {queue: false});
	}, function(){
		$(".down4").animate({top: down4top}, {queue: false});
	});

	// manage pulling content from rss feed
//	$.get("feed?type=blogs", function(xml){
//		$list = $("#blogcontent ul");
//		var month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
//		r = new RegExp(/<link>(.*)/);  // for grabbing the link.  For some reason, the </link> disappears and there's no better way.
//		// note also that newline isin't counted by /./
//		
//		jQuery(xml).find("item:lt(3)").each(function(){
//			date = new Date($(this).find("pubDate").text());
//			title = '<h5>' + $(this).find("title").html() + ':<span>' +  month_names[date.getMonth()] + ' ' + date.getDate() + '</span></h5>';
//			link_a = r.exec(this.innerHTML);
//			//$(this).find("link").html()
//			p = '<p>' + $(this).find("description").text().replace(/<[^>^<]+>/g,'').substr(0,110) + '… <a href="'+ link_a[1] + '">more</a></p>';
//			$list.append('<li>' + title + p + '</li>')
//		});
//	}, 'xml');
	
	//pull twitter:
	$('#twittercontent').tweetable({username: 'd2design',time: true, limit: 3});
});


