var my = {};

my.vkInit = function () {
	VK.init({apiId: 2235500, nameTransportPath: '/xd_receiver.htm'}); 
}

my.sqldate = "%yyyy-%MM-%dd %HH:%mm:%ss";


//my.vk = false;   //vkontakte

// http://javascript.ru/unsorted/top-10-functions 

// возвращает значение cookie если есть или undefined
function getCookie(name) {
	var matches = document.cookie.match(new RegExp(
	  "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	))
	return matches ? decodeURIComponent(matches[1]) : undefined 
}

// уcтанавливает cookie   
function setCookie(name, value, props) {
	props = props || {}
	var exp = props.expires
	if (typeof exp == "number" && exp) {
		var d = new Date()
		d.setTime(d.getTime() + exp*1000)
		exp = props.expires = d
	}
	if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }

	value = encodeURIComponent(value)
	var updatedCookie = name + "=" + value
	for(var propName in props){
		updatedCookie += "; " + propName
		var propValue = props[propName]
		if(propValue !== true){ updatedCookie += "=" + propValue }
	}
	document.cookie = updatedCookie

}

// удаляет cookie
function deleteCookie(name) {
	setCookie(name, null, { path: "/", expires: -1 })
}


function dtDate (dt) { //returns javascript date
	try{
		var y    = parseInt(dt.substr(0,4),10);
		var M    = parseInt(dt.substr(5,2),10);
		var d    = parseInt(dt.substr(8,2),10);
		var H    = parseInt(dt.substr(11,2),10); H=(H?H:0); 
		var m 	 = parseInt(dt.substr(14,2),10); m=(m?m:0);
		var s    = parseInt(dt.substr(17,2),10); s=(s?s:0);
		return new Date(y,M-1,d,H,m,s);
	}catch(e){ return null;}
}

function i2 (i){
	return (i.toString().length==1? "0"+i:i);
}

// formats sqlite output similarly to C#
function dtFormat(format, dt){
	try{
		var dat = dtDate (dt);
		var y = dat.getFullYear();
		var M    = dat.getMonth()+1;
		var d    = dat.getDate();
		var H    = dat.getHours();
		var m	 = dat.getMinutes();
		var s    = dat.getSeconds();
		var h = H%12; h = (h?h:12);
		var tt = ( H>11 ? "PM" : "AM" );
		var t = ( H>11 ? "P" : "A" );  
		var day = dat.getDay();

		var MMMM = (my.MMMM)[M-1];
		var MMM  = MMMM.substr(0,3);
		var ddd = (my.ddd)[day];
		var dddd = (my.dddd)[day];
		
		var res = format.replace("%yyyy", y).replace("%MMMM",MMMM).replace("%dddd",dddd);
			res = res.replace("%MMM",MMM).replace("%ddd",ddd);
			res = res.replace("%MM",i2(M)).replace("%dd",i2(d));
			res = res.replace("%y",y).replace("%M",M).replace("%d",d);
			res = res.replace("%tt",tt).replace("%hh",i2(h)).replace("%HH",i2(H)).replace("%mm",i2(m)).replace("%ss",i2(s));
			res = res.replace("%t",t).replace("%h",h).replace("%H",H).replace("%m",m).replace("%s",s);
			
		return res;	

	}catch(e){ return "";}
}
