var mobile_img_loaded = 0;
var files_added = 0; 
var pageURL = window.location.pathname;
$().ready(function() { 
			pageURL = window.location.pathname;
			background_resize();
});				   
$(window).resize(function(){//Adjusts image when browser resized 
			$().ready(function() { 
				 	background_resize();
			});
});

		function background_resize () {
			var screen_width = $(window).width();
			if (screen_width <= 480 ) {
				//line below redundant but necessary for screen refresh pickup of screen width- ??
				screen_width = $(window).width();
				if (files_added == 0) {
					files_added = 1;
					loadjscssfile("http://code.jquery.com/jquery-1.6.4.min.js", "js");
					loadjscssfile("http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js", "js");
					loadjscssfile("http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css", "css");
					
					//$('body').css("background-image","url('http://www.zenmudra.com/rockyDuskWide_zazen_notes_320x180.jpg')");
					//$('body').css("background-repeat", "no-repeat");
					$('.content').css("width","100%");
					$("#home_header1").html("<h1>The Mudra<br> of Zen</h1>").trigger("create");
					$("p").css("text-align", "left")
						  .css("text-indent",".5em");
					$(".like_table_zenmudra").css("padding","10px");
					//$(".like_table_zenmudra").css("margin-left","10px");
					$(".like_table").css("padding","10px");
					$(".like_table").css("margin-top","140px");
					$("#Google_privacy_notice").html("Google SiteSearch<br />privacy notice");
					$("#google_search").css("float","left");
					$("a[href='#']").attr('href', pageURL);
					$("a").attr('data-role', 'button');
					$("a").each(function() {
							var thisHref=this.href;
							$(this).attr("rel","external")
							if (thisHref=='#letters') {
								$(this).click(function(){	
     					  				$('html, body').animate({scrollTop: $("#letters").offset().top}, 2000);
								});
							} else if (thisHref=='#zen_mudra') {
								$(this).click(function(){
									$('html, body').animate({scrollTop: $("#home_header1").offset().top}, 2000);
								});
							} else if (thisHref=='http://www.zenmudra.com/#letters') {
								$(this).attr("href", "http://www.zenmudra.com");
								$(this).click(function(){
									$(".content").live('pageinit',function(event){
										$('html, body').animate({scrollTop: $("#letters").offset().top}, 2000);
									});
								});
							}
					});
				}
			}

			if ((screen_width > 480 )&&(screen_width <= 640 )) {
					if (files_added == 1) {
						removejscssfile("http://code.jquery.com/jquery-1.6.4.min.js", "js");
						removejscssfile("http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js", "js"); 
						removejscssfile("http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css", "css");							
						files_added = 0;
					}
					$('body').css("background-image","url('http://www.zenmudra.com/rockyDuskWide_zazen_notes_640.jpg')");
					$('body').css("background-repeat","no-repeat");
					$('.content').css("width","100%");
					$("p").css("text-align", "justify")
						  .css("text-indent","1em");
					$(".like_table_zenmudra").css("padding","40px 40px");
					$(".like_table").css("padding","40px 40px");
					$(".like_table").css("margin-top","400px");
					$("#home_header1").html("<h1>The Mudra of Zen</h1>");
			}
			if ((screen_width > 640 )&&(screen_width <= 800 )) {
				$('body').css("background-image","url('http://www.zenmudra.com/rockyDuskWide_zazen_notes.jpg')");
					$('.content').css("width","95%");
					$(".like_table").css("margin-top","400px");
					$(".like_table_zenmudra").css("padding","40px 40px");
					$(".like_table").css("padding","40px 40px");
			}
			if ((screen_width > 800 )&&(screen_width <= 1280 )) {
				$('.content').css("width","800px");
				$(".like_table").css("margin-top","400px");
				$(".like_table_zenmudra").css("padding","80px 60px");
				$(".like_table").css("padding","80px 60px");
			}
			if (screen_width > 1280 ) {
				$('body').css("background-image","url('http://www.zenmudra.com/rockyDuskWide_zazen_notes_1920.jpg')");
			}
		}
		 
	// code below from http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml		 
	function loadjscssfile(filename, filetype){
		 if (filetype=="js"){ //if filename is a external JavaScript file
			  var fileref=document.createElement('script');
			  fileref.setAttribute("type","text/javascript");
			  fileref.setAttribute("src", filename);
		 } else if (filetype=="css"){ //if filename is an external CSS file
			  var fileref=document.createElement("link");
			  fileref.setAttribute("rel", "stylesheet");
			  fileref.setAttribute("type", "text/css");
			  fileref.setAttribute("href", filename);
		 }
	 	 if (typeof fileref!="undefined") {
			 document.getElementsByTagName("head")[0].appendChild(fileref);
		 }
	}

	function removejscssfile(filename, filetype){
		 var removedelements=0;
		 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; //determine element type to create nodelist using
		 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"; //determine corresponding attribute to test for
		 var allsuspects=document.getElementsByTagName(targetelement);
		 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
	    	 if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1){
				   allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
				   removedelements+=1;
			 }
		 	 if (removedelements>0) {
				  //alert("Removed "+removedelements+" instances of "+filename);
			}
		 }
	}
