window.addEvent( "domready", function() { new SSArtists(); } );
window.addEvent("load", function() {new FBLazyLike(); } );

var ArtistDataVO = {
    id:null,
    name:null,
    heading:null,
    desc:null,
    links:null,
    ituneslink:null
};

var FBLazyLike = new Class
({
    initialize: function()
    {
        var c = 1;
        $("artList").getElements("li").each( function( el, i ) {
            (function(el, i) {
                var url = el.getElement( "a" ).get("href");
                var ifram = FBLazyLike.createElement( url, "il"+i, 1 );
                ifram.inject( el, "bottom" );
            }).delay(300*c, this, [el,i]);
            c++;
        });
    }
});
FBLazyLike.createElement  = function( url, id, styleType )
{
    var ifUrl = "http://www.facebook.com/plugins/like.php?href=";
    var style, w, h;
    if( !$chk(styleType) ) {
        styleType = 1;
    }
    switch( styleType )
    {
        case undefined :
            style = "margin-top:24px;border:none; overflow:hidden; width:440px; height:35px;";
            w = 440;
            h = 35;
            break;
        case 1 :
            style = "margin-top:6px;border:none; overflow:hidden; width:110px; height:21px;";
            w = 110;
            h = 21;
            break;
    }
    ifUrl += encodeURI(url);
    ifUrl += "&layout=button_count&show_faces=false&width="+w+"&action=like&font=arial&colorscheme=light&height="+h;
    id = id ? id : "artDetIlike";
    return new Element("iframe").setProperties
    ({
        id:id,
        style: style,
        scrolling: "no",
        frameborder: "0",
        src: ifUrl,
		allowTransparency:"true"
    });
};
var SSArtists = new Class
({
	currentData:null,
	detLayer: null,
	
	initialize: function()
	{
		this.detLayer = new SSArtistDetail();
		
		$$(".artL").each( function( el ) {
			el.addEvent( "click", this.clickItemListener.bindWithEvent( this ) );
		}.bind( this ));
		
		this.handleDirectLoad();
	},
	
	handleDirectLoad: function()
	{
		if( window["directLoad"] ) {
			this.detLayer.clear();
			this.detLayer.show();
			var url = directLoad;
			var r = new Request.JSON( {url:url, onComplete:this.loadListener.bind( this )} );
			r.post();
            
		}
	},
	
	clickItemListener: function( e )
	{
		e.stop();
		this.detLayer.clear();
		this.detLayer.show();
		var target = $(e.target);
		if( target.get( "tag" ) != "a" ) {
			target = target.getParent( "a" );
		}
		var url = target.get( "href" );
		var r = new Request.JSON( {url:url, onComplete:this.loadListener.bind( this )} );
		r.post();
	},
	loadListener: function( res )
	{
		this.currentData = res.result;
		this.detLayer.update( this.currentData );
		
	}
});

