/*
 * Bebo.com Copyright 2007 
 */

var beboITunes = {
   iTunesFields : ['trackViewUrl', 'trackLinkUrl', 'collectionViewUrl', 'collectionLinkUrl', 'artistViewUrl', 'artistLinkUrl'],
   DefaultPartnerPrefix : "http://clkuk.tradedoubler.com/click?p=23708&a=1374761&url=",
   DefaultStoreId : "&s=143444&partnerId=2003",
   BandSearchUrl : '',
   PartnerPrefix : '',
   StoreId : '',
   StoreUrl : '',
   DepthCount : 0,
   navigatePage : function(url) {
      try {
         window.location.replace(url) ;
      } catch (e) {
         window.location.assign(url);
      }
      beboITunes.DepthCount = 0;
   },
   navigatePageByForm : function(url) {
      var el = document.createElement("form");
      el.method = "POST";
      el.action = url;
      document.body.appendChild(el);
      el.submit();
      beboITunes.DepthCount = 0;
   },
   doCallback : function(result, callback, doEncode) {
      for (a in beboITunes.iTunesFields) {
         if (result[beboITunes.iTunesFields[a]]) {
            //document.referrer = document.location;
            callback(beboITunes.PartnerPrefix + (doEncode ? encodeURI(result[beboITunes.iTunesFields[a]]) : result[beboITunes.iTunesFields[a]]));
            break;
         }
      } 
   },
   dispatch : function(rval, callback, doEncode) {
      var firstResult = rval.results[0];
      //   debugger;
      if (firstResult) {
         beboITunes.doCallback(firstResult, callback, doEncode);
         return true;
      } else {
         return false;
      }
   },
   dispatchWrap : function(rval, callback, doEncode) {
      beboITunes.DepthCount++;
      if (beboITunes.dispatch(rval, beboITunes.navigatePageByForm, false) == false) {
         if (beboITunes.BandSearchUrl && beboITunes.DepthCount <= 1) {
            beboITunes.doITunes(beboITunes.BandSearchUrl);
         } else {
            beboITunes.navigatePageByForm(beboITunes.PartnerPrefix + encodeURI(beboITunes.StoreUrl));
         }
      }
   },
   gotoITunes : function(rval) {
      beboITunes.dispatchWrap(rval, beboITunes.navigatePage, true);
   },
   gotoITunes2 : function(rval) {
      beboITunes.dispatchWrap(rval, beboITunes.navigatePageByForm, true);
   },
   gotoITunes2US : function(rval) {
      beboITunes.dispatchWrap(rval, beboITunes.navigatePageByForm, false);
   },
   doITunes : function(url) {
      var el = document.createElement("script");
      el.src = url;
      document.body.appendChild(el);
   },
   doFreeSingle : function(url) {
      if(beboITunes.PartnerPrefix && beboITunes.StoreId) {
         beboITunes.navigatePageByForm(beboITunes.PartnerPrefix + encodeURI(url + beboITunes.StoreId));
      } else {
         beboITunes.navigatePageByForm(beboITunes.DefaultPartnerPrefix + encodeURI(url + beboITunes.DefaultStoreId));
      }
   }
};

   

