(function(){
	var $ = jQuery,
		cleanline = window.cleanline || (window.cleanline = {}),
		site = cleanline.site = {};
	
	site.paths = {};
	site.paths.root = window.location.protocol+"//"+window.location.host+"/";
	site.paths.theme = "/themes/site_themes/project/";

	site.paths.segments = (window.location.pathname).replace(/(\/$)/,"").split(/\/+/);

	
	site.pages = {};
	
	
	var $html, $head, $body, $bg, $wrapper, $header, $nav, $page, $footer;
	
	site.pages["common"] = new esf.site.Page({
		test:function(){return false;}, //to hardset a page to display, redefine this to "true". See embeds/footer_assets template.
		init:function(){
			var I = this;

            $html = $("html");
            $head = $html.children("head");
            $body = $html.children("body");
            $bg = $("#bg");
            $wrapper = $("#wrapper");
            $header = $("#header");
            $nav = $("#nav");
            $page = $("#page");
            $footer = $("#footer");
			
			//Add clearfix classes. We do this with JS so the HTML isn't cluttered with them.
			$header.add($page).addClass("clearfix");

            //Add IE browser class
            if ($.browser.msie) {
                $html.addClass("ie" + $.browser.version.replace(/(\d+).*/, "$1"));
            }
			
			//Prep the navigation
			I.prepSubNav();
			
			//Create posts out of anchor posts
			$("a[data-post]").anchorPost();
			
			//Create banners
			I.prepBanners("#banner");
			
			//Create definition list accordions
			I.prepDefListAccordions("dl.accordion");
			
			//Create sided tabsets
			I.prepDefListTabsets("dl.tabset");
			
			//Prep admin actions
			I.prepAdminActions();
			
			//Prep the captioned images 'n such
			I.prepCaptions("img.caption");
			
			
			//Create placeholders in textboxes
			$("input[placeholder]").placeholder();
			
			//Setup overlays/lightboxes			
			$page.find("a[rel='overlay']").each(function(){
				var $a = $(this),
					tipText = $a.attr("tip") != null ? $.trim($a.attr("tip")) : "Enlarge",
					$zoomTip = $("<span class='tip'>"+ tipText +"</span>");
					
					if(tipText.length){
						$zoomTip.appendTo($a);
					}
					
			}).colorbox({transition:"none", maxWidth:$(window).width(), maxHeight:$(window).height(), scalePhotos:true});
			
			//Setup image maps
			$("img.mapster,[data-mapster]").each(function(){
				var $img = $(this),
					optsStr = $img.attr("data-mapster"),
					opts = {fillColor:"999999", fillOpacity:.4};
				if(optsStr){
					try{
						opts = eval("("+optsStr+")");
					}catch(err){
						
					}
				}
					
				$img.mapster(opts);
			});
			
			//Add the ready class so the CSS shows the body
			$html.addClass("ready");
		},
		
		prepSubNav:function(){
			var subNav = $("#subNav");
			//Make all non-linked items use their first child's url, or their nearest parent's.
			subNav.find("li").not(":has(>a:first-child)").each(function(l, li){
				var $li = $(li),
					label = $li.contents().first().remove().text(),
					link = $li.find("a[href]:first");
					
				if(!link.length){
					link = $li.closest("li").find("a[href]:first");
				}
				
				link.clone().prependTo($li).empty().text(label);
			});
		},
		
		prepBanners:function(elements){
			$(elements).each(function(e, el){
				var $wrap = $(this),
					$banners = $wrap.children("ul.banners"),
					$controls = $wrap.children("ul.controls");
					
				$banners.cycle({
					fx: "fade",
					prev:$controls.children(".prev"),
					next:$controls.children(".next"),
					timeout:6000,
					slideResize:false,
					height:"auto",
					before:function(currSlide, nextSlide, options, forwardFlag){
						//console.log(arguments);	
						var index = $.inArray(nextSlide, $banners.children("li"));
						$controls.children(".state").find(".current").html(index+1);
					}
				});
				//Set status total slides
				$controls.children(".state").find(".total").html($banners.children("li").length);
				
			});
			return this;
		},
		
		prepDefListTabsets:function(elements){
			var $dls = $(elements);
			
			$dls.each(function(){
				var $tabset = $("<div class='tabset clearfix'><ul/></div>"),
					$tabsUl = $tabset.children("ul"),
					$dl = $(this),
					$dts = $dl.children("dt"),
					$dds = $dl.children("dd"),
					$tabsUlLi = $(),
					$tabContents = $(),
					$adminActions = $dl.children(".admin-actions");

				$tabset.addClass($dl.attr("class"));
				$tabset.prepend($adminActions);
				$tabset.insertAfter($dl);
				//$dl.addClass("printonly");
				$dl.remove();
				
				$dts.each(function(t, dt){
					var $dt = $(dt),
						$dd = $dt.next("dd"),
						$li = $("<li>"+$(dt).html()+"</li>"),
						$div = $("<div>"+$dd.html()+"</div>");
						
					$tabsUl.append($li);						
					$tabset.append($div);				
					$li.data("tabContent", $div);
					$div.data("tabLi", $li);
					
					$tabsUlLi = $tabsUlLi.add($li);
					$tabContents = $tabContents.add($div);
				});
				
				
						

				$tabsUlLi.click(function(evt){
					
					//$tabContents.hide();
					var $li = $(this),
						$div = $li.data("tabContent");
					
					$tabContents.stop();
					$div.fadeIn("fast",function(){
						$div.css("display","");	
					});
					
					//$tabContents.not($div.get(0)).fadeOut();
					
					$tabsUlLi.not($li).add($tabContents.not($div)).removeClass("opened").addClass("closed");
					$li.add($div).removeClass("closed").addClass("opened");
					//$div.show();
					//$tabset.height($div.height());
				});
				
				//Show the first item
				$tabsUlLi.first().click();
				
			});
			
			
			
			return this;
		},
		
		prepCaptions:function(elements){
			$(elements).each(function(){
				var $el = $(this),
					caption;
				if($el.is("[title],[alt]") && !$el.next("*").is(".caption")){
					caption = $el.attr("title") || $el.attr("alt");
					if(caption.length){
						var $caption = "<span class='caption'>"+caption+"</span>";
						$el.after($caption);
					}
				}
			});
		},
		
		prepDefListAccordions:function(elements){
			$(elements).each(function(){
				var $accordion = $(this),
					$dts = $accordion.children("dt"),
					$dds = $accordion.children("dd");
					
				$dts.add($dds).addClass("closed");
				//$dds.hide();
				
				$dts.click(function(evt){
					var $dt = $(this),
						$dd = $dt.next("dd");
					
					$dds.not($dd).slideUp("fast", function(){$(this).css("display","");});
					$dd.slideDown("fast", function(){$(this).css("display","");});	
					
					$dts.add($dds).removeClass("opened").addClass("closed");
					$dt.add($dd).removeClass("closed").addClass("opened");
									
				});
			});
			return this;
		},
		
		prepAdminActions:function(){
			var $actions = $(".admin-actions");
			
			$actions.each(function(d, div){
				var $div = $(div),
					scope = $div.parent().addClass("admin-action-scope");
				
					
				$div.children(".edit").button({
					text:false,
					icons:{primary:"ui-icon-pencil"}
				});
				
				$div.buttonset();
			});
			
			return this;
		}
		
		
	});
	
	
	
	
	
	
	
})();
