function createPlayer (streamer, file) {
  var flashvars = {
    streamer: streamer, 
    file: file, 
    type: 'video',
    bufferlength: 'null',
    autostart:"true"
  };

  var params = {
    allowfullscreen:"true", 
    allowscriptaccess:"always"
  };

  var attributes = {
    id:"player1",  
    name:"player1"
  };

  swfobject.embedSWF("/flash/player.swf", "video", "480", "308", "9", false, flashvars, params, attributes);
}

function appendTag(element, tag) {
  if(element.val() == "") {
    element.val(tag);
  }
  else {
    var tags = element.val().split(/ *, */);
    var i;
    if((i = tags.indexOf(tag)) < 0) {
      tags.push(tag);
    }
    else {
      tags[i] = undefined;
    }
    // I hate you jquery. Fix this:
    var new_tags = [];
    $(tags).each(function(el) {
      if(tags[el]) new_tags.push(tags[el]);
    });
    element.val(new_tags.sort().join(', '));
  }
}

$(document).ready(function() {
  
  $('#mycarousel').jcarousel();
  
  $('#carousels .product-carousel').jcarousel();
  
  $('#frm-ddvb2').submit(function(e) {
    if ($('#remember_email')[0].checked) {
      var email = $('#epost').val();
      $.cookie('dvbook_email', email, {expires:30});
    } else {
      $.cookie('dvbook_email', null);
    }
  });
  
   $('a.new-window').click(function() {
     var text = $(this).parent().siblings().clone();
     
     var info_window = window.open('','InformationWindow','width=600,height=600');
     var html = '<html><head><title>Mediatecgroup</title></head><body><div id="information">' + $('<div/>').append(text).html() + '</div></body></html>';
     
     info_window.document.open();
     info_window.document.write(html);
     info_window.document.close();

    return false;
   });  
  
  $('#contact-general .vcard select').each(function() {
    
    var children = $(this).parent().find("div.adr").length;
    if (children === 1) {
      $(this).css({visibility: 'hidden'}); 
      $(this).parent().find("div.adr").removeClass('hide');
    }
    
    $(this).change(function() {
      var index = $(this).val();
      $(this).parent().find("div.adr").addClass('hide');
      $(this).parent().find("div.adr").each(function() {
        if ($(this).hasClass(index)) {
          $(this).removeClass('hide'); 
        }
      });
    });
  });
});

var Slideshow = function(images) {
  if(images.length > 1) {
    $(document).ready(function() {
      $('#slideshow').each(function() {

        var slideshow = $(this);
        var current_slide = 0;

        var cache_next_slide = function(index) {
          var next_index = index + 1;
          if(next_index >= images.length) next_index = 0;

          var slide = slideshow.find('li').get(next_index);

          // Only cache if the next slide doesn't exist
          if(!slide && images[next_index]) {
            var img = $('<img/>').attr({src: images[next_index].picture_url, alt: 'Slidshow image'});
            var a = $('<a></a>').append(img).attr({href: images[next_index].landing_url});

            slideshow.append($('<li></li>').append(a));
          }
        };

        var activate_slide = function(index) {
          var slide = $(slideshow.find('li').get(index));
          slide.css({zIndex: 200, display: 'block'});
          cache_next_slide(index);
        };

        var deactivate_slide = function(index) {
          var slide = $(slideshow.find('li').get(index));
          slide.css({zIndex: 300});
          slide.fadeOut(1000);
        };

        activate_slide(current_slide);

        setInterval(function() {
          deactivate_slide(current_slide);
          current_slide++;
          if(current_slide >= images.length) current_slide = 0;
          activate_slide(current_slide);
        }, 5000);
      });
    });
  }
};

function jsonFlickrFeed(json) {
  var items = json.items.slice(0, 6);
  
  if (items.length == 0) return;
  
  var pictures = $("#gallery .pictures");
  
  $(items).each(function() {
    var link = $('<a></a>').attr({href: this.link, title: this.title});
    link.append($('<img/>').attr({src: this.media.m, alt: this.title, width: 132, height: 99}));
    pictures.prepend(link);
  });
  
  $('#gallery').css({display:'inline'});
}

function jsonYoutubeFeed(json) {
  if (!json.feed.entry) return;
  
  var entry = json.feed.entry[0];
  var url = entry.link[0].href;
  url = url.replace(/watch\?v=(.+)$/, 'v/$1&amp;hl=en&amp;fs=1');

  var embed = $('<embed/>').attr({
    src: url,
    type: "application/x-shockwave-flash",
    allowscriptaccess: "always", 
    allowfullscreen: "true",
    width: "380",
    height: "277"
  });
  
  if($.browser.msie) {
    $("#gallery .movie").append(embed);
  } else {
    var object = $('<object></object>').attr({ width: '380', height: '277' });
    object.append($('<param/>').attr({ name: "movie", value: url }));
    object.append($('<param/>').attr({ name: "allowFullScreen", value: "true" }));
    object.append($('<param/>').attr({ name: "allowscriptaccess", value: "always" }));
    object.append(embed);
    $("#gallery .movie").append(object);
  }
  
  $('#gallery').css({display:'inline'});
}
