$(document).ready(initAll);

function initAll() {
	init_gallery();
	initTabs();
	initSound();
	
	
	//$("#gallery a").lightBox();
}

Timers = Array;

// =TABS
//__________________________________________________

function initTabs() {
	Tabs = $(".tabs").eq(0);
	if( Tabs.length > 0 ) {
		
		Tabs.before("<ul class='tabs_menu'></ul>");
		Tabs.Menu = $(".tabs_menu").eq(0);
		Tabs.Tabs = Tabs.find(".tab");
		
		for( var i=0; i<Tabs.Tabs.length; i++ ) {
			var tab = Tabs.Tabs.eq(i);
			var tab_h = tab.find("h3").eq(0);
			Tabs.Menu.append( "<li><a href='#'>"+tab_h.text()+"</a></li>" );
			tab_h.remove();
		}
		Tabs.Menu_items = Tabs.Menu.find("li");
		if( $('.page_galeria').length < 1 )
			Tabs.Menu_items.eq(1).addClass("double");
		Tabs.Menu_items.click(clickTab);
		
		changeTab(0);
	}
}
function clickTab() {
	num = $(this).index();
	changeTab(num);
	return false;
}
function changeTab(num) {
	Tabs.Tabs.hide();
	Tabs.Tabs.eq(num).show();
	
	Tabs.Menu_items.removeClass("active");
	Tabs.Menu_items.eq(num).addClass("active");
}


// =SOUND
//__________________________________________________

function initSound() {
	Sound = $("#sound");
	Sound.click(clickSound);
}
function clickSound() {
	if( Sound.hasClass("active") ) {
		soundStop();
	} else {
		soundStart();
	}
	return false;
}
function soundStart() {
	Sound.html('<embed src="'+Audio_File+'" autostart="true" hidden="true" loop="true" />');
	Sound.addClass('active');
	$.cookie( "stop_audio", null );
}
function soundStop() {
	Sound.html('');
	Sound.removeClass('active');
	$.cookie( "stop_audio", "true", {expires:7} );
}


// =GALLERY
//__________________________________________________

function init_gallery() {
	Galleries = $("div.gallery");
	if( Galleries.length > 0 ) {
		
		for( var i=0; i<Galleries.length; i++ ) {
			
			var Gallery = Galleries.eq(i);
			Gallery.wrapInner( '<div class="thumbs"><div class="movil"><div class="width"></div></div></div>' );
			Gallery.prepend( '<table border=0 style="width:478px;height:478px;margin:0"><tr><td style="text-align:center; vertical-align: middle; padding:0; border:none"><img class="gallerypic" alt="foto" /></td></tr></table>' );
			Gallery.data( 'changing', false );
			Gallery.timer = null;
			
			var Links = Gallery.find( 'a' );
			Links.click( gallery_click );
			
			gallery_show( i, 0 );
			
			
			// Movement
			var thumbs = Gallery.find( '.thumbs' );
			var width = thumbs.find( '.width' );
			width = width.width();
			
			if( width > 478 ) {
				thumbs.mouseenter( thumbs_start ).mouseleave( thumbs_stop ).mousemove( thumbs_mouse );
			}
		}
	}
}

function thumbs_start(e) {
	
	// Get gallery
	var gallery = $(this).parent();
	var gallery_number = Galleries.index( gallery );
	
	// Detect direction
	var factor = gallery.offset();
	factor = ( e.pageX - factor.left );
	factor = factor / 478;
	
	var dir = ( factor * -2 ) +1;
	
	// Update vars
	gallery.data( 'move', true );
	gallery.data( 'move_dir', dir );
	
	// Move!
	thumbs_move( gallery_number );
}

function thumbs_stop() {
	
	// Get gallery
	var gallery = $(this).parent();
	var gallery_number = Galleries.index( gallery );
	
	// Update vars
	gallery.data( 'move', false );
}

function thumbs_mouse(e) {
	
	//alert( 'move!' );
	// Get gallery
	var gallery = $(this).parent();
	
	// Detect direction
	var factor = gallery.offset();
	factor = ( e.pageX - factor.left );
	factor = factor / 478;
	
	var dir = ( factor * -2 ) +1;
	
	// Update vars
	gallery.data( 'move_dir', dir );
	
}

function thumbs_move( gallery_number ) {
	
	// Get gallery
	var gallery = Galleries.eq( gallery_number );
	var movil = gallery.find( '.movil' ).eq(0);
	var current_left = parseInt( movil.css( 'left' ), 10);
	var new_left = current_left + ( 4 * gallery.data('move_dir') );
	
	var limit1 = gallery.find( '.width' ).eq(0);
	limit1 = 478 - ( limit1.width() + 6 );
	var limit2 = 0;
	
	if( new_left < limit1 )
		new_left = limit1;
	if( new_left > limit2 )
		new_left = limit2
	
	
	//alert( current_left+' || '+limit1+' || '+limit2 );
	
	if( ( gallery.data( 'move' ) == true ) ) {
		movil.css( 'left', new_left );
		setTimeout( function(){ thumbs_move( gallery_number ) }, 20 );
	}
}






function gallery_click() {
	var number = $(this).index();
	var gallery = $(this).parent().parent().parent().parent();
	var gallery_number = Galleries.index( gallery );
	
	var name = 'timer'+gallery_number;
	
	
	if( gallery.data( 'changing' ) == false ) {
		clearTimeout( Timers[name] );
		gallery_show( gallery_number, number );
	}
	
	return false;
}

function gallery_show( gallery_number, photo_number ) {
	
	// Select gallery
	var gallery = Galleries.eq( gallery_number );
	
	if( gallery.data( 'changing' ) == false ) {
		
		gallery.data( 'changing', true );
	
		// Select photo
		var photo_total = gallery.find( 'a' );
		photo_total = photo_total.length;
		
		if( photo_number < 0 )
			photo_number = photo_total-1;
		if( photo_number >= photo_total )
			photo_number = 0;
		
		var photo = gallery.find( 'a' ).eq( photo_number );
		var big_photo = gallery.find( '.gallerypic' );
		
		function fadedIn() {
			var name = 'timer'+gallery_number;
			Timers[name] = setTimeout( function(){ gallery_show(gallery_number,photo_number+1) }, 6000 );
			//alert( gallery.timer );
			gallery.data( 'changing', false );
		}
		
		function fadedOut() {
			// Load new image	
			var img = new Image();
			// Fade in
			img.onload = function() {
				big_photo.attr( 'src', photo.attr( 'href' ) ).fadeTo( '', 1, fadedIn );
			}
			img.src = photo.attr( 'href' );
		}
		
		// Fade out
		big_photo.fadeTo( '', 0, fadedOut );
	}
}
