/**
 * coding by thomjjames@gmail.com
 */
var faceboxPop; 
var playing;
if (Drupal.jsEnabled) {  
  $(document).ready(function() {
		
		/**
		 * facebox
		 */
		//updateHeightFirst(); 
		$('#album-description').hide();
			
		
		$('a#play').hide();
		$('#main-photo').cycle({ 
			fx:     'myFx',
			speed:  'slow', 
			speedOut:  100,
			timeout: 6000, 
			next:   '#next', 
			prev:   '#prev', 
			pager:  '#thumbnails',
			
				pagerAnchorBuilder: function(idx, slide) { 
						// return selector string for existing anchor 
						return '#thumbnails li:eq(' + idx + ') a'; 
				}		
		});
		playing = true;
		$('#main-photo').css('margin-left', '0px');

		
		$('a#pause').click(function() { 
			$('#main-photo').cycle('pause');
			$(this).hide();
			$('a#play').show();
			playing = false;
			return false;
		});
		$('a#play').click(function() { 
			$('#main-photo').cycle('resume'); 
			$(this).hide();
			$('a#pause').show();
			playing = true;	
			return false;
		});	
		
		/* auto load description */
		description = $('#album-description').html();
		if (description) {
			$(window).bind('resize', function() {
			  curr_left = $(window).width() / 2 - ($('#facebox table').width() / 2);
			  $('#facebox').css('left', curr_left);
			});
			
			$('#content_after').fadeOut();
			$('#main-photo').cycle('pause');
			
			$(document).bind('afterReveal.facebox', function(data, klass) {
			   border = 12;
			   content_top = $("#logo").innerHeight() + 4;
			   facebox_height = $('#main-photo img:eq(0)').innerHeight() + border;
			   facebox_width = 590; //$('#main-photo img:eq(0)').innerWidth() + border;
			   $('#facebox table').height(facebox_height);
			   $('#facebox table').width(facebox_width);

			   //curr_top = $(window).height() / 2 - ($('#facebox table').height() / 2);
			   $('#facebox').css('top', 68);
			   curr_left = $(window).width() / 2 - ($('#facebox table').width() / 2);
			   $('#facebox').css('left', curr_left);
			   $('#main-photo').fadeOut();
			});
			
			jQuery.facebox(description);
			$('#facebox .close_image').attr('src', Drupal.settings.theme.play_button);
			$('#facebox .close_image').attr('title', 'Play');
			$('#facebox .close_image').attr('alt', 'Play');	
			$('#facebox .close_image').parent('a').attr('id', 'play');
			faceboxPop = true;			
			playing = false;
		}
		
		/* repop description */
		$('a[rel*=facebox]').click(function() {
			if (faceboxPop == true) {
				$('#main-photo').fadeIn();
				jQuery(document).trigger('close.facebox');
				faceboxPop = false;
			}
			else {
				description = $('#album-description').html();
				jQuery.facebox(description);
				$('#main-photo').cycle('pause');
				playing = false;
				faceboxPop = true; 
			}
			return false;
		});
	
		/* resume slideshow on close */
		$(document).bind('close.facebox', function() {      
			$('#facebox .close_image').attr('src', Drupal.settings.theme.close_button);
			$('#facebox .close_image').attr('title', 'Close');
			$('#facebox .close_image').attr('alt', 'Close');	  
			$('#main-photo').cycle('resume');
			showSlideshow();
			$('#content_after').fadeIn();
			faceboxPop = false;
		});	
		
		/* show/hide thumbnails */
		$('a.thumbnail-show').click(function() {
			$('#thumbnails').slideDown();
			$(this).hide();
			$('a.thumbnail-hide').show();
			createCookie('slideshow-thumbnails', 'show', 365);
			return false;
		});
		$('a.thumbnail-hide').click(function() {
			$('#thumbnails').slideUp();
			$(this).hide();
			$('a.thumbnail-show').show();
			createCookie('slideshow-thumbnails', 'hide', 365);
			return false;
		});

		var thumbnails = readCookie('slideshow-thumbnails');
		if (thumbnails == 'hide') {
			$('a.thumbnail-hide').hide();
			$('a.thumbnail-show').show();		
		}
		else if (thumbnails == 'show') {
			$('a.thumbnail-show').hide();
			$('a.thumbnail-hide').show();
			$('#thumbnails').show();
		}
		else {
			$('a.thumbnail-hide').hide();
			$('a.thumbnail-show').show();
			$('#thumbnails').hide();	  
		}
		
		/* thumbnail holder centred */
		var total = $('#thumbnails li a').size();
		
		if (total) {
		var imagewidth = String();/*Drupal.settings.theme.thumbnail_width;*/
		var imagewidth = Math.floor(Drupal.settings.theme.thumbnail_width);
		var width = 0;
		var total_width = 0;
		var row_width = 0;
		var thumbs = 13;
		var i = 0;
		var last = 0;
		var margin = 12;
		var border = 6;	
		var rows = total/thumbs;
		$('#thumbnails li a').each(function () { 
		  ++i;
		  if (i < thumbs) {
			row_width = row_width + (imagewidth + margin + border);
		  }		
		  if (i == total) {
			last = imagewidth + border;
		  }
		  total_width = total_width + (imagewidth + margin + border);
		});	
		
		if ($('#thumbnails li:eq(0)').hasClass('first')) {			
			if (i > thumbs) {
			  width = row_width;
			}
			else if (total < thumbs) {			  
			  width = total_width;
			}
			else {
			  width = row_width - last;
			}
			$('div.thumbnails-holder').width(width);		
		}
		}/* if total */

		
		/**
		 * drop down main menu 
		 */
		$('.main-navigation-links ul.primary-menu li a:eq(0)').hide(); 
		$('li.expanded').each(function() {
		  //click show/hide
		  var li = $(this);
		  li.expanded = false;
		  
		  li.find('a').eq(0).click(function() {
			if (li.expanded == false) {
			  li.find('ul.primary-menu').slideDown();
			  li.expanded = true;
			}
			else {
			  li.find('ul.primary-menu').slideUp();
			  li.expanded = false;				
			}
		    return false;
		  });
		  
		  if (li.find('ul.primary-menu li').hasClass('active-trail')  || li.hasClass('active-trail')) {
		    li.find('ul.primary-menu').slideDown();
			li.expanded = true;
		  }		  
		});
		
		var srcs =  Array();
		//menu hover states
		$('.main-navigation-links a').each(function() {   
		  a = $(this);
          
		  //preload hover images
		  preloadImages();
		  a.hover(
		    function () { 
			  if (!$(this).hasClass('active')) {
			    base_text = $(this).find('span').text();
			    srcs[base_text] = $(this).find('img').attr('src');
			    hover_url = $('span#'+base_text+'-hover').text();
			    $(this).find('img').attr('src', '/'+ hover_url);
			  }
		    }, 
		    function () {
			  if (!$(this).hasClass('active')) {
			    base_text = $(this).find('span').text();
			    $(this).find('img').attr('src', srcs[base_text]);
			  }
		    }
		  );
											
		});
		
		/* slideshow controls hover */
		var normal =  Array();
		$('#controls a, #facebox .footer a').each(function() {      
			base_url = Drupal.settings.basePath;
		  $(this).hover(
		    function () {		  
					action  = $(this).attr('id');
			    img = $(this).find('img').attr('src');
		      normal[action] = img;
			    hover_img = $('#controls-hover div.'+action).text();
			    if (hover_img) {
			      $(this).find('img').attr('src', hover_img);
			    }
			  },
		    function() {
			  action  = $(this).attr('id');
			  $(this).find('img').attr('src', normal[action]);
		    }
		  );
		});
  
	  /* thickbox hover */
	  $(".thickbox-hover").each(function(i) {
		$(this).hover(
		function () {
			var t = this.title || this.name || null;
			var a = this.href || this.alt;
			var g = this.rel || false;
			tb_show(t,a,g);
			this.blur();
			return false;
		});
	  });		
	
  });
}

