// $Id: dada.js,v 1.12 2005/03/04 08:42:16 kl-www Exp $

/*/ onko mainos kokosivunhallinta vai erillisiä mainoksia? 
// Tämä on tärkeää yläsilmäkulman ja silmäkulman erossa.
var KOKOSIVU = 100;
var SEPARATE = 101;
var KLREDIR = 'http://mainos.kauppalehti.fi/rdr/r3.pl';
*/
function setCookie(name, value, expire) {
	document.cookie = name + "=" + escape(value) + 
		((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(Name) {
	var search = Name + "=";
	if (document.cookie.length > 0) { // if there are any cookies
      		offset = document.cookie.indexOf(search);
      		if (offset != -1) { // if cookie exists
         		offset += search.length;
         		// set index of beginning of value
         		end = document.cookie.indexOf(";", offset);
         		// set index of end of cookie value
         		if (end == -1)
            			end = document.cookie.length;
         		return unescape(document.cookie.substring(offset, end));
      }
   }
   return "";
}

// Näytetäänkö laajeneva mainos?
// =============================

function showExpanding() {
	var cookieName = 'rights';
	var cookie = getCookie(cookieName);
	var ieVersion = getIeVersion();

	if (ieVersion < 5.5) {
		return false;
	} else if ((cookie == 'f') || (cookie == 'i') || (cookie == '')) {
		return true;
	} else {
		return false;
	}
}

// Näytetäänkö flash?
// ==================

function showFlash(minimumFlashVersion) {
	var flashVersion = getFlashVersion();
	var rights = getCookie('rights');

	if (flashVersion >= minimumFlashVersion) {
	  return true;
	}

	return false;
}

function getFlashVersion() {
	var flashVersion = -1;
	var ieVersion = getIeVersion();

	if (navigator.plugins.length > 0) {
		flashVersion = getFlashVersionForStandarsCompliantBrowsers();
	} else if (ieVersion > 5) {
		flashVersion = getFlashVersionIe();
	}

	return flashVersion;
}

function getFlashVersionForStandarsCompliantBrowsers() {
	var flashVersion = -2;

	for (var i = 0; i < navigator.plugins.length; i++) {
        	var pluginDescription = navigator.plugins[i].description;
		var re = /^Shockwave Flash (\d+(\.\d+)?)/;
		var result = pluginDescription.match(re);
		
        	if (result != null) {
			return result[1];	
		}
	}

	return flashVersion;
}

function getIeVersion() {
	var ieVersion = -1;
	var re = /MSIE (\d+(\.\d+)?)/;
	var result = navigator.userAgent.match(re);

	if (result == null) {
		return -2;
	} else {
		return result[1];
	}
}

// MSIE ei tue navigator.plugins-taulukkoa, joten homma pitää tehdä näin.
// Vaatii ECMAScript eli IE 5.5:n tai uudemman.

function getFlashVersionIe() {
	var flashVersion = -3;

        for(var i=7; i>0; i--){
                try{
                        var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
                        flashVersion = i;
                        return flashVersion;
                } catch(e) {
                }
        }

	return flashVersion;
}

function isMac() {
	if (navigator.userAgent.indexOf('Mac') < 0) {
		return false;
	} else {
		return true;
	}
}

// Karu selli
// ==========

/*
ads: taulukko, jonka alkiot ovat mainosmerkkijonoja.
incrementOk: kasvatetaanko keksiä?
isCode: pitääkö pätkälle tehdä eval?


function showAd(ads, incrementOk, position, isCode) {
	var index = getIndex(ads, incrementOk, position);

	if (isCode[index]) {
		eval(ads[index]);
	} else {
		document.write(ads[index]);
	}
}

function getIndex(ads, incrementOk, position) {
	var cookieName = 'ks' + position;
	var cookie = getCookie(cookieName);

	if (cookie.length > 0) {
		if (incrementOk) {
			++cookie;

			if (cookie > (ads.length - 1)) {
				cookie = 0;
			}
		}
	} else {
		var date = new Date();
		//cookie = date.getSeconds() % ads.length;
		cookie = 0;
	}

	setCookie(cookieName, cookie);

	return cookie;
}

// Tagien tekeminen
// ===================

function getRedirUrl(url, campaignId, adId, deptId) {
	var s = KLREDIR + '/c' + campaignId + '/h' + adId + '/p' + 
		deptId + '/';
	s = s + url;

	return s;
}

// Huomioarvotutkimukset
// =====================

function showResearch(frequency) {
	if ((Math.floor(Math.random() * frequency) + 1) == frequency) {
		return true;
	} else {
		return false;
	}
}

function doResearch(url, frequency, cookieName, expires) {
	var authCookie = getCookie('AuthCookie');

        if (authCookie.indexOf('kli-') != -1) {
                return;
        } else if (getCookie(cookieName) == "true") {
               return;
        } else if (showResearch(frequency)) {
                setCookie(cookieName, "true", expires);
                window.open(url, 'Tutkimus', 'width=820,height=620,resizable=yes,scrollbars=yes');
        }

        return;
}

*/

function kerro(p)
{ d = "width=728,height=500,scrollbars=1,toolbar=1,location=0,directories=0,status=0,menubar=0,resizable=1"; window.open( p, "ikkuna2", d ); }

// Bannerin näyttö -cookiet

var showBan;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var mod = "";
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) mod = c.substring(nameEQ.length,c.length);
	}
	if(mod == "") return null;
	else return mod.substring(mod.indexOf("=")+1);
}

