$(document).ready(function(){  
  $('#fader_container').hide();
  $('.navi_create_pdf').bind('click', onCreatePdfClick); 
  updatePdfImageCount();
  
  $('#prevBtn')
    .bind('mouseover touchstart', function()
    {
      setspeed(10);
      return false;
    })
    .bind('mouseout touchend', function()
    {
      setspeed(1);
      return false;
    })
    .bind('click', function()
    {
      this.blur();
      return false;
    })
  ;
  
  $('#nextBtn')
    .bind('mouseover touchstart', function()
    {
      setspeed(-10);
      return false;
    })
    .bind('mouseout touchend', function()
    {
      setspeed(-1);
      return false;
    })
    .bind('click', function()
    {
      this.blur();
      return false;
    })
  ;
});

function showSlides(rel, href)
{        
  var url = 'http://www.jonathanweyland.com';
  href = href.replace(url, '');
  
  var imgs = []; // array of images
  var caps = []; // array of captiomns
  
  var curr = 0;
  
  var img = ''; // comma seperated list of images
  var cap = ''; // comma seperated list of captions   
  
  if (rel != '')
  {
    $('#' + rel + ' a').each(function() 
    {      
      var h = $(this).attr('href').replace(url, '');
      if (h == href) curr = imgs.length;
      imgs.push(h);   
      caps.push($(this).attr('title'));         
    });
    img += imgs.join('||');
    cap += caps.join('||');
  }
  else
  {
    img += href;
  }
  
  openSlides(img, cap, curr);         
}

function faderShowTooltip(i)
{
  var f = flashMovie("faderSubnaviTop");
  if (f['faderShowTooltip']) f.faderShowTooltip(i);  
}

function faderHideTooltip(i)
{
  var f = flashMovie("faderSubnaviTop");
  if (f['faderHideTooltip']) f.faderHideTooltip(i);  
}

function closeSlides(i)
{
  $('#slider').width(761);
  $('#htmlSubNavi').width(763);
  
  goToImage(i); 
  
  $('#fader_container').html('');
  $('#fader_container').hide(); 
  
  $('#slider').focus();
  //$('body').css({'overflow':'auto'});  
}

function initLoadXml()
{    
  if (flashMovie("slider") != null && flashMovie("slider").loadXML != undefined) 
  {
    loadXML(xmlId); 
    setspeed(-1);   
  }
  else
  {
    setTimeout('initLoadXml()', 5000);
  }
}

function openSlides(imgs, caps, curr)
{  
  if(DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision)) 
  {
    var h = getFlashCode('fader', 'fader', '100%', '100%', "images=" + imgs + "&current=" + curr + "&fill=0&size=0&&wait=5&fade=2&auto=1&captions=" + caps);    
    
    // hide other flash
    $('#slider').width(0);
    $('#htmlSubNavi').width(0);
  
    // scroll to top to hide black bar at bottom
    $('html,body').animate({scrollTop: 0}, 300);
    
    // set fader html and show it
    $('#fader_container').html(h);
    $('#fader_container').show(); 
  }
}

function loadStrip(id)
{
  if (insertFlashCode('photostrip-flash', 'slider_2_15', 'slider', '761px', '410px', 'gid=' + id))
  {
    $('#photostrip-images').hide();  
  }
}

function loadPdfCounter()
{
  insertFlashCode('pdf_images_counter', 'PdfImagesCounter', 'PdfImagesCounter', '52px', '12px');
}

function loadPdfImages(images)
{
  insertFlashCode('pdf_images', 'PdfImages', 'PdfImages', '1050px', '470px', 'images=' + images);
}

function loadSubNavi()
{  
   insertFlashCode('fotoSubnaviTop', 'fader_subnavi_top',  'faderSubnaviTop', '755', '25');
   insertFlashCode('fotoSubnaviBottom', 'fader_subnavi_bottom',  'faderSubnaviBottom', '755', '100%');
}

function faderSubHeight(h)
{
  $('#fotoSubnaviBottom').css({'height': (h - 30) + 'px', 'paddingBottom': '12px'});
  $('#disclaimer').css({'top': (480 + h) + 'px'});
}

var thickboxGroup="";
  
function activeCategorie(_id)
{    
	if(!document.getElementById('tag_'+_id))return;		
	if (!DHTML) return;
	
	for(var y = 0; y < maxTagI;  y++)
  {
		var elem=document.getElementById('tag_'+y);
		elem.style.top = "0px";
		
		var elactive=document.getElementById('tag_'+y+'_active');
		elactive.style.top = "-10000px";
	}
	
	var elem=document.getElementById('tag_'+_id);
	elem.style.top = "-10000px";
	
	var elactive=document.getElementById('tag_'+_id+'_active');
	elactive.style.top = "0px";	
}
	

function verplaatsNaar(elemId,xpos,ypos)
{
	if(!document.getElementById(elemId)) return;	
	var elem=document.getElementById(elemId);
	if (!DHTML) return;
	elem.style.left = parseInt(xpos)+"px;"; //Firefox vereist hier parseInt
	elem.style.top = parseInt(ypos)+"px;";    
		
}		
			
function setspeed(speed)
{
  var f = flashMovie("slider");
  if (f && f['setSpeed']) f.setSpeed(speed);
  sliderSpeed = speed;
}
			
var xmlId = '';			
function loadXML(id)
{
 xmlId = id;
 //flashMovie("slider").loadXML(xmlId);
 loadStrip(id);
 thickboxGroup = "group_"+xmlId;
}			
			
function goToImage(i)
{
  var f = flashMovie("slider");
  if (f['goToImage']) f.goToImage(i);
}

var got_current_image = true;
function getCurrentImage()
{
  if (!got_current_image || pdf_image_count >= pdf_max_images) return;
  got_current_image = false;
  var f = flashMovie("slider");
  if (f['getCurrentImage']) 
  {
    var p = f.getCurrentImage();
    // alert(p);
    if (p != '') jQuery.get('photography_pdf.php?add=' + p, null, function(data, status) 
    {
      // alert(data);
      pdf_images = data;
      updatePdfImageCount();
      got_current_image = true;
    });
  }
}

var pdf_images = '';
var pdf_max_images = 16;
var pdf_image_count = 0;

function updatePdfImageCount()
{      
  pdf_image_count = (pdf_images == '') ? 0 : pdf_images.split(',').length;
  
  $('.navi_create_pdf').addClass('navi_pdf_disabled') 
  if (pdf_image_count > 0) $('.navi_create_pdf').removeClass('navi_pdf_disabled');  

  $('#pdf_count').html(pdf_image_count + ' &gt; ' + pdf_max_images);  
  
  updatePdfImageCountFlash();
}

function updatePdfImageCountFlash()
{
  var f = flashMovie("PdfImagesCounter");  
  if (f && f['setCount'])
  {
    f.setCount(pdf_image_count, pdf_max_images);
  }
  else
  {
    setTimeout('updatePdfImageCountFlash()', 100);
  }
}

function onCreatePdfClick()
{             
  console.log (pdf_image_count);
}

function showViewer(s)
{
  var f = flashMovie("slider");  
  if (f['showViewer']) f.showViewer(s);
}
			
// deze functie wordt vanuit flash aangeroepen
function openImage(_image)
{
	showSlides(thickboxGroup, _image);				
	//_image=URLEncode(_image);		 
}
			
function tabfocus(link)
{
	if(!link)return;
	link.blur();
}
