function VideoStreamCreator() {
	var self = this;
	
    this.vimeoGetImageUrl       = '/externals/planetatv/vimeo.php';
    this.defaultImageVimeo      = '/externals/planetatv/vimeo_logo.png';
	this.linksCollection        = new Array();
    this.linksCollectionImages  = new Array();
    this.pattern                = /^([a-zA-Z]+)_([a-zA-Z0-9\-\_]+)/
    
    this.config                 = {
		defaultWidth: 			500,
		defaultHeight:			315,
		defaultFlashVersion:	'9',
		searchLinkClass:		'videostream',
        searchLinkImageClass:   'videostream_image'
	}

	this.construct = function() {
		self.fetchLinks();
		self.convertLinks();
	}

	this.fetchLinks = function() {
	    $("a[class='" + self.config.searchLinkClass + "']").each( function() {			
			self.linksCollection.push($(this));
		})
                
        $("a[class='" + self.config.searchLinkImageClass + "']").each( function() {			
			self.linksCollectionImages.push($(this));
		})
    }

	this.convertLinks = function() {
        for(i = 0; i < self.linksCollection.length; i++) {  
			self.createMovieObject(self.linksCollection[i]);
		}
                
        for(i = 0; i < self.linksCollectionImages.length; i++) {  
			self.createMovieImageObject(self.linksCollectionImages[i]);
		}
	}
    
    
    this.createMovieImageObject = function(linkObject){
        var movieID             = linkObject.attr('id');
        var movieSourceMatched  = movieID.match(this.pattern);
        var embedId             = movieSourceMatched[2];

        if(movieSourceMatched[1]=='youtube') {                
            var vi = $('<img/>').attr('src','http://i2.ytimg.com/vi/'+embedId+'/default.jpg')[0];
            linkObject.html(vi);
        } else if (movieSourceMatched[1]=='vimeo') {
            $.ajax({
                url:        self.vimeoGetImageUrl,
                data:       'embedId='+embedId,   
                type:       'POST',
                dataType:   'json',
                async:      false,
                success:    function(json) {
                    if (json[0]) {
                        var vimeoImageUrl = json[0].thumbnail_small
                    } else {
                        var vimeoImageUrl = self.defaultImageVimeo
                    }
                    
                    var  vi = $('<img/>').attr('src',vimeoImageUrl).css('border','1px solid #333')[0];
                    linkObject.html(vi);
                }
            });
        }
    }

	this.createMovieObject = function(linkObject) {            
        var movieTitle          = linkObject.attr('title');
		var movieLink           = linkObject.attr('href');
        var movieID             = linkObject.attr('id');
        var movieDims           = new Array();
        var flashContainer      = $('<div class="nest_'+movieTitle+'"></div>');
        var movieSourceMatched  = movieID.match(this.pattern);
        var embedId             = movieSourceMatched[2];

		if (linkObject.attr('rel')) {
			movieDims = linkObject.attr('rel').split("-");
		} else {
			movieDims[0] = self.config.defaultWidth;
			movieDims[1] = self.config.defaultHeight;
		}
      
        if ('youtube' == movieSourceMatched[1]) {
            var objtext = '<iframe width="'+movieDims[0]+'" height="'+movieDims[1]+'" src="'+movieLink+'" frameborder="0" allowfullscreen></iframe>';
        } else if(movieSourceMatched[1]=='vimeo') {                                       
            var objtext = '<iframe src="'+movieLink+'" width="'+movieDims[0]+'" height="'+movieDims[1]+'" frameborder="0"></iframe>'
        }

        flashContainer.html(objtext);
        linkObject.after(flashContainer);
		linkObject.remove();
	}

	self.construct();
}

$(document).ready(function() {
    new VideoStreamCreator();
});
