(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;f.$label.data("InFieldLabels",f);f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);(function($){$.fn.fancyZoom=function(options){var options=options||{};var directory=options&&options.directory?options.directory:'/images';var zooming=false;if($('#zoom').length==0){var ext=$.browser.msie?'gif':'png';var html='<div id="zoom" style="display:none;"> \
                  <table id="zoom_table" style="border-collapse:collapse; width:100%; height:100%;"> \
                    <tbody> \
                      <tr> \
                        <td class="tl" style="background:url('+directory+'/tl.'+ext+') 0 0 no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                        <td class="tm" style="background:url('+directory+'/tm.'+ext+') 0 0 repeat-x; height:20px; overflow:hidden;" /> \
                        <td class="tr" style="background:url('+directory+'/tr.'+ext+') 100% 0 no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                      </tr> \
                      <tr> \
                        <td class="ml" style="background:url('+directory+'/ml.'+ext+') 0 0 repeat-y; width:20px; overflow:hidden;" /> \
                        <td class="mm" style="background:#fff; vertical-align:top; padding:10px;"> \
                          <div id="zoom_content"> \
                          </div> \
                        </td> \
                        <td class="mr" style="background:url('+directory+'/mr.'+ext+') 100% 0 repeat-y;  width:20px; overflow:hidden;" /> \
                      </tr> \
                      <tr> \
                        <td class="bl" style="background:url('+directory+'/bl.'+ext+') 0 100% no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                        <td class="bm" style="background:url('+directory+'/bm.'+ext+') 0 100% repeat-x; height:20px; overflow:hidden;" /> \
                        <td class="br" style="background:url('+directory+'/br.'+ext+') 100% 100% no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                      </tr> \
                    </tbody> \
                  </table> \
                  <a href="#" title="Close" id="zoom_close" style="position:absolute; top:0; left:0;"> \
                    <img src="'+directory+'/closebox.'+ext+'" alt="Close" style="border:none; margin:0; padding:0;" /> \
                  </a> \
                </div>';$('body').append(html);$('html').click(function(e){if($(e.target).parents('#zoom:visible').length==0)hide();});$(document).keyup(function(event){if(event.keyCode==27&&$('#zoom:visible').length>0)hide();});$('#zoom_close').click(hide);}
var zoom=$('#zoom');var zoom_table=$('#zoom_table');var zoom_close=$('#zoom_close');var zoom_content=$('#zoom_content');var middle_row=$('td.ml,td.mm,td.mr');this.each(function(i){$($(this).attr('href')).hide();$(this).click(show);});return this;function show(e){if(zooming)return false;zooming=true;var content_div=$($(this).attr('href'));var zoom_width=options.width;var zoom_height=options.height;var width=window.innerWidth||(window.document.documentElement.clientWidth||window.document.body.clientWidth);var height=window.innerHeight||(window.document.documentElement.clientHeight||window.document.body.clientHeight);var x=window.pageXOffset||(window.document.documentElement.scrollLeft||window.document.body.scrollLeft);var y=window.pageYOffset||(window.document.documentElement.scrollTop||window.document.body.scrollTop);var window_size={'width':width,'height':height,'x':x,'y':y}
var width=(zoom_width||content_div.width())+60;var height=(zoom_height||content_div.height())+60;var d=window_size;var newTop=Math.max((d.height/2)-(height/2)+y,0);var newLeft=(d.width/2)-(width/2);var curTop=e.pageY;var curLeft=e.pageX;zoom_close.attr('curTop',curTop);zoom_close.attr('curLeft',curLeft);zoom_close.attr('scaleImg',options.scaleImg?'true':'false');$('#zoom').hide().css({position:'absolute',top:curTop+'px',left:curLeft+'px',width:'1px',height:'1px'});fixBackgroundsForIE();zoom_close.hide();if(options.closeOnClick){$('#zoom').click(hide);}
if(options.scaleImg){zoom_content.html(content_div.html());$('#zoom_content img').css('width','100%');}else{zoom_content.html('');}
$('#zoom').animate({top:newTop+'px',left:newLeft+'px',opacity:"show",width:width,height:height},500,null,function(){if(options.scaleImg!=true){zoom_content.html(content_div.html());}
unfixBackgroundsForIE();zoom_close.show();zooming=false;})
$('body').append('<div id="curtain" style="width:'+$(window).width()+'px;height:'+$(window).height()+'px;top:'+y+'px;left:0px;background-color:#000000;opacity:0.0;z-index:9998;visibility: visible;"></div>');$('#curtain').animate({opacity:0.8},'fast');return false;}
function hide(){if(zooming)return false;zooming=true;$('#zoom').unbind('click');fixBackgroundsForIE();if(zoom_close.attr('scaleImg')!='true'){zoom_content.html('');}
zoom_close.hide();$('#zoom').animate({top:zoom_close.attr('curTop')+'px',left:zoom_close.attr('curLeft')+'px',opacity:"hide",width:'1px',height:'1px'},500,null,function(){if(zoom_close.attr('scaleImg')=='true'){zoom_content.html('');}
unfixBackgroundsForIE();zooming=false;});$('#curtain').remove();return false;}
function switchBackgroundImagesTo(to){$('#zoom_table td').each(function(i){var bg=$(this).css('background-image').replace(/\.(png|gif|none)\"\)$/,'.'+to+'")');$(this).css('background-image',bg);});var close_img=zoom_close.children('img');var new_img=close_img.attr('src').replace(/\.(png|gif|none)$/,'.'+to);close_img.attr('src',new_img);}
function fixBackgroundsForIE(){if($.browser.msie&&parseFloat($.browser.version)>=7){switchBackgroundImagesTo('gif');}}
function unfixBackgroundsForIE(){if($.browser.msie&&$.browser.version>=7){switchBackgroundImagesTo('png');}}}})(jQuery);if(window.addEventListener){var kkeys=[],kcode="38,38,40,40,37,39,37,39,66,65";window.addEventListener("keydown",function(e){kkeys.push(e.keyCode);if(kkeys.toString().indexOf(kcode)>=0)
jQuery('body').append("<img id='l' src='/images/l.png' style='position:fixed;z-index:999999;top:50%;left:50%;margin:-290px 0px 0px -370px;'>");},true);}
(function($) {
	var has_VML, create_canvas_for, add_shape_to, clear_canvas, shape_from_area,
		canvas_style, fader, hex_to_decimal, css3color, is_image_loaded, options_from_area, regions, offices;


	has_VML = document.namespaces;
	has_canvas = !!document.createElement('canvas').getContext;

	if(!(has_canvas || has_VML)) {
		$.fn.maphilight = function() { return this; };
		return;
	}
	
	if(has_canvas) {
		fader = function(element, opacity, interval) {
			if(opacity <= 1) {
				element.style.opacity = opacity;
				window.setTimeout(fader, 10, element, opacity + 0.1, 10);
			}
		};
		
		hex_to_decimal = function(hex) {
			return Math.max(0, Math.min(parseInt(hex, 16), 255));
		};
		css3color = function(color, opacity) {
			return 'rgba('+hex_to_decimal(color.substr(0,2))+','+hex_to_decimal(color.substr(2,2))+','+hex_to_decimal(color.substr(4,2))+','+opacity+')';
		};
		create_canvas_for = function(img) {
			var c = $('<canvas style="width:'+img.width+'px;height:'+img.height+'px;"></canvas>').get(0);
			c.getContext("2d").clearRect(0, 0, c.width, c.height);
			return c;
		};
		add_shape_to = function(canvas, shape, coords, options, name) {
			var i, context = canvas.getContext('2d');
			context.beginPath();
			if(shape == 'rect') {
				context.rect(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]);
			} else if(shape == 'poly') {
				context.moveTo(coords[0], coords[1]);
				for(i=2; i < coords.length; i+=2) {
					context.lineTo(coords[i], coords[i+1]);
				}
			} else if(shape == 'circ') {
				context.arc(coords[0], coords[1], coords[2], 0, Math.PI * 2, false);
			}
			context.closePath();
			if(options.fill) {
				context.fillStyle = css3color(options.fillColor, options.fillOpacity);
				context.fill();
			}
			if(options.stroke) {
				context.strokeStyle = css3color(options.strokeColor, options.strokeOpacity);
				context.lineWidth = options.strokeWidth;
				context.stroke();
			}
			if(options.fade) {
				fader(canvas, 0);
			}
		};
		clear_canvas = function(canvas) {
			canvas.getContext('2d').clearRect(0, 0, canvas.width,canvas.height);
		};
	} else {   // ie executes this code
		create_canvas_for = function(img) {
			return $('<var style="zoom:1;overflow:hidden;display:block;width:'+img.width+'px;height:'+img.height+'px;"></var>').get(0);
		};
		add_shape_to = function(canvas, shape, coords, options, name) {
			var fill, stroke, opacity, e;
			fill = '<v:fill color="#'+options.fillColor+'" opacity="'+options.fillOpacity+'" />';
			stroke = (options.stroke ? 'strokeweight="'+options.strokeWidth+'" stroked="t" strokecolor="#'+options.strokeColor+'"' : 'stroked="f"');
			opacity = '<v:stroke opacity="'+options.strokeOpacity+'"/>';
			if(shape == 'rect') {
				e = $('<v:rect name="'+name+'" filled="t" '+stroke+' style="zoom:1;margin:0;padding:0;display:block;position:absolute;left:'+coords[0]+'px;top:'+coords[1]+'px;width:'+(coords[2] - coords[0])+'px;height:'+(coords[3] - coords[1])+'px;"></v:rect>');
			} else if(shape == 'poly') {
				e = $('<v:shape name="'+name+'" filled="t" '+stroke+' coordorigin="0,0" coordsize="'+canvas.width+','+canvas.height+'" path="m '+coords[0]+','+coords[1]+' l '+coords.join(',')+' x e" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:0px;width:'+canvas.width+'px;height:'+canvas.height+'px;"></v:shape>');
			} else if(shape == 'circ') {
				e = $('<v:oval name="'+name+'" filled="t" '+stroke+' style="zoom:1;margin:0;padding:0;display:block;position:absolute;left:'+(coords[0] - coords[2])+'px;top:'+(coords[1] - coords[2])+'px;width:'+(coords[2]*2)+'px;height:'+(coords[2]*2)+'px;"></v:oval>');
			}
			e.get(0).innerHTML = fill+opacity;
			$(canvas).append(e);
		};
		clear_canvas = function(canvas) {
			$(canvas).find('[name=highlighted]').remove();
		};
	}
	
	shape_from_area = function(area) {
		var i, coords = area.getAttribute('coords').split(',');
		for (i=0; i < coords.length; i++) { coords[i] = parseFloat(coords[i]); }
		return [area.getAttribute('shape').toLowerCase().substr(0,4), coords];
	};

	options_from_area = function(area, options) {
		var $area = $(area);
		return $.extend({}, options, $.metadata ? $area.metadata() : false, $area.data('maphilight'));
	};
	
	is_image_loaded = function(img) {
		if(!img.complete) { return false; } // IE
		if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) { return false; } // Others
		return true;
	};

	canvas_style = {
		position: 'absolute',
		left: 0,
		top: 0,
		padding: 0,
		border: 0
	};
	
	var ie_hax_done = false;
	$.fn.maphilight = function(opts) {
		opts = $.extend({}, $.fn.maphilight.defaults, opts);
		
		if($.browser.msie && !ie_hax_done) {
			document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
			var style = document.createStyleSheet();
			var shapes = ['shape','rect', 'oval', 'circ', 'fill', 'stroke', 'imagedata', 'group','textbox'];
			$.each(shapes,
				function() {
					style.addRule('v\\:' + this, "behavior: url(#default#VML); antialias:true");
				}
			);
			ie_hax_done = true;
		}
		
		return this.each(function() {
			var img, wrap, options, map, canvas, canvas_always, mouseover, highlighted_shape, usemap;
			img = $(this);

			if(!is_image_loaded(this)) {
				// If the image isn't fully loaded, this won't work right.  Try again later.
				return window.setTimeout(function() {
					img.maphilight(opts);
				}, 200);
			}

			options = $.extend({}, opts, $.metadata ? img.metadata() : false, img.data('maphilight'));

			// jQuery bug with Opera, results in full-url#usemap being returned from jQuery's attr.
			// So use raw getAttribute instead.
			usemap = img.get(0).getAttribute('usemap');

			map = $('map[name="'+usemap.substr(1)+'"]');

			if(!(img.is('img') && usemap && map.size() > 0)) { return; }

			if(img.hasClass('maphilighted')) {
				// We're redrawing an old map, probably to pick up changes to the options.
				// Just clear out all the old stuff.
				var wrapper = img.parent();
				img.insertBefore(wrapper);
				wrapper.remove();
				$(map).unbind('.maphilight').find('area[coords]').unbind('.maphilight');
			}

			wrap = $('<div></div>').css({
				display:'block',
				background:'url('+this.src+')',
				position:'relative',
				padding:0,
				width:this.width,
				height:this.height
				});
			if(options.wrapClass) {
				if(options.wrapClass === true) {
					wrap.addClass($(this).attr('class'));
				} else {
					wrap.addClass(options.wrapClass);
				}
			}
			img.before(wrap).css('opacity', 0).css(canvas_style).remove();
			if($.browser.msie) { img.css('filter', 'Alpha(opacity=0)'); }
			wrap.append(img);
			
			canvas = create_canvas_for(this);
			$(canvas).css(canvas_style);
			canvas.height = this.height;
			canvas.width = this.width;
			
			mouseover = function(e) {
				var shape, area_options;
				area_options = options_from_area(this, options);
				if(
					!area_options.neverOn
					&&
					!area_options.alwaysOn
				) {
					shape = shape_from_area(this);
					add_shape_to(canvas, shape[0], shape[1], area_options, "highlighted");
					if(area_options.groupBy && $(this).attr(area_options.groupBy)) {
						var first = this;
						map.find('area['+area_options.groupBy+'='+$(this).attr(area_options.groupBy)+']').each(function() {
							if(this != first) {
								var subarea_options = options_from_area(this, options);
								if(!subarea_options.neverOn && !subarea_options.alwaysOn) {
									var shape = shape_from_area(this);
									add_shape_to(canvas, shape[0], shape[1], subarea_options, "highlighted");
								}
							}
						});
					}
				}
				if(!$(map).hasClass('inactive')){
					$('#officepanel div.countrylist')
						.empty()
						.append('<h5>'+$(e.target).attr('title')+'</h5>');
					$('#officepanel')
						.css({visibility : 'visible'});
				}
			}
			
			mouseout = function(e) {
				clear_canvas(canvas);
				if(!$(map).hasClass('inactive')){
					$('#officepanel').css({visibility: 'hidden'});
				}
			}
			
			click = function(e) {
				var region = $(e.target).attr('alt'),
					name = $(e.target).attr('title');
				$(map).addClass('inactive')
				showcities(region, name);
			}
			
			showoffices = function() {
				var country = $(this).text();
				$('#officepanel').css({'background-position' : '160px 8px'});
				$.ajax({
					type: "post",
				  	url: 'http://ddb.com/offices/officedb.php',
				  	data: "country="+country,
				  	success: function(data) {
				  		$('#officepanel div.officelist').remove();
				  		$('#officepanel')
				  			.append(data);
				  		
				  		var oh = $('#officepanel').height(),
				  			ch = $('#officepanel div.officelist').eq(0).height(),
				  			bh = $('#officewrapper').height();
				  		
				  		if(ch > bh){
				  			nbh = "+="+(ch-bh)+"px";
				  		}else{
				  			nbh = "+=0px";
				  		}
				  		
				  		if((oh - ch) < 0){
				  			var dh = "+="+(oh - ch)*(-1)+"px"
				  		}else{
				  			var dh = "+=0px";
				  		}
				  		
				  		$('#officewrapper').animate({'height':nbh},'fast');
				  		$('#officepanel')
				  			.animate({'width' : 720, 'margin-right' : 60, 'height' : dh},'fast',function(){
				  				$(this)
				  					.css({
				  						'-webkit-box-shadow' : '0px 0px 4px rgba(0,0,0,0.2)',
				  						'-moz-box-shadow' : '0px 0px 4px rgba(0,0,0,0.2)',
				  						'box-shadow' : '0px 0px 4px rgba(0,0,0,0.2)',
				  						'-webkit-border-radius' : '8px',
				  						'-moz-border-radius' : '8px',
				  						'border-radius' : '8px',
				  						'border-right' : 'solid 1px #e0e0e0',
				  						'background-position' :  '-1000px -1000px'
				  					});
				  				$('#close').css({'display' : 'block'});
				  			})
				  			
				  	}
				});
			}
			
			showcities = function(region, name) {
				$('#officepanel').css('height','16px');
				$('#officepanel div.countrylist')
					.empty()
					.append('<h5>'+name+'</h5>');
				$('#officepanel').css({'background-position' : '160px 8px'});
				$.ajax({
					type: "post",
				  	url: 'http://ddb.com/offices/officedb.php',
				  	data: "region="+region,
				  	success: function(data) {
				  		$('div.countrylist')
				  			.append(data)
				  			.find('li a')
				  			.bind('click',showoffices);
				  			
				  		var oh = $('#officepanel').height(),
				  			ch = $('div.countrylist').height(),
				  			bh = $('#officewrapper').height();
				  			
				  			if(ch > bh){
				  				nbh = "+="+(ch-bh)+"px";
				  			}else{
				  				nbh = "+=0px";
				  			}
				  		
				  		$('#officewrapper').animate({'height':nbh},'fast');
				  		$('#officepanel').animate({'height' : ch},'fast',function(){
				  			$(this).css({'background-position' :  '-1000px -1000px'});
				  		});
				  		
				  	}
				});
			}
			
			hideoffices = function(){
				$('#close').hide(); 
				clear_canvas(canvas);
				$('#officepanel').fadeOut('fast',function(){
					$('#officepanel div.countrylist').empty();
					$('#officepanel div.officelist').remove();
					$(map)
						.removeClass('inactive')
					$('#officepanel').css({
						'visibility'			: 'hidden',
						'-webkit-box-shadow'	: '-4px 0px 4px rgba(0,0,0,0.1)',
						'-moz-box-shadow'		: '-4px 0px 4px rgba(0,0,0,0.1)',
						'box-shadow'			: '-4px 0px 4px rgba(0,0,0,0.1)',
						'-webkit-border-top-right-radius': '0px',
						'-webkit-border-bottom-right-radius': '0px',
						'-moz-border-radius-topright': '0px',
						'-moz-border-radius-bottomright': '0px',
						'border-top-right-radius': '0px',
						'border-bottom-right-radius': '0px',
						'width'					: '180px',
						'margin-right'			: '0px',
						'height'				: '16px',
						'opacity'				: '1.0',
						'display'				: 'block',
						'border-right'			: '0px'
					});
				});
			}
			
			$('#close').bind('click', hideoffices);
			
			$(map).bind('alwaysOn.maphilight', function() {
				// Check for areas with alwaysOn set. These are added to a *second* canvas,
				// which will get around flickering during fading.
				if(canvas_always) {
					clear_canvas(canvas_always)
				}
				if(!has_canvas) {
					$(canvas).empty();
				}
				$(map).find('area[coords]').each(function() {
					var shape, area_options;
					area_options = options_from_area(this, options);
					if(area_options.alwaysOn) {
						if(!canvas_always && has_canvas) {
							canvas_always = create_canvas_for(img.get());
							$(canvas_always).css(canvas_style);
							canvas_always.width = img.width();
							canvas_always.height = img.height();
							img.before(canvas_always);
						}
						shape = shape_from_area(this);
						if (has_canvas) {
							add_shape_to(canvas_always, shape[0], shape[1], area_options, "");
						} else {
							add_shape_to(canvas, shape[0], shape[1], area_options, "");
						}
					}
				});
			});
			
			if(options.alwaysOn) {
				$(map).find('area[coords]').each(mouseover);
			} else {
				$(map).find('area[coords]')
					.trigger('alwaysOn.maphilight')
					.bind('mouseover.maphilight', mouseover)
					.bind('mouseout.maphilight', mouseout)
					.bind('click.maphilight', click);
			}
			
			img.before(canvas); // if we put this after, the mouseover events wouldn't fire.
			
			img.addClass('maphilighted');
		});
	};
	$.fn.maphilight.defaults = {
		fill: true,
		fillColor: '000000',
		fillOpacity: 0.2,
		stroke: true,
		strokeColor: 'ffffff',
		strokeOpacity: 1,
		strokeWidth: 1,
		fade: true,
		alwaysOn: false,
		neverOn: false,
		groupBy: false,
		wrapClass: true
	};
})(jQuery);
