﻿/* DEVELOPEMENT BY DAMIANMORTEN.COM */

var core = {
	 
	 getSection : function() {
		var url = document.URL;
		start = url.search(/section-/i); 
		if(start == -1){return 'roaming';}
		var bld = url.substring(start);  
		var end = bld.search(/\//);
		bld = bld.substring(0, end);
		return bld;
	 },
	 
	 createAccordians : function() {
	
		$('ul.dui-accordian').each(function(u,y){
		  		 var titleHeight= 25;
		 		 var li = $(y).find('li');
				 var openli = null;
				 $.each(li, function(i,e){ 	
								var $this = $(e);
								var origcontents = $this.children().remove();		   
								var header = $('<div class="dui-accordian-item-title" style="height:'+(titleHeight-3)+'px; padding-top:2px; cursor:pointer; text-align:center; width:100%;"></div>');
								var wrapper = $('<div class="dui-accordian-inner-wrapper"></div>');
								wrapper.html(header).append(origcontents);
								$this.css({height:titleHeight, overflow:'hidden'}).prepend(wrapper); 
								var thisHeight = wrapper.height();
								var title = String($this.attr('title'));
								$(header).css({color:'grey'}).html(title);
								$this.click(function(){
													    if($this.is(openli)){return};
													    if(openli)openli.animate({height:titleHeight}, 400); 
													    $this.animate({height:thisHeight}, 400);
														openli = $this;
														});	
									 
										   
						});//each li
					});//each ul
	
	},
	 
	 openLoginPanel : function(){
		 
		 $('.Login-Box').animate({height: 75}, 500);
		 
	},
	
	openSearchPanel : function() {
	
		 $('.Search-Box').animate({height: 50}, 500);
	
	},
	
	closePanels : function() {
		
		$('.Search-Box').css({height: 0});
		$('.Login-Box').css({height: 0});
		
	},
	
	
	getBreadCrumbs: function() {
		 
		var url = document.URL; 
		start = url.search(/section-/i);
		var title = $('title:first').html();
		title = String(title).replace('| Mercer Regional Chamber of Commerce', '').replace(/^\s+|\s+$/g, ''); 
		if(start == -1){return title;} 
		var bld = url.substring(start); 
		bld = bld.split('/');
		bld.pop();  
		bld.push(title);	
		var comp = "",
			homelink = 'HTTP://MERCERCHAMBER.ORG/CWT/EXTERNAL/WCPAGES/LANDING_PAGE.ASPX',
		    baseurl = 'HTTP://MERCERCHAMBER.ORG/CWT/EXTERNAL/WCPAGES/';
		if(bld.length<2)return' ';
		var corrections =  {
							'Newjersey'   : 'New Jersey',
							
							'Central Nj'  : 'Central NJ',
							
							'Memberships' :  'Overview',
							
					'Resources Services'  : 'Resources &amp; Services' 
							
							};

		 							
		$.each(bld, function(i, e){  
							 var output = String(e).replace(/section-/i,'').replace('-',' ');  
							 output = output.toLowerCase();
							 output = output.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );   
							 for ( n in corrections ) { 
								 
								if(output.search(n)!=-1){
									output = output.replace(n,corrections[n]);
								 }
							
							 }
							 var link  = baseurl + String(bld.slice(0, (i+1))).replace(',','/'); 
							 if (i==0) {
							 comp = '<a href="'+homelink+'">Home</a>  >>  '+'<a href="'+link+'">'+output+'</a>'; 
							 }else if( i == (bld.length-2)) {
							 comp = comp+'  >>  <a>'+output+'</a>';
							 }else if( i == (bld.length-1)) {
							 comp = comp+'  >>  <a>'+output+'</a>'; 
							 }else{
							 comp = comp+'  >>  <a href="'+link+'">'+output+'</a>';
							 }
							 });
		
		return comp;
		
	},
	
	
	createAreAnim : function(options){
		var defaults = {
			
			container : $('.AREAnimBox:first'),   
			
			left : 150
		} 
		
		options = $.extend(defaults, options); 
		
		var container    = options.container.css({position:'relative'}),
			containerpos = container.position(),
			images 	     = $('img', container),
			headers      = $('h2', container),
			textConts    = $('div', container),
			fired	     = false;

					
			$.each(headers, function(b,x){
										$(this).hide();
										 });
					
			$.each(textConts, function(b,x){
									var $this = $(this); 
								    $this.hide();
												  
								      });

			$.each(images, function(b,x){ 
							var $this = $(x);
							var pos = $(this).position();
							$this.data('pos', pos); 
							$this.css({left:options.left, opacity:0 });

			});
	
		var fireLast = function(){$(textConts[3]).contents().show(); $(textConts[3]).delay(200).fadeIn();};
	
		var animation = function() { 
	 
					$.each(images, function(b,x){  
											var pos =  $(x).data('pos');
											$(x).delay(500+(b*600)).animate({opacity:1}, function(){
																						if(b==2){ fireLast(); }	  
																						$(this).delay(4600).animate({left:pos.left},1000);
																						$(headers[b]).delay(5200).fadeIn(); 
																						$(textConts[b]).delay(5200).fadeIn();
																						
																						});
					});
		
		};//var animation
		
		if(window.navigator.appName == "Microsoft Internet Explorer"){fired = true; animation();} else {
	
			$(window).bind("load",function(){ 
				if( $(window).height() > 800 || window.navigator.appName == "Microsoft Internet Explorer"){fired = true; animation();} else { 
					$(document).bind('scroll',function (i, e) {  
						if(fired){$(document).unbind('scroll'); return;}
						if(($('body').scrollTop()+$(window).height()) > 450){ fired = true; animation();}
					});	
				}//else
			});
		
		}
			
	},
	
	
	insertCustomImages	: 	function(){
			
			var images 		= slideshowurls,
				container	= $('.Slideshow:first').empty();
			$.each(images, function(i,e){
									var img = new Image();
									img.src = e;
									$(img).css({opacity:0});
									container.prepend(img);
									$(img).fadeIn();
									
									});
			if(images.length > 1){
					container.easySlideShow();
				}
			
	
	},
	
	createCollapsingSidebar : function(){
	 
		var sbpanels = $('.Nav-Menu-Drop-Down');
	    
		
	   sbpanels.each(function(i,e){ 
					     
	   		var dropdown = $(this).css({position:'relative'}),
				 contents = $('a', dropdown),
				 $height = 0,
				 $parent = dropdown.parent(),
				 $link = $('a', $parent),
				 $positioner = $('<div class="positioner" style="position:absolute; bottom:0;"></div>');
 			
			$.each(contents, function(i,e){
									  $height = $height + $(e).outerHeight() + 1;
									  });
			
			contents.prependTo($positioner);
			dropdown.prepend($positioner).data({listHeight: $height});
			
 
			$link.click(function(){		   
			  if(dropdown.hasClass('openQuickNavPanel')){return;} 
				 
				 $(this).css({backgroundPosition: '5px 15px'});
				 
				 $('.openQuickNavPanel').animate({height:0}).removeClass('openQuickNavPanel').parent().find('a').css({backgroundPosition: '5px -8px'});	 
				 
				 dropdown.animate({height: $height}).addClass('openQuickNavPanel');		 
				 
			 });  
			   
	    }); 
		 $('.openQuickNavPanel').css( {height: $('.openQuickNavPanel').data('listHeight') });
		
		
	},
	
	init : function (){
			
	   if( typeof(StartClass) != 'undefined' )    {  StartClass(); }
	   if( typeof(openSBNavPanel) != 'undefined' ){  openSBNavPanel(); }
	   if( typeof(updateHeader) != 'undefined' )  {  updateHeader(); } 
	   if( typeof(slideshowurls) != 'undefined' ) { this.insertCustomImages(); }
	   if( $('ul.dui-accordian').length )         { this.createAccordians(); } 
  	   if( $('.AREAnimBox').length )			  { this.createAreAnim({left:125}); } 
	   if( $('.AREAnimBoxSm').length )            { this.createAreAnim({container:$('.AREAnimBoxSm'), left:-0}); } 
 	   if($('.Nav-Menu-Drop-Down').length)		  { this.createCollapsingSidebar(); }	   
	   if($('#upcomingEvents').length)            { $('#upcomingEvents').miniFeed('/cwt/external/wcpages/wcevents/eventsstartpage.aspx?RSS=True&oe=True', { limit: 6, getItemDate:false, nextLinkText:'Attend', getFeedTitle:false, getItemSummary: false}); }
		   

		
 
		  
		//DROPDOWNS 
		
		if(navigator.appName != 'Microsoft Internet Explorer') {
		
		 		$('.Header-Navigation>li').each(function(i,e){
											   var $this = $(e), 
											   	   $dropdown = $('.Header-Drop-Down-Menu-List', $this),
											   	   $height = $dropdown.height(),
												   $children = $('a',$dropdown).css({opacity:0});
												   
											   $this.hover(function(){
															$children.stop().css({opacity:0}).each(function(i,e){$(e).queue('fx2', true).delay(i*50).animate({opacity:1});});		
																	},function(){
															$children.stop().css({opacity:0});	 	
																	});
											   
											   });
			
		}//if ie
			
		
		$('.intRightCollumn').prepend('<div class="Ansestor-Menu" style=" float:right; font:11px Arial; color:grey;">'+core.getBreadCrumbs()+'</div>');
			
			
	 }//init
	
}
	
$(function(){ core.init(); });
