var Galleryswitch = Class.create();

Galleryswitch.prototype = {

	// initialize()
	// Constructor runs on completion of the DOM loading. Calls updateGalleryList
	initialize: function() {
		this.updateGalleryList();
		//this.updateGalleryplayer();
	},

	updateGalleryList: function() {
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			var relAttribute = String(anchor.getAttribute('rel'));
			// use the string.match() method to catch 'galleryswitch' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('galleryswitch'))){
				anchor.onclick = function () {myGalleryswitch.start(this); return false;}
			}
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('galleryshow'))){
				anchor.onclick = function () {myGalleryswitch.drawgallery(this); return false;}
			}
		}
	},

	start: function(imageLink) {
		if (!document.getElementsByTagName){ return; }
		if((imageLink.getAttribute('rel') == 'galleryswitch')){

			//$('product-img-box').getElementsByClassName('gallery-preview').each(function(s) {
			  //s.setAttribute("style", "border-color: #CCCCCC");
			//});

			//var selected = imageLink.firstChild();
			//selected[0].setAttribute("style", "border-color: #000000");
			$('product-image').update('<a href="'+ imageLink.getAttribute('href') +'"  rel="lightbox[rotation]"><img id="galleryswitch-image-preview" src="' + imageLink.getAttribute('href') + '"/></a>');
			$('galleryswitch-image-preview').style.width = '670px';
			$('galleryswitch-image-preview').style.height = 'auto';
		}
		initLightbox();
		return false;
	},

	drawgallery: function(imageLinks) {
		if (!document.getElementsByTagName){ return; }
		if((imageLinks.getAttribute('rel') == 'galleryshow')){
			var vars = imageLinks.getAttribute('vars').split(/(,)/);
			var out1 = '';
			var out2 = '<div class="more-views"><ul>';
			for (var i=0; i<vars.length; i++){
				if(vars[i] != ","){
					if(i == 0 ){
						out1+= '<div id="product-image"><a href="' + vars[i] + '" rel="lightbox[rotation]" title=""><img src="' + vars[i] + '" alt="" id="galleryswitch-image-preview" /></a></div>';
						var firstplus = '<li><a href="' + vars[i] + '" rel="galleryswitch" ><img src="' + vars[i] + '" width="50" height="50" alt=""/></a></li>';
					}else{
						out2+= '<li><a href="' + vars[i] + '" rel="galleryswitch" ><img src="' + vars[i] + '" width="50" height="50" alt="" class="gallery-preview"/></a></li>';

					}
				}
			}
			out2+= firstplus;
			out2+= '</ul></div>';
			$('product_tabs_photos_contents').update(out1+out2);
			$('galleryswitch-image-preview').style.width = '670px';
			$('galleryswitch-image-preview').style.height = 'auto';
			initGalleryswitch();
			//alert(out1+out2);
		}
		initLightbox();
	}

}

function initGalleryswitch() { myGalleryswitch = new Galleryswitch(); }
Event.observe(window, 'load', initGalleryswitch, false);
