
function attach_images() {
       
    	jQuery('#project_images li img').click(function() {
    			// hide clicked image
    			jQuery(this).parent().attr('class', 'hidden');
    			clicked_id = jQuery(this).parent().attr('id');
    			// un-hide current image
    			image_index_id = jQuery('#main_project_image').attr('rel');
    			jQuery("#" + image_index_id).attr('class', ''); 			
				
				// set new image src
				jQuery('#main_project_image').attr('src', jQuery(this).attr('rel'));
				// set image index in rel
				jQuery('#main_project_image').attr('rel', clicked_id);
		    }
    	);
		jQuery("a[href^='http:']").not("[href*='edge-architecture.co.uk']").attr('target','_blank');
}

initialiseScrollPane = function(item, width)
{
    jQuery(item).css(
    	{'height': '100px',
    	 'width': width,
    	 'overflow' : 'auto',
    	 'margin-top' : '0px'
    	}
    )
	jQuery(item).jScrollPane(
        {
         showArrows:true, 
         scrollbarWidth: 12, 
         scrollbarOnLeft:true
        }
    );
}


function mark_selected(item) {
    existing_text = item.parent().attr('class');    
    item.parent().attr('class', existing_text + " selected");

    // remove any selected classes from sibling nodes
    item.parent().siblings().attr('class', '');
}

max_nav_level = 3;

MAX_ITEMS = 6;
                         
function attach_nav(start_nav) {
    
    jQuery("#" + start_nav + ' a').click(
        function() {
			// special case for home on main nav.
			if (jQuery(this).attr("class").match("home")) {
				return true;
			}
            leaf_node = jQuery(this).attr('class').match("leaf") == "leaf";
			
            mark_selected(jQuery(this));
            if (leaf_node) {
                // load content
                 jQuery.ajax({
                        url : jQuery(this).attr('href') + '?ajax=1',
                        cache: false,    
                        success : function(html) {
                                jQuery('#content').html(html);
                                attach_images();
                        }
                });
				// change address bar
				current_location = jQuery(this).attr('href');
                window.location.hash = current_location;
				
                // clear next level nav
                current_lev_str = jQuery(this).parent().parent().parent().attr('id');
                next_lev = parseInt(current_lev_str.replace('nav_bar_', '')) + 1; 
                            
                for (lev = next_lev; lev < max_nav_level + 1; lev++)
                {
                    jQuery('#nav_bar_' + lev).remove();
                }
                            
		
            } else {
                // load nav
                var noslash = this.pathname.indexOf('/') === 0 ? this.pathname.slice(1) : this.pathname;
                jQuery.ajax({
                  url: "/nav/" + noslash,
                  success: function(data) {
                       next_nav = jQuery(data).val('div.nav_bar').attr('id');
                       nav_bar_level = next_nav.replace('nav_bar_', '');
                       // remove all menus right of current
                       for (lev = parseInt(nav_bar_level); lev < max_nav_level + 1; lev++)
                       {                       		
                            jQuery('#nav_bar_' + lev).remove();
                       }
                       jQuery('#nav_bars').append(data);
                       // attach nav load to items if it has descendants
                       attach_nav(next_nav);
                       
                       // put scroll pane on items bigger than MAX_ITEMS
                       jQuery(".nav_bar ul").each(function(){
                       		if (jQuery(this).children().size() > MAX_ITEMS) { 
                                if (nav_bar_level == 3){
                                    width = '400px';
                                } else {
                                    width = '200px';
                                }
                       			initialiseScrollPane(this, width);
                       		}
                       	}
                       )
					   next_level = parseInt(nav_bar_level);
					   next_nav_bar = jQuery("#nav_bar_" + next_level);
					   next_nav_bar.find('a').eq(0).trigger('click');

                    }
                  }
                );
                
            }
            return false;
        }
    );              
}

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

jQuery(document).ready(function(){ 

    
    // check if we have a hash in the url (from a bookmarked page)
    // if so redirect to path after hash
    hash = window.location.hash;
    if (hash) {
    	path = hash.replace(/^.*#/,'');
    	window.location.pathname = path;
    	window.location.hash = '';
    }
    // center page
    jQuery("#page").vAlign();
	jQuery(window).bind('resize', function() {
		jQuery("#page").vAlign();
	});
	
    attach_nav("nav_bar_0");

    // un-hide images
    jQuery('#slideshow img.hidden').attr('class' ,'');
    
    // start slideshow 
     jQuery('#slideshow').cycle({ 
        timeout:  5000
    }); 

	jQuery("a[href^='http:']").not("[href*='edge-architecture.co.uk']").attr('target','_blank');

});





