jQuery(function($){

	/* CONTACT FORM DROP  DOWN */
	/*
	var dropDown = 
	{
		init: function()
		{
			// View Locations 
			$(".headerNav li").last().addClass("inquire");
			$("li.inquire a").click(function(){
				var clickedItem = $(this);							 
				$(".actions").slideToggle(1000, function(){
					clickedItem.toggleClass("chosen");
				});
			return false;
			});
		}
	}
	*/
	var services = 
	{
		init: function()
		{
			
		//When page loads...
			//$("ul.services li:first").addClass("active").show(); //Activate first tab
			//$(".servicesContent:first").show(); //Show first tab content

			//On Click Event
			/*
			$("ul.services li").hover(function(){
				//hover in							   
				$(".servicesCo
				  ntentDefault, .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);
				$(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").click(function() {
				$("ul.services li").removeClass("active"); //Remove any "active" class
				$(this).addClass("active"); //Add "active" class to selected tab
				$(".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
	
	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"}, 450, "swing", function(){
					var curItem = $("#gallery li").eq(1).attr("class");
					$(".post-list-about").load("/AboutContent/"+curItem + " .blogContent", function (){
						pullBlogAvatars();																		   
					});
					$(".selectNameAndTitle").load("/AboutContent/"+curItem + " .nameAndTitle");
					$(".selectSideContent").load("/AboutContent/"+curItem + " .side-content");
				});
			});
			$("#gallery-next").click(function(){	
				$("#gallery li").first().stop(true, true);
				$("#gallery li").first().animate({"margin-left":"-=260px"}, 450, "swing", function(){
					$(this).appendTo("#gallery");
					$(this).css("margin-left", "0");
					var curItem = $("#gallery li").eq(1).attr("class");	
					$(".post-list-about").load("/AboutContent/"+curItem + " .blogContent", function (){
						pullBlogAvatars();																		   
					});
					$(".selectNameAndTitle").load("/AboutContent/"+curItem + " .nameAndTitle");
					$(".selectSideContent").load("/AboutContent/"+curItem + " .side-content");					
				});
			});

			var fNavWidth = $("#gallery li").first().width();
			var itemCnt = $("#gallery li").length;
			$("#gallery").css("width", (fNavWidth*(itemCnt+1)+"px"));
			var curItem = "level9";
			$(".post-list-about").load("/AboutContent/"+curItem + " .blogContent", function (){
				pullBlogAvatars();																		   
			});
			$(".selectNameAndTitle").load("/AboutContent/"+curItem + " .nameAndTitle");
			$(".selectSideContent").load("/AboutContent/"+curItem + " .side-content");					
			
		}
	}
	level9Featured.init();
	var fade = "done";	
	$(".featureNavigation a").click(function(){
		if($(this).attr("class") != "selected"){
			$(".featureNavigation a").removeClass("selected");
			$(this).addClass("selected");
			var activeSlide = $(this).attr("href");
			var pageBG = $(activeSlide).find(".pageBG").text();
			$(".featuredItemsContainer .panel").stop(true, true);
			$(".featuredItemsContainer .panel").fadeOut(650);
			$(activeSlide).fadeIn(650);
		}
		
		/*
		if($("#supersize:animated").length > 0){
			if(fade == "in"){
				fade = "out";
				$("#supersize").stop(true, true);
				$("#supersize").fadeOut(500, function (){
					fade = "in";
					$("#supersize img").attr("src", pageBG);
					setImage();
					$('#supersize').resizenow(); 
					$("#supersize").fadeIn(500, function(){
						fade = "done";
					});
				});
			}else if(fade == "out"){
				$("#supersize").stop(true, true);
				face = "in";
				$("#supersize img").attr("src", pageBG);
				setImage();
				$('#supersize').resizenow(); 
				$("#supersize").fadeIn(500, function(){
					fade = "done";
				});					
			}
		}else if(fade == "done"){	
			fade = "out";
			$("#supersize").fadeOut(500, function (){
				fade = "in";
				$("#supersize img").attr("src", pageBG);
				setImage();
				$('#supersize').resizenow(); 
				$("#supersize").fadeIn(500, function(){
					fade = "done";
				});
			});
		}*/
		return false;							   
	});
	$(".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");
		});
	});

	//Resize image on ready or resize
	$.fn.supersize = function() {	
		//Invoke the resizenow() function on document ready
		$(document).ready(function(){
			setImage();
			$('#supersize').resizenow(); 
		});
		
		//Invoke the resizenow() function on browser resize
		$(window).bind("resize", function() {
    		$('#supersize').resizenow(); 
		});
	};
	
	var bgImage = new Image();
	function setImage(){
		bgImage.src = $("#supersize img").attr("src");
	}
	//Adjust image size
	$.fn.resizenow = function() {
		//Define starting width and height values for the original image
		var startwidth = bgImage.width;
		var startheight = bgImage.height;
		//Define image ratio
		var ratio = startheight/startwidth;
		//Gather browser dimensions
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		//Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio) {
		    $(this).height(browserheight);
		    $(this).width(browserheight / ratio);
		    $(this).find("img").height(browserheight);
		    $(this).find("img").width(browserheight / ratio);
		} else {
		    $(this).width(browserwidth);
		    $(this).height(browserwidth * ratio);
		    $(this).find("img").width(browserwidth);
		    $(this).find("img").height(browserwidth * ratio);
		}
		//Make sure the image stays center in the window
		$(this).find("img").css('left', (browserwidth - $(this).width())/2);
		$(this).find("img").css('top', (browserheight - $(this).height())/2);
		
	};

	$("div#supersize").supersize();
	
	ss.addEvent($("#scrollTo a")[0],'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();
