/**nokia download survey
 *author: Satama Trainers' House
 *!!!
 * jquery library requried prior to this script
 *!!!
 var lotteryMaxValue  = 1; // result = 1/2*(lotteryMaxValue-1)
                          // 1=always, 2=~50%, 3=25%, 11=5%, 51=1%, 101=0,5%, 201=0,25%
 *
 *
 *
 *   */
 
function IE() {
return navigator.appName == "Microsoft Internet Explorer" ;
}

function surveytrigger() {
//dltext & dltarget & lotteryMaxValye must be defined in the page
    if(!showSurvey()) //no lottery hit, no survey, quit.
      return true; //activate normal lnk behaveiour if there was no lottery hit

    var surveytarget;
    //use the target from the clicked element if it has one, otherwise use global target
    //multiple targets still unsolved
    $(this).attr('href') === undefined ? surveytarget = target : surveytarget = $(this).attr('href');
    var surveyTrigger =$('#surveytrigger').attr('href');
      // parameters to the iframe are passed via parent window's location.hash
    window.location.hash = 'dltarget='+surveytarget+'&dltext='+escape(dltext);
    var t=setTimeout("$('#surveytrigger').click()",2000);
}

function showSurvey() {
  // check if this survey has already been shown
  // lotteryMaxvalue must be defined Globally
  // 1=always, 2=~50%, 3=25%, 11=5%, 51=1%, 101=0,5%, 201=0,25%
	var randNum = Math.round(Math.random()*(lotteryMaxValue-1));
	//returns true if there is a hit, false otherwise
    return  randNum == 0  ;
}

function injectCss(css) {
//if css path is set, import the given css
var headNode = document.getElementsByTagName('head')[0];
if(IE()) {
  //IE6 does not properly support createElement & setAttribute functions
  var linkTag = '<link href="'+css+'" rel="stylesheet" type="text/css"/>';
  linkNode = document.createElement(linkTag);
}
else
{
  var linkNode = document.createElement('link');
      linkNode.setAttribute('href',css);
      linkNode.setAttribute('rel' , 'stylesheet');
      linkNode.setAttribute('type','text/css');
}
headNode.appendChild(linkNode);
return;
}

function initDownloadSurvey(target) {
 if(typeof target == 'string')
   target = new Array(target);
  //bind survey to all maching links
  $('a').each(function(){
    for(i=0;i<target.length;i++){
     if ( $(this).attr('href') !== undefined ){
      if($(this).attr('href').indexOf(target[i]) !== -1 ) {
          $(this).click(surveytrigger);
      }
     }
    }
  });
}