// JavaScript Document
var Gallery = function ( mediumId , captionId ){
	// -------------------------- private variables -------------------------- 
	var _that = this;					
	// -------------------------- public variables -------------------------- 
	this.largeUrl 		= '';
	this.caption		= '';
	this.list 			= [];
	this.elMediumImage	= mediumId;
	this.elCaption		= captionId;
	// -------------------------- private methods -------------------------- 
	function stopDefaultEvent( eventObject ){
		if ( eventObject.preventDefault ) eventObject.preventDefault();
		eventObject.returnValue = false;
	}
	function getLargeImage(){ return _that.largeUrl; }
	function getCaption(){ return _that.caption; }
	// -------------------------- events -------------------------- 
	function thumb_onClick ( eventObject ){
		void stopDefaultEvent( eventObject );
		var data = this;
		// medium image
		void pln.node.setProperty( pln.node.getById( _that.elCaption ) , 'innerHTML' , data.caption );
		void pln.node.setProperty( pln.node.getById( _that.elMediumImage ) , 'src' , data.medium );
		// large image
		_that.largeUrl 	= data.large;
		_that.caption 	= data.caption;
	}
	// -------------------------- public methods -------------------------- 					
	this.add = function ( id , medium , large , caption ){
		void _that.list.push( { id : id , medium : medium , large : large , caption : caption } );
	}		
	// -------------------------- init -------------------------- 			
	this.init = function (){
		for ( var i = 0, count = _that.list.length; i < count; i++ ){
			var data	= _that.list[ i ];
			var thumb 	= pln.node.getById( data.id );
			void pln.events.addListener( thumb , 'click' , data , thumb_onClick );
		}
		// default image
		_that.largeUrl	= _that.list[ 0 ].large;
		_that.caption 	= _that.list[ 0 ].caption;
		// popup
		jQuery( '#' + _that.elMediumImage ).parent().colorbox({ maxHeight:'100%', maxWidth:'100%' , href : getLargeImage , title : getCaption });
	}
};							

var BackgroundSlideshow = function ( containerId , paginationId , delay ){
	// -------------------------- private variables -------------------------- 
	var _that 		= this;					
	var _currentId	= 0;
	var timeoutId	= null;
	// -------------------------- public variables -------------------------- 
	this.containerId	= containerId;
	this.paginationId	= paginationId;
	this.list 			= [];
	this.pages			= [];
	this.delay			= delay || 4;
	this.slides			= [];
	this.overlay		= null;
	this.isFirst		= true;
	this.animation		= null;
	// -------------------------- private methods -------------------------- 
	function stopDefaultEvent( eventObject ){
		if ( eventObject.preventDefault ) eventObject.preventDefault();
		eventObject.returnValue = false;
	}
	function getPageIndex ( element ){
		var index = -1;
		var count = 0;
		while ( count < _that.pages.length && index == -1 ){
			if ( element == _that.pages[ count ] ) index = count;
			count++;
		}
		return index;
	}
	function createSlide ( url ){
		var slide = pln.node.create( 'div' );
		void pln.node.add( slide , pln.node.getById( _that.containerId ) );
		void pln.node.setProperty( slide , 'width' , '100%' );
		void pln.node.setProperty( slide , 'height' , '100%' );
		void pln.node.setProperty( slide , 'display' , 'none' );
		void pln.node.setProperty( slide , 'top' , '0' );
		void pln.node.setProperty( slide , 'left' , '0' );
		void pln.node.setProperty( slide , 'position' , 'absolute' );
		void pln.node.setProperty( slide , 'background' , 'url(' + url + ') no-repeat top center' );
		
		var animation = new pln.motion.composition( {
			equation 	: pln.motion.equations.linear ,
			element 	: slide  ,
			property 	: 'alpha' ,
			duration 	: 10 , 
			start 		: 100 , 
			end 		: 0 
		} );
		
		return { slide : slide , animation : animation };
	}
	function show ( index ){
		var previous	= _that.slides[ _currentId ];
		var current	 	= _that.slides[ index ];
		
		void pln.node.setProperty( previous.slide , 'zIndex' , '1' );
		void pln.node.setProperty( current.slide , 'zIndex' , '0' );
		
		if ( !_that.isFirst ) void previous.animation.start();
		void pln.node.setProperty( current.slide , 'display' , 'block' );	
		void pln.node.setProperty( current.slide , 'alpha' , 100 );
		
		_that.isFirst = false;
		_currentId = index;
				
		// show the selected page
		for ( var i = 0, count = _that.pages.length; i < count; i++ ){
			void pln.node[ ( i == index ? 'add' : 'remove' ) + 'ClassName' ]( _that.pages[ i ] , 'selected' );			
		}
		
		// set timeout
		clearTimeout( timeoutId );
		if ( _that.list.length > 1 ) timeoutId = setTimeout( next , _that.delay * 1000 );
	}
	function getNextId (){
		var index = _currentId + 1 >= _that.list.length ? 0 : _currentId + 1;
		return index;
	}
	function next (){
		void show( getNextId() );
	}
	// -------------------------- events -------------------------- 
	function page_onClick ( eventObject ){
		void stopDefaultEvent( eventObject );
		var index = getPageIndex( pln.events.getElement( eventObject ) );
		void show( index );
	}
	// -------------------------- public methods --------------------------
	this.add = function ( src ){
		void _that.list.push( escape( src ) );
		// preload images
		var image = new Image();
		image.src = src;
	}	
	// -------------------------- init -------------------------- 			
	this.init = function (){
		// create slides
		for ( var i = 0 , count = _that.list.length; i < count; i++ ) _that.slides.push( createSlide( _that.list[ i ] ) );
		
		// get pages
		if ( pln.node.getById( _that.paginationId ) ) _that.pages = pln.node.getByName( 'a' , pln.node.getById( _that.paginationId ) );
		// add click events on page links
		
		for ( var i = 0, count = _that.pages.length; i < count; i++ ) void pln.events.addListener( _that.pages[ i ] , 'click' , null , page_onClick );
		// create overlay (for fadeIn effect)
		//void createOverlay();
		
		// Show the first one by default
		void show( 0 );
	}
}

