$(document).ready(function() {

  // unbind click events from persisting elements
  $('#impressum a, a#logo, #sprache a').unbind('click');

  // SET LANGUAGE LINK HREF FIRST
  var langURL = $.address.value();
  if (langURL == '/') {langURL = '/index.php'};

    // update links where necessary

    if ( $('#impressum a').text() == 'Impressum' ) 
    { // DE version
      $('#sprache a').attr('href','../en'+langURL);      
      $('#a_prev').text('zur'+unescape("%FC")+'ck');
      $('#a_next').text('weiter');
      $('#impressum a').attr('href','../de/impressum.php');
      $('a#logo').attr('href','../de/index.php');
    }
    else
    {
      $('#sprache a').attr('href','../de'+langURL);
      $('#a_prev').text('back');
      $('#a_next').text('next');
      $('#impressum a').attr('href','../en/impressum.php');
      $('a#logo').attr('href','../en/index.php');
     }

 // AJAX LOAD PAGES

  var getThis = '';
$('#firstNav a,#secondNav a,#thirdNav a,#impressum a,a#logo,#sprache a').click(function(){
  getThis = $(this).attr('href');
  getThisContent = getThis +' #contentAjax';
  getLeftNav = getThis + ' #navAjax';


  // CHECK IF LANG LINK WAS CLICKED - IF SO UPDATE FOOTER
  if ($(this).parent('div#sprache').length > 0) {
    if ($(this).text() == 'DE') 
    {
     $('#sprache').html('<span class="inact">DE / </span><a class="act" href="#">EN</a>');
     $('#impressum a').text('Impressum');
     $('a#a_prev').text('zurück');
     $('a#a_next').text('weiter');
    }
    else 
    {
     $('#sprache').html('<a class="act" href="#">DE</a><span class="inact"> / EN</span>');
     $('#impressum a').text('Imprint');  
     $('a#a_prev').text('back');
     $('a#a_next').text('next');   
    }
  }

  $('#navCage').load(getLeftNav);

  $('#cntCage').load(getThisContent, function() { 
if ( $('#content a#player').attr('href') !== '' ) {

if ( $('div.loopThis').size() > 0 ) { 

var newVid = $('a#player').attr('href');

$f('player', '../content/flash/flowplayer-3.2.7.swf',{
  key: '$c144771256ebde5d453',
  autoBuffering: true,
  autoPlay:true,
  logo: null,
  play: {opacity: 0},
  scaling: 'fit',
  loop: true,
  cachebusting: true,
  onMouseOver: function() {
  this.getPlugin('play').css({opacity: 1}).ipad();
},

// when mouse is exited remove exposing
	onMouseOut: function() {
	 	this.getPlugin('play').css({opacity: 0}); },
				plugins: { controls: null },
playlist: [     {url: newVid  
    , onBeforeFinish: function ()  { return false; }  

} ]

			});
} // endif loopThis

else
{
$f('player', '../content/flash/flowplayer-3.2.7.swf',{
  key: '$c144771256ebde5d453',
  autoBuffering: true,
  autoPlay:true,
  logo: null,
  play: {opacity: 0},
  scaling: 'fit',
  loop: false,
  cachebusting: true,
  onMouseOver: function() {
this.getPlugin('play').css({opacity: 1}).ipad();
},

// when mouse is exited remove exposing
	onMouseOut: function() {
	 	this.getPlugin('play').css({opacity: 0}); },
				plugins: { controls: null },
playlist: [     {url: newVid  
   // , onBeforeFinish: function ()  { return false; }  

} ]

			});
}  // end else


if( !(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) {
   $('a#player img#splash').remove();
   $f().play(); 
}



} //endif player

 // SET PREV/NEXT
var nlHref= $('#nextLink').text(); 
$('#a_next').attr('href', '/'+ nlHref);

// if (console) console.log('@@ nlHref: ' + nlHref);

var blHref= $('#prevLink').text(); 
$('#a_prev').attr('href', '/'+ blHref);

//disable prev on index
if (blHref == 'de/107_kontakt.php') {
   $('a#a_prev').addClass('disabled');
      $('a#a_prev').attr('href', 'javascript:void(0)').unbind('click');
}

//disable next on kontakt.php
if (nlHref == 'de/101_der-designer.php' && blHref == 'de/index.php') {
   $('a#a_next').addClass('disabled');
      $('a#a_next').attr('href', 'javascript:void(0)').unbind('click');
}


 // Fake the URL change
 var niceURL = getThis.replace('../','');
 niceURL = niceURL.replace('en/','');
 niceURL = niceURL.replace('de/','');
 $.address.value(niceURL);


// REINITIALIZE AJAX BTNs
$.ajax({
   type: 'GET',
   url: '/content/js/ajax.js',
   dataType: 'script'
});

  });

  return false;
});


});

