$(document).ready(function(){  
  $('#fader_container').hide();
  $('.navi_create_pdf').bind('click', onCreatePdfClick); 
  updatePdfImageCount();
});

function loadFlash()
{
  loadPdfCounter();
  loadSubNavi();
  loadStrip();
}

// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 124;
// -----------------------------------------------------------------------------		

var DHTML = (document.getElementById || document.all || document.layers);	

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 != '')
  {
    $('.thickbox').each(function() 
    {
      if ($(this).attr('rel') == rel)
      {
        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 noFlashHtml(s)
{
  var i = !s ? '/images/getflash.gif' : 'http://www.adobe.com/macromedia/style_guide/images/160x41_Get_Flash_Player.jpg';
  var h = '';
  h += '<div id="noflash">';
  h += '<p>In order to view these images you need the latest version of the <a href="http://www.macromedia.com/go/getflashplayer">Adobe Flash Player</a>.';
  h += '<p>Please download the latest <a href="http://www.macromedia.com/go/getflashplayer">Adobe Flash Player</a> by clicking on the button below.<p>';
  h += '<p><br/></p>';
  h += '<p><a href="http://www.macromedia.com/go/getflashplayer"><img src="' + i + '" alt="Download Adobe Flash Player"/></a><p>';
  if (s) h += '<p><br/></p><a href="#" onclick="closeSlides();"><strong>Click here to close this window</strong></a>';
  h += '</div>';
  return h;
}

function openSlides(imgs, caps, curr)
{
  // hide other flash
  $('#slider').width(0);
  $('#htmlSubNavi').width(0);
  
  var h = noFlashHtml(true);   
  if(DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision)) 
  {
    h = getFlashCode('fader', 'fader', '100%', '100%', "images=" + imgs + "&current=" + curr + "&fill=0&size=0&&wait=5&fade=2&auto=1&captions=" + caps);    
  }
  
  // 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)
{
  insertFlashCode('photostrip', 'slider_2_15', 'slider', '761px', '410px', 'gid=' + id);
}

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 insertFlashCode(el, src, id, width, height, flashVars)
{
  var html = noFlashHtml(false);  
  if(DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision)) 
  {
    html = getFlashCode(src, id, width, height, flashVars);
   }  
   document.getElementById(el).innerHTML = html;  
}

function getFlashCode(src, id, width, height, flashVars)
{
  if (!flashVars) flashVars = '';
  return AC_FL_GetContent(
    'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7',
    'width', width,
    'height', height,
    'src', src,
    'quality', 'high',
    'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
    'play', 'true',
    'wmode', 'transparent',
    'id', id,
    'name', id,
    'allowScriptAccess', 'always',
    'movie', src,
    'salign', 'lt',
    'scale', 'showAll',
    'align', 'middle',
    'flashVars', flashVars
  );  
}

function faderSubHeight(h)
{
  $('#fotoSubnaviBottom').css({'height': (h - 30) + '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 flashMovie(name)
{
  if(navigator.appName.indexOf("Microsoft") != -1) return window[name]; 
  return document[name]; 
}			
			
function setspeed(speed)
{
  var f = flashMovie("slider");
  if (f['setSpeed']) f.setSpeed(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();
}
			
function URLEncode( _txt )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'():/";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = _txt;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) 
  {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "%20";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//document.URLForm.F2.value = encoded;
	return encoded;
}