// wait for the DOM to be loaded
$(document).ready(function () {

/*
    $('#nav_cat_archive>li>ul').hide();
    $('#nav_cat_archive>li').click(function () {
        $heading = $(this);
        if ($('ul:visible', $heading).size() == 0) {
            $heading.siblings().find('ul:visible').slideUp(500);
            $('ul', $heading).slideDown(500);
            return false;
        }
    });
*/
	
	//Login Dropdown
	$('a#login-drop').click(function(){
		$('div#login-box').slideToggle('250');
		return false;
	});
	
	// Add toggle buttons
	$('#nav_cat_archive li:has(ul)').each(function(){
		$(this).prepend('<a class="toggler">+</a>');
	});

    // Left Nav Toggle
    $('#nav_cat_archive .toggler').bind('click', function(){
    	$(this).siblings('ul').slideToggle('med');
    	$(this).parent().siblings().find('ul').slideUp().siblings('.toggler').text('+'); // slides up sub level
    	
    	if($(this).text() == '+') {
    		$(this).text('-');
    	} else {
    		$(this).text('+');
    	}
    });
    
    // Show current menu item
    var pathname = window.location.pathname;
    var urlSegments = pathname.split('/');
    var noUrlSegments = urlSegments.length;
	// Do not run if homepage or search page
    if(urlSegments.length > 2 && urlSegments[2] != 'search') {
    
    	var itemToShow;
    	
    	// Which item to show
    	if(urlSegments[3] == 'view') {
    		itemToShow = $('#' + urlSegments[noUrlSegments-2]);
    	} else {
    		itemToShow =$('#' + urlSegments[noUrlSegments-1]);
    	}
    	// Show the item
    	$(itemToShow).addClass('current').siblings('.toggler').text('-').end()
    									.parent().parent().show()
    									.siblings('.toggler').text('-')
    									.parent().parent().show()
    									.siblings('.toggler').text('-');
    	// Show the siblings UL if it has any
    	$(itemToShow).siblings('ul').show().siblings();
    }
    
    // IE PNG Fix
    DD_belatedPNG.fix('#ccwireLogo, #computacenterLogo, #comment-toggle, #cta-contact, #cta-quote, #cta-meeting, .collateral-wrapper .toggle, .collateral-img-download');
 
    
    // Slideshow
    $('.slideshow').cycle({
        fx: 'scrollDown',
        speed: 8000,
        timeout: 9000,
        pause: 4
    });
    
    $('div#cta-contact-form, div#cta-meeting-form, div#cta-quote-form, div#comment-entry').hide();
    
    $('a#cta-contact').click(function () {
        $('#cta-meeting-form').slideUp("slow");
        $('#cta-quote-form').slideUp("slow");
        $('#cta-contact-form').delay(800).slideToggle("slow");
    });
    
    $('a#cta-quote').click(function () {
        $('#cta-contact-form').slideUp("slow");
        $('#cta-meeting-form').slideUp("slow");
        $('#cta-quote-form').delay(800).slideToggle("slow");
    });
    
    $('a#cta-meeting').click(function () {
        $('#cta-contact-form').slideUp("slow");
        $('#cta-quote-form').slideUp("slow");
        $('#cta-meeting-form').delay(800).slideToggle("slow");
    });
    
    $('.collateral-wrapper .toggle').bind('click', function () {
        $(this).toggleClass('active').parent().siblings('.collateral-box').slideToggle('slow');
        return false;
    });
    
    $('div.scrollable').scrollable({
        vertical: true,
        size: 4
    });
    
    $('.collateral-wrapper').each(function () {
        var count = $(this).find('.collateral-item a.collateral-download').length;
        $(this).find('h3 a').after('<span class="collateral-count">(' + count + ' items)</span>');
        if (count == 0) {
            $(this).find('h3 a').unbind('click').css({
                'opacity': '0.4',
                'cursor': 'default'
            });
        }
    });
    
    $('a#comment-toggle').click(function () {
    	$(this).toggleClass('active');
    	$('div#comment-entry').slideToggle('slow');
    	return false;
    });
    
  
    
    // Facebox
	$('a[rel*=facebox]').facebox();
});