var MAPS     = [];
var ONLOADS  = []; //ONLOADS.push( {fn:"myFunction",args:"'string1','string2',100"} )
var TOOLTIPS = [];

function trkbLoginAlert(strMessage,strRegister,strPasswordReminder)
{
		dojo.require('dijit.Dialog');
		dojo.require('dijit.form.Button');
		
		var title = mayaFish.consts.SITE_TITLE;
		trkbDialog = new dijit.Dialog( {'title': title} );
	
		var body  = '<div class="alertDialogContent">';
			body += '<p>'+strMessage+'</p>';
			body += '<p><a href="/register">'+strRegister+'</a> | <a href="/forgottenPassword">'+strPasswordReminder+'</a></p>';
			body += '<p><button dojoType="dijit.form.Button" onClick="trkbDialog.destroy();">OK</button></p>';
			body += '</div>';
		trkbDialog.attr('content', body);
		trkbDialog.show();
}

function trkb_onLoad()
{
	if( MAPS.length > 0 )
	{	
		if(GBrowserIsCompatible())
		{
			for(var i=0;i<MAPS.length;i++){ eval( "loadMap_"+MAPS[i]+"()" ); }
		}
	}
	if( ONLOADS.length > 0 )
	{
		for(var i=0;i<ONLOADS.length;i++){ eval( ONLOADS[i].fn+"("+ONLOADS[i].args+")" ); }
	}
	if( TOOLTIPS.length > 0 )
	{
		for(var i=0;i<TOOLTIPS.length;i++)
		{
			new Tip(TOOLTIPS[i].ele,TOOLTIPS[i].text,TOOLTIPS[i].params);
		}
	}
	if( typeof(onLoad) == 'function' )
	{
		onLoad();
	}
}
function trkb_onUnload()
{
	if( MAPS.length > 0 ){ GUnload(); }
}

function openDoc(_contID)
{
	var _url = "/docs/"+_contID+"?popup=1";
	var _params = "width=600,height=400,resizable=yes,scrollbars=yes";
	helpWin = open(_url, "doscWin", _params);
	helpWin.focus();
}

function showHelp(_contID)
{
	var _url = "/help/"+_contID;
	var _params = "width=600,height=400,resizable=yes";
	helpWin = open(_url, "helpWin", _params);
	helpWin.focus();
}

function showLoader()
{
	var _obj = document.getElementById("loadingBox");
	//var posX = parseInt((document.body.clientWidth-_obj.clientWidth)/2);
	//var posY = parseInt((document.body.clientHeight-_obj.style.clientHeight)/2);;
	//_obj.style.left = posX;
	//_obj.style.top  = posY;
	_obj.style.visibility = "visible";
}
function hideLoader()
{
	document.getElementById("loadingBox").style.visibility = "hidden";
}

/**********************************************/
/* TOOLS                                      */
/**********************************************/

var kTools = function()
{
	return {
	
		prepairPostString: function(_str)
		{
			_str = _str.replace(/\?/g,"%3F");
			_str = _str.replace("&","%26");
			return _str;
		}
	};
}();

/**********************************************/
/* GALLERY                                    */
/**********************************************/

var kGallery = function()
{
	return {
		galleryID    : null,
		galleryTitle : "",
		imageID      : null,
		selectedIndex: 0,
		images       : [],
		
		prevIdx      : -1,
		nextIdx      : -1,
		
		
		open: function(_galleryID,_imageID)
		{
			//kGallery.show("<p>...</p>","Loading...");
			
			kGallery.images    = [];
			kGallery.galleryID = _galleryID;
			kGallery.imageID   = _imageID;
			kGallery.nextIdx   = -1;
			kGallery.prevIdx   = -1;
			
			var url = "/trkbTripServices/gallery/"+_galleryID+"/"+_imageID;
			var pars  = 'init=1';
			var myAjax = new Ajax.Request( url, {   method: 'post', 
												parameters: pars, 
												onComplete: kGallery.inited });
		},
		
		inited: function(request)
		{
			console.log(request.responseText);
		
			eval(request.responseText);
			kGallery.show("/trkbTripServices/gallery/"+kGallery.galleryID+"/"+kGallery.imageID,kGallery.galleryTitle);
		},
		
		show: function(_url,_title)
		{
			Modalbox.show(_url,{title:_title,width:820,height:530});
		},
		
		selectImage: function(_idx)
		{
			var selectedIndex = ( arguments.length<1 ) ? 0 : _idx;
			var refImg = kGallery.images[selectedIndex];
			kGallery.prevIdx = refImg.prev;
			kGallery.nextIdx = refImg.next;
			$('galleryBoxPreviewImg').src = refImg.url;
		},
		
		prev: function()
		{
			if( kGallery.prevIdx > -1 ){ kGallery.selectImage( kGallery.prevIdx ); }
		},
		
		next: function()
		{
			if( kGallery.nextIdx > -1 ){ kGallery.selectImage( kGallery.nextIdx ); }
		},
		
		showImage: function(_imgId)
		{
			this.select( this.getImageIndex(_imgId) );
		},
		
		getImageIndex: function(_imgId)
		{
			var idx = 0;
			for( var i=0; i<this.images.length-1;i++ )
			{
				if( this.images[i].id == _imgId )
				{
					idx = i;
				}
			}
			return idx;
		}
	};
}();


/**********************************************/
/* SECTIONS                                   */
/**********************************************/

function kSectionHandler(){ }
kSectionHandler.prototype.openClose = function(_sender,_targetID)
{
	if( _sender.className=='kSectionControlOpened' )
	{
		_sender.className = "kSectionControlClosed";
		$(_targetID).className = "kSectionClosed";
	}
	else
	{
		_sender.className = "kSectionControlOpened";
		$(_targetID).className = "kSectionOpened";
	} 
}
kSection = new kSectionHandler();

/**********************************************/
/* DEBUG                                      */
/**********************************************/

function TrkbDebug(){ };
TrkbDebug.prototype.on    = false;
TrkbDebug.prototype.trace = function (_text)
{
	if( !this.on ){ return "Debug is turned OFF!"; }
	this.show();
	document.getElementById("debugText").innerHTML += _text+"\n";
	document.getElementById("debugWin").scrollTop = document.getElementById("debugWin").scrollHeight;
}
TrkbDebug.prototype.show = function()
{
	document.getElementById("debugWin").style.display = "block";
}
TrkbDebug.prototype.hide = function()
{
	document.getElementById("debugWin").style.display = "none";
}
TrkbDebug.prototype.clear = function()
{
	document.getElementById("debugText").innerHTML = "";
}
var tdebug = new TrkbDebug();
