

var adCheck = {
  uriParse : /[\?\&]rc=([0-9]+)/i,
  load : function(){
    var ref = adCheck.uriParse.exec(document.location.href);
    if(ref){
      Cookie.set("rc", ref[1], 30, "/");
      Stat.sendStat({'t':'landing','c':ref[1],'b':Browser.vendor});
    }
    var template =
    '<div id="divInstallHelper" style="display:none">'+
    '<div id="divInstallHeader" style="height: 35px;">'+
    '    <div id="installhelper_window_close"></div>'+
    '    <div id="divInstallingIsEasy"></div>'+
    '</div>'+
    '<div id="ff3_instructions" style="display:none; padding: 15px; text-align: left;">'+
    '    <ol>'+
    '        <li id="clickAllow">Click Allow Button<br/> Above from Firefox <div id="div_arrow_up" style="display: none;">&nbsp;</div></li>'+
    '        <li id="clickInstallNow">Click Install Now</li>'+
    '        <li id="restartFirefox" style="color: #666;">After downloading, Restart Firefox</li>'+
    '    </ol>'+
    '</div>'+
    '<div id="ff2_instructions" style="display:none; padding: 15px; text-align: left;">'+
    '    <ol>'+
    '        <li id="ff2_clickEditOptions">Click Edit Options <div id="div_arrow_up" style="display: none;">&nbsp;</div></li>'+
    '        <li id="ff2_clickAllow" style="color: #666;">In the window, click Allow</li>'+
    '        <li id="ff2_clickDownloadAgain" style="color: #666;"><a href="#" onclick="InstallHelper.ff2_clickedDownloadAgain()">Click here to Download Now!</a></li>'+
    '    </ol>'+
    '</div>'+
    '<div id="na_allproducts" style="display:none; padding: 15px; text-align: left;">'+
    '    <ul>'+
    '        <li>Sorry, Yoono currently only supports Firefox 2 and later and Internet Explorer 7 and later though we plan on supporting more browsers in the future!</li>'+
    '    </ul>'+
    '</div>'+
    '<div id="na_ie" style="display:none; padding: 15px; text-align: left;">'+
    '    <ul>'+
    '        <li>We\'re very close to launching Yoono 6.0 beta for IE!  Please check back here soon.</li>'+
    '    </ul>'+
    '</div>'+
    '</div>';
    var div = document.createElement("div");
    div.innerHTML = template;
    document.body.appendChild(div);
  },
  download : function(br){
    
    var c = Cookie.get("rc")?Cookie.get("rc"):0;
    setTimeout(function(){
      Stat.sendStat({'t':'download','c':c,'b':Browser.vendor,'v':(br == "ie" ? "exe" : "ff")})
    },0);
    
    if(Browser.vendor == 'ff'){
      if(br == 'ie'){
        setTimeout(function(){document.location.href='http://cache.yoono.com/download/ie/yoono-ie.exe'},500);
        return false;
      }
      else {
        return InstallHelper.clickedDownload();
      }
    }
    else if(Browser.vendor == 'ie'){
      if(br == 'ie'){
        //return true;
        return InstallHelper.ie_not_ready();
      }
      else {
        return true;
      }
    }
    else {
      return InstallHelper.browser_not_supported();
    }
  }
}

var Stat = {
  statPath : "http://api.yoono.com/rest/counters/one/campaign/",
  sendStat : function(params) {

    var prms = params.c + "-" + params.t + "-" + params.b;
    prms += (params.v ? '-'+params.v : "");
    
    var url = this.statPath + prms;
    var iFrameID = "hif";
    if($('hif') == null){
      var iframe = document.createElement("iframe");

      iframe.setAttribute("name", iFrameID);
      iframe.setAttribute("id", iFrameID);
      iframe.style.display = "none";

      document.body.appendChild(iframe);

      if(document.all && self.frames[iFrameID].name != iFrameID) {
        self.frames[iFrameID].name = iFrameID;
      }
    }

    var form = document.createElement("form");

    form.setAttribute("method", "POST");
    form.setAttribute("action", url);
    form.setAttribute("target", iFrameID);

    document.body.appendChild(form);

    form.submit();
  }
}

var Cookie = {
  // set a cookie (e.g., a session key or a user id)
  set : function(name, value, days, path) {
    var expires = "";
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      expires = "; expires=" + date.toGMTString();
    }
    if (!path) path = '/';
    document.cookie = name + "=" + value + expires + "; path=" + path;
  },

  eat : function(name) {
    this.set(name, "", -1);
  },

  get : function(cookieName) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (var i = 0; i < a_all_cookies.length; i++) {
      // now we'll split apart each name=value pair
      a_temp_cookie = a_all_cookies[i].split('=');

      // and trim left/right whitespace while we're at it
      cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

      // if the extracted name matches passed check_name
      if (cookie_name == cookieName) {
        b_cookie_found = true;
        // we need to handle case where cookie has no value but exists (no = sign, that is):
        if (a_temp_cookie.length > 1) {
          cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
        }
        // note that in cases where cookie is initialized but no value, null is returned
        return cookie_value;
      }
      a_temp_cookie = null;
      cookie_name = '';
    }
    if (!b_cookie_found) {
      return null;
    }
  }
}
var Browser = {
	init: function () {
		this.vendor = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "ff"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "ie",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		},
		{ 		// for iceweasle
			string: navigator.userAgent,
			subString: "Iceweasel",
			identity: "ff",
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};
Browser.init();

if(window.addEventListener){
  window.addEventListener("load",adCheck.load,false);
} else {
  window.attachEvent('onload',adCheck.load);
}
