function VastImpressions(params){
	this.id = 'w'+Math.floor(Math.random()*100001);
	if ((typeof window.vimp) != 'object'){
		window.vimp = {};
	}
	window.vimp[this.id] = this;
	this.impressURL = window.location.protocol+'//a.vast.com/impressions';
	this.params = params;
	this.impress();
}
VastImpressions.prototype.impress = function(){
	this.params.domain = this.extractDomainName();
	this.params.aref = encodeURIComponent(document.referrer);
	this.params.aurl = encodeURIComponent(window.location.href.replace(/\#.+$/, ''));
	this.params.uagent = encodeURIComponent(navigator.userAgent);
	this.params.res = screen.width+'x'+screen.height;
	var ab = this.getCookie('V_AB');
	if (ab){this.params.ab = encodeURIComponent(ab);}
	var uid = this.getCookie('V_UID');
	if (!uid){uid = this.createUID();}
	this.logImp({"uid":uid});
}
VastImpressions.prototype.createUID = function(){
	var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
	var string_length = 32;
	var rs = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		rs += chars.substring(rnum,rnum+1);
	}
	return rs;
}
VastImpressions.prototype.extractDomainName = function(){
	var regex = new RegExp("http[s]{0,1}://([a-zA-Z0-9\.\-]+)(\/{0,1}.*)", "i");
	var matched = regex.exec(window.location.href);
	if (matched && matched[1]){
		return encodeURIComponent(matched[1]);
	}
	return false;
}
VastImpressions.prototype.logImp = function(data){
	if (data && data.uid){
		this.params.uid = encodeURIComponent(data.uid);
		this.setCookie('V_UID', data.uid, 365);
		var url = this.impressURL;
		var fr = true;
		for (var i in this.params){
			if (fr){fr = false; url += '?';}else{url += '&';}
			url += i+'='+this.params[i];
		}
		this.doRequest(url);
	}
}
VastImpressions.prototype.doRequest = function(uri) {
	this.insertScript({'src':uri, 'type':'text/javascript'});
}
VastImpressions.prototype.insertScript = function(params, elem) {
	if (params) {
		if (!elem) {
			var scr = document.createElement('script');
		}else{
			var scr = document.createElement(elem);
		}
		for (var i in params) {
			if (i == 'text') {
				scr.innerHTML = params[i];
			}else{
				scr.setAttribute(i, params[i]);
			}
		}
		var head = document.getElementsByTagName('head')[0];
		head.appendChild(scr);
	}
}
VastImpressions.prototype.setCookie = function(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
VastImpressions.prototype.getCookie = function(check_name){
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	for ( i = 0; i < a_all_cookies.length; i++ ){
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ){
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){
		return null;
	}			
}
try{runVastImpression();}catch(e){}
