jQuery(function($){
	
	if($('.pageHeader img').length > 0){
		if($('.pageHeader img').width() < 960){
			$('.pageHeader').hide();
		}
	}

	$(".nav ul li").each(function(){
		$(this).append("<div class='navHoverItem' style='width:"+($(this).find("a").width()+4)+"px'></div>");
	});
	$(".nav ul li").hover(function(){
		$(this).find(".navHoverItem").stop(true, true);
		$(this).find(".navHoverItem").animate({height:'10px'}, 100, "linear");
	},function(){
		$(this).find(".navHoverItem").stop(true, true);
		$(this).find(".navHoverItem").animate({height:'1px'}, 150, "linear");
	});


	$("li.inquire a, a.inquire").click(function(){
		$(".headerNav a:contains('Inquire')").toggleClass("chosen");
		var clickedItem = $(this);							 
		$(".actions").slideToggle(1000, function(){});
	});
				

	var services = 
	{
		init: function()
		{
			
			//On Click Event
			/*
			$("ul.services li").hover(function(){
				//hover in							   
				$(".servicesContentDefault, .servicesContent").fadeOut(150); //Hide all tab content
				var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
				$(activeTab).stop(true, true);
				$(".servicesContentDefault").stop(true, true);
				$(".servicesContentDefault").hide();
				$(activeTab).fadeIn(150); //Fade in the active ID content
											   
			},function(){
				//hover out
				if($("ul.services li").hasClass("active") == false){
					var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
					$(activeTab).stop(true, true);
					$(".servicesContentDefault").stop(true, true);
					$(activeTab).fadeOut(150); //Fade in the active ID content				
					$(".servicesContentDefault").fadeIn(150); 
				}else{
					var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
					var activatedTab = $("ul.services li.active").find("a").attr("href"); //Find the href attribute value to identify the active tab + content
					$(activeTab).stop(true, true);
					$("ul.services li.active").stop(true, true);
					$(activeTab).fadeOut(150); //Fade in the active ID content				
					$(activatedTab).fadeIn(150); 
				}
			});
			$("ul.services li a").click(function() {
				return false;
			});
			*/
			
			$("ul.services li").hover(function(){
				$(this).find("span").stop(true,true);
				$(this).find("a").stop(true,true);
				if($(this).hasClass("active") == false){
					$(this).find("span").fadeOut(250);
					$(this).find("a").animate({color:"#ffffff"}, 250);
				}
			},function(){
				$(this).find("span").stop(true,true);
				$(this).find("span").fadeIn(250);
				if($(this).hasClass("active") == false){
					$(this).find("a").stop(true,true);
					$(this).find("a").animate({color:"#aaaaaa"}, 250);
				}
			});
			
			$("ul.services li").click(function() {
				$("ul.services li").removeClass("active"); //Remove any "active" class
				$("ul.services li a").css("color", "#aaaaaa");
				$(this).addClass("active"); //Add "active" class to selected tab
				$(this).find("a").css("color", "#ffffff");
				$(".servicesContentDefault, .servicesContent").fadeOut(150); //Hide all tab content
	
				var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
				$(activeTab).fadeIn(150); //Fade in the active ID content
				return false;
			});
			
		}/* init function */
		
	}// end services object 
	
	//dropDown.init(); // Calling Dropdown object
	services.init(); // Callling services object
	
	function responseCallBack(resp,curItem){
		$(".tempAboutInfo").html(resp);
		var itemHeight = 350;	
		var sideHeight = 250;
		var fadeSpeed = 1000;
		if($(".tempPosts:hidden").length > 0){
			$(".tempPosts").html($(".tempAboutInfo").find(".blogContent").html());
			$(".tempContent").each(function(i){
				var curItem = i;
				$(".post-body").eq(curItem).text($(".tempContent").eq(curItem).text().substring(0,300));
				$(".post-body").eq(curItem).append("<a href='"+$('.post-title').eq(curItem).find("a").attr("href")+"'><span class='ellipsis'>... </span> Read Full Article</a>");
			});
			$(".tempSideContent").html($(".tempAboutInfo").find(".nameAndTitle").html());
			$(".tempSideContent").append($(".tempAboutInfo").find(".side-content").html());
			$(".tempPosts").fadeIn(fadeSpeed);
			$(".tempSideContent").fadeIn(fadeSpeed);
			$(".postListAbout").fadeOut(fadeSpeed);
			$(".selectSideContent").fadeOut(fadeSpeed);
			itemHeight = $(".tempPosts").height();
			sideHeight = $(".tempSideContent").height();
		}else if($(".postListAbout:hidden").length > 0){
			$(".postListAbout").html($(".tempAboutInfo").find(".blogContent").html());
			$(".tempContent").each(function(i){
				var curItem = i;
				$(".post-body").eq(curItem).text($(".tempContent").eq(curItem).text().substring(0,300));
				$(".post-body").eq(curItem).append("<a href='"+$('.post-title').eq(curItem).find("a").attr("href")+"'><span class='ellipsis'>... </span> Read Full Article</a>");
			});			
			$(".selectSideContent").html($(".tempAboutInfo").find(".nameAndTitle").html());
			$(".selectSideContent").append($(".tempAboutInfo").find(".side-content").html());
			$(".postListAbout").fadeIn(fadeSpeed);
			$(".selectSideContent").fadeIn(fadeSpeed);
			$(".tempPosts").fadeOut(fadeSpeed);
			$(".tempSideContent").fadeOut(fadeSpeed);
			itemHeight = $(".postListAbout").height();
			sideHeight = $(".selectSideContent").height();
		}
		$(".postListContainer").height(itemHeight);
		$(".aboutSideContent").height(sideHeight);
		
		pullBlogAvatars();
		if(curItem == "level9"){
			$(".socialSec").fadeIn(fadeSpeed);
		}else{
			$(".socialSec").fadeOut(fadeSpeed);
		}
	}
	
	var level9Featured = {
		init: function() {
			// Gallery
			$("#gallery-prev").click(function(){
				$("#gallery li").first().stop(true, true);
				$("#gallery li").last().css("margin-left", "-260px").prependTo("#gallery");
				$("#gallery li").first().animate({"margin-left":"0"}, 500, "swing", function(){
					var curItem = $("#gallery li").eq(1).attr("class");
					$.get("/AboutContent/"+curItem, function(resp, stat){
						responseCallBack(resp, curItem);
					});
				});
			});
			$("#gallery-next").click(function(){	
				$("#gallery li").first().stop(true, true);
				$("#gallery li").first().animate({"margin-left":"-=260px"}, 500, "swing", function(){
					$(this).appendTo("#gallery");
					$(this).css("margin-left", "0");
					var curItem = $("#gallery li").eq(1).attr("class");	
					$.get("/AboutContent/"+curItem, function(resp, stat){
						responseCallBack(resp, curItem);
					});
				});
			});

			var fNavWidth = $("#gallery li").first().width();
			var itemCnt = $("#gallery li").length;
			$("#gallery").css("width", (fNavWidth*(itemCnt+1)+"px"));
			var curItem = "level9";
			
			$.get("/AboutContent/"+curItem, function(resp, stat){
				responseCallBack(resp, curItem);
			});
		}
	}
	level9Featured.init();
	var fade = "done";
	
	function setSliderBG(bgItem, first){
		if(first != true){
			first = false;	
		}
		var pageBG = "";
		if(!bgItem.hasClass("selected")){
			$(".featureNavigation a").removeClass("selected");
			bgItem.addClass("selected");
			var activeSlide = bgItem.attr("href");
			pageBG = $(activeSlide).find(".pageBG").text();
			$(".featuredItemsContainer .panel").stop(true, true);
			$(".featuredItemsContainer .panel").fadeOut(650);
			$(activeSlide).fadeIn(650);
		}else if(first){
			var activeSlide = bgItem.attr("href");
			pageBG = $(activeSlide).find(".pageBG").text();
		}
		
		if(pageBG != ""){
			if($(".fullSliderBG:animated").length > 0){
				if(fade == "in"){
					fade = "out";
					$(".fullSliderBG").stop(true, true);
					$(".fullSliderBG").fadeOut(500, function (){
						fade = "in";
						$(".fullSliderBG").css("background-image", "url("+pageBG+")");
						$(".fullSliderBG").fadeIn(500, function(){
							fade = "done";
						});
					});
				}else if(fade == "out"){
					$(".fullSliderBG").stop(true, true);
					face = "in";
					$(".fullSliderBG").css("background-image", "url("+pageBG+")");
					$(".fullSliderBG").fadeIn(500, function(){
						fade = "done";
					});					
				}
			}else if(fade == "done"){	
				fade = "out";
				$(".fullSliderBG").fadeOut(500, function (){
					fade = "in";
					$(".fullSliderBG").css("background-image", "url("+pageBG+")");
					$(".fullSliderBG").fadeIn(500, function(){
						fade = "done";
					});
				});
			}
		}
	}
	
	$(".featureNavigation a").click(function(){
		setSliderBG($(this));
		return false;
	});
	//alert($(".featureNavigation a.selected").length);
	setSliderBG($(".featureNavigation a.selected"), true);
	
	$(".featuredItemsNav .back").click(function(){
		$(".featureNavigation li").first().stop(true, true);
		$(".featureNavigation li").last().css("margin-left", "-240px").prependTo(".featureNavigation");
		$(".featureNavigation li").first().animate({"margin-left":"0"}, 450, "swing");
	});
	$(".featuredItemsNav .forward").click(function(){	
		$(".featureNavigation li").first().stop(true, true);
		$(".featureNavigation li").first().animate({"margin-left":"-=240px"}, 450, "swing", function(){
			$(this).appendTo(".featureNavigation");
			$(this).css("margin-left", "0");
		});
	});

	$("#scrollTo a").click(ss.smoothScroll);
});

function jsonFlickrFeed(x) {
	var y, c;
	var z = document.getElementById("flickrFeed");
	for (var a = 0; a < x.items.length; a++) {
		y = x.items[a];
		c = document.createElement("li");
		c.style.display = "none";
		c.setAttribute("id", "flickrItem"+a);
		c.innerHTML = "<a href=\"" + y.link + "\" target=\"_blank\">" + "<img style=\"width:60px;\" src=\"" + y.media.m.replace(/_m\.jpg$/gi, "_s.jpg") + "\" alt=\"\" /></a>";
		z.appendChild(c);
	}
	var liArr = document.getElementById("flickrFeed").getElementsByTagName("li");
	var listCount = 0;
	var infLoopProtect =0;
	var liList = "";
	var liListArr = new Array();
	while(listCount < 8){
		var id=Math.round(Math.random()*(liArr.length-1));
		if(liList.indexOf("flickrItem"+id+"|") == -1){
			liList += "flickrItem"+id+"|";
			liListArr.push("flickrItem"+id);
			listCount++;
		}
		if(infLoopProtect > 100){
			listCount = 9;
		}
		infLoopProtect++;
	}
	for(i=0;i<liListArr.length;i++){
		document.getElementById(liListArr[i]).style.display = "block";
	}
}

function pullBlogAvatars(){
	$(".blog-post").each(function(){
		var author = $(this).find(".blogAuthor").text();
		var modName = author.toLowerCase().replace(/ /gi,"_");
		$(this).find(".author-pic img").attr("src", "/images/blogAuthors/avatar_"+modName + ".jpg");
	});
}
pullBlogAvatars();


$("#scrollTo").hover(function(){
	$(this).find("a").stop(true,true);
	$(this).find("a").fadeIn(250);
	$(this).find("a").css("display", "block");
},function(){
	$(this).find("a").fadeOut(250);
});
