if (typeof trolleycards == "undefined" || !trolleycards) {
    var trolleycards = {};
}

Event.observe(window, 'load', function() {
	artistModal = new trolleycards.artist();
	$$('.artist-name a').each(function(i) {
		i.observe('click', artistModal.showWindow.bindAsEventListener(artistModal, i));
		i.href = 'javascript:;';
	});
});

document.observe('lightview:hidden', function(event) {
	screenManager.restorePageTitle();
	$$('.lv_Loading').invoke('show'); // http://www.nickstakenburg.com/forum/comments.php?DiscussionID=1423/
});

trolleycards.artist = Class.create({	
	initialize: function() {
		this.template = $('artist-modal').innerHTML;
		this.zoomedIn = false;
		this.draggable = null;
		this.currentId = null;
		
		document.observe('lightview:opened', this.onOpen.bindAsEventListener(this));
	},
	
	onOpen: function () {
		if($('artist-modal').visible()) {
			this.zoomOut();
		}
	},
	
	resetWindow: function () {
		if($('artist-modal').visible()) {
			$('artist-modal').hide();
			screenManager.restorePageTitle();
		}
	},
	
	showWindow: function () {
		var elem = $A(arguments)[1];
		var artistId = isNaN(elem) ? elem.getAttribute('artist') : elem;
		var artist = artistData[artistId];
		
		//if there is no data for the artist, bail now
		if(artist == undefined) { 
			return;	
		} else {
			this.resetWindow();
		}
		
		//create template to replace data
		var html = new Template(this.template).evaluate(artist);
		$('artist-modal').update(html);
		
		//manually replace link
		$('artist-modal-link').href = artist.url;
		
		//manually replace small images
		/*
		var imgs = $$('#artist-modal .modal-gallery img.small');
		for(x=0; x<4; x++) {
			imgs[x].src = eval('artist.image'+(x+1));
			imgs[x].observe('click', this.swapImage.bindAsEventListener(this, imgs[x]));
		}
		*/
		
		//manaully replace large image
		var imgLg = $$('#artist-modal .modal-gallery img.large')[0];
		imgLg.src = artist.image1;
		
		//set up zoom control button
		var zoom = $$('#artist-modal .modal-gallery .zoom')[0];
		zoom.stopObserving('click');
		zoom.observe('click', this.zoomToggle.bindAsEventListener(this));
		
		//next & previous
		$('artist-modal-next').stopObserving('click');
		$('artist-modal-next').observe('click', this.next.bindAsEventListener(this));
		$('artist-modal-previous').stopObserving('click');
		$('artist-modal-previous').observe('click', this.previous.bindAsEventListener(this));
		
		this.currentId = artistId;
		screenManager.appendToPageTitle(artist.name);
		screenManager.trackPageview(artist.url.replace('.html', '.modal'));
		/**
		 * Adding a width & height here prevents an IE error.  Since autosize is true, these
		 * options are ignored anyway.
		 */
		Lightview.show({ href: 'artist-modal', rel: 'inline', options: {autosize: true, width: 200, height: 200} });
	},
	
	swapImage: function() {
		var imgLg = $$('#artist-modal .modal-gallery img.large')[0];
		var elem = $A(arguments)[1];
		
		imgLg.style.height = '';
		imgLg.style.width = '';
		imgLg.src = elem.src;
		
		this.zoomOut();
	},
	
	zoomOut: function() {
		var imgLg = $$('#artist-modal .modal-gallery img.large')[0];
		var zoom = $$('#artist-modal .modal-gallery .zoom')[0];
		
		this.imgLgHeight = imgLg.getHeight();
		this.imgLgWidth = imgLg.getWidth();
		imgLg.style.height = imgLg.parentNode.getHeight() + 'px';
		imgLg.style.width = imgLg.parentNode.getWidth() + 'px';
		imgLg.style.top = 0;
		imgLg.style.left = 0;
		imgLg.style.cursor = 'auto';
		
		//if the image is about the right size, don't give zoom controlls
		var diffX = Math.abs(imgLg.getWidth() - this.imgLgWidth);
		var diffY = Math.abs(imgLg.getHeight() - this.imgLgHeight);
		if(diffX < 20 && diffY < 20) {
			zoom.hide();
		} else {
			zoom.show();
			zoom.style.backgroundPosition = '0px 0px';
		}
		
		if(this.draggable) this.draggable.destroy();
		this.zoomedIn = false;
	},
	
	zoomIn: function() {
		var imgLg = $$('#artist-modal .modal-gallery img.large')[0];
		imgLg.style.height = this.imgLgHeight + 'px';
		imgLg.style.width = this.imgLgWidth + 'px';
		imgLg.style.cursor = 'move';
		
		var zoom = $$('#artist-modal .modal-gallery .zoom')[0];
		zoom.style.backgroundPosition = '0px -15px';
		
		this.draggable = new Draggable(imgLg, {
            starteffect:false,
            reverteffect:false,
            endeffect:false,
            snap:this.contain.bind(this)
        });
		
		this.zoomedIn = true;
	},
	
	zoomToggle: function() {
		this.zoomedIn ? this.zoomOut() : this.zoomIn();
	},

    contain: function (x,y,draggable) {
		var imgLg = $$('#artist-modal .modal-gallery img.large')[0];
        var dim = Element.getDimensions(draggable.element);

        var xMin = 0, xMax = imgLg.parentNode.getWidth()-dim.width;
        var yMin = 0, yMax = imgLg.parentNode.getHeight()-dim.height;

        x = x>xMin ? xMin : x;
        x = x<xMax ? xMax : x;
        y = y>yMin ? yMin : y;
        y = y<yMax ? yMax : y;

        this.imageX = x;
        this.imageY = y;

        imgLg.style.left = this.imageX+'px';
        imgLg.style.top = this.imageY+'px';

        return [x,y];
    },
    
    next: function() {
    	var ids = Object.keys(artistData);
    	var current = null;
    	
    	for(var x=0; x<ids.length; x++) {
    		if(ids[x] == this.currentId) {
    			current = x;
    		}
    	}
    	
    	if(ids[current] == ids.last()) {
    		this.showWindow(this, ids.first());
    	} else {
    		this.showWindow(this, ids[current+1]);
    	}
    },
    
    previous: function() {
    	var ids = Object.keys(artistData);
    	var current = null;
    	
    	for(var x=0; x<ids.length; x++) {
    		if(ids[x] == this.currentId) {
    			current = x;
    		}
    	}
    	
    	if(ids[current] == ids.first()) {
    		this.showWindow(this, ids.last());
    	} else {
    		this.showWindow(this, ids[current-1]);
    	}
    }
	
});