function adsEnableBackgroundWallpaperSupport() {
  var bodyElement = document.getElementById('motoso');

  bodyElement.className = bodyElement.className + ' mAdsBackgroundWallpaper';

}


function adsSetBackground(color) {
  document.getElementsByTagName('HTML')[0].style.backgroundColor = color;

  if (document.body) {
    document.body.style.backgroundColor = color;
  }
}

function adsSetBackgroundImage(imageUrl) {
  document.getElementsByTagName('HTML')[0].style.backgroundImage = 'url(' + imageUrl + ')';

  if (document.body) {
    document.body.style.backgroundImage = 'url(' + imageUrl + ')';
  }
}

function adsSetBackgroundClickHandler() {
  var element = document.body;

  if (element) {
     element.className = element.className + ' mAdsBackgroundClickable';        

     if (element.addEventListener) {
         element.addEventListener("click", adsBackgroundClickHandler, false);
     } else if (element.attachEvent) {
        element.attachEvent("onclick", adsBackgroundClickHandler);
     }
  }

  element = document.getElementById('motoso_center');
  if (element) {
     if (element.addEventListener) {
         element.addEventListener("click", adsBackgroundClickCancel, false);
     } else if (element.attachEvent) {
        element.attachEvent("onclick", adsBackgroundClickCancel);
     }
  }


  element = document.getElementById('mBannerFullHandler');
  if (element) {
     if (element.addEventListener) {
         element.addEventListener("click", adsBackgroundClickCancel, false);
     } else if (element.attachEvent) {
        element.attachEvent("onclick", adsBackgroundClickCancel);
     }
  }
    
  element = document.getElementById('mBannerSkyHandler');
  if (element) {
     if (element.addEventListener) {
         element.addEventListener("click", adsBackgroundClickCancel, false);
     } else if (element.attachEvent) {
        element.attachEvent("onclick", adsBackgroundClickCancel);
     }
  }
}


function adsBackgroundClickHandler(event) {
    if (adclick && adclick != '') {
      window.open(adclick);
    }
}


function adsBackgroundClickCancel(event) {
  // stop event bubble phase for any event inside motoso_center

   if(!event.stopPropagation){
      event = window.event;
      event.cancelBubble = true;
   }

   if(event.stopPropagation){
      event.stopPropagation();
   }
}


function adsPositionHockeyStick() {
  var fullsize = document.getElementById('mFullsizeAd');
  var skyscraper = document.getElementById('mSkyscraperAd');

  if (fullsize && skyscraper) {
    fullsize.className = fullsize.className + " hockey";
    skyscraper.className = skyscraper.className + " hockey";

     if (isIE6()) {
        fullsize.className = fullsize.className + " hockeyIE6";
        skyscraper.className = skyscraper.className + " hockeyIE6";
     }
  }
}

function adsPositionExpandableHockeyStick() {
  var fullsize = document.getElementById('mFullsizeAd');
  var skyscraper = document.getElementById('mSkyscraperAd');

  if (fullsize && skyscraper) {
    fullsize.className = fullsize.className + " hockeyExpand";
    skyscraper.className = skyscraper.className + " hockeyExpand";
  }
}

function adsHandler() {
  var enabled = false;

  if (typeof(bgcolor) != 'undefined' && bgcolor != null) {
    adsEnableBackgroundWallpaperSupport();
    enabled = true;
    adsSetBackground(bgcolor);
  }
  if (typeof(bgimage) != 'undefined' && bgimage != null) {
    if (!enabled) {
      adsEnableBackgroundWallpaperSupport();
      enabled = true;
    }
    adsSetBackgroundImage(bgimage);
  }
  if (typeof(adclick) != 'undefined' && adclick != null) {
    if (!enabled) {
      adsEnableBackgroundWallpaperSupport();
      enabled = true;
    }
    adsSetBackgroundClickHandler();
  }

  if (typeof(adtype) != 'undefined' && adtype == 'wallpaper') {
    adsPositionHockeyStick();
  }

  if (typeof(adtype) != 'undefined' && adtype == 'expwallpaper') {
    adsPositionExpandableHockeyStick();
  }
}


function isIE6() {
   var browser = navigator.appName;
   var version = navigator.appVersion;

   if ( browser== "Microsoft Internet Explorer" ) {
     return version.substring(22,25) == '6.0';
    }

   return false;
}
