/**
 * jQuery Panorama plugin
 * @name jquery.panorama.js
 * @author Valerij Primachenko - vprimachenko(at)ya.ru
 * @version 0.1
 * @date Juni 20, 2008
 * @category jQuery plugin
 * @copyright (c) 2008 Valerij Primachenko - vprimachenko(at)ya.ru
 * @license CC Attribution-Noncommercial-No Derivative Works 2.0 Germanyl - http://creativecommons.org/licenses/by-nc-nd/2.0/de/
 * @example Visit http://neo101.freeweb7.com/jQuery.Panorama for more informations about this jQuery plugin
 */

var panoramamove = false;
var panoramaurl = "";
var panoramawidth = 0;
(function($){
	$.fn.panorama = function(options) {
		var defaults = {  
			width: 0,  
			lefttext: "&lt;",  
			righttext: "&gt;",  
			showalttext:true
		};
		
		var options = $.extend(defaults, options); 
		this.each(function() {  
			if (options.width == 0) options.width = parseInt($(this).attr("height"))/3*4;
			panoramaurl = $(this).attr("src");
			panoramawidth = parseInt($(this).attr("width"));
			var map = $(this).attr("usemap");
			var panoramahtml = '<div class="panorama" style="width:'+options.width+'px;height:'+$(this).attr("height")+'px;"><div class="title">'+$(this).attr("alt")+'</div><div class="scroller" style="width:'+options.width+'px;height:'+$(this).attr("height")+'px;"><div id="image-'+map+'" class="image-holder" style="background:url('+panoramaurl+');width:'+(panoramawidth+options.width)+'px;height:'+$(this).attr("height")+'px;">'
			
			$("map#"+map+" area[@shape=rect]").each(function(){
				var areacoordArray = coords_fill($(this).attr("coords"));
				panoramahtml += '<a href="'+$(this).attr("href")+'" title="'+$(this).attr("alt")+'" class="panormaarea" style="position:absolute; left: '+areacoordArray[0]+'px; top: '+areacoordArray[1]+'px; width: '+(areacoordArray[2]-areacoordArray[0])+'px; height: '+(areacoordArray[3]-areacoordArray[1])+'px;"><span>'+$(this).attr("alt")+'</span></a>';
				if ( parseInt(areacoordArray[0]) < options.width ) panoramahtml += '<a href="'+$(this).attr("href")+'" title="'+$(this).attr("alt")+'" class="panormaarea" style="position:absolute; left: '+(panoramawidth+parseInt(areacoordArray[0]))+'px; top: '+areacoordArray[1]+'px; width: '+(areacoordArray[2]-areacoordArray[0])+'px; height: '+(areacoordArray[3]-areacoordArray[1])+'px;"><span>'+$(this).attr("alt")+'</span></a>';
			});
			panoramahtml += '</div></div><div class="leftbtn">'+options.lefttext+'</div><div class="rightbtn">'+options.righttext+'</div></div>';
			$(this).after(panoramahtml);
			$(this).remove();
			$("map#"+map).remove();
		});
		function coords_fill(mycoords) {
			var position1=0;
			var position2=0;
			var tabresult = new Array();
			while ((position2 = mycoords.indexOf(',', position1)) >= 0) {
				tabresult.push(mycoords.substring(position1, position2));
				position1 = position2+1;
				position2 = position1+1;
			}
			tabresult.push(mycoords.substring(position1));
			return tabresult;
		}

		$(".rightbtn").hover(function(){panoramamove = true;panoramamoveit('#'+$(this).parent().children(".scroller").children(".image-holder").attr("id"),-5);},function(){panoramamove = false;});
		$(".leftbtn" ).hover(function(){panoramamove = true;panoramamoveit('#'+$(this).parent().children(".scroller").children(".image-holder").attr("id"),5);},function(){panoramamove = false;});
		$(".panormaarea").hover(function(){
			$(this).fadeTo(500,.5);
			if (options.showalttext) $("span",this).fadeIn("slow");
		},function(){
			$(this).fadeTo(500,.3);
			if (options.showalttext) $("span",this).fadeOut("fast");
		});
	};  
})(jQuery);
		function panoramamoveit(holderid, movement) {
			if (panoramamove) {
				var marginleft = parseInt($(holderid).css("margin-left"))+parseInt(movement);
				if ( marginleft > 0 ) $(holderid).css({marginLeft: "-"+panoramawidth+"px"});
				else if ( marginleft < -panoramawidth ) $(holderid).css({marginLeft: "0px"});
				else $(holderid).css({marginLeft: marginleft + "px"});
				window.setTimeout("panoramamoveit('"+holderid+"', "+movement+")", 1);
			}
		}
