<?php 
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."init.php");

$projectId = $_POST['projectId'];
$tableName = $_POST['tableName'];
$questionId = $_POST['questionId'];
$languageId = $_POST['id_language'];
//we use the same form to show uniqe cawi links!
$title = $tableName;
if($questionId)
$title .= ' - '. $db->get($tableName,$questionId,'q_name',DB_WARPIT_WEBCATI);
else if($id_site = $_POST['id_site'])
{
	$title .= " - links";
}
//iframe source without question ID and show together! we add those to on reloading!
$src = "survey/preview.php?".
		http_build_query(array('projectId' => $projectId,
								'tableName' => $tableName,
								'id_user' => $_SESSION['UserInfo']['id'],
								'preview'=>true,
								'id_language' => $_POST['id_language'],
								'id_site' => $id_site ));

$iframe = 'iPreview_'.$tableName.'_'.$questionId;
echo "var p = new Ext.Panel({
	title:'$title',
	iconCls:'RMicon_view_details',
	layout:'absolute',";
if($questionId)	
echo "RMconn: new RMajax({
		url: 'webcati/_ajax/buildQuestionare/ajax.buildQuestionnaire.php',
		params: {action : 'getNextQuestion',id_project: $projectId },
		success: function(resp){
			r = Ext.decode(resp.responseText);
			p.ovCh.qId = r.id;
			p.setTitle('$tableName - '+r.qName);
			p.ovCh.refresh();
		}
	}),
	//buttons for moving between questions!
	tbar:[{
		iconCls:'RMicon_first',
		tooltip:'First Question',
		scale:'medium',
		handler:function(){
			p.RMconn.request({
				params: {
					qGet: 'first'
				}
			});
		}
	},'&nbsp;&nbsp;&nbsp;',{
		iconCls:'RMicon_back',
		tooltip:'Previous Question',
		scale:'medium',
		handler:function(){
			p.RMconn.request({
				params: {
					qGet: 'previous',
					qId: p.ovCh.qId
				}
			});
		}
	},'&nbsp;&nbsp;&nbsp;',{
		iconCls:'RMicon_forward',
		tooltip:'Next Question',
		scale:'medium',
		handler:function(){
			p.RMconn.request({
				params: {
					qGet: 'next',
					qId: p.ovCh.qId
				}
			});
		}
	},'&nbsp;&nbsp;&nbsp;',{
		iconCls:'RMicon_last',
		tooltip:'Last Question',
		scale:'medium',
		handler:function(){
			p.RMconn.request({
				params: {
					qGet: 'last'
				}
			});
		}
	}],";
echo "listeners:{
		show: function(){
			if(this.ovCh)
			this.ovCh.refresh();
           	lastActiveDesignerOverviewWindow = this;
		},
		afterrender:function(){
			this.getEl().on('click',function(e){
				var target = e.getTarget('td.RM_panel_designer_basic'+p.id_template);
	             	 if(target)
	             	 {
						if(p.prevSelected)
						{
							if(target.id == p.prevSelected.id) return;
							$('#'+ p.prevSelected.id ).removeClass('RM_panel_selected_overview');
						}
						$('#'+ target.id ).addClass('RM_panel_selected_overview');
						var pType = target.getAttribute('paneltype');
						if(pType == 3 || pType == 4)
							$('#'+p.editIcon.id).position({
								of: $('#'+ target.id ),
								at: 'right top',
								my: 'right top',
								offset:'-7 3'
							});
						else
							$('#'+p.editIcon.id).css('top',-10000);
						
						p.prevSelected = target;
						
					}
					else if(target = e.getTarget('.ux-cell-action'))
					{
						var pType = p.prevMoveOver.getAttribute('paneltype');
						if(pType == 3 || pType == 4)
						var win = new Ext.Window({
							title:'Editing',
							layout:'fit',
							height:350,
							width:500,
							modal:true,
							items:{
								xtype:'form',
								ref:'myForm',
								hideLabels:true,
								items:{
									xtype:'htmleditor',
									ref:'myEditor',
									value:p.prevMoveOver.innerHTML,
									name:'value'
								}
							},
							buttons: [{
							text:'Save',
							scope:this,
							handler:function(){
								win.myForm.getForm().submit(
								{
									url:'webcati/_ajax/buildQuestionare/ajax.buildQuestionnaire.php',
									params:{
										id_project:$projectId,
										RMcomp:'grid',
										RMtable:'$tableName',
										id_language:'$languageId',
										action:'update',
										field: pType == 3 ? 'vpr_sql' : 'nav_sql',
										id:p.ovCh.qId
									},
									success:function(){
											win.close();
											p.ovCh.refresh();
										
									},
									failure:function(){
											win.close();
											Ext.Msg.alert('Some error','Too bad');
									}
								});
							}
							}]
						}).show();
					}
					else
					{
						if(p.prevSelected)
						{
							$('#'+ p.prevSelected.id ).removeClass('RM_panel_selected_overview');
							$('#'+p.editIcon.id).css('top',-10000);
						}
						p.prevSelected = null;
					}
			});
			
			this.getEl().on('mousemove',function(e){
				var target = e.getTarget('td.RM_panel_designer_basic'+p.id_template);
	             	 if(target)
	             	 {
						if(p.prevMoveOver)
						{
							if(target.id == p.prevMoveOver.id) return;
						}
						var pType = target.getAttribute('paneltype');
						if(pType == 3 || pType == 4)
							$('#'+p.editIcon.id).position({
								of: $('#'+ target.id ),
								at: 'right top',
								my: 'right top',
								offset:'-7 3'
							});
						else
							$('#'+p.editIcon.id).css('top',-10000);
						
						p.prevMoveOver = target;
						
					}
					else if(target = e.getTarget('.ux-cell-action'))
					{
						
					}
					else
					{
						if(p.prevMoveOver)
						{
							$('#'+p.editIcon.id).css('top',-10000);
						}
						p.prevMoveOver = null;
					}
			});
			
		}
	},
	closable:true,
	items:[
	{
		qId:'$questionId',
		id:'$iframe',
		height:'100%',
		refresh: function(){
				if(this.rendered)
				//this.load({
				Ext.Ajax.request({	
					scripts:true,
					scope:this,
					url:'survey/preview.php',
					params:{
						'projectId': $projectId,
						'tableName':  '$tableName',
						'id_user': '{$_SESSION['UserInfo']['id']}',
						'preview':true,
						'id_language': '{$_POST['id_language']}',
						'id_site' : '$id_site',
						questionId: this.qId
				
					},
					success :function(r){
						this.update(r.responseText,true);
						this.ownerCt.id_template = $(this.el.dom).find('input.preview_template').val();
					}
				});
				$('#'+p.editIcon.id).css('top',-10000);
				p.prevSelected = null;
				p.prevMoveOver = null;
				//})
			},
		xtype:'container'
		,listeners:{
				
              afterrender:function(){
              	this.refresh();
              	this.ownerCt.ovCh = this;
              	lastActiveDesignerOverviewWindow = this;
              	
              }
             }
	},
	{
	xtype:'component',
	style:{ top:-10000 },
	ref:'editIcon',
	html:\"<div class='ux-cell-action RMicon_edit_record'>&nbsp;</div>\"
	}
	]
	/*html: '<iframe id=\"$iframe\" width=100% height=100% src=\"$src&".http_build_query(array('questionId' => $questionId,
								'id_showTogether' => $_POST['id_showTogether']
								)). "\"></iframe>'*/
});
mainTabPanel.add(p).show();
"

?>