// JavaScript Document
String.prototype.toProperCase = function(){
     return this.toLowerCase().replace(/\w+/g,function(s){
          return s.charAt(0).toUpperCase() + s.substr(1);
     })
}
var js_global = {
	username: '',
	RF: ''
}
var image1 = new Image;
image1.src = '/images/ajax-loading.gif';
var image2 = new Image;
image2.src = '/images/add.png';

function ajaxloading(elem) {
	e(elem).innerHTML = "<img src='/images/ajax-loading.gif' style='float: left;' />Your content is currently being requested.";
}
function es(tag) {return document.getElementsByTagName(tag);}
function e(id) {return document.getElementById(id);}
//function $(id) {return document.getElementById(id);}
function changedata(id, type, dest) {
	js_global.RF = type + "_" + id;
	sendRequest("/user_space.php?gallery=" + id + "&change=" + type + "&to=" + dest, checkit);
}
function properties(gallery){
	js_global.RF = gallery;
	ajaxloading('properties_' + gallery);
	sendRequest("/user_space.php?properties&gallery=" + gallery, load_properties);
}
function close_properties(that) {
	if(!isNaN(that.id)) {
		js_global.RF = that.id;
	}
	e('properties_' + js_global.RF).innerHTML = '';
	sendRequest("/user_space.php?loadsingle=" + js_global.RF, function(result){ e('album_' + js_global.RF).innerHTML = result.responseText;});
}
function load_properties(result) {
	e('properties_' + js_global.RF).innerHTML = result.responseText;
}
function update_properties(gallery) {
	js_global.RF = gallery;
	sendRequest("/user_space.php?update=" + gallery + "&title=" + e('title' + gallery).value + "&description=" + e('description' + gallery).value, close_properties);
}
function checkit(result) {
	if(result.responseText != 'OK') {
		alert("ERROR!" + result.responseText);
	}
	var updated = js_global.RF.split("_");
	if(updated[0] == "public"){
		if(e('album_' + updated[1]).className != 'hidden'){
			e('album_' + updated[1]).className = 'hidden';
		} else {
			e('album_' + updated[1]).className = ''
		}
	}
	
}
//-----------------------------------------------------------------------------------------------
//		Author: PPK
//-----------------------------------------------------------------------------------------------
function sendRequest(url, callback, postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? 'POST' : 'GET';
	req.open(method, url, true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		// 20080702 added check for callback
		if (callback){
			callback(req);
		}
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject('Msxml2.XMLHTTP')},
	function () {return new ActiveXObject('Msxml3.XMLHTTP')},
	function () {return new ActiveXObject('Microsoft.XMLHTTP')}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0, l=XMLHttpFactories.length; i<l; i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		} catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function findPos(obj,smax) {
	var curleft = curtop = count = 0;
	if (smax=='abs' && obj.offsetParent){
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	} else if (parseInt(smax)>0 && obj.offsetParent){
		do {
			if (count<smax){
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
			count++;
		} while (obj = obj.offsetParent);
	} else {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft, curtop];
}
function hintschool(result){
	var list = result.responseText.split(":");
	if (e('dropmenu')){
		var div = document.getElementById('dropmenu');
		div.parentNode.removeChild(div);
	}
	var data = '';
	for(var i = 0; (i < list.length && i < 10); i++) {
		data += "<span id=\"" + list[i] + "\">" + list[i] + "</span><br />";
	}
	if(list.length > 10) {
		var data2 = "<span>" + list.length - 10 + " more matches.</span>";
	}
	var loc = findPos(e(js_global.RF));

	var newdiv = document.createElement('div');
	newdiv.id = 'dropmenu';
	newdiv.className = 'dropmenu';
	var spot_height = e(js_global.RF).offsetHeight;
	newdiv.style.top = (loc[1]+spot_height) + 'px';
	newdiv.style.left = (loc[0]) + 'px';
	document.body.appendChild(newdiv);
	newdiv.innerHTML = data;
	var newspans = es('SPAN');
	var gallery_id = js_global.RF.split("_");
	for(var i = 0; i < newspans.length; i++){
		if(newspans[i].id && newspans[i].parentNode.id == 'dropmenu') {
			newspans[i].onclick = function() {
				js_global.RF = "spacerfill_" + gallery_id[1];
				sendRequest("/user_space.php?mod=add&school=" + this.id + "&gallery=" + gallery_id[1], update_schools);
				this.parentNode.parentNode.removeChild(this.parentNode);
			}
		}
	}
}
function update_schools(result){
	var album = js_global.RF.split("_");
	e(js_global.RF).innerHTML = "<strong class='label'>Schools:</strong> " + result.responseText;
	change_ems();

}
function lookup(that){
	if(that.value != '') {
		var field = that.id.split("_");
		js_global.RF = that.id;
		sendRequest("/ajax/lookup.php?db=d3sports&tab=school_list&school=" + that.value, hintschool);
	}
}
function lookup_abbrev(that, dest){
	if(that.value != '') {
		if(that.id){
			var field = that.id.split("_");
			js_global.RF = that.id;
			var item_content = that.value;
		} else {
			var item_content = that;
		}
		if(e(dest).tagName != 'INPUT') {
			sendRequest("/ajax/lookup.php?db=d3sports&tab=school_list&abbrev=" + item_content, function(result){ e(dest).innerHTML = result.responseText; });
		} else {
			sendRequest("/ajax/lookup.php?db=d3sports&tab=school_list&abbrev=" + item_content, function(result){
					var thisisit = result.responseText.split("&&");
					e(dest).value = thisisit[0];
					if(thisisit.length > 1) {
						e(dest).removeAttribute('readonly');
					}
				}
			);
		}
	}
}

function lightbox(url){
	if(!e('lightbox_back')) {
		var newdiv = document.createElement('div');
		newdiv.id = 'lightbox_back';
		var newdiv2 = document.createElement('div');
		newdiv2.id = 'lightbox_front';
		document.body.appendChild(newdiv);	
		document.body.appendChild(newdiv2);
	}
	sendRequest(url, function(result) { e('lightbox_front').innerHTML = result.responseText; });
	if(!e('lightbox_back')) {
		var button = document.createElement('button');
		button.innerHTML = "<h3>Remove Window<\/h3>";
		button.onclick = function() {
			document.removeChild(e('lightbox_back'));
			document.removeChild(e('lightbox_front'));
		}
		newdiv.appendChild(button);
	}
}
function reload_list(dir){
	ajaxloading('file_tree');
	if(!dir){var dir = '';}
	sendRequest("/user_space.php?browse=" + dir, function(result) { e('file_tree').innerHTML = result.responseText; });
}
function create(value){

	var work_space = e('lightbox_front');
	if(!e('gallery_box')){
		var work_div = document.createElement('div');
		work_div.id = "gallery_box";
		work_space.appendChild(work_div);
	} else {
		var work_div = e('gallery_box');
	}
	work_div.innerHTML = "This is where albums are made<br \/>";
	value = value.replace("%2F", "/", "g");
	var breakdown = value.split("/");
	var game = breakdown[(breakdown.length)-1].split("-");
	if(!e('gallery_date')){
		
		//Create Date Field
		var label1 = document.createElement('label');
		label1.innerHTML = 'Date:';
		work_div.appendChild(label1);
		var input1 = document.createElement('input');
		input1.type = 'text';
		input1.id = 'gallery_date';
		input1.readOnly = 'readonly';
		work_div.appendChild(input1);

		//Create Title Field
		var label1 = document.createElement('label');
		label1.innerHTML = 'Sport:';
		work_div.appendChild(label1);
		var input1 = document.createElement('input');
		input1.type = 'text';
		input1.id = 'gallery_sport';
		input1.readOnly = 'readonly';
		work_div.appendChild(input1);

		//Create away_team Field
		var label1 = document.createElement('label');
		label1.innerHTML = 'Away: ';
		work_div.appendChild(label1);
		var input1 = document.createElement('input');
		input1.type = 'text';
		input1.id = 'away_team';
		input1.readOnly = 'readonly';
		work_div.appendChild(input1);

		//Create home_team Field
		var label1 = document.createElement('label');
		label1.innerHTML = 'Home: ';
		work_div.appendChild(label1);
		var input1 = document.createElement('input');
		input1.type = 'text';
		input1.id = 'home_team';
		input1.readOnly = 'readonly';
		work_div.appendChild(input1);

		//Create photographer Field
		var label1 = document.createElement('label');
		label1.innerHTML = 'Shooter: ';
		work_div.appendChild(label1);
		var select1 = document.createElement('select');
		select1.id = 'photographer';
		work_div.appendChild(select1);
		sendRequest("/ajax/lookup.php?shooter_list=1", function(result) {
				var list = result.responseText.split("&&");
				for(var i = 0; i < list.length; i++) {
					select1.add(new Option(list[i], list[i]),  null);
				}
			});

		//Create description Field
		var label1 = document.createElement('label');
		label1.innerHTML = 'Extra Descr: ';
		work_div.appendChild(label1);
		var input1 = document.createElement('textarea');
		input1.id = 'description';
		work_div.appendChild(input1);
		
		//Create submit Field
		var submit1 = document.createElement('input');
		submit1.id = 'creategallbutton';
		submit1.type = 'button';
		submit1.onclick = function() {
			var iframe1 = document.createElement('iframe');
			var title = e('gallery_sport').value + ': ' + e('home_team').value + ' vs. ' + e('away_team').value;
			var description = (e('description').value != '') ? e('photographer').value + '\n' + e('description').value : e('photographer').value ;
			var src = '/create_gallery.php?date=' + e('gallery_date').value + '&title=' + title + '&description=' + description + '&category=' + e('gallery_sport').value + '&schools=' + e('home_team').value + ';+' + e('away_team').value + ';+&path=' + value;
			iframe1.style.width = '100%';
			iframe1.style.height = '4em';
			//alert(src);
			iframe1.src = src;
			work_div.appendChild(iframe1);
			
		};
		submit1.value = 'Create this gallery and parse the folder';
		work_div.appendChild(submit1);
	} else {
		var submit1 = e('creategallbutton');
		submit1.type = 'button';
		submit1.onclick = function() {
			var iframe1 = document.createElement('iframe');
			var title = e('gallery_sport').value + ': ' + e('home_team').value + ' vs. ' + e('away_team').value;
			var description = (e('description').value != '') ? e('photographer').value + '\n' + e('description').value : e('photographer').value ;
			var src = '/create_gallery.php?date=' + e('gallery_date').value + '&title=' + title + '&description=' + description + '&category=' + e('gallery_sport').value + '&schools=' + e('home_team').value + ';+' + e('away_team').value + ';+&path=' + value;
			iframe1.style.width = '100%';
			iframe1.style.height = '4em';
			//alert(src);
			iframe1.src = src;
			work_div.appendChild(iframe1);
			
		};
		submit1.innerHTML += ' 4';
		submit1.value = 'Create this gallery and parse the folder';
		
	}
	var team = '';
	if(game[game.length-1].toLowerCase() == 'm') {
		team = "Men's ";
	} else if(game[game.length-1].toLowerCase() == 'w') {
		team = "Women's ";
	}
	if(game.length > 1) {
		lookup_abbrev(game[1],'home_team');
		lookup_abbrev(game[2],'away_team');
	} else {
		e('gallery_date').value = '';

	}
	
	if(breakdown[(breakdown.length)-3] == 'hockey') { breakdown[(breakdown.length)-3] = 'ice hockey';}
	e('gallery_sport').value = team + breakdown[(breakdown.length)-3].toProperCase();
	e('gallery_date').value = '20' + game[0].substr(0,2) + '-' + game[0].substr(2,2) + '-' + game[0].substr(4,2);
	
	if(e('home_team').value == '') {
		e('home_team').removeAttribute('readonly');
		e('away_team').removeAttribute('readonly');
		e('gallery_date').removeAttribute('readonly');
		e('gallery_sport').removeAttribute('readonly');
	}
	if(e('gallery_date').value.substr(0, 4) == '20m1' || e('gallery_date').value.substr(0, 4) == '20w1' ) {
		e('gallery_date').value = this.getFullYear() + "-" + this.getMonth() + "-" + this.getDay();
	}


	
}
function random_thumbnail(gallery){
	sendRequest("/user_space.php?thumbnail=random&gallery=" + gallery, function() {
			e('highlight_' + gallery).src = "/albums/" + gallery + "/highlight.jpg";
		}
	);
}
















