// -----------------------------------------------------------------------------
// 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);	

var agent = navigator.userAgent.toLowerCase();
var iOs = agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0; 
var isChrome = agent.indexOf('chrome') >= 0;
var clickEvent = iOs ? 'touchend' : 'click';

$(document).bind('ready', function() 
{
  $('#navigatie a').each(function()
  { 
    var text = getText($(this));
    if (text == '') return;
    html = ''+
      '<img class="off" src="./text/text2image.php?text=' + text + ' &color=FFFFFF&size=16.7&bg=333333" alt="' + text + '"/>' +
      '<img class="on" src="./text/text2image.php?text=' + text + ' &color=000000&size=16.7&bg=333333" alt="' + text + '"/>'
    ; 
    $(this).html(html).attr('title', text);
  });
  
  $('#categorie').each(function()
  {  		      
    var text = getText($(this));
    if (text == '') return;
    html = '<img src="./text/text2image.php?font=Steinerlight.ttf&text=' + text + ' &color=CD333D&size=17&bg=000000" alt="' + text + '"/>'; 
    $(this).html(html).attr('title', text);
  });
  
  $('.video_page a').each(function()
  {
    var text = getText($(this));
    if (text == '') return;
    html = '<img src="./text/text2image.php?font=HelveticaLTStd-LightCond.otf&text=' + text + ' &color=FFFFFF&size=11&bg=333333" alt="' + text + '"/>'; 
    $(this).html(html).attr('title', text);
  });
  
  initDisclaimer();    
});

function initDisclaimer()
{
  // show disclaimer on rollover if not on ios
  if (!$('body').hasClass('ios'))
  {
    $('#disclaimer')
      .css({opacity: 0})
      .show()
      .bind('mouseover', function()
      {
        $(this).stop().animate({opacity: 1}, 500)
      })
      .bind('mouseout', function()
      {
        $(this).stop().animate({opacity: 0}, 500)
      })
    ;
  } 
}

function initMp3()
{
  $('#mp3-file').jPlayer(
  {
    swfPath: 'swf',
    solution: 'html, flash',
    volume: 0.5,
    supplied: 'm4a, mp3, oga, aac',
    backgroundColor: '#333333',
    ready: function () 
    {     
      // load music file
      /*
      $(this).jPlayer('setMedia', {
        mp3: '/audio/gun.mp3',
        m4a: '/audio/gun.m4a',
        oga: '/audio/gun.ogg',
        aac: '/audio/gun.aac'
      });
      */
      
      $(this).jPlayer('setMedia', {
        mp3: '/audio/jw.mp3',
        oga: '/audio/jw.ogg',
        aac: '/audio/jw.aac'
      });
      
      if (!iOs)
      {
        // start playing if state is not paused (either empty or play)
        if ($.cookie('mp3-state') != 'pause')
        {                                                        
          // resume audio file where we stopped last time
          $(this).jPlayer('play', Number($.cookie('mp3-time')));
          $('#mp3-pause').show();  
        }
        else
        {
          $('#mp3-play').show();
        }
      }
      else
      {
        // wait for user click
        $('#mp3-play').show().click();
      }      
    },
    ended: function() 
    {
      // Add a repeat behaviour so media replays when it ends. (Loops)
      $(this).jPlayer("play"); 
    },
    pause: function(e)
    {
      // remember audio position
      $.cookie('mp3-time', e.jPlayer.status.currentTime);       
      $('#mp3-play').show();
      $('#mp3-pause').hide(); 
    },
    play: function()
    {           
      $('#mp3-play').hide();
      $('#mp3-pause').show();
    }       
  });
  
  // play button
  $('#mp3-play').bind('click', function()
  {             
    $('#mp3-file').jPlayer('play');
    $.cookie('mp3-state', 'play', {expired: 9999}); 
  });
  
  // pause button
  $('#mp3-pause').bind('click', function()
  {
    $('#mp3-file').jPlayer('pause');
    $.cookie('mp3-state', 'pause', {expired: 9999});     
  }); 
  
  // pause to remember audio position when leaving
  $(window).bind('beforeunload', function() 
  {  
    $('#mp3-file').jPlayer("pause"); 
  });   
}
    
function noFlashHtml(s)
{
  return '';
  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 loadFlashMenu()
{  
   insertFlashCode('navigatie', 'menu',  'menu', '180', '390');
}

function loadPdfCounter2()
{
  insertFlashCode('pdf_images_counter2', 'PdfImagesCounter2', 'PdfImagesCounter', '230px', '20px');
}

function loadFlashTitle(t)
{
  insertFlashCode('categorie', 'title', 'title', '300px', '30px', 't=' + t);
}

function loadFlashTitleRed(t)
{
  insertFlashCode('categorie', 'title-red', 'title', '400px', '30px', 't=' + t);
}

function loadProfileSlider()
{
  insertFlashCode('slideshow', 'slider_strip_200', 'slider_strip', '761px', '202px');
}

function loadSubNavi(titles)
{
  if (insertFlashCode('subnaviFlash', 'navigatie6', 'subnavi', '220px', '420px', 'Titles=' + titles))
  {
    $('#subnaviHtml').hide();
  }
  else
  {
    $('#subnaviFlash').hide();
  }
}

function loadProfileSlideshow()
{
  insertFlashCode('slider_strip_200', 'slideshow', 'slider_strip', '761px', '200px');
}

function insertFlashCode(el, src, id, width, height, flashVars)
{
  if (DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision)) 
  {
    var h = getFlashCode(src, id, width, height, flashVars);
    var e = document.getElementById(el); 
    if (e) 
    {
      e.innerHTML = h;
      return true;
    }  
  } 
  return false;
}

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 flashMovie(name)
{
  if(navigator.appName.indexOf("Microsoft") != -1) return window[name]; 
  return document[name]; 
}			

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;
}

function getText(el)
{
  if (el.hasClass('text-replaced')) return '';
  el.addClass('text-replaced');
  return $.trim(el.text());
}   
