var firstItem = false;
var script = 'test';

var openBox = function(player) {
  Shadowbox.clearCache();
  Shadowbox.setup();
  Shadowbox.open({
    content: firstItem,
    gallery: 'gallery',
    player: player
  });
}

var startVideo = function() {
  alert('test');
}

var playVideo = function(video) {
  tmp = video.split('/');
  $.ajax({
    type      : 'GET',
    url       : '/' + lang + '/ajax.xml?videoFile=' + tmp[tmp.length-1],
    dataType  : 'xml',
    success   : function(xml) {
      $(xml).find('video').each(function() {
        var $item = $(this);
        Shadowbox.clearCache();
        Shadowbox.setup();
        Shadowbox.open({
            content:    '<div id="videoContainer"><div id="dynVideo"></div></div>',//$item.find('player').text(),
            player:     'html',
            width:      $item.find('width').text(),
            height:     $item.find('height').text()
        });
        var playerConfig  = $item.find('script').text();
        var videoId       = $item.find('id').text(); 
        eval(playerConfig);
        setTimeout(function() {
          $('#dynVideo').html($item.find('player').text());
          setTimeout(function() {
            eval("jwplayer('" + videoId + "').setup(config_" + videoId + ");");
          },100);
          
        }, 1500);
      });
    } 
  });
}

var setVideo = function(id) {
  $.ajax({
    type      : 'GET',
    url       : '/' + lang + '/ajax.xml?video=' + id,
    dataType  : 'xml',
    success   : function(xml) {
      $(xml).find('video').each(function() {
        var $item = $(this); 
        var playerConfig  = $item.find('script').text();
        var videoId       = $item.find('id').text(); 
        $('#video').fadeOut(500, function() {
          //uid = $item.find('uid').text();
          $('#video').html($item.find('player').text());
          eval(playerConfig);
          eval("jwplayer('" + videoId + "').setup(config_" + videoId + ");");
          $('#video').delay(150).fadeIn(750);
        });
      });
    }
  });
}

var isiPhone = function(){
  return (
    (navigator.platform.indexOf("iPhone") != -1) ||
    (navigator.platform.indexOf("iPod") != -1)
  );
}

$(function() {

  if (userAgent != 'iOS') {
    $('.notFixed').removeClass('notFixed');
  }
  
  if (isiPhone() && document.cookie.indexOf('mobile=no-m') == -1) {
    string = (lang == 'de') ? 'Möchten Sie zur Mobilversion wechseln?' : 'Would you like to see the iPhone optimized page?';
    if (confirm(string)) {
      location.href = 'http://m.videograph.de/' + lang + '/';
    } else {
      document.cookie = 'mobile=no-m;'; 
    }
  }  
  
  $('.nojs').removeClass('nojs');

  $("a[href*=.pdf], a[href*=.PDF]").click(function(){
    var item = this;
    var file = this.href.split('/');
    $('body').addClass('videograph');
    if (lang == 'de') {
      $('body').append('<div id="pdfDialog">Was möchten sie mit dem PDF Dokument machen?</div>')
    } else {
      $('body').append('<div id="pdfDialog">What would you like to do?</div>')
    }
    $('#uiCSS').remove();
    $("head").append('<style type="text/css" id="uiCSS"></style>');
    var dialog = $('#pdfDialog').dialog({
      show: 'fade',
      close: 'fade',
      title: file[file.length-1],
      draggable: false,
      close: function() {
        $('#pdfDialog').remove();
        $('body').removeClass('videograph');
      },
      width : 420,
      resizable: false,
      buttons: [{
                  text: (lang == 'de') ? 'Öffnen' : 'Open',
                  click: function() { window.location = item.href; dialog.dialog('close'); }
                }, {
                  text: (lang == 'de') ? 'Im neuen Fenster öffnen' : 'Open in new window',
                  click: function() { window.open(item.href); dialog.dialog('close'); }
                }, {
                  text: (lang == 'de') ? 'Download' : 'Download',
                  click: function() { window.location = '/download.php?file=' + item.href; dialog.dialog('close');  }
                }] 
    });
  	return false;
  });  

  $("a[href*=.mp4]").click(function(){
  	playVideo(this.href);
  	return false;
  });


  $('.external-link, .internal-link-new-window').live('click', function() {
    window.open($(this).attr('href'));
    return false;
  });

  $('.lightboxImage').live('click', function() {
    Shadowbox.open({
      content: $(this).attr('href'),
      player: 'img',
      title: $(this).attr('title')
    });
    return false;
  });

  $('.lightboxFlash').live('click', function() {
    var url   = '/flashSize.php?f=' + $(this).attr('href')
    var swf   = $(this).attr('href');
    var title = $(this).attr('title');
    $.get(url, function(data) {
      size = data.split('x');
      Shadowbox.open({
        content: swf,
        player: 'swf',
        title: title,
        width: size[0],
        height: size[1]
      });
    });
    return false;
  });
  
  $('#item4, a[href*=showroom.html]').click(function() {
      try {
        Shadowbox.open({
          content: $(this).attr('href'),
          player: 'iframe',
          title: 'videograph Showroom',
          width: $(window).width() - 20,
          height: $(window).height() - 50
        });
      } catch(e) {
      
      }
      return false;
  });
  $('a[rel*=external]').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
    
  $('.ajaxGallery').live('click', function() {
    var url = $(this).attr('href') + '?ajax';
    $.get(url, function(data) {
      $('#gallery').remove();
      $('body').append('<div id="hiddenGallery" style="display:none;"></div>');
      eval(data);
      window.setTimeout('openBox("img");', 0);
    });
    return false;
  });
  
  $("#menuArea ul li a").each(function(i){
    var no = i + 1;
    if ($('#menuArea').hasClass('index')) {
      $(this).addClass('indexItem'+no);
    } else {
      $(this).addClass('item'+no);
    } 
  });  
  
  var effect = 'fade';
  $('#lightSwitch:not(.hidden)').html('Lights: <a name="lightSwitch"></a>');
  $('#lightSwitch:not(.hidden) a').click(function() {
    $(this).blur().toggleClass('on');
    $('#video').toggleClass('shadow');
    if ($('#showroom').html() == null)
      $('body').prepend('<div id="showroom"></div>');
    if (userAgent == 'iOS')
      $('#showroom').height($(document).height()).toggleClass('visible');
    else
      $('#showroom').height($(document).height()).toggle(effect, {}, 750);
  });
  
  $(window).resize(function() {
    var h = $(document).height();
    $('#showroom').height(h);
  });
  
  $('#q').click(function() {
    if ($(this).val() == 'Suchbegriff' || $(this).val() == 'Search term')
      $(this).val('');  
  })
  
  if ($('#q').val() == '') {
    switch(lang) {
      case 'de':
        $('#q').val('Suchbegriff');
        break;
      default:
        $('#q').val('Search term');
        break;
    }
    
  }
});
