var slideshow = {
  fire: function(seconds){
	var interval = 0;
	var time = seconds * 1000;
	var slideNum = 0;
	var pause = false;
	var pauseTime = 0;
	var restack = false;
	var container = $('#slideshow');
	var slideshow = $('#slideshow img');
	var numberOfSlides = slideshow.length;
	var bullets = $('<div class="bullets"></div>');
	var caption = $('<div class="caption"></div>');
		
	//If slideshow has more than 1 slide then build show/////////////////////
	if (numberOfSlides > 1 && seconds){
		// Arrange order of slides and add links to bullet navigation
		$(slideshow).each(function(i){
			$(this).css({
				'z-index' : 0
			});
			$(bullets).append('<a href="#" class="navB' + (i+1) +'">' + (i+1) + '</a>');
		});
		
		// Set default positions of first and second slide
		$(slideshow).eq(0).css({'z-index' : 2});
		$(slideshow).eq(1).css({'z-index' : 1});
		comment($(slideshow).eq(0).attr("name"));
		
		// Load container with bullet and caption
		$(container)
			.append(bullets, caption)	
		createCaption();
		
		// Add current class to first bullet
		currentBullet(slideNum);
		
		// Add functionality to navigation links
		$('#slideshow .bullets a')
			.click(function(e){
				e.preventDefault();
				if ($(slideshow).is(":animated")){
					return;
				}else{
					var linkName = $(this).html();
					arrangeNavStack(linkName);
					pauseTime = 0;
					
				    if(!pause){
					  pause = true;
					  startClock();
					  pauseAndPlay(); 
				    }else{
					  return;  
				    } 
				}
			});
		// Start Clock
		setTimeout(function() {startClock();},time)
	}
	
	function createCaption(){
		var position = bullets.position();
		var capW = $(caption).width();
		var capH = $(caption).height();
		var bulletH = $(bullets).height();
		var capP = $('#slideshow p');
		$(caption).css({'left': ( position.left - capW), 'top': ((position.top - capH) + bulletH)})
		$(capP).each(function(i){
			$(caption).append(this);
		});
	};
	
	// Add class to current bullet
	function currentBullet(bulletNum){
		$('.bullets a').removeClass('current');
		$('.bullets a').eq(bulletNum).addClass('current');
	}
	
	//Comment
	function comment(slideName){
		if(slideName){
			var comment = $('#slideshow .' + slideName);
			var capP = $('#slideshow p');
			var visible = $(comment).is(':visible');
			if(!visible){
			  if($.browser.msie){
				$(capP).hide();
			    $(comment).show();	
			  }else{
				$(capP).fadeOut(500);
			    $(comment).fadeIn(1000); 
			  } 
			}	
		}else{
			return;
		}
	};
	
	// Arrange navigation Stack
	function arrangeNavStack(linkName){
		restack = true;
		
		$(slideshow).each(function(i){
			$(this).css({
				'z-index' : 0
			});
		});
		
		var slideName = $(slideshow).eq(linkName - 1).attr('name')
			if((linkName - 1) == 0){
				$(slideshow).eq(0).css({'z-index' : 2});
				$(slideshow).eq(1).css({'z-index' : 1});
			}else if((linkName - 1) == numberOfSlides - 1){
				$(slideshow).eq(linkName - 1).css({'z-index' : 2});
				$(slideshow).eq(0).css({'z-index' : 1});
			}else{
				$(slideshow).eq(linkName - 1).css({'z-index' : 2});
				$(slideshow).eq(linkName).css({'z-index' : 1});
			}
			slideNum = linkName - 1;
			currentBullet(linkName - 1);
			comment(slideName);			
	}
		
	// ENGINE
	function engine (currentSlide){	
		$(slideshow).eq(currentSlide).fadeOut(function(){
			if(currentSlide == numberOfSlides - 2){
				++slideNum;
				$(slideshow).eq(currentSlide + 1).css({'z-index': 2});
				$(slideshow).eq(0).css({'z-index': 1});
			}else if(currentSlide == numberOfSlides - 1){
				slideNum = 0;
				$(slideshow).eq(0).css({'z-index': 2});
				$(slideshow).eq(1).css({'z-index': 1});
			}else{
				++slideNum;
				$(slideshow).eq(currentSlide + 1).css({'z-index': 2});
				$(slideshow).eq(currentSlide + 2).css({'z-index': 1});
			}
			$(this).show().css({'z-index' : 0});
		});		
	};
	
	// IGNITION
	function ignite(){
		if(restack){
				restack = false;
				$(slideshow).each(function(i){
					$(this).css({
						'z-index' : 0
					});
				});
				if(slideNum == 0){
					$(slideshow).eq(0).css({'z-index' : 2});
					$(slideshow).eq(1).css({'z-index' : 1});
				}else if(slideNum == numberOfSlides - 1){
					$(slideshow).eq(slideNum).css({'z-index' : 2});
					$(slideshow).eq(0).css({'z-index' : 1});
				}else{
					$(slideshow).eq(slideNum).css({'z-index' : 2});
					$(slideshow).eq(slideNum + 1).css({'z-index' : 1});
				}
			}
			
		if(slideNum == numberOfSlides - 1){
			interval = 0;
			$('.currentTime').replaceWith('<div class="currentTime">' + interval + '</div>');
			
			var slideName = $(slideshow).eq(0).attr('name');
			currentBullet(0);
			comment(slideName);
		}else{
			++interval
			$('.currentTime').replaceWith('<div class="currentTime">' + interval + '</div>');
			
			var slideName = $(slideshow).eq(slideNum + 1).attr('name');
			currentBullet(slideNum + 1);
			comment(slideName);
		}	
		engine(slideNum);
	};
	
	//PAUSE AND PLAY
	function pauseAndPlay(){
			if(pause && pauseTime <= 3){
				++pauseTime
				setTimeout(function() {pauseAndPlay();},1000)
			}else if(!pause){
				return;
			}else{
				pause = false;
				startClock();	
			}
		}
	
	//CLOCK
	function startClock(){
			if(!pause){
				ignite();
				setTimeout(function() {startClock();},time)
			}else{
				return;
			}		
		};   
  }
}
$(document).ready(function() {
  var e = window.location.href;
  var j = e.substring(e.length,e.length - 1);
  var newsNav = $('.newsCatNav a');
  var box = $('.acordCon');
  
  
 /* if($(newsNav).attr('href') == e ){
	$(this).addClass('current');
  }*/
  
  $(newsNav)
    .each(function(i){
	  var x = $(this).attr('href');
	  if(x == e){
		$(this).addClass('current');
	  }
    });
  //Slideshow
  slideshow.fire(10); //Set the number of seconds between each slide.

  //Default
  $('.cssform, #search form').defaultvalues({focusaction:"clear"});
  
  //Accordian
  $('div.acordCon').eq(0).css({"width":"355px"});
  $('div.acordCon').eq(2).css({'border-right':'solid 1px #B0BDB8'});
 
  
  $('a.title').click(function(e){
	$(this).addClass('current');
	var selfClick = $(this).parent().children(".acordCon");
	if(selfClick.width() <= 0) {
	  $(this)
	    .parent()
		.children(".acordCon")
		.stop()
		.animate({"width":"355px"},500);	
		collapse();
		$(this).addClass('current');
    }
	e.preventDefault();  		
  });	
  
  // toggle paragraph functionality
  $('.toggle').hide();
  $('.textToggle').click(function(){
	  var t = $(this);
	  if(t.next().css('display') == 'none') {
		  t.next().slideDown('slow');
   	      t.text('[Hide]');
		}  
  	  else {
		t.next().slideUp('fast');
 	    t.text('[Read More]');
  	  }  
  	});


  $('#step1').click(function(){
	  if ($('.consultationExpander').text() == '[Hide]'){
		  	$('.consultationExpander').text('[Read More]');
  			$('.consultation').slideUp('fast');
	  }
	  else{
		  	$('.consultationExpander').text('[Hide]');
			$('.consultation').slideDown('slow');
	  }
  });
  
  $('#step2').click(function(){
	  if ($('.creationExpander').text() == '[Hide]'){
		  	$('.creationExpander').text('[Read More]');
  			$('.creation').slideUp('fast');
	  }
	  else{
		  	$('.creationExpander').text('[Hide]');
			$('.creation').slideDown('slow');
	  }
  });
  
  $('#step3').click(function(){
	  if ($('.portfolioExpander').text() == '[Hide]'){
		  	$('.portfolioExpander').text('[Read More]');
  			$('.portfolio').slideUp('fast');
	  }
	  else{
		  	$('.portfolioExpander').text('[Hide]');
			$('.portfolio').slideDown('slow');
	  }
  });  
  
  $('#step4').click(function(){
	  if ($('.monitoringExpander').text() == '[Hide]'){
		  	$('.monitoringExpander').text('[Read More]');
  			$('.monitoring').slideUp('fast');
	  }
	  else{
		  	$('.monitoringExpander').text('[Hide]');
			$('.monitoring').slideDown('slow');
	  }
  });  

 
 });

function collapse(){
	var box = $('.acordCon');
	var title = $('.title');
	
	$(title)
	  .each(function(i){
	    $(this).removeClass('current');  
	  });
	$(box)
	  .each(function(i){
	    if($(this).width() > 0){
		 $(this)
		  .stop()
		  .animate({"width":"0"},500);
		}else{
		  return;	
		}
	  });	
};


