var overlay = {
	
	loginDiv:			 'loginDiv',
	welcomeDiv:			 'welcomeInfo',
	infoDiv:	 'infoDiv',
	userInfoDiv:	 'userInfoDiv',
	overlayShadow: 'overlayShadow',
	userInfoIframe:'userInfoIframe',
	infoIframe:'infoIframe',
	galleryMainPhoto:	'main_photo',
	gmpContainer:	 'main_photo_tag',
	gmpInfo:	 		 'overDescDiv',

	url:					 'http://'+location.host+'/_json_/?mod=overlay_control',//url do jsona
	infoUrl:			 'http://'+location.host+'/profile-view/',//url do info
	eventsInfoUrl: 'http://'+location.host+'/profile-view/events/',//url do info
	blankUrl:			 'http://'+location.host+'/blank',//url do inf=o
	ifilesUrl:		 'http://'+location.host+'/files/ifiles',//url do info
	iEventId:			 '',
	sInfoType:		 'info',

	signed:				 undefined,
	sLogin:				 undefined,
	rLogin:				 undefined,
	iUserId:			 undefined,
	loginType:		 '!ext',

	_window:			 undefined,
	triggerSet:		 false,
	photosLimit:	 10,
	photosPage:		 1,
	pagesPerPage:	 3,
	naviSeparator:	' of ',
	iAll:					 undefined,

	//hack
	hckCaller: '',

	openInfo: function(login,bContinue,type,eventId){
							$('type').value='!ext';//tryb logowania

							if(typeof(GM)!='undefined'){
								this.sInfoType = GM.sMapType;
							}
							else{
								this.sInfoType = 'human';
							}

							if(login!=undefined && login!=''){this.rLogin = login;}
							if(eventId!=undefined){this.iEventId = eventId;}
							if(bContinue==undefined){this.checkForSignIn(undefined,'overlay.openInfo(undefined,true)');}

							if(typeof(GM)!='undefined'){
								GM.closeCloud();	
							}
						
							if(bContinue===true || this.signed===true || this.signed===false){
								if(this.signed==true){
									var data = this.getUserData('','overlay.openInfoWindow');
								}
//								else{if(this.signed==true){this.openInfo(undefined,true);}}
							}
						},
 
	checkForSignIn: function(req,callback){
										if(this.signed==undefined){
											if(req==undefined){
												GetRequest(this.url+'&a=check','overlay.checkForSignIn','','topLoader',callback);
											}
											else{
												var json = req.responseText;
									      var data = json.evalJSON();
									      var res = data;
	
												this.signed=res.success;
												this.sLogin = res.login;

												if(callback!=undefined){
													eval(callback);
												}
												if(this.signed==false){
													this.openOverlayWindow(this.loginDiv);
												}
											}
										}
										else if(this.signed==false){
											this.openOverlayWindow(this.loginDiv);
										}
										else if(this.signed==true && callback!=undefined){
											eval(callback);
										}
									},

	getUserData: function(req,callback){
								if(req==''){
								 	GetRequest(this.url+'&a=get:last_name,first_name|l='+this.rLogin+'','overlay.getUserData','','topLoader',callback);
								}
								else{
									var json = req.responseText;
                  var data = json.evalJSON();
                  var res = data.data;
									
									eval(callback+'(res)');
								}
							 },
	openInfoWindow: function(data){
										this.openOverlayWindow(this.userInfoDiv);
										if(data.last_name!=undefined && data.last_name!='' && data.last_name!=null){
											$('ifr_lname').innerHTML = data.last_name;
										}
										if(data.first_name!=undefined && data.first_name!='' && data.first_name!=null){
											$('ifr_fname').innerHTML = data.first_name;
										}
										
										if(this.sInfoType=='human' || this.sInfoType=='info'){
											url = this.infoUrl+this.rLogin+'.html'
										}
										else if(this.sInfoType=='event'){
											if(this.iEventId!=''){
												var eventId = '?id='+this.iEventId;
											}
											url = this.eventsInfoUrl+this.rLogin+'.html'+eventId;
										}
									
										$(this.userInfoIframe).src=url;
									},

	infoWindow: function(width,height,obj,title,loud){
											if(loud==true){
												$(this.infoIframe).src=this.blankUrl;
												$('overInfoTitle').innerHTML = title;

												if(width>0){
													$(this.infoDiv).style.width = width+'px';
													var innerWidth = (width*90)/100+'px';
													var postfix = '#w:'+innerWidth;
												}
												if(height>0){
													$(this.infoDiv).style.height = height+'px';
													$(this.infoIframe).style.height = (height-25)+'px'
												}
									
												window.setTimeout(function(){
														overlay.openOverlayWindow(overlay.infoDiv);
														$(overlay.infoIframe).src=obj.href+postfix;
													},100
												);
												return false;
											}
									},

	closeAll: function(){
							if($(this.overlayShadow)!=null){$(this.overlayShadow).hide();}
							if($(this.loginDiv)!=null){
								$(this.loginDiv).hide();
								$('l').value = '';
								$('p').value = '';
							}
							if($(this.userInfoDiv)!=null){$(this.userInfoDiv).hide();}
							if($(this.infoDiv)!=null){$(this.infoDiv).hide();}
							if($(this.userInfoIframe)!=null){$(this.userInfoIframe).src=this.blankUrl;}
							if($(this.infoIframe)!=null){$(this.infoIframe).src=this.blankUrl;}
							if($('overError')!=null){$('overError').hide();}

							if($('ifr_lname')!=null){$('ifr_lname').innerHTML = '';}
							if($('ifr_fname')!=null){$('ifr_fname').innerHTML = '';}

						},

	openOverlayWindow: function(windowId){
											$(this.overlayShadow).show();
											$(windowId).show();
											this._window = windowId;
											this.positionWindows();
									 },


	positionWindows: function(){
										 if(this._window!=undefined){

											if(this.triggerSet===false){
												this.triggerSet = true;
												window.onresize = function(){
																						if(typeof(overlay._window)=='object'){
								                              overlay.positionWindows(overlay._window.id);
								                            }
								                            else{
								                              overlay.positionWindows(overlay._window);
								                            }
																					}
											}

											this._window = $(this._window);
											
											var oneWidth = $(this.overlayShadow).offsetWidth;
									 		var oneHeight = $(this.overlayShadow).offsetHeight;
									 		
											var twoWidth = this._window.offsetWidth;
									 		var twoHeight = this._window.offsetHeight;

											var id = this._window.id;
											
											$(id).style.left = ((Number(oneWidth)/2)-(Number(twoWidth)-(Number(twoWidth)/2)))+'px';
											if(oneHeight>Number(twoHeight)+Number(twoHeight)/2){
												$(id).style.position = 'fixed';
												$(id).style.top = ((Number(oneHeight)/2)-(Number(twoHeight)-(Number(twoHeight)/2)))+'px';
											}
											else if(oneHeight>Number(twoHeight)+100){
												$(id).style.position = 'fixed';
												$(id).style.top = ((Number(oneHeight)-Number(twoHeight))/2)+'px';
											}
											else{
												$(id).style.position = 'absolute';
												$(id).style.top = '10px';
											}
										 }
									 },
	
	login: function(req,loginId,passId,type,caller){
					 	if(req==undefined){
							if(this.hckCaller=='' || caller!=undefined){
								this.hckCaller = caller
							}
							if(caller==undefined){
								caller = this.hckCaller;
//								alert('asd');
							}
//							alert(caller.type);
							if((caller.type=='keypress' && (caller.which==13 || caller.keyCode==13)) || caller.type=='click'){
						 		var login = $(loginId).value;
								var pass = $(passId).value;
								this.loginType = type;
								$(loginId).value = '';
								$(passId).value = '';
		
							  sPostParams={login:login,password:pass};
							  PostRequest(this.url+'&a=login', sPostParams, 'overlay.login', 'topLoader', false);
							}
						}
						else{
							var json = req.responseText;
				      var data = json.evalJSON();
				      var res = data;
		
							this.signed=res.success;
							if(this.signed===true){
								$('overError').hide();
								this.sLogin = res.login;
								$(this.loginDiv).hide();
								$('loggedOut').hide();
								$('logged').show();
								$('welcomeLogin').innerHTML = '<b>Welcome '+this.sLogin+'</b>';
								this.closeAll();


								if(location.pathname == '/'){
									location.assign('http://'+location.host+'/map/');
									return true;
								}

								if(this.loginType=='!ext'){
									this.openInfo(undefined,true);
								}
								else if(location.href.indexOf('/map/')==-1){
									window.location.reload();
								}
							}
							else{
								this.showError(res.message);
							}
						}
				 },

	logout: function(req){
					 	if(req==undefined){
							GetRequest(this.url+'&a=logout','overlay.logout','','topLoader');
						}
						else{
							var json = req.responseText;
				      var data = json.evalJSON();
				      var res = data;
		
							this.signed=res.success;
							if(this.signed===false){
								$('loggedOut').show();
								$('logged').hide();
								if(location.href.indexOf('/map/')==-1){
									window.location.reload();
								}
							}
						}
				 },

//social functions

addFriend: function(req,bContinue,login){
						 	if(bContinue==undefined || bContinue==''){
								this.checkForSignIn(undefined,'overlay.addFriend(undefined,true,\''+login+'\')');
							}
							if(bContinue==true){
								if(req==undefined || req==''){
									GetRequest(this.url+'&a=friend&login='+login+'','overlay.addFriend','','topLoader',bContinue);
								}
								else{
									var json = req.responseText;
						      var data = json.evalJSON();
						      var res = data;

									this.showError(res.success);
								}
							}
					 },

loadMainPhoto:	function(req,id,byWhat,mode){
									if(req==undefined || req==''){
										id = id.toString();
										var check = id.split('|');
										if(check.length>1){
											//po zapisaniu id w formie [id]|[nazwa funkcji], po wykonaniu zapytania, zostanie wykonana funkcja zdefiniowana jako nazwa funkcji
											var aid = check;
											id = aid[0];
											var callback = aid[1]+'_'+id+'_'+mode;
										}
										else{
											var callback = mode;
										}
										if(byWhat==undefined || byWhat==''){byWhat = 'pid';}
										if(mode==undefined){mode = '';}

										GetRequest(this.url+'&a=load'+mode+'&'+byWhat+'='+id+'','overlay.loadMainPhoto','','topLoader',callback);
									}
									else{

										var json = req.responseText;
							      var data = json.evalJSON();
                    var res = data.data;

//										window.setTimeout(function(){
													$(overlay.galleryMainPhoto).style.backgroundImage = "url("+overlay.ifilesUrl+res.normal_path+")";
//												$(overlay.gmpContainer).src = overlay.ifilesUrl+res.normal_path;
//												$(overlay.gmpContainer).width = res.normal_width;
//												$(overlay.gmpContainer).height = res.normal_height;
//												$(overlay.gmpContainer).show();
//											},25
//										);
								
										var title = res.title != undefined ? res.title : 'no title';
										var desc = res.description != undefined ? res.description : 'no description';

										var HTML = '<div style="font-weight:bold;">'+title+'</div>';
										HTML += '<div>'+desc+'</div>';

										$(this.gmpInfo).innerHTML = HTML;

										var aid = id.split('_');
										if(aid.length==1){
											mode = aid;
										}
										else{
											mode = aid[2];
										}

										if(id!=undefined && mode == ''){
											var _return = {
												src:	this.ifilesUrl+res.normal_path,
												width:	res.normal_width,
												height:	res.normal_height,
												title:	res.title,
												desc:		res.description
											};

											eval(aid[0]+'('+aid[1]+','+_return+')');
											d.split('|')
										}

										else if(mode=='Big' || mode=='prev' || mode=='next'){
											if(mode=='Big'){
												this.iAll = res.all;
												this.generatePages();
											}
											
											if(res.prev!=null && res.prev!='' && res.prev!=undefined){
												$('main_prev').innerHTML = '<a title="'+res.prev+'" href="javascript:;" onclick="overlay.loadMainPhoto(\'\','+res.prev+',\'pid\',\'prev\');">&laquo Previous</a>';
												$('main_prev').show();
											}
											else{$('main_prev').hide();}

											if(res.next!=null && res.next!='' && res.next!=undefined){
												$('main_next').innerHTML = '<a href="javascript:;" title="'+res.next+'" onclick="overlay.loadMainPhoto(\'\','+res.next+',\'pid\',\'next\');">Next &raquo;</a>';
												$('main_next').show();
											}
											else{$('main_next').hide();}
										}

//	  							if(id==undefined){

//  								}
									}
								},

loadPhotoBasics:	function(req,login){
										if(req==undefined){
											GetRequest(this.url+'&a=get:id|l='+login+'','overlay.loadPhotoBasics','','topLoader');
										}
										else{
											var json = req.responseText;
								      var data = json.evalJSON();
	                    var res = data.data;

											if(this.iUserId==undefined){
												this.iUserId = res.id;
											}

											this.loadMainPhoto('',this.iUserId,'uid','Big');
										}
									},

generatePages:	function(){
									var iAll = this.iAll;
									var iAllPages = Math.ceil(Number(iAll)/Number(this.photosLimit));
									
									var navi = '';
									if(this.pagesPerPage+this.photosPage<=iAllPages){
										var limit = this.pagesPerPage+this.photosPage;
									}
									else{
										var limit = iAllPages+1;
									}
									for(var i = this.photosPage-1;i<limit;i++){
										if(i!=0){
											if(i==this.photosPage){
												navi += '<b>'+i+'</b>';
											}
											else{
												navi += '<a href="javascript:;" onclick="overlay.changePage(undefined,'+i+')"';
												navi += '>['+i+']</a>';										
											}
										}
									}
									if(iAllPages==0){
										navi = '';
									}
									else{
										if(this.photosPage==iAllPages){
											navi += this.naviSeparator+'<b>'+iAllPages+'</b>';
										}
										else{
											navi += this.naviSeparator+'<a href="javascript:;" onclick="overlay.changePage(undefined,'+iAllPages+')">['+iAllPages+']</a>';
										}
										$('photoListNavi').innerHTML = 'Page:<br />'+navi;
									}
								},

changePage:	function(req,iPage){
							if(req==undefined || req==''){
								this.photosPage=iPage;
								GetRequest(this.url+'&a=loadPage&p='+iPage+'&l='+this.photosLimit+'&uid='+this.iUserId+'','overlay.changePage','','topLoader',iPage);
							}
							else{

								var json = req.responseText;
                var data = json.evalJSON();
                var res = data;

								var _HTML = '';
								
								res.photos.each(
											function(item){
												_HTML += '<div style="float:left;margin:1px;width:49px;height:49px;background: #ffffff url('+overlay.ifilesUrl+item.mini_path+') no-repeat center;"  onclick="overlay.loadMainPhoto(\'\','+item.id+',\'pid\',\'next\')" >';
												_HTML += '&nbsp;';
												_HTML += '</div>';
											}
										);

								$('photos_list').innerHTML = _HTML;

								this.generatePages()
							}
						},

sendMessage: function(req,fields_ids){
							if(req==undefined || req == ''){
								var ids = fields_ids.split(',');
								var sPostParams={subject:$(ids[0]).value,content:$(ids[1]).value,receiver:ids[2]};
							  PostRequest(this.url+'&a=message', sPostParams, 'overlay.sendMessage', 'topLoader', false); 
							}
							else{
								var json = req.responseText;
                var data = json.evalJSON();
                var res = data;

								this.showError(res.success);
							}
						 },

showError:	function(message){
							$('overError').style.display='block';
							$('overError').innerHTML = message;
						}


}
