/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var TC_promo_Base64 = {
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = TC_promo_Base64._utf8_encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
		}
		return output;
	},
	
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = TC_promo_Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}
}
//parse a URL to form an object of properties
function tc_promo_parseURL(url)
{
    //save the unmodified url to href property
    //so that the object we get back contains
    //all the same properties as the built-in location object
	if(url!="") {
		var loc = { "href" : url };

		//split the URL by single-slashes to get the component parts
		var parts = url.replace("//", "/").split("/");

		//store the protocol and host
		loc.protocol = parts[0];
		loc.host = parts[1];

		//extract any port number from the host
		//from which we derive the port and hostname
		parts[1] = parts[1].split(":");
		loc.hostname = parts[1][0];
		loc.port = parts[1].length > 1 ? parts[1][1] : "";

		//splice and join the remainder to get the pathname
		parts.splice(0, 2);
		loc.pathname = "/" + parts.join("/");

		//extract any hash and remove from the pathname
		loc.pathname = loc.pathname.split("#");
		loc.hash = loc.pathname.length > 1 ? "#" + loc.pathname[1] : "";
		loc.pathname = loc.pathname[0];

		//extract any search query and remove from the pathname
		loc.pathname = loc.pathname.split("?");
		loc.search = loc.pathname.length > 1 ? "?" + loc.pathname[1] : "";
		loc.pathname = loc.pathname[0];

		//return the final object
		return loc;
	}
	return false;
}

function tc_promo_setCookie(c_name,value,expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function tc_promo_getCookie(c_name) {
if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function tc_promo_encode(str) {
	var result = str;
	if(result != '') {
		result = TC_promo_Base64.encode(result);
		result = result.replace('/','_');
		result = result.replace('+','-');
		result = result.replace(/=/g,'');
		result = encodeURIComponent(result);
	}

	return result;
}

function tc_promo_decode(str) {
	var result = str;
	if(result != '') {
		result = decodeURIComponent(result);
		result.replace('_','/');
		result.replace('-','+');
		var len = result.length();
		while(len % 4 != 0) {
			len++;
			result += "=";
		}
		result = TC_promo_Base64.decode(result);
	}
	return result;
}

// checks for promotool id
if (pid != undefined) {
	var url;
	var def_width=630;
	var def_height=3000;
	//controls if owner already captured for this page
	if(owner == undefined) {
		var owner = window.document.URL;
		owner = tc_promo_encode(owner);
	}
	// controls if referrer already captured for this page and site
	if(tc_ref == undefined) {
		var tc_ref;
		// gets referrer hostname
		var tc_ref_host = tc_promo_parseURL(window.document.referrer);
		// compares referrer with actual page host name, if different, save referrer
		if (location.host != tc_ref_host.hostname) {
			tc_ref = window.document.referrer;
			if (tc_ref != "") {
				tc_ref = tc_promo_encode(tc_ref);
			}
		} else { // if equal, return saved referrer, if does not have one, save current
			tc_ref = tc_promo_getCookie("tc_referrer");
			if (tc_ref == "") {
				tc_ref = window.document.referrer;
				if (tc_ref != "") {
					tc_ref = tc_promo_encode(tc_ref);
				}
			}
		}
		tc_promo_setCookie("tc_referrer",tc_ref,null);
	}
	/* -> test manual referrer
		tc_ref = "http://www.bing.com/search?q=stringlover&go=&form=QBLH&filt=all";
		tc_ref = TC_promo_Base64.encode(tc_ref);
		tc_ref = tc_ref.replace('/','_');
		tc_ref = encodeURIComponent(tc_ref);
	*/
	// controls if this promotool already exists in the current page
	var promotool_inuse=false;
	if(promotools == undefined) {
		var promotools = new Array();
		promotools[0]=pid;
	} else {
		for( var i in promotools) {
			if(promotools[i]==pid) {
				promotool_inuse=true;
				break;
			}
		}
		if(promotool_inuse==false) promotools.push(pid);
	}
	// -------------------------------------------------------------
	// Controls declared/default width and height
	if(width == "0")
			var width=def_width;
	if(height == "0")
			var height=def_height;
	// -------------------------------------------------------------
	// prepares url parameters
		// count view or not
		var countview = "";
		if(promotool_inuse==true) countview="/cv/"+1;
		// owner
		var v_own = "/own/"+owner;
		// ref
		var v_ref = (tc_ref != "") ? "/ref/"+tc_ref : "";
	//construct iframe with url
	width+=2; //height+=2;

var niche_url="";
if(typeof(niche)!="undefined") {
	niche_url="/cat/"+niche;
}
// Production URL
	url='<iframe id="promotool_'+pid+'" width="'+width+'px" height="'+height+'"px" frameborder=0 src="http://promo.vador.com/index.php/promotool/index/id/'+pid+v_own+v_ref+countview+'" style="overflow-x:hidden">Your browser does not support iframes.</iframe>';
// Development URL
//	url='<iframe id="promotool_'+pid+'" width="'+width+'px" height="'+height+'"px" frameborder=0 allowTransparency="allowTransparency" src="http://www.promotools.com/index.php/promotool/index/id/'+pid+niche_url+v_own+v_ref+countview+'" style="overflow-x:hidden">Your browser does not support iframes.</iframe>';
// Development test ref from goolge
//vteste = '/own/aHR0cDovL3d3dy5zdHJpbmdsb3Zlci5jb20v/ref/aHR0cDovL3d3dy5iaW5nLmNvbS9zZWFyY2g_cT1zdHJpbmdsb3ZlciZnbz0mZm9ybT1RQkxIJmZpbHQ9YWxs';
//url='<iframe id="promotool_'+pid+'" width="'+width+'px" height="'+height+'"px" frameborder=0 src="http://www.promotools.com/index.php/promotool/index/id/'+pid+vteste+countview+'" style="overflow-x:hidden">Your browser does not support iframes.</iframe>';

	document.write(url);
} else {
	document.write('undeclared variable "pid"');
}

