// Set up global Gallery array
var yellGallery = [];
var thumbWidth = 94;
var thumbNumber = 6;
var portalWidth = thumbWidth*thumbNumber;
// Create new gallery object constructor
function Gallery(galleryID,imgIndex){
	this.id = galleryID;
	this.imgIndex = imgIndex || 0;
	this.gallery = $('#media-gallery-'+galleryID);
	this.thumbCount = $('#thumbs-inner-'+galleryID).children().size();
	this.currentThumb = 0;
	this.leftPos = 0
	this.criticalWidth = this.thumbCount * thumbWidth;
	this.thumbStrip = null;
	this.prevLink = null;
	this.nextLink = null;
	this.mediaViewer = null;
	this.mediaTitle = null;
	this.video = null;
	this.init(galleryID);
}
Gallery.prototype.hasVideo = function(){return this.video!=null;}
Gallery.prototype.init = function(galleryID){
	var self= this;
  this.gallery.addClass('jsCarousel');
	// Update gallery object with references to the newly created elements
	this.thumbStrip = $('#thumbs-inner-'+galleryID);
  this.outerThumbStrip = $('#thumbs-outer-'+galleryID);
	this.prevLink = $('#gallery-prev-'+galleryID);
	this.nextLink = $('#gallery-next-'+galleryID);
	this.mediaViewer = $('#media-viewer-'+galleryID);
	this.mediaTitle = $('#media-title-'+galleryID);

  this.mediaViewer.css({
    "height" : (portalWidth-14)*0.75,
    "width" : portalWidth-14
  });
  this.thumbStrip.css({
    "width" : self.criticalWidth
  });
  this.outerThumbStrip.css({
    "width" : portalWidth-10
  });

	// Assign on click events to the gallery
	this.prevLink.click(function(){ self.prevFunc(); return false; });
	this.nextLink.click(function(){ self.nextFunc(); return false; });
	this.thumbStrip.children().click(function(){ self.thumbFunc($(this)); return false; });

	// Select an image
	this.thumbFunc(this.thumbStrip.children().eq(self.imgIndex));

	if(this.thumbCount <= thumbNumber){
		this.gallery.addClass('hideThumbNav');
	}
	if(this.thumbCount == 1){
		this.gallery.addClass('hideThumbs');
		this.gallery.parent().removeClass('media-tab');
	}

}
Gallery.prototype.nextFunc = function(){
	var newPos = this.leftPos-portalWidth;
	if(Math.abs(newPos) < this.criticalWidth){
		this.thumbStrip.animate({'left':newPos+'px'},{ duration: 300, easing: 'swing' });
		this.leftPos = newPos;
	}
	if(Math.abs(newPos-portalWidth) > this.criticalWidth){
		this.nextLink.addClass('gallery-next-deactivated');
	}
	this.prevLink.removeClass('gallery-prev-deactivated');
}
Gallery.prototype.prevFunc = function(){
	var newPos = this.leftPos+portalWidth;
	if(newPos <= 0 ){
		this.thumbStrip.animate({'left':newPos+'px'},{ duration: 300, easing: 'swing' });
		this.leftPos = newPos;
	}
	if((newPos+portalWidth) > 0 ){
		this.prevLink.addClass('gallery-prev-deactivated');
	}
	this.nextLink.removeClass('gallery-next-deactivated');
}
Gallery.prototype.thumbFunc = function(el){
	var self = this;
	el.siblings().removeClass('currentThumb ldcCurrentThumb').end().addClass('currentThumb');
	if(el.hasClass('video')){
    if(el.data('provider') == 'RealityDigital'){
      this.createVid(el.data('videoID'), el);
	}else{
      this.createFlashVid(el.attr('href'), 'media-viewer-'+this.id, el);
    }
  }else{
		this.mediaTitle.text(el.attr('data-source'));
		if(el.hasClass('ldc-gallery')){
			el.addClass('ldcCurrentThumb');
			this.mediaViewer.html('<img class="ldc-padding" src="'+el.attr('href')+'" />');
		}else{
			this.mediaViewer.html('<img height="'+(portalWidth-14)*0.75+'" width="'+(portalWidth-14)+'" src="'+el.attr('href')+'" />');
		}
	}
}
Gallery.prototype.createVid = function(stream, el){
  var streamname = $(el).attr('id').slice(11);
  
  this.mediaViewer.html('<video id="mediaVideo-'+this.id+'" width="550" height="325" controls preload/>');
  this.mediaViewer.parent().find('.report-link').remove();
  this.mediaViewer.before('<a class="report-link" href="mailto:service@yellgroup.com?Subject=Video%20report%20for%20Video%20'+stream+'/mp4&Body=%0D%0A%0D%0A%0D%0APlease%20do%20not%20edit%20the%20content%20below:%0D%0AReporting%20Video%20'+stream+'/mp4">Report video</a>');
  var vidFormat = this.getVidFormat();
  if(vidFormat.error == 'nohtml5'){
    this.mediaViewer.html('');
    this.createFlashVid(el.attr('href'), 'media-viewer-'+this.id, el);
  }else {
    $('#mediaVideo-'+this.id).attr('src', stream+'/'+vidFormat.ext);
    if(vidFormat.codec != ''){
      $('#mediaVideo-'+this.id).attr('type', vidFormat.videotype+';codecs="'+vidFormat.codec+'"');
    }
    $('#mediaVideo-'+this.id).attr('onplay', 'viewCountPost(this,"' + streamname  + '")');
    
    $('#mediaVideo-'+this.id)[0].load();
    $('#mediaVideo-'+this.id)[0].play();
  }
}
Gallery.prototype.createFlashVid = function(url, target, el){
  this.video = new SWFObject(url, "mymovie", (portalWidth-14), (portalWidth-14)*0.75, "8", "#336699");
  this.video.addParam("wmode", "transparent");
  this.video.addParam("allowFullScreen", "True");
  this.video.addParam("allowScriptAccess", "always");
  this.video.write(target);
  if(el){
    this.mediaTitle.text(el.data('source'));
  }
}
Gallery.prototype.getVidFormat = function () {
  myTypes = {
    "mp4" : {
      "ext" : "mp4",
      "videotype" : "video/mp4",
      "codec" : ""
    },
    "webm" : {
      "ext" : "webm",
      "videotype" : "video/webm",
      "codec" : "vp8, vorbis"
    },
    "ogv" : {
      "ext" : "ogv",
      "videotype" : "video/ogg",
      "codec" : "theora, vorbis"
    }
  }
  var myVideo = $('#mediaVideo-'+this.id)[0];
  for (mediatype in myTypes){     
    try{

      var canPlay = myVideo.canPlayType(myTypes[mediatype].videotype);
      if ((canPlay=="maybe") || (canPlay=="probably")) {
        return myTypes[mediatype];
      }
    } catch(err){
    
    }
  }
  return {"error" : "nohtml5"};
};


