﻿Ext.useShims = true;

//Add to favorites.
function AddToFavorites(url,title)
{
	if(window.sidebar) //Mozilla Firefox Bookmark.
	{ 
		window.sidebar.addPanel(title, url, "");
	} 
	else if(window.external) //IE Favorite.
	{ 
		window.external.AddFavorite(url, title); 
	}
	else if(window.opera && window.print) //Opera Hotlist.
	{ 
		return true;
	}
}

var sendMessage_accountId, sendMessage_moduleId, sendMessage_itemId;
function ShowSendMessage(accountId, moduleId, itemId, message) {
    sendMessage_accountId = accountId;
    sendMessage_moduleId = moduleId;
    sendMessage_itemId = itemId;
    
    if (!message) message = '';
    
    Ext.MessageBox.show({
       title: 'הודעה פרטית',
       msg: 'תוכן ההודעה שתשלח:',
       value: message,
       width:300,
       modal:false,
       buttons: Ext.MessageBox.OKCANCEL,
       multiline: true,
       fn: SendMessage
   });
}

function SendMessage(btn, text){
    if (btn != 'ok') return;
    
    var commonObj = new AtpCommon();
    var r = commonObj.sendMessage(sendMessage_accountId, sendMessage_moduleId, sendMessage_itemId, text)
    
   	if (r.isSuccess) {
		Ext.AtpUtilities.msg(r.message, '');
	} else
		Ext.MessageBox.alert('הפעולה נכשלה', r.message);

}

function AddToMyFavorites(moduleId, itemId) {
    var commonObj = new AtpCommon();
    var r = commonObj.addToMyFavorit(moduleId, itemId);
    
	if (r.isSuccess) {
		Ext.AtpUtilities.msg(r.message, '');
	} else
		Ext.MessageBox.alert('הפעולה נכשלה', r.message);
}

var SendToFriend = function() {
    var dialog, formData;
    return {
        showDialog: function() {
            var title = 'שלח לחבר';
            
            if (!this.dialog) {
		        Ext.QuickTips.init();
    			Ext.form.Field.prototype.msgTarget = 'side';
		        this.dialog = new Ext.LayoutDialog("dlgCSendFriend", { 
                    width:520,
                    height:400,
                    shadow:true,
                    draggable: true,
                    resizable: false,
                    fixedcenter: true,
                    buttonAlign: 'left',
                    center: { autoScroll:false, alwaysShowTabs: false }
                });
                
    	        this.formData = new Ext.form.Form({
			        labelAlign: 'left',
			        buttonAlign: 'left'
		        });

                this.formData.column({width:250, labelWidth:50}); // open column, without auto close
		            this.formData.fieldset({legend:'פרטי השולח'},
			            new Ext.form.TextField({
			                id: 'email_senderName',
				            name: 'email_senderName',
				            fieldLabel: 'שם',
				            allowBlank:false
			            }),

				        new Ext.form.TextField({
					        fieldLabel:'דוא"ל',
					        id:'email_sender',
					        name:'email_sender',
					        cls: 'eng',
					        vtype: 'email',
					        allowBlank:false
				        })
				    );
			    this.formData.end();

                this.formData.column({width:250, labelWidth:50, style:'margin-right:5px;'}); // open column, without auto close
		            this.formData.fieldset({legend:'פרטי הנמען'},
			            new Ext.form.TextField({
			                id: 'email_destinationName',
				            name: 'email_destinationName',
				            fieldLabel: 'שם',
				            allowBlank:false
			            }),

				        new Ext.form.TextField({
					        fieldLabel:'דוא"ל',
					        id:'email_destination',
					        name:'email_destination',
					        cls: 'eng',
					        vtype: 'email',
					        allowBlank:false
				        })
                    );
			    this.formData.end();

			    this.formData.applyIfToFields({
				    width: 150
			    });

		        this.formData.fieldset({legend:'פרטי ההודעה', style:'clear:both;'},
			        new Ext.form.TextField({
			            id: 'email_subject',
				        name: 'email_subject',
				        fieldLabel: 'כותרת הודעה',
				        allowBlank:false
			        }),
        	        
			        new Ext.form.TextArea({
			            id: 'email_message',
				        name: 'email_message',
				        fieldLabel: 'תוכן ההודעה',
				        qtip:'תכנים בתוך סוגריים מרובעיים<br/>יוחלפו בעת השליחה באופן אוטומטי',
				        width: 355,
				        height: 130
			        }),

			        new Ext.form.TextField({
			            id: 'email_linkUrl',
				        name: 'email_linkUrl',
				        cls: 'eng',
				        fieldLabel: 'קישור לשליחה',
				        allowBlank:false
			        })
		        );

			    this.formData.applyIfToFields({
				    width: 355
			    });
        		
		        this.formData.render('dlgFSendFriend');
        		
	            var panel = new Ext.ContentPanel('dlgFSendFriend', 
	    	        {'title': 'test', fitToFrame:true, closable:true});
	            this.dialog.getLayout().add('center', panel);     

                this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
                this.dialog.addButton('שלח', this.sendMessage, this);
                this.dialog.addButton('סגור', this.dialog.hide, this.dialog);
                
            }
            this.formData.reset();
            
            this.formData.setValues({
                'email_linkUrl': location.href,
                'email_subject': 'קישור לתוכן שיעניין אותך',
                'email_message': 'היי [שם_הנמען],\n\nלהלן קישור שימצא חן בעינך:\n[קישור]\n\nמקווה שתהנה/י,\n[שם_השולח]'
            });
            
            this.dialog.setTitle(title);
            this.dialog.show();
        },  
        
        sendMessage: function () {
            if (!this.formData.isValid()){
                Ext.MessageBox.alert ('אימות נתונים', 'נא בדוק את הנתונים ונסה שוב');
                return;
            }
            
            var atp = new AtpCommon();
            var r = atp.sendToFriend(
                Ext.get('email_senderName').getValue(),
                Ext.get('email_sender').getValue(),
                Ext.get('email_destinationName').getValue(),
                Ext.get('email_destination').getValue(),
                Ext.get('email_subject').getValue(),
                Ext.get('email_message').getValue(),
                Ext.get('email_linkUrl').getValue()
            );
            
			if (r.isSuccess) {
			    this.dialog.hide();
				Ext.AtpUtilities.msg('סטאטוס השליחה', '{0}', r.message);
			} else
				Ext.MessageBox.alert('פעולת השליחה נכשלה', r.message);

        }
    };
}();

var DialogManager = function(){
    var dialog;
    
    // return a public interface
    return {
        hideDialog : function(){
			dialog.hide();
        },
        showDialog : function(title, link){
			Ext.get('dlgContainer').addClass('centeredLoadingIcon');
            if(!dialog){ // lazy initialize the dialog and only create it once
                dialog = new Ext.LayoutDialog("dlgContainer", { 
                        modal:true,
                        width:740,
                        height:555,
                        shadow:true,
                        draggable: false,
                        fixedcenter: true,
                        resizable: false,
	                    center: { autoScroll:false, alwaysShowTabs: false }
                });
                var iframe = Ext.DomHelper.append(document.body, 
					{id: 'dlgIframe', tag: 'iframe', allowtransparency:'true', scrolling: 'auto', frameBorder: 0, src: 'loading.html'});
				
				Ext.get(iframe).addListener('load', function() {
					//Ext.get('dlgContainer').removeClass ('centeredLoadingIcon');
				});
				
				var panel = new Ext.ContentPanel(iframe, 
					{'title': 'test', fitToFrame:true, closable:true});
				dialog.getLayout().add('center', panel);     
				dialog.on('hide', function() { document.getElementById('dlgIframe').src = 'loading.html'; });
                
                dialog.addKeyListener(27, dialog.hide, dialog);
                //dialog.addButton('Submit', dialog.hide, dialog);
                //dialog.addButton('Close', dialog.hide, dialog);
            } 
            
            document.getElementById('dlgIframe').src = link;
            
            dialog.setTitle(title);
            dialog.show();
        }
    };
}();

var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);}
