/* ******************************************************************************
  JavaScript Gallery Transition controller
	
 	Copyright (c) 2008 David Turner (davidturner.info)
 	Date: 30/07/2008	 
		 
****************************************************************************** */
var menuExpDuration = 0.4;
var menuExpMinHeight = 400;
var menuExpMaxHeight = 400;

document.observe('dom:loaded', function(){	
		__fixEmails();
		
		$$('body')[0].removeClassName('jsOff').addClassName('jsOn');
		

		__expandCollapseList_setup();
		
		jQuery('div.expandable').each(function(i,divExpand){
			//console.log(jQuery(divExpand).html());
			if(jQuery(divExpand).html().match('<!-- expandhere -->')){
				//console.log('found');
				var Match = jQuery(divExpand).html().match(/(.*)<!-- expandhere -->/);
				//console.info(Match);
				//console.log(Match[1].stripTags());
				//maxLength = 500;
				//console.log(Match[0]);
				//console.log(strip_tags(Match[0],'<b><strong>'));
				maxLength = strip_tags(Match[0],'<b><strong>').length+11;
			}else{
					var maxLength = 320;
			}
			jQuery(divExpand).truncate({max_length: maxLength,more:'<b>more...</b>',less:'<b>less</b>'});
		});
		//jQuery('div.htmlExpandable').truncate({max_length: 320,more:'<b>more...</b>',less:'<b>less</b>'});		
		
		jQuery("img.googleMapsContainer").googleMapsStaticConvert();
				
		jQuery('div.scrollerWidget').mouseenter(function(){
			//console.log('here');
			if(jQuery(this).find('div.controls a.playPause').hasClass('pause')){
				jQuery(this).find('div.controls a.playPause').addClass('holdPause');
			}
			jQuery(this).find('div.controls a.playPause').addClass('pause');
			//var height = jQuery(this).find('.scrollItem.selected .featTitles').outerHeight();
			jQuery(this).find('.featTitles').each(function(c,title){
				//console.log(jQuery(title));
				jQuery(title).animate({'margin-bottom':0},{duration:250});																					
			});
		});
		
		jQuery('div.scrollerWidget').mouseleave(function(){
			if(jQuery('div.scrollerWidget').find('div.controls').hasClass('autoplay') && !jQuery(this).find('div.controls a.playPause').hasClass('holdPause')){
				jQuery(this).find('div.controls a.playPause').removeClass('pause');
			}
			jQuery(this).find('div.controls a.playPause').removeClass('holdPause');
			jQuery(this).find('.featTitles').animate({'margin-bottom':-jQuery(this).outerHeight()},{duration:250});																					
		});
		//__cufonConfig();
		//text resize 
		/*
		function init()  {
			 var iBase = TextResizeDetector.addEventListener(onFontResize,null);
		}
		function onFontResize(e,args) {
			__expandCollapse_setup();
		}
		//id of element to check for and insert control
		TextResizeDetector.TARGET_ELEMENT_ID = $$('body')[0].identify();
		//function to call once TextResizeDetector has init'd
		TextResizeDetector.USER_INIT_FUNC = init;
*/
	
	
	
	// load flash movies
	/*
	if($$('div.VideoContainer').size()>0){
		
		var i = 1;
		if($$('#footerContainer a').size()>0){
			rgbCol = $$('#footerContainer a')[0].getStyle('color');
			//console.log(rgbCol);
			aryRGB = rgbCol.match(/([0-9]+), ([0-9]+), ([0-9]+)/); 
			
			var colHL="";
			
			for(var i=1;i<=3;i++){
				var hexArray = new Array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" );
				var code1 = Math.floor(aryRGB[i] / 16);
				var code2 = aryRGB[i] - code1 * 16;
				
				colHL += hexArray[code1];
				colHL += hexArray[code2];
				}
			//console.log(decToHex);
			
		}else{
			colHL ='333333';
		}
		$$('div.VideoContainer').each(function(videoCont){
			
			var vidSrc = 		videoCont.readAttribute('title').match(/\(([^)]+)\)$/)[1]; 
			var vidTitle = 	videoCont.readAttribute('title').match(/(^[^(]+) \(/)[1];
			videoCont.writeAttribute('title',vidTitle);
			
			
			if(videoCont.hasClassName('YouTube') || videoCont.hasClassName('Vimeo')){
				var swfInsert = new SWFObject(vidSrc,"player","100%","100%","9","#000000");
			}else{
				//alert('here')
				if($$('#'+videoCont.identify()+' img').size()>0){
					
					var bgImage = $$('#'+videoCont.identify()+' img')[0].readAttribute('src');
					//var obBgImage = new Element('img',{'src':bgImage,'alt':vidTitle,'class':'videoBg'});
					//console.log(bgImage.readAttribute('src'));
					videoCont.setStyle({'backgroundImage':'url('+bgImage+')'});
				}else{
					var bgImage = "";
				}
				
				var swfSRC = "/images_site/media-player/MediaPlayer.swf?mediaTitle="+vidTitle+"&mediaURL="+vidSrc+"&bgImage="+bgImage+"&fsEnable=TRUE&colHL="+colHL;
				var swfInsert = new SWFObject(swfSRC,"player_"+i,"100%","100%","9","#000000");
				swfInsert.addParam("allowfullscreen","true");
				swfInsert.addParam("wmode","transparent");
				
			}
			swfInsert.write(videoCont.identify());
			i++;
		});
	}
	*/
	/*
		var s1 = new SWFObject("/images_site/media-player/MediaPlayer.swf?mediaTitle=<?=$data['Video']['Title'];?>&mediaURL=<?="/download-file".$data['Video']['FileLocation'];?>&fsEnable=TRUE&colHL=993366","player","100%","340","9","#000000");
									s1.addParam("allowfullscreen","true");
									s1.write("VideoContainer");
								document.observe('dom:loaded', function(){	

									$('VideoContainer').removeClassName('message');
									
								});
	*/
	/*
	if($('colMain') && $('colCalendar')){
		
		if($('colMain').getHeight() > $('colCalendar').getHeight()){
			//console.log('Cal');
			$('colCalendar').setStyle({'paddingBottom':(($('colMain').getHeight()-$('colCalendar').getHeight())/10)+'em'});
		}else{
			//console.log('Main');
			$('colMain').setStyle({'paddingBottom':(($('colCalendar').getHeight()-$('colMain').getHeight()+10)/10)+'em'});
		}
		
	}
	*/
	/* Expandable lists */
	
	$$('ul.expandable').each(function(expList){
			//console.log('here');
			expLink = new Element ('a',{'class':'MoreLink','href':'#'}).update('Click for more &raquo;');
			slideContainer = new Element ('div',{'class':'expandContainer'});
			
			expList.insert({'before':expLink});
			slideContainer.insert(expList);
			expLink.insert({'after':slideContainer});
			
			Effect.BlindUp(slideContainer.identify(), { duration: 0.1 });

			Event.observe(expLink, 'click', function(event){	
				if(this.hasClassName('expanded')){
					Effect.BlindUp(this.nextSiblings()[0].identify());
					this.update('Click for more &raquo;');
					this.removeClassName('expanded')
				}else{
					Effect.BlindDown(this.nextSiblings()[0].identify());
					this.update('Hide info');
					this.addClassName('expanded')
				}
				Event.stop(event);
			});
	});
	
	
});

function __expandCollapseList_setup(){
	//alert('here');
	//var possHeight =0;
	/*
	$$('ul.expandContract').each(function(expContainer){
		//console.info(expContainer);																
		expContainer.childElements().each(function(itemHeight){
			//console.log(menuExpMinHeight+' -> '+menuExpMaxHeight);
			//console.info(itemHeight);
			//console.log(possHeight);
			if(!itemHeight.hasClassName('Selected')){
				itemHeight.addClassName('Measure');
				//alert('here');
			}
			//console.log(itemHeight.getHeight());
			var possHeight = itemHeight.getHeight()+ (itemHeight.childElements()[0].getHeight()*(itemHeight.siblings().size()));
		//	console.info(itemHeight);
			//console.log(possHeight);
			if(menuExpMaxHeight<menuExpMinHeight){
				menuExpMaxHeight= menuExpMinHeight;
			}
			if(menuExpMinHeight<possHeight){
				menuExpMinHeight=possHeight;
			}
			
			if(!itemHeight.hasClassName('Selected')){
				itemHeight.removeClassName('Measure');
			}
		});
		
		
		expContainer.hide();
		//console.log('here3');
	})
	*/
	//alert(menuExpMinHeight+' -> '+menuExpMaxHeight);
	
	var contHeight = $('mainContainer').childElements()[0].childElements()[0].getHeight();
	if(menuExpMinHeight>contHeight){
		contHeight = 	menuExpMinHeight;
	}

	if(menuExpMaxHeight<contHeight){
		contHeight = menuExpMaxHeight;
	}
	
	
	//console.log(contHeight);
	
	$$('ul.expandContract').each(function(expContainer){expContainer.show(); })
	
	$$('ul.expandContract.html li.topLevel.Selected').each(function(Header){
		Header.removeClassName('Selected');
	});
	if($$('ul.expandContract.html li.topLevel').size()>0){
		$$('ul.expandContract.html li.topLevel')[0].addClassName('Selected');
	}
	
	
	
	$$('ul.expandContract li.topLevel a.Header').each(function(headerLink){
		itemHeight = $$('ul.expandContract li.topLevel a.Header')[0].getHeight();
		if(!headerLink.ancestors()[0].hasClassName('Selected')){
			headerLink.ancestors()[0].setStyle({'height':itemHeight+'px'});
		}
		Event.observe(headerLink, 'mouseover', function(event){	
			if(!headerLink.ancestors()[0].hasClassName('Selected')){
				Event.stop(event);
				__expandCollapse_select(this.identify());
			}
		});
	});
		$$('ul.expandContract li.topLevel.Selected').each(function(expColCont){
		//console.log(expColCont.siblings().size());
		expColCont.ancestors()[0].setStyle({'height':contHeight+'px'});
		expColCont.setStyle({'height':(contHeight - expColCont.siblings().size() * itemHeight)+'px'});
	});
	//
	
}

function __expandCollapse_select(link_id){
	//console.info($(link_id));
	
	if(!$(link_id).ancestors()[1].hasClassName('Animate')){
		$(link_id).ancestors()[1].addClassName('Animate');
		
		$$('ul.expandContract').each(function(expContainer){
		
			expContainer.childElements().each(function(itemHeight){
				//itemHeight.addClassName('Selected')
				//console.info(itemHeight);
				
				
				var possHeight = itemHeight.getHeight()+ (itemHeight.childElements()[0].getHeight()*itemHeight.siblings().size());
				
				if(menuExpMaxHeight<menuExpMinHeight){
					menuExpMaxHeight= menuExpMinHeight;
				}
			
				if(menuExpMinHeight<possHeight){
					menuExpMinHeight=possHeight;
				}
				
				//if(!itemHeight.hasClassName('Selected')){
						//itemHeight.hide();
				//}
			});
		})
		
	
		var contHeight = $('mainContainer').childElements()[0].childElements()[0].getHeight()-10;
		
		if(menuExpMaxHeight<contHeight){
			contHeight = 	menuExpMinHeight;
		}
		
			
		if(menuExpMaxHeight<contHeight){
			contHeight = menuExpMaxHeight;
		}
		
		$(link_id).ancestors()[0].siblings().each(function(expColCont){ 
			if(expColCont.hasClassName('Selected')){
				expColCont.addClassName('hide');
				
				new Effect.Parallel([
					new Effect.Morph($(link_id).ancestors()[0].identify(),{
						style:'height:'+(contHeight-$(link_id).ancestors()[0].siblings().size() * itemHeight)+'px',
						beforeStart: function(){ 
							$(link_id).ancestors()[0].addClassName('Selected'); 
						}
						
					}),
					new Effect.Morph(expColCont.identify(),{
						style:'height:'+itemHeight+'px',
						afterFinish:	function(){ 
							
							
						}
					})
					
					//new Effect.Opacity(element, { sync: true, from: 1, to: 0 }) 
				], { 
					duration: menuExpDuration,
					afterFinish: function(){ 
						
						expColCont.removeClassName('hide');	
						expColCont.removeClassName('Selected'); 
						$(link_id).ancestors()[1].removeClassName('Animate'); 
						
					}
				});

				
				
				
				//expColCont.setStyle({'height':itemHeight+'px'});
			}
		});
		
		//$(link_id).ancestors()[0].setStyle({'height':(contHeight- $(link_id).ancestors()[0].siblings().size() * itemHeight)+'px'});
		
		//$(link_id).ancestors()[0].slideDown();
	}
}

function __fixEmails(){

	var excludeThese = ['cufon'];

	// loop over all elements on page, replacing :-) with :wink: for anything
	// that is *not* a tag name in the excludeThese array
	//console.log('yup');
	jQuery('#mainContainer p').each(function() {
		//console.log('yup');
		jQuery(this).html(jQuery(this).html().replace(/(\[at\])/g,'@'));
	});



	//); //.replace('[at]','&');
	//console.info($$('body')[0].innerHTML.match(/(\[at\])/));
}

