$(document).ready(function(){
	$("div#menu li").hover(
		function(){
			//$("ul", this).slideDown(120);
			$("ul", this).show();
		}, 
		function(){
			//$("ul", this).slideUp(120);
			$("ul", this).hide();
		}
	);
	
	Cufon.replace("h1")("h2")("h3")("#internal_ref a .center")('.c2a_text');
	
	$(".popup").fancybox({
		'zoomOpacity' : true,
		'zoomSpeedIn' : 300,
		'zoomSpeedOut' : 300
	});
	
	if($('#referenties')) $('#referenties').innerfade({
		speed: 'slow',
		timeout: 5000,
		type: 'sequence',
		containerheight: '162px'
		
		//slide_nav_id: 'slide_nav',
		//slide_timer_on: 'yes',
		//slide_ui_parent: 'referenties'
		
		
		/*slide_timer_on: 	'yes',
		slide_ui_parent: 	'referenties',
		slide_ui_text:		'portfolio-desc',
		pause_button_id: 	'pause_button',
		slide_nav_id:		'slide_nav'*/
	});
	
	if($('#testimonials')) $('#testimonials').innerfade({
		speed: 'slow',
		timeout: 8000,
		type: 'random',
		containerheight: '210px'
	});
        
        $('.call2action .button_left').click(function(){
            c2a_index       = $('.call2action ol .active').index() + 1;
            c2a_amount      = $('.call2action ol li').length;
            
            if (c2a_index == 1) {
                $('.call2action ol li:nth-child(' + c2a_amount +')').click();
            } else {
                new_index = c2a_index - 1;
                $('.call2action ol li:nth-child(' + new_index +')').click();
            }
                
        });
        
        $('.call2action .button_right').click(function(){
            c2a_index       = $('.call2action ol .active').index() + 1;
            c2a_amount      = $('.call2action ol li').length;
            
            if (c2a_index == c2a_amount) {
                $('.call2action ol li:nth-child(' + 1 +')').click();
            } else {
                new_index = c2a_index + 1;
                $('.call2action ol li:nth-child(' + new_index +')').click();
            }
                
        });
        
        
        $('.call2action ol li').click(function(){
            
            if (!$(this).hasClass('first') && !$(this).hasClass('last')) {
                border_width = $(this).width();
                
                $(this).removeAttr('style');
                
                $(this).css('border-left','1px solid #A8A8A8').css('border-right', '1px solid #A8A8A8;').css('width',border_width +'px');
                
                $(this).prev().attr('style', $(this).prev().attr('style') + ';border-right:none;');
                $(this).next().attr('style', $(this).next().attr('style') + ';border-left:none;');
                
//                $(this).css('width',border_width +'px');
            }else if( $(this).hasClass('first') ){
                border_width = $(this).css('width');
                $(this).css('border-right', '1px solid #A8A8A8').css('width',border_width +'px');
                $(this).next().attr('style', $(this).next().attr('style') + ';border-left:none;');
            } else if ( $(this).hasClass('last') ){
                border_width = $(this).css('width');
                $(this).prev().attr('style', $(this).prev().attr('style') + ';border-right:none;');
                $(this).css('border-left', '1px solid #A8A8A8').css('width',border_width +'px');
            } 
            
            $('.call2action .active').removeClass('active');
            $(this).addClass('active');
            index = $('.call2action ol li').index(this) + 1;
            tabname = 'tab' + index;
            $('.call2action > div').each(function(){
                $(this).addClass('invisible')
            });
            $('.'+tabname).removeClass('invisible');
            
        });
        
        
        //Gather information to build the tabs
        amount_tabs = $('.call2action ol li').length;
        size        = $('.call2action').width() / amount_tabs;
        size        = Math.floor(size);
        border_size = amount_tabs + 1;
        new_size    = size * amount_tabs;
        size        = size - border_size;
        content_width= $('.call2action .content').width();
        
        //set the new size of the whole c2a
        $('.call2action').width(new_size);
        
        //set the size of the tab items on top
        $('.call2action ol li').attr('style','width:' + size +'px');
        
        //calculate the new size of the content field
        total = 0;
        $('.call2action ol li').each(function(){
           total = total + $(this).width();
        });
        total = total - 22;
        
        //set the new content size 
        $('.call2action .content').attr('style','width:' + total +'px');
        
        //calculate the new place for the right button
        right_button    = content_width - total - 29;
        $('.call2action .button_right').css('right', right_button +'px');
        
        $('.call2action .content').each(function(){
            margin_bottom = 0;
            $(this).children('.tab').removeClass('invisible');
            margin_bottom = $(this).children('.tab').first().height() - 135;
            if ($(this).hasClass('invisible')){
                $(this).children('.tab').addClass('invisilbe');
            }
            
//            alert($(this).children('.tab').height());
            if ( margin_bottom > 20 ) {
                $(this).children('.c2a_images').css('margin-bottom',margin_bottom);
            }
        })
        
//        link_width = 0
//        $('.call2action_link').each(function(){
//            $(this).children().each(function(){
//                link_width = link_width + $(this).width() + 4;
//            });
//            if (link_width < 250) {
//                link_width = 250;
//            }
//            $(this).width(link_width);
//        });
        
//        margin_right    = total - $('.call2action_link').width() - 205;
//        $('.call2action_link').attr('style', $('.call2action_link').attr('style') + 'margin-right:'+ margin_right + 'px');
        
});