function setBanCookie(name){
  if(showBan == null || showBan == "" || showBan == 0){
  	createCookie(name,1,7);
  } else {
  	createCookie(name,2,7);
  }
}


function kirjoitaFlash() {
  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"' +
    ' id="il' + adId + '" width="' + width + '" height="' + height + '">' +
    ' <param name="movie" value="' + flashUrl + '"> <param name="quality" value="high"> <param name="wmode" value="' + wmode + '"> <param name="bgcolor" value="' + background + '"> ' +
    ' <embed src="' + flashUrl + '" quality="high" wmode="' + wmode + '" bgcolor="' + background + '" ' +
    ' swLiveConnect="false" width="' + width + '" height="' + height + '" name="il' + adId + '"' +
    ' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">' +
    '</embed>' +
    '</object>');
}

var ie4 = (document.all) ? true : false;
var ns6 = (navigator.userAgent.indexOf("Netscape")!=-1) ? true : false;
// pitää muistaa päivittää dhtml-bannerit
var ff = (document.getElementById && !document.all) ? true : false;
var mac = (navigator.appVersion.indexOf('Mac')!=-1)? true : false;
var opera = (navigator.userAgent.indexOf('Opera') > -1) ? true : false;
var timeOutShow = [];
var timeOutHide = [];
//var visible = false;

function naytaLaajennus() {
  if (ie4) {document.all['laajennus'].style.visibility = "visible";}
  if (ns6) {document.getElementById(['laajennus']).style.visibility = "visible";}
  //if (ie4) {document.all['laajeneva'].style.visibility = "hidden";}
  //if (ns6) {document.getElementById(['laajeneva']).style.visibility = "hidden";}
}
function piilotaLaajennus() {
  if (ie4) {document.all['laajennus'].style.visibility = "hidden";}
  if (ns6) {document.getElementById(['laajennus']).style.visibility = "hidden";}
  //if (ie4) {document.all['laajeneva'].style.visibility = "visible";}
  //if (ns6) {document.getElementById(['laajeneva']).style.visibility = "visible";}
}                                                                                                                                                                                                                         

function showLayer(name) {
  if (ie4) {document.all[name].style.visibility = "visible";}
  if (ff && !ns6) {document.getElementById([name]).style.visibility = "visible";}
}

function hideLayer(name) {
  if (ie4 && document.all[name]) {document.all[name].style.visibility = "hidden";}
  if (ff && !ns6 && document.getElementById([name])) {document.getElementById([name]).style.visibility = "hidden";}
}

function showExpand(name) {
  //clearInterval(toh)
  //alert(document.getElementById([name]).style.visibility)
 // if ((ie4 && document.all[name].style.visibility == "visible") || (ns6 && document.getElementById([name]).style.visibility == "visible")) {
  //if (visible == true) {
    //showLayer(name)
  //}
  //else {
    //alert(toh.length)
  for (var x=0;x < timeOutHide.length;x++) {
    clearTimeout(timeOutHide.pop());
  }
  timeOutShow.push(setTimeout(function() {showLayer(name)},500))
  //}
//visible = true;
}

function hideExpand(name) {
  //if (tos != '') {
    //alert(tos)
    //alert(tos.length)
  for (var y=0;y < timeOutShow.length;y++) {
    clearTimeout(timeOutShow.pop());
  }
  timeOutHide.push(setTimeout(function() {hideLayer(name)},50))
 //tos = clearInterval(tos)
    //alert(tos)
//   tos = ''
  // }
  //alert('hide'+name)
 // toh = setInterval(function() {hideLayer(name)},1000)*/
//hideLayer(name)
//visible = false;
  }

function hideDhtml() {
  hideLayer('floatosa')
}

