/*
jquery.event.drag.js ~ v1.5 ~ Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
Liscensed under the MIT License ~ http://threedubmedia.googlecode.com/files/MIT-LICENSE.txt
*/
(function(E){E.fn.drag=function(L,K,J){if(K){this.bind("dragstart",L)}if(J){this.bind("dragend",J)}return !L?this.trigger("drag"):this.bind("drag",K?K:L)};var A=E.event,B=A.special,F=B.drag={not:":input",distance:0,which:1,dragging:false,setup:function(J){J=E.extend({distance:F.distance,which:F.which,not:F.not},J||{});J.distance=I(J.distance);A.add(this,"mousedown",H,J);if(this.attachEvent){this.attachEvent("ondragstart",D)}},teardown:function(){A.remove(this,"mousedown",H);if(this===F.dragging){F.dragging=F.proxy=false}G(this,true);if(this.detachEvent){this.detachEvent("ondragstart",D)}}};B.dragstart=B.dragend={setup:function(){},teardown:function(){}};function H(L){var K=this,J,M=L.data||{};if(M.elem){K=L.dragTarget=M.elem;L.dragProxy=F.proxy||K;L.cursorOffsetX=M.pageX-M.left;L.cursorOffsetY=M.pageY-M.top;L.offsetX=L.pageX-L.cursorOffsetX;L.offsetY=L.pageY-L.cursorOffsetY}else{if(F.dragging||(M.which>0&&L.which!=M.which)||E(L.target).is(M.not)){return }}switch(L.type){case"mousedown":E.extend(M,E(K).offset(),{elem:K,target:L.target,pageX:L.pageX,pageY:L.pageY});A.add(document,"mousemove mouseup",H,M);G(K,false);F.dragging=null;return false;case !F.dragging&&"mousemove":if(I(L.pageX-M.pageX)+I(L.pageY-M.pageY)<M.distance){break}L.target=M.target;J=C(L,"dragstart",K);if(J!==false){F.dragging=K;F.proxy=L.dragProxy=E(J||K)[0]}case"mousemove":if(F.dragging){J=C(L,"drag",K);if(B.drop){B.drop.allowed=(J!==false);B.drop.handler(L)}if(J!==false){break}L.type="mouseup"}case"mouseup":A.remove(document,"mousemove mouseup",H);if(F.dragging){if(B.drop){B.drop.handler(L)}C(L,"dragend",K)}G(K,true);F.dragging=F.proxy=M.elem=false;break}return true}function C(M,K,L){M.type=K;var J=E.event.handle.call(L,M);return J===false?false:J||M.result}function I(J){return Math.pow(J,2)}function D(){return(F.dragging===false)}function G(K,J){if(!K){return }K.unselectable=J?"off":"on";K.onselectstart=function(){return J};if(K.style){K.style.MozUserSelect=J?"":"none"}}})(jQuery);


Math.mid = function(a, b, c) { return Math.max(a, Math.min(b, c)); };


$(function()
{

	$("div.map").each(function()
	{
		var $this = $(this), pause = false, speed = 0, angleX = Math.random() * 360, angleY = Math.random() * 360, dragEndTimer = null;

		$this.empty().append('<div class="clip"><p class="legend"><span>Certified DMC</span> <span class="free">Location available</span></p><p class="author">Map by <a href="http://www.merixstudio.com/">MerixStudio.com</a></p></div>');

		var $clip = $this.find("div.clip");
		var $map = $('<ul></ul>').appendTo($clip);
		var $nav = $('<div class="nav"><p class="map"><span class="window"><a href="#"></a></span></p><p class="toggle"><a href="#"><span class="on">Hide Navigation</span><span class="off">Show Navigation</span></a></p></div>').appendTo($clip);
		var $navWindow = $nav.find("span.window");
		var x = Math.floor((Math.sin(angleX * Math.PI / 180) + 1) * ((3800 - $clip.width()) / 2));
		var y = Math.floor((Math.cos(angleY * Math.PI / 180) + 1) * ((1700 - $clip.height()) / 2));

		if (typeof markers != "undefined")
			for (var i = 0; i < markers.length; i++)
			{
				if (markers[i].length != 5)
					continue;
				var markerType = markers[i][4] ? '' : 'class="free" ';
				$map.append('<li ' + markerType + 'style="left: ' + markers[i][0] + 'px; top: ' + markers[i][1] + 'px"><a href="http://www.thedmcdirectory.com/wp-content/themes/tdd/dmc.php?c=' + (markers[i][4] ? markers[i][2] : 'FREE') + '">' + markers[i][3] + '</a></li>');
				var pointType = markers[i][4] ? '' : ' free';
				$nav.find("p.map").append('<span class="point' + pointType + '" style="left: ' + (markers[i][0] * .07) + 'px; top: ' + (markers[i][1] * .07) + 'px"></span>');
			}

		$map.find("li").click(function() { $(this).find("a").click(); return false; });

		$map.find("a").click(function() { return false; });
		$map.find("a").fancybox({
			'zoomOpacity'			: true,
			'overlayShow'			: true,
			'overlayOpacity'		: 0,
			'frameWidth'			: 850,
			'frameHeight'			: 550,
			'hideOnContentClick': 	false
		});

		var moveMap = function()
		{
			if (pause)
				return;

			speed < 5 ? speed += 0.05 : speed = 5;
			angleX < 360 ? angleX += .01 * speed : angleX -= 360;
			angleY < 360 ? angleY += .003 * speed : angleY -= 360;

			x = Math.floor((Math.sin(angleX * Math.PI / 180) + 1) * ((3800 - $clip.width()) / 2));
			y = Math.floor((Math.cos(angleY * Math.PI / 180) + 1) * ((1700 - $clip.height()) / 2));

			mapGoTo(x, y);
			navGoTo(x, y);
		}

		var mapPause = function()
		{
			pause = true;
			$map.stop(true);
			$navWindow.stop(true);
		};

		var mapResume = function()
		{
			if (dragEndTimer)
				clearTimeout(dragEndTimer);

			dragEndTimer = setTimeout(function()
			{
				dragEndTimer = null;
				mapGoTo(x, y, 15000, function() { speed = 0; pause = false; });
				navGoTo(x, y, 15000);
			}, 15000);
		};

		var mapGoTo = function(x, y, time, callback) { $map.stop(true).animate({ left: -x + "px", top: -y + "px" }, time || 0, callback); };
		var navGoTo = function(x, y, time) { $navWindow.stop(true).animate({ left: (x * .07) + "px", top: (y * .07) + "px" }, time || 0); };

		$map
			.bind("mousedown", mapPause)
			.bind("mouseup dragend", mapResume)
			.bind("drag", { not: "li" }, function(e)
			{
				var pos = $this.offset();
				var x = Math.mid(-$map.width() + $clip.width(), e.offsetX - pos.left, 0);
				var y = Math.mid(-$map.height() + $clip.height(), e.offsetY - pos.top, 0);
				$(this).stop(true).animate({ left: x + "px", top: y + "px" }, 0);
				navGoTo(-x, -y);
			});

		$nav.find("p.toggle a").click(function()
		{
			$(this).toggleClass("hide");
			$nav.find("p.map").stop(true).animate({ width: "toggle", height: "toggle", marginTop: "toggle", marginLeft: "toggle", marginRight: "toggle" }, 250);
			$nav.stop(true).animate({ width: ($(this).hasClass("hide") ? $(this).outerWidth() : "289") + "px" }, 250);
			return false;
		});

		$navWindow.css({ width: (Math.floor($clip.width() * .07) - 2) + "px", height: (Math.floor($clip.height() * .07) - 2) + "px" });

		$nav.find("p.map").click(function(e)
		{
			mapPause();

			var pos = $(this).offset();
			var x = Math.mid(0, e.pageX - pos.left - $navWindow.outerWidth() / 2, ($map.width() - $clip.width()) * .07);
			var y = Math.mid(0, e.pageY - pos.top - $navWindow.outerHeight() / 2, ($map.height() - $clip.height()) * .07);
			$navWindow.stop(true).animate({ left: x + "px", top: y + "px" }, 333);
			mapGoTo(x * (3800 / 265), y * (1700 / 119), 333);

			mapResume();
			return false;
		});

		$nav.find("p.map span.window a")
			.bind("mousedown", mapPause)
			.bind("mouseup dragend", mapResume)
			.click(function() { return false; })
			.bind("drag", function(e)
			{
				var pos = $(this).parent().parent().offset();
				var x = Math.mid(0, e.offsetX - pos.left, ($map.width() - $clip.width()) * .07);
				var y = Math.mid(0, e.offsetY - pos.top, ($map.height() - $clip.height()) * .07);
				$(this).parent().stop(true).animate({ left: x + "px", top: y + "px" }, 0);
				mapGoTo(x * (3800 / 265), y * (1700 / 119));
			});

		$(window).load(function() { setInterval(moveMap, 50); });

		if ($.browser.msie && $.browser.version <= 7)
			$map.find("li").bind("mouseenter mouseleave", function() { $(this).toggleClass("hover"); });
	});

});


$(document).ready(function(){
	$(".map").hide();
	$("a.map-on").show();
	
	$("a.map-on").click(function(){
		$(".map").show();
		$("a.map-off").show();
		$("#theworld").hide();
		$(this).hide();
		return false;
	});
	$("a.map-off").click(function(){
		$(".map").hide();
		$("a.map-on").show();
		$("#theworld").show();
		$(this).hide();
		return false;
	});
	
	$(".popup a.close").click(function(){
		$(".popup").fadeOut();
		return false;
	});
	
	$("#email").focus(function(){ if($(this).val() == "your email") $(this).val(""); });
	$("#email").blur(function(){ if($(this).val() == "") $(this).val("your email"); });
	
	$("#month").change(function(){
		$(this).parent().parent().submit();
	});
});