// JavaScript Document
function $( sElementId ) {
	return document.getElementById(sElementId) ;
}

var td_display  = '';
var orig_width  = 0;
var orig_height = 0;
var fit_mode    = 0;

var Page = {
	AddFavoriteGroup : function( sUrl , event ) {
		if ( ! event ) event = window.event ;

		TimedPopup(sUrl);
	},
	
	Post : function( group , event ) {
		Page.GoTo('/upload?GROUP=' + group, event);
	},
	
	GoTo : function( sUrl , event ) {
		if ( ! event ) event = window.event ;
		
		window.location.href = sUrl ;
	},
	
	FocusSearch : function( event ) {
		if ( ! event ) event = window.event ;
		
		var oInput = $('query') ;
		oInput.className = "focused" ;
		oInput.value = "" ;
		
		event.cancelBubble = true ;
	},
	
	BlurSearch : function( event ) {
		if ( ! event ) event = window.event ;
		
		setTimeout("Page.ClearSearch()", 500) ;
		
		event.cancelBubble = true ;
	},
	
	ClearSearch : function( ) {
		var oInput = $('query') ;
		oInput.className = "" ;
		oInput.value = "search this group..." ;
	},

	FocusTopSearch : function( event ) {
		if ( ! event ) event = window.event ;
		
		var oInput = $('topquery') ;
		oInput.className = "focused" ;
		oInput.value = "" ;
		
		event.cancelBubble = true ;
	},
	
	BlurTopSearch : function( event ) {
		if ( ! event ) event = window.event ;
		
		setTimeout("Page.ClearTopSearch()", 500) ;
		
		event.cancelBubble = true ;
	},
	
	ClearTopSearch : function( ) {
		var oInput = $('topquery') ;
		oInput.className = "" ;
		oInput.value = "search ..." ;
	},

	HideCalendar : function( button ) {
		var calstyle = $('calendars').style;
		if (calstyle.display =='none') {
			calstyle.display           = td_display;
			button.innerHTML           = 'Hide Calendar';
			$('top').style.width       = '874px';
			$('groups').style.cssFloat = 'right';

			document.cookie = 'showcal=1;path=/;max-age=631152000';
		}
		else {
			td_display                 = calstyle.display;
			calstyle.display           = 'none';
			button.innerHTML           = 'Show Calendar';
			$('top').style.width       = '730px';
			$('groups').style.cssFloat  = 'none';

			document.cookie = 'showcal=0;path=/;max-age=631152000';
		}
	},

	MaybeHideCalendar : function( ) {
		var cookies = document.cookie.split(/;\s*/);
		for (var i in cookies) {
			if (cookies[i] == "showcal=0") {
				Page.HideCalendar($('hidecal'))
			}
		}
	},

	// ------------------------------------------------------------------
	// Get window (image) size in pixels; return [0, 0] if unidentifiable
	// ------------------------------------------------------------------
	GetWindowDims : function () {
	    var d  = document;
	    var de = d.documentElement;
	    if (de && typeof de.clientWidth != 'undefined'
	        && de.clientWidth != 0) {
	        return [de.clientWidth, de.clientHeight];
	    }
	    else if (d.body && typeof d.body.clientWidth != 'undefined') {
	        return [d.body.clientWidth, d.body.clientHeight];
	    }
	    else if (typeof window.innerWidth != 'undefined') {
	        return [window.innerWidth, window.innerHeight];
	    }
	    else {
	        // alert ("Can't identify window width.");
		return [0, 0];
	    }
	},

	GetImageDims : function (image) {
	    var image_width  = image.width;
	    var image_height = image.height;

	    // Ugly hack to work around IE brain death
	    if (image_width == 0 || image_height == 0) {
	        var img = image.outerHTML;
		if (typeof(img) == 'string') {
		    var w_match  = img.match(/width\s*=\s*['"]?(\d+)['"]?/);
		    var h_match  = img.match(/height\s*=\s*['"]?(\d+)['"]?/);
		    image_width  = w_match[1];
		    image_height = h_match[1];
		}
	    }

	    return [image_width, image_height];
        },

	FitPicture : function (button, force) {
	    var image      = $('mainpic');
	    var win_dims   = Page.GetWindowDims();
	    var fit_width  = win_dims[0] - 26;
	    var fit_height = win_dims[1] - 72;
	    var fit_ratio  = 1;

	    if (   orig_width <= 0 || orig_height <= 0
	        ||  fit_width <= 0 ||  fit_height <= 0) {
	        return;
	    }

	    if (force != 1) {
	        fit_mode = (fit_mode + 1) % 3;
            }

	    if (fit_mode == 1) {
	        // Smaller of height and width
		fit_ratio        = Math.min(fit_width  / orig_width,
		                            fit_height / orig_height);
	        fit_ratio        = Math.min(fit_ratio, 1);
		window.onresize  = function () { Page.FitPicture(button, 1) };
		button.innerHTML = 'Fit Width';
	    }
	    else if (fit_mode == 2) {
		// Width only
	        fit_ratio        = Math.min(fit_width / orig_width, 1);
		window.onresize  = function () { Page.FitPicture(button, 1) };
		button.innerHTML = 'Full Size';
	    }
	    else {
		// No fit
		fit_mode         = 0;
	        window.onresize  = null;
		button.innerHTML = 'Fit Image';
	    }

	    image.width     = fit_ratio * orig_width;
	    image.height    = fit_ratio * orig_height;
	    document.cookie = 'fitpic=' + fit_mode + ';path=/;max-age=631152000';

	},

	MaybeFitPicture : function () {
	    var cookies = document.cookie.split(/;\s*/);
	    var do_fit  = 1;
	    for (var i in cookies) {
		if (   cookies[i].length > 7
		    && cookies[i].substr(0,7) == "fitpic=") {
		    var mode = cookies[i].substr(7);
		    do_fit   = mode;
		    fit_mode = mode - 1;
		}
	    }
	    if (do_fit != 0) {
	        Page.FitPicture($('fitpic'), 0);
	    }
	}
};

var want_pop = 0;
var timeout  = 0;
var Pop = {
	Reveal : function( iImageId , iTargetId , event ) {
		// $('debug').innerHTML += 'Reveal ';
		if ( ! event ) event = window.event ;
		
		var oPop = $('pop') ;
		oStrip = $('strip' + iTargetId) ;
		oRow = oStrip.rows[1] ;
		
		var iLeft = Pop.FindLeft(oRow) ;
		var iTop = Pop.FindTop(oRow) ;
		
		$('head').innerHTML = oInfo[iImageId].sTitle ;
		$('content').innerHTML = oInfo[iImageId].sContent ;
		
		oPop.style.left = iLeft + "px" ;
		oPop.style.top = (iTop - 10) + "px" ;

		want_pop++;
		if (timeout != 0) clearTimeout(timeout);
		timeout = setTimeout("Pop.MaybeReveal();", 500);
	},

	MaybeReveal : function ( ) {
		// $('debug').innerHTML += 'MaybeReveal ';
		if (want_pop > 0) {
			$('pop').style.display = "block" ;
		}
	},

	Hide : function( event ) {
		// $('debug').innerHTML += 'Hide ';
		if ( ! event ) event = window.event ;

		if (--want_pop < 0) want_pop = 0;
		if (  want_pop > 0) return;
		
		if (timeout != 0) clearTimeout(timeout);
		timeout = setTimeout("Pop.MaybeHide();", 500);
	},
	
	MaybeHide : function ( ) {
		// $('debug').innerHTML += 'MaybeHide ';
		if (want_pop == 0) {
			$('pop').style.display = "none" ;
		}
	},

	FindLeft : function( oElement ) {
		var iLeft = 0 ;
		
		while ( oElement && oElement != document.body ) {
			iLeft += oElement.offsetLeft ;
			oElement = oElement.offsetParent ;
		}
		
		return iLeft ;
	},
	
	FindTop : function( oElement ) {
		var iTop = 0 ;
		
		while ( oElement && oElement != document.body ) {
			iTop += oElement.offsetTop ;
			oElement = oElement.offsetParent ;
		}
		
		return iTop ;
	}
};