var SSArtistDetail = new Class
({
	layer: null,
	modal:null,
	layerCont:null,
	head: null,
	subhead: null,
	desc: null,
	imgShell: null,
	linkList: null,
	img: null,
	close: null,
	minHeight:800,
	ie7:false,
	pag:null,

	
	initialize: function()
	{
		this.layer = $("artistDetail");
		this.layerCont = $("artDetCont");
		this.modal = new Element( "div" ).setProperties( {id:"artistModal"}).inject( document.body, "bottom" );
		this.ie7 = window.navigator.userAgent.indexOf( "MSIE 7" ) > -1;
		this.head = $("artDetHead");
		this.subhead = $("artDetSubHead");
		this.desc = $("artDetDesc");
		this.imgShell = $("artistDetailImg");
		this.linkList = $("artDetFoot").getElement("ul");
		this.close = $("artClose");
		this.pag = $("artPagination");
		
		this.layer.setStyles( {"min-height":"1px", "height":"1px"} );
		this.layer.set( "tween", { duration:500, onComplete:this.slideCompleteListener.bind(this) } );
		
		this.close.addEvent( "click", this.clickCloseListener.bind( this ) );
		this.modal.addEvent( "click", this.clickModalListener.bind(this) );

		if( this.ie7 ) {
			this.layer.inject( document.body ).setStyle( "left", $("canvasOuter").getPosition().x + 30  );
			this.pag.getElements( "li" ).each( function( li ) {
				var sl = new Element("em").inject( li );
				if( !li.hasClass("f0") ) {
					sl.set( "text", " / " );
				}
			});
			var lbl = new Element("li").set("class", "f0").set("text", "Select a season").inject( this.pag.getElement( "ul" ), "top" );
		}
	},
	
	show: function()
	{
		this.layer.setStyle.delay( 300, this.layer, ["display", "block"] );
		this.layer.tween.delay( 300, this.layer, ["height", this.minHeight ] );
		this.modal.setStyles( {"opacity":0, "display":"block"} ).tween( "opacity", .8 );
		new Fx.Scroll(window).toElement('artTopCombo');
    },
	hide: function()
	{
		this.modal.tween.delay( 300, this.modal, ["opacity", 0] );
		this.layer.tween( "height", "0" );
		//this.modal.setStyles( {"opacity":0, "display":"block"} ).tween( "opacity", 0 );
		this.layerCont.setStyle( "display", "none" );
	},
	clear: function()
	{
		if( this.img ) {
			this.img.dispose();
		}
	},
    /**
     * 
     * @param {ArtistDataVO} data 
     * @return void
     */
	update: function( data )
	{
		this.subhead.set("html", "" );
		this.head.set( "text", data.name );
		if( data.heading ) {
			this.subhead.set("html", data.heading );
		}
		this.desc.set( "html", Base64.decode( data.desc ) );
		this.createImage(data.image);
		this.createLinks( data.links, data.ituneslink );


        if( $("artDetIlike") ) {
            $("artDetIlike").dispose();
        }

        var ifram = FBLazyLike.createElement( data.link );
        ifram.inject( $("artDetFoot"), "bottom" );
	},
	createImage: function( url )
	{
		var i = new Element( "img" );
		i.setProperties( {src:url} );
		i.inject( this.imgShell, "bottom" );
		this.img = i;
	},
	createLinks: function( links, ituneslink )
	{
		this.linkList.empty();
		var l = new Element( "li" );
        var a,url;
		l.set("text", "For music and further information on the band please check:");
		this.linkList.adopt( l );
		for( var i=0; i<links.length; i++ )
		{
			l = new Element( "li" );
		    a = new Element( "a" );
			url = links[i].replace( /^http:\/\//, "" );
			a.setProperties( {"href": "http://" + url, "target" : "_blank" } );
			a.set("text", url );
			l.adopt( a );
			this.linkList.adopt( l );
		}
		
		if( ituneslink ) {
			l = new Element( "li" );
			l.setProperty( "class", "l" );
			a = new Element( "a" );
			url = ituneslink.replace( /^http:\/\//, "" );
			a.setProperties( {"href": "http://" + url, "target" : "_blank" } );
			a.set("text", url );
			l.adopt( a );
			this.linkList.adopt( l );
		}
	},
	clickCloseListener: function()
	{
		this.hide();
	},
	clickModalListener: function()
	{
		this.hide();
	},
	slideCompleteListener: function()
	{
		if( parseInt( this.layer.getStyle("height") ) < 10 ) {
			return;
		}
		var size = this.layerCont.measure(function(){
		    return this.getSize();
		});
		if( size.y > this.minHeight ) {
			this.layer.setStyle( "height", size.y+"px" );
		}
		this.layerCont.setStyles( {"visibility":"hidden","display":"block"} ).tween( "opacity", 1 );
	}
});




var Base64 = {
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
		encode : function (input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = Base64._utf8_encode(input);
	 
			while (i < input.length) {
	 
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
	 
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
	 
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
	 
				output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	 
			}
	 
			return output;
		},
		decode : function (input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = input.replace(/[^A-Za-z0-9\+\/=]/g, "");
	 
			while (i < input.length) {
	 
				enc1 = this._keyStr.indexOf(input.charAt(i++));
				enc2 = this._keyStr.indexOf(input.charAt(i++));
				enc3 = this._keyStr.indexOf(input.charAt(i++));
				enc4 = this._keyStr.indexOf(input.charAt(i++));
	 
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
	 
				output = output + String.fromCharCode(chr1);
	 
				if (enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if (enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
	 
			}
	 
			output = Base64._utf8_decode(output);
	 
			return output;
	 
		},
		_utf8_encode : function (string) {
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
	 
			for (var n = 0; n < string.length; n++) {
	 
				var c = string.charCodeAt(n);
	 
				if (c < 128) {
					utftext += String.fromCharCode(c);
				}
				else if((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
	 
			}
	 
			return utftext;
		},
		_utf8_decode : function (utftext) {
			var string = "";
			var i = 0;
            var c, c1, c2, c3;
			c = c1 = c2 = 0;
	 
			while ( i < utftext.length ) {
	 
				c = utftext.charCodeAt(i);
	 
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				}
				else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else {
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
	 
			}
	 
			return string;
		}
	 
};