// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function onEndCrop( coords, dimensions ) {
	$( 'x1' ).value = coords.x1;
	$( 'y1' ).value = coords.y1;
	$( 'x2' ).value = coords.x2;
	$( 'y2' ).value = coords.y2;
	$( 'width' ).value = dimensions.width;
	$( 'height' ).value = dimensions.height;
}
		
		// with a supplied ratio


function loaded_thumb(tn){
	msg("loaded thumbn: "+tn);
}

function pre_load_thumbs(max){

}
var curmenu= 0;

function turnon(chmenu){
	if (curmenu==chmenu){
		return ;
	}
	mc=$("menu_"+curmenu)
	if (mc.hasClassName("menu_selected")){
		mc.removeClassName("menu_selected") ;
	}
	curmenu=chmenu ;
	just_turnon() ;
}

function just_turnon(){
	mc=$("menu_"+curmenu)
	if (!mc.hasClassName("menu_selected")){
		mc.addClassName("menu_selected") ;
	}
}
function update_div(divn,dat){
	if ($(divn)) {
		$(divn).innerHTML=dat;
	}
	else {
		alert('oh, not hai : '+divn);
	}
}
var targ ;
if (typeof(oldsel) == "undefined") {
	var oldsel = document.onselectstart;
	var oldmouse= document.onmousedown ;
}
if (typeof(editmode)=="undefined"){	
	if (true){ //!meditmode){
		//window.onload = function() {
		document.onselectstart = function() {return false;} // ie
		document.onmousedown = function() {return false;} // mozilla
		document.oncontextmenu= function(){return false ;} //new Function("doalert(message);return false")
		//}
	}
}
function turn_on_txt(){
	document.onselectstart = oldsel ;//function() {return false;} // ie
	document.onmousedown = oldmouse ;//function() {return false;} // mozilla
//	document.oncontextmenu= function(){return false ;} //new Function("doalert(message);return false")

//	alert('on');
}

function turn_off_txt(){
	document.onselectstart = function() {return false;} // ie
	document.onmousedown = function() {return false;} // mozilla

//	alert('off');
}
	//document.onselectstart = function () { return false; }
if (false){
window.onload = function() {
	document.onselectstart = function () { return false; }
	document.onmousedown = function (e) {
		//targ="" ;
		if (!e) e = window.event ;
		if (e.target) targ = e.target ;
		else if (e.srcElement) targ = e.srcElement ;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode ;
		if ( $(targ) && $(targ.id) && $(targ.id).up("table").id !='entry_table')
			return false; 
	}
	pe=$("post_name") ;
	if (pe){
		pe.focus();
	}
}
}

function test_submit(){
	//alert("hi !!"+ni);
	ni.focus() ;
	return false;
}
var ni="";
function ret_to_tab(e,next_focus){
	var event=e
	var keynum
	var keychar
	var numcheck
	
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	//alert("keynum: "+keynum);
	if (keynum==13){
		ni=$(next_focus) ;//.next(".textfield") ;//'input') ;//.focus();
	//alert("next: "+ni+" id: "+ni.id);
		ni.focus();
		event.preventDefault() ;
		Event.stop(event) ;//.returnValue=false;
		return false ;
	}
}
function choosepic(p){
// 	alert("pic chosen:" + p);
}
function choosemenu(m){
}
var got=false ;
var py=0 ;
var dir=0 ;
var ght=0 ;
function geth(id){
	sb=$(id);
	sbw=sb.style.height; //;;="auto" ;//setAttribute("style","
	sb.style.height="auto" ;//setAttribute("style","
	sch=sb.offsetHeight ;
	sb.style.height=sbw;
	return sch ;
}
var hts=Array();
var htsadd=Array() ;
//var last_real_ht=0 ;
function gethts(){
	tot=0 ;
	for ( i = 1 ; i <= maxpix ; i++ ) {
		isrc = "img_" + i + "_div";
		if ( i == maxpix) { isrc="imgonly_"+maxpix ;}
		hts[i] = geth(isrc);
		htsadd[i] = tot ;
		tot += hts[i] + 120;
		//$( "img_crop_" + i ).scrollTop=200 ;
		//$( "img_crop_" + i ).scrollLeft=30 ;
	}
}
function moveif(imgid){
	i=imgid ;
	ntop = htsadd[i] ;
	ntop-= 220 - (hts[i]/2) ;
	
	//$("scroll_box").scrollTop=ntop ;
	//$("right_stuff").innerHTML="i:"+i+"<br>ntop:"+ntop+"<br>id: "+imgid+"<br>size:"+hts[imgid]+ "<br>pos: "+$("scroll_box").offsetHeight;
}
function coora(event){
	ev=mouseCoords(event)
	//$( "img_crop_1" ).scrollLeft=event.pageX;
	//$( "img_crop_1" ).scrollTop=event.pageY;
	$( "img_crop_1" ).scrollLeft=ev.x;
	$( "img_crop_1" ).scrollTop=ev.y;

	//lpy=event.pageY ;
	//lpx=event.pageX ;
	lpy=ev.y ;
	lpx=ev.x ;

	if (lpy>100 && lpy<500) { return ;}
//	if (ght==0) { ght=geth("scroll_box");gethts();}
	if (ght==0) { ght=1 ;gethts();}
	if (lpy<=100) {dir=-1 ;lpy=100-lpy ;}
	if (lpy>=500) {dir=1;lpy=lpy-500 ;}
	if (lpy>100) { lpy=100;}
	py=lpy/10 ;
	if ($("right_stuff")){
		$("right_stuff").innerHTML=" ev:"+py+" px:"+ lpx+ " img ht: "+hts +"  maxpics: "+maxpix;
	}
}
function mv(){
	ev=mouseCoords(event)
	$("scroll_box").scrollTop=ev.y ;
}

var supported = /MSIE ((5\.5)|[6789])/.test(navigator.userAgent) && navigator.platform == "Win32";

function del_on(src){
		$(src).style.width="3em" ;
		$(src).style.height="10px" ;//visibility="visible"
}
var vtitle,hsz,vsz,vsrc ;
var vid=-2 ;
var currid=-1 ;
var dd_loaded ;
var ldimg=0;
var newimg=0 ;
var simple=false;

function dd(msg){
	//$("debug").innerHTML=msg;
}
function doall(src,szx,sz,id,title){
	//#alert('hello doall !!');
	//dd("in do all");
	if (id==currid) { 
		//alert("id: "+id+" vid: "+vid+" currid: "+currid);
		return ; 
	} 
	else {
		//alert("change .. id: "+id+" vid: "+vid+" currid: "+currid);
	}
	vtitle=title ;
	vid=id ;
	vsz=sz ;
	hsz=szx ;
	vsrc=src ;
//	dd("mid doall");
	if (currid!=-1){
		//dd("currid");
		//new Effect.Opacity("all_main",{duration:3,from:1,to:0,queue:'end'})
		//new Effect.Opacity("all_main",{to:0.0,queue:'end'}) ;
		//new Effect.Opacity("all_main",{duration:0.1,from:0.1,to:0.0,queue:'end'}) ;
	}
	else {
		//dd("fo");
		fade_out() ;
	}
	ldimg=new Image() ;
	ldimg.src=vsrc;
	dd_loaded=false ;

	//dd("end doall");
	//window.setTimeout("swon()",100) ;
//window.setTimeout("swon()",100) ;
	swon();
}
function msg(data){
		//$('top_msg').innerHTML=data;
}
function fade_out(){

    new Effect.Opacity("main_img", {
        duration: 0.0,
        to: 0.0,
        queue: 'end'
    });
    $('main_img').setOpacity(0.0);
    $('main_img').hide();
    
    
}

function fade_in(){

    $('main_img').setOpacity(0.0);
    $('main_img').show()
    
    new Effect.Opacity("main_img", {
        duration: 0.5,
        from: 0.0,
        to: 1.0,
        queue: 'end'
    });
}
function fade_out1(){
//new Effect.Opacity("main_img_top",{duration:0.5,from:0.2,to:0.8}) ;
		//new Effect.Opacity("all_main",{duration:3,from:1,to:0.0,queue:'end'}) ;
		//msg("fade out");
// 		dd("fo m");
		

	//$('all_main').setAttribute("class","hi_colour") ;
		//new Effect.Opacity("all_main",{duration:0.0,from:0.0,to:0.0,queue:'end'}) ;
if (true) {//false){
		new Effect.Opacity("main_img",{duration:0.0,to:0.0,queue:'end'}) ;
}


// 		dd("afo");
	//	new Effect.Opacity("main_img_bottom",{duration:0.5,from:1.1,to:-0.0}) ;
}
function fade_in1(){
//alert('faded in !!');
//$$('#main_img').invoke('writeAttribute','style',"display:screen;	opacity: 0.0; filter: alpha(opacity=0);");
//$$('#all_main').invoke('writeAttribute','style',"display:screen;	opacity: 0.0; filter: alpha(opacity=0);");
if (true){ //false){
	new Effect.Opacity("main_img",
			{duration:1.5,from:0.0,to:1.0,queue: 'end'});
}
}
function swon(){
	currid=vid ;
	if (simple!=true){
		mk_update("right","/test/hello?id="+vid);
		//mk_title("main_img_top",vtitle) ;
	}
	reloadImage("main_img",vsrc,vsz) ;
}

var browser=whichBrs() ;

function reloadImage(div,img,vert_sz)
{
	if (simple!=true){
		if (dd_loaded==false){
			window.setTimeout("reloadImage('main_img',vsrc,vsz)",100) ;
			return ;
		}
	}
	if (!simple==true){
		var msz=300
		if (false) {
			msz=300-vert_sz;
			//pos=100-vert_sz 
			pos = msz - (vert_sz/2);
		}
		else {
			pos = msz - (vert_sz/2)
		}
		if (pos<0 ) { pos=0 }
		pos=pos+15 ;
		imgdat=img +"?" + Math.random();
	}
	midh=$("middle").offsetHeight ;
	if (midh<300){
		midh=400 ;
	}
	midw=$("middle").offsetWidth ;
	imgw=hsz ;
	imgh=vsz ;
	posl=(midw-imgw)/2
	postop=(midh-imgh)/2
// explorer - title page - needs centering
//					- gallery - no centering !?!?

// firefox - needs centering
	var cent=false ;
	if (browser=='f' || (simple==true && browser=='ie')){
		//alert("loaded !?!?");
		cent=true ;
	}
	$(div).src=ldimg.src;
	if (isie){
		if (simple!=true) {$('middle').writeAttribute('style',"margin-left:-60px") ;}
		$('all_main').writeAttribute('style',"display:none") ;
		$('main_img').writeAttribute('style',"display:screen") ;
		$('main_img').setOpacity(0.0) ;
		$('all_main').writeAttribute('style',"display:screen") ;//;
		
	}
	else {
		$$('#all_main').invoke('writeAttribute','style',"display:none");
		$$('#main_img').invoke('writeAttribute','style',"display:screen");
		$('main_img').setOpacity(0.0) ; //invoke('writeAttribute','style',"display:screen");
		$$('#all_main').invoke('writeAttribute','style',"display:screen");
		if (simple==true){
			//alert("loaded !?!?");
		}
		//newimg=ldimg ;
		if (typeof (edit_mode)!='undefined') {
			refresh_vals() ;
		}
	}
}
function myloaded(){
	//msg('my loaded !!');
	//msg("loaded !!");
	if (simple!=true){
		if (dd_loaded==false){
			window.setTimeout("myloaded()",100) ;
			return ;
		}
	}

	//alert('goddim');
	mi=$("main_img");
	imgh=mi.getHeight() ;
	imgw=mi.getWidth() ;
	//alert("w: "+imgw + " h: "+imgh);
	var ht=0 ;// "" ;
	var wd=0 ;//"" ;
	var imghw=imgh/imgw ;
	var imgwh=imgw/imgh ;

	//if (imgh/imgw > 1.2){ // tall?
	if (imghw > 1.2){ // tall?
		//ht=450; ;//px" ;
		ht=400; ;//px" ;
		//wd=ht * img
// 		alert("tall");
	}
	//else if (imgw/imgh >1.3) { // wide?
	else if (imgwh >1.3) { // wide?
		//wd="510px";
		//wd=490;//px";
		wd=440;//px";
			//wd=290;//px";
// 		alert("wide");
	}
	else { // boxy?
		//ht=400;//px";
		ht=370;//px";
		//alert("boxy");
	}
	if (wd>0){
	wd=wd/1.2 ;
		ht=wd*imghw ;
	}
	else {
	ht=ht/1.2;
		wd=ht*imgwh ;
	}
	apx='';
if (isie){ apx=''; } else { apx='px'; }

		$('main_img').writeAttribute( {style:'width:'+wd+apx+';height:'+ht+apx+';'}) ;//');
//}
//else {
//		$('main_img').writeAttribute( //{style:'width:'+wd+'px;height:'+ht+'px;'}) ;//');
//}
//		$('main_img').writeAttribute( {style:'width:'+wd+'px;height:'+ht+'px;'}) ;//');
	fade_in() ;
	currid=vid ;

}
function myloaded_old(){
	//msg('my loaded !!');
	//msg("loaded !!");
	if (simple!=true){
		if (dd_loaded==false){
			window.setTimeout("myloaded()",100) ;
			return ;
		}
	}

	//alert('goddim');
	mi=$("main_img");
	imgh=mi.getHeight() ;
	imgw=mi.getWidth() ;
	var ht=0 ;// "" ;
	var wd=0 ;//"" ;
	if (imgh/imgw > 1.2){ // tall?
		ht=450; ;//px" ;
	}
	else if (imgw/imgh >1.3) { // wide?
		//wd="510px";
		wd=490;//px";
	}
	else { // boxy?
		ht=400;//px";
	}
if (isie){
	if (wd>0){
		 $('main_img').writeAttribute( {width:wd}) ;
 		$('main_img').writeAttribute( {height:null}) ;
	}
	if (ht>0){
		 $('main_img').writeAttribute( {height:ht}) ;
		$('main_img').writeAttribute( {width:null}) ;
	}
}
else {
	if (wd>0) {
		$$('#main_img').invoke('writeAttribute','width',wd);
 		$$('#main_img').invoke('writeAttribute','height',null);
	}

	if (ht>0){
		 $$('#main_img').invoke('writeAttribute','height',ht);
		$$('#main_img').invoke('writeAttribute','width',null);
	}
}
wd=0 ;
ht=0 ;
	fade_in() ;
	currid=vid ;

}

function showel(){
// 	$("msg").innerHTML="got allmain1: "+$("all_main1").readAttribute("style");
}
function lowlight(n,lo_colour){
//c6bb99
	//$('menu_'+n).setAttribute("style","color: #"+lo_colour+";");
	$('menu_'+n).setAttribute("class","lo_colour");
}
var dragger=0 ;
function highlight(n,hi_colour){
//a69b89
	//$('menu_'+n).setAttribute("style","color: #"+hi_colour+";");
	$('menu_'+n).setAttribute("class","hi_colour") ;//: #"+hi_colour+";");
//	dragger=new Draggable("row_"+n);
}

function mk_title(div,titlei,t2)
{
	t1=t2;
	if (t1==undefined){
		t1="";
		op=""; //"<"+t1+">"
		cl=""; //" </"+t1+">"
	}
	else {
		op="<"+t1+">"
		cl="</"+t1+">"
	}
	$(div).innerHTML=op+titlei+cl;
}
function mk_update_delay(div,func){
	window.setTimeout("mk_update('"+div+"','"+func+"')",1000) ;
}

function mk_update(div,func)
{
		new Ajax.Updater(div,
			func
			,{asynchronous:true
				, evalScripts:true
				,parameters:'hello=1'
			} )
}
function mk_update_param(div,func,param)
{
		new Ajax.Updater(div,
			func
			,{asynchronous:true
				, evalScripts:true
				,parameters:'id='+param
			} )
}

function mkimg(div,src){
	$(div).innerHTML="<img src='"+src+"'>" ;
}

function aaselect(row_id,action_t){
	//$('left').hide() ;
	//alert("row: "+row_id+ "  act: "+action_t);
	if (row_id=="new"){
		//mk_update("exec","new_img" );
		mk_update("exec","new_"+action_t );
	}
	else {
		//mk_update("mid_data","edit_img?id="+row_id );
		//mk_update("exec","edit_img?id="+row_id );
		mk_update("exec","edit_"+action_t+"?id="+row_id );
	}
}
var wasback=0 ;

function set_on(row,id){
	wasback=$(row).getAttribute("style") ;//#,"background:#e0e0d0;");
//alert("sb: "+sb);
	$(row).setAttribute("style","background:#30e0d0;");

}
function set_off(row,id){
	$(row).setAttribute("style",wasback) ;//"background:#ffffff;");

}
function clearscroll(){
	//sl.setValue(0);
	if ($("scroll_box")){
		$("scroll_box").scrollTop=0 ;
	}
}

function flash_clear(){
	//fdone=false ;
}
function mv_up(){
	//set("topdiv","color","#1280ff") ;
	//isrunning=true ;
	doloop_mv("down") ;
	//$("topdiv").setAttribute("style","color: #123456");
}
var val=0 ;
function mv_down(){
	//set("bottomdiv","color","#12ee56");
	//$("bottomdiv").innerHTML="hello val:"+val ;
	val++;
	//isrunning=true ;
	doloop_mv("up") ;
}
var dir="stopped" ;
var pending_change="" ;
function doloop_mv(mvdir){
	if (dir==mvdir || dir=="stopped"){
		dir=mvdir ;
		isrunning=true ;
		doloop() ;
	}
	else {
		// stop , then change dir
		isrunning=false ;
	}
}
function stop_mv(){
	//set("topdiv","color","#ffffff") ;
	//set("bottomdiv","color","#ffffff") ;
	isrunning=false ;
}
function set(div,att,val){
	$(div).setAttribute("style",att+": "+val);
}

var isrunning=false ;
var uval=0 ;
var speed=0 ; 
var speed_accel=1 ;
var maxspeed=30 ;
var speed_time=40 ;
var maxpix=1 ;
//var height= getheight() ;
//function getheight(){  $("left_div").offsetHeight} ;
function ogetheight(){  
//$("scroll_box").offsetHeight
	var lht=0 ;

	for (i=1 ; i< (maxpix -1) ; i++){
		lht+= $("img_"+i+"_div").offsetHeight ;
	}
// 	alert('got lht: '+lht);
	return lht;
}
function getheight(mx){  
	return getheight1(mx,0);
}
function getheight1(mx,offs){  
//$("scroll_box").offsetHeight
	var lht=0 ;
	var i=1 ;
	//alert("start height");
	for (i=1 ; i< (mx ) ; i++){
		if ($("img_"+i+"_div")){
			lht+= $("img_"+i+"_div").offsetHeight +offs ;
		}
	}
	//$("debug3").innerHTML="lht: "+lht;
// 	alert('got lht: '+lht+ " - mx:"+mx);
	return lht;
}
var thumb_idx=[]
var nxtthumb=0 ;
function add_thumb_idx(imgid){
	thumb_idx[imgid]=nxtthumb ;
	nxtthumb++ ;
	//alert('oh, hai');
}
function move_to_thumb(thid){
	//alert("to "+ids[thid]);
	rthid=ids[thid]; //thumb_idx[thid+1] ;
	
	if (rthid && rthid>0 ){
		amt=getheight1(rthid ,0) ;
		//$("scroll_box").scrollTop =  amt ;
		totmt=getheight(maxpix-1);//$('scroll_box').scrollHeight ;
		if (totmt){
			slv = ((amt)/totmt) ;
			//slv = ((amt*1.02)/totmt) ;
//alert("to rthid: "+rthid+ " idsid: "+ids[thid]+"  thid:"+thid + " maxpix:"+maxpix+" amt: "+amt+" totamt: "+ totmt+ "  slv: "+slv);
			sl.setValue(slv);
		}
	}
	else {
		sl.setValue(0);
	}
}
function doloop() { //dir){
		
		//$("topdiv").innerHTML="val: "+uval+ "  speed:"+speed ; ;//$("left_size").offsetHeight ;	
	if ( isrunning && 
			(
				( dir=="up" &&  uval < getheight()-(10*speed) ) 
					|| 
				( dir=="down" && uval > (10*speed) ) 
			)
		) {

		if (dir=="up") {
			uval+=speed ;
		}
		else if (dir=="down"){
			uval-=speed ;
		}
		if (speed<maxspeed){
			speed+=speed_accel ;
		}
		else {
			speed=maxspeed ;
		}
		$("scroll_box").scrollTop=uval ;
		window.setTimeout("doloop()",speed_time) ;
	}
	else	{
		if (speed>0){
			//$("topdiv").innerHTML="val: "+uval ;
			if (dir=="up") {
				uval+=speed ;
			}
			else if (dir=="down"){
				uval-=speed ;
			}
			if (speed>0){
				speed-=speed_accel ;
			}
			else {
				speed=0 ; //maxspeed ;
				
			}
			$("scroll_box").scrollTop=uval ;
			window.setTimeout("doloop()",speed_time) ;
		}
		else {
			dir="stopped";
			speed=0 ;
		}
	}
	if (uval<0 ){ uval=0 }
	if (uval>getheight()) { uval=getheight() }
}
var isie=whichBrs()=='ie';
function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'f' ;//Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'ie'; //Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}
mouseCoords = function(event){
	if(event.pageX || event.pageY) {
		return {x:event.pageX, y:event.pageY};
	} 
	else if (event.clientX || event.clientY) {
	//variables IEleft and IEtop locate mouse position for Internet Explorer, with or without Doctype declarations
		var IEleft = (window.pageXOffset) ? (window.pageXOffset) : (document.documentElement) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		var IEtop = (window.pageYOffset) ? (window.pageYOffset) : (document.documentElement) ? document.documentElement.scrollTop : document.body.scrollTop;
		return {
			x:event.clientX+IEleft,
			y:event.clientY+IEtop};
		}
	return {x:0, y:0};
}
if (false){
var message="Function Disabled!";

///////////////////////////////////
function clickIE4(){
	if (event.button==2){
	//alert(message);
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
		//alert(message);
			return false;
		}
	}
}

if (document.layers){
//document.captureEvents(Event.MOUSEDOWN);
//document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
//document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("doalert(message);return false")
function doalert(msg){
}
}
