Ext.BLANK_IMAGE_URL = '/inc/ext22/resources/images/default/s.gif';

/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.ns('spp');

Ext.ux.IFrameComponent = Ext.extend(Ext.BoxComponent, {
     onRender : function(ct, position){
          this.el = ct.createChild({tag: 'iframe', id: 'iframe-'+ this.id, frameBorder: 0, src: this.url, width: '100%', height: '100%', scrolling: 'auto'});
     }
});


var cp = new Ext.state.CookieProvider({
       path: "/",
       expires: new Date(new Date().getTime()+(1000*60*60*24*180)), //180 days
       domain: "wirelessbroadbandsupport.com"
   });
Ext.state.Manager.setProvider(cp);
Ext.QuickTips.init();

 

 spp.stable = [];
 var prefArray;
 //cp.set('prefs', prefArray);


spp.ppp = 0;
 


Ext.onReady(function(){
	
	spp.loginfunc  = function(){

				var subObj = {
				               	url: "inc/functions/factory.php",
				                method: 'POST',
								params : {action: 'login'},
				                waitMsg:'Logging In...',
				                reset: true,
				                failure: function(form, action) {
				                    Ext.MessageBox.alert('Warning','Username or Password is incorrect ');
				                },
				                success: function(form, action) {
 							 
									Ext.getCmp('loginformpanel').hide();
									Ext.getCmp('logoutBtn').show();
									Ext.getCmp('changepassbtn').show();
										 
									Ext.getCmp('centerportal').remove('contpanel');
									var pCmp = Ext.ComponentMgr.create(portalConfig, 'portal');
									Ext.getCmp('centerportal').add(pCmp);
									loadPortal();
									Ext.getCmp('centerportal').doLayout();
									Ext.getCmp('centerportal').getTopToolbar().show();
							
				                }
				};


				Ext.getCmp('loginformpanel').getForm().submit(subObj);
			};
			
	//  END LOGIN FUNCTION
	
 
 
			function changepassword(){
				if(!spp.passwin){
				            spp.passwin = new Ext.Window({
								title		: 'Change Password',
			 					layout		: 'fit',
								stateful	: false,
								closable	: false,
				                width       : 300,
				                height      : 190,
								modal		: true,
				                closeAction : 'hide',
				                plain       : true,
								
				                items       : new Ext.form.FormPanel({
									id			: 'passformpanel',
									labelWidth  : 90,
									monitorValid: true,
									border      : false,
									autoScroll  : true,
									bodyStyle   : 'background-color: #CAD9EC; padding: 10px;',
									defaultType : 'textfield',
									defaults    : {
										width  : 120
									},
									items       : [

										{
											id		   : 'newpass',
										 	inputType  : 'password',
											fieldLabel : 'New Password',
											name       : 'newpass',
											allowBlank : false
										},
										{
								 			inputType  : 'password',
											fieldLabel : 'Verify Password',
											allowBlank : false,
											name       : 'new_pass_v',
							 				msgTarget  : 'side',
											validator  : function(fieldValue){
											    			if ( fieldValue == Ext.get('newpass').getValue() ){ 
																	return true;
																}else{
																	return 'Passwords do not match, please try again.';
																}
														 
											  }
										}


									],
									buttons : [
										{
											text    : 'Save',
											formBind: true,
											disabled: true,
											handler : function() {
												Ext.Ajax.request({
													url: "inc/functions/factory.php",
													method: "POST",
													timeout: 30000,
													autoAbort: false,
													disableCaching: true,
													params: {
														action	: "changepassword",
														pw		: Ext.get('newpass').getValue()
													}, 
													success: function(response, options) {
														Ext.MessageBox.alert('Message', 'Password Changed');
														return true;
													},
													failure: function(response, options) {
														Ext.MessageBox.alert('Message', 'Error Changing Password');
														return true;
													} 	
												});
												Ext.getCmp('passformpanel').getForm().reset();
												spp.passwin.hide();
											}
										},
										{
											text    : 'Cancel',
											handler : function() {
												Ext.getCmp('passformpanel').getForm().reset();
												spp.passwin.hide();
											}
										}
									]
								})

				            });
				        }
				        spp.passwin.show();


			}; // end change password
				
					
					
		function getpassword(){
			
			Ext.Msg.prompt('Retreive Login Info', 'Please enter your email address:', function(btn, text){
			    if (btn == 'ok'){
			        if ( text == '') {
						Ext.MessageBox.alert('Alert', 'Please enter something');
					}else{
						Ext.Ajax.request({
							url: "forgot.php",
							method: "POST",
							timeout: 30000,
							autoAbort: false,
							disableCaching: true,
							params: {
								e: text
							},	
						 
							success: function(response, options) {
								Ext.MessageBox.alert('Message', 'An email containing your login information has been sent to the provided address.');
								return true;
							},
							failure: function(response, options) {
								Ext.MessageBox.alert('Message', 'Error Retreiving Password');
								return true;
							} 	
						});
					};
			    } // end if
			});
			
			
		} // end getpassword
		
 			

		/*   GLOBAL EXTRAS   */
		
		spp.mainportalconfig = {
			id: 'mainportal',
            xtype:'portal',
			bodyStyle: "background-image:url(img/viewport_bg.gif);background-repeat:repeat",
            region:'center',
            margins:'10 0 0 0',	
			items:[{
				id: 'leftcol',
                columnWidth: 0.50,
                style:'padding:10px 10px 10px 10px;'

				},{
				id: 'rightcol',
                columnWidth: 0.50,
                style:'padding:10px 10px 10px 0px'
				  
					}

			], // end portal items

			listeners: {'drop': function(e){
				
					//Examine left and right columns and stable and write prefs to cookie
					
					var currLCitems = Ext.getCmp('leftcol').items.items;
					var currRCitems = Ext.getCmp('rightcol').items.items;
					var newLCArray = [];
					var newRCArray = [];
					
					prefArray = [];
					
					for (var i=0; i < currLCitems.length; i++) {
						newLCArray.push(currLCitems[i].id);
					}
					prefArray[0] = newLCArray;
					
					
					for (var j=0; j < currRCitems.length; j++) {
						newRCArray.push(currRCitems[j].id);
					}
					prefArray[1] = newRCArray;
					prefArray[2] = [];
					cp.set('prefs', prefArray);
					
				}}
	        };

		
		var contactform = new Ext.FormPanel({
					id: 'contactform',
	            	labelAlign: 'top',
					frame: false,
					border: false,
			        defaultType: 'textfield',
			 		bodyStyle: "font-size:10px",
					autoWidth:true,
					 defaults: {width: 190},

			        items: [
						{
			                fieldLabel: 'First Name',
			                name: 'first_name',
			                allowBlank:false
	 		            },{
				      	    fieldLabel: 'Last Name',
			                name: 'last_name',
			                allowBlank:false
		 		        },{
			                fieldLabel: 'Email',
			                name: 'email',
							allowBlank:false
	 		            },{
							fieldLabel: 'Comment',
							xtype:'textarea',
							name: 'comment'

						}
					] // end form items 
	    });
	
		contactform.addButton({
	        text: 'Submit',

	        handler: function(){
	            contactform.form.submit({
	                url:'contact.php', 
	                waitMsg:'Sending...',
	                success: function (form, action) {
	                    Ext.MessageBox.alert('Message', 'Comment Sent, thank you.');
	                },
	                failure:function(form, action) {
	                    Ext.MessageBox.alert('Message', 'There was a problem sending the comment');
	                }
	            });
	        }
	    }); 
	
 
	
		
 
    var viewport = new Ext.Viewport({
		id: 'mainview',
        layout:'border',
        items:[
			{
				region:'north',
				id: 'north-panel',
				height:90,
				margins: '0 0 0 0',
				items: [{
						id: 'portalheader',
						xtype: 'panel',
						frame: false,
						height: 90,
						autoWidth:true,
						html: "<div style='background:url(img/portal_bg2.jpg);background-repeat:repeat-x'><a target='_blank' href='http://www.motorola.com/'><img src='img/ChannelPortal_Cap.jpg' width='1200' height='90'></a></div>"
						}
					]
			},{
	            region:'west',
	            id:'west-panel',
	            title:'Welcome',
	            split:true,
	            width: 205,
	            collapsible: true,
	            margins:'10 0 0 0',
	            cmargins:'10 5 0 0',
	         
			 
	            layoutConfig:{
	                animate:true,
				 	fill:false
            	},
				
            	items: [
				{ 	xtype:'panel',
					collapsible	: true,
					id: 'accountpanel',
					iconCls:'user',
					title: 'Login',
					bodyStyle: 'background-image:url(img/home/navgrad.jpg);background-repeat:no-repeat;padding: 10px 10px 10px 10px',
					items:[
					{
						xtype		: 'form',
						
						id			: 'loginformpanel',
						labelAlign: 'top',
						labelWidth  : 70,
						monitorValid: true,
						border      : false,
						autoScroll  : true,
						bodyStyle: 'background-color: transparent;',
						defaultType : 'textfield',
						items       : [

							{
								id		   : 'uname',
							 	inputType  : 'text',
								fieldLabel : 'Username',
								name       : 'uname',
								allowBlank : false
							},{
								id		   : 'upass',
					 			inputType  : 'password',
								fieldLabel : 'Password',
								allowBlank : false,
								name       : 'upass',
				 				msgTarget  : 'side'
							}
						],
							buttons: [{
				                text     	: 'Login',
								id 			: 'loginBtn',
								formBind	: true,
				                handler  	: spp.loginfunc 
				            }]	// end buttons for form panel
						},		// end form panel
					 
					
					
							{
								xtype: 'button',
								id : 'logoutBtn',
								style: 'padding-top:10px;',
								text: 'Log Out',
								hidden:true,
								handler: function(){
									window.location.href= 'logout.php';
								}
							
							},
							{
								xtype: 'button',
								id : 'changepassbtn',
								style: 'padding-top:10px;',
								text: 'Change Password',
								hidden:true,
								handler: changepassword
								
							},
							{
								xtype: 'button',
								style: 'padding-top:10px;',
								text: 'Retrieve Password',
								handler: getpassword
								
							}
					]
				},
				{
					xtype: 'panel',
	                title:'Resource Links',
					collapsible: true,
					bodyStyle: "font-size:10px;padding:4px;",
	                iconCls:'nav',
					autoLoad: 'moto_links.php'
	        	},{
					xtype: 'panel',
					collapsible: true,
			        title:'Technical Training',
					bodyStyle: "font-size:10px;padding:4px;",
			        iconCls:'nav',
					autoLoad: 'moto_training.php'
				},{	
					xtype:'panel',
					collapsible: true,
					iconCls:'portlet_icon',
					id: 'stable',
					title: 'Available Portlets'
				},{
					xtype:'panel',
					collapsible: true,
					id: 'contactuspanel',
					title: "Contact Us/ Feedback",
					autoScroll:true,
				 
	                border:false,
					bodyStyle: "font-size:10px;padding:4px;",
					iconCls:'email',
					items:[contactform]
					
				}
				
				
				]
			 	 
			 
        }, {
			id: 'centerportal',
			region:'center',
            xtype:'panel',
			layout: 'fit',
			bodyStyle: "background-image:url(img/viewport_bg.gif);background-repeat:repeat",
			tbar:[{
			     	text:'Collapse All',
			     	iconCls : 'collapse',
					handler : function(){
							 	Ext.getCmp('leftcol').items.each(function(portlet){
									portlet.collapse(false);
								});
								
								Ext.getCmp('rightcol').items.each(function(portlet){
									portlet.collapse(false);
								});

							
					}
				},	{
					     	text:'Expand All',
					     	iconCls : 'expand',
							handler : function(){
									 	Ext.getCmp('leftcol').items.each(function(portlet){
											portlet.expand(false);
										});

										Ext.getCmp('rightcol').items.each(function(portlet){
											portlet.expand(false);
										});


							}
						}],
			items:[ 
					{
						xtype: 'panel',
						id	: 'contpanel',
						hidden: true,
						bodyStyle: "background-image:url(img/home/home_bg_mid.jpg);background-repeat:no-repeat",
						margins:'10 0 0 0',	
						layout: 'absolute',
						autoWidth : true, 
						 
						items:[		 
								{
									xtype	: 'panel',
									id		: "mainpanel", 
									width	: 805,
									height	: 373,
									x		: 50,
									y		: 50,
									html	: "<img src='img/home/SPP_Home_motosp_big.jpg' width='803' height='373' usemap='#map1'><div id='l1box' style='display:none' class='box1'><a href='http://products.adtrack.com/00383/pdfs/ServiceProviderGuidelines2009.pdf'>Guidelines</a><br><a href='http://products.adtrack.com/00383/service_app.asp?country=USA&tier='>US Application</a><br><a href='http://products.adtrack.com/00383/service_app.asp?country=Canada&tier='>Canadian Application</a></div>"
								},{
									xtype	: 'panel',
									id		: 'p1',
									width	: 190,
									height	: 142,
									x		: 50,
									y		: 440,
									html	: "<img src='img/home/SPP_Home_motosp_small.jpg' width='188' height='140'>"
				 
								},{
									xtype	: 'panel',
									id		: 'p2',
									width	: 190,
									height	: 142,
									x		: 255,
									y		: 440,
									html	: "<img src='img/home/SPP_Home_kit_small.jpg' width='188' height='140' usemap='#map2'>"
								},{
									xtype	: 'panel',
									 id		: 'p3',
									width	: 190,
									height	: 142,
									x		: 460,
									y		: 440,
									html	: "<img src='img/home/SPP_Home_ezine_small.jpg' width='188' height='140'>"
								},{
									xtype	: 'panel',
									 id		: 'p4',
									width	: 190,
									height	: 142,
									x		: 665,
									y		: 440,
									html	: "<img src='img/home/SPP_Home_spot_small.jpg' width='188' height='140'>"
								}
								]	// end portal items
							} // end single panel item
			] 	// end enclosing panel item 
		 
	        },{
				region:'south',
				id: 'south-panel',
				height:25,
				margins: '0 0 0 0',
				items: [{
						xtype: 'panel',
						frame: false,
						height: 90,
						autoWidth:true,
						bodyStyle: "background-color:#F2F2F2",
						autoLoad: 'footer.html'
						}
					]
			}
			

	
	] // end central portal
}); //end new viewport

//Ext.getCmp('accountpanel').expand();

	var currMainImg ='';
	var m = 'map1';
	var panelimg ={'p1':'SPP_Home_motosp_big.jpg','p2':'SPP_Home_kit_small.jpg'};

	function doUpdate(el){
		 var bigsrc = "<img src='img/home/"+ currMainImg +"' width='803' height='373' usemap='#"+ m +"'>";
		 Ext.getCmp(el.id).body.update(bigsrc);
		 
	}
	
	function doEffect(imgname,map){
		
		currMainImg = imgname;
		m = map;
		Ext.get('mainpanel').fadeOut({  
		                    easing: 'easeOut',  
		                    duration: .5,  
		                    remove: false,  
		                    useDisplay: true,
							callback : doUpdate
		                });
		
 
		Ext.get('mainpanel').fadeIn({  
		                    easing: 'easeIn',  
		                    duration: .5,  
		                    remove: false,  
		                    useDisplay: true  
		                });
		
		
	}


	Ext.getCmp('p1').body.on('click', function(){
	   	Ext.get('p1').frame('0099cc',1);
	//	doEffect('SPP_Home_motosp_big.jpg','map1');
	 Ext.getCmp('mainpanel').body.update(
		"<img src='img/home/SPP_Home_motosp_big.jpg' width='803' height='373' usemap='#map1'><div id='l1box' style='display:none' class='box1'><a href='http://products.adtrack.com/00383/pdfs/ServiceProviderGuidelines2009.pdf'>Guidelines</a><br><a href='http://products.adtrack.com/00383/service_app.asp?country=USA&tier='>US Application</a><br><a href='http://products.adtrack.com/00383/service_app.asp?country=Canada&tier='>Canadian Application</a></div>");
		
	 });
	
	Ext.getCmp('p2').body.on('click', function(){
	   	Ext.get('p2').frame('0099cc',1);
		doEffect('SPP_Home_kit_big.jpg','map2');
	 });
	
	Ext.getCmp('p3').body.on('click', function(){
	   	Ext.get('p3').frame('0099cc',1);
		doEffect('SPP_Home_ezine_big.jpg','');
		window.open('http://ezine.motorola.com/serviceprovider');
	 });
	
	Ext.getCmp('p4').body.on('click', function(){
	   	Ext.get('p4').frame('0099cc',1);
		doEffect('SPP_Home_spot_big.jpg','map3');
		window.open('http://motorola.mychannelportal.com/eblast/2010/docs/02/04/ARRAGrantWritingRebateGuidelinesExtended.pdf');
	 });	
	
	
	
	//determine if user is logged in and which portal to show
	Ext.Ajax.request({
		url: "inc/functions/factory.php",
		method: "POST",
 		disableCaching: true,
		autoAbort: false,
		params: {
			action: "logincheck"
		},
		success: function(response, options) {
 			 
			var res = Ext.decode(response.responseText);
			if(res.success == true) {
					 	Ext.getCmp('loginformpanel').hide();
						Ext.getCmp('logoutBtn').show();
						Ext.getCmp('changepassbtn').show();
							 
						Ext.getCmp('centerportal').remove('contpanel');
						var pCmp = Ext.ComponentMgr.create(portalConfig, 'portal');
						Ext.getCmp('centerportal').add(pCmp);
						loadPortal();
						Ext.getCmp('centerportal').doLayout();
						Ext.getCmp('centerportal').getTopToolbar().show();
				}else{
					Ext.getCmp('centerportal').getTopToolbar().hide();
					Ext.getCmp('contpanel').show();
				}
		 
			return true;
		},
		failure: function(response, options) {
			return true;
		},
		callback: function(response, options) {
			return true;
		}	
	});

 
}); // end onReady