$(document).ready(function() {

	//dropDownMenu();
	addSeparators();
	autoSlide(5000, 1);
	bg_changer();

	portfolio();

});

function dropDownMenu() {

	$("#menu ul ul").css({display: "none"});

	$("#menu ul li").hover(function() {
		$(this).find('ul:first').css({display: "block", opacity: 0}).stop().animate({ opacity: 1 }, 200); //Slides down when hover the UL
		$(this).children('a').addClass("hovered"); //Adds a hovered class, so you can see the menu path you are following
	}
	,function() {
		$(this).find('ul:first').css({display: "none"}); //Slides up on mouseleave
		$(this).children('a').removeClass("hovered"); //removes the hovered class.
	});

}

function autoSlide($time, $stop) {

	$('#slider ul#slider_images li:first').addClass('current');

	$total_slides = ($('#slider ul li').length);
	$i = 1

	$('#slider_caption').text($('#slider ul#slider_images li.current a img').attr('alt'));



	//Sets the link for learn more button
	var learn_more_URL = $('#slider_images li.current a').attr('href');
	$('#learn_more a').attr('href', learn_more_URL);


	$('#slider_info .left ul').after('<ul id="slider_selector"></ul>');


	$('#slider ul#slider_images li').each(function() {

		$(this).addClass('slider_'+$i);

		if($i == 1) {
			$('#slider_selector').append('<li class="current sel_'+$i+'"></li>');
		} else {
			$('#slider_selector').append('<li class="sel_'+$i+'"></li>');
		}

		$i++;

	})

	if(typeof(isClicked) == 'undefined') { var isClicked = 0; }

	$('#slider_selector li').click(function() {

		$id = $(this).attr('class').split(' ');

		if($id[0] == 'current' || $id[1] == 'current') {

			//do nothing

		} else {

			$id = $id[0].split('_');
			callSlide($id[1]);

			isClicked = 1;

		}

	});



	if(typeof(isHovered) == 'undefined') { var isHovered = 0; }
	//If no item has been hovered yet, the variable isHovered doesn't exist, so we create it.

	$("#slider").hover(function() { // Whenever an item is hovered
		isHovered = 1; //Setting isHovered 1, we stop the autsliding from going on
	}, function() {
		isHovered = 0;//Setting isHovered 1, we make the autsliding go on
	});

	setInterval(function() {

		if($stop == 1) {

			if(isHovered == 0 && isClicked == 0) { //It only calls the next item IF no item is hovered

				nextSlide();

			}

		} else if(isClicked == 0) {

			nextSlide();

		}



	}, $time);

	$('#left_arrow').click(function(){ prevSlide(); isClicked = 1; });

	$('#right_arrow').click(function(){ nextSlide(); isClicked = 1; });

}

function nextSlide() {

	$current = $('#slider_images li.current');
	$next = $('#slider_images li.current').next();

	$current_sel = $('#slider_selector li.current');
	$next_sel = $('#slider_selector li.current').next();


	if($next.children('a').children('img').attr('alt') == undefined) {
		$next = $("#slider_images li:first");
		$next_sel = $("#slider_selector li:first");
	} else {
		//Do nothing. The  $next element exists
	}

	$next.addClass('next');
	$current.stop().animate({ opacity: 0 }, 300, function() {

		$next.addClass('current').removeClass('next');
		$current.removeClass('current').css({ left: '0px', opacity: 1 });

		$current_sel.removeClass('current');
		$next_sel.addClass('current');

		$('#slider_caption').text($next.children('a').children('img').attr('alt'));

		var learn_more_URL = $('#slider_images li.current a').attr('href');
		$('#learn_more a').attr('href', learn_more_URL);

	});

	//$next.addClass('next').css({ opacity: 0 }).stop().animate({ opacity: 1 }, 300, function() {
//
//		$next.addClass('current').removeClass('next');
//		$current.removeClass('current');
//
//	});


}

function prevSlide() {

	$current = $('#slider_images li.current');
	$prev = $('#slider_images li.current').prev();

	$current_sel = $('#slider_selector li.current');
	$prev_sel = $('#slider_selector li.current').prev();


	if($prev.children('a').children('img').attr('alt') == undefined) {
		$prev = $("#slider_images li:last");
		$prev_sel = $("#slider_selector li:last");
	} else {
		//Do nothing. The  $next element exists
	}

	$prev.addClass('next');
	$current.stop().animate({ opacity: 0 }, 300, function() {

		$prev.addClass('current').removeClass('next');
		$current.removeClass('current').css({ left: '0px', opacity: 1 });

		$current_sel.removeClass('current');
		$prev_sel.addClass('current');

		$('#slider_caption').text($prev.children('a').children('img').attr('alt'));

		var learn_more_URL = $('#slider_images li.current a').attr('href');
		$('#learn_more a').attr('href', learn_more_URL);

	});

	//$next.addClass('next').css({ opacity: 0 }).stop().animate({ opacity: 1 }, 300, function() {
//
//		$next.addClass('current').removeClass('next');
//		$current.removeClass('current');
//
//	});


}


function callSlide($slideID) {

	$current = $('#slider_images li.current');
	$next = $('#slider_images li.slider_'+$slideID);

	$current_sel = $('#slider_selector li.current');
	$next_sel = $('#slider_selector li.sel_'+$slideID);

	$next.addClass('next');
	$current.stop().animate({ opacity: 0 }, 300, function() {

		$next.addClass('current').removeClass('next');
		$current.removeClass('current').css({ left: '0px', opacity: 1 });

		$current_sel.removeClass('current');
		$next_sel.addClass('current');

		$('#slider_caption').text($next.children('a').children('img').attr('alt'));

		var learn_more_URL = $('#slider_images li.current a').attr('href');
		$('#learn_more a').attr('href', learn_more_URL);

	});

}

function addSeparators() {

	$('#menu ul:first').children('li').each(function() {

		$(this).after('<li class="border"></li>');

	});

	$('.border:last').remove();

	$('#menu ul li ul').each(function() {

		$(this).children('li:first').children('a').css('border-top', 'none');
		$(this).children('li:last').children('a').css('border-bottom', 'none');

	});

	$('.sidebar_item_content ul').each(function(){ $(this).children('li').last().css({ background: 'none' }) });
	$('.footer_item_content ul li:last').css({ background: 'none' });
	$('.favorite_links ul li:last').css({ background: 'none' });

	$('#copyright ul li:last').css({ background: 'none' });

}

function bg_changer() {

	$('#universe').click(function() { $('#header, #header_index').css({ 'background': '#100e16 url(images/site/header_bg.jpg) no-repeat top center' }) });
	$('#l_grey').click(function() { $('#header, #header_index').css({ 'background': '#100e16 url(images/site/header_bg_2.jpg) no-repeat top center' }) });
	$('#d_blue').click(function() { $('#header, #header_index').css({ 'background': '#061c2a url(images/site/header_bg_5.jpg) no-repeat top center' }) });
	$('#maroon').click(function() { $('#header, #header_index').css({ 'background': '#0f0504 url(images/site/header_bg_3.jpg) no-repeat top center' }) });
	$('#b_black').click(function() { $('#header, #header_index').css({ 'background': '#0f1110 url(images/site/header_bg_4.jpg) no-repeat top center' }) });

}

function portfolio() {

	$('#portfolio_cats ul li a').click(function() { return false; });

	$('#portfolio_cats ul li a').click(function() {

		var current_cat = $(this).text();

		if(current_cat == 'All') {

			$('#portfolio_content ul li').stop().animate({ opacity: 0 }, 300, function() {

				$('#portfolio_content ul li').hide();

				$('#portfolio_content ul li').each(function() {

					$(this).stop().show().animate({ opacity: 1 }, 300).css({ opacity: 1 });

				});

				$('#portfolio_cats ul li a.current').removeClass('current');
				$('#portfolio_cats ul li a:contains('+current_cat+')').addClass('current');

			});

		} else {

			$('#portfolio_content ul li').stop().animate({ opacity: 0 }, 300, function() {

				$('#portfolio_content ul li').hide();

				$('#portfolio_content ul li[title='+current_cat+']').stop().show().animate({ opacity: 1 }, 300);

				$('#portfolio_cats ul li a.current').removeClass('current');
				$('#portfolio_cats ul li a:contains('+current_cat+')').addClass('current');

			});

		}

	});


	//resize the a tag
	var img_width_base = $('#portfolio_content ul li img').width();
	var img_height_base = $('#portfolio_content ul li img').height();

	var img_width = (img_width_base + 8);
	var img_height = (img_height_base + 8);

	$('#portfolio_content ul li a').css({ width: img_width, height: img_height });

	//hover effect
	$('#portfolio_content ul li').hover(function() {

		$(this).children('a').show();
		$('#portfolio_content ul li').stop().animate({ opacity: .5 }, 300);
		$(this).stop().css('opacity', 1);

	}, function() {

		$(this).children('a').hide();

		$('#portfolio_content ul li').stop().animate({ opacity: 1 }, 300);

	});

}
