$(document).ready(function(){
	
	// Remove no-js Class
	if (!$.browser.msie) {
		$('body').removeClass('no-js');
	}
	
	// Custom Scroll
	$('#event-scroll .event:last-child').addClass('last');
	var eventScrollL = $('.event').length+1;
	var eventW = $('.event').outerWidth();
	$('#event-scroll .scroller').width(eventScrollL*eventW-50);
	
	$('#event-scroll').jScrollPane({
		showArrows: true,
		horizontalDragMinWidth: 104,
		horizontalDragMaxWidth: 104,
		arrowButtonSpeed : 332
	});
	
	$('#calendar-scroll').jScrollPane({
		showArrows: true,
		verticalDragMinHeight: 27,
		verticalDragMaxHeight: 27
	});
	
	$('#twitter-scroll').jScrollPane({
		showArrows: true,
		verticalDragMinHeight: 27,
		verticalDragMaxHeight: 27,
		autoReinitialise: true
	});
	
	$('#videos-list').jScrollPane({
		showArrows: true,
		verticalDragMinHeight: 75,
		verticalDragMaxHeight: 75
	});
	
	// Navigation fades
	if (!$.browser.msie) {
		$('nav a').append('<span></span>');	
		$('nav li').hover(function(){		
			$(this).find('a span').stop().animate({'opacity': 1}, 400);
		}, function(){
			$(this).find('a span').stop().animate({'opacity': 0}, 400);
		});
	}
	
	// Recent News Marquee
	$('marquee').marquee('marq').mouseover(function () {
            $(this).trigger('stop');
        }).mouseout(function () {
            $(this).trigger('start');
	});
	
	$('.marq').css("height","20px");
	
	// Footer Fade
	$('footer li').hover(function(){
		$(this).siblings().stop().animate({'opacity':.35}, 400);
	}, function(){
		$(this).siblings().stop().animate({'opacity':1}, 400);
	});
	
	// Event Fade	
	$('div.event').hover(function(){
		$(this).siblings().stop().animate({'opacity':.35}, 400);
	}, function(){
		$(this).siblings().stop().animate({'opacity':1}, 400);
	});
	
	// Media Switch
	var recVid = $('#recent-video-tab');
	var recGal = $('#recent-galleries-tab');
	
	recVid.click(function(){
		$(this).css({'z-index': '3', 'border':'none', 'height': '26px'});
		recGal.css({'z-index': '2', 'border-bottom': '1px solid #000', 'height': '25px'});
		$('#recent-galleries').fadeOut(400);
		setTimeout(function(){
			$('#recent-video').fadeIn(400);
		}, 400);
		return false;
	});
	
	recGal.click(function(){
		$(this).css({'z-index': '3', 'border':'none', 'height': '26px'});
		recVid.css({'z-index': '2', 'border-bottom': '1px solid #000', 'height': '25px'});
		$('#recent-video').fadeOut(400);
		setTimeout(function(){
			$('#recent-galleries').fadeIn(400);
		}, 400);
		return false;
	});
	
	// Colorbox
	/*$("a[rel='recent-gallery']").colorbox({
		opacity: .5,
		current: "{current} / {total}"
	});
	
	$("a[rel='single-gallery']").colorbox({
		opacity: .5,
		current: "{current} / {total}"
	});*/
	
	// Shadowbox	
	Shadowbox.init({
		overlayOpacity: .65,
		continuous: true,
		resizeDuration: .15
	});
	
	// Email List Form
	$('#join-email-list input[type="text"]').each(function(){			  
		if(!$(this).val()){
			this.value = $(this).attr('title');
		}
		$(this).focus(function(){
			if(this.value == $(this).attr('title')) {
				this.value = '';
			}
		});
		$(this).blur(function(){
			if(this.value == '') {
				this.value = $(this).attr('title');
			}
		});
	});
	
	// News Page 
	$('article').eq(-1).addClass('last');	
	
	// Events Page
	var easeDown = 'easeOutBack';
	var easeUp = 'easeOutExpo'
	if (!$.browser.msie){
		$('li.event .more-info').click(function(){
			if(!$(this).hasClass('eventShown')){
				$(this).addClass('eventShown');
				$(this).parent().siblings().stop().animate({'opacity':.15}, 400, easeUp);
				$(this).parent().stop().animate({'opacity':1}, 400);
				$(this).parent().siblings().find('.more-info').css({'background-position': 'top left'}).removeClass('eventShown');;
				$(this).css({'background-position': 'bottom left'});
				$(this).parent().siblings().find('.more-details').stop(false,true).slideUp(400, easeUp);
				$(this).parent().find('.more-details').stop(false,true).slideDown(400, easeDown);
				return false;
			}
			if($(this).hasClass('eventShown')){
				$(this).removeClass('eventShown');
				$(this).parent().siblings().stop().animate({'opacity':1}, 400);
				$(this).parent().stop().animate({'opacity':1}, 400);
				$(this).parent().siblings().find('.more-info').css({'background-position': 'top left'}).removeClass('eventShown');
				$(this).css({'background-position': 'top left'});
				$(this).parent().siblings().find('.more-details').stop(false,true).slideUp(400, easeUp);
				$(this).parent().find('.more-details').stop(false,true).slideUp(400, easeDown);
				return false;
			}
		});
	}
	
	if ($.browser.msie){
		$('li.event .more-info').click(function(){
			if(!$(this).hasClass('eventShown')){
				$(this).addClass('eventShown');
				$(this).parent().siblings().find('.more-info').css({'background-position': 'top left'}).removeClass('eventShown');;
				$(this).css({'background-position': 'bottom left'});
				$(this).parent().siblings().find('.more-details').hide();
				$(this).parent().find('.more-details').show();
				return false;
			}
			if($(this).hasClass('eventShown')){
				$(this).removeClass('eventShown');
				$(this).parent().siblings().find('.more-info').css({'background-position': 'top left'}).removeClass('eventShown');
				$(this).css({'background-position': 'top left'});
				$(this).parent().siblings().find('.more-details').hide();
				$(this).parent().find('.more-details').hide();
				return false;
			}
		});
	}
	
	// Gallery Page
	
		// Add .last
		$('#featured-galleries li:last-child').addClass('last');
		$('#all-galleries li:nth-child(3n)').addClass('last');
		$('#single-gallery li:nth-child(7n)').addClass('last');
		
		// Gal Hover
		
		$('#featured-galleries li .info, #all-galleries li .info').css('opacity',.75);
		
		$('#featured-galleries li').hover(function(){
			$(this).find('.info').stop().animate({'height':'252px', 'opacity':.9}, 400);	
		}, function(){
			$(this).find('.info').stop().animate({'height':'20px', 'opacity':.75}, 400, 'easeInOutExpo');
		});
		
		$('#all-galleries li').hover(function(){
			$(this).find('.info').stop().animate({'height':'165px', 'opacity':.9}, 400);	
		}, function(){
			$(this).find('.info').stop().animate({'height':'20px', 'opacity':.75}, 400, 'easeInOutExpo');
		});
		
		$('#single-gallery li').hover(function(){
			$(this).siblings().stop().animate({'opacity': .5}, 400);
		}, function(){
			$(this).siblings().stop().animate({'opacity': 1}, 400);
		});
		
	// Videos Page
	
		// Add .last
		$('#featured-videos li:last-child').addClass('last');
		$('#all-videos li:nth-child(3n)').addClass('last');
		
		// Videos Hover
		
		$('#featured-videos li .info, #all-videos li .info').css('opacity',.75);
		
		$('#featured-videos li').hover(function(){
			$(this).find('.info').stop().animate({'height':'252px', 'opacity':.9}, 400);	
		}, function(){
			$(this).find('.info').stop().animate({'height':'20px', 'opacity':.75}, 400, 'easeInOutExpo');
		});
		
		$('#all-videos li').hover(function(){
			$(this).find('.info').stop().animate({'height':'165px', 'opacity':.9}, 400);	
		}, function(){
			$(this).find('.info').stop().animate({'height':'20px', 'opacity':.75}, 400, 'easeInOutExpo');
		});
		
	// Nivo Slider
	
	$('#slider').nivoSlider();
	
	/*=====================================================*/
	/* Ad Rotation
	/*=====================================================*/
	var featureAds = function(){
		var adSpeed = 600;
		var adEasing = 'linear';	
		
		var largeCount = $('#large-ad-inner a').length;
		var largeHeight = $('#large-ad').height();	
		var largeRotate = function(){		
			$('#large-ad-inner').animate({'top' : '-='+largeHeight+'px'}, adSpeed, adEasing, function(){
				$('#large-ad-inner').css({'top' : '0'});
				$('#large-ad-inner a').eq(0).appendTo('#large-ad-inner');
			});			
		}
		
		var smallCount = $('#small-ad-inner a').length;
		var smallHeight = $('#small-ad').height();
		$('#small-ad-inner').css('height', smallCount*smallHeight+'px');
		var smallRotate = function(){		
			$('#small-ad-inner').animate({'top' : '-='+smallHeight+'px'}, adSpeed, adEasing, function(){
				$('#small-ad-inner').css({'top' : '0'});
				$('#small-ad-inner a').eq(0).appendTo('#small-ad-inner');
			});			
		}	
	
		if(smallCount > 1){
			smallRotate();
		}
		
		if(largeCount > 1){
			largeRotate();
		}
	}
	
	setInterval(featureAds, 5000);
	
	
	
	/*=====================================================*/
	/* Single Line Overflow Marquee
	/*=====================================================*/
		$('.cal-event a').each(function(){
			$(this).wrapInner('<span><span></span></span>');
		});
		var titleMover;
		$('.cal-event a').live('mouseover',function(){
			var $this = $(this);
			var calcWidth = $this.find('span').width();
			var title = $this.find('span').find('span');
			titleMover = setInterval(function(){
				if(parseInt(title.css('left')) > (title.width()-calcWidth)*-1){
					title.stop().css({'left' : '-=1.5px'});
				}
			}, 25);		
		});
		
		$('.cal-event a').live('mouseout',function(){
			clearInterval(titleMover);
			var title = $(this).find('span').find('span');
			title.stop().animate({'left' : '0'}, 500);
		});
	
	/*================================================================================*/
	/* Artist Twitter Feed
	/*================================================================================*/
	if($('#home').length == 1){		
		$.ajaxSetup({ cache: false });		
		$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=triaddragons&count=10&include_rts=true&callback=?',function(data){
			if(data.length >= 1){
				$.each(data, function(i, tweet) {			
					if(tweet.text !== undefined) {
						var formattedTweet = formatTwitString(tweet.text);		
						var tweet_html = '<li class="tweet"><p>'+formattedTweet+'</p> <a href="http://twitter.com/triaddragons/status/'+tweet.id_str+'" target="_blank" class="timestamp">'+H(tweet.created_at)+'</a></li>';		
						$('#twitter-scroll .jspPane').append(tweet_html);					
					}
				});
			} 
		}); 
	}	
		
	/*================================================================================*/
	/* Tweet Date Formatting
	/*================================================================================*/
	var K=function(){var a=navigator.userAgent;return{ie:a.match(/MSIE\s([^;]*)/)}}();
	var H=function(a){var b=new Date();
	var c=new Date(a);if(K.ie){c=Date.parse(a.replace(/( \+)/,' UTC$1'))}
	var d=b-c;
	var e=1000,minute=e*60,hour=minute*60,day=hour*24,week=day*7;
	if(isNaN(d)||d<0){return""}
	if(d<e*7){return"Posted: Right Now"}
	if(d<minute){return"Posted: "+Math.floor(d/e)+" Seconds Ago"}
	if(d<minute*2){return"Posted: About 1 Minute Ago"}
	if(d<hour){return"Posted: "+Math.floor(d/minute)+" Minutes Ago"}
	if(d<hour*2){return"Posted: About 1 Hour Ago"}
	if(d<day){return"Posted: "+Math.floor(d/hour)+" Hours Ago"}
	if(d>day&&d<day*2){return"Posted: Yesterday"}
	if(d<day*365){return"Posted: "+Math.floor(d/day)+" Days Ago"}else{return"Posted: Over a Year Ago"}};
	
	/*================================================================================*/
	/* Twitter Link Setup
	/*================================================================================*/	
	function formatTwitString(str){
		str=' '+str;
		str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
		str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
		str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
		return str;
	}
	
});
