// ==UserScript==
// @name           Vkontakte Download Video
// @namespace      http://polkila.googlecode.com
// @author         Васютинский Олег http://vasyutinskiy.ru
// @version        9.12
// @include        http://vkontakte.ru/video*
// @include        http://*.vkontakte.ru/video*
// ==/UserScript==

var scriptName = 'VkontakteRu.DownloadVideo';
var scriptVersion = 9.12;
var lang = parseInt(readCookie('remixlang'));

(function(){
	if (!window.ge) window = unsafeWindow;
	var download = lang?'Download':'&#1057;&#1082;&#1072;&#1095;&#1072;&#1090;&#1100;';
	var embed = lang?'Embed code':'Embed &#1082;&#1086;&#1076;';
	var flashVars = document.evaluate("//object[@id='player']//param[@name='flashvars']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
	var swfArgs = readVars(flashVars.value);
	document.getElementById('videoactions').innerHTML+="<a href='http://"+swfArgs['host']+"/assets/videos/"+swfArgs['vtag']+""+swfArgs['vkid']+".vk.flv'>"+download+"</a><div style='color:#45688E;padding:3px;width:100%;'>"+embed+":</div><input type=text value=\"&lt;object width=&quot;460&quot; height=&quot;345&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://gmflowplayer.googlecode.com/files/FlowPlayer.swf&quot;&gt;&lt;/param&gt;&lt;param name=&quot;wmode&quot; value=&quot;transparent&quot;&gt;&lt;/param&gt;&lt;param name=&quot;flashvars&quot; value=&quot;config={playList:[{url:'http://"+swfArgs['host']+"/assets/videos/"+swfArgs['vtag']+""+swfArgs['vkid']+".vk.flv'}], showPlayListButtons:false, initialScale:'scale', showLoopButton:false, allowResize:true, useNativeFullScreen:true, autoBuffering:false, autoPlay:false, loop:false}&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://gmflowplayer.googlecode.com/files/FlowPlayer.swf&quot; type=&quot;application/x-shockwave-flash&quot; wmode=&quot;transparent&quot; width=&quot;460&quot; height=&quot;345&quot; flashvars=&quot;config={playList:[{url:'http://"+swfArgs['host']+"/assets/videos/"+swfArgs['vtag']+""+swfArgs['vkid']+".vk.flv'}], showPlayListButtons:false, initialScale:'scale', showLoopButton:false, allowResize:true, useNativeFullScreen:true, autoBuffering:false, autoPlay:false, loop:false}&quot;&gt;&lt;/embed&gt;&lt;/object&gt;\" style='width:100%;' onclick=this.select() readonly>";
	setTimeout(checkForUpdate, 2000);
})();

function readVars(s){
	var pairs = s.split('&');
	r = [];
	for(var i=0; i<pairs.length; i++){
		t = pairs[i].split('=');
		r[t[0]] = t[1];
	}
	return r;
}

function readCookie(name){
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for (i=0; i<ca.length; i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else expires = '';
	document.cookie = name+'='+value+expires+'; path=/';
}

function checkForUpdate(){
	if (readCookie(scriptName+'.checked')) return;
	GM_xmlhttpRequest({
		method: 'GET',
		url: 'http://polkila.googlecode.com/svn/trunk/'+scriptName+'.ver',
		onload: function(result){
			createCookie(scriptName+'.checked', true, 3);
			newVer = parseFloat(result.responseText);
			if (newVer>scriptVersion && confirm(lang?'There is a new version of '+scriptName+' script.\nDo You want to update?':'Появилась новая версия скрипта '+scriptName+'.\nОбновить?')){
				location.href = 'http://polkila.googlecode.com/files/'+scriptName+'.'+newVer+'.user.js';
			}
		}
	});
}