var InteractiveMap = function ( mapId , markers ){
	// -------------------------- private variables -------------------------- 
	var googleMap 	= null;
	var x 			= 45.548273;
	var y 			= -73.619471;

	// -------------------------- private methods -------------------------- 
	function stopDefaultEvent( eventObject ){
		if ( eventObject.preventDefault ) eventObject.preventDefault();
		eventObject.returnValue = false;
	}
	
	function addEvents ( marker ){
		if ( marker.enabled ) google.maps.event.addListener( marker.shape , 'click', function() { highlightMarker( marker.index ); });
		
		var element 	= pln.node.getById( marker.elementId );
		var checkbox 	= pln.node.getById( marker.checkboxId );
		if ( pln.isHtmlElement( element ) ) 	void pln.events.addListener( element , 'click' , null , element_onClick , marker.index );
		if ( pln.isHtmlElement( checkbox ) ) 	void pln.events.addListener( checkbox , 'click' , null , checkbox_onClick , marker.index );
	}

	function clearInfoBubbles (){
		// remove the info bubbles
		for ( var i = 0, count = markers.length; i < count; i++ ){ 
			try{ 
				markers[ i ].info.close(); 
			}catch (e){}
		}
	}

	function highlightMarker ( index ){
		var marker = markers[ index ];
		if ( !marker || !marker.enabled ) return;
		
		void clearInfoBubbles();		
		void googleMap.panTo( marker.shape.getPosition() );
		void marker.info.open( googleMap , marker.shape );		
	}	
		
	// -------------------------- init -------------------------- 
	function init (){
		if ( markers && markers[ 0 ] ){
			var found = false;
			for ( var i in markers ){
				if ( found ) continue;
				if ( markers[ i ].x && markers[ i ].y ){
					x = markers[ i ].x;
					y = markers[ i ].y;
					found = true;
				}
			}
		}
		
		googleMap = new google.maps.Map(document.getElementById( mapId ) , {
		  zoom				: 5,
		  center			: new google.maps.LatLng( x , y ),
		  mapTypeId			: google.maps.MapTypeId.ROADMAP,
		  maxZoom			: 12,
		  streetViewControl	: false
		} );			
			
		// create all the markers
		for ( var i in markers ){
			var marker = markers[ i ];
			
			// create marker shape and info bubble
			var attr = { 
				position 	: new google.maps.LatLng( marker.x , marker.y ),
				labelContent: '<strong>'+marker.title+'</strong>',
				labelAnchor	: new google.maps.Point(150, 0),
				labelClass	: "markerLabel"
			};
		   			
			if ( marker.icon ) attr.icon = marker.icon;
			marker.index	= i;
			marker.shape 	= marker.x !== null && marker.y !== null ? new MarkerWithLabel( attr ) : null;
			marker.kmlShape	= marker.kml ? new google.maps.KmlLayer( marker.kml , { preserveViewport : false } ) : null;
			marker.info		= new google.maps.InfoWindow({ content : '<h3>' + marker.title + '</h3><p>' + marker.summary + ' ' + marker.readMore + '</p>' , maxWidth : 350 });
			marker.visible 	= false;
			marker.enabled	= !!( marker.shape && marker.info );
			markers[ i ] 	= marker;
						
			// add interactivity
			void addEvents( marker );
			
			// auto check/display
			if ( marker.enabled ){
				var checkbox = pln.node.getById( marker.checkboxId );
				if ( checkbox ) checkbox.checked = true;
						
				if ( marker.shape ) void marker.shape.setMap( googleMap );
				marker.visible = true;
			}			
		}
	}
		
	// -------------------------- events -------------------------- 
	function element_onClick ( e , index ){
		var element	= pln.events.getElement( e );
		var marker 	= markers[ index ];
				
		if ( !marker || !marker.enabled ){
			try{ console.log( 'marker "' + marker.title + '" is null' ); }catch(e){}
		}
				
		if ( !marker.visible ){
			var checkbox = pln.node.getById( marker.checkboxId );
			if ( checkbox ) checkbox.checked = true;
						
			if ( marker.shape ) 	void marker.shape.setMap( googleMap );
			if ( marker.kmlShape ){	
				void clearInfoBubbles();
				void marker.kmlShape.setMap( googleMap );
			}
			
			marker.visible = true;
		}
		void highlightMarker( index );
		void stopDefaultEvent( e );
	}	

	function checkbox_onClick ( e , index ){
		var checkbox	= pln.events.getElement( e );
		var marker 		= markers[ index ];
		if ( !marker || !marker.enabled ){
			try{ console.log( 'marker "' + marker.title + '" is null' ); }catch(e){}
		}
		
		marker.visible 	= checkbox.checked;
		
		if ( marker.visible ){ 
			if ( marker.kmlShape ){ 
				void clearInfoBubbles();
				void marker.kmlShape.setMap( googleMap );
			}
			if ( marker.shape ){
				void marker.shape.setMap( googleMap );
				void googleMap.panTo( marker.shape.getPosition() );
			}
		}else{
			void clearInfoBubbles();
			if ( marker.shape ) void marker.shape.setMap( null );
			if ( marker.kmlShape ) void marker.kmlShape.setMap( null );
		}
	}	
		
	void init();
}

