function checkEnter(e) {
	var characterCode

	if (e && e.which) {
		e = e
		characterCode = e.which
	} else {
		e = event
		characterCode = e.keyCode
	}
	
	if(characterCode == 13) {
		document.forms[0].submit()
		return false
	} else {
		return true
	}

}

function redir(){
	window.location = window.location;
}

//-------------------------------------------------------------------------------------------------------------- EXT JS DIALOG

if(typeof(Ext)!= 'undefined'){

	
	var dialog,subject;
	
	Ext.onReady(function() {
		
		if(typeof(labels) == "undefined") return;
		Ext.QuickTips.init();
		Ext.form.Field.prototype.msgTarget = 'under';
		if(!dialog){
			subject = new Ext.form.Field({
				hideLabel: true,name: 'Относно',bodyStyle:"margin-bottom:5px",
				width:'94%',allowBlank:false
			});
			var fs = new Ext.FormPanel({
				labelAlign:"left",
				waitMsgTarget: true,
				url :"http://"+window.location.hostname+"/"+lang+"/products/sendFriend",
				border:false,
				id:"formPanelMain",
				items:[{
					layout:'column',
					border:false,
					items:[{
						columnWidth:.5,
						layout: 'form',
						border:false,
						labelWidth: 120,
						items: [{
							fieldLabel:labels['prod_name'],name: 'prod_name', value: prodName,
							xtype:'textfield',tabindex: 1,width:'82%',allowBlank:false,blankText:labels['field_required'],invalidText:labels['field_required']
						},{
							fieldLabel:labels['target_mail'],name: 'target_mail',
							xtype:'textfield',tabindex: 3,width:'82%',allowBlank:false,blankText:labels['field_required'],vtype:"email",vtypeText:labels['invalid_mail']
						}]
					},{
						columnWidth:.5,
						layout: 'form',
						border:false,
						labelWidth: 120,
						items: [{
							fieldLabel:labels['prod_code'],name: 'prod_code',value: prodCode ,
							xtype:'textfield',tabindex: 2,width:'82%',allowBlank:false,blankText:labels['field_required'],minText:0
						},{
							fieldLabel:labels['sender_mail'],name: 'sender_mail',
							xtype:'textfield',tabindex: 4,width:'82%',allowBlank:false,blankText:labels['field_required'],minText:0,vtype:"email",vtypeText:labels['invalid_mail']
						}]
					},{
						columnWidth:"1",
						layout: 'form',
						border:false,
						labelWidth: 120,
						items: [{
							fieldLabel:labels['about'],name: 'about',
							xtype:'textfield',tabindex: 5,width:493,allowBlank:false,blankText:labels['field_required']
						}]
					}]
				},
				{html:labels['comment'],border:false,bodyStyle:"margin-bottom:8px;"},{
					hideLabel: true,name: 'comment',
					xtype:'textarea',tabindex: 6,width:'94%',height: 100
				},{
					name: 'id',value: 'test' ,
				    xtype:'hidden',id:'prodId'
			    }]
			}); 
			
			dialog = new Ext.Window({
				layout:'fit',title:labels['send_friend'],width:680,height:340,closeAction:'hide',closable : true,shadow: true,modal: false,resizable : false,bodyStyle: "padding: 10px;background:white;",
				items: [fs],
				buttons: [{
					text:labels['send'],
					type: "submit",
					handler:function() {
						if(fs.form.isValid()) {
							fs.form.submit({
								waitMsg: labels['wait'],
								success: function(fs, action) {
									dialog.hide();
									fs.reset();
									Ext.MessageBox.alert(action.result.title, action.result.message);
								} 
							})
						}
					}
				},{
					text: labels['close'],
					handler: function(){
						dialog.hide();
					}
				}]
			});
		}
	});
	
	
	
	function showForm(id){
		Ext.getCmp('prodId').setValue(id);
		dialog.show();
	}
}

var loadedGalleries = new Array();
var galleryImages = new Array();
var submitedImages = new Array();
var submitedLinks = new Array();





function prepareGallery(images,id,start){
	
    
	loadedGalleries[id] = new Array();
	submitedImages[id] = images;
	submitedLinks[id] = rotatorLinks;
	
	var  a;
	
	
	
	galleryImages[id] = images.length-2;
	loadedGalleries[id][0] = new Image();
	loadedGalleries[id][0].src = images[0];
	
	loadedGalleries[id][1] = new Image();
	loadedGalleries[id][1].onload = function(){ 
		     
		     setTimeout(function() {
				rotateAll(id,2);
			},3000);
		}
	loadedGalleries[id][1].src = images[1];
	
}



function rotateAll(id,index){
	
	galleryImages[id]--;
	
	
	if(galleryImages[id]>=0){
		
		loadedGalleries[id][index] = new Image();
		
		loadedGalleries[id][index].onload = function(){
			setTimeout(function() {
				rotateAll(id,index+1);
			}, 3000);
		}
		loadedGalleries[id][index].src = submitedImages[id][index];
		
		var way = "in";
		var myFx = new Fx.Tween($(id),{
			onComplete:function() {
			  if(way == "in"){	
			    $(id).src = loadedGalleries[id][index].src;
			    
			    $('rotatorLink').href=submitedLinks[id][index];
				myFx.start("opacity",0,1); 
				
				way = "aout";
			  }
			}
		});
	
        
		myFx.start("opacity",1,0);
	}else{
		if(galleryImages[id]==-1){ 
            
			index=0; 
			
			
		}
		
		var way = "in";
		var myFx = new Fx.Tween($(id),{
			onComplete:function() {
			  if(way == "in"){	
			  	
			    $(id).src = loadedGalleries[id][index].src;
			    
			    $('rotatorLink').href=submitedLinks[id][index];
				myFx.start("opacity",0,1);
				
				if(index<loadedGalleries[id].length-1){
					index++;
				}else{
					index=0;
				}
				
				setTimeout('rotateAll("'+id+'",'+index+')',5000);
				
				way = "aout";
			  }
			}
		});
	
        
		myFx.start("opacity",1,0);
		
		
		
		
		
	}
	
}
