jQuery(document).ready(cy_set_main_actions);

function cy_set_main_actions(){
	var arr_actions=[
		{'do_type':'main_logout','do_after':cy_do_after_logout,'do_before':false}
//Tests: {'do_type':'main_logout','do_after':false,'do_before':false}
	];
	for(var i=0;i<arr_actions.length;i++){
		//jQuery("div#"+blockId+" a[class="+arr_actions[i].do_type+"]").livequery('click',
		jQuery("a."+arr_actions[i].do_type).livequery('click',
		//jQuery("a."+arr_actions[i].do_type).each(//livequery('click',
			executeData
			//function(){jQuery(this).livequery('click',executeData);}
			//function(){jQuery(this).bind('click',executeData);}
		);
	}

	function cy_find_func(do_type){
		var exec_func=false;
		for(var j=0;j<arr_actions.length;j++){
			if(arr_actions[j].do_type==do_type){
				exec_func=arr_actions[j].do_after;
				break;
			}
		}
		return exec_func;
	}

	function cy_find_func_before_do_action(do_type){
		var exec_func=false;
		for(var j=0;j<arr_actions.length;j++){
			if(arr_actions[j].do_type==do_type){
				exec_func=arr_actions[j].do_before;
				break;
			}
		}
		return exec_func;
	}

	function cy_do_after_logout(response){
		//if(window['FBConnect']){
		//Variable is added by Alex
		if(window['FB_is_logged']){
			FBConnect.logout();
		}else{
			cy_do_reload(response);
		}
	}

	function cy_do_reload(response){
		window.location.reload();
	}

	function cy_do_before_delete(){
		return confirm("Are you sure?");
	}

		function executeData(Wnd){//no Wnd
//if(typeof window['console']!='undefined'){
//try{
//console.log('in');
//}catch(e){}
//}
			jQuery('#ajax-loader').toggle();
//alert(jQuery(this).attr('href'));
			var frpage = jQuery(this).attr('href');
			frpage = frpage.split('?_wpnonce=');
			var do_action='main_action';
			var do_type=jQuery(this).attr('class');

			var execute_func_before_do_action=cy_find_func_before_do_action(do_type);
			if(execute_func_before_do_action){
				var res_func=execute_func_before_do_action();
				if(!res_func){return false;}
			}

//alert('ok2 '+frpage[1]+' : '+frpage[0]+' :: '+do_type);return false;
			//Call from iframe without loaded in 
//alert(frpage+' '+do_type+' '+execute_func_before_do_action);
//alert(ajaxurl);
			(jQuery.post?jQuery:top.jQuery).post( ajaxurl, {
				'action': do_action,
				'cookie': encodeURIComponent(document.cookie),
				'_wpnonce': frpage[1],
				'href': frpage[0],
				'do_type': do_type
			},
			function(response)
			{
//alert(frpage+' :: '+do_type+' '+execute_func_before_do_action);
//alert(response.content+':'+response.errors);
				(jQuery.post?window:top).jQuery('#ajax-loader').toggle();

				var execute_func=cy_find_func(do_type);
				if(execute_func){execute_func(response);}
			},
			"json"
			);
			return false;
		}

}



function scrollLock(e) {
    var a=e.data.pos;
    window.scrollTo(a[0],a[1]);
    return false;
}

function cy_m_onOpen() {
	jQuery("body").css("overflow", "hidden"); 

    var a = [
        self.pageXOffset ||
        document.documentElement.scrollLeft ||
        document.body.scrollLeft
        ,
        self.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop
        ];
    jQuery(window).bind('scroll',{pos: a},scrollLock);
    
}

function cy_m_onClose() {
    jQuery(window).unbind('scroll',scrollLock); // unbinds lock
    jQuery("body").css("overflow", "auto"); 
}



//Dialogs
var cy_dialogs={
	dialogsIds:{},
	defaultOptions:{//Only different from standard default
	        autoOpen: false,
	        bgiframe: true,
    		width: 350,
	        height: 220,
	        modal: true,
	        title: 'Yellowpages',
			//draggable: false, // modified on 2010-03-20 by Alex Milanov
			resizable: false
	},
	openDialog:function(Wnd_id,obj){
		
		//cy_m_onOpen();
		
		if(!Wnd_id){return false;}
		if(this.dialogsIds[Wnd_id]){
			//jQuery("#"+this.dialogsIds[Wnd_id]).dialog('open');
			var id=this.createDialog(Wnd_id,obj);
			jQuery("#"+id).dialog('open');
			return this.dialogsIds[Wnd_id];
		}else{
			var id=this.createDialog(Wnd_id,obj);
			jQuery("#"+id).dialog('open');
			return id;
		}
	},
	createDialog:function(Wnd_id,obj){
		
		if(!Wnd_id){return false;}
		if(!obj||typeof(obj)!='object'){obj={};}
		var id=(typeof(obj.id)!='undefined'&&obj.id?obj.id:'d'+new Date().getTime());//cTimestamp);
		var frame_html='';
		if(obj.framed||obj.src){
			frame_html='<iframe onload="cy_dialogs.hideLoading(\''+id+'\');" class="dialog_frame" frameborder="no" style="width:100%;height:100%;border:none;" src="'+(typeof(obj.src)!=undefined&&obj.src?obj.src:'about:blank')+'"></iframe>';
		}
		var dialog_html='<div id="'+id+'" style="display:none;">'+frame_html+'</div>';
		if(this.dialogsIds[Wnd_id]){this.destroyDialog(Wnd_id);}
		jQuery("body").append(dialog_html);
		for(var attr in this.defaultOptions){
			if(typeof(obj[attr])=='undefined'){
				obj[attr]=this.defaultOptions[attr];
			}
		}
		if(typeof obj['doOnClose']=='undefined'){
			//obj.doOnClose='reload';
			obj.doOnClose='none';
		}
		obj.close=this.onClose;

		if(obj.width&&obj.width.toString().indexOf('px')<1){obj.width+='px';}
		//if(obj.height&&obj.height.toString().indexOf('px')<1){obj.height+='px';}
	        jQuery("#"+id).dialog(obj);
	        this.dialogsIds[Wnd_id]=id;

		var offset=-10;
		var height_loader='100%';
		if(obj.height){height_loader=(obj.height+offset)+'px';}
		var width_loader='100%';
		//if(obj.width){width_loader=(obj.width+offset)+'px';}
		jQuery('<div id="loader_'+id+'" style="position:absolute;z-index:1000;height:'+height_loader+';width:'+width_loader+';padding:0 100px 10px 0;background:url(\'/wp-content/themes/bp-default/_inc/images/ajax-loader.gif\') no-repeat center center;"></div>').insertBefore("#"+id);
	        
	        return id;
	},
	destroyDialog:function(Wnd_id){
		//alert('destroyDialog');
		if(!Wnd_id){return false;}
		if(this.dialogsIds[Wnd_id]){
			//alert(jQuery("#"+this.dialogsIds[Wnd_id]).dialog('option','doOnClose'));
			jQuery("#"+this.dialogsIds[Wnd_id]).dialog('destroy');
			jQuery("#"+this.dialogsIds[Wnd_id]).remove();
			delete this.dialogsIds[Wnd_id];
			return true;
		}
		return false;
	},
	setContent:function(Wnd_id,content_html){
		if(!Wnd_id){return false;}
		if(this.dialogsIds[Wnd_id]){
			jQuery("#"+this.dialogsIds[Wnd_id]).html(content_html);
			return true;
		}
		return false;
	},
	onClose:function(event,ui){
		//cy_m_onClose();
		//alert('onClose:'+jQuery(event.target).dialog('option','doOnClose'));
		switch(jQuery(event.target).dialog('option','doOnClose')){
		case 'reload':
			if(typeof event.view!='undefined'){
				event.view.location.reload(true);
			}else{
				window.location.reload(true);
			}
			break;
		case 'refresh':
			//"this" not same object:this.reloadPage(null);
			var notRealWnd={parent:window};
			cy_dialogs.reloadPage(notRealWnd);
			break;
		default:
			/*
			var onCloseVal=jQuery(event.target).dialog('option','doOnClose');
			if(onCloseVal.indexOf('func_')===0){
			alert('Super');
				var funcOnClose=onCloseVal.substr(5);
				if(typeof window[funcOnClose]=='function'){
					window[funcOnClose]();
				}
			}*/
			cy_dialogs.destroyDialog(event.target.id);//better by players
		}
		//for(var i in event){
		//	alert(i+' : '+event[i]);
		//}
	},
	reloadPage:function(Wnd){
		try{
			if(Wnd.parent!=Wnd){
				var pWnd=Wnd.parent;
				//if(document.body.attachEvent){
				//document.body.attachEvent("onunload",function(){alert(parent);});
				//document.body.attachEvent("onload",function(){window.close();});
				//}else if(document.body.addEventListener){
				//document.body.addEventListener("unload",'s1',false);
				//document.body.addEventListener("load",'s2',false);
				//top.document.body.addEventListener("unload",'GUnload',false);
				//}

				/*var all_iframes=pWnd.document.getElementsByTagName("iframe");
				for(var i=0;i<all_iframes.length;i++){
					if(all_iframes[i].className=='dialog_frame'){
						var parentEl=all_iframes[i].parentNode;
						parentEl.removeChild(all_iframes[i]);
						//this.destroyDialog(parentEl.id);
					}
				}*/
				//Unknown problem with geo_mashup on Firefox: pWnd.location.reload(true);
				//Not reload: pWnd.history.go(0);
				//pWnd.location.href=pWnd.location.href;
				//pWnd.location.replace(pWnd.location.href);
				var url_href=pWnd.location.href.replace(/(\?|\&)nDate=\d+/,'');
				url_href+=url_href.indexOf('?')==-1?'?':'&';
				var nDate=new Date();
				url_href+='nDate='+nDate.getTime();
				pWnd.location.replace(url_href);
			}
		}catch(e){alert(3);}
	},
	hideLoading:function(id){
		var loader_id='loader_'+id;
		jQuery('#'+loader_id).hide();
	}
	
};


