FANDOM

2,054,160 Pages

//
/*jslint browser */
/*global window, jQuery, mediaWiki */
 
(function ($, mw) {
  var iTurl='http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/';
  var request=new XMLHttpRequest();
  var storeShortNames = new Array("ar", "au", "at", "be", "br", "ca", "cl", "cn", "co", "cr", "hr", "cz", "dk", "do", "ec", "eg", "sv", "ee", "fi", "fr", "de", "gr", "gt", "hn", "hk", "hu", "in", "id", "ie", "il", "it", "jm", "jp", "kz", "kr", "kw", "lv", "lb", "lt", "lu", "mo", "my", "mt", "mx", "md", "nl", "nz", "ni", "no", "pk", "pa", "py", "pe", "ph", "pl", "pt", "qa", "ro", "ru", "sa", "sg", "sk", "si", "za", "es", "lk", "se", "ch", "tw", "th", "tr", "ae", "gb", "us", "uy", "ve", "vn");
  var storeLongNames = new Array("Argentina", "Australia", "Austria", "Belgium", "Brazil", "Canada", "Chile", "China", "Colombia", "Costa Rica", "Croatia", "Czech Republic", "Denmark", "Dominican Rep.", "Ecuador", "Egypt", "El Salvador", "Estonia", "Finland", "France", "Germany", "Greece", "Guatemala", "Honduras", "Hong Kong", "Hungary", "India", "Indonesia", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Kazakstan", "Korea, Republic Of", "Kuwait", "Latvia", "Lebanon", "Lithuania", "Luxembourg", "Macau", "Malaysia", "Malta", "Mexico", "Moldova, Republic Of", "Netherlands", "New Zealand", "Nicaragua", "Norway", "Pakistan", "Panama", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russia", "Saudi Arabia", "Singapore", "Slovakia", "Slovenia", "South Africa", "Spain", "Sri Lanka", "Sweden", "Switzerland", "Taiwan", "Thailand", "Turkey", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Venezuela", "Vietnam");
  var itStores;
  var ITform;
 
function addITbutton(){
  $('#wpwikify').after('<button type="button" id="itBtn" style="padding:0;margin-left:10px;vertical-align: middle;"><img src="http://images4.wikia.nocookie.net/lyricwiki/images/0/06/ITunes_icon.png" style="vertical-align: middle;" title="Open iTunes Wikifyer"> iT Wikifyer </button>');
  $('#itBtn').click(openITW);
}
 
 
function openITW() {
    if (!itStores) {
      $.each (storeShortNames, function (i, val){
        itStores +='<option value="' + val + '">' + storeLongNames[i] + '</option>';
      });
      ITform ='Designed to be used as a tool for creating new albums and their cover images (not deadlinks to them).<br/><table style="min-width:500px; width:100%; background-color:#FDF6E3; font-size: 12px;"><tr><td colspan="2"><fieldset style="border:thin solid silver;padding:5px;"> Artist <input id="tbart"> Album <input id="tbalb"> Song <input id="tbsong"> in store <select id="country">' + itStores + '</select> <Button id ="btnSrch">Search</Button></fieldset></td></tr><tr><td style="width:40%"><select id="mList" size="16" style="background-color: #FDFAE6;height:250px;width:100%; border:1px solid silver; font-size: 12px; color: #3F3A3A;"></select></td><td rowspan="2" valign="top" style="width:60%"><textarea id="resList" style="background-color: rgb(253, 250, 230);max-height:1000px !important;height:400px;max-width:1200px !important; width:98% !important;font-size: 12px;color: #004080;resize:none;"></textarea></td></tr><tr><td valign="middle"><fieldset style="padding:0;margin:0;"><legend>iTunes info:</legend><table style="width:100%;min-width:350px;"><tr><td valign="middle" rowspan="4" style="padding-right:10px;width:100px;"><img id="aimg" width="100" height="100"></td><td style="text-align:right;">Artist</td><td><input id="iTart" style="width:100%;"></td></tr><tr><td  style="width:40px;text-align:right;">Album</td><td><input id="iTalb" style="width:100%;"></td></tr><tr><td style="text-align:right;">Year</td><td><input id="iTyear" style="width:40px;"> ID <input id="iTid" style="width:120px;"></td></tr><tr><td style="text-align:right;">Genre</td><td><input id="iTgen"  style="width:100px;"> Dur. <input id="iTdur" style="width:50px;"></td></tr></table></fieldset></td></tr><tr><td colspan="2"><div id="resdiv" style="font-size: 12px; color:#804000;border:thin solid silver;">Ready.</div></td></tr></table>';    
    }
        $.showCustomModal('iTunes Wikifyer', ITform, {
            id: 'ITW',
            width: 970,
            height: 600,
            buttons: [{  
                message: 'Copy',
                defaultButton: true,
                handler: function() {
                  $('#resList').select();
                  document.execCommand("Copy", false, null);
                  $('#resdiv').html('Copied to the Clipboard.');
                }
            }]
        });
 
    $('#ITW').css({"background-color":"#FDF6E3"});
    $('#country option[value="us"]').prop('selected', 'selected').change();
    $('#mList').click(getITtracks);
    $('#btnSrch').click(getITalbs);
    $('#aimg').dblclick(getITimg);
    $('#btnCopy').click(ClpBrd);
    $("#ITW").keypress(function (e) {
      if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
          getITalbs();
          return false;
      } else {
          return true;
      }
    });
}
 
  function ClpBrd() {
    $('#resList').select();
    document.execCommand("Copy", false, null);
    $('#resdiv').html('Copied to the Clipboard.');
  }
 
  function getITalbs() {
    $('#resdiv').html('Searching iTunes...');
    var art = $('#tbart').val();
    var alb = $('#tbalb').val();
    var song = $('#tbsong').val();
    var country = $('#country').val();
    var mList = $('#mList');
    var entity = ((song == '') ? 'album' : 'song');
    mList.children().remove();
    $.ajax({
      url: iTurl + 'wsSearch',
      dataType: "jsonp",
      data: {
        term: art + ' ' + alb + ' ' + song,
        media: "music",
        entity: entity,
        country: country
      },
      error: function (jqXHR, textStatus, message) {
        $('#resdiv').html(message);
      },
      success: function (data, textStatus, jqXHR) {
        if (data.hasOwnProperty('results')){
          var a=data.results;
          if (a.length > 0) {
            var lart=art.toLowerCase();
            var lalb=alb.toLowerCase();
            for (var i in a) {
              if (lart ==='' || a[i].artistName.toLowerCase().indexOf(lart) !== -1){
                var ttalb=a[i].collectionName;
                if ((ttalb) && (alb ==='' || ttalb.toLowerCase().indexOf(lalb) !== -1)){
                  mList[0].appendChild(new Option(a[i].artistName + ':' + ttalb + ' (' + a[i].releaseDate.substr(0,4) + ')', a[i].collectionId));
                }
              }
            }
            SortMlistByYear();
            $('#resdiv').html(mList[0].options.length + '/' + a.length + ' matches found.');
          }
          else $('#resdiv').html('No matches found');
        }
      }
    });
  }
 
  function getITtracks() {
    var tid =this.value;
    var country = $('#country').val();
    $.ajax({
      url: iTurl + 'wsLookup',
      dataType: "jsonp",
      data: {
        id: tid,
        media: "music",
        entity: "song",
        country: country
      },
      error: function (jqXHR, textStatus, message) {
        $('#resdiv').html(message);
      },
      success: function (data, textStatus, jqXHR) {
        if (data.hasOwnProperty('results')){
        var albDur = 0;
         var aArtist;     // album artist
         var aTitle = ''; // album title
          var apTitle;     // album page title
          var aSuff = ''   // album notation
          var tSuff;       // track notation
          var aYear;       // album date
          var tArtist;     // track artist
          var tTit;        // track title
          var albtext='';  // wikified tracklist text
          var a=data.results;
          aArtist = iniCaps(a[0].artistName); 
          aTitle = a[0].collectionName.replace(/\[/g,'(').replace(/\]/g,')');
          aTitle = aTitle.replace(/ \([^\)]*?(edition|version|ep|single|live|remastered|deluxe|anniversary|digital|compilation|mix|recording|session|demo)\)/gi,'').replace(/ - (single|ep|compilation)$/i,''); 
          var m = aTitle.match(/ \((ft\.|feat\S*) (.+?)\)/i);
          if (m){
            aTitle = aTitle.replace(m[0],'');
            aSuff += "* ''Featuring " + m[2] + "''\n";
          }  
          aYear = a[0].releaseDate.substr(0,4);
          $('#iTart').val(aArtist);
          $('#iTalb').val(aTitle);
          $('#iTyear').val(aYear);
          $('#iTgen').val(a[0].primaryGenreName);
          $('#aimg').attr('src', a[0].artworkUrl100);
          var ttid = 'id' + tid;
          if (country != 'us'){
            ttid += "&cc=" + country;
          }
          aArtist= ((aArtist == 'Various Artists') ? '' : aArtist);
          var picname = aTitle.replace(/[:?*/]/g, '-');
          if (aArtist == '') {picname += '.jpg';}
          else {
            if (picname == aTitle) {picname = '';}
            else picname = aArtist + ' - ' + picname + '.jpg';
          }
          var apArtist = ((aArtist == '') ? '' : aArtist + ':');
          apTitle = aTitle + ' (' + aYear + ')';
          var albtext = '==[[' + apArtist + iniCaps(apTitle) + "|" + apTitle + ']]==\n';
          albtext += '{{Album Art|' + picname + '|' + aTitle + '}}\n'
          if (aSuff !==''){
            albtext += aSuff;
          }
          $.each(a,function(){ // didn't know how to filter by wrapperType...
            tSuff = "";
            if (this.wrapperType == 'track'){
              try {
                if (this.trackTimeMillis){
                  albDur += this.trackTimeMillis;
                }
                tArtist = iniCaps(this.artistName);
                tTit = this.trackName.replace(/\[/g,'(').replace(/\]/g,')');
                var m = tTit.match(/ \((live.*?|acoustic.*?|alternative.*?|original.*?|video.*?|.*mix.*?|.*take|.*edit|.*version|.*session|.*demo)\)/i);
                if (m){
                  tTit = tTit.replace(m[0],'');
                  tSuff=m[0];
                }  
                m = tTit.match(/ \(((ft\.|feat\S*|duet( with)?|with)) ([^\]\)]+?)\)/i);
                if (m){
                  tTit = tTit.replace(m[0],'');
                  if (m[1][0].toLowerCase()=='f'){
                    tSuff += ' {{ft|' + m[4].replace(/, /g, '|') + '}}';
 
                  }
                  else {
                    tSuff += ' {{with|' + m[4] + '}}';
                  }
                }  
                if (tArtist != aArtist) {
                  if (aArtist == ''){tSuff += ' by [[' + tArtist + ']]';}
                  else if (tArtist.indexOf(aArtist) == 0){
                    var sf = tArtist.replace(aArtist, '').trim();
                    m = sf.match(/^(,|&|and|with|feat\S*) (.+)$/i);
                    if (m){
                      if (m[1][0].toLowerCase()=='f'){tSuff += ' {{ft|' + m[2].replace(/, /g, '|') + '}}';}
                      else {tSuff += ' {{with|' + m[2].replace(/, /g, '|') + '}}';}
                    }
                    else {tSuff += ' by [[' + tArtist + ']]';}
                  }
                  else {tSuff += ' by [[' + tArtist + ']]';}
                }                
                albtext += "# '''[[" + tArtist + ':' + iniCaps(tTit) + "|" + tTit + "]]'''" + tSuff + "\n";
              }
              catch(err) {  
                alert(err);
                alert(this.wrapperType);
              } 
            }
          });
          albtext += '{{Clear}}\n\n';
          $("#resList").val(albtext);
          $("#iTdur").val(ms2MinSecs(albDur));
          $("#resList").setSelection($('#tbsong').val());
          $("#iTid").val('id' + tid);
          $('#resdiv').html('ArtistID: id' + a[0].artistId);
        }
      }
    });
  }
 
  $.fn.setSelection = function(seltext) {
    if(this.lengh == 0 || seltext.lengh ==0) return this;
    input = this[0];
    var selectionStart=input.value.toLowerCase().indexOf(seltext.toLowerCase());
    if (selectionStart <= 0) return this;
    var selectionEnd = selectionStart + seltext.length;
    if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selectionEnd);
      range.moveStart('character', selectionStart);
      range.select();
    } else if (input.setSelectionRange) {
      input.focus();
      input.setSelectionRange(selectionStart, selectionEnd);
    }
    return this;
  }
 
  function iniCaps(tit){
    return tit.replace(/(['\^!?\/\s\(\"])(\w)/g, iniCap);
  }
 
  function iniCap(m, p1, p2) {
    if (p1 === "'") {return m;}
    return p1 + p2.toUpperCase();
  }
 
 
  function ms2MinSecs(ms) {
    var mins = Math.floor(ms / 60000);
    var secs = ((ms % 60000) / 1000).toFixed(0);
    return mins + ":" + (secs < 10 ? '0' : '') + secs;
  }
 
  function SortMlistByYear() {
    var options = mList.options;
    Array.prototype.sort.call(options, function (a, b) {
      return a.text.substr(a.text.length-5,4)-b.text.substr(b.text.length-5,4)
    });
  }
 
  function init() {
    if (wgPageName === 'Special:Wikify') {
      addITbutton();
    }    
  }
 
  var img=new Image();
  img.crossOrigin='anonymous';
  img.onload=startResizing;
 
  function startResizing(){
     resizeImg(img,0.8333);
  }
 
  function getiTimg(url) {
    img.src=url.replace('100x100','600x600');
  }
 
  function resizeImg(img,scale){
      var c=document.createElement('canvas');
      var ctx=c.getContext('2d');
      var iw=img.width;
      var ih=img.height;
      c.width=iw*scale;
      c.height=ih*scale;
      ctx.drawImage(img,0,0,iw*scale,ih*scale);
      $('#aimg').attr('src', c.toDataURL('image/jpeg'));
  } 
 
  function getITimg(){
    var iurl=$('#aimg').attr('src');
    if (iurl) {
      getiTimg(iurl);
      $('#resdiv').html('Done. Ready to view 500x500 image.');
    }
  }   
 
  $(init);
 
}(jQuery, mediaWiki));
 
//
Community content is available under Copyright unless otherwise noted.