window.onload = getPage();

var cfile='';
function hashChange(){
	//alert(hc+"|"+location.hash);
	//var hasht = 'page.php?page='+location.hash.replace(/\#/,'');
	//if(cfile != hasht){
		grabFile(hasht);
	//} 
}
 
function detectMobile(){
	var uagent = navigator.userAgent.toLowerCase();
   if (uagent.match(/(iphone|ipod|android)/gi)){
      return true;
   }else{
      return false;
   }
}

function getPage() {
	var str = location.href;
	if(str.match(/#.+/gi)){
		tpg = str.split(/#/gi);
		tdom = tpg[0].split(/\//gi);
		if(tdom[2]=="jamfor.us"){
			tusr = tdom[3] + '/'; 	
		}else{
			tusr = "";	
		}
		
		hc = tpg[1];
		
		if(tpg[1]=="home"){tpg[1]='';}
		
		//if(detectMobile()==true){ m="m.";}
		
		location.replace('http://' + tdom[2] + '/' + tusr + tpg[1] );

		return;
	}else if(str.match(/(404)/gi)){
		grabFile('page.php?page=404'); 
		zin();
		return;
	}
	prepareForm();		
}

function show(show){
	location.href = "#"+show;
	grabFile('page.php?page='+show);		
}

function song(song){
	location.href = "#song/"+song;
	grabFile('page.php?page=song&song='+song);		
}

function zin(){
	document.getElementById("header").style.zIndex= 10;
	document.getElementById("main").style.zIndex= 0;	
}

function onsub() {
    var data = "";
    for (var i=0; i<this.elements.length; i++) {
      data+= this.elements[i].name;
      data+= "=";
      data+= escape(this.elements[i].value); 
      data+= "&";
    }
    return !sendData(data);
}
function onsub2(frm) {
	if(frm.id =="del"){
		var r=confirm("Are you sure you want to cancel this show");;
		if (r==false){
			return false;
		}
	}
    var data = "";
    for (var i=0; i<frm.elements.length; i++) {
      data+= frm.elements[i].name;
      data+= "=";
      data+= escape(frm.elements[i].value); 
      data+= "&";
    }
	
    return !sendData(data);
}

function prepareForm() {
	
  if(!document.getElementById) {
    return;
  }
  if(!document.getElementById("frm")) {
	
    return;
  }  
  document.getElementById("frm").onsubmit = onsub();
  
}
function sendData(data) {
  var request = getHTTPObject();
  //alert(data);
  
  switch(data.substring(0,5)){
	case "email":
	  	page = "email";
		break;
	case "title":
		page = "shorter";
		break;
	case "mails":
		page = "send";
		break;
	case "btitl":
		page = "blog";
		break;
	default:
		page = "showinfo";
		break;
  }
  
  
  if (request) {
	  displayLoading(document.getElementById("main"));
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("POST", "page.php?page=" + page, true );
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(data);
	
    return true;

  } else {
    return false;
  }
}
function grabFile(file) {
	cfile = file;
	tmp = file.split(/=/gi);
	googAn(tmp[1]);
	tmp[1].replace(/\//," ");
	document.title = document.domain + " :: " + tmp[1];
	//pgCh(tmp[1]);
	if(tmp[1]=="home"){
		//document.getElementById("contentFlow").style.display = 'inline';
	}else{
		//document.getElementById("contentFlow").style.display = 'none';
	}
	
  var request = getHTTPObject();
  if (request) {
    displayLoading(document.getElementById("main"));
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("GET", file, true);
    request.send(null);
	if(tmp[1]=="home"){setTimeout("flow()",2000);}
    return true;
  } else {
	window.location = tmp[1];
    return false;
  }
}

function parseResponse(request) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
      var details = document.getElementById("main");
      details.innerHTML = request.responseText;
      parseScript(request.responseText);
    }
  }
}

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

function displayLoading(element) {
  while (element.hasChildNodes()) {
    element.removeChild(element.lastChild);
  }
  var image = document.createElement("img");
  image.setAttribute("src","/loading.gif");
  image.setAttribute("alt","Loading...");
  element.appendChild(image);
}


function parseScript(_source) {
		var source = _source;
		var scripts = new Array();
		
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
			
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		
		// Return the cleaned source
		return source;
}
