// namespace ADS4GOOD
if (typeof(ADS4GOOD) === 'undefined') {
  ADS4GOOD = new Object();
}

// namespace ADS4GOOD.Adjet
if (typeof(ADS4GOOD.Adjet) === 'undefined') {
  ADS4GOOD.Adjet = new Object();
}

// singleton for ADS4GOOD.Adjet
ADS4GOOD.Adjet = function() {

  return {
  _version: 1,
  render: function() {
    document.write(unescape("%3Cdiv id='ads4good-adjet' class='ads4good-adjet' %3E"));
    if (this.adNetwork == 'k') {
      this.renderKomli();
    } else {
      this.renderGoogle();
    }
    document.write(unescape("%3Cdiv class='ads4good-adjet-footer' %3E"));
    document.write(unescape("%3Ca title='Even you can raise funds for your favorite cause for free - Learn More' style='text-decoration:none' href='http://ads4good.org/why-what-how/' %3E"));
    document.write(unescape("%3Cimg src='" + this.footerImgUrl + "' width='" + this.width + "' border='0' /%3E"));
    document.write(unescape("%3C/a%3E"));
    document.write(unescape("%3C/div%3E"));
	document.write(unescape('%3Cimg src="') +this.beaconImgUrl + unescape('" %3E'));
    document.write(unescape("%3C/div%3E"));
  },
  _getFlashVersion: function(){ 
  	// ie 
  	try { 
    	try { 
      		// avoid fp6 minor version lookup issues 
     		// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
     		var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
     		try { axo.AllowScriptAccess = 'always'; } 
      		catch(e) { return '6,0,0'; } 
    	} catch(e) {} 
    	return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
  		// other browsers 
  	} catch(e) { 
    	try { 
      		if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
       		return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
      		} 
    	} catch(e) {} 
  	} 
  	return '0,0,0'; 
	},
  renderKomli: function() {
    document.write(unescape("%3Ciframe frameborder='0' marginwidth='0' marginheight='0' scrolling='NO' width='" + this.width + "' height='" + this.height + "' src='http://ads.komli.com/st?ad_type=iframe&ad_size=" + this.adType + "&section=712873' %3E%3C/iframe%3E"));
  },
  
  renderGoogle: function() {
    if (this.adType == '160x600') {
      // Show 160x600
      document.write(unescape("%3Cscript%20type%3D%22text%2Fjavascript%22%3E%3C%21--%0Agoogle_ad_client%20%3D%20%22pub-5424209419669468%22%3B%0Agoogle_ad_slot%20%3D%20%225383610390%22%3B%0Agoogle_ad_width%20%3D%20160%3B%0Agoogle_ad_height%20%3D%20600%3B%0A%2F%2F--%3E%0A%3C%2Fscript%3E%0A%3Cscript%20type%3D%22text%2Fjavascript%22%0Asrc%3D%22http%3A%2F%2Fpagead2.googlesyndication.com%2Fpagead%2Fshow_ads.js%22%3E%0A%3C%2Fscript%3E"));
    } else {
      // Show 300x250
      document.write(unescape("%3Cscript%20type%3D%22text%2Fjavascript%22%3E%3C%21--%0Agoogle_ad_client%20%3D%20%22pub-5424209419669468%22%3B%0A%2F*%20300x250%2C%20created%201%2F8%2F10%20*%2F%0Agoogle_ad_slot%20%3D%20%225120594171%22%3B%0Agoogle_ad_width%20%3D%20300%3B%0Agoogle_ad_height%20%3D%20250%3B%0A%2F%2F--%3E%0A%3C%2Fscript%3E%0A%3Cscript%20type%3D%22text%2Fjavascript%22%0Asrc%3D%22http%3A%2F%2Fpagead2.googlesyndication.com%2Fpagead%2Fshow_ads.js%22%3E%0A%3C%2Fscript%3E"));
    }
  }, 
 
  init: function(options) {
    this.options = options;
    this.userid = options.userid;
    this.causeid = options.causeid;
    this.width = options.width;
    this.height = options.height;
    this.adType = '300x250';
    this.adTypeFileSuffix = '';
    this.adNetwork = 'g'; // k - Komli; g - Google
	// beacon image url genaration
    this.beaconImgUrl = 'http://analytics.ads4good.org/?'; 
	this.tracking_params = {
		userid: this.userid,
		causeid: this.causeid,
		ad_type: this.adType,
		flash_version: this._getFlashVersion(),
		referrer: escape(document.referrer),
		url: escape(document.URL),
		host: escape(document.location.host),
		browser_name: navigator.appName,
		browser_version: navigator.appVersion,
		platform: navigator.platform,
		browser_language:(navigator.userLanguage ? navigator.userLanguage : (navigator.language ? navigator.language : 'en')),
		adjet_version: this._version
	};	
	// appending parameters to the beacon url
	for(var param in this.tracking_params){
		this.beaconImgUrl += param + '=' + this.tracking_params[param] + '&';
	}
	// cleaning the url for an extra param seperator
	this.beaconImgUrl = this.beaconImgUrl.replace(/&$/, '');
    // Check for adType
    if (this.width == 160 && this.height == 600) {
      this.adType = '160x600';
      this.adTypeFileSuffix = '-160x600';
    } else {
      this.adType = '300x250';
      this.adTypeFileSuffix = '';
      // Just make sure that the height and width are sane
      this.width = 300;
      this.height = 250;
    }
    
    // Decide the footerImgUrl
    switch(this.causeid){
      case 1:
        this.footerImgUrl = 'http://static.ads4good.org/ads4good-edu' + this.adTypeFileSuffix + '.png';
        break;
      case 2:
        this.footerImgUrl = 'http://static.ads4good.org/ads4good-animal' + this.adTypeFileSuffix + '.png';
        break;
      case 3:
        this.footerImgUrl = 'http://static.ads4good.org/ads4good-poverty' + this.adTypeFileSuffix + '.png';
        break;
      case 4:
        this.footerImgUrl = 'http://static.ads4good.org/ads4good-env' + this.adTypeFileSuffix + '.png';
        break;
      default:
        this.footerImgUrl = 'http://static.ads4good.org/ads4good-poverty' + this.adTypeFileSuffix + '.png';
        break;
    }
  }
  }
}();
