(function () {

	Just.common = function () {
	};

	Just.common.video = function () {
	};
	Just.common.video.getType = function (href) {
		var type;
		if (href.match(/youtube\.com\/watch/i)) {
			type = 'youtube';
		}
		else if (href.match(/youtube\.com\/embed/i)) {
			type = 'youtube_embed';
		}
		else if (href.match(/metacafe\.com\/watch/i)) {
			type = 'metacafe';
		}
		else if (href.match(/google\.com\/videoplay/i)) {
			type = 'google';
		}
		else if (href.match(/dailymotion\.com\/video/i)) {
			type = 'dailymotion';
		}
		else if (href.match(/blip\.tv\/play/i)) {
			type = 'blip';
		}
		else if (href.match(/myspace\.com\/video/i)) {
			type = 'myspace';
		}
		else if (href.match(/hulu\.com\/watch/i)) {
			type = 'hulu';
		}
		else if (href.match(/revver\.com\/video/i)) {
			type = 'revver';
		}
		else if (href.match(/veoh\.com\/watch/i)) {
			type = 'veoh';
		}
		else if (href.match(/vimeo\.com\//i)) {
			type = 'vimeo';
		}
		else if (href.match(/smotri\.com\/video\/view/i)) {
			type = 'smotri';
		}
		else if (href.match(/vkontakte\.ru\/video/i)) {
			type = 'vkontakte';
		}
		else if (href.match(/vk.com\/video/i)) {
			type = 'vk';
		}
		else if (href.match(/rutube\.ru\/tracks/i)) {
			type = 'rutube';
		}
		else if (href.match(/video\.mail\.ru\/inbox/i)) {
			type = 'mailru';
		}
		else if (href.match(/video\.qip\.ru\/video/i)) {
			type = 'qipru';
		}
		else if (href.match(/v\.kiwi\.kz\/v/i)) {
			type = 'kiwikz';
		}
		else if (href.match(/kiwi\.kz\/watch/i)) {
			type = 'kiwi_watch';
		}
		else if (href.match(/\.mov/i)) {
			type = 'mov_file';
		}
		else if (href.match(/\.wmv/i) || href.match(/\.asx/i)) {
			type = 'wmv_file';
		}
		else if (href.match(/\.flv/i)) {
			type = 'flv_file';
		}
		return type;
	};
	Just.common.video.getImage = function (href) {
		var path = '', videoId = [];
		switch (Just.common.video.getType(href)) {
			case 'youtube':
				path = 'http://i2.ytimg.com/vi/' + href.split('=')[1].split('&')[0] + '/default.jpg';
				break;
			case 'youtube_embed':
				path = 'http://i2.ytimg.com/vi/' + href.split('/')[4] + '/default.jpg';
				break;
			case 'metacafe':
				path = 'http://gen.metacafe.com/thumb/' + href.split('/')[4] + '/0/0/0/0/' + href.split('/')[5] + '.jpg';
				break;
			case 'revver':
				path = 'http://frame.revver.com/frame/120x90/' + href.split('/')[4] + '.jpg';
				break;
			case 'kiwi_watch':
			case 'kiwikz':
				path = 'http://im8.asset.kwimg.kz/screenshots/small/' + href.split('/')[4].substr(0, 2) + '/' + href.split('/')[4] + '_1.jpg';
				break;
			case 'vimeo':
			case 'google':
			case 'dailymotion':
			case 'blip':
			case 'myspace':
			case 'hulu':
			case 'veoh':
			case 'smotri':
			case 'rutube':
			case 'mailru':
			case 'qipru':
			case 'mov_file':
			case 'flv_file':
			case 'wmv_file':
				path = 'http://www.thumbl.in/api/url/?url=' + href;
				break;
			case 'vkontakte':
			case 'vk':
				path = 'http://www.thumbl.in/api/url/?url=http%3A%2F%2Fvkontakte.ru%2Fvideo' + /\/video(.*)/ig.exec(href)[1];
				break;
			default:
				path = 'css/video_icon.png';
				break;
		}
		return path;
	};
	Just.common.video.getLink = function (href, opts) {
		var link, type = Just.common.video.getType(href);
		switch (type) {
			case 'youtube':
				link = "http://www.youtube.com/v/" + href.split('=')[1].split('&')[0] + '?rel=0&version=3';
				break;
			case 'youtube_embed':
				link = "http://www.youtube.com/v/" + href.split('/')[4] + '?rel=0&version=3';
				break;
			case 'metacafe':
				link = "http://www.metacafe.com/fplayer/" + href.split('/')[4] + "/.swf";
				break;
			case 'google':
				link = "http://video.google.com/googleplayer.swf?docId=" + href.split('=')[1] + "&hl=en";
				break;
			case 'dailymotion':
				link = '<iframe frameborder="0" width="' + opts.options.width + '" height="' + opts.options.height + '" src="http://www.dailymotion.com/embed/video/' + href.replace(/(.*)video\/(.*?)_(.*)/, '$2') + '?theme=none&wmode=transparent"></iframe>';
				break;
			case 'smotri':
				link = 'http://pics.smotri.com/player.swf?file=' + href.split('=')[1] + '&bufferTime=3&autoStart=false&str_lang=rus&xmlsource=http%3A%2F%2Fpics.smotri.com%2Fcskins%2Fblue%2Fskin_color.xml&xmldatasource=http%3A%2F%2Fpics.smotri.com%2Fskin_ng.xml';
				break;
			case 'vk':
			case 'vkontakte':
				var src = 'http://' + (type == 'vk' ? 'vk.com' : 'vkontakte.ru') + '/video_ext.php';
				if (href.match(/\?oid=/)) {
					src += /\?(.*)/ig.exec(href);
				} else {
					var videoId = /video(.*?)_(.*?)($|\?)/ig.exec(href);
					var hash = /hash=(.*?)($|&)/ig.exec(href);
					src += '?oid=' + videoId[1] + '&id=' + videoId[2] + '&hash=' + hash[1] + '&hd=1';
				}
				link = '<iframe frameborder="0" width="' + opts.options.width + '" height="' + opts.options.height + '" src="' + src + '"></iframe>';
				break;
			case 'revver':
				link = "http://flash.revver.com/player/1.0/player.swf?mediaId=" + href.split('/')[4];
				break;
			case 'veoh':
				link = "http://www.veoh.com/static/swf/veoh/MediaPlayerWrapper.swf?permalinkId=" + href.split('/')[4];
				break;
			case 'vimeo':
				link = '<iframe frameborder="0" width="' + opts.options.width + '" height="' + opts.options.height + '" src="http://player.vimeo.com/video/' + href.split('/')[3] + '?title=0&byline=0&portrait=0"></iframe>';
				break;
			case 'rutube':
				link = "http://video.rutube.ru/" + href.split('=')[1];
				break;
			case 'mailru':
				var _vid = href.split('/');
				var number = _vid[6].split('.');
				opts.params.flashvars = 'movieSrc=inbox/' + _vid[4] + '/' + _vid[5] + '/' + number[0];
				link = opts.attributes.data = opts.params.movie = "http://img.mail.ru/r/video2/player_v2.swf";
				break;
			case 'qipru':
				link = 'http://pics.video.qip.ru/player.swf?file=' + href.split('=')[1] + '&bufferTime=3&autoStart=false&str_lang=rus&xmlsource=http%3A%2F%2Fpics.video.qip.ru%2Fcskins%2Fqip%2Fskin_color.xml&xmldatasource=http%3A%2F%2Fpics.video.qip.ru%2Fskin_ng.xml';
				break;
			case 'kiwikz':
				link = '<iframe title="Kiwi player" width="' + opts.options.width + '" height="' + opts.options.height + '" src="' + href + '" frameborder="0" allowfullscreen></iframe>';
				break;
			case 'kiwi_watch':
				link = 'http://v.kiwi.kz/v/' + href.split('/')[4];
				break;
			case 'mov_file':
				if (navigator.plugins && navigator.plugins.length) {
					link = '<object id="qtboxMovie" type="video/quicktime" codebase="http://www.apple.com/qtactivex/qtplugin.cab" data="' + href + '" width="' + opts.options.width + '" height="' + opts.options.height + '"><param name="src" value="' + href + '" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
				} else {
					link = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="' + opts.options.width + '" height="' + opts.options.height + '" id="qtboxMovie"><param name="src" value="' + href + '" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
				}
				break;
			case 'wmv_file':
				link = '<object NAME="Player" WIDTH="' + opts.options.width + '" HEIGHT="' + opts.options.height + '" align="left" hspace="0" type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"><param NAME="URL" VALUE="' + href + '"></param><param NAME="AUTOSTART" VALUE="false"/><param name="showControls" value="true"/><embed WIDTH="' + opts.options.width + '" HEIGHT="' + opts.options.height + '" align="left" hspace="0" SRC="' + href + '" TYPE="application/x-oleobject" AUTOSTART="false"></embed></object>';
				break;
			case 'flv_file':
				link = opts.options.flvplayer + "?file=" + href;
				break;
			default:
				link = href;
				break;
		}
		return link;
	};
	Just.common.video.getCode = function (link, opts) {
		var code;
		if (link.substr(0, 1) === '<') {
			code = link;
		} else {
			opts.attributes.width = opts.options.width;
			opts.attributes.height = opts.options.height;
			opts.attributes.src = opts.attributes.data = link;
			code = '<object type="application/x-shockwave-flash"';
			for (var a in opts.attributes) {
				code += ' ' + a + '="' + opts.attributes[a] + '"';
			}
			code += '>';
			for (var p in opts.params) {
				code += '<param name="' + p + '" value="' + opts.params[p] + '"/>';
			}
			code += '</object>'
		}
		return code;
	};
	Just.common.video.gallery = function (options) {
		var defaults, params, attributes, code, link, self;
		defaults = {
			width:400,
			height:300,
			flvplayer:false,
			callback:function () {}
		};
		options = $.extend(true, defaults, options);
		var o = {options:options, params:params, attributes:attributes};
		o.params = {'wmode':'transparent'};
		o.attributes = {'width':options.contentsWidth, 'height':options.contentsHeight, 'id':'flvvideo'};
		self = Just.common.video;
		return Just.$.each(function () {
			var href = $(this).data('link');
			if (href.length) {
				this.type = self.getType(href);
				try {
					this.link = self.getLink(href, o);
					this.code = self.getCode(this.link, o);
				} catch (e) {
					this.code = $(this).data('code');
				}
				this.image = self.getImage(href);
			} else {
				this.code = $(this).data('code');
			}
			options.callback.call(this);
		});
	}

})(Just);
