﻿//
// init
//
$(document).ready(function() {
    
    // zoom
    $("a.miPIC img").live("mousedown", function(e) { tnRCZ(e,this) });
        
});


//
// callback: update page trail w/ show filter (js must be above callback for this event handler to work!)
//
function bcUpdadePageTrail(sender, args) {
    var sShow = $(".pgrFilter:first select[name='show']").val();
    if (sShow != null) $(".topSubHead h3").html(sShow.toUpperCase());
}


//
// city messenger
//
function tnCTM(sTID, sTUSR) {
    return bcChatIM(sTID, sTUSR);
}


//
// delete friend
//
function tnDFR(iMID,sUSR) {
    return bcFriendsDel(iMID,sUSR);
}


//
// right-click thumb zoom 
//
function tnRCZ(evt,oImg) {
    if (_bRCZ && (evt.button == 2))  {   
        oImg.oncontextmenu = function(){ return false; }
        var sIdxPhoto = oImg.src.substr(oImg.src.lastIndexOf("/"));
        if (sIdxPhoto.indexOf(".png") == -1) {
            tnZOM(evt,sIdxPhoto); 
        }
    }
}


//
// zoom: init
//
var _j = 10;
var _x = 0;
var _iPhotoW = 0;
var _iPhotoH = 0;
var _imgPhoto = null;
var _divZoom = null;
var _imgPict = null;
var _iMaxSz = 200;
var _bRCZ = true;


//
// zoom: get objects
//
function tnZOMGetObj() {
	//if (_divZoom == null || _imgPict == null) { //this chk breaks zoom under callback
	_divZoom = (document.getElementById) ? document.getElementById("divZoom") : (document.all) ? document.all["divZoom"] : null;
	_imgPict = (document.getElementById) ? document.getElementById("imgPict") : (document.all) ? document.all["imgPict"] : null;
	//}
	return (_divZoom != null && _imgPict != null);
}

//
// zoom: start
//
function tnZOM(evt,sImg)	{
	tnZOMReset();
	var e = (!evt) ? window.event : evt;	
	if (e != null) {
		var iX = null;
		var iY = null;
		if (e.pageX){//FF
			iX = e.pageX; // + window.pageXOffset;
			iY = e.pageY; // + window.pageYOffset;
		} else if (e.clientX){//IE (in either quirks or compliant mode)
			iX = e.clientX + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
			iY = e.clientY + Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		}			
		if (iX != null && iY != null) {
		    var sUrl = "../media/pub/index/" + sImg;
			tnZOMStep1(sUrl,iX,iY);
		}
    }
	return false;
}

//
// zoom: pre-load + check
//
function tnZOMStep1(sUrl,iX,iY) {		
	if (!_imgPhoto.src) {
		_imgPhoto.src = sUrl;
		setTimeout("tnZOMStep1('" + sUrl + "'," + iX + "," + iY + ")",100);
	} else if (_imgPhoto.complete && _imgPhoto.width > 25) {
		tnZOMStep2(iX,iY);
	} else {		
		if (_x > 50000) { return; } else { _x += 1; }
		setTimeout("tnZOMStep1('" + sUrl + "'," + iX + "," + iY + ")",100);
	}
}

//
// zoom: display
//
function tnZOMStep2(iX,iY) {
	if (tnZOMGetObj()) {
		_iPhotoW = _imgPhoto.width;
		_iPhotoH = _imgPhoto.height;
		_divZoom.style.display = "";
		_divZoom.style.top  = Math.max((iY - 10),0) + "px";
		_divZoom.style.left = Math.max((iX - Math.min(_iPhotoW,_iMaxSz)),0) + "px";			
		_imgPict.src = _imgPhoto.src;	
		_imgPict.width = 1;
		_imgPict.height = 1;
		tnZOMStep3();
	}
}

//
// zoom: loop zoom
//
function tnZOMStep3() {
	if (tnZOMGetObj() && (_imgPict.width < _iPhotoW && _imgPict.width < _iMaxSz)) {
		_imgPict.width += _j;
		if (_imgPict.width > _iPhotoW) _imgPict.width = _iPhotoW;
		_imgPict.height = Math.round(_imgPict.width*((_iPhotoH)/(_iPhotoW)));
		setTimeout("tnZOMStep3()",10);
		_j += 1;
	}
}

//
// zoom: reset
//
function tnZOMReset() {
	if (tnZOMGetObj()) {
		_divZoom.style.display = "none";
		_imgPhoto = new Image();
		_j = 10;
		_x = 0;
		_iPhotoW = 0;
		_iPhotoH = 0;			
	}
}