window.addEvent('domready', function(){
if($('form1') != null){new NaMooForm();}
if($$('a[rel=NaMoo]') != null){new NaMooBox();}
if($('image_fade') != null){new ImageFade('image_fade');}

	var obj = new Swiff('queen/pic/queen_bg2.swf', {
    width:  980, 
    height: 730, 
    container: $('container1'),
	params: {
        wmode: 'transparent',
        bgcolor: '#FFFFA5',
		menu:'false'
    }
	});

//TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS 
var tooltip = new Element('div',{'id': 'tooltip'}).inject(document.body,'top');
tooltip.setStyles({
	'background':'#000',
	'opacity':0,
	'font-size':18,
	'color':'#fff',
	'border':'1px solid #fff',
	'padding':'8px',
	'position':'absolute',
	'z-index':1000
});

$$('.tooltip').addEvents({
	'mousemove': function(e){
		tooltip.setStyles({'top':e.page.y +10});
		tooltip.setStyles({'left':e.page.x + 10});
	},
	'mouseenter': function(){
		tx = this.get('title');
		var ex = tx.split('::')
		tooltip.set('html','<b>'+ex[0]+'</b><br>'+ex[1]);
		this.set('title',''); 
		tooltip.fade(0.9);
	},
			
	'mouseleave': function(){
		this.set('title',tx);
		tooltip.fade(0);
	}
});		
//TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS TIPS 
//POLL POLL POLL POLL POLL POLL POLL POLL POLL POLL POLL 
if($('VboxR') != null){
		new Request.HTML({
		url:path+'miss_earth_dir/vote.php',
		method: 'get',
		update: 'VboxR'
		}).send(); 
};
//POLL POLL POLL POLL POLL POLL POLL POLL POLL POLL POLL
}); 


//NaMoo
var NaMooBox = new Class({

	initialize: function(){

		this.anchors = $$('a[rel=NaMoo]');
			
		this.anchors.each(function(a){
			a.store("caption", a.get("title") || a.getElement("img").get("alt"));
			a.addEvent("click", this.open.bind(this,a));
			
			//a.setStyles({'color':'red'});
			//alert(a);
		},this);
		
		this.body = $(document.body);
		
		this.overlay = new Element("div", {
			styles: {
				background:'#000',
				position: "absolute",
				index:100,
				opacity:0,
				left:0,
				top:0
			},
			events: {
				click: this.close.bind(this)
			}
		}).inject(this.body);
		
			
		this.quickBox = new Element("div", {
			styles: {			
				width:10,
				height:10,
				background:'#fff',
				position: "absolute",
				index:101,
				opacity:0,
				display:'block',
				marginTop:-5,
				marginLeft:-5,
				top: window.getScrollTop() + (window.getHeight()/2), 
				left: window.getScrollLeft() + (window.getWidth()/2)
			}
		}).inject(this.body);
		
		

		this.top = new Element("div", {
		styles: {			
				'position': 'absolute',
				'index': 104,
				'width': '100%',
				'height': 30,
				'display': 'block',
				'color':'#000',
				'font-size':'12px',
				'font-family':'Arial',
				'top':-30,
				'left':0,
				'background':'#fff',
				'cursor':'pointer'
			},
			events: {
				click:this.close.bind(this)
			}
		}).inject(this.quickBox);
		
		
		this.prevLink = new Element("div", {
			styles: {			
				'position': 'absolute',
				'index': 104,
				'width': '50%',
				'height': '100%',
				'display': 'block',
				'color':'#fff',
				'font-size':'40px',
				'text-decoration':'none',
				'font-weight':700,
				'cursor':'pointer',
				'font-family':'Arial'
			}
			
		}).inject(this.quickBox);
		
		this.prevLink.addEvent("click", this.changeImage.bind(this, -1));
		
		this.nextLink = this.prevLink.clone().setProperty("id", "qbNext").inject(this.quickBox);
		this.nextLink.setStyles({'left':'50%'});
		
		this.nextLink.addEvent("click", this.changeImage.bind(this, 1));
			
		this.stage = new Element("div", {id: "qbStage"}).inject(this.quickBox);
		
		var nextEffect = this.nextEffect.bind(this);
		var nextEffect2 = this.nextEffect2.bind(this);
		
		this.fx = {
			overlay: new Fx.Tween(this.overlay, {
				property: "opacity"
			}),
			quickBox: new Fx.Tween(this.quickBox, {
				property: "opacity"
			}),
			resize: new Fx.Morph(this.quickBox, {
				duration: 600,
				transition: Fx.Transitions.Circ.easeOut,
				onComplete: nextEffect2
			}),
			show: new Fx.Tween(this.stage, {
				property: "opacity"
			})
		};
	},
	
	open: function(link){
		this.active = true;
				
		this.overlay.setStyles({
			width:window.getScrollSize().x,
			height:window.getScrollSize().y,
			display: "block"
		}); 
		
		this.quickBox.setStyles({
			display: "block"
		});

		this.fx.overlay.start(0.8);
		this.fx.quickBox.start(1);
		this.startLoad(link);
		return false;
	},
	
	startLoad: function(link,preload){

		if(!link) return;
		
		var image = new Asset.image(link.get("href"), {
			onload: function(){
				if(!preload && this.currentLink == link) this.nextEffect();
			}.bind(this)
		});
		if(!preload){
			this.stage.empty();
			this.top.set("html", '<div style="position:absolute;top:8px;left:10px;">Loading...</div>');
			this.currentLink = link;
			this.currentImage = image;
			this.currentIndex = this.anchors.indexOf(link);
			this.currentCaption = link.retrieve("caption");
		}
	},
		
	changeImage: function(step,event){
		event.preventDefault();
		var link = this.anchors[this.currentIndex+step];
		if(!link) return false;
		this.startLoad(link);
	},
	
	nextEffect: function(){
	
				var w = this.currentImage.width;
				var h = this.currentImage.height;
				
				var ratio = w/h;
	
				var wi = window.getSize().x;
				var hi = window.getSize().y;
				var ratio_w = wi/hi;
						
				if(w > wi && ratio > ratio_w){
				w = wi-20;
				h = w/ratio;
				this.currentImage.width = w;
				this.currentImage.height = h;				
				} 
				
				if(h > hi && ratio < ratio_w){
				h = hi-80;
				w = h*ratio;
				this.currentImage.width = w;
				this.currentImage.height = h;
				}
				

				this.fx.resize.start({
					width: w,
					height: h,
					marginTop:-(h/2),
					marginLeft:-(w/2),
					opacity:1,
					top: window.getScrollTop() + (window.getHeight()/2), 
					left: window.getScrollLeft() + (window.getWidth()/2)
				});
				
				
				var total = this.anchors.length;
				var num = this.currentIndex + 1;
				if(total != 1){
				this.prevLink.set('html','<div style="position:absolute;top:45%;left:0;padding:10px 2px;display:block;color:#870101;background:#fff">&lt;</div>');
				this.nextLink.set('html','<div style="position:absolute;top:45%;right:0;padding:10px 2px;display:block;color:#870101;background:#fff">&gt;</div>');
				}
				var bottom_text1 = "<div style='position:absolute;top:8px;left:10px;'><b>"+this.currentCaption+"</b> &nbsp; &nbsp; ("+num+"/"+total+")</div>";
				var bottom_text2 = "<div style='color:#870101;position:absolute;background:#fff;display:block;font-weight:700;font-size:20px;padding:5px 5px 2px 5px;top:0px;right:0px;'><b>X</b></div>";
				this.top.set("html", bottom_text1+bottom_text2);
				
				//alert(w+'-'+h);
				
	},
	
	nextEffect2: function(){
				//alert('ne2');
				this.stage.empty();
				this.currentImage.inject(this.stage);
				this.fx.show.start(1);
	},

	close: function(){
		this.quickBox.setStyle("display", "none");
		this.overlay.fade("out");
		this.active = false;
	}
});	

/*form------------*/
var NaMooForm = new Class({
	initialize: function(){

		this.submitB = $$('.sub');
		this.form = $('form1');
		this.formInput = this.form.getElements('input');
		this.ch = true;
		this.submitB.addEvent("click", this.checkID.bind(this));
		
	},
	
	checkID: function(){
		this.active = true;
		
		this.ch = 0;
		this.formInput.each(function(el,e) {
			
			if(el.get('id') != null){
				if(el.get('id') == 'xmail'){ /* email check */
					var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/;
					(el.get('value').toUpperCase().match(regEmail))? this.style1(el):this.style2(el);
				} 
				else{
					(el.get('value') == '')? this.style2(el):this.style1(el);
				}
			}

		}, this);

		if(this.ch == 0){this.formSend(this);}

		return false;
	},
	
	formSend: function(){
		this.active = true;
		//console.log('out = ' + this.ch);
				
			this.form.set('send', {
				url: path+'js/form1.php',
				method: 'post',
				onRequest: function() {$('form1_on').empty().addClass('form1_loading');}.bind(this),
				onSuccess: function() {$('form1_on').removeClass('form1_loading');$('form1_off').setStyle('display', 'block');}.bind(this)
			});
				
			this.form.send();
						
			return false;
	},
	
	style1: function(b){b.setStyles({'border':'1px solid #ccc','background':'#fff'});},
	style2: function(b){b.setStyles({'border':'2px solid red','background':'#FFF79C'});this.ch = 1;}
	
});

//

ImageFade = new Class({

	showDuration: 3000,

	initialize: function(el) {
		this.x = 0;
		this.li = $(el).getElements('li');
		this.li.each(function(li,i){
			li.setStyle('opacity',[i<1?1:0]);
		},this);
		
		this.li.addEvents({
			mouseenter: function() {this.stop(); }.bind(this),
			mouseleave: function() {this.start(); }.bind(this)
		});
		
		this.start();	

	},  
	show: function() {
		this.effect(this.x,0);
		this.x=this.x<this.li.length-1?this.x+1:0;
		this.effect(this.x,1);
	},

	start:function() {this.interval = this.show.bind(this,'').periodical(this.showDuration);},
	
	stop: function() {clearInterval(this.interval);},

	effect: function(x,t) {
		this.li[x].set('morph', {duration: 1000, transition: 'linear'});
		this.li[x].morph({opacity: t});
	}
});

