Ajax.Responders.register({
	onCreate: function() {
		$('loader').update('Loading ...');
		$('loader').removeClassName('hidden');
		if($('loader') && Ajax.activeRequestCount > 0) Effect.Appear('loader',{duration: 0.25, queue: 'end'});
	},
	onComplete: function() {
		if($('loader') && Ajax.activeRequestCount == 0) Effect.Fade('loader',{duration: 0.25, queue: 'end'});
	}
});

var timer = null;
Event.observe(window, 'load', function() {

	if($('advancedSearch') != null){
		$('advancedSearch').observe('click',function(event){
			Event.stop(event);
			var element = Event.element(event);
			new Effect.toggle(element.next(),'blind',{duration: 0.5});
			//element.addClassName('opened');
			if(element.hasClassName('opened')){
				element.removeClassName('opened');
				element.update("Advanced search");
			}else{
				element.addClassName('opened');
				element.update("Advanced search");
			}
		});
	}
		
	if($('showcase') != null) var sc = new necShowcase();
	
	$$('a.enlarge').invoke('observe','click',function(event){
		Event.stop(event);
		var element = Event.element(event);
		if(element.hasClassName('features')) var enlarge = new enlargeItem(element);
		else if(element.hasClassName('tags')) var enlarge = new showTags(element);
		else if(element.hasClassName('endpoint')) var enlarge = new enlargeImage(element);
		
		
	});
	
	$$('.nav a').invoke('observe','mouseover',function(event){
		var element = Event.element(event);
		var dd 		= new dropDown(element);
	});
	
	$$(".productContainer img").invoke('observe','mouseover',function(event){
		var element = Event.element(event);
		var wrapper = element.up(2);
		var glance 	= new showGlance(wrapper,'browser');
	});


	$$('.gallery img').invoke('observe','click',function(event){
		var element = Event.element(event);
		var swapper = new impressionMover(element);
	});
	
	$$('.impression img').each(
		function(image){
			new Effect.Appear(image);
		}
	);
	
	$$('.embedFlash').invoke('observe','click',function(event){
		var el = Event.element(event);
		var ef = new embedFlash(el); 
	});
	
	
	if($('news') != null) var nws = new necNewsBrowser();
	if($('endpointGallery') != null) var epg = new endpointBrowser();

	if($('chooseLanguage') != null) var lang = new showNECLanguages();
});

var showNECLanguages = Class.create({
	initialize: function() {
		this.element		= $('langSelector');
		this.duration		= 0.5;
		this.timeOut		= 500;
		this.timer			= null;
		this.observeMouse();			
	},
	observeMouse : function(){
		$('chooseLanguage').observe("mouseover",this.showLangList.bind(this));
		$('chooseLanguage').observe("mouseout",this.createTimeout.bind(this));
	},
	createTimeout: function(){
			
		this.timer = setTimeout(function(){
			this.timer = null;
			clearInterval(this.timer);
			this.observeMouseOut();
			$('chooseLanguage').stopObserving("mouseout");
		}.bind(this),this.timeOut);	
		
	},
	showLangList: function(){
		$('chooseLanguage').stopObserving("mouseover");
		
		new Effect.SlideDown(this.element,{duration:this.duration,
			afterFinish: function(){
			}.bind(this)	
		});
	},
	observeMouseOut: function(){
			
		//set timeout for hovering to dropdown div
		this.timer 	= setTimeout(function(){

			document.observe('mouseover', function(event){
				var element = Event.element(event);	
				if (element.descendantOf(this.element) == false){
					this.hideLangList();			
					document.stopObserving('mouseover');
					this.observeMouse();
				}
			}.bind(this));

		}.bind(this),200);
		
	},
	hideLangList: function(){
		new Effect.SlideUp(this.element,{duration:this.duration});
	}	
});

var endpointBrowser = Class.create({
	initialize: function() {
		
		//this.direction 		= dir; 
		this.effectTime		= 0.5;
		this.wrapper		= $('endpointGallery');
		this.mover			= this.wrapper.down().next(1).down();
		this.itemCount		= $$("#endpointGallery img").length;
		this.maxClick		= Math.floor(this.itemCount/6);
		this.currentCount	= 0;
		this.observeClicks();
						
	},
	observeClicks: function(){
		
		$$('#endpointGallery .mover').invoke('observe','click', function(event){
			var element = Event.element(event);
			if(element.hasClassName('toLeft')) this.direction = "left";
			else this.direction = "right";	
			this.moveBrowser()
		}.bind(this));
		
	},
	moveBrowser: function(){
		
		if(this.direction == "left" && this.mover.hasClassName('start')) return;
		this.movePx = (this.direction == "left") ? 600 : -600;
		if(this.currentCount == 0 && this.direction == "left") return;
		if(this.currentCount == this.maxClick && this.direction == "right") return;
		
		new Effect.Move (this.mover,{ y: 0, x: this.movePx, duration:this.effectTime, mode: 'relative',
			afterFinish: function(){
				
				this.mover.removeClassName('start');
				if(this.direction == "left") this.currentCount--;
				else this.currentCount++;
				
			}.bind(this)
		});	
		
	}
});

// home showcase
var necNewsBrowser = Class.create({
	initialize: function() {
		this.newsContainer 	= $('news'); 
		this.timeOut		= 10000;
		this.effectTime		= 0.5;
		this.newsCounter	= 0;
		this.newsTotal		= $$("#newsSelector span").length;
		this.newsTimer 		= null;
		this.paused			= false;
		this.currentNews	= this.newsContainer.down();
		this.nextNews		= this.newsContainer.down().next();
		this.observeClicks();
		this.createInterval();
					
	},
	fadeCurrentNews: function(loadClicked){
		new Effect.Fade(this.currentNews,{
			duration:this.effectTime,
			afterFinish:function(){
				this.showNext(loadClicked);
			}.bind(this)
		});
	},
	showNext: function(loadClicked){
		if(loadClicked !=null) this.nextNews = $("nb"+loadClicked);
		if(loadClicked == null) this.updateSelector();
		
		new Effect.Appear(this.nextNews,{
			duration:this.effectTime,
			afterFinish:function(){
				if(this.paused == false) this.createInterval();
				this.updateItems()
			}.bind(this)
		});
	},
	updateItems: function(){
		this.currentNews	= this.nextNews;
		this.nextNews		= this.newsContainer.down().next(this.newsCounter);
		// reset to first
		if((this.newsCounter+1) >= this.newsTotal) this.nextNews = this.newsContainer.down();
	},
	createInterval: function(){
		this.newsTimer = setTimeout(function(){
			this.fadeCurrentNews();
			this.newsTimer = null;
			clearInterval(this.timer);
		}.bind(this),this.timeOut);
	},
	updateSelector: function(){
		
		$$("#newsSelector span").invoke('removeClassName','active');
		if(this.paused == true){
			if(this.newsCounter == 0) $("newsSelector").down().addClassName("active");
			else $("newsSelector").down().next(this.newsCounter).addClassName("active");
		}
		else if( (this.newsCounter+1) >= this.newsTotal ){
			this.newsCounter = 0;
			$("newsSelector").down().addClassName("active");
		}else{
			$("newsSelector").down().next(this.newsCounter).addClassName("active");
			this.newsCounter++;
		}
		
	},
	observeClicks: function(){
	
		$$("#newsSelector span").invoke('observe','click',function(event){
			var element = Event.element(event);
					
			//clear timeout
			clearTimeout(this.newsTimer);
			this.newsTimer 	= null;
			var loadClicked = element.id.replace("sel","");
			
			this.fadeCurrentNews(loadClicked);
			this.paused 	= true;
			
			$$(".selector span").invoke('removeClassName','active');
			element.addClassName("active");
		 
		}.bind(this));
	}
});


var embedFlash = Class.create({
	initialize: 	function(element) {
		this.element	= element;
		this.overlay 	= new overlay();
		//this.div		= new Element("div",{id: "flashContent"});	
		this.flashLink	= "flash/"+this.element.id;
		
		var inner 	= "<a href='#' class='close padded'>close</a><div id='flashContent'></div>";
		this.overlay.updateOverlay(inner);
		
		$$('#overlayBox .close').invoke('observe','click',function(event){
			Event.stop(event);
			this.overlay.closeOverlay();
		}.bind(this));
		
		$('overlayBox').addClassName("large");
		this.flashIt();
	},
	flashIt: function(){
		var flashvars = {};
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		var swf = swfobject.embedSWF(this.flashLink, "flashContent", "800", "500", "8.0.0", false, flashvars, params, attributes);
	}
	
});

var necShowcase = Class.create({
	initialize: 	function() {

		this.timer			= null;
		this.wrapper		= $('showcase');
		this.cases			= $('cases');
		this.currentCase 	= this.cases.down();
		this.nextCase 		= this.cases.down().next();
		this.itemCount		= $$('#cases .case').length-1;
		this.currentCount	= 0;
		this.selector		= this.cases.next();
		this.clickedItem	= null;
		this.speed			= 0.5;		
		
		this.timer = setTimeout(function(){
			this.fadeCurrent();
		}.bind(this),7000);
		
		this.observeClicks();
	},
	fadeCurrent: 	function(){
		//new debug("fade current case");		
		new Effect.Fade(this.currentCase,{
			duration:this.speed,
			afterFinish: function(){
				this.showNext();
				this.updateSelector();		
			}.bind(this)
		});
	},
	showNext: 		function(){
		//new debug("show next");		
		new Effect.Appear(this.nextCase,{
			duration:this.speed,
			afterFinish: function(){
				this.currentCase.removeClassName("active"); 
				this.nextCase.addClassName("active");
				
				this.currentCount++;
				this.createInterval();
				this.resetVars();
			}.bind(this)
		});
	},
	createInterval: function(){
		this.timer 	= setTimeout(function(){
			clearInterval(this.timer);
			this.timer = null;
			this.fadeCurrent();
		}.bind(this),7000);
	},
	resetVars: 		function(){
		if(this.currentCount == this.itemCount){
			this.currentCase 	= this.nextCase;
			this.nextCase 		= this.cases.down();
			this.currentCount	= -1;
		}else{		
			this.currentCase 	= this.nextCase;
			this.nextCase		= this.cases.down().next(this.currentCount);
		}
	},
	updateSelector: function(){
		$$('#showcase .selector span').invoke('removeClassName','active');
		if( this.currentCount == -1){
			this.selector.down().addClassName('active');
		}else{
			this.selector.down().next(this.currentCount).addClassName('active');
		}
	},
	observeClicks: 	function(){
		
		$$('#showcase .selector span').invoke('observe','click',function(event){
			//stop observer on selector
			$$('#showcase .selector span').invoke('stopObserving','click');
			
			var element = Event.element(event);
			var id		= parseInt(element.id.replace("case",""));
		
			// clear interval
			clearInterval(this.timer);
			this.timer = null;
			
			//new debug("interval cleared");		
			$$('.selector span').invoke('removeClassName','active');
			element.addClassName('active');
			
			// active case
			var activeItem 	= $$('#cases .active');
			if(id == 0) var clickedItem = this.cases.down();
			else var clickedItem = this.cases.down().next(id-1);
			
			// fade current item
			new Effect.Fade(activeItem[0] ,{
				duration:this.speed,
				afterFinish: function(){
					activeItem[0].removeClassName('active');
					clickedItem.addClassName('active');
					
					new Effect.Appear(clickedItem,{
						duration:this.speed,
						afterFinish: function(){
							//start observing selector clicks
							this.observeClicks();
						}.bind(this)
					});
					
				}.bind(this)
			});
			
		}.bind(this));
	
	}
});


function showPartners(lang,lbl){
	
	var country = lang.replace("country=","");
	new Ajax.Request("pages/global/get_partners.php",{
		parameters: "country="+country+"&lbl="+lbl,
		onComplete:function(transport){
			
			var resp = transport.responseText;
			$('country').update(resp);
			new Effect.ScrollTo('country');
			
		}.bind(this)
	});
		
}
var impressionMover = Class.create({
	initialize: 	function(element) {
		this.element		= element;
		this.wrapper		= this.element.up().previous();
		this.itemCount		= $$(".gallery img").length-1;
		this.position 		= parseInt(this.element.id.replace("pos-",""));
		this.updateThumbs();
	},
	updateThumbs: 	function(){
		$$(".gallery img").invoke("removeClassName","active");
		this.element.addClassName("active");
		this.moveImpression();
	},
	moveImpression: function(){
		
		var xPos = -1 * 665 * this.position;
		new Effect.Move (this.wrapper,{ y: 0, x:xPos , duration:0.7, mode: 'absolute'});
		
	}
});

var ncpShowcase = Class.create({
	initialize: 	function(element) {
		this.timer			= null;
		this.wrapper		= $('showcase');
		this.cases			= $('cases');
		this.currentCase 	= this.cases.down();
		this.nextCase 		= this.cases.down().next();
		this.itemCount		= $$('#cases .item').length-1;
		this.currentCount	= 0;
		this.selector		= this.cases.next();
		
		this.clickedItem	= null;		
		this.showFirst();
		this.observeClicks();
	},
	showFirst: 		function(){
		// new debug("show first");		
		
		new Effect.Appear(this.currentCase,{
			duration:1.0,
			afterFinish: function(){
				this.animateCase(0);
			}.bind(this)
		});
		
	},
	animateCase: 	function(count){
		
		//new debug("animate case headers");		
		
		if(count == 0){
			this.h2 = this.currentCase.down();
			this.h3	= this.currentCase.down().next();
		}else if(count == 1){
			this.h2 = this.nextCase.down();
			this.h3	= this.nextCase.down().next();
		}else{
			this.h2 = count.down();
			this.h3	= count.down().next();
		}
		
		new Effect.Move (this.h2,{
			y: 0,
			x: 630,
			afterFinish: function(){
				new Effect.Move (this.h3,{y: 0,x: 630});	
				
				if(count == 0){
					this.timer = setTimeout(function(){
						clearInterval(this.timer);
						this.timer = null;
						this.fadeCurrent();
					}.bind(this),6000);
				}
							
			}.bind(this)
		});
		
	},
	fadeCurrent: 	function(){
		//new debug("fade current case");		
		
		new Effect.Fade(this.currentCase,{
			duration:1.0,
			afterFinish: function(){
				
				this.currentCase.down().setStyle({
					'left' : '-630px'
				});
				this.currentCase.down().next().setStyle({
					'left' : '-630px'
				});
				
				this.showNext();
				this.updateSelector();
				this.currentCase.removeClassName('active');
				
			}.bind(this)
		});
		
	},
	showNext: 		function(){
		//new debug("show NEXT");		
		
		new Effect.Appear(this.nextCase,{
			duration:1.0,
			afterFinish: function(){
				
				this.animateCase(1);
				this.nextCase.addClassName('active');
				this.currentCount++;
				this.createInterval();
				
			}.bind(this)
		});
		
	},
	updateSelector: function(){

		$$('#showcase .selector span').invoke('removeClassName','active');
		if( this.currentCount == -1){
			this.selector.down().addClassName('active');
		}else{
			this.selector.down().next(this.currentCount).addClassName('active');
		}
		
	},
	createInterval: function(){		
		
		this.timer 	= setTimeout(function(){
			clearInterval(this.timer);
			this.timer = null;
			this.resetVars();
		}.bind(this),8000);
		
	},
	resetVars: 		function(){

		if(this.currentCount == this.itemCount){
			this.currentCase 	= this.nextCase;
			this.nextCase 		= this.cases.down();
			this.currentCount	= -1;
		}else{		
			this.currentCase 	= this.nextCase;
			this.nextCase		= this.cases.down().next(this.currentCount);
		}
		this.fadeCurrent();
		
	},
	observeClicks: 	function(){
		$$('#showcase .selector span').invoke('observe','click',function(event){
			var element = Event.element(event);
			var id		= parseInt(element.id.replace("case",""));
		
			// clear interval
			clearInterval(this.timer);
			this.timer = null;
			
			//new debug("interval cleared");		
			$$('.selector span').invoke('removeClassName','active');
			element.addClassName('active');
			
			
			var activeItem 	= $$('#cases .active')
			if(id == 0){
				var clickedItem = this.cases.down();
			}else{
				var clickedItem = this.cases.down().next(id-1);
			}
			
			clickedItem.down().setStyle({
				'left' : '-630px'
			});
			clickedItem.down().next().setStyle({
				'left' : '-630px'
			});
			
			new Effect.Fade(activeItem[0] ,{
				duration:1.0,
				afterFinish: function(){
					
					activeItem[0].removeClassName('active');
					new Effect.Appear(clickedItem,{
						duration:1.0,
						afterFinish: function(){
							clickedItem.addClassName('active');
							this.animateCase(clickedItem);
						}.bind(this)
					});
										
				}.bind(this)
			});
			
		}.bind(this));
	}
});

var showTags = Class.create({
	initialize: 	function(element) {
		this.element	= element;
		if($('tagBlock') == null) this.createWrapper();
		else{
			new Effect.Appear('tagBlock',{duration:0.5});
		}
	},
	createWrapper: 	function(){
		this.updateDiv = new Element('div',{id: 'tagBlock' }).hide(); //update('<div class="btop"></div> <div class="bbottom"></div>');			
		$$("body").first().insert(this.updateDiv);
		this.getTags();
	},
	getTags: 		function(){
		new Ajax.Request("pages/global/all_tags.php",{
			onComplete:function(transport){
				
				var resp = transport.responseText;
				var inner = "<div class='btop'></div><a href='#' class='close'>close</a>"+resp+"<div class='bbottom'></div>";
				this.updateDiv.update(inner);
			//	Cufon.replace('h4.cf');
				this.positionItem();

			}.bind(this)
		});
	},
	positionItem: 	function(){
		var th 			= this.updateDiv.getHeight();
		var clickPos 	= this.element.cumulativeOffset();
		this.updateDiv.setStyle({
			top: (clickPos[1]-th-35) + 'px',
			marginLeft: -225 + 'px'
		});
		
		this.showItem();
	},
	showItem: 		function(){
		//this.updateDiv.show();
		new Effect.Appear(this.updateDiv,{duration:0.5});
		this.observeClose();
	},
	observeClose: 	function(){
		$$('#tagBlock .close').invoke('observe','click',function(event){
			Event.stop(event);
			//$('tagBlock').hide();
			new Effect.DropOut('tagBlock');
		});
	}
});

var showGlance = Class.create({
	initialize: 	function(element,place){
		this.timer		= null;
		this.element	= element;
		this.place 		= place;
		this.pars	 	= this.element.id.replace("glance-","");
		this.startTimer();
	},
	startTimer: 	function(){
		this.timer 	= setTimeout(function(){
			this.createWrapper();
		}.bind(this),150);
	
		this.element.observe('mouseout',function(){
			this.stopTimer();
		}.bind(this));
	},
	stopTimer: 		function(){
		clearInterval(this.timer);
		this.timer = null;
	},
	createWrapper: 	function(){
		this.updateDiv = new Element('div',{id: 'glanceBlock' }).hide();	
		$$("body").first().insert(this.updateDiv);

		//position for products
		if(this.place == 'product'){
			var parentPos	= this.element.up().positionedOffset();
			var bodyPos 	= $('glance').cumulativeOffset();
			
			this.updateDiv.setStyle({
				top: (bodyPos[1]+parentPos[1]+80) + 'px',
				left: (bodyPos[0]+parentPos[0]-20) + 'px'
			});
			
		}
		//position for product browser
		else{
			//var parentPos	= this.element.up(2).cumulativeOffset();
			var pos			= this.element.cumulativeOffset();			
			this.updateDiv.setStyle({
				top: (pos[1]+100) + 'px',
				left: (pos[0]) + 'px'
			});
		}
		

		this.getGlanceText();
	},
	getGlanceText: 	function(){
		new Ajax.Request("pages/global/get_glance_text.php",{
			parameters: "pars="+this.pars,
			onComplete:function(transport){
				
				this.glance = transport.responseText;
				var inner = "<div class='btop'></div><div class='wrapper'>"+this.glance+"</div><div class='bbottom'></div>";
				this.updateDiv.update(inner);
				
				Cufon.replace('h4.cf');
				
				this.updateDiv.show();
				this.observeMouseOut();
				
			}.bind(this)
		});
	},
	observeMouseOut:function(){
		this.element.observe('mouseout',function(){
			if($('glanceBlock') != null) $('glanceBlock').remove();
		}.bind(this));
	}
});

var dropDown = Class.create({
	initialize: 	function(element) {
		this.timer		= null;
		this.element	= element;
		this.class_name = this.element.className.replace(" active","");
		if(this.class_name == "home" || this.class_name == "services") return;
		this.country	= this.element.up(1).className.replace("nav ","");
		this.startTimer();
	},
	startTimer: 	function(){
		this.timer 	= setTimeout(function(){
			this.createWrapper();
			this.hideWrappers();
		}.bind(this),150);
	
		this.element.observe('mouseout',function(){
			this.stopTimer();
		}.bind(this));
	},
	stopTimer: 		function(){
		clearInterval(this.timer);
		this.timer = null;
	},
	hideWrappers: 	function(){
		var links = ['solutions','products','newsroom','company','partners'];
		links.each(function(link){
			if(link != this.class_name){
				var id = 'dropdown_'+link;
				if($(id) != null) $(id).hide();
			}
		}.bind(this));
	},
	createWrapper: 	function(){
		this.dropid = 'dropdown_'+this.class_name; 
		if($(this.dropid) == null) {
			this.wrapper = new Element('div',{id: this.dropid ,'class': 'dropdown'}).hide();	
			//this.wrapper.setOpacity(0.9);
			$$("body").first().insert(this.wrapper);
			this.getCategories();
		}
		else{
			this.wrapper = $(this.dropid);
			this.showWrapper();	
		}
		
		var pos	= this.element.positionedOffset();
		this.wrapper.setStyle({
			left: (pos[0]-16) + 'px'
		});
	},
	getCategories: 	function(){
		new Ajax.Updater(this.wrapper,"pages/global/get_categories.php",{
			parameters: "page="+this.class_name+'&lang='+this.country,
			onComplete:function(transport){
				var resp = transport.responseText;
				if(resp != "none") this.showWrapper();
			}.bind(this)
		});
	},
	showWrapper: 	function(){
		if($(this.dropid).innerHTML != "none"){
			new Effect.SlideDown(this.dropid,{duration:0.5});
			//observe mouseout from link
			this.observeMouseOut();
		}
			
	},
	observeMouseOut:function(){
		
		// observe link mouseout
		this.element.observe('mouseout',function(){
			
			//set timeout for hovering to dropdown div
			this.timer 	= setTimeout(function(){
				$(this.dropid).hide();				
				clearInterval(this.timer);
				this.timer = null;
				//this.observeDropDown();
			}.bind(this),500);
		
			// observe mouse over dropdown down
			this.observeMouseEnterWrapper();
		}.bind(this));
		
	},
	observeMouseEnterWrapper: function(){
		// observe mouse enter on wrapper
		$(this.dropid).observe('mouseover',function(){
			
			clearInterval(this.timer);
			this.timer = null;
			//observe mouse leave
			this.observeMouseLeaveWrapper();	
					
		}.bind(this));
		
	},
	observeMouseLeaveWrapper: function(){
			
		document.observe('mouseover', function(event){
			var element = Event.element(event);	
			if (element.descendantOf(this.dropid) == false){
				//$('debug').update('no desc '+this.dropid);
				$(this.dropid).hide();
				document.stopObserving('mouseover');
			}
		}.bind(this));
	
	}
});

var enlargeItem = Class.create({
	initialize: 	function(element) {
		this.element	= element;
		this.eid		= this.element.id.replace("eid_","")
		this.cn			= this.element.className.split(" ");
		this.enlargeType= this.cn[1];
		if($('enlargedBlock') == null) this.createWrapper();
		else{
			new Effect.Appear('enlargedBlock',{duration:0.5});
		}
	},
	createWrapper: 	function(){
		this.updateDiv = new Element('div',{id: 'enlargedBlock' }).addClassName('features').hide(); //update('<div class="btop"></div> <div class="bbottom"></div>');			
		$$("body").first().insert(this.updateDiv);
		var clickPos = this.element.positionedOffset();
		this.updateDiv.setStyle({
			top: (clickPos[1] +153) + 'px'
		});
		this.getEnlargedData();
	},
	getEnlargedData:function(){
		new Ajax.Request("pages/global/enlarge_item.php",{
			parameters: "eid="+this.eid+'&type='+this.enlargeType,
			onComplete:function(transport){
				this.resp = transport.responseText;
				var inner = "<div class='btop'></div><a href='#' class='close'>close</a>"+this.resp+"<div class='bbottom'></div>";
				this.updateDiv.update(inner);
				this.showItem();
			}.bind(this)
		});
	},
	showItem: 		function(){
		//this.updateDiv.show();
		new Effect.Appear(this.updateDiv,{duration:0.5});
		this.observeClose();
	},
	observeClose: 	function(){
		$$('#enlargedBlock .close').invoke('observe','click',function(event){
			Event.stop(event);
			//$('tagBlock').hide();
			new Effect.DropOut('enlargedBlock');
		});
	}
});

var productBrowser = Class.create({
	initialize: 	function(element) {
		this.element			= element;
		this.productContainer	= this.element.up(1);
		this.productWrapper		= this.productContainer.down();
		this.nextBtn 			= this.productWrapper.next();
		this.productCount		= $$("."+this.productWrapper.className+" .item").length;
		this.maxCount			= Math.floor(this.productCount/6);
		this.duration			= 0.5;
		this.positionCounter 	= 0;
		this.direction 	= 'next';
		this.movePx		= -650;
		this.moveBrowser();
		this.stopObservers();
	},
	moveBrowser: 	function(){
		new Effect.Move (this.productWrapper,{ y: 0, x: this.movePx, duration:this.duration, mode: 'relative',
			afterFinish: function(){
				this.insertPrevButton();
				this.positionCounter++;
				this.checkPosition();
				this.observeButtons();
			}.bind(this)
		});	
	},
	insertPrevButton:function(){
		var previousButton 	= " <span class='btn'><input type='button' value='Show previous' class='observe productSlider prev' /></span>";
		this.productWrapper.next().insert({after: previousButton});
		this.prevBtn 		= this.nextBtn.next(); 
		//this.observeButtons();
	},
	observeButtons: function(){
		$$("."+ this.productContainer.className +" .btn").invoke('observe','click',function(event){
			
			this.stopObservers();
			
			var element = Event.element(event);
			if(element.hasClassName('next')){
				this.direction 	= 'next';
				this.movePx		= -650;
				this.positionCounter++;
			}else{
				this.direction 	= 'prev';
				this.movePx		= 650;
				this.positionCounter--;
			}
			new Effect.Move (this.productWrapper,{ y: 0, x: this.movePx, duration:this.duration, mode: 'relative',
				afterFinish: function(){
					this.checkPosition();
					this.observeButtons();
				}.bind(this)
			});
			
		}.bind(this));
	},
	checkPosition: 	function(){
		if(this.positionCounter == 0) this.prevBtn.hide();
		else this.prevBtn.show();		
		if(this.positionCounter == this.maxCount) this.nextBtn.hide();
		else this.nextBtn.show();
	},
	stopObservers: 	function(){
		$$("."+ this.productContainer.className +" .btn").invoke('stopObserving','click');
	}
});

var enlargeImage = Class.create({
	initialize: 	function(element) {
		this.element	= element;
		this.image_id	= element.id.replace("img-","");
		this.lightbox 	= new overlay();	
		this.getRelatedData();
	},
	getRelatedData: function(){
		$('overlayBox').addClassName("medium");
		new Ajax.Request("pages/global/enlarge_image.php",{
			parameters: "image_id="+this.image_id,
			onComplete:function(transport){
				var resp	= transport.responseText;
			//	var h1		= $$();
			//	var h2		= df;
				var inner 	= "<div class='btop'></div><a href='#' class='close'>close</a>"+resp+"<div class='bbottom'></div>";
				this.lightbox.updateOverlay(inner);
				this.observeClose();
			}.bind(this)
		});
	},
	observeClose: 	function(){
		$$('#overlayBox .close').invoke('observe','click',function(event){
			Event.stop(event);
			this.lightbox.closeOverlay();
		}.bind(this));
	}
});

var newsBrowser = Class.create({
	initialize: 	function() {
		this.newsContainer 	= $('news'); 
		this.timeOut		= 8000;
		this.effectTime		= 0.5;
		this.newsCounter	= 1;
		this.newsTotal		= ($$("#newsSelector span").length);
		this.newsTimer 		= null;
		this.paused			= false;
		this.observeClicks();
		this.createTimeOut();
		
	},
	getNextItem: 	function(loadClicked){

		if(this.newsCounter >= this.newsTotal) var load_position = 1; // reset the counter
		else load_position = this.newsCounter+1; 
		
		if(loadClicked != null){
			var load_position = loadClicked;
			this.newsCounter  = loadClicked;
		}
				
		new Ajax.Request('pages/news/get_news_item.php',{
			parameters: 'load_news='+load_position,
			onComplete: function (transport){
				this.nextNews = transport.responseText;
				this.fadeCurrentNews();				
			}.bind(this)
		});
	},
	fadeCurrentNews:function(){
		
		new Effect.Fade(this.newsContainer,{
			duration:this.effectTime,
			afterFinish:function(){
				this.newsContainer.update(this.nextNews);
				this.showNews();
			}.bind(this)
		});
		
	},
	showNews: 		function(){

		new Effect.Appear(this.newsContainer,{
			duration:this.effectTime,
			afterFinish:function(){
			
				this.updateSelector();
				if(this.paused == false) this.createTimeOut();
				
			}.bind(this)
		});
		
	},
	createTimeOut: 	function(){
		
		this.newsTimer = setTimeout(function(){
			this.getNextItem();
			this.newsTimer = null;
			clearInterval(this.timer);
		}.bind(this),this.timeOut);
		
	},
	updateSelector: function(){
		$$("#newsSelector span").invoke('removeClassName','active');
		
		if(this.paused == true){
			if(this.newsCounter == 1) $("newsSelector").down().addClassName("active");
			else $("newsSelector").down().next(this.newsCounter-1).addClassName("active");
		}
		else if(this.newsCounter >= this.newsTotal){
			this.newsCounter = 1;
			$("newsSelector").down().addClassName("active");
		}else{
			$("newsSelector").down().next(this.newsCounter-1).addClassName("active");
			this.newsCounter++;
		}
	},
	observeClicks: 	function(){
	
		$$("#newsSelector span").invoke('observe','click',function(event){
			var element = Event.element(event);
					
			//clear timeout
			clearTimeout(this.newsTimer);
			this.newsTimer 	= null;
			
			this.getNextItem(element.innerHTML);
			this.paused 	= true;
		}.bind(this));
	}
});

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.showOverlay();
	},
	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});
	},
	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];
		
	}
});

var toolTip = Class.create({
	initialize: 	function(tip) {
		$$("body").first().insert(
	      new Element('div',{ id: 'toolTip' })
	    );
		this.tip  	= tip;
		this.delay	= 4000;
		this.tipDiv = $('toolTip'); 
		this.tipDiv.hide();
		this.tipDiv.update(this.tip);
		this.showTip();
	},
	showTip: 		function(){
		new Effect.Appear(this.tipDiv,{duration:0.3,delay:1});
		
		//setTimeout(function(){
		//	new Effect.Highlight(this.tipDiv,{ startcolor: '#21759B',endcolor: '#90bacd'});
		//}.bind(this),1300);
		
		//new Effect.Appear(this.tipDiv,{duration:0.3, delay:0.5});
		this.timer = setTimeout(function(){
			this.fadeTip();
		}.bind(this), this.delay );
	},
	fadeTip: 		function(){
		new Effect.Fade(this.tipDiv,{duration:0.3});
		this.timer = setTimeout( function(){
			this.tipDiv.remove();
		}.bind(this), 300);
	},
	cancelTip: 		function(){
    	if(!this.timer) return;
    	clearTimeout(this.timer);
    	this.tipDiv.remove();
  	}
});

var debug 	= Class.create({
	initialize: function(msg){
		this.msg = msg;
		this.createDiv();
	},
	createDiv: function(){
		if($('debug') == null) {
			this.debug = new Element('div',{id: 'debug'});	
			$$("body").first().insert(this.debug);
		}
		$('debug').update(this.msg);
	}
});

function sendForm(){
	
	var inputs = $('businessBoarding').getInputs('text');
	var missed = false;
	inputs.each(function(i){
		if($F(i) == "" && i.name != "resellers" && i.name != "other"){
			if(i.hasClassName("req")){
				i.addClassName("missed");
				missed = true;
			}
			//alert(i.name);
		}
	});
	
	if(missed == true){
		$('msg').update("<span>!</span> You did not fill in all the required fields. Please fill in the fields marked in blue.");
		return false;
	}
	
	
	form_values = $('businessBoarding').serialize(true);
	new Ajax.Updater('','pages/senddata.php',{
		parameters: form_values,
		onComplete: function () {
			var upd = "<h3>Your data has been send to NEC Philips. We'll contact you as soon as possible.</h3>";
			$('formContents').update(upd);
			$('header').scrollTo();
		}
	});
}

