var loaded = false;
/* start of analytics tracking block */
 var _gaq = _gaq || [];
   _gaq.push(['_setAccount', 'UA-27525465-1']);
   _gaq.push(['_trackPageview']);
 
   _gaq.push(['b._setAccount', 'UA-21615002-1']);
   _gaq.push(['b._trackPageview']);
 
 
   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

   function logGATweetEvent(url) 	
   {
	   //if it errored once don't keep trying it
	   		try {
				//it errored so log to the output console
				_gaq.push(['_trackSocial', 'twitter', 'tweet', url]);
				_gaq.push(['b._trackSocial', 'twitter', 'tweet', url]);
			}
			catch(e) {
				if(arguments.callee.analyticsTTErrorLogged == undefined) {
					logError('Analytics Twitter Logging errored - ' . e);
					arguments.callee.analyticsTTErrorLogged = true;
				}
			}
	}
       
   
   function logGAFBShareEvent(url) 	
   {
	   //if it errored once don't keep trying it
			try {
				//it errored so log to the output console
				_gaq.push(['_trackSocial', 'facebook', 'comment', url]);
				_gaq.push(['b._trackSocial', 'facebook', 'comment', url]);
			}
			catch(e) {
				//it errored so log to the output console
				if(arguments.callee.analyticsFBErrorLogged == undefined) {
					logError('Analytics Facebook Logging errored - ' . e);
					arguments.callee.analyticsFBErrorLogged = true;
				}
			}
    }   

  
   function logGAPageViewEvent(url) 	
   {
	   //if it errored once don't keep trying it
			try {
				//it errored so log to the output console
				_gaq.push(['_trackPageview', url]);
				_gaq.push(['b._trackPageview', url]);				
			}
			catch(e) {
				if(arguments.callee.analyticsErrorLogged == undefined) {				
					//it errored so log to the output console
					logError('Analytics Logging errored - ' . e);
					arguments.callee.analyticsErrorLogged = true;
				}
			}
	}
    

    function logError(msg) 
    {
    	if (window.console && window.console.log) {
	      window.console.log(msg);
    	}
    	else if (window.opera && window.opera.postError) {
	      window.opera.postError(msg);
		}
    }

/* end of analytics tracking block */
   