$.fn.cycle.transitions.myFx = function($cont, $slides, opts) {
  $slides.not(':eq('+opts.startingSlide+')').css('opacity',0);
  
  opts.before.push(function(curr, next, opts) {
	curr_width = $(this).width();
	curr_height = $(this).height();
    $('#main-photo').height(curr_height);//OK 
    $('#main-photo').width(curr_width);	

    //center controls with photo
    newwidth = $("#main-photo").outerWidth();
    $('.slideshow-controls').width(newwidth);	

    next_left = 900 - newwidth;
    next_left = (next_left/2) - 150;

	//$('#main-photo img').css('left', next_left +'px');
    $('.slideshow-controls').css('margin-left', next_left +'px');
	
	opts.cssBefore = { left: next_left, opacity: 0 };
	
	$(this).show();  
  });

  //center first photo
  first_width = $('#main-photo img:eq('+opts.startingSlide+')').width();
  first_height = $('#main-photo img:eq('+opts.startingSlide+')').height();
  $('#main-photo').height(first_height);//OK 
  $('#main-photo').width(first_width);	
  first_newwidth = $("#main-photo").outerWidth();
  $('.slideshow-controls').width(first_newwidth);	

  first_left = 900 - first_newwidth;
  first_left = (first_left/2) - 150;

  opts.cssFirst  = { left: first_left};
  opts.animIn    = { opacity: 1 };
  opts.animOut   = { opacity: 0 };
  opts.cssAfter  = { display: 'none' };
  opts.onAddSlide = function($s) { 
	$s.hide(); 
  };
};

function hideSlideshow() {
  playing = false;
	$('#main-photo').hide();
  $('#controls').hide();
  $('#thumbnails').hide();
  $('a.description-link').hide();
}
function showSlideshow() {
  playing = true;
	$('#main-photo').show();
  $('#controls').show();
  triggerSlideshow();
  $('a.description-link').show();
  
  var thumbnails = readCookie('slideshow-thumbnails');
  if (thumbnails == 'hide') {
		$('a.thumbnail-hide').hide();
		$('a.thumbnail-show').show();
		$('#thumbnails').hide();
  }
  else if (thumbnails == 'show') {
	  $('a.thumbnail-show').hide();
	  $('a.thumbnail-hide').show();	
	  $('#thumbnails').show();
  }  
  else {
		$('a.thumbnail-hide').hide();
		$('a.thumbnail-show').show();
		$('#thumbnails').hide();	  
  }
}

function triggerSlideshow() {
  $('#play').hide();	
  $('#main-photo').height($('#main-photo img:eq(1)').height());
  $('#main-photo').width($('#main-photo img:eq(1)').width());
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};
function eraseCookie(name)
{
	createCookie(name,"",-1);
};
// /cookie functions

//preloader for hover image
var preloaded = new Array();
function preloadImages() {    
  var i = 0;
  $('.hover-image').each(function() {
	preloaded[i] = document.createElement('img');
    preloaded[i].setAttribute('src',$(this).text());
	++i;
  });
};

