$(document).ready(function(){
	 
	var sets = $("#filmstrip > li").length - 4;
	var filmstrip_width = 0;
	var counter = 1;
	var flag = 0;
	
	$(".link-prev").bind('click', function(){
		if(counter > 1 && flag == 0){
			flag = 1;
			counter--;
			// filmstrip_width = ($("#filmstrip > li").outerWidth()) * ($("#filmstrip > li").length - counter);
			filmstrip_width = 228;
			$(".gallery #filmstrip").stop().animate({
				'margin-left': '+='+filmstrip_width+'px'
			}, 750, 'easeOutExpo' , function(){
				flag = 0;
			});
		}
		return false;
	});
	
	$(".link-next").bind('click', function(){
		if((counter <= (sets)) && flag == 0){
			flag = 1;
			// filmstrip_width = ($("#filmstrip > li").outerWidth()) * ($("#filmstrip > li").length - counter);
			filmstrip_width = 228;
			$(".gallery #filmstrip").stop().animate({
				'margin-left': '-='+filmstrip_width+'px'
			}, 750, 'easeOutExpo' , function(){
				flag = 0;
				counter++;
			});
		}
		return false;
	});
	
	

	/** 
		returns either medical-devices, electronic-backlighting, about-us to set
		respective active state for top menu
	*/ 
	var location = window.location.toString().split('/');

	$('.text-' + location[3]).parent().addClass('active');
	
	if (location[3]=="medical-devices") {
			$('.add-nav').addClass('medical-devices');
	}
	

/*
	$("li.current_page_ancestor").removeClass("current_page_ancestor").addClass("top_parent").children("ul.children").css("display", "block");
	
	$("li.current_page_item").removeClass('current_page_item').addClass("active").find("ul.children").addClass('active');
*/

	// 3rd tier navigation
	
	$('ul.text-nav > li > ul.children > li > a').click(function() {
			
		var thisEl = $(this).parent();
		console.log( $(this).html() );
		
		if (isLowestLevel()) {

			
			
			
			
			
			$('li.current_page_item').removeClass('current_page_item');
			//$('li.current_page_ancestor.current_page_parent').children('ul.children').slideUp(function() {
			
				$('li.current_page_ancestor.current_page_parent').removeClass('current_page_ancestor current_page_parent')
				//thisEl.children('ul.children').slideDown(function() {
					thisEl.addClass('current_page_item');
					thisEl.delay(1000).getContent(thisEl.children('a').attr('href') + '?v=1');
				//});
			//});
			
			// load in content
			
			
			return false;
			
		} else {
		
			//console.log(thisEl);
		
			// clicked on lowest level
			/*
if (thisEl.children('ul').length == 1) {
				return true;
			} else {
*/
			// clicked on mid level			
				//$('li.current_page_item').children('ul.children').slideUp(function() {
				
					$('li.current_page_item').removeClass('current_page_item')
				
					//thisEl.children('ul.children').slideDown(function() {
						thisEl.addClass('current_page_ancestor current_page_parent');
						//return true;
					//});
				//});
			}
/* 		} */
		
	});
	
	
	
/*
	$("ul.text-nav li a").click(function() {
	
		var href = "";
		
		$("li.active").removeClass('active');
		
		// if a top level page (Applications, etc.) get the first child page
		if (topParent($(this))) {
			$("li.top_parent").removeClass('top_parent');
			$(this).parent().addClass('top_parent');
			href = $(this).next("ul").children("li:first-child").first().find("a").attr('href') + "?v=1";
			$(this).next("ul").children("li:first-child").first().addClass('active');
		} else {
			href = $(this).attr('href') + "?v=1";
		}
		
		// grab the page's content

		$.ajax({
			type: "GET",
			url: href,
			dataType: "html",
			success: function(data) {
				$("#sub-holder").html(data);
			},
			error: function(data) {
			}
		});

		
		// display its children
		if (hasChildren($(this)) && ($(this).parent().find("ul.children").css('display') == 'none')) {
		
		// if a top-level page (Applications, Technologies, etc.)
		if ($(this).parent().parent().hasClass('text-nav')) {
			
			
			$(this).parent().siblings('li').find('ul.children').slideUp(750, 'easeOutExpo');
			

			$(this).siblings('ul.children').slideDown(750, 'easeOutExpo');
			
		} else {		
			$("ul.children.active").slideUp(750, 'easeOutExpo', function() {
						$(this).removeClass('active');
					});
				$(this).parent().find("ul.children").slideToggle(750, 'easeOutExpo', function() {
					$(this).addClass('active');
				});
			}
			
			
		}
	
		
		
		if (!$(this).parent().parent().hasClass('text-nav'))
			$(this).parent().addClass('active');
		$("li.parent").removeClass('parent');
		
		if (!$(this).parent().parent().parent().parent().hasClass('text-nav'))
			$(this).parent().parent().parent().addClass('parent');
	
		return false;
	});
*/
	
});

$.fn.getContent = function(href) {
	$.ajax({
		type: "GET",
		url: href,
		dataType: "html",
		success: function(data) {
			$("#sub-holder").html(data);
		},
		error: function(data) {
		}
	});
}

function lowestLevelClicked() {
	
}

function isLowestLevel() {
	if ($("li.current_page_ancestor").length == 4)
		return true;
	else
		return false;
}
function isMidLevel() {
	if ($("li.current_page_ancestor").length == 2)
		return true;
	else
		return false;
}

function topParent(el) {
	if (el.parent().parent().hasClass('text-nav'))
		return true;
	else
		return false;
}

function hasChildren(el) {
	var children = el.children('ul.children');
	
	if (children.length != 0)
		return true;
	else
		return false;
}