$(document).ready(function() { 

  // put the initial set of click handers in for the site nav.
  set_clickhandlers();	

  function dynamic_link_loader(caller)
  {
	if(loaded)
	{
		var target_href = caller.href;

		logGAPageViewEvent(target_href);

		//deselect the top level nav
		clear_selected_toplevel_nav();
		
		if($(caller).hasClass('sub_menu_trigger'))
		{
			$(caller.parentNode).addClass("selected");
			$('ul.archive_nav').fadeOut(100);
		}
		else
		{
			var $parentLI = caller.parentNode.parentNode.parentNode;
			
			$($parentLI).addClass("selected");
			$('ul.archive_nav').fadeOut(100);
		}

		// unhook teh previous set of click event handlers
		//clear_clickhandlers();
		
		// load the page content
		$('div.content_holder').load(caller.href + ' ul.content', function() { 
			  if ($('div.jsblock').length == 1)
			  {
				$('div.jsblock').remove();
			  }
 			  $('div.content_holder').before("<div class='jsblock'><script src='http://platform.twitter.com/widgets.js' type='text/javascript'></script><script async='' type='text/javascript' src='http://mebanddevelopment.disqus.com/count.js'></script></div>");			

	  		  // create the page nav div if it doesn't exist
  			  if ($('div.page_nav').length == 0) { $('ul.content').before("<div class='page_nav cufon_me'></div>"); }
			  // load the page nav content
			  $('div.page_nav').load(target_href + ' div.page_nav a', function() {

		  		  // create the page nav div if it doesn't exist
  				  if ($('div.page_nav_bottom').length == 0) { $('ul.content').after("<div class='page_nav_bottom cufon_me'></div>"); }
				  // load the page nav content

				  $('div.page_nav_bottom').load(target_href + ' div.page_nav_bottom a', function() {

					  //set_clickhandlers();
					  $("iframe.disqus-holder").hide();
				  });
			  });
	          set_lightbox_trigger();			  
		  }); 	

	}
	return false;
  }
	

  function media_link_loader(caller)
  {
	if(loaded)
	{
		var target_href = caller.href;

		logGAPageViewEvent(target_href);

		//deselect the top level nav
		clear_selected_toplevel_nav();

		if($(caller).hasClass('media_menu_trigger'))
		{
			$(caller.parentNode).addClass("selected");
			$('ul.media_nav').fadeOut(100);
		}
		else
		{
			var $parentLI = caller.parentNode.parentNode.parentNode;
			
			$($parentLI).addClass("selected");
			$('ul.media_nav').fadeOut(100);
		}

		// unhook teh previous set of click event handlers
		//clear_clickhandlers();

		// load the page content
		var query = caller.href + ' div#content_wrapper';
		$('div.content_holder').load(query, function() { 
			  if ($('div.jsblock').length == 1)
			  {
				$('div.jsblock').remove();
			  }
			  $('div.content_holder').before("<div class='jsblock'><script src='http://platform.twitter.com/widgets.js' type='text/javascript'></script></div>");			

	          set_lightbox_trigger();
			  
		}); 	

	}
	return false;
  }

  function disqus_button_click(caller)
  {
	if($(caller).attr("src") == 'uploads/meband/images/comment-like-tweet_active.png')
	{
	  	$(caller).attr('src', 'uploads/meband/images/comment-like-tweet_hide.png');
	  	$('iframe#holder'+$(caller).attr("id")).show();
	    resizeIframe($('iframe#holder'+$(caller).attr("id")));
		  
	}
        else 
	{
		$(caller).attr('src','uploads/meband/images/comment-like-tweet_active.png');
		$('iframe#holder'+$(caller).attr("id")).hide();
	}
  }

  function resizeIframe(iframe_id)
  {
	$(iframe_id).height($(iframe_id).contents().height());
  }
 
  function set_clickhandlers()
  {
	  $(".main_nav a.dynamic_load").live('click',function() { return dynamic_link_loader(this); });
	  $(".main_nav a.media_load").live('click', function() { return media_link_loader(this); });
	  $("a.disqus-trigger img").live('click', function() { disqus_button_click(this); });
	  
	  /**
	   * Lightbox for blog image posts
	   */
   	 
	 if($('a.lightbox').length > 0)
	 {	
		$('a.lightbox').lightBox({fixedNavigation:true});
	 }
	 
	 /**
	  * ajax the search form
	  */
	 
	 $("#search_form").submit(function(event) {

		    /* stop form from submitting normally */
		    event.preventDefault(); 
		        
		    /* get some values from elements on the page: */
		    var $form = $( this ),
		        term = $form.find( 'input[name="search_term"]' ).val(),
		        url = $form.attr( 'action' );

		    logGAPageViewEvent(url + '&term=[' + term + ']');

		    /* Send the data using post and put the results in a div */
		    $.post( url, { search_term: term },
		      function( data ) 
		      {
		          var content = $( data ).find( 'div.content_holder' );
		          $('div.content_holder').empty().append(content);
		          set_lightbox_trigger();
		      }
		    );
		  });
	 
	 
	  /**
	  * .load call for calling in seperate feeds
	  */
	  $('.main_nav li').hover(
	        	function () {
		            //show its submenu
	        	    $('ul', this).fadeIn(100);
			    $('ul', this).css('z-index', '1000');
		        },
	        	function () {
		            //hide its submenu
	        	    $('ul', this).fadeOut(100);        
		       }
	  );
	 
  }
  
  function clear_clickhandlers()
  {
	  $("a.dynamic_load").unbind('click');
	  $("a.media_load").unbind('click');
	  $("a.disqus-trigger img").unbind('click');
	  $('.main_nav li').unbind('hover');
  }
  
  function clear_selected_toplevel_nav()
  {
	  $("ul.main_nav li.selected").removeClass("selected");
  }

  function set_lightbox_trigger()
  {
	 if($('a.lightbox').length > 0)
	 {	
		$('a.lightbox').lightBox({fixedNavigation:true});
	 }
  }
  
  loaded = true;

  //give the shockwave players an arbitrary z-index 	
  $("embed").css("z-index", "100");

//call the image preloader
  try   {  $.preloadCssImages(); }
  catch (e) {}
});

