/*
 * jQuery autoResize (textarea auto-resizer)
 * @copyright James Padolsey http://james.padolsey.com
 * @version 1.04
 */
(function(a){a.fn.autoResize=function(j){var b=a.extend({onResize:function(){},animate:true,animateDuration:150,animateCallback:function(){},extraSpace:20,limit:1000},j);this.filter('textarea').each(function(){var c=a(this).css({resize:'none','overflow-y':'hidden'}),k=c.height(),f=(function(){var l=['height','width','lineHeight','textDecoration','letterSpacing'],h={};a.each(l,function(d,e){h[e]=c.css(e)});return c.clone().removeAttr('id').removeAttr('name').css({position:'absolute',top:0,left:-9999}).css(h).attr('tabIndex','-1').insertBefore(c)})(),i=null,g=function(){f.height(0).val(a(this).val()).scrollTop(10000);var d=Math.max(f.scrollTop(),k)+b.extraSpace,e=a(this).add(f);if(i===d){return}i=d;if(d>=b.limit){a(this).css('overflow-y','');return}b.onResize.call(this);b.animate&&c.css('display')==='block'?e.stop().animate({height:d},b.animateDuration,b.animateCallback):e.height(d)};c.unbind('.dynSiz').bind('keyup.dynSiz',g).bind('keydown.dynSiz',g).bind('change.dynSiz',g)});return this}})(jQuery);

(function(b) {
	/*
	 * custom plugins
	 */
	b.fn.expander = function (opt) {
		
		var def = {
			collapsedTitle:'Show',
			collapseCallback:function () {},
			expandedTitle:'Hide',
			expandCallback:function () {},
			display:'block',
			showCallback:function () {},
			hideCallback:function () {},
			expandAnimation: {
				properties:{
					width:'100%'
				},
				duration: 300
			},
			collapseAnimation: {
				properties:{
					width:'100px'
				},
				duration: 300
			}
		};
		var t =  b.extend(def, opt);
		
		return this.each(function() {
			var $t = b(this);
			
			var $container = $t.addClass('ui-accordion ui-widget ui-helper-reset ui-accordion-icons');
			var $content = $container.wrapInner('<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="display:none;"></div>').children();
			var $header = b('<h3 class="ui-accordion-header ui-helper-reset ui-corner-all ui-state-active"></h3>').prependTo($container);
			
			var $icon = b('<span class="ui-icon ui-icon-triangle-1-e"></span>').prependTo($header);
			var $title = b('<a href="#">' + t.collapsedTitle + '</a>').appendTo($header);
			
			$container.css('display',t.display);
			
			var expand = function () {
				$header.removeClass('ui-corner-all').addClass('ui-corner-top');
				$icon.removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s');
				
				$content.slideDown(300, function () {
					$title.html(t.expandedTitle);
					t.expandCallback();
				});
			};
			
			var collapse = function () {
				$title.html(t.collapsedTitle);
				
				$content.slideUp(300, function () {
					$header.removeClass('ui-corner-top').addClass('ui-corner-all');
					$icon.removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
					t.collapseCallback();
				});
			};
			
			$t.bind('expand', function () {
				$t.animate(t.expandAnimation.properties, t.expandAnimation.duration, function() {
					expand();
				});
				
			}).bind('collapse', function () {
				$t.animate(t.collapseAnimation.properties, t.collapseAnimation.duration, function() {
					collapse();
				});
				
			}).bind('show', function () {
				$container.show(300, function () {
					$header.click();
				});
				
			}).bind('hide', function () {
				$container.hide(300, function () {
					$header.click();
				});
			});
			
			$header.toggle(function () {
				$t.animate(t.expandAnimation.properties, t.expandAnimation.duration, function() {
					expand();
				});
				
			}, function () {
				$t.animate(t.collapseAnimation.properties, t.collapseAnimation.duration, function() {
					collapse();
				});
				
			}).mouseover(function() {
				$header.addClass('ui-state-default').removeClass('ui-state-active');
				
			}).mouseout(function(){
				$header.addClass('ui-state-active').removeClass('ui-state-default');
			});
		});
	};
})(jQuery);

var $jq = jQuery.noConflict();
$jq(document).ready(function ($) {
	
	/*
	 * drop-down
	 * */
	$jq("ul.dropdown > li > a").click(function(){
        $jq(this).parent().find('ul:first').css('visibility', 'visible');
		return false;
    });
	$jq("ul.dropdown li ul li").hover(function () {
		$jq(this).addClass("hover");
	}, function () {
		$jq(this).parent().css('visibility', 'hidden');
		$jq(this).removeClass("hover");
	});

	$jq('#talk img').mouseenter(function () {
		$jq(this).effect( 'bounce', {'direction':'left','times':3}, 300);
		return false;
	});
	
	$jq('.contribution').each(function () {
		var text_area = $jq(this).find('.datamEditor textarea');
		var attachments = $jq(this).find('.AttachmentsList');
		var edit = $jq(this).find('.ContentEditMenu');
		
		text_area.focus(function () {
			attachments.fadeIn(100);
			edit.fadeIn(100);
		});
		
		text_area.autoResize({});
	});
	
	$jq('.GroupRegistration').each(function () {
		var text_area = $jq(this).find('textarea');
		text_area.autoResize({});
	});
	
	/*
	 * Specifically for the briefcase template
	 * */
	$jq('#briefcase').each(function () {
		
		var cle = false;
		var setupCLE = function () {
			if ( !cle ) {
				/*
				 * Blog Post WYSIWYG
				 * */
				$jq('.textarea textarea').cleditor({height:250});
				cle = true
			}
		}
		
		var $expander = $jq('#blogPost', this).expander({
			collapsedTitle:'New Post',
			expandedTitle:'Hide',
			display:'none',
			expandCallback:function () {
				setupCLE();
			},
			expandAnimation: {
				properties:{
					width:'700px'
				},
				duration: 300
			},
			collapseAnimation: {
				properties:{
					width:'100px'
				},
				duration: 300
			}
		});
		var $newPost = $jq('<ul class="newPost"><li><a href="#">Add New Post</a></li></ul>').appendTo('.zoneLeft #customMenu');
		
		$newPost.click(function () {
			$expander.trigger('show');
			return false;
		});
	});
	
	
	/*
	 * video overlay
	 * */
	$jq('a.video').prepend(''/*'<img class="play-button" src="/datam/images/play-button.png" />'*/).click(function () {
		$jq.modal('<div style="background-color:#fff;padding:10px;"><iframe src="' + $jq(this).attr('href') + '" frameborder="0" scrolling="no" height="375" width="590" style="border-color:#CCCCCC #EEEEEE #EEEEEE #CCCCCC;border-style:solid;border-width:2px" /><div>',{
			opacity:50,
			overlayClose:true,
			overlayCss: {
				backgroundColor:"#000"
			},
			onClose: function () {
				/*iframes can cause script errors in IEx*/
				window.location.reload();
				return false;
			}
		});
		return false;
	}).mouseover(function() {
		$jq(this).find('.play-button').hide();
	}).mouseout(function(){
		$jq(this).find('.play-button').show();
	});
	
	/*
	 * handlers for print and popup save 
	 * */
	$jq('a.save.popup').click(function () {
		$jq.modal('<div style="background-color:#fff;padding:10px;"><iframe src="' + $jq(this).attr('href') + '" frameborder="0" scrolling="no" height="375" width="590" style="border-color:#CCCCCC #EEEEEE #EEEEEE #CCCCCC;border-style:solid;border-width:2px" /><div>',{
			opacity:50,
			overlayClose:true,
			overlayCss: {
			backgroundColor:"#000"
			},
			onClose: function () {
				/*iframes can cause script errors in IEx*/
				window.location.reload();
				return false;
			}
		});
		return false;
	});
	$jq('a.print').click(function () {
		window.print();
	});
	
	if ( $jq('.newpost #plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_toolbar_AddNewMessage_list_editDoc_pnlEdit').html() ) {
		$jq('.newpost #plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_toolbar_AddNewMessage_list_pnlEdit').modal({
			appendTo: 'form',
			minWidth: 700,
			opacity:50,
			overlayClose:true,
			containerCss: {
				backgroundColor:'#FFF',
				padding:'10px'
			},
			overlayCss: {
				backgroundColor:"#000"
			},
			onClose: function () {
				document.location=document.location;
			}
		});
		$jq('#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_toolbar_AddNewMessage_list_editDoc_pnlEdit').parents('.simplemodal-wrap').css({
			'overflow-y':'scroll',
			'border':'2px solid #666666'
		});
	}
	
	/*
	 *  set up google maps
	 */
	$jq('.map_canvas').each(function () {
		var canvas = $jq(this);
		var latLng = new google.maps.LatLng(canvas.attr('lat'), canvas.attr('lng'));
		var infoStr = '<span style="color: black; font-size: 14px;">' + 
			canvas.attr('office') + 
			'<br></span><a href="http://maps.google.com/maps?q=to ' + 
			canvas.attr('addr') + 
			' NZ" style="margin-top:8px;display:block;text-decoration:underline;color:#0000CC;cursor:pointer;font-size:12px;">Directions &raquo;</a>';
		
		var map = new google.maps.Map(canvas.get(0), {
			zoom: 11,
			center: latLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		
		var infowindow = new google.maps.InfoWindow({
		    content: infoStr
		});
		
		var marker = new google.maps.Marker({
			map: map,
			position: latLng,
			title: 'Datam'
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
	});
	
	
	/*whiteboard image hover*/
	/*$jq('.whiteboard_new img').mouseenter(function (){
		var sp_src = $jq(this).attr('src').split('/Datam/images/whiteboard/');
		if( sp_src[1].substring(0,3) !== 'red') {
			$jq(this).attr('src', '/Datam/images/whiteboard/red/' + sp_src[1]);
		}
	}).mouseleave(function () {
		var sp_src = $jq(this).attr('src').split('/Datam/images/whiteboard/red');
		if( sp_src[1]) {
			$jq(this).attr('src', '/Datam/images/whiteboard/' + sp_src[1]);
		}
	});*/
	
	/*
	 *active browse image hover
	 */
	(function () {
		var firstImg = $jq('.browse a img:first');
		/*validate there are active images*/
		if ( firstImg ) {
			if ( firstImg.attr('src') ) {
				var initSideSize = firstImg.attr('src').split('maxSideSize=')[1];
				/*make sure the CSS is stable for overflows and child resizing*/
				$jq('.browse li').css({
					'display':'block',
					'float':'left',
					'width':'107px',
					'height':'68px'
				}).find('a').css({
					'overflow':'visible',
					'display':'block'
				});
				/*attach style and hover events for active images*/
				$jq('.browse a img').css({
					'position':'relative'
				}).mouseenter(function () {
					if( $jq(this).attr( 'width') === 107 ) {
						$jq(this).attr( 'width', 131 ).css({
							'left':'-12px',
							'top': '-8px',
							'z-index':'1000'
						});
					}
				}).mouseleave(function () {
					if( $jq(this).attr( 'width') !== 107 ) {
						$jq(this).attr( 'width', 107 ).css({
							'left':'0px',
							'top': '0px',
							'z-index':'0'
						});
					}
				});
			}
		}
	})();
	
	/*
	 * white-board
	 * */
	(function () {
		
		if ( $jq('.whiteboard_new').length ) {
			var clients = $jq.parseJSON( $jq('.whiteboard_new div[data]').attr('data') );
			
	       	$jq('.whiteboard_new .whiteboard_res_links').append(
	       		$jq.map(clients, function (n, i) {
	       			return '<a client="' + n['name'] + '"style="display:none;" href="' + n['url'] + '"><img src="' + n['imageURL'] + '?maxSideSize=60" /><p class="title">' + n['name'] + '</p><p class="discription">' + n['title'] + '</p></a>';
	       		}).join('')
	       	);
	       	
	       	var tick = '<img class="tick" src="/datam/images/Tick-red_60x57.png" style="position:absolute;display:none;left:0px;top:0px;"/>';
	       	var resLinks = $jq('.whiteboard_res_links');
	       	
	       	$jq('.whiteboard_new table a').each(function () {
	       		$jq(this).append(tick);
	       	}).mouseenter(function () {
	       		var top = Math.floor($jq('img:first',this).height()/2)-28 + 'px';
	       		var left = Math.floor($jq('img:first',this).width()/2)-30 + 'px';
	       		$jq(this).find('.tick:first').css({'top':top,'left':left}).show();
	       	}).mouseleave(function () {
	       		$jq(this).find('.tick:first').hide();
	       	}).click(function () {
	       		if ( !$jq(this).hasClass('selected') ) {
	       			$jq(this).addClass('selected');
	       			var tag = $jq(this).attr('tag');
	       			$jq.each(clients, function (i,n) {
	       				if ( $jq.inArray(tag + '', n['tagIds']) !== -1 ) {
	       					/*could be more elegant*/
	       					$jq('.whiteboard_res').slideDown(1000);
	       					$jq('.whiteboard_res a[client=' + n['name'] + ']').show();
	       				}
	       			});
	       		} else {
	       			$jq('.whiteboard_res a').hide();
	       			$jq(this).removeClass('selected');
	       			var count = 0;
	       			$jq('.whiteboard_new a').each(function () {
	       				if ( $jq(this).hasClass('selected') ) {
	       					var tag = $jq(this).attr('tag');
	       					$jq.each(clients, function (i,n) {
	       						if ( $jq.inArray(tag + '', n['tagIds']) !== -1 ) {
	       							$jq('.whiteboard_res a[client=' + n['name'] + ']').show();
	       							count++;
	       						}
	       					});
	       				}
	       			});
	       			if( count === 0 ) {
	       				$jq('.whiteboard_res').slideUp(1000);
	       			}
	       		}
	       		var width = 0;
	       		//alert('before each');
	       		//alert(resLinks.find('a:visible').length);
	       		resLinks.find('a:visible').each(function (i, n) {
	       			width += $jq(n).outerWidth() + 4;/*at least 2 pixels due to inline-block spacing on some browsers*/
	       			//alert('width: ' + width);
	       		});
	       		resLinks.width(width);
	       		return false;
	       	});
		}
	}());
});