$(document).ready(function() {

	var m_head_slider = $('#m_bg-hold');
	var m_head_slides = $('#m_bg-hold li');
	var m_head_slideLength = m_head_slides.length;
	var m_head_currentIndex = 1;
	var m_head_nextIndex = 1;
	var m_head_duration = 800;
	var m_head_controls = $('#m_head_controls');
	var m_head_controlWidth = m_head_slideLength * 25;
	var m_head_titleList = $('#m_head_main #m_title');
	var m_head_titles = $('li',m_head_titleList);
	var m_head_animating = false;
	var m_head_controlsActive = true;
	
	//INITIATE SLIDESHOW
	m_head_slides.css('opacity', 0);
	$('li:nth-child('+m_head_nextIndex+')',m_head_slider).addClass('show').css('opacity',1);
	
	//INITIATE TITLES
	m_head_titles.css({'width':'100%','position':'absolute','opacity':0});
	$('li:nth-child('+m_head_nextIndex+')',m_head_titleList).addClass('show').css('opacity',1);
	
	//INITIATE CONTROLS
	if(m_head_controlsActive){
		m_head_controls.css({'width': m_head_controlWidth});
		$('#home .container_main').css('padding','1.625em 0 4.875em');
		for(c=0; c<m_head_slideLength; c++){
			if(c==0){
				m_classAdd = 'active';
			}else{
				m_classAdd = '';
			}
			m_head_controls.append('<a id="'+c+'_hs" class="'+m_classAdd+'"></a>');
		}
	
		//SLIDESHOW CONTROL
		var m_head_controlLink = $('#m_head_controls a');
		m_head_controlLink.click(function(){
			m_click_newIndex = parseInt($(this).attr('id').split("_")[0]);
			m_click_currentIndex = $('li.show', m_head_slider).index();
			if(m_click_currentIndex == m_click_newIndex){return;}else{
				if(m_head_slides.size()>1) {
					clearInterval(m_head_play);
					m_hslide_move(m_click_newIndex+1);
					m_head_automate();
				}
			}
		});
		
		//SLIDESHOW NAV
		var m_head_control_nav = $('#m_bg-nav a');
		m_head_control_nav.click(function(){
			m_click_currentIndex = $('li.show', m_head_slider).index();
			
			if($(this).attr("id")=="prev") {
				if(!m_click_currentIndex) m_req = m_head_slideLength-1;
				else m_req=m_click_currentIndex-1;
			}
			if($(this).attr("id")=="next")	{
				if(m_click_currentIndex==m_head_slideLength-1) m_req=0;
				else m_req=m_click_currentIndex+1;
			}	

			if(m_head_slides.size()>1) {
				clearInterval(m_head_play);
				m_hslide_move(m_req+1);				
				m_head_automate();			
			}
		});
	}
	
	//SLIDESHOW MOVEMENT
	m_hslide_move = function(request){
		if(!m_head_animating){
			m_head_animating = true;
			if(!request){
				m_head_nextIndex = m_head_currentIndex+1;
				if(m_head_nextIndex > m_head_slideLength){
					m_head_nextIndex = 1;
				}
			}else{
				m_head_nextIndex = request;
			}
			$('li:nth-child('+m_head_nextIndex+')',m_head_slider).addClass('show').animate({'opacity':1},m_head_duration);
			$('li:nth-child('+m_head_currentIndex+')',m_head_slider).removeClass('show').animate({'opacity':0},m_head_duration,function(){m_head_animating=false;});
			$('li:nth-child('+m_head_nextIndex+')',m_head_titleList).addClass('show').animate({'opacity':1},m_head_duration);
			$('li:nth-child('+m_head_currentIndex+')',m_head_titleList).removeClass('show').animate({'opacity':0},m_head_duration);
			$('.active',m_head_controls).removeClass('active');
			$('#'+(m_head_nextIndex-1)+'_hs').addClass('active');
			m_head_currentIndex = m_head_nextIndex;
		}else{return;}
	}
	
	m_head_automate = function(){
		m_head_play = setInterval(function(){m_hslide_move();},5000);
	}
	
	if(m_head_slides.size()>1) m_head_automate();
});
