if(typeof $ == 'undefined') { var $; }

var to_thumbnails, int_thumbsscroll, currentImage, currentGallery = false;

function startup(src) {
  'use strict';
	var albums = $(src.albums[0].childs.albums);
		
	// generate main menu
	$('#mainmenu').empty();
	albums.each(function() {
		var data = this;
		$('#mainmenu').append(
			$('<li><a href="#'+this.title+'">'+this.title+'</a></li>').click(
				function() { loadMainGallery(data.childs); }
			)
		);
	});
	
	
	// load Gallery
	loadGallery(src.albums[0]);
	
	// show/hide navigation elements on mousemove
	int_thumbsscroll = false;
	$(document).mousemove(function (e) {
		if(int_thumbsscroll) { clearInterval(int_thumbsscroll); }
		int_thumbsscroll = setInterval(hideThumbs, 3000);
		showThumbs();
	});
	
	// control thumbnail bars
	$('#bu_prevthumbs').click(prevThumbs);
	$('#bu_nextthumbs').click(nextThumbs);
	
	
	// get MP3 Player for External Interface
	player = swfobject.getObjectById("player");	
}



function loadGallery(gallery) {
  'use strict';
	var i=0;
	var items = gallery.childs.items;
	
	
	// set Gallery Label
  currentGallery = gallery;
	if(currentGallery.title == 'startpage') {
	 currentGallery.title = 'Sabrina Hyde';
	}
	$('#gallery_label').html(currentGallery.title);
	
	$('#pagebox').fadeOut(500);
	//$('#gallerybox').stop().fadeOut(500, function() {
	    
	  $('#images .inner').empty();
		$('#thumbnails .inner').empty().css({marginLeft:0});
		
		$(items).each(function() {
			var item = this;
			
			// append Image
			$('#images .inner').append(
				$('<li><a href="'+item[2]+'"></a></li>')
				.css({zIndex:(i===0?(items.length+101):100)+i})
				.addClass(i===0?'loading':'')
			);
			
			// append thumbnail
			$('#thumbnails .inner').append(
				$('<li><a href="'+item[3]+'"></a></li>')
				.css({
					marginLeft:String(111*(i++))+'px',
					zIndex:500+(i),
					opacity:0.5
				})
			);
		});

		$('#gallerybox').stop().fadeIn(500);
	  
    currentImage = false;

    loadThumb(0);
  	loadImage(0);
  	slideshow_start();
  	
  	/*
  	$('#gallery_like').html('<fb:like href="' + 'http://www.sabrina.co.nz#' + currentGallery.title + '" layout="button_count" show_faces="false" width="110" action="like" colorscheme="light" />');
  	if (typeof FB !== "undefined") {
  	    FB.XFBML.parse($('#gallery_like'));
  	}
  	*/
  	
  	setTimeout(function () {
  	 player.playSong(gallery.id);
  	}, 2000);
	
	//});
}


function loadImage(i, speed) {
  'use strict';
	// transition speed
	if(typeof speed == 'undefined') {
		speed = 3000;
	}
	
	if(currentImage && currentImage == i) {
		return;
	}
	
	var li,image, data;
		li = $($('#images .inner li')[i]);
	
	
	
	if(li.length>0) {
		if(currentImage !== false) {
			var prevImage_li = $($('#images .inner li')[currentImage]);
          prevImage_li.find('img').stop();
			li.css({
				zIndex:(Number(prevImage_li.css('z-index'))+1)
			});
		}
		
		// add image if not added yet
		if(li.find('a').length > 0) {
			var a = li.find('a');
			li.addClass('loading');
			image = a.replaceWith(
				$('<img src="'+a.attr('href')+'"/>')
				.css({opacity:0})
				.load(function () {
					$(this).animate({opacity:1}).unbind();
					li.removeClass('loading');
				})
			);
			
		} else {
			image = li.find('img');
			image.css({opacity:0}).animate({opacity:1},speed);
		}
		
		// update Facebook Thumbnail
		$('#og_image').attr('content', image.attr('src'));
		
		// remember remember
		currentImage = i;
		
		// slideshow options
		slideshow_start();
	}
}	

function loadThumb(i) {
  'use strict';
	var thumb = $($('#thumbnails .inner li')[i]);
	
	if(thumb.length>0) {
		// add thumbnail
		thumb.find('a').replaceWith(
			$('<img src="'+thumb.find('a').attr('href')+'"/>')				
			
			.load(function() {
				if(i<10) {
					thumb.animate({opacity:1});
				} else {
					thumb.css({opacity:1});
				}
				thumb.removeClass('loading').unbind('load');
				loadThumb(i+1);
			})
			.click(function () {
				loadImage(i, 300);
			})
		);
	}
}


function loadPage(name) {
  'use strict';
	$('#gallerybox').fadeOut(500);
	
	$('#pagebox').fadeOut(500, function () {
		$.ajax({
		  url: ['/site/pages/'+name+".php"].join(''),
		  success: function(data){
		    $('#pagebox').hide().html(data).fadeIn();
		  }
		});
	});
}


function loadMainGallery(data) {
  'use strict';
	$('#submenu').empty();
	$(data.albums).each(function () {
		var album = this;
		$('#submenu').append(
			$('<li><a href="#'+this.title+'">'+this.title+'</a>')
				.click(function () { loadGallery(album); })
		);
	});
	
	if(data.items.length === 0) {
		data = data.albums[0];
	}
	
	loadGallery(data);
}


function showThumbs () {
'use strict';
	$('#thumbnails, #mainmenu, #submenu, #topmenu, #controls').stop().animate({opacity:1},'fast');
}


function hideThumbs () {
  'use strict';
	to_thumbnails = setTimeout(function() {
		//$('#thumbnails, #mainmenu, #submenu, #topmenu, #controls').stop().animate({opacity:0},2000);
	}, 200);
}


function nextThumbs() {
  'use strict';
	var max = ((($('#thumbnails .inner li').size()-1))*111 - 737)*-1;
	var currpos = parseInt($('#thumbnails .inner').css('margin-Left'),10);
	var newpos = currpos - 670;
	
	if(newpos < max) {
		newpos = max;
	}
	
	$('#thumbnails .inner').animate({marginLeft:(newpos+'px')});
}


function prevThumbs() {
  'use strict';
	var currpos = parseInt($('#thumbnails .inner').css('margin-Left'),10);
	var newpos = currpos + 670;
	
	if (newpos > 0) {
		newpos = 0;
	}
	
	$('#thumbnails .inner').animate({marginLeft:(newpos+'px')});
}	



var tosl = false;
function slideshow_start() {
  'use strict';
	clearTimeout(tosl);
	tosl = setTimeout(function() {
		var currentLi = $($('#thumbnails .inner li')[currentImage + 1]);
		var nextimage = currentLi.length>0 ? currentImage+1 : 0;
		
		loadImage(nextimage, 3000);
		
	}, 7000);
}

function slideshow_stop() {
  'use strict';
	clearTimeout(tosl);
	tosl = false;
}


function updateFacebookLike (url) {
  'use strict';
    url = "http://www.facebook.com/plugins/like.php?href="+url+"&layout=button_count&show_faces=false&width=400&action=like&font=arial&colorscheme=light";
    $('#facebook').attr('src', url);
}
 

