// created by:
// Brian C. Thomas bct.x42@gmail.com
// Brent Pedersen bpederse@gmail.com
// distributed under the Perl Artistic license
// See LICENSE file included
//

var xpos = 15;
var ypos = 15;
var ajaxposition;

if(getCookie('ajaxposition')) {
	ajaxposition = getCookie('ajaxposition');
	xpos = parseInt(ajaxposition.substr(0, ajaxposition.indexOf('-')));
	ypos = parseInt(ajaxposition.substr(ajaxposition.indexOf('-') + 1));
}

var ajax = [];
function pjx(args,fname,method) {
  this.dt=args[1];
  this.args=args[0];
  this.method=method;
  this.r=ghr();
  this.url = this.getURL(fname);
}

function getVal(id) {
  if (id.constructor == Function ) { return id; }
  if (typeof(id)!= 'string') { return id; }
  var element = document.getElementById(id);
  if (element.type == 'select-multiple') {
  var ans = new Array();
	for (var i=0;i<element.length;i++) {
	  if (element[i].selected) {
		ans.push(element[i].value);
	  }
	}
	return ans;
  }
  if(element.type == 'radio'){
	ans =[];
	var elms = document.getElementsByTagName('input');
	var endk = elms.length;
	for(var k=0;k<endk;k++){
	  if(elms[k].type=='radio' && elms[k].checked && elms[k].id==id){
		ans.push(elms[k].value);
	  }
	}
	return ans;
  }
  try {
	return element.value.toString();
  } catch(e) {
	try {
	  return element.innerHTML.toString();
	} catch(e) {
	  var errstr = 'ERROR: cant get html element with id:' +
	  id + '.  Check that an element with id=' + id + ' exists';
	  alert(errstr);
	  return false;
	}
  }
}

function fnsplit(arg) {
  var arg2="";
  if (arg.indexOf('__') != -1) {
	var arga = arg.split(/__/);
	arg2 += '&' + arga[0] +'='+ encodeURIComponent(arga[1]);
  } else {
	var ans = getVal(arg);
	if ( typeof ans != 'string' ) {
	  if ( ans.length == 0 ) { arg2 += '&args=&' + arg + '='; }
	  for (var i=0;i < ans.length;i++) {
		arg2 += '&args=' + encodeURIComponent(ans[i]) + '&' + arg + '=' + encodeURIComponent(ans[i]);
	  }
	} else {
	  arg2 += '&args=' + encodeURIComponent(ans) + '&' + arg + '=' + encodeURIComponent(ans);;
	}
  }
  return arg2;
}

pjx.prototype.send2perl=function() {
  var r = this.r;
  var dt=this.dt;
  var url=this.url;
  var pd;
  if(this.method=="POST"){
	var tmp = url.split(/\?/);
	url = tmp[0];
	pd = tmp[1];
  }
  r.open(this.method,url,true);
  r.setRequestHeader('Cache-Control','no-cache');
  if(this.method=="POST"){
	r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	r.send(pd);
  }
  if(this.method=="GET"){
	r.send(null);
  }
  r.onreadystatechange=handleReturn;
};

var handleReturn = function() {
  for( var k=0; k<ajax.length; k++ ) {
	if (ajax[k].r==null) { ajax.splice(k--,1); continue; }
	if ( ajax[k].r.readyState== 4) { 
	  var data = ajax[k].r.responseText.split(/__pjx__/);
	  dt = ajax[k].dt;
	  if (dt.constructor != Array) { dt=[dt]; }
	  if (data.constructor != Array) { data=[data]; }
	  if (typeof(dt[0])!='function') {
		for ( var i=0; i<dt.length; i++ ) {
		  var div = document.getElementById(dt[i]);
		  if (div.type =='text' || div.type=='textarea' || div.type=='hidden' ) {
			div.value=data[i];
		  } else{
			div.innerHTML = data[i];
		  }
		}
	  } else if (typeof(dt[0])=='function') {
		eval(dt[0](data));
	  }
	  ajax.splice(k--,1);
	}
  }
};

pjx.prototype.getURL=function(fname) {
  var args = this.args;
  var url= 'fname=' + fname;
  for (var i=0;i<args.length;i++) {
	url=url + args[i];
  }
  return url;
};
var ghr=getghr();
function getghr(){
	if(typeof XMLHttpRequest != "undefined")
	{
		return function(){return new XMLHttpRequest();}
	}
	var msv= ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0",
	"Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	for(var j=0;j<=msv.length;j++){
		try
		{
			A = new ActiveXObject(msv[j]);
			if(A){ 
			  return function(){return new ActiveXObject(msv[j]);}
			}
		}
		catch(e) { }
	 }
	 return false;
}
function ajaxalbum() { 
	var args = ajaxalbum.arguments; 
	for( var i=0; i<args[0].length;i++ ) { 
		args[0][i] = fnsplit(args[0][i]);
	}
	var method="GET";
	if( args.length==3 && (args[2]=="POST"||args[2]=="post") ) { 
		method="POST";
	}
	ajax.push(new pjx(args,"ajaxalbum",method)); 
	var l = ajax.length-1; 
	var sep = '?'; 
	if ( '/album/ajaxalbum.cgi' == '0') { 
		if ( window.location.toString().indexOf('?') != -1) { 
			sep = '&';
		} 
		ajax[l].url = window.location + sep + ajax[l].url;
	}
	else { 
		if ( '/album/ajaxalbum.cgi'.indexOf('?') != -1) { 
			sep = '&';
		} 
		ajax[l].url = '/album/ajaxalbum.cgi' + sep + ajax[l].url;
	} 
	ajax[l].send2perl(); 
	if (0) { 
		var tmp = document.getElementById('__pjxrequest').innerHTML = "<br \/><pre>"; 
		for( i=0; i < ajax.length; i++ ) { 
			tmp += '<a href= '+ ajax[i].url +' target=_blank>' + decodeURIComponent(ajax[i].url) + ' <\/a><br \/>'; 
		} 
		document.getElementById('__pjxrequest').innerHTML = tmp + "<\/pre>..........";
	}
	return(2);
}

function resetalbum(txt) {
	
	/*if(document.getElementById('containinner') != null) {
		document.getElementById('dwindowcontainer').removeChild(document.getElementById('containinner'));
	}*/

	var thep = document.createElement('p');
	var theimg = document.createElement('img');
	var thetxt = document.createTextNode(txt);

	theimg.setAttribute('src', '/bilder/indicator.gif');
	theimg.setAttribute('width', '16');
	theimg.setAttribute('height', '16');
	theimg.setAttribute('style', 'border-width: 0px;');
	theimg.setAttribute('alt', 'loading');
	
	thep.setAttribute('id', 'containinner');
	thep.setAttribute('class', 'text');
	thep.className = 'text';
	thep.appendChild(theimg);
	thep.appendChild(thetxt);

	
	return(thep);
}

