var pid;
var caid;
var pos;
function enable_brackets(b1,b2) {
lb = document.getElementById(b1);
rb = document.getElementById(b2);
//show them
lb.style.color = "yellow";
rb.style.color = "yellow";
}
function disable_brackets(b1,b2) {
lb = document.getElementById(b1);
rb = document.getElementById(b2);
//show them
lb.style.color = "black";
rb.style.color = "black";


}
function show_thumb_overlay(oid) {
lb = document.getElementById(oid);
lb.style.display ="";


}
function hide_thumb_overlay(oid) {
lb = document.getElementById(oid);
lb.style.display ="none";


}


function ajax_request(method,url,post_string,return_element) {
    var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
  
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) {
                 return_element.innerHTML=xhr.responseText; 
             } else { 
                 resp.innerHTML="Error code " + xhr.status;
	     }
	  return xhr.status;
         }
    }; 

   xhr.open(method, url,  true); 
   xhr.send(post_string); 




}
function update_thumb_line(line,aid,pos,w) {
resp = document.getElementById(line);
post_string = "show=thumb_line&aid=" + aid + "&pid=" + pos + "&width=" + w;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}
function add_like(iid) {
resp = document.getElementById("like_reply");
post_string = "show=add_like&iid=" + iid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}
function remove_like(iid) {
resp = document.getElementById("like_reply");
post_string = "show=remove_like&iid=" + iid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}

function move_forward(aid) {
resp = document.getElementById("main_image");
post_string = "show=move_forward&aid=" + aid + "&pid=" + pid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}
function move_backward(aid) {
resp = document.getElementById("main_image");
post_string = "show=move_backward&aid=" + aid + "&pid=" + pid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}

function display_photo(pid,aid) {
resp = document.getElementById("main_photo");
post_string = "show=display_photo&pid=" + pid + "&aid=" + aid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}
function check_thumbline(pid,aid) {
set_vars(pid,aid);
var resp = document.getElementById("photo_details");

var post_string = "show=load_photo_details&pid=" + pid + "&aid=" + aid;

var status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

 resp = document.getElementById("sthumbline");
 testid = document.getElementById("thumb" + pid);

if (!testid) {
post_string = "show=check_thumbline&pid=" + pid + "&aid=" + aid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp);
 
}
var resp3 = document.getElementById("detail_container");
var post_string3 = "show=load_photo_comments&pid=" + pid + "&aid=" + aid;
status =   ajax_request('GET', "ajax.php?" + post_string3,"",resp3);

//add_post(2,pid);
 var resp2 = document.getElementById("main_photo_coment");
 var post_string2 = "show=add_post&type=2&iid=" + pid;
 var status2 =   ajax_request('GET', "ajax.php?" + post_string2,"",resp2); 

}
function jump_right(pid,aid) {
resp = document.getElementById("sthumbline");
post_string = "show=jump_right&pid=" + pid + "&aid=" + aid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 


}
function jump_left(pid,aid) {
resp = document.getElementById("sthumbline");
post_string = "show=jump_left&pid=" + pid + "&aid=" + aid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 


}
function display_stat(pid,days) {
resp = document.getElementById("sstat");
post_string = "show=show_stat&pid=" + pid + "&days=" + days;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 


}

function display_next_photo(pid,aid) {
resp = document.getElementById("main_photo");
post_string = "show=display_next_photo&pid=" + pid + "&aid=" + aid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}
function display_previous_photo(pid,aid) {
resp = document.getElementById("main_photo");
post_string = "show=display_previous_photo&pid=" + pid + "&aid=" + aid;

status =   ajax_request('GET', "ajax.php?" + post_string,"",resp); 

}

function set_pid(val,aid) {
pid = val;
caid = aid;
// set the thumb line
update_thumb_line("thumb_line"+aid,aid,pid,800);
//update comments
//add_post(2,pid);
}
function set_vars (a,b) {
pid = a;
caid = b;
}

function keypress_call(evt) {
var keynum;
var keychar;
var numcheck;
//keynum = e.keyCode;
var keycode = evt.which?evt.which:evt.keyCode
var keychar = String.fromCharCode(keycode);

/*
if(window.event) // IE
{
    keynum = e.keyCode;
//    keychar = e.charCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
    keynum = e.which;
//    keychar = e.charCode;
}
*/
//keychar = String.fromCharCode(keynum);
//   alert("Pressed code "+keynum+ " key " + keychar);
if (keycode == 39 ) {
//test for main photo
resp = document.getElementById("main_photo");
if (resp) {
display_next_photo(pid,caid);

}
}
if (keycode == 37) {
//test for main photo
resp = document.getElementById("main_photo");
if (resp) {
display_previous_photo(pid,caid);

}
}


}
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function add_post(itype,iid,c,aid) {
name = document.getElementById("name" + itype + "x" + iid );
if (name) {
nval = name.value;
nval  = Base64.encode(nval);

} else {
nval = "";
}
code = document.getElementById("code" + itype + "x" + iid );
if (code) {
cval = code.value;
} else {
cval = "";
}
var h = "h" + c;
var fb = document.getElementById(h);
if (fb) {
fbconnect = fb.value;
} else {
//alert("unable to locate fbconnect " + h );
fbconnect = "";
}

mess = document.getElementById("mess" + itype + "x" + iid );
if (mess) {
mval = mess.value;
mval  = Base64.encode(mval);
}
if (itype == 2) {
var resp2 = document.getElementById("main_photo_coment");

} else {
var resp2 = document.getElementById("comment" + itype + "x" + iid);
}
if (resp2) {
var post_string = "show=add_post&type=" +itype + "&fbconnect=" + fbconnect + "&iid=" + iid + "&code=" + cval + "&name=" + nval + "&aid=" + aid + "&text=" + mval ;
//alert(post_string);
var status =   ajax_request('GET', "ajax.php?" + post_string,"",resp2); 
} else {
alert("Comment form not found!!");
}

}

function show_comment_form(f) {
n = document.getElementById(f);
n.style.display="";

}
function show_comment_form2(f,g) {
n = document.getElementById(f);
n.style.display="";
n = document.getElementById(g);
n.style.display="";

}
function show_arrow(f) {
n = document.getElementById(f);
n.style.display="";

}

function hide_arrow(f) {
n = document.getElementById(f);
n.style.display="none";

}
function draw_user_box(c) {
var box = "user_box" + c;
var user_box = document.getElementById(box);
var h = "h" + c;
var hidden_box = document.getElementById(h);
hidden_box.value = 1;
user_box.innerHTML = "<span valign=top> <fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic> " 
                     +  "You are logged in as <fb:name uid='loggedinuser' useyou='false'></fb:name></span>";
//say_hello("<fb:name uid='loggedinuser' useyou='false'></fb:name>");
FB.XFBML.Host.parseDomTree();

}

function draw_boxes() {
//global box_count;
//alert(box_count);
for (i=1;i <= box_count;i++) {
draw_user_box(i);

}
}
function hide_element(n) {
k = document.getElementById(n);
k.innerHTML = '';

}

function publish_to_feed(message,aname, alink, plink,tlink) {
//var message = "some"; 
eval ("var attachment = {'caption':'{*actor*} commented on a photo from this album','name':'"+ aname +"', 'href':'"+alink+"', 'description':'', 'media': [{'type': 'image', 'src': '"+tlink+"', 'href': '"+plink+"'}]};");
//FB.init("1f0d7fdf197bf58ae20bdf0e52f06bd4","xd_receiver.htm");
FB.ensureInit ( function () {
 FB.Connect.streamPublish(message, attachment,null,null,null,null,1);
});
}

function publish_album_to_feed(message,aname, alink, plink,tlink) {
//var message = "some"; 
eval ("var attachment = {'caption':'"+message+"','name':'"+ aname +"', 'href':'"+alink+"', 'description':'', 'media': [{'type': 'image', 'src': '"+tlink+"', 'href': '"+plink+"'}]};");
//FB.init("1f0d7fdf197bf58ae20bdf0e52f06bd4","xd_receiver.htm");
FB.ensureInit ( function () {
 FB.Connect.streamPublish('', attachment,null,null,null,null,1);
});
}

function publish_like_to_feed(aname, alink, plink,tlink) {
//var message = "some"; 
eval ("var attachment = {'caption':'{*actor*} likes this photo','name':'"+ aname +"', 'href':'"+alink+"', 'description':'', 'media': [{'type': 'image', 'src': '"+tlink+"', 'href': '"+plink+"'}]};");
//FB.init("1f0d7fdf197bf58ae20bdf0e52f06bd4","xd_receiver.htm");
FB.ensureInit ( function () {
 FB.Connect.streamPublish('', attachment,null,null,null,null,1);
});
}

function draw_album_desc_box() {
k1= document.getElementById("descbox1");
if (!k1) {
alert("No box1!");
}
k1.style.display = '';
//k1.innertHTML = '';

k2= document.getElementById("descbox2");
if (!k2) {
alert("No box2!");
}

k2.innerHTML = '';
}