//	custom.js
//	author: stephen mitchell, July 2011


$(document).ready(function(){
	
	$("body").removeClass("no-js").addClass("navattop");
	if($(window).width() <= 480){$("body").addClass("iOS smallscreen");}
	if($(window).width() <= 320){$("body").addClass("iOSportrait");}

	if(Modernizr.touch = "false"){
	// get jquery plugins (use absolute URL to google)
	$.getScript("js/scripts_plugins/jquery.ui.core.js");
	$.getScript("js/scripts_plugins/jquery.ui.widget.js");
	$.getScript("js/scripts_plugins/jquery.ui.mouse.js");
	$.getScript("js/scripts_plugins/jquery.ui.draggable.js");
	$.getScript("js/scripts_plugins/jquery.ui.slider.js");
	$.getScript("js/scripts_plugins/jquery.ui.droppable.js");
	$.getScript("js/scripts_plugins/jquery.ui.resizable.js", resizewrapper);					   
	$.getScript("js/scripts_plugins/jquery.ui.sortable.js");
	}
	
	$.getScript("js/scripts_plugins/jquery.effects.core.js");
	$.getScript("js/scripts_plugins/jquery.effects.highlight.js");

	
	
	// get custom plugins
	$.getScript("js/scripts_default/navigation.js");
	$.getScript("js/scripts_default/footer.js");
	$.getScript("js/scripts_default/header.js");
	$.getScript("js/scripts_default/iframetoggle.js");

	
	// get browser
	if( $.browser.msie){$("html").addClass("msie");}
	if( $.browser.mozilla){$("html").addClass("mozilla");}
	if( $.browser.webkit){$("html").addClass("webkit");}
	if( $.browser.opera){$("html").addClass("opera");}

	var 	screenheight = $(window).height(),
			dir = $("html").attr("dir"),
			$platform = navigator.platform;
			
	if( $platform == "iPhone" || $platform == "iPod"){$("body").addClass("iOS smallscreen "+ $platform +"");}
	
	if( $platform == "iPad"){$("body").addClass($platform);}

	if(dir === "rtl"){$("html").addClass("rtl");}

	else{$("html").addClass("ltr");}

	$("body.no-touch #wrapper").css("min-height", screenheight).addClass("automargin");
	
	$(window).resize(function(){
							  
			 var 	screenheight = $(window).height(),
			 		width = $(window).width(),
					computedpagesize = $("#wrapper").outerWidth();
							
			
			$("body.no-touch #wrapper").css("min-height", screenheight);
		
			// to give fake iOS display on desktop
					if(width <= 480){	$("body").addClass("iOS smallscreen");$("#footer").css("width","100%");}
					if(width > 480){	$("body").removeClass("iOS smallscreen");
										$("#nav dl").css("display","");
										$("#nav h5").text("Open Navigation");
										var computedpagesize = $("#wrapper").outerWidth();
										$("#footer").width(computedpagesize);
										if(computedpagesize <= 480){$("body").addClass("iOS");}
									}
					if($(window).width() <= 320){$("body").addClass("iOSportrait");}
					if(width > 320){$("body").removeClass("iOSportrait");}
	});

	
	$("head script:last").before("<style type=text/css data-themecss=default />");
	$("head style[data-themecss]").load("extra/themes/css/theme-default.css");

	$("#header .hgroup div.section").hide();
	$("#content").addClass("widget-right");
	$("#main").addClass("layout04");
	$("#main .title h1").after("<button class=fileslink>File</button>");
	$("button.fileslink").attr("title","Add this page to the Files section");
	$("#main h3").append("<button class=popup></button><a class=scrolltobottom href=javascript:void(null) data-loadajax=false>&dArr;</a><a class=scrolltotop href=javascript:void(null) data-loadajax=false>&uArr;</a>");
	$("#main h3 a.scrolltotop").attr("title","Previous Section");
	$("#main h3 a.scrolltobottom").attr("title","Next Section");
	$("#main h3 button.popup").attr("title","Toggle Fullscreen");
	$("#main h3:first").find("a.scrolltotop").css("visibility","hidden");
	$("#main h3:last").find("a.scrolltobottom").css("visibility","hidden");
	
	// add dynamic breadcrumb

	var 	$location = location.href,
			$locationsplit = $location.split('/'),
			$splitlength = $locationsplit.length,
			$page = $locationsplit[$splitlength -1],
			$breadcrumb = $page.split('-'),
			$breadcrumb = $breadcrumb[0];
	
			if($breadcrumb == "index.htm"){var 	$breadcrumb = "index";}
	
			$("#main nav.breadcrumb").load("extra/structure-default/breadcrumbs/"+$breadcrumb+".htm", navload);
			
			 function navload(){
			$("#main nav.breadcrumb").find("a[href='"+ $page +"']").wrapInner("<mark />");
			$("#main nav.breadcrumb mark").unwrap();}

// add top banner
$("#header").before("<div id=dynamic-top class=banner />");
// load top banner content
$("#dynamic-top").load("extra/banners/banner-jugheadweb.htm");
	
// to add widget panel		
	var withwidget = $("#wrapper").find("#widget").length;
	
	if(withwidget === 0){$("#main").after("<div id=widget />");
	$("#widget").hide();}

	//add back navigation
	var $thistitle = 	$doctitle = window.document.title,
						$doctitlesplit = $doctitle.split(':'),
						$title = $doctitlesplit[1];
						
	$("#main").after("<nav id=navigate class=navigate> <mark class=current>" + $title + "</mark><a title=Scroll&nbsp;to&nbsp;top class=scrolltotop href=javascript:void(null) data-loadajax=false>&#8593;</a></nav>");
	$("#navigate").addClass("off");	
	
	// link override function
	$("#main a:not( .pagenavigation a, #main a.widget-link, a[data-loadajax='false'], a[data-anchors='true'], a[rel='external']), #nav dd a.dd-link, #dl-footer a:not( #dl-footer a.widget-link), #files  a:not(.clearfiles, a[data-anchors='true'], a[data-loadajax='false']), #navigate a:not(a[data-loadajax='false']), #widget a:not(a[data-loadajax='false'], a[rel='external'])").live("click", linkoverride);

		function linkoverride(e){
					  
				e.preventDefault();
				
				var 
				
				$this = $(this),
				$thisParents = $this.parents("#history").length,
				$thisinnav = $this.parents("#nav").length,
				$thisinnavigate = $this.parents("#navigate").length,
				navigateon = $(this).parents("#navigate.on").length,
				breadcrumb = $(this).parents(".breadcrumb").length,
				inwidget = $(this).parents("#widget").length,
				$thisError = $this.parents("#error").length,
				$thisClass = $this.hasClass("dd-link"),
				$historyClass = $this.hasClass("history"),
				$thisRel = $this.attr("rel"),
				$thistitle = $this.attr("title"),
				$datagetscript = $this.attr("data-getscript"),
				$previousdatagetscript = $("#main article").attr("data-getscript"),
				$thisvalue = $this.attr("href"),
				$previousvalue = $("#main").attr("data-location"),
				$previoustitle = $("#main").attr("data-title"),
				heightbusinessvalue = $("#main").height(),
				$historyexists = $("#nav #history").length,
				$history = "<dt class=off><a href=javascript:void(null) title=History><strong>&raquo;</strong> History</a></dt><dd id=history ><ul /></dd>";
				$this.attr("href", "javascript:void(null)");
				
				if($thisClass == true ){$("#nav dd:visible").slideUp(500);
										$("dt.on").find("a strong").removeClass("rotate");
										}
				
				
				
				
				// get index page dynamic content
				if($thisRel === "index" ){var $thisvalue = "content/index/content.htm";}
	
				$("body").removeClass("bodyfullscreen");
				
				$("#main").height(heightbusinessvalue);
				
				$("#main article").fadeOut("slow", callback);
				
				function callback() {
						
							$.ajax({
								  
										type: "GET",
										url: $thisvalue,
										dataType: "html",
										error: function(xhr, textStatus, errorThrown){
	
											// TO DO: fix
											if(textStatus === null ){
											
												var 	$previousvalue =  $("#main").attr("data-location"),
														$previoustitle = $("#main").attr("data-title");
														
										
												if($previousvalue === undefined){ 
													
													var 	$previousvalue = window.location;}
													
												if($previoustitle === undefined){ 
													
													var 	$doctitle = window.document.title,
															$doctitlesplit = $doctitle.split(':'),
															$previoustitle = $doctitlesplit[1];}
											
												var $error = $("<div id=error><p>Error: " +  $thisvalue + " not found</p><a href=" + $previousvalue + " title=" + $previoustitle + ">Return to previous page</a></div>");
											
												$("#main").find("#loading").remove().end().append($error);
												
												return false;
											}
										},
										success: function(html, textStatus){

											// need to innershiv the loading html for IE
										
										
											$("#main").load($thisvalue +"#main article", change);
											
												function change() {
													
													if($thisRel !== undefined){$.getScript("content/" + $thisRel + "/content.js");}
													
													$("#main").height(heightbusinessvalue).css("overflow-y","hidden");
													
													$("#main").attr("data-location", $thisvalue).attr("data-title", $thistitle);
											
											// load breadcrumbs
											var 	$breadcrumb = $thisvalue.split('-'),
		$breadcrumb = $breadcrumb[0];

		if($thisvalue === "content/index/content.htm" ){ 
												
			var 	$breadcrumb = "index";}
													
					$("#main nav.breadcrumb").load("extra/structure-default/breadcrumbs/"+$breadcrumb+".htm", navload);
			
			 		function navload(){
													
								if($thisvalue === "content/index/content.htm"){$("#main nav.breadcrumb").find("dd a:first").wrapInner("<mark />");}
								else{
									$("#main nav.breadcrumb").find("a[href='"+ $thisvalue +"']").wrapInner("<mark />");}
													
									$("#main nav.breadcrumb mark").unwrap();
												
	}

													
													$("#main article").hide().fadeIn(400, callback);
												
													$("#main .title h1").after("<button class=fileslink>File</button>");
													$("button.fileslink").attr("title","Add this page to the Files section");
											
													$("#main h3").append("<button class=popup></button><a class=scrolltobottom href=javascript:void(null) data-loadajax=false>&dArr;</a><a class=scrolltotop href=javascript:void(null) data-loadajax=false>&uArr;</a>");
													$("#main h3 a.scrolltotop").attr("title","Previous Section");
													$("#main h3 a.scrolltobottom").attr("title","Next Section");
													$("#main h3 button.popup").attr("title","Toggle Fullscreen");
													$("#main  h3:first").find("a.scrolltotop").remove();
													$("#main  h3:last").find("a.scrolltobottom").remove();
													
													if($datagetscript !== undefined ){$.getScript($datagetscript);}
													if($previousdatagetscript !== undefined ){$.getScript($previousdatagetscript);}
													
													function callback() {
														
														var filesexists = $("#files").length;

				if($("body.iOS").length){
					
					if($("#files").length){var mainoffset = $("dt.currentpage").offset().top;}
					
					else{var mainoffset = 0}
				
					$("html,body").animate({scrollTop:mainoffset}, 800);}
														
														// scroll page depending on type of link
														if (breadcrumb == 0 && $thisinnav == 0 && inwidget == 0 && $thisinnavigate == 0){
													
														var mainoffset = 0;
														if(filesexists === 1){var mainoffset = $("dt.currentpage").offset().top;}
														$("html,body").animate({scrollTop:mainoffset}, 600);
														}
														
														if (inwidget == 1 || $thisinnavigate == 1  || $thisinnav == 1 && filesexists == 1){
														
														if(filesexists === 1){var mainoffset = $("dt.currentpage").offset().top;}
														
														else{var mainoffset = 0}
															
																$("html,body").animate({scrollTop:mainoffset}, 600);}
														
														
														$("#main").height("").css("overflow-y","");
								
															if(filesexists === 0){
															
																var 	setheight = $("#main").outerHeight();
															
																		$("#widget:visible").css("min-height", setheight);}
																		
															if(filesexists === 1){
															
																var 	businessheight = $("#main").outerHeight(),
																		filesheight = $("#files").outerHeight(),
																		setheight = businessheight + filesheight;
														
																		$("#widget:visible").css("min-height", setheight);}	
																
												  
		}
		
		// deselect selected tab		
		$("#nav a.dd-link.thispage").removeClass("thispage");
		$("#nav dt.on").removeClass("on").find("a strong").removeClass("rotate");
		// update selected tab
		$("#nav a.dd-link").each(function(i) {
										  
						var $href =  $(this).attr("href");
						
						 if($href === $thisvalue){

							$(this).addClass("thispage");
							$("a.thispage:first").parents("dd").prev("dt").addClass("on").find("a strong").html("&raquo;");
							
							return;
						}
				
		});									
				// add history tab
				if($historyexists == 0 && $thisError == 0){
											
										
											$("#nav dl:first").append($history);
											
											var 	$doctitle = window.document.title,
													$doctitlesplit = $doctitle.split(':'),
													$title = $doctitlesplit[1],
													$location = window.location;
												
										if( $previousdatagetscript === "content/index/content.js"){var  	$location = "content/index/content.htm";}
																						
										
											$("#navigate").prepend("<a class=backlink href=" + $location + " title=" + $title + " data-getscript=" + $previousdatagetscript + ">Previous</a> <span class=pipe> | </span>");
											
											}
											
				if($historyexists == 1 && $thisError == 0){
					
					
											var 	$title = $previoustitle,
													$location = $previousvalue;
												
													
													}
													
				if($previoustitle == undefined){var 	$doctitle = window.document.title,
														$doctitlesplit = $doctitle.split(':'),
														$title = $doctitlesplit[1];
													}
																
				
				if($thisError == 0 && navigateon == 0 )
				{	
					
				// hides duplicates
					$("#history li.visible a").each(function(i) {
											  
						 var $titleexists =  $(this).attr("title");
						 
						 if($titleexists == $title){

							$("#history").addClass("duplicate");
						}
						

					});

					$("#history ul").append("<li class=visible><a href=" + $location + " title=" + $title + " class=dd-link data-getscript=" + $previousdatagetscript + "><span>" + $title + "</span></a></li>");
					
					var $duplicate = $("#history.duplicate").length;
					
					if($thisinnavigate === 0){
					$("#nav li.previous").removeClass("previous");
					$("#nav").find("li:last").addClass("previous");
					$("#navigate.on").removeClass("on").addClass("off");}
					
					if($thisinnavigate === 1){
					$("#nav li.previous").removeClass("previous").prev("li").addClass("previous");
					$("#navigate").removeClass("off").addClass("on");
			
					$("#navigate a.backlink").remove();
					
					var  		$prevlocation = $("#history li.previous a").attr("href"),
								$prevtitle = $("#history li.previous a").attr("title"),
								$prevdatagetscript = $("#history li.previous a").attr("data-getscript");
					
					if( $prevdatagetscript === "content/index/content.js"){var  	$prevlocation = "content/index/content.htm";}
					
					
												
					$("#navigate").prepend("<a class=backlink href=" + $prevlocation + " title=" + $prevtitle + " data-getscript=" + $prevdatagetscript + ">Previous</a>");
					
					if	($prevlocation === undefined){		$("#navigate a.backlink").addClass("hide");
															$("#navigate span.pipe").hide();
														}	
						}
					

					if($previousdatagetscript === "content/index/content.js")
					{$("#nav").find("a:last").attr("rel","index").attr("href","content/index/content.htm");}
					
					if($duplicate === 1){$("#nav li:last").addClass("hidden");}
					
						$("#nav li.hidden").hide().removeClass("visible");
						$("#history").removeClass("duplicate");
						
					if($thisError == 0 && $thisinnavigate == 0 )
						{	
							$("#navigate").find("a:first").removeClass("hide").attr("href", $location).attr("title", $title).attr("data-getscript", $datagetscript);
							$("#navigate span.pipe").show();
							$("mark.current").text($thistitle);

							}
						}

					}
											
										},
										beforeSend: function(){
											
											var loading = $("<div id=loading><p>Please wait: " + $thisvalue +" loading</p></div>");
											
											$("#main").find("#error").remove().end().append(loading);
											
											if(
											   		$("body.iOS").length){$("#nav dl").slideUp();
													$("#nav h5").text("Open Navigation");
											}
										}
										
								});
							
						}
						
	
	
		$this.mouseleave(function(event){
			
			$this.attr("href", $thisvalue);
	
			return false;	
			
			});
	}
// page navigation

$("#main .pagenavigation a, #files .pagenavigation a, .features a, a[data-anchors]").live("click", pagenavigation);	
	
		function pagenavigation(e){

			e.preventDefault();
			
			var 	$anchor = $(this).attr("href"),
					$offset = $(this).parents(document).find($anchor).offset().top,
					popup = $(this).closest("section.popup").length;
		
					if(popup == 0){$("html,body").animate({scrollTop:$offset}, 600);return;}
					
					else{$(this).parents("section:first").removeClass("popup");
					$(document).find($anchor).addClass("popup");return;}
}

$("a.scrolltotop, a.scrolltobottom").live("click", scrollsection);	
	
		function scrollsection(e){
			
			e.preventDefault();
			
			var 
			hasclass = $(this).hasClass("scrolltobottom"),
			fullscreen = $("body").hasClass("bodyfullscreen"),
			iswidget = $(this).parents("#widget").length,
			notdropped = $(this).parents("div.dropped").length;
				
				if(hasclass == false && fullscreen == true){
					$(this).parents("section:first").removeClass("popup").prev("section").addClass("popup");return;}
					
				if(hasclass == true && fullscreen == true){
					$(this).parents("section:first").removeClass("popup").next("section").addClass("popup");return;}
											
				if(hasclass == false){var 	$offsetprevious = $(this).parents("section").prev("section").offset().top;
											$("html,body").animate({scrollTop:$offsetprevious}, 600);}
										
				if(hasclass == true){var 	$offsetnext = $(this).parents("section").next("section").offset().top;
											$("html,body").animate({scrollTop:$offsetnext}, 600);}

	
	return false;				
}

$("#navigate").find("a.scrolltotop").bind("click", scrollwindow);	
	
		function scrollwindow(e){
		
			e.preventDefault();
			
			$("html,body").animate({scrollTop:0}, 1200);
			
			$(this).blur(function(event){
					
					$("html,body").stop();
								  
		});
	return false;				
}


// load themes

	$("#themes a[data-theme], #themes a[data-usertheme]").live("click", function(){

			$("#themes a.thistheme").removeClass("thistheme").siblings("mark").remove();
			$(this).addClass("thistheme").after(" <mark>&radic;</mark>");
			$("#addcss").val("");
});

	$("#themes a[data-theme]").live("click", function(){

			
			var 	theme = 	$(this).attr("data-theme"),
					path = "extra/themes/theme-" + theme + ".js";

			$.getScript(path);
			
			$("#themetitle").val(theme);
			$("#theme-resetcss").show();
			
			$("#loadthemes, #deletethemes").find("option:first").attr("selected", "selected");

});
			
	$("#themes a[data-usertheme]").live("click", function (){


			var 	theme = $(this).attr("data-usertheme", theme),
					val = $("div[data-themecss="+theme+"]").text(),
					logo = $("div[data-themecss="+theme+"]").attr("data-logo"),
					footer = $("div[data-themecss="+theme+"]").attr("data-footer"),
					logosrc = $("div[data-themecss="+theme+"]").attr("data-logosrc");	
				
				$("head style[data-themecss]").text(val).attr("data-themecss",theme).removeAttr("media");
				$("#themetitle").val(theme);
				$("#loadthemes, #deletethemes").find("option[value='"+theme+"']").attr("selected", "selected");
				$("#a-banner img").attr("src", logosrc).removeClass().addClass(logo);
				
				
				$("#footer").removeClass().addClass(footer).find("img").each(function(i) {

							var 	$src = $(this).attr("src"),
									$srcsplit = $src.split('_'),
									$oldsrc = $srcsplit[0],
									newsrc = $oldsrc + "_" + footer +".png";
					
							$(this).attr("src", newsrc);
			});	

				if($("#tools").length){$.getScript("extra/tools/js/update.js");}
});
	
	// files
	$("#main .fileslink").live("click", files);
	
		function files(e){
		
		var 
				filesdata = $("#main").html(),
				layout = $("#main").attr("class"),
				filestitle = $(this).prev().text(),
				filesexists = $("#files").length,
				fontslider = $("#fontslider:visible").length;
		
				e.preventDefault();
				
				if(filesexists === 0){$("#main-holder").prepend("<div id=files><dl><dt class=fileslabel>Files</dt><dt class=currentpage>Current Page</dt></dl><a class=clearfiles>Remove files</a></div>");}
				
				if(fontslider === 1){var $fontsize = $("#fontsize").attr("value", $fontsize);$("#files").css("font-size", $fontsize);}

				
				$("#files:hidden").slideDown(300, function(){var 	businessheight = $("#main").height(),
																	filesheight = $("#files").outerHeight(),
																	setheight = businessheight + filesheight;
									
													$("#widget:visible").css("min-height", setheight);});
				
				$("#files dl dt:first").after("<dd class=filesholder><div class=fileswrap><div class=bar><span class=filestitle><span class=button>+</span><span class=plus /></span><img src=img/img-default/bg/img-bg-jewel.png alt=close title=Close class=close></div>" + filesdata +"</div></dd>");
				
				$("#main").find("textarea").each(function(i) {
								
						var id	 = 	$(this).attr("id"),
							val	 = 	$(this).val();
						
							$("#files dd:first").find("#"+ id).val(val);
				 });
				
				$("#files button.fileslink").hide();
				$(this).addClass("hide");
				
				$datagetscript = $("#files dl dd:first article").attr("data-getscript");
				
				if($datagetscript !== undefined ){$.getScript($datagetscript);}
				
				$("#files dl:first").sortable({
											items: "dd",
											handle: ".bar",
											cancel:".plus, .minus",
											});
				
				$("#files dl dd:first")
											.find("span.plus").text(filestitle).end()
											.find("article").hide();
											
				$("#files").removeClass().addClass(layout);
									
			$("#widget:visible").css("min-height", "");						
			
			var 	businessheight = $("#main").height(),
					filesheight = $("#files").outerHeight(),
					setheight = businessheight + filesheight;
									
			$("#widget:visible").css("min-height", setheight);
			
		// animate to #files pos top
		var 	$offset = $("#files").offset().top;
				$("html,body").animate({scrollTop:$offset}, 600);
			
									
				$("#files img.close").click(function(e){	
												
					$(this).parents("dd").remove();
					
					var filescontent = $("#files dd").length;
					if(filescontent == 0){$("#files").remove();}
					
					$("#widget:visible").css("min-height", "");
					
					var setheight = $("#main-holder").outerHeight();
					
					$("#widget:visible").css("min-height", setheight);
					
					e.stopImmediatePropagation();
									
		});
				$("#files").find("a.clearfiles").live("click", removefiles);
				
				function removefiles(e){
		
							$("#files").remove();
							
							filesexists = $("#files").length;
							
							if(filesexists === 0){
							
								var 	setheight = $("#main").outerHeight();
							
										$("#widget:visible").css("min-height", setheight);}
							
							e.stopImmediatePropagation();
					
						}
						
				$(".filestitle span").bind("click", togglefiles);

					function togglefiles(e){
				
							var 
									$element = $(this),
									imgclass = $element.attr("class"),
									$togglefiles = $element.parents(".fileswrap").find("article"),
									$toggleopen = $element.parents("#files").find("article:visible");
									
									if(imgclass === "plus"){$element.prev("span.button").text("-").next("span").removeClass("plus").addClass("minus");}
									if(imgclass === "minus"){$element.prev("span.button").text("+").next("span").removeClass("minus").addClass("plus");}
									
									$togglefiles.animate({
							 									height:"toggle",
																opacity:"toggle"
							 									}, "slow", callback);
									function callback() {
													
														var 	
														
														businessheight = $("#main").outerHeight(),
														filesheight = $("#files").outerHeight(),
														setheight = businessheight + filesheight;
														
														$("#widget:visible").css("min-height", setheight);																											
					
														}
									$toggleopen
												.slideUp("slow")
												.parents(".fileswrap")
												.find("span.button")
												.text("+");
											
									$toggleopen.parents(".fileswrap").find("span.minus").removeClass("minus").addClass("plus");
									
									
									var 	$offset = $("#files").offset().top;
											$("html,body").animate({scrollTop:$offset}, 600);

									e.stopImmediatePropagation();
	}
	
	$("a.dd-link, #dl-footer a, #files  a:not(.clearfiles, #files .pagenavigation a, a[data-loadajax='false'], a[data-anchors]), #navigate a:not(a[data-loadajax='false']),button.fileslink").live("click", closefiles);
	
		function closefiles(e){

			$("#files").find("article:visible").slideUp(400);
			$("#files").find("span.minus").removeClass("minus").addClass("plus").prev("span.button").text("+");

		}	
	}

		
	$("a:not(#history a)").live("click", cleanhistory);	
	
		function cleanhistory(e){
			$("#history dd a").css("outline","");
		}
		
	$("#header, #main-holder, #footer:not(body.iOS #footer), #widget, #tools").live("click", closetabmenus);	
	
		function closetabmenus(e){
			
			$("dt.on").find("a strong").removeClass("rotate").end().next("dd:visible").slideUp(500);
		
			e.stopPropagation;
		}
		
// files
	$("#navigate a.backlink").live("click", navigate);
	
		function navigate(e){
			
			// animate this
			//$("html,body").animate({scrollTop:0}, 600);
			
			$("dt.on").find("a strong").removeClass("rotate").end().next("dd:visible").slideUp(500);
		
			var  	$datagetscript = $(this).attr("data-getscript"),
					navigateoff = $(this).parents("#navigate.off").length,
					$title = $(this).attr("title");
		
					$("mark.current").text($title);
					
					if(navigateoff === 0){
					if($datagetscript !== undefined ){$.getScript($datagetscript);}
					
					$("#history li.previous").removeClass("previous").prev("li").addClass("previous");
				
					var  		$prevlocation = $("#history li.previous a").attr("href"),
								$prevtitle = $("#history li.previous a").attr("title"),
								$prevdatagetscript = $("#history li.previous a").attr("data-getscript");
					
			
							
					if	($prevlocation === undefined){		$("#navigate a.backlink").addClass("hide");
															$("#navigate span.pipe").hide();
															return false;}	
				else{	


					$("#navigate a.backlink").remove();
					
					if( $prevdatagetscript === "content/index/content.js"){var  	$prevlocation = "content/index/content.htm";}
					
					$("#navigate").prepend("<a class=backlink href=" + $prevlocation + " title=" + $prevtitle + " data-getscript=" + $prevdatagetscript + ">Previous</a>");
					
					
		}}
			return false;	
	//}				
}
	
// end navigate
		
	//$("a mark").live("click", nolink);

	//	function nolink(e){
	//			e.preventDefault();
		//		e.stopPropagation();
		//		return false;}
	
	// remove input error class
	$("input.error").live("click keydown", removerror);	
		function removerror(){$(this).removeClass("error");}
	
	// resize #wrapper
	function resizewrapper(){

		$("#wrapper").resizable({
								handles:"e",
								autoHide: true,
								start: function( event, ui ) {
										
										$("div.widgetholder:not(div.dropped)").each(function(i){
																						 
											var		$this = $(this),
													scrolltop = $("html").scrollTop(),
													offset = $this.offset(),
													offsettop = offset.top - scrolltop;
													$this.css({		"top":offsettop,
																	"left":offset.left,
																	"position":"fixed" 		});
											});
										
										$("div.hidehandle").addClass("resizing").after("<div class=resizing-helper>Page Width: px</div>");

										
								},
								stop: function(event, ui) {
									
									var 	$computedfontsize = parseFloat($("#wrapper").css("font-size")),
											width = $("#wrapper").width(),
											$width = width / $computedfontsize,
											$width = $width + "em";
										
									$("#footer").width(width);
									$("#wrapper").height("").width($width);
									$("#pagewidth").val(width +"px");
									$("div.resizing.").removeClass("resizing");
									$("div.resizing-helper").remove();
									
									$("div.widgetholder:not(div.dropped, div.fixed)").each(function(i){
																						 
											var		$this = $(this),
													offsetparent = $("#main-holder").offset().top,
													offsetwidget = $("#widget").offset().left,
													offset = $this.offset(),
													offsettop = offset.top - offsetparent,
													offsetleft = offset.left - offsetwidget;

													$this.css({		"top":offsettop,
																	"left":offsetleft,
																	"position":"absolute" 		});
											});
									
							}
		
	}).find("div.ui-resizable-e:last").addClass("hidehandle").attr("title","Drag to resize page width");	
		
	}
	
	$("#wrapper").resize(function(event, ui) {
															
								var 		$computedfontsize = parseFloat($("#wrapper").css("font-size")),
											width = $("#wrapper").width(),
											$width = width / $computedfontsize,
											$width = $width + "em";
						
					$("#footer").css("width",width);
					$("#pagesize").next("span").text(" [" + width +"px]");
					// fix
					$("dt.on").find("a strong").removeClass("rotate").end().next("dd:visible").slideUp(500);
					$("#pagewidth").val(width +"px");
					
					$("div.resizing-helper").text("Page Width: " + width +" px");
					
					// to give fake iOS display on desktop
					if(width <= 480){$("body").addClass("iOS");}
					if(width > 480){$("body").removeClass("iOS");$("#nav dl").css("display","");$("#nav h5").text("Open Naviagtion").next("section").css("font-size","");}
					if(width == 320){$("body").addClass("iOSportrait");}
					if(width > 320){$("body").removeClass("iOSportrait");}
					
					// need to resize #widget height
							
							if(! $("#files").length){
										
								var 	setheight = $("#main").outerHeight();
										
										$("#widget:visible").css("min-height", setheight);}
													
							else{
										
								var 	businessheight = $("#main").outerHeight(),
										filesheight = $("#files").outerHeight(),
										setheight = businessheight + filesheight;
									
										$("#widget:visible").css("min-height", setheight);}
										
			});
	
	$(window).unload( function(){$(window).scrollTop(0);});	

	// to open links in another window	
	$("a[rel='external']").live("click", blanklink);


		function blanklink(e){

			e.preventDefault();
			window.open(this.href);
	
			return false;
 }
 	
// to open section as fullscreen
$("#main-holder h3 button.popup").live("click",function(e){
	
	$(this).parents("section:first").toggleClass("popup");
	if($(this).parents("h3").hasClass("off")== false){$("body").toggleClass("bodyfullscreen");}
	
	e.preventDefault();
	e.stopPropagation();
	e.stopImmediatePropagation();
	
	return false;
	
});

$("video").live("dblclick",function(){
	
	//var src = $(this).find("source").attr("src");
	//window.open(src,'','scrollbars=no,width=800,height=" +screen.height+",left=0,top=0');
	
	$(this).toggleClass("fullscreen");
	$("#videoplayer video").unwrap();
	$("video.fullscreen").wrap("<div id=videoplayer />");
	$("#footer, #tools").toggle();
});

$("video").live("hover",function(){

	$(this).attr("title", "Double click to toggle fullscreen");
});
 
// Test for position:fixed support
//Modernizr.addTest('positionfixed', function () {
 //   var test  = document.createElement('div'),
 //     control = test.cloneNode(false),
 //        fake = false,
 //        root = document.body || (function () {
 //           fake = true;
 //           return document.documentElement.appendChild(document.createElement('body'));
 //     }());

 //  var oldCssText = root.style.cssText;
 //  root.style.cssText = 'padding:0;margin:0';
 //  test.style.cssText = 'position:fixed;top:42px';
 //  root.appendChild(test);
//   root.appendChild(control);
   
 //  var ret = test.offsetTop !== control.offsetTop;
   
 //  root.removeChild(test);
 //  root.removeChild(control);
 //  root.style.cssText = oldCssText;
   
//   if (fake) {
 //     document.documentElement.removeChild(root);
 //  }
   
 //  return ret;
//});



});		