function setup_window(bid, kid) {
	
	
	var thebody = document.getElementsByTagName('body').item(0);

	var thewin = document.createElement('div');
	var thetitle = document.createElement('div');
	var thecontent = document.createElement('div');
	var theclose = document.createElement('a');
	var theopenclose = document.createTextNode('[');
	var thecloseclose = document.createTextNode(']');
	var theclosetext = document.createTextNode('stäng');
	var exif = document.createElement('a');
	var openexif = document.createTextNode(' [');
	var closeexif = document.createTextNode(']');
	var exiftext = document.createTextNode('bildinfo');
	var thefake = document.createElement('img');

	theclose.setAttribute('href', 'javascript:closeit();');
	theclose.setAttribute('class', 'menu');
	theclose.setAttribute('onmouseup', 'blur();');
	theclose.setAttribute('accesskey', 'w');
	theclose.className = 'menu';
	theclose.appendChild(theclosetext);
	
	exif.setAttribute('href', 'javascript:toggle_exif(' + bid + ', ' + kid + ')');
	exif.setAttribute('class', 'menu');
	exif.className = 'menu';
	exif.appendChild(exiftext);
	
	thetitle.setAttribute('class', 'menus');
	thetitle.className = 'menus';
	thetitle.setAttribute('id', 'dtitle');
	thetitle.setAttribute('onmouseup', 'lock_position()');

	thetitle.appendChild(theopenclose);
	thetitle.appendChild(theclose);
	thetitle.appendChild(thecloseclose);
	
	thetitle.appendChild(openexif);
	thetitle.appendChild(exif);
	thetitle.appendChild(closeexif);

	thefake.setAttribute('id', 'ajaxstorbild');
	thefake.setAttribute('src', '/bilder/indicator.gif');
	thefake.setAttribute('alt', 'Laddar...');

	thecontent.setAttribute('id', 'dwindowcontent');

	thewin.setAttribute('id', 'dwindow');
	thewin.setAttribute('class', 'opaque');
	thewin.className = 'opaque';
	thewin.appendChild(thetitle);
	thewin.appendChild(thecontent);

	thebody.appendChild(thewin);
	
	document.getElementById("dwindow").style.left = xpos + 'px';
	document.getElementById("dwindow").style.top = getScrollTop() + ypos + 'px';
	document.getElementById('dwindow').style.display = 'block';

/* 2006-01-28 - testar annat "fönster" */
	var drag = ToolMan.drag();
	var group;
	var boxHandle = document.getElementById("dwindow");
	group = drag.createSimpleGroup(boxHandle, document.getElementById("dtitle"));
	
	
/* */

}

function albumize(date) {
	var para;

	if(document.getElementById('album_' + date).style.display == 'block') {
		document.getElementById('album_' + date).style.display = 'none';
	}
	else {

		para = resetalbum(' Hämtar bilder...');

		document.getElementById('album_' + date).appendChild(para);

		document.getElementById('album_' + date).style.display = 'block';
		ajaxalbum( ['datum__' + date], ['album_' + date] );
	}

}



function picturize(bid, kid) {
	if(document.getElementById('innercontainer') != null) {
		document.getElementById('dwindowcontent').removeChild(document.getElementById('innercontainer'));
	}
	if(document.getElementById('dwindow') != null) {
		document.getElementsByTagName('body').item(0).removeChild(document.getElementById('dwindow'));
	}
	setup_window(bid, kid);
	var para = resetalbum(' Laddar bild...');
	document.getElementById('dwindowcontent').appendChild(para);
	
	
	
	if(ajaxalbum( ['bid__' + bid, 'kid__' + kid], ['dwindowcontent'] ) == 2) {
		
	//	document.getElementById("dwindow").style.display = 'block';
	//	loadwindow('doh.html', 60, 40);
		
		
//		document.getElementById("dwindow").style.left = document.getElementById("dwindow").style.left + 10 +'px';
		//document.getElementById("dwindow").style.left - 1 +'px';
	}
	
}

function closeit(){
	document.getElementById("dwindow").style.display="none";
	blur;
}

function lock_position() {
	ypos = document.getElementById('dwindow').style.top.substr(0, document.getElementById('dwindow').style.top.indexOf('p')) - getScrollTop();
	xpos = document.getElementById('dwindow').style.left.substr(0, document.getElementById('dwindow').style.left.indexOf('p'));
	setCookie('ajaxposition', xpos + '-' + ypos, 365);
}

function toggle_exif(bid, kid) {
	var exif;
	if(getCookie('album_exif')) {
		exif = getCookie('album_exif');
		
		if(exif == 1) {
			setCookie('album_exif', 0, 365);
		}
		else {
			setCookie('album_exif', 1, 365);
		}
	}
	else {
		setCookie('album_exif', 1, 365);
	}
	picturize(bid, kid);

}