var sc,dd,np,el;
document.observe("dom:loaded", function() {
	
	sc = new NECshowcase();
	dd = new scenarioDropper();
	fs = new featureSlider();
	el = new imageEnlarge();
	dd = new NECdropper().init();
	
	var i;
	for (i in document.images) {
	    if (document.images[i].src) {
	        var imgSrc = document.images[i].src;
	        if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
	            document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
	        }
	    }
	}
	
	testappear(0);	
});

function testappear(count){
	
	var benefits = $$("#businessBenefits .circle");
	if(count == benefits.length) return;
		
//	new Effect.BlindDown(benefits[count]);
	new Effect.Appear(benefits[count],{
		duration:2
	});
	
	setTimeout(function(){testappear(count+1)},350);
}

function embedFlash(embedId){
	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF("flash/beating-logo.html", embedId, "200", "200", "9.0.0", false, flashvars, params, attributes);
}
function showFlashMovie(id,width,height){
	var flashOverlay 	= new overlay();
	
	flashOverlay.showInner();
	
	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF("flash/"+id, "flashContent", width, height, "9.0.0", false, flashvars, params, attributes);
}
var NECdropper = Class.create({
	initialize: function(){
		this.dropliciousShowingUpDuration 	= 0;
		this.dropliciousHidingDuration 		= 0;
		this.dropliciousHideDelay 			= 0;
	},
	init: function(){
		$$('a.drops').each(function(name){
			name.observe('mousemove', this.linkMouseOver.bindAsEventListener(this));
			name.observe('mouseout',  this.linkMouseOut.bindAsEventListener(this));
		}.bind(this));

		$$('ul.subs').each(function(name){
			name.observe('mousemove', this.submenuMouseOver.bindAsEventListener(this));
			name.observe('mouseout',  this.submenuMouseOut.bindAsEventListener(this));
		}.bind(this));
	},
	linkMouseOver: function(id){
		var dropElement = id.element().next();
		if(dropElement){
			if (!dropElement.hasClassName('hidding')){
				dropElement.removeClassName('waitingtohide');
			}
			if (!dropElement.hasClassName('active')){
				dropElement.addClassName('active');
				this.dropliciousShowingUpEffect(dropElement);
			}
		}
	},
	linkMouseOut: function(id){
		var dropElement = id.element().next();		
		if (dropElement && dropElement.hasClassName('active')){
			this.setDelayedHide(dropElement);
		}
	},
	dropliciousShowingUpEffect: function(element){
		if(!element.visible()){
			new Effect.BlindDown(element, {
				duration: this.dropliciousShowingUpDuration,
				queue: {
					position: 'end',
					scope: element.identify(),
					limit:2
				}
			});
		}
	},
	dropliciousHidingEffect: function(element){
		new Effect.BlindUp(element, {
			duration: this.dropliciousHidingDuration,
			queue: {
				position: 'end',
				scope: element.identify(),
				limit: 2
			}
		 });
	},
	setDelayedHide: function(element){
		element.addClassName('waitingtohide')
		if(!element.hasClassName('hidding')){
			if (!element.hasClassName('hiddingtimerset')){	
				element.addClassName('hiddingtimerset');
				setTimeout(
					function(){ 
						this.delayedHide(element);
					}.bind(this), this.dropliciousHideDelay * 1000);
			}
		}
	},
	delayedHide: function(dropElement){
		dropElement.removeClassName('hiddingtimerset');
		if (dropElement.hasClassName('waitingtohide')){
			this.dropliciousHidingEffect(dropElement);
			dropElement.addClassName('hidding');
			setTimeout(
				function(){
					dropElement.removeClassName('waitingtohide');
					dropElement.removeClassName('hidding');
					dropElement.removeClassName('active');
				}, this.dropliciousHidingDuration * 1000);
		}
	},
	submenuMouseOut: function(event){
		var dropElement = event.findElement("ul");	
		if (dropElement && dropElement.hasClassName('active')){
			this.setDelayedHide(dropElement);

		}
	},
	submenuMouseOver: function(event){
		var dropElement = event.findElement("ul");	
		if (dropElement && !dropElement.hasClassName('hidding')){
			dropElement.removeClassName('waitingtohide');
		}
	}
});
var imageEnlarge = Class.create({
	initialize: function(){
		$$("a.enlarge").invoke("observe","click",this.init.bind(this));
	},
	init: function(event){
		// get clicked element
		Event.stop(event);
		var el 			= Event.element(event);
		
		// init overlay
		this.overlay 	= new overlay();
		
		//preload image
		var imgPreloader = new Image();
        imgPreloader.onload = (function(){
			this.activeImg = [imgPreloader.width,imgPreloader.height];
			//update overlay
			//var h2  = new Element("h2").update(el.innerHTML);
			
			new Ajax.Request("pages/get_image_gallery.html",{
				parameters: "h="+el.innerHTML,
				onComplete: function(t){
					var r 		= t.responseText;
					var imgwrap = new Element("div",{"class" : "img",id:"imgWrapper"}).update(imgPreloader)
					var div 	= new Element("div",{"class":"wrapper"}).update(imgwrap).insert({bottom:r});

					this.initOverlay(div);
					
					$$("#imageGallery img").invoke("observe","click",this.preloadImage.bind(this));

					
				}.bind(this)
			});
		
        }).bind(this);
        imgPreloader.src = el.href;
        
	},
	initOverlay: function(insert){
		this.overlay.showInner();
		this.overlay.innerBox.setStyle({height:this.activeImg[1]+"px"})
		this.overlay.insertContent(insert);
	},
	preloadImage: function(event){
		var el = Event.element(event);
		
		$$("#imageGallery img").invoke("removeClassName","active");
		el.addClassName("active");
		
		var imgPreloader = new Image();
        imgPreloader.onload = (function(){
			this.activeImg = [imgPreloader.width,imgPreloader.height];
			
			this.crossFade(imgPreloader.hide());
			
			
		}).bind(this);
		imgPreloader.src = el.alt;
			
	},
	crossFade: function(newImg){
		new Effect.Fade($("imgWrapper").down(),{
			duration:0.5,
			afterFinish: function(){
				$("imgWrapper").update(newImg);
				new Effect.Appear($("imgWrapper").down(),{duration:0.5});
				
			}
		});
		
	}
	
});

var featureSlider = Class.create({
	initialize: function(){
		if(!$("featureSlider")) return;
		this.slider = $("featureSlider");
		this.groups = this.slider.down();
		
		this.amount	= $$("#featureSlider .group").length;
		this.half	= 1;
		this.timer  = null;
		this.delay	= 3500;
		this.counter = 0;
		
		this.duplicateFirst();
		this.setTimer();
	},
	duplicateFirst: function(){
		var div = new Element("div",{"class":"group value"});
		div.update(this.groups.down().innerHTML);
		this.groups.insert({bottom:div});
	},
	setTimer: function(){
		this.timer = setTimeout(this.startMove.bind(this),this.delay);
		if(this.counter == this.amount){
			this.groups.setStyle({
				top: 0
			});
			this.counter = 0;
		}

		this.counter++;
	},
	startMove: function(){
		new Effect.Move(this.groups,{y:-150,x:0,mode: 'relative',duration:this.half,
			afterFinish: function(){
				this.setTimer();
			}.bind(this)
		});
			
	}
});

var scenarioDropper = Class.create({
	initialize: function(){
		if(!$("scenarioDropper")) return;
		this.dropper 	= $("scenarioDropper");
		this.active		= this.dropper.down();
		this.list 		= this.dropper.down().next();
		this.observeHover();
	},
	observeHover: function(){
		this.dropper.observe("mouseover",this.openDropper.bind(this));
	},
	openDropper: function(){
		this.list.show();
		this.dropper.observe("mouseout",this.hideDropper.bind(this));
	},
	hideDropper: function(){
		this.list.hide();
	}
});

var NECshowcase = Class.create({
	initialize: function(){
		if(!$("showcase")) return;
		this.showcase 	= $("showcase");
		this.slider		= this.showcase.down().next();
		this.cases 		= this.showcase.down().next().down();
		this.caseCount	= $$(".case").length;
		this.selector	= $("caseSelector");
		
		this.counter	= 0;
		this.timer 		= null
		this.delay		= 7000;
		this.full	 	= 1;
		this.half		= 0.5;
		this.quart		= 0.25;
		this.currentCase= this.cases.down();
		this.nextCase 	= this.cases.down().next();
		
		this.cases.setStyle({
			left: "750px"
		});
		
		this.init();
		$$(".selector a").invoke("observe","click",this.showCase.bind(this));
	},
	init: function(){
		this.texts 	= this.currentCase.down();
		this.bg 	= this.currentCase.down().next();
		
		new Effect.Appear(this.bg,{
			duration:this.full,
			afterFinish: function(){
				this.showText();
			}.bind(this)
		});
	},
	showText: function(){
		new Effect.Appear(this.texts,{duration:this.half});
		new Effect.Move(this.texts,{y:50,x:295,mode: 'absolute',duration:this.half,
			afterFinish: function(){
				this.setTimer();
			}.bind(this)
		});
	},
	setTimer: function(){
		this.timer = setTimeout(this.moveToNextCase.bind(this),this.delay);
	},
	updateSelector: function(){
		$$('.selector a').invoke('removeClassName','active');

		if(this.counter == (this.caseCount-1)) this.selector.down(1).addClassName('active');
		else this.selector.down().next(this.counter).down().addClassName('active');
		
	},
	moveToNextCase: function(){
		clearInterval(this.timer);
		this.timer = null;
		
		this.updateSelector();
		
		
		new Effect.Fade(this.bg,{duration:this.half});
		new Effect.Fade(this.texts,{duration:this.half});
		
		new Effect.Move(this.slider.down(),{y:0,x:-1500,mode: 'relative',duration:this.full,
			afterFinish: function(){
				
				this.currentCase.down().writeAttribute("style",false).hide();
				
				// update current case
				this.currentCase = this.cases.down().next(this.counter);
				
				this.counter++;
				// reset or show next
				if(this.counter == this.caseCount) this.resetAll();
				else this.init();
				
			}.bind(this)
		});
		
	},
	resetAll: function(){
		this.cases.setStyle({
			left: "750px"
		});
		
		this.currentCase 	= this.cases.down();
		this.counter		= 0;
		
		this.init();
	},
	fadeCurrent: function(){
		
		new Effect.Fade(this.bg,{duration:this.half});
		new Effect.Fade(this.texts,{duration:this.half});
				
	},
	showCase: function(event){
		Event.stop(event);
		this.element	= Event.element(event);
		this.caseid		= parseInt(this.element.id.replace("tocase","")); 		
		
		// return if clicked = current
		if(this.caseid == this.counter+1)return; 
		
		// clear interval
		clearInterval(this.timer);
		this.timer = null;
		
		// fade current 
		this.fadeCurrent();
		
		// update selector
		$$('.selector a').invoke('removeClassName','active');
		this.element.addClassName("active");
		
		// set active in viewport
		this.cases.setStyle({
			left: (750- (1500*(this.caseid-1)))+"px"
		});
		
		// current case
		this.currentCase.down().writeAttribute("style",false).hide();
		
		
		if(this.caseid-2 < 0) this.currentCase = this.cases.down();
		else this.currentCase = this.cases.down().next(this.caseid-2);
		
		this.texts 	= this.currentCase.down();
		this.bg 	= this.currentCase.down().next();
		
		new Effect.Appear(this.bg,{
			duration:this.full,
			afterFinish: function(){
				
				new Effect.Appear(this.texts,{duration:this.half});
				new Effect.Move(this.texts,{y:50,x:295,mode: 'absolute',duration:this.half});
				this.counter = this.caseid-1;
				
			}.bind(this)
		});
		
		
	}
	});
	
	

function validateForm(formid){
	
	var inputs 		= $(formid).getInputs("text");
	var cbs 		= $(formid).getInputs("radio");
	var form_values = $(formid).serialize(true);
	var reg 		= /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var namefield 	= /mail/
	var missed 		= false;
	var fieldCheck	= "";
	var mail		= "";

	inputs.each(function(i){
		if(i.hasClassName("required") && namefield.test(i.name) && reg.test($F(i)) == false){
			i.addClassName("missed");
			missed 		= true;
			mail 		= "- The email email address is not valid<br>";
		}
		else if(i.hasClassName("required") && $F(i) == ""){
			i.addClassName("missed");
			missed 		= true;
			fieldCheck 	= "- Not all mandatory fields have been filled<br>";
		}else i.removeClassName("missed");
		
	});
	
	var groups = $$("#"+formid+" .block");
	groups.each(function(g){
		var count = 0;
		var gid = g.id;
		if(!g.hasClassName("required")) return;
		
		var l 	= $$("#"+gid+" input");
		l.each(function(item){
			if(item.checked == true) count++;
		});		
		

		if(count == 0){
			missed = true;
			g.addClassName("missed");
		}else g.removeClassName("missed");
	});

	if(missed == true){
	//	var tt = new toolTip(fieldCheck+mail);
	//	var pos = $("submit"+formid.replace("form","")).cumulativeOffset();
	//	tt.setPosition(pos);
		
		$('msg').update(fieldCheck+mail).addClassName("missed");
		return false;
	}	
	
	
}

var overlay = Class.create({
	initialize: 	function(element) {
		$$("body").first().insert(
	      new Element('div',{ id: 'overlay' })
	    );
		$$("body").first().insert(
	      new Element('div',{ id: 'overlayBox' })
	    );
		this.element  	= element;
		this.delay		= 2000;
		this.overlay 	= $('overlay'); 
		this.overlayBox	= $('overlayBox'); 
		this.overlayBox.hide();
		this.overlay.hide();
		
		this.updateBox();
		this.showOverlay();
	},
	updateBox: function(){
		
		var inner = "<div class='top'></div><div class='middle'><div id='flashContent'></div></div><div class='bottom'></div><a href='#' class='close padded'>close</a>";
		this.overlayBox.update(inner);	
		
		this.innerBox = $("flashContent");
		$$('#overlayBox .close').invoke('observe','click',function(event){
			Event.stop(event);
			this.closeOverlay();
		}.bind(this));	
	},
	insertContent: function(content){
		$("flashContent").insert({top:content});
		new Effect.Appear(content,{delay:1.2,duration:0.5});
		
		var yScale = (560 / 500) * 100;
		new Effect.Scale($("flashContent"), yScale, {scaleX: false, duration: 0.5,delay:1.25,scaleContent: false}); 
		
	},
	showOverlay: 	function(){
		var arrayPageSize = this.getPageSize();
		this.pageHeight = arrayPageSize[1];
		this.overlay.setStyle({ height: this.pageHeight + 'px' });
		
		new Effect.Appear(this.overlay,{duration:0.5, from:0.0, to:0.5});
		this.overlay.observe('click',this.closeOverlay.bind(this));
	},
	showInner: 		function(){
		var overlayBoxHeight = this.overlayBox.getHeight();
		var marginTop = this.pageHeight - (overlayBoxHeight+400);
		
		var arrayPageScroll = document.viewport.getScrollOffsets();
        var posTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
		
		this.overlayBox.setStyle({ top: posTop+'px' });	
		
		new Effect.Appear(this.overlayBox,{delay:0.6,duration:0.5});
		new Effect.Appear(this.overlayBox,{delay:0.6,duration:0.5});
	},
	updateOverlay: 	function(update){
		this.overlayBox.update(update);
		this.showInner();
	},
	closeOverlay: 	function(){
		new Effect.Fade(this.overlayBox,{duration:0.2, from:1, to:0.0});
		new Effect.Fade(this.overlay,{delay:0.2, duration:0.5, from:0.5, to:0.0});
		
		timer = setTimeout( function(){
			$('overlayBox').remove();
			$('overlay').remove();
		}, 600);
	},
	getPageSize: 	function(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
		
	}
});








