jQuery(document).ready(function() {


	//show the #content on the client page
	jQuery('.slug-clients #content').css({visibility: 'visible'});
	
	//Justify the menu
	var total_width = 0;
	jQuery('.secondary-menu li').each(function(){
		total_width = total_width + jQuery(this).width();
	});
	var extra_width = (890 - total_width - 1)/(jQuery('.secondary-menu li').length - 1 );
	jQuery('.secondary-menu li').css('paddingRight', extra_width);
	jQuery('.secondary-menu li:last').css('paddingRight', '0');
	
	//Show Hide posts
	var the_eq = jQuery('.blog .keep-reading').index();

	jQuery('.blog .entry-content').children().each(function(){
		if (jQuery(this).index() > the_eq){
			jQuery(this).hide();
		}
	});
	
	jQuery('.blog .hentry').each(function(){
		
		var the_id = jQuery(this).attr('id'); //we get the ID of the post first so the click bind if per post
		var the_target = '#' + the_id + ' .entry-content';
		jQuery(the_target + ' .keep-reading span').click(function(){
			jQuery(the_target).children().each(function(){
				if (jQuery(this).index() > the_eq){
					jQuery(this).show();
				}
			});
			jQuery(this).parent().hide();
			
			jQuery(the_target+ ' .done-reading').show().click(function(){
				jQuery(the_target).children().each(function(){
				if (jQuery(this).index() > the_eq){
					jQuery(this).hide();
				}
				jQuery(the_target + ' .keep-reading').show();
			});
			});
		});

	});

	//Remove the title atribute for the menu. It just get's in the way.
	jQuery("li.menu-item a").each(function(){
		jQuery(this).removeAttr('title');
	})                

	jQuery("li.page_item a").each(function(){
		jQuery(this).removeAttr('title');
	})
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////// cycle
	
	//hide the #clients ul on first load
	jQuery('#clients, #client-nav').hide();
	
	//on click show #clients and #client-nav and calculate the height of the #clients div
	jQuery('#client-list li').click(function(){
			
			the_eq = jQuery(this).prevAll().length;
			the_eq_selector = '#clients>li:eq('+the_eq+')';
			
			var $ht = jQuery(the_eq_selector + ' .screen-shots li:eq(0) img').attr('height');
			//set the container's height to that of the current slide
			if($ht != 0) {
				jQuery('#clients').height( $ht+70);
			} else {
				
				var src = jQuery(the_eq_selector + ' .screen-shots li:eq(0) img').attr('src');							
				var img = new Image();
				img.src = src;
				var $ht2 = img.height;
				jQuery('#clients').height( $ht2+70);
			}
			jQuery('#clients, #client-nav').show('fast');
	});
	
	// hide #clients and #client-nav on image click
	jQuery('#see-clients').click(function(){
		jQuery('#clients, #client-nav').hide('fast');
		jQuery('#client-list li').each(function(){
				jQuery(this).removeClass('selected');
		});				
	});
	
	//initiate the cycle plugin on the clients page
	var clients = jQuery('#clients').cycle({ 
		fx:     'scrollHorz', 
		speed:  'fast', 
		timeout: 0, 
		containerResize: 1, 
		next: '#next-client',
		prev: '#prev-client',
		prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement){ 
			jQuery('#client-list li').each(function(){
				jQuery(this).removeClass('selected');
			});	
			jQuery('#client-list li:eq('+zeroBasedSlideIndex+')').addClass('selected');			
		},
		after:function(){
			var setheight = jQuery('#clients li:visible .screen-shots li:eq(0) img').attr('height');			
			if(setheight != 0) {
				if(jQuery('#clients').css('display') != 'none'){
					jQuery('#clients').animate({height: setheight+70});
				}
			}
			else{
				var src = jQuery('#clients li:visible .screen-shots li:eq(0) img').attr('src');							
				var img = new Image();
				img.src = src;
				var setheight2 = img.height;
				if(jQuery('#clients').css('display') != 'none'){
					jQuery('#clients').animate({height: setheight2+70});
				}
			}
		}
	});
	
	clients.children().each(function(i) {
		jQuery('#client-list li:eq('+i+')').click(function() { 
            // cycle to the corresponding slide
            jQuery('html, body').animate({scrollTop:251}, 'fast');
			jQuery('#client-list li').each(function(){
				jQuery(this).removeClass('selected');
			});
			jQuery(this).addClass('selected');
			clients.cycle(i);
            return false; 
        }); 
	});

	//initialize the printscreen slideshows
	jQuery('#clients>li').each(function(){
		
		var the_id = jQuery(this).attr('id');
		
		jQuery(this).children('.screen-shots').addClass(the_id);
		jQuery(this).children('.pager-images').addClass(the_id);
		
		jQuery('ul.'+the_id).cycle({ 
			fx:     'scrollHorz', 
			speed:  'fast', 
			timeout: 0, 
			containerResize: 1,
			pager:  'div.'+the_id,			
			after: onAfter,
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				//return '#nav li:eq(' + idx + ') a';
				my_idx = idx + 1;
				return '<a href="#"class="pi'+my_idx+'">0'+ my_idx + '</a>';
			}

		});
		function onAfter(curr, next, opts, fwd) {
			//get the height of the current slide
			
			var $ht = jQuery(this).children('img').attr('height');
			//alert($ht);
			//set the container's height to that of the current slide
			if($ht != 0) {
				jQuery(this).parent().animate({height: $ht});
				if(jQuery('#clients').css('display') != 'none'){
					jQuery(this).parents('#clients').animate({height: $ht+70})
				}
			} else {
				var src = jQuery(this).children('img').attr('src');								
				var img = new Image();
				img.src = src;
				var $ht2 = img.height;
				jQuery(this).parent().animate({height: $ht2});
				if(jQuery('#clients').css('display') != 'none'){
					jQuery(this).parents('#clients').animate({height: $ht2+70});
				}
			}			
		}
		
		//add leading zero's
		jQuery(this).children('.pager-images a').prepend('0');	
	});
	
});
