TSN_BASE_PATH = window.location.toString();
if( TSN_BASE_PATH.indexOf( 'index.php' ) != -1 ) {
	TSN_BASE_PATH = TSN_BASE_PATH.substr( 0,
		TSN_BASE_PATH.indexOf( 'index.php' ) );
}

TSNMediaPath = function( id ) {
	
	// make id a string
	id += '';

	while( id.length < 10 ) {
		id = '0'+id;
	}
		
	var path = '';

	for( i=0; i<=6; i+=2 ) {
		path += id.substr( i, 2 )+'/';
	}

	return path;	
};


TSNSetMessage = function( type, data ) {
	if( !$('container-syserror') ) {
		$(document.body).adopt( new Element( 'div', {
			'id': 'container-syserror'
		}));
	}
	$('container-syserror').empty();
	var container = new Element( 'div', {'class':type} );
	$('container-syserror').adopt( container );
	
	if( typeof data == 'string' ) {
		container.set( 'html', data );
	} else {
		container.adopt( data );
	}
	
	var coords = container.getCoordinates();
	var scroll = window.getCoordinates();
	var leftOffset = (scroll.width/2) - (coords.width / 2 );
	
	$('container-syserror').setStyle( 'left', leftOffset );
	
	timeout = 10000;
	if( arguments[2] ) {
		timeout = arguments[2];
	}
	if( arguments[2] === false ) {
		timeout = false;
	}
	
	// using a timeout to allow any modals to clear before showing message
	if( timeout != false ) {
		setTimeout( function() {
			$('container-syserror').getFirst().fade( 'out' );
		}, timeout );
	}
};

// general
window.addEvent( 'domready', function() {


	$$('input.replace-text').each( function(el) {
		var text = el.value;
		el.store( 'default-text', text );
		
		el.addEvents({
			'focus': function(e) {
				this.setStyle( 'color', '#000' );
				if( this.value == this.retrieve( 'default-text' ) ) {
					this.value = '';
				}
			},
			'blur': function(e) {
				this.setStyle( 'color', '#999' );
				if( this.value == '' ) {
					this.value = this.retrieve( 'default-text' );
				}
			}
		});
	});
});


// comment view
window.addEvent( 'domready', function() {
	
	$$('.comment-full-view').addEvent( 'click', function(e) {

		e.stop();
		
		var url = this.get( 'href' );
		
		var el = this;
		
		el.innerHTML = 'Loading...';
		
		new Request.JSON({
			'method': 'get',
			'url': url+'.json',
			'onComplete': function( data ) {

				if( data.isError ) {
					TSNSetMessage( 'notice', data.message );
					return;
				}
	
				el.getParent().set( 'html', unescape(
					data.comment.comment_formatted ) );
				
			}
		}).send();
	});
	
});

// tabboxes

window.addEvent( 'domready', function() {


	$$('.tab-box').each( function(tabBox) {
		// grab all the title links
		var childLists = tabBox.getChildren();

		var linkContainer = new Element( 'div', {
			'class': 'tab-box-header',
			'styles': {
				'margin': 0,
				'padding': 0
			}
		});
		
		linkContainer.injectBefore( tabBox );
		
		var totalLinks = childLists.length;
		
		var childWidth = 300 / totalLinks;

		childLists.each( function( list, idx ) {
			var link = list.getFirst();

			linkContainer.grab( link );
			link.setStyles({
				'display': 'block',
				'float': 'left',
				'width': childWidth+'px',
				'text-align': 'center'
			});
			
			if( idx > 0 ) {
				list.setStyle( 'display', 'none' );
			} else {
				link.addClass( 'tab-header-active' );
			}
			
			link.addEvent( 'click', function(e) {
				e.stop();
				linkContainer.getChildren().removeClass( 'tab-header-active' );
				this.addClass( 'tab-header-active' );
				childLists.setStyle( 'display', 'none' );
				list.setStyle( 'display', '' );
			});
		
		});
		
	});
});

// podia
window.addEvent( 'domready', function() {

	var podPreviews = $$('#pod_container #pod_previews a img');
	if( !podPreviews.length ) {
		return;
	}
	
	podPreviews[0].addClass( 'selected' );
	
	$('pod_main').set( 'tween', {
		'duration': 100
	});
	
	var arrow = new Element( 'img', {
		'class': 'pod-pointer',
		'src': TSN_BASE_PATH+'style/images/arrow_down_333.png'
	});
	
	$(document.body).adopt( arrow );
	
	
	setTimeout( function() {
		var coords = podPreviews[0].getCoordinates();
		arrow.setStyles({
			'position': 'absolute',
			'top': coords.bottom - 2,
			'left': coords.left + (coords.width/2) - 12
		});
	}, 500 );	
	
	podPreviews.addEvent( 'click', function(e) {
		var id = this.getParent().get( 'rel' );
		if( !id ) {
			return;
		}
		e.stop();
		
		podPreviews.removeClass( 'selected' );
		
		this.addClass( 'selected' );

		var elCoords = this.getCoordinates();
		
		arrow.setStyles({
			'top': elCoords.bottom - 2,
			'left': elCoords.left + (elCoords.width/2) - 12
		});
		
		var coords = $('pod_main').getCoordinates();
		var cover = new Element( 'div', {
			'styles': {
				'background': '#333 url( '+TSN_BASE_PATH+
					'style/images/throbber_333.gif ) no-repeat center center',
				'position': 'absolute',
				'color': '#333',
				'width': coords.width,
				'height': coords.height,
				'top': coords.top,
				'left': coords.left,
				'opacity': 0
			},
			'text': 'Loading'
		});

		$(document.body).adopt( cover );
		cover.set( 'tween', {
			'duration': 100
		});
		
		
		
		cover.tween( 'opacity', 1 );
		$('pod_main').tween( 'opacity', 0 );
//		$('pod_caption').slide( 'out' );
		
		var fbUrl = this.getParent().get( 'href' );

		// load the image data
		new Request.JSON({
			'url': TSN_BASE_PATH+'index.php/media/xinfo.json/'+id,
			'onComplete': function( data ) {

				if( !data.base_name ) {
					cover.destroy();
					return;
				}

				var url = unescape( data.base_name );
//				var caption = unescape( data.caption_formatted );
				var caption = decodeURIComponent( data.caption_formatted );
				
				var authors = [];
				if( data.authors.length ) {
					data.authors.each( function(author) {
						authors.push( unescape( author.name ) );
					});
				}
				
				var author = authors.join( ', ' );
				
				// load the image
				var loader = new Image;
				loader.onload = function() {
					cover.set( 'tween', {
						'onComplete': function() {
							cover.destroy();						
						}
					});
					cover.tween( 'opacity', 0 );
					$('pod_main').set( 'src', url );
					$('pod_main').getParent().set( 'href', fbUrl );
					$('pod_caption').set( 'html', '<span class="small">'+author+' / '+
						unescape( data.source )+'</span>'+caption );
					$('pod_main').tween( 'opacity', 1 );
					$('pod_caption').slide( 'in' );
				};
				loader.src = url;
				
			}
		}).send();

	
	});

});

// mm slider
window.addEvent( 'domready', function() {

	var images = $$('#mmslider a img');
	var parent = $('mmslider');

	if( !images.length ) {
		return;
	}
	
	// set images objects, create previews
	images.each( function(el, idx) {
		var image = $(el);

		image.store( 'order', idx );
		image.set( 'id', 'mmbox-image-'+idx );
		image.addClass( 'mmslider-image-preview' );
		image.setStyle( 'display', 'inline' );

		image.set( 'morph', {
			'duration': 200
		});
		image.set( 'tween', {
			'duration': 200
		});
		
		image.addEvent( 'click', function(e) {
			if( this.hasClass( 'mmslider-image-selected' ) ) {
				return true;
			}
			e.stop();
			clearInterval( interval );
			
			mmslider_setImage( this.retrieve( 'order' ) );
		});
		
 	//	$('mmslider-info').set( 'text', image.get( 'title' ) );
		$('mmslider-info').innerHTML = image.get( 'title' );		

		if( idx >= images.length - 1 ) {
			el.addClass( 'mmslider-image-selected' );
			return;
		}
		
		var rightOffset = (idx * 50) + (idx * 10);

		image.setStyles({
			'width': 50,
			'max-height': 50,
			'position': 'absolute',
			'border': '1px solid #000',
			'bottom': '20px',
			'right': rightOffset+'px',
			'z-index': 10
		});
	});

	var mmslider_setImage = function() {
		var currentImage = $$('.mmslider-image-selected').pop();

		var currentIndex = currentImage.retrieve( 'order' );

		if( arguments[0] == null ) {
			var nextIndex = currentIndex - 1;
			if( nextIndex < 0 ) {
				nextIndex = images.length - 1;
			}
		} else {
			nextIndex = arguments[0];
		}
		
		var nextImage = $('mmbox-image-'+nextIndex);
		
		var rightOffset = 0;

		currentImage.setStyles({
			'width': 50,
			'max-height': 50,
			'position': 'absolute',
			'border': '1px solid #000',
			'bottom': '20px',
			'right': -60,
			'z-index': 10,
			'opacity': 0
		});
		
		currentImage.morph({
			'right': rightOffset,
			'opacity': 1
		});
		
		
		currentImage.removeClass( 'mmslider-image-selected' );

		nextImage.addClass( 'mmslider-image-selected' );
		
		nextImage.setStyles({
			'width': 300,
			'max-height': 150,
			'position': 'relative',
			'border': 0,
			'bottom': 0,
			'right': 0,
			'z-index': 1,
			'opacity': 0
		});
		
		nextImage.tween( 'opacity', 1 );

//		$('mmslider-info').set( 'text', nextImage.get( 'title' ) );
		$('mmslider-info').innerHTML =  nextImage.get( 'title' );
		$$('.mmslider-image-preview').each( function(el) {
			if( el == nextImage || el == currentImage ) {
				return;
			}
			rightOffset += 60;

			el.tween( 'right', rightOffset );
		});	
	};
	
	var interval = setInterval( function() {
		mmslider_setImage();	
	}, 5000 );
});


window.addEvent( 'domready', function() {

if( !$('mmaccordion') ) {
	return;
}

$$('#mmaccordion .info').setStyle( 'opacity', 0.9 );

var accordion = new Accordion( $$('#mmaccordion-controls > span'),
	$$('#mmaccordion > div'), {
		'onActive': function( tg, el ) {
			$$('#mmaccordion-controls span').removeClass( 'selected' );
			tg.addClass( 'selected' );
		}
	}
);

var currentIndex = 0;
var sliderInterval = setInterval( function() {
	
	currentIndex++;
	if( currentIndex >=  $$('#mmaccordion > div').length ) {
		currentIndex = 0;
	}
	
	accordion.display( currentIndex );
	
}, 5000 );

$$('#mmaccordion-controls span').addEvent( 'click', function() {
	if( sliderInterval ) {
		clearInterval( sliderInterval );
		sliderInterval = false;
	}
	
	var idx = parseInt( this.get( 'text' ) );
	
	accordion.display( idx-1 );
});


});
