//<![CDATA[

/* Data for the carousel */

var carouselData = [
	{ "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='img/carousel/1.jpg' alt='1'></a></div>" }, 
	{ "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='img/carousel/2.jpg' alt='2'></a></div>" }, 
	{ "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='img/carousel/3.jpg' alt='3'></a></div>" }, 
	{ "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='img/carousel/4.jpg' alt='4'></a></div>" }, 
	{ "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='img/carousel/5.jpg' alt='5'></a></div>" }, 
	{ "content": "<div class='slide_inner'><a class='photo_link' href='#'><img class='photo' src='img/carousel/6.jpg' alt='6'></a></div>" }
]

/* Document ready */

$(document).ready(function() {

	/* Parameters */
	
	var minTabContentHeight = $("#page_content").height() - 665;										// min content height for the tab is page height - the height of the other elements.
	var userId = '101383694309767621007';																// userId for Picassa.

	/* Page name */
	
	var path = window.location.pathname;
	pageName = (/\/([a-zA-Z]*)\.php/).exec(path);
	if (pageName == null) {
		pageName = "index";
	} else {
		pageName = pageName[1];
	}
	
	/* Carousel */	

	$("#carousel").agile_carousel({
		carousel_data: carouselData,
		carousel_outer_height: 260,
		carousel_height: 260,
		slide_height: 260,
		carousel_outer_width: 860,
		slide_width: 860,
		transition_type: "fade",
		transition_time: 800,
		timer: 5000,
		continuous_scrolling: true,
		control_set_1: "numbered_buttons"
	});
	
	/* Img scaler */
	
	$("div.polaroid_container img").scaleImage();	
	
	/* Sub menu tabs & Slider */
	
	$("#sub_menu_tabs div.container_content").css("min-height", minTabContentHeight + "px");					// Set min content height of the tab area.			

	$("#sub_menu_tabs div.tab_content").hide(); 																// Hide all content.
	$("#sub_menu_tabs div.tab_content:first").show(); 															// Show first tab content by default.
	
	/* Bind an event to window.onhashchange that, when the history state changes: tab changed */
	
	$(window).bind( 'hashchange', function(e) {
		var hash = window.location.hash || '#tab_1';
		
		var newTab = $("#sub_menu_tabs ul li").eq( hash.replace("#tab_", "") - 1 );
		var oldTab = $("#sub_menu_tabs ul li.active");	
		var newTabContent = $(hash);
		
		/* Active class switch */
		oldTab.removeClass("active"); 		
		newTab.addClass("active"); 																				
		
		/* Tab content switch */
		var ajaxLoader = $('#sub_menu_tabs div.ajax-loader');
		
		if (newTabContent.html() == "") {
			$("#sub_menu_tabs div.container_content div.tab_content").hide();
			
			// Load new content.
			$.ajax({																							
				url: 'ajax/' + pageName + '/' + pageName + '_' + hash.replace('#','') + '.php',
				beforeSend: function(xhr) {
					ajaxLoader.show();
				},
				success: function(data){
					var target = $('#sub_menu_tabs div.container_content div.tab_content');
					
					newTabContent.html(data);
					var newTabContentHeight = newTabContent.height();
					var newMinTabHeight = (newTabContentHeight > minTabContentHeight) ? newTabContentHeight : minTabContentHeight;
					$("#sub_menu_tabs div.container_content").css("min-height",  newMinTabHeight  + "px");
									
					newTabContent.find("div.polaroid_container img").scaleImage();
					newTabContent.fadeIn("slow");
					
					ajaxLoader.hide();
				}
			});
		} else {
			var newTabContentHeight = newTabContent.height();
			var newMinTabHeight = (newTabContentHeight > minTabContentHeight) ? newTabContentHeight : minTabContentHeight;
			$("#sub_menu_tabs div.container_content").css("min-height",  newMinTabHeight  + "px");
			$("#sub_menu_tabs div.container_content div.tab_content").hide();
			newTabContent.fadeIn("slow");
		}
		
		/* Slider */
		var slider = $("#sub_menu_tabs div.sub_menu div.slider");
		$(slider).animate({ 
				left: newTab.offset().left - $("#sub_menu_tabs ul").offset().left + newTab.width() / 2 + slider.width() / 2
			},{
				duration: 'slow',
				easing: 'easeOutBack',
				complete: function() {}
			}
		);
	})

	// Since the event is only triggered when the hash changes, we need to trigger the event now, to handle the hash the page may have loaded with.
	$(window).trigger('hashchange');
	
	/* Albums */
	
	$("#galleria").hide();
	$("div.ajax-loader").hide();

	Galleria.loadTheme("js/galleria.classic.js");												

	$("div.album div.polaroid").live('click', function() { 
		// Get img position in the set.
		var polaroidIndex = $(this).parent().find("div.polaroid").index(this);

		// Get album id.
		var albumId = $(this).parents("div.album").attr("id");

		$('#galleria').galleria({
			picasa: 'useralbum:' + userId + '/' + albumId,
			picasaOptions: {
				sort: 'date-posted-asc',
				imageSize: 'big'
			},
			imageCrop: false,
			maxScaleRatio: 1.0,
			fullscreenCrop: false,
			thumbQuality: "auto",
			transition: "fade",
			show: polaroidIndex
		});
		$('#galleria').show();
	});
	
	/* Dialog */
	
    $('.dialog_overlay, .dialog_close').live('click', function () {    
        $(this).parents('.dialog').fadeOut();     
    });
});
//]]>
