$(document).ready(function(){
	// flash embed properties for SWFObject
	var videoPlayerProperties = {
		
		playerInfo: {
			location: 'http://interactive.foxbusiness.com/projects/video-player/video-player.swf',
			flashVersion: '9.0.115',
			expressInstall: 'http://interactive.foxbusiness.com/swf/swfobject/expressInstall.swf'
		},
		
		flashVars: {
			core_ads_enabled: 'false',
			core_swf_url: 'http://interactive.foxnews.com/projects/mmcore/mmcore.swf',
			core_omniture_player_name: 'FOX Business',
			core_omniture_account: 'foxnewsbusinessmaven',
			core_yume_ad_domain_code: '109DLWefYRt',
			core_yume_ad_server_domain: 'pl.yumenetworks.com',
			core_ad_player_name: 'smallinpage',
			core_yume_ad_library_url:'http://interactive.foxbusiness.com/swf/yume/yume_ad_library.swf',
			core_yume_player_url:'http://interactive.foxbusiness.com/swf/yume/yume_player_4x3.swf', 
			auto_play:'false'
		},

		
		playerParams: {
			bgcolor: '#000000',
			wmode: 'opaque',
			scale: 'noScale',
			menu: 'false',
			allowScriptAccess: 'always',
			allowFullScreen: 'true'
		},
		
		dimensions: { // class declared attribute
			'carousel-video': [300,170]
		},
		
		markupVideoTriggers: {
			clickThruImage: true,
			setLandingPage: { 
				baseLink: "http://video.foxsmallbusinesscenter.com" 
			}
		}
		
	};
	// Instantiate player globally
	window.CarouselVideoPlayer = new MvPlayer();
	CarouselVideoPlayer.init('CarouselVideoPlayer', 'videoid', videoPlayerProperties);
	CarouselVideoPlayer.embedPlayers();

});

//Multiple Video Player - Minimum Version. Last Updated: 11/09/2009
//Dependencies: swfobject, jquery
function MvPlayer() {
	this.playTimeout = {};
	this.properties = {};
	this.dataObj = {};
	this.holderObj = {};
	this.playerIds = [];
	this.videoFormats = [];
	this.init = function(namespace,instanceId,playerProperties) {
		this.properties['playerProperties'] = playerProperties;
		this.properties['instanceId'] = (typeof(instanceId)==undefined || instanceId==null)?'videoid:':instanceId+':';
		this.properties['namespace'] = namespace;
	}
};

MvPlayer.prototype.embedPlayers = function() {
	var vProp = this.properties;
	var thisObj = this;
	var playerProperties = vProp['playerProperties'];
	var isClickThru = playerProperties['markupVideoTriggers']['clickThruImage'];
	$(".video").each(function() { // Search for all video items
		var videoMarkup = this;
		thisObj.dataObj['videoHolders'] = {};
		$("div[id^='"+vProp['instanceId']+"']",videoMarkup).each(function(i) { // Get all divs with player instanceId ids
			var id = $(this).attr("id");
			thisObj.holderObj[id] = this;
			// Only specified formats
			var validFormat = false;
			for (f in playerProperties['dimensions']) {
				if ($(this).hasClass(f)) { validFormat=true; break; } 
			}
			if (validFormat) {
				thisObj.playerIds.push(id); // store player ids
				thisObj.videoFormats[id] = f;
				if (isClickThru) {
					thisObj.getVideoData(id);
				} else {
					thisObj.embedPlayer(id);
				}
			}
		});
	});
};

MvPlayer.prototype.embedPlayer = function(id) { // Embed a specific video
	var vProp = this.properties;
	var thisObj = this;
	var playerProperties = vProp['playerProperties'];
	var Hlpr = this._helper;
	var playerInfo = playerProperties['playerInfo'];
	var dimensions = playerProperties['dimensions'];
	var flashVars = playerProperties['flashVars'];
	var playerParams = playerProperties['playerParams'];
	// Initialize player dimensions to default
	var format = thisObj.videoFormats[id];
	var playerWidth = dimensions[format][0];
	var playerHeight = dimensions[format][1];
	// Build Embed swfobject
	swfobject.embedSWF(playerInfo['location'],id,playerWidth,playerHeight,playerInfo['flashVersion'],playerInfo['expressInstall'],flashVars, playerParams);
	thisObj.loadVideoAttempt(id); // Load Video Player
	
};

MvPlayer.prototype.clickThruImage = function(data) {
	var thisObj = this;
	var Hlpr = this._helper;
	var vProp = this.properties;
	var oVideo = Hlpr.videoObjectJSON(data);
	var id = vProp['instanceId']+oVideo['guid'];
	if (!this.dataObj[id]) { this.dataObj[id] = oVideo; }
	var videoHolders = thisObj.holderObj[id];
	$(videoHolders).each(function(i) {
		var thisVidId = $(this).attr("id").substr(((vProp['instanceId']).length));
		if (parseInt(thisVidId)==parseInt(oVideo['guid'])) {
			var html = '<a style="display:block;visibility:visible" href="javascript:void(0);"><img src="'+oVideo['thumbnail']+'" /></a>';
			$(this).html(html);
			$("a",this).click(function(){ thisObj.embedPlayer(id); })
				.hover(function(){ 
					$("img",this).addClass("hover");
				},function(){ 
					$("img",this).removeClass("hover");
				});
		}
	});
}

MvPlayer.prototype.loadVideoAttempt = function(videoId) {
	var vProp = this.properties;
	var playerProperties = vProp['playerProperties'];
	var thisObj = this;
	var isClickThru = playerProperties['markupVideoTriggers']['clickThruImage'];
	if (document.getElementById(videoId).playFile) {
		clearTimeout(this.playTimeout[videoId]);
		if (this.dataObj[videoId]) { 
			var autoPlay = (isClickThru)?true:false;
			document.getElementById(videoId).playFile(this.dataObj[videoId],autoPlay);
		} else { this.getVideoData(videoId,false); }
		
	} else {
		this.playTimeout[videoId] = setTimeout(function() { thisObj.loadVideoAttempt(videoId); },100);
	}
};

MvPlayer.prototype.getVideoData = function(videoId) {
	var vProp = this.properties;
	var Hlpr = this._helper;
	videoId = videoId.substr(((vProp['instanceId']).length));
	var callbackFunction = vProp['namespace']+'.parseFeed'; //must provide namespace since this callback will be triggered from outside the object scope
	$.getJSON('http://video.foxbusiness.com/feed/video/'+videoId+'.js?callback='+callbackFunction+'&jsonp=?'); //jsonp call
};

MvPlayer.prototype.mute = function(videoId) {
	var vProp = this.properties;
	document.getElementById(vProp['instanceId']+videoId).mute();
};

MvPlayer.prototype.unmute = function(videoId) {
	var vProp = this.properties;
	document.getElementById(vProp['instanceId']+videoId).unmute();
};

MvPlayer.prototype.unpause = function(videoId) {
	var vProp = this.properties;
	document.getElementById(vProp['instanceId']+videoId).unpause();
};

MvPlayer.prototype.play = function(videoId) {
	this.unpause(videoId);
};

MvPlayer.prototype.pause = function(videoId) {
	var vProp = this.properties;
	document.getElementById(vProp['instanceId']+videoId).pause();
};

MvPlayer.prototype.playerState = function(videoId) { // states: pause,stopped,playing,error
	var vProp = this.properties;
	var state = document.getElementById(vProp['instanceId']+videoId).playerState();
	return state;
};

MvPlayer.prototype.parseVideoFeed = function(data) {
	var Hlpr = this._helper;
	var vProp = this.properties;
	var oVideo = Hlpr.videoObjectJSON(data);
	var id = vProp['instanceId']+oVideo['guid'];
	this.dataObj[id] = oVideo;
	document.getElementById(vProp['instanceId']+oVideo['guid']).playFile(oVideo, false);
	return id;
};

MvPlayer.prototype.parseFeed = function(data) {
	var vProp = this.properties;
	var Hlpr = this._helper;
	var playerProperties = vProp['playerProperties'];
	var markUpVideoTriggers = playerProperties['markupVideoTriggers'];
	var isClickThru = playerProperties['markupVideoTriggers']['clickThruImage'];
	var id;
	if (isClickThru) { id=this.clickThruImage(data); } else { id=this.parseVideoFeed(data); }	
};

MvPlayer.prototype._helper = {
	convertToRtmp: function(url) {
		return url.replace('http://media2.foxnews.com/','rtmp://cp71330.edgefcs.net/ondemand/foxnews/').replace('.flv','');
	},
	convertDate: function(d) {
		if (typeof(d) != 'undefined') {
			var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
			var date = d.toString().split('T');
			date = date[0].split('-');
			date = new Date(date[0],(date[1]-1),date[2]);
			return months[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear();
		} else {
			return '';
		}
	},
	extractVideoId: function(str) {
		var s = str.replace(/^(http:\/\/)?[a-zA-Z0-9\.]+/,'');
		s = s.indexOf('#')==0 ? s.substring(2) : s.substring(1);
		return s.substring(0,s.indexOf('/'));
	},
	extractCategoryId: function(str) {
		return str.split('category_id=')[1];
	},
	cleanTitle: function(title) {
		title = title.replace(/ /g,'-').toLowerCase();
		return title.replace(/[^a-z0-9\-]/g,'');
	},
	cleanHref: function(href) { // necessary for ie 6 link cleanup
		return href.replace(/^(http:\/\/)?[a-zA-Z0-9\.]+/,'');
	},
	cleanImageUrl: function(url) {
		return url.replace('http://media2.foxnews.com','');
	},
	convertTime: function(sec) {
		var minutes = Math.floor(sec/60);
		var seconds = Math.floor(sec%60);
		if (seconds.toString().length == 1) seconds = '0'+seconds;
		return minutes+':'+seconds;
	},
	videoObjectJSON: function(json) {
		var obj = new Object();
		var item = json.channel.item;
		var details = item['media-content'];
		var credits = details['media-credit'];
		obj.url = this.convertToRtmp(details['@attributes'].url);
		obj.title = item['title'];
		obj.description = details['media-description'];
		obj.keywords = details['media-keywords'];
		obj.thumbnail = details['media-thumbnail'];
		obj.guid = details['mvn-assetUUID'];
		obj.channel = (details['mvn-fnc_channel'])?details['mvn-fnc_channel']:details['mvn-fbn_channel'];
		obj.format = (details['mvn-fnc_format'])?details['mvn-fnc_format']:details['mvn-fbn_format'];
		obj.playlist = details["mvn-fnc_default_playlist"];
		obj.personality = (details['mvn-fnc_personality'])?details['mvn-fnc_personality']:details['mvn-fbn_personality'];
		obj.format = 'fsb';
		obj.show = (details['mvn-fnc_show'])?details['mvn-fnc_show']:details['mvn-fbn_show'];
		obj.category = 'fsb';
		obj.creationDate = this.convertDate(details['mvn-creationDate']);
		obj.shortDescription = details['mvn-shortDescription'];
		obj.loc = 'http://'+window.location.host+window.location.pathname;
		return obj;
	}
}; // Multiple Video Player End