; // you never know if a previous piece of JS is closed properly 


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18828939-2'], ['_trackPageview']);

var zoominPlayer = zoominPlayer || (function() {

	var genericZoomin = new GenericZoomin;

	function GenericZoomin() {
	    var self = {
          modules		: {
            external	: {}
          },
	        videos: [],
	        options: {
	            pid: 'culturaencadenaesdp',
	            cntry: 'es',
	            clng: 'spa',
	            domainName: 'http://blackbird.zoomin.tv',
	            assetsLink: 'http://blackbird.zoomin.tv/FloatingPlayers/',
       				bgColor		: 'transparent',
              blanketColor: '#000',
              borderWidth	: '0px',
              borderColor	: 'transparent',
              category	: '',
              elementid	: 'zoominPlayer',
              galleryMargin: '0px',
              imgSize		: '150x150',
              margin		: '0px',
              opacity		: 0.8,
              videoAmount	: 1,
              videoid		: ''
	        }

	    };

		return self;
	}
	return genericZoomin;
})();

zoominPlayer.modules.external.googleAnalitics = (function( d ) {
	var ga = d.createElement('script'),
		s = d.getElementsByTagName('script')[0]; 
		ga.async = ga.src = ('https:' == d.location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js';
		s.parentNode.insertBefore(ga, s);

})(document);

zoominPlayer.modules.init = (function(win) {
	win.zoominFloatingPlayer	= zoominPlayer;
	jQuery.fn.ready( function() {
		jQuery('body').delegate('.zoomin-floating-player-link', 'click', function() {
			var el = jQuery(this)
				videoId = null,
				opacity = null,
				blanketColor = zoominPlayer.options.blanketColor;

			if ( el.data('zoominVideoId') || el.data('zoomin-video-id') ) {
				videoId = ( el.data('zoominVideoId') ? el.data('zoominVideoId') : el.data('zoomin-video-id') );
			}
			if ( el.data('zoominVideoOpacity') || el.data('zoomin-video-opacity') ) {
				opacity = ( el.data('zoominVideoOpacity') ? el.data('zoominVideoOpacity') : el.data('zoomin-video-opacity') );
			}
			if ( el.data('zoominVideoBlanketColor') || el.data('zoomin-video-blanket-color') ) {
				blanketColor = ( el.data('zoominVideoBlanketColor') ? el.data('zoominVideoBlanketColor') : el.data('zoomin-video-blanket-color') );
			}
			zoominPlayer.modules.floatingPlayer.show(videoId, opacity, blanketColor);
		});
	});
})(window);



zoominPlayer.modules.videoImage = (function() {
	var videoImage = new VideoImage();

	function VideoImage() {	
		var self = this;
		self.defaultOptions = zoominPlayer.options;
		self.validImageSizes = {
			'default'	: {
				imgWidth	: 150,
				imgHeight	: 150
			},
			'50x50'		: {
				imgWidth	: 50,
				imgHeight	: 50
			},
			'75x75'		: {
				imgWidth	: 75,
				imgHeight	: 75
			},
			'100x100'	: {
				imgWidth	: 100,
				imgHeight	: 100
			},
			'125x125'	: {
				imgWidth	: 125,
				imgHeight	: 125
			},
			'150x150'	: {
				imgWidth	: 150,
				imgHeight	: 150
			},
			'200x200'	: {
				imgWidth	: 200,
				imgHeight	: 200
			},
			'145x105'	: {
				imgWidth	: 145,
				imgHeight	: 105
			},
			'300x190'	: {
				imgWidth	: 300,
				imgHeight	: 190
			},
			'300x300'	: {
				imgWidth	: 300,
				imgHeight	: 300
			},
			'400x300'	: {
				imgWidth	: 400,
				imgHeight	: 300
			},
			'400x400'	: {
				imgWidth	: 400,
				imgHeight	: 400
			},
			'590x400'	: {
				imgWidth	: 590,
				imgHeight	: 400
			}
		};
	}

	VideoImage.prototype.init = function ( options ) {
		var self = this;
		self.options = jQuery.extend( {}, self.defaultOptions, options );
		if ( typeof options.imgSize === 'undefined' || !self.validImageSizes[ options.imgSize ]) {
			options.imgSize = 'default';
		}
		self.options.imgWidth	= self.validImageSizes[ options.imgSize ].imgWidth;
		self.options.imgHeight	= self.validImageSizes[ options.imgSize ].imgHeight;



		var jsonlink	= zoominPlayer.options.domainName + '/programxml/.json?feedtype=json&callback=?&pid=' + zoominPlayer.options.pid  + '&width=' + self.options.imgWidth + '&height=' + self.options.imgHeight + '&cat=' + self.options.category;

		if(self.options.videoid === 'undefined' || self.options.videoid === '') {
			zoominPlayer.options.jsonlink = jsonlink + '&quantity=' + self.options.videoAmount;
		} else {
			zoominPlayer.options.jsonlink = jsonlink + '&aid=' + self.options.videoid;
		}

		if ( self.options.videoAmount > 6 ) {
			self.options.videoAmount = 6;
		}
	};

    VideoImage.prototype.listeners = function() {
        jQuery.fn.ready(function() {
            zoominPlayer.videos = zoominVideoImage;
            for (var i = 0, l = zoominPlayer.videos.length; i < l; i++) {
				zoominPlayer.videos[ i ].VideoImage = new VideoImage();
				zoominPlayer.videos[ i ].VideoImage.init( zoominPlayer.videos[ i ].options );
				zoominPlayer.videos[ i ].VideoImage.buildHTML( i );
            }
        });
    };

	VideoImage.prototype.buildHTML= function( arrayIndex ) {
		var self = this,
			html = '';

                jQuery.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8" , cache:false});
		jQuery.getJSON(zoominPlayer.options.jsonlink + '' , {} , function( data ) {
			var imgSpriteUrl = zoominPlayer.options.domainName + '/Images/.jpg?width='+self.options.imgWidth+'&height='+self.options.imgHeight+'&aid='+ data.assetids;
			jQuery.each(data.programme, function(itemNr, item){ 

				html += '<div class="zoomin-video-image-content zoomin-floating-player-link" style="border: '+ self.options.borderWidth +' solid ' + self.options.borderColor + '; margin: '+ self.options.margin +'; width: ' + self.options.imgWidth + 'px !important;line-height: ' + self.options.imgHeight + 'px !important ;height: ' + self.options.imgHeight + 'px !important ;background: url(' + imgSpriteUrl + ') 0 '+ ( -self.options.imgHeight * itemNr )+'px !important" data-zoomin-video-id="' + item.programmeid + '" data-zoomin-video-opacity="' + self.options.opacity+ '" data-zoomin-video-blanket-color="' + self.options.blanketColor + '">';
				html += 	'<img class="zoomin-video-image-playbutton" style="margin-top: ' + self.getPositionY( self.options.imgHeight) + '" width="32" height="32" src="' + zoominPlayer.options.assetsLink + 'img/play.png">';
				html += 	'<div class="zoomin-video-image-caption">';
				html += 		'<div class="zoomin-video-image-caption-text zoomin-video-image-caption-text-' + self.getFontSizeClassName( self.options.imgWidth , self.options.imgHeight ) + '">'+item.title+'</div>';
				html += 	'</div>';
				html += '</div>';

			}); 	
			html = '<div id="'+ self.options.elementid+'-data" class="zoomin-video-image-wrapper zoomin-video-image-gallery" style="margin-left: ' + self.options.galleryMargin + '">' + html + '</div>';
      if( self.options.videoAmount > 1 ) {
      html += '<a class="zoomin-powered" href="http://www.zoomin.tv/ "target="_blank">Powered by zoomin.tv (2011)</a>';
      }
      jQuery( '#' + self.options.elementid).css({'backgroundColor' : self.options.bgColor}).prepend(html);
		});
	};
	VideoImage.prototype.getPositionY = function( x ) {
		return Math.round( (Number(x) - 32) / 3 ) + 'px';
	};
	VideoImage.prototype.getFontSizeClassName = function( width, height ) {
		width	= Number(width);
		height	= Number(height);

		if (width < 150 || height < 150) {
			return 'small';
		} else if ( width >= 300 || height >= 300) {
			return 'large';
		}
		return 'medium';
	};

	videoImage.listeners();

	return videoImage;
})();





zoominPlayer.modules.floatingPlayer = (function() {
	var self = {
		settings	: {
			width		: 650,
			height		: 432
		},
		methods	: {
			init		: function() {
				self.methods.generatePlayerHtml();
				jQuery('body').delegate('#zoomin-floating-player-close', 'click', function() {
					jQuery('#zoomin-blanket, #zoomin-floating-player').hide().html('');
				});
			}, 

			generatePlayerHtml	: function() {
				var blanketHtml = '<div id="zoomin-blanket"></div>',
				playerHtml = '<div id="zoomin-floating-player"></div>';
				jQuery('body').append( blanketHtml + playerHtml );
			},
			generateSwfHtml		: function( videoId ) {
				var html = '';

				html += '<object id="content-zoominPlayer_169" height="'+self.settings.height+'" width="'+self.settings.width+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">';
				html += '	<param value="http://cdn.zoomin.tv/video/zoominPlayer_169_v2.swf?pid=culturaencadenaesdp" name="movie" />';
				html += '	<param value="#ffffff" name="bgcolor" />';
				html += '	<param value="high" name="quality" />';
				html += '	<param value="noScale" name="scaleMode" />';
				html += '	<param value="true" name="allowFullScreen" />';
				html += '	<param value="always" name="allowScriptAccess" />';
				html += '	<param value="transparent" name="wmode" />';
				html += '	<param value="zoominPlayer_169" name="name" />';
                html += '	<param value=adServer=http://blackbird.zoomin.tv/adhandler/.adm&amp;adType=zoomin&amp;autoPlayit=true&amp;video=' + videoId + '&amp;cntry=es&amp;language=spa&amp;expandable=true" name="flashvars" />';
                html += '	<embed width="' + self.settings.width + '" height="' + self.settings.height + '" flashvars="adServer=http://blackbird.zoomin.tv/adhandler/.adm&amp;adType=zoomin&amp;autoPlayit=true&amp;video=' + videoId + '&amp;cntry=es&amp;language=spa&amp;expandable=true" name="zoominPlayer_169" style="" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" scalemode="noScale" quality="high" bgcolor="#FFFFFF" id="content-zoominPlayer_169" src="http://cdn.zoomin.tv/video/zoominPlayer_169_v2.swf?pid=culturaencadenaesdp" type="application/x-shockwave-flash">';
				html += '</object>';
				html += '<a id="zoomin-floating-player-close">&times;</a>';

				jQuery('#zoomin-floating-player').html( html );
			},
			show: function ( videoId, opacity, blanketColor ) {
				if (!opacity) {
					opacity = 0.8;
				}

				self.methods.generateSwfHtml( videoId );
				jQuery('#zoomin-floating-player').fadeIn(150);
				jQuery('#zoomin-blanket').css({ 'opacity': opacity, 'backgroundColor': blanketColor }).show();
			}
		}
	};
	self.methods.init();
	return {
		show: function( videoId, opacity, blanketColor ) {
			return self.methods.show( videoId, opacity, blanketColor);
		}
	};
})();



