$(document).ready(function() {	
	
	// Set Homepage Content Box Height
	setHeight();
	
	// Image Slider  
	if( $('ul.switcher').length > 0 ){
		$('ul.switcher li:first').remove();
	}
	$('ul.gallery').cycle({ 
		fx:      'fade',
        timeout:  0,
		pause:    1,
		pager: 'ul.switcher',
		pagerAnchorBuilder: customLink
        //prev:    '.link-prev',
        //next:    '.link-next'
	});	
	
	// Menu Item Selected
	$(".services-box ul li").click( function(e){			
		e.preventDefault();
		
		// If this is already selected ignore
		if( $(this).attr('class') == 'active'){
			return false;
		}
		
		$('a.readmore').hide();
		var url = $(this).children('a').attr('href');
		var urlArray = url.split('/');
		
		// Active status
		$("div.services-box ul li").removeClass('active');
		$(this).addClass('active');
		
		// Visibility of content
		$("div.services-box-content").hide();
		$("#service_content_"+urlArray[2]).show();
		setHeight(urlArray[2]);
		
		// Change Slider Image
		var id = $(this).children('a').attr('name');
		if(id){
			$('ul.gallery').cycle('pause');
			$('ul.gallery li').css('z-index','9').removeClass('selected').css('opacity', 0).fadeOut('slow');
			$('ul.gallery li.'+id).css('z-index','10').addClass('selected').css('opacity', 1).fadeIn('slow');			
				
			// Pager
			var indx = $('ul.gallery li.selected').index(); // Select index of selected slide
			$('ul.switcher li').removeClass('activeSlide');
			if(indx >= 0){
				$('ul.switcher li.switch_'+(indx+1)).addClass('activeSlide');
			}
		}
	});

});



function customLink(i, el){
	return "<li class='switch_"+(i+1)+"'><a href='#'>"+(i+1)+"</a></li>";
}


function setHeight(elemId){	
	$('a.readmore').hide();
	elemId = (elemId == '') ? 'a.readmore:first' : '#read_'+elemId;
	var h = $('.services-box ul').height();

	// Set to height of first container	
	var content = $('.services-box-content:visible').height();		
	if(h < content+5){ 
		$('.services-box-content:visible').height(h);
		$(elemId).fadeIn();
	}else{
		$('a.readmore').hide();
	}	
}
