/* Javascript for Sound Trends */

var navTimer;
var homeTimer;

function openNav() {
	if (!$('#n_apps').hasClass('expanded')) {
		$('#n_apps').addClass('expanded');
		$('#apps_nav ul li a').fadeIn('slow');
	}
}

function closeNav() {
	if ($('#n_apps').hasClass('expanded')) {
		$('#apps_nav ul li a').fadeOut('fast');
		$('#n_apps').removeClass('expanded');
	}
}



$(document).ready(function() {
	$('#n_apps a').bind('click',function() {openNav();});
	$('#n_apps').bind('mouseleave',function() {
		navTimer = window.setTimeout("closeNav()",350);
	});
	$('#n_apps').bind('mouseenter',function() {window.clearTimeout(navTimer);});
	
	$('#tab1 a').bind('click',function() {
		$('#panel1').addClass('active');
		$('#panel2').removeClass('active');
		$('#panel3').removeClass('active');
		$('#tab1').addClass('active');
		$('#tab2').removeClass('active');
		$('#tab3').removeClass('active');
		return false;
	});
	
	$('#tab2 a').bind('click',function() {
		$('#panel2').addClass('active');
		$('#panel1').removeClass('active');
		$('#panel3').removeClass('active');
		$('#tab2').addClass('active');
		$('#tab1').removeClass('active');
		$('#tab3').removeClass('active');
		return false;
	});
	
	$('#tab3 a').bind('click',function() {
		$('#panel3').addClass('active');
		$('#panel2').removeClass('active');
		$('#panel1').removeClass('active');
		$('#tab3').addClass('active');
		$('#tab2').removeClass('active');
		$('#tab1').removeClass('active');
		return false;
	});
	
	$('.watch-video').bind('click', function() {
		$('#ss_shroud').show();
		$('#ss_box').show();
		$('#ss_content').load($(this).attr('href'));
		return false;
	});
	
	$('.listen-audio').bind('click', function() {
		$('#ss_shroud').show();
		$('#ss_audio').show();
		$('#ss_audio_content').load($(this).attr('href'));
		return false;
	});
	
	/*
	$('#n_contact a').bind('click',function() {
		$('#ss_shroud').show();
		$('#ss_box').show();
		$('#ss_content').load($(this).attr('href'));
		return false;
	});
	*/
	
	this.$shroud = $('<div id="ss_shroud"></div>').hide();
	$('body').append(this.$shroud);
	this.$box = $('<div id="ss_box"><a href="#" id="ss_close" class="noem"><em>Close</em></a><div id="ss_content"></div></div>').hide();
	$('body').append(this.$box);
	this.$audio = $('<div id="ss_audio"><a href="#" id="ss_close" class="noem"><em>Close</em></a><div id="ss_audio_content"></div></div>');
	$('body').append(this.$audio);
	
	$('#ss_shroud, #ss_close').bind('click',function() {
		$('#ss_box').fadeOut('fast');
		$('#ss_audio').fadeOut('fast');
		$('#ss_content').html('');
		$('#ss_shroud').hide();
		return false;
	});
	
	$('select.styled').bind('change',function() {
		if ($(this).attr('value') != 'null') {
			window.location.href = $(this).attr('value')+'?support=yes';
		}
	});
	
	var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
	
	$('.tweet').each(function() {
		$(this).html($(this).html().replace(regexp,"<a href=\"$1\" target=\"_blank\">$1</a>"));
	});
	
	$('#bottomnav_twitter p.big').each(function() {
		$(this).html($(this).html().replace(regexp,"<a href=\"$1\" target=\"_blank\">$1</a>"));
	});
	
	var twit_open = false;
	$('.link_twitter a').bind('click',function() {
		if (!twit_open) {
			$('#bottomnav_carousel').hide();
			$('#bottomnav_twitter').show();
			$('.link_twitter a').addClass('active');
			twit_open = true;
			return false;
		}
		else {
			$('#bottomnav_twitter').hide();
			$('#bottomnav_carousel').show();
			$('.link_twitter a').removeClass('active');
			twit_open = false;
			return false;
		}
	});
	
	if ($('#home-hero').length) {
		homeTimer = window.setInterval("$.galleria.next();",15000);
	}
	
	$('#home-hero').hover(function() {window.clearTimeout(homeTimer);}, function() { homeTimer = window.setInterval("$.galleria.next();",15000); });
	
	positionBox();
});

function positionBox() {
	ww = $(window).width();
	wh = $(window).height();
	bx = Math.round((ww / 2) - ($('#ss_box').width() /2));
	by = Math.round((wh / 2) - ($('#ss_box').height() /2) -100);
	ax = Math.round((ww / 2) - ($('#ss_audio').width() /2));
	ay = Math.round((wh / 2) - ($('#ss_audio').height() /2) -50);
	$('#ss_box').css({'left':bx,'top':by});
	$('#ss_audio').css({'left':ax,'top':ay});
}
