// JavaScript Document

$(document).ready(function() {
	 $(".tab_menu ul li").mouseover(function() { $(this).addClass("over");}).mouseout(function() { $(this).removeClass("over");});
	  $(".tab_menu2 ul li").mouseover(function() { $(this).addClass("over");}).mouseout(function() { $(this).removeClass("over");});
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$(".tab_menu ul li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$(".tab_menu ul li").click(function() {

		$(".tab_menu ul li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	//Tab menu 2
	$(".tab_content2").hide(); //Hide all content
	$(".tab_menu2 ul li:first").addClass("active").show(); //Activate first tab
	$(".tab_content2:first").show(); //Show first tab content
	
	//On Click Event
	$(".tab_menu2 ul li").click(function() {

		$(".tab_menu2 ul li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content2").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	//Active Menu
	$(".top_menu ul li a:first").addClass("active").show(); //Activate first menu item
	// on menu click event
	$(".top_menu ul li a").click(function(){
		$(".top_menu ul li a").removeClass("active");
		$(this).addClass("active");  //Add "active" class to selected menu
		//var activeMenu = $(this).find("a").attr("href"); //Find the href attribute value to identify the active menu 
		//$(activeMenu).fadeIn();
		//return false;
	});
	
//Slide content script goes here
  var currentPosition = 0;
  var slideWidth = 560;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl"></span><br />')
	.prepend('<span class="control" id="leftControl_off"></span><br />')
	.append('<span class="control" id="rightControl_off"></span> <br />')
    .append('<span class="control" id="rightControl"></span> <br />');


  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
	
  $('.control')
    .bind('click', function(){
    
	
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    
	currentPosition  = currentPosition <0 ? 0 : currentPosition;
	currentPosition  = currentPosition > numberOfSlides-1 ? numberOfSlides-1 : currentPosition;
	
	//console.log(numberOfSlides);
	//console.log(currentPosition);

	// Hide / show controls
    manageControls(currentPosition);
	
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
	//if(position==0){ $('#leftControl').hide(); } else{ $('#leftControl').show() }
	if(position==0){ $('#leftControl').hide(); $('#leftControl_off').show();  } else{ $('#leftControl').show(); $('#leftControl_off').hide(); }
	//if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	//if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide(); $('#rightControl_off').show(); } else{ $('#rightControl').show(); $('#rightControl_off').hide(); }
	
  }
	

});
