

var GMF = {

	params: {},
	type: '',

	resetValue: function(obj){
								var value = obj.value;
								var title = obj.title;
								if(value=='' || value==' ' || value==null){
									obj.value = title;
								}
								else if(value==title){
									obj.value = '';
								}
							},

	prevTarget: '',
	currentTarget: '',
	over: '',
	eType: '',
	on:	'',
	jsonMapControlUrl: '',
	year: '',
	type: '',
	prevMapType: '',
	filtersType: '',
	onCleared: 0,
	overlibActive: 0,
	isImg: false,

	changeParameters: function(obj){
											if(obj.value!=' '){
												var name = obj.name;
												var value = obj.value;
												var data = name.split('_');
												
												this.type = data.pop();
												var end = this.type.substr(this.type.length-3,this.type.length);
												var rest = this.type.substr(0,this.type.length-3);
												//in case of hierselect occurence
												if(end=='[0]' || end=='[1]'){
													if(end=='[0]'){name = 'country_'+data[1];}
													else{name='region_'+data[1];}
													this.type = rest;
													eval('this.params.'+name+'= value+"|"+name;');
												}
												//in case of date
												else if(end=='[F]' || end=='[d]' || end=='[Y]'){
													name = name.substr(0,(name.length-(end.length+rest.length+1)));
													eval('var curVar = this.params.'+name+';');
													if(curVar==undefined){curVar = '0/0/0';}

													var tarVar;

													tarVar = curVar.split('/');
													tarVar[0]= tarVar[0]==undefined ? 0 : tarVar[0];
													tarVar[1]= tarVar[1]==undefined ? 0 : tarVar[1];
													tarVar[2]= tarVar[2]==undefined ? 0 : tarVar[2];

													if(end=='[F]'){value = value+'/'+tarVar[1]+'/'+tarVar[2];}
													if(end=='[d]'){value = tarVar[0]+'/'+value+'/'+tarVar[2];}
													if(end=='[Y]'){this.year = value+'-'+value;value = tarVar[0]+'/'+tarVar[1]+'/'+value;}
													eval('this.params.'+name+'= value+"|"+name;');												
												}
												else{
													name = data.join('_');
													eval('this.params.'+name+'= value+"|"+name;');
												}

//												this.params[name]=value+'|'+name;
											}
										},

	sendParams:	function(url,type) {
								this.type = this.type.substr(0,2);
								this.params.type=this.type+'|type';
								if(type!='timeOff'){
									if(this.year=='' && this.type == 'es'){
										alert('Year is required.');
										return false;
									}
								}
								GM.getUsersList(url,this.year,this.params);
//								this.params = [];
							},

	switchMode:	function(type,changeMapType){

								if(this.filtersType==type){
									return false;
								}

								if(changeMapType==undefined){
									changeMapType = true;
								}
								if(location.href.indexOf('/map')==-1){
									location.href='http://'+location.host+'/map#'+type;
									return false;
								}
								
								var mapType = false;

								switch(type){
									case 'qs':
										$('eventSearch','advancedSearch','forMeSearch').invoke('hide');
										$('filterDesc_es','filterDesc_as','filterDesc_fm').invoke('hide');
										$('quickSearch').show();
										$('filterDesc_qs').show();
										$('searchMode').innerHTML = 'Quick Search';	
										this.params = {};
										mapType = 'qs';
										this.clearOn();
										this.overlibActive = 0;
									break;
									case 'as':
										$('eventSearch','quickSearch','forMeSearch').invoke('hide');
										$('filterDesc_es','filterDesc_qs','filterDesc_fm').invoke('hide');
										$('advancedSearch').show();
										$('filterDesc_as').show();
										$('searchMode').innerHTML = 'Advanced Search';		
										this.params = {};
										mapType = 'as';
										this.clearOn();
										this.overlibActive = 0;
									break;
									case 'es':
										$('quickSearch','advancedSearch','forMeSearch').invoke('hide');
										$('filterDesc_qs','filterDesc_as','filterDesc_fm').invoke('hide');
										$('eventSearch').show();
										$('filterDesc_es').show();
										$('searchMode').innerHTML = 'Contact Search';		
										this.params = {};
										mapType = 'es';
									break;
									case 'fm':
										$('eventSearch','advancedSearch','quickSearch').invoke('hide');
										$('filterDesc_es','filterDesc_as','filterDesc_qs').invoke('hide');
										$('forMeSearch').show();
										$('filterDesc_fm').show();
										$('searchMode').innerHTML = 'Who Is Searching For You?';	
										this.params = {};
										mapType = 'fm';
									break;
								}
							
								this.type = mapType;

								
								if(this.filtersType!=mapType){
									var id = 'img_'+mapType;

									if($(id).src.substr(($(id).src.length-8),$(id).src.length)=='Over.gif'){
										$(id).src = $(id).src.substr(0,($(id).src.length-8))+'On.gif';
									}
									else if($(id).src.substr(($(id).src.length-8),$(id).src.length)!='Over.gif'){
										$(id).src = $(id).src.substr(0,($(id).src.length-4))+'On.gif';
									}
									if(this.filtersType.length==2){
										var oldId = 'img_'+this.filtersType;
										if($(oldId).src.substr(($(oldId).src.length-6),$(oldId).src.length)=='On.gif'){
											$(oldId).src = $(oldId).src.substr(0,($(oldId).src.length-6))+'.gif';
										}
									}

								}

								this.filtersType = mapType;
							
								GM.searchedUser = '';

								if(changeMapType===true && (mapType=='es' || mapType=='fm')){
									GM.getUsersList(this.jsonMapControlUrl+'&type='+mapType+'',this.year);
								}
								
								if(this.prevMapType=='es' || this.prevMapType=='fm'){
									GM.getUsersList(this.jsonMapControlUrl);
								}

								this.prevMapType=mapType;


								$('filterForm').show();
								$('filtersHeader').show();
								$('members_gallery').hide();
								$('gad').hide();
								$('filtersInner').style.height = '744px';
								GM.mapEnlarge();

							},

	initTimelineActions: function(e){
													var tmp = this.prevTarget;
													var target = e.target!=undefined ? e.target : e.srcElement;
													this.eType = e.type;
													var type = this.eType;

													if(target.src != undefined){
														this.isImg = true;
													}
													else{
														this.isImg = false;
														nd();
													}

												 	this.prevTarget = this.currentTarget;
													this.currentTarget = target;
													if(this.prevTarget==this.currentTarget){
														this.prevTarget = tmp;
													}

													if(target!=undefined){
														this.swapImg();
														this.timeChange();
													}

											 },

	swapImg:	function(){

								if(this.eType=='click'){
									if(GM.loaded===false){
										alert('Please wait, until all map markers are loaded.');
										return false;
									}
								}

							if((this.type!= 'es' && this.type!= 'fm') && this.eType=='mouseover'){
									if(this.isImg===true){
										overlib('Clicking on the timeline will automatically redirect you to the Contact Search page and display all available options.');
										var doNotOver = true;
//										return false;
									}
							}

							var cSrc = this.currentTarget.src;
							var pSrc = this.prevTarget.src;
							
							var cState = cSrc!=undefined ? cSrc.substr(0,cSrc.indexOf('.gif')) : '';
							var pState = pSrc!=undefined ? pSrc.substr(0,pSrc.indexOf('.gif')) : '';

							if(cState.substr(cState.length-4,cState.length)!='Over' && doNotOver!==true){
								if(cState.substr(cState.length-2,cState.length)!='On' && this.currentTarget.src!=undefined){
									cSrc = cState+'Over.gif';
									this.over = this.currentTarget;
							  }
							}
							
							if(this.eType=='click'/* && cState.substr(cState.length-4,cState.length)=='Over'*/){
								if(cState.substr(cState.length-2,cState.length)!='On' && this.currentTarget.src!=undefined){
									if(this.on!='' && this.onCleared == 0){
										var workPath =  this.on.src.substr(0,this.on.src.length-4);
										this.on.src = workPath.substr(0,workPath.length-2)+'.gif';
									}
									cSrc = cState.substr(cState.length-4,cState.length)=='Over' ? cState.substr(0,cState.length-4) : cState;
									cSrc = cSrc+'On.gif';
									this.on = this.currentTarget;
									this.onCleared = 0;
							  }								
							}

							if(pState.substr(pState.length-4,pState.length)=='Over'){
								if(this.prevTarget.src!=undefined){
									pSrc = pState.substr(0,pState.length-4)+'.gif';
								}
							}
							this.currentTarget.src = (cSrc!=undefined || cSrc!='') ? cSrc : this.currentTarget.src;
							if(this.eType=='mouseover'){


								this.prevTarget.src = (pSrc!=undefined || pSrc!='' ) ? pSrc : this.prevTarget.src;
							}
						},

	timeChange:	function(){



								if((this.type!= 'es' && this.type!= 'fm') && this.eType=='click'){
									this.year = this.on.id;
//									this.clearOn();
									this.clearOver();
									nd();

									if(this.overlibActive==0){
										this.overlibActive = 1;
										var startTimeout = 1;
									}
									else if(this.overlibActive==1){
//										nd();
//										this.overlibActive = 0;
										var startTimeout = 0;
									}

									if(startTimeout==1){
										window.setTimeout(function(){
											GMF.switchMode('es',false);
											GM.getUsersList(GMF.jsonMapControlUrl+'&type=es',GMF.year);
											GMF.clearOverlib;

											GMF.overlibActive = 0;
											nd();
										},1);									
									}

									return true;
								}

								if(this.eType=='click' && this.year != this.on.id){
									if(this.overlibActive==1){
										nd();
	                  this.overlibActive = 0;
									}

									this.year = this.on.id;
									GMF.sendParams(this.jsonMapControlUrl,'timeOff');
								}
								else if(this.eType=='click' && this.year == this.on.id){
									this.year = null;
									if(this.type!= 'es' && this.type!= 'fm'){
										GMF.switchMode('es',false);
										GM.getUsersList(this.jsonMapControlUrl+'&type=es',this.year);
									}
									else{
										GMF.sendParams(this.jsonMapControlUrl);
									}							
									GMF.clearOn();
								}

							},

	clearOver:	function(){
								//4 = '.gif'
								if(this.over){
									var workPath = this.over.src.substr(0,this.over.src.length-4);
									if(workPath.substr(workPath.length-4,workPath.length)=='Over'){
										this.over.src = workPath.substr(0,workPath.length-4)+'.gif';
									}
								}
							},

	clearOn:	function(){
								//4 = '.gif'
								if(this.on){
									var workPath = this.on.src.substr(0,this.on.src.length-4);
									if(workPath.substr(workPath.length-2,workPath.length)=='On'){
										this.on.src = workPath.substr(0,workPath.length-2)+'.gif';
										this.onCleared = 1;
									}
								}
							},

	clearOverlib:	function(){
									if(this.overlibActive==1){
										this.overlubActive=0;
										return nd();
									}
								}

};
