/*
 * iTunes Music Store Search
 *
 * Copyright (c) MusicDiner.com
 *
 * Revision: 1a
 *
 */

(function(d){

var $ = function(id){ return d.getElementById(id) };

var json2list = function(json){
	var ul = d.createElement('ul');
//	ul.setAttribute('style', 'list-style:none;');
        var a = d.createElement('a');
        a.href = json['itemLinkUrl'];
        a.target = 'itunes_store';
        var img = d.createElement('img');
        img.src = json['artworkUrl100'];
        a.appendChild(img);
        ul.appendChild(a);
        var li = d.createElement('li');
        var h1 = d.createElement('h1');
//      h1.setAttribute('class','itmstitle');
        h1.appendChild(d.createTextNode('Title: '));
        li.appendChild(h1);
		var a = d.createElement('a');
        a.href = json['itemLinkUrl'];
        a.target = 'itunes_store';
        a.appendChild(d.createTextNode(json['itemName']));
		li.appendChild(a);
        ul.appendChild(li);
        var li = d.createElement('li');
        var h1 = d.createElement('h1');
 //     h1.setAttribute('class','itmstitle');
        h1.appendChild(d.createTextNode('Artist: '));
        li.appendChild(h1);
		var a = d.createElement('a');
        a.href = json['artistLinkUrl'];
        a.target = 'itunes_store';
        a.appendChild(d.createTextNode(json['artistName']));
		li.appendChild(a);
        ul.appendChild(li);
        var li = d.createElement('li');
        var h1 = d.createElement('h1');
 //     h1.setAttribute('class','itmstitle');
        h1.appendChild(d.createTextNode('Album: '));
        li.appendChild(h1);
		var a = d.createElement('a');
        a.href = json['itemParentLinkUrl'];
        a.target = 'itunes_store';
        a.appendChild(d.createTextNode(json['itemParentName']));
		li.appendChild(a);
        ul.appendChild(li);
   return ul;
};

JSONP = {
  get:function(term){
	$('itmsresults').innerHTML = '<em>Searching...</em>';
	$('itmsresults').setAttribute('class', 'loading');
//	$('itmsresults').setAttribute('style', 'visibility: visible;');
    var u = 'http://ax.phobos.apple.com.edgesuite.net'
          + '/WebObjects/MZStoreServices.woa/wa/itmsSearch?'
          + ['output='   + 'json',
             'callback=' + 'JSONP.run',
             'country='  + 'US',
             'lang='     + '1',
             'limit='    + '50',
             'media='    + 'all',
             'partnerId='+ '30',
             'LS_PARAM=' + 'http%3A%2F%2Fclick.linksynergy.com%2Ffs-bin%2Fstat%3Fid%3DGfkZUlqtlHE%26offerid%3D78941%26type%3D3%26subid%3D0%26tmpid%3D1826%26RD_PARM1%3D',
             'term='     + encodeURIComponent(term)
          ].join('&');
    var s = d.createElement('script');
    s.charset = 'UTF-8';
    s.id = s.src = u;
    d.body.appendChild(s);
  },
  run:function(json){
	$('itmsresults').innerHTML = '';
	$('itmsresults').setAttribute('class', '');
//	$('itmsresults').setAttribute('style', 'visibility: visible;');
    if (json.errorMessage){
         $('itmsresults').appendChild(json.errorMessage);
    }else{
        var ol = d.createElement('ol');
        for (var i = 0, l = json.resultCount; i < l; i++){
            if (!json.results[i]) continue;
            var li = d.createElement('li');
            li.appendChild(json2list(json.results[i]));
            ol.appendChild(li);
        }
        $('itmsresults').appendChild(ol);
    }
  }
};

})(document);