var ImageSwapper = Class.create();	
ImageSwapper.prototype = {
	watchLinks : new Array(),	
	_swapping : false,
	
	initialize : function(options){
		this.options = Object.extend({
			productName : null,
			watch_for : ".swapper",
			issuu_config : {
				viewer_url : "http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf",
				mode : "embed",
				layout : "http://skin.issuu.com/v/dark/layout.xml",
				username : "pediment"
			}
		}, options || {});
		
		this.watchLinks = $$(this.options.watch_for);
		if (this.watchLinks.length > 0){
			var loadingImg = new Element('img', {
				src: "/skin/frontend/pediment/default/images/swapper_spinner.gif",
				alt: "Spinner"
			});
			var loadingEl = new Element('p', {
				'class': "loading",
				id: "loading-bar"
			}).setStyle({
					display: "none"
			}).insert(loadingImg).insert(" Loading...");

			$('main-product-img').up(1).insert(loadingEl);
			this.watchLinks.each(function(el) {
				el.observe('click', function(ev){
					ev.stop();
					var hrefEl = ev.element();
					while(!hrefEl.tagName.match(/^a$/i)){
						hrefEl = hrefEl.up();
					}
					if($(hrefEl).hasClassName('issuu') || $(hrefEl).hasClassName('video')){
						myImageSwapper.swapImage(hrefEl);
					}else{
						myImageSwapper.showLoading(hrefEl);
					}	
				});
			});
		}
	},

	showLoading : function(hrefEl){
		if(!this._swapping){
			new Effect.Appear('loading-bar', {
				from: 0.0, 
				to: 0.8, 
				duration: 0.1,
				beforeStart: function(){myImageSwapper._swapping = true},
				afterFinish: this.swapImage(hrefEl)
			});
		}
	},

	swapImage : function(hrefEl){
		var newEl;
		$('main-product-img').id = 'main-product-img-old';
		if($(hrefEl).hasClassName('issuu')){
			var parsed_issuu_url = parseUri(hrefEl.href);
			var new_issuu_url = parsed_issuu_url.protocol+"://"+parsed_issuu_url.authority+parsed_issuu_url.path+"?mode=embed&layout=grey";
			
			newEl = new Element('iframe', {
				'class': 'issuu-preview embed',
				id: 'main-product-img',
				src: new_issuu_url,
				frameborder: 0,
				width: 545,
				height: 340
			});
			this.rebuildWrappers(newEl);
		}else if($(hrefEl).hasClassName('video') && $(hrefEl).href.match(/youtube/i)){
			var videoId = parseUri(hrefEl.href).queryKey.v;
			var video_url = "http://www.youtube.com/v/"+videoId+"&hl=en&fs=1&rel=0";
			newEl = new Element('div', {id: 'main-product-img'});
			this.rebuildWrappers(newEl);
			
			swfobject.embedSWF(video_url, "main-product-img", '545', '340', "9.0.0", null, null, {
				movie: video_url,
				wmode: 'transparent',
				allowfullscreen: 'true',
				menu: 'false',
				allowscriptaccess: 'always'
			}, {
				'class': 'video embed',
				id: 'main-product-img',
				width: '545',
				height: '340',
				type: 'application/x-shockwave-flash',
				data: video_url
			});
		}else{
			newEl = new Element('img', {
				id: 'main-product-img',
				title: hrefEl.title
			});
			
			newEl.observe('load', function(){
				myImageSwapper.rebuildWrappers(newEl);
			});
			
			newEl.src = hrefEl.href;
		}
	},
	
	rebuildWrappers : function(newEl){
		var date = new Date();
		var timestamp = date.getTime();
		var curWrapper = $('main-product-img-old').up();
		$(curWrapper).setStyle({zIndex: "50"});
	
		var newWrapper = new Element('div', {
			'class': 'main-img-subwrap',
			id: 'wrapper-'+timestamp
		}).setStyle({
			display: "none",
			zIndex: "100"
		});
		
		$(newWrapper).insert(newEl);
		$(curWrapper).up().insert({top: $(newWrapper)});
		
		if( $('main-product-img-old').hasClassName('embed') || $(newEl).hasClassName('embed') ){
			$(curWrapper).hide();
		}
		
		$(newWrapper).appear({
			afterFinish: function(){
				$(curWrapper).remove();
				$('loading-bar').setStyle({opacity: "0"});
				myImageSwapper._swapping = false
			}
		});
	}
}

/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
