
// globals voor counter defineren
/*var c, timerInterval;
var timePerInterval = 10; // Elke 10 msec wordt de teller geactiveerd
var totalTime = 1000; // 1500msec totaal, dus 1.5 sec
var percentagePerStep = 100 / (totalTime / timePerInterval); // Per stap op te hogen percentage
var steps = 0; // We beginnen, goh, bij stap 0
*/
window.addEvent('domready', function(){
	initNavigation();
//	initLogo();
	initPopUp();
	initGoogleMaps();
	initIntro();
	initNewsletter();

	initHR();

	initButtons();
	
	
});	

function initButtons() {
	
	
	
	$(document.body).getElements('img.button_img').each( function( el ){
		el.addEvents({
		  'mouseenter': function(){
			 // Always sets the duration of the tween to 1000 ms and a bouncing transition
			 // And then tweens the height of the element
			 el.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
			 }).tween('margin-bottom', '-54px');
		  },
		  'mouseleave': function(){
			 // Resets the tween and changes the element back to its original size
			 el.set('tween', {}).tween('margin-bottom', '-10px');
			 
		  }
	   });
	});
}

function initHR(){
	var hrs = document.getElements('hr');

	$each(hrs, function(el, index){
		div = new Element('div', {
			'class': 'hr'
		});

		div.wraps(el);
	}); 
}

window.addEvent("unload", function(){
	unloadGoogleMaps();
});	


function initNewsletter(){

	$(document.body).getElements('.newsletter_button').each( function( el ){
		el.addEvent('click', function(){
			showNewsletterPopup();
		});	
	});
	
	/*if (!$('newsletter')) return;
	//newsletter_button
	$('newsletter').addEvent('click', function(){
		showNewsletterPopup();
	});*/
}

function showNewsletterPopup(unsubscribe, email){

	if(!$('background')) return;

	if(unsubscribe && email){
		iframeSrc = 'http://www.sunfactor.nl/newsletter.php?view=unsubscribe&email='+email;
	}else{
		iframeSrc = 'http://www.sunfactor.nl/newsletter.php';
	}

	backgroundDiv = $('background');	
	subscribeDiv = $('newsletter_subscription');

	backgroundDiv.setStyle('display','block');
	subscribeDiv.setStyle('display','block');

	if(subscribeDiv.getElements('iframe').length == 0){
		
		closeButton = new Element('p', {
		'html': 'x',
		'id':'close_p',
		'events': {
		'click': function(){
			hideNewsletter();      	
		}
		}
		});
		subscribeDiv.appendChild(closeButton);	
		
		iframe = new Element('iframe', {
		'src': iframeSrc,
		'scrolling': 'no',
		'name': 'newsletter_frame',
		'height':'180px',
		'frameborder': '0',	    
		'styles': {	    
			//'border': '1px solid white'
		}
		});
		subscribeDiv.appendChild(iframe);
				
		
	}


	backgroundDiv.addEvent('click', function(){		
		hideNewsletter();
	});
}

function hideNewsletter(){
	backgroundDiv.setStyle('display','none');
	subscribeDiv.setStyle('display','none');
}

function appendCounter() {
	c.set('text', Math.round(++steps*percentagePerStep) + '%');

	// Totaaltijd verlagen met de tijd-per-stap waarde
	totalTime -= timePerInterval;
	
	// Als de tijd op is stoppen we de interval
	if (totalTime <= 0) {
		window.clearInterval(timerInterval);
	}
}


function initIntro(){

	if (!$('intro_main')) return;

	$('intro_main').setStyle('opacity', '0.0');
	$('palm_left').setStyle('opacity', '0.0');

	$('intro_main').setStyle('display', 'block');
	$('palm_left').setStyle('display', 'block');

	$('intro_main').fade('in');	
	$('palm_left').fade('in');

	if (!$('progress_bar')) return;

	pb = new dwProgressBar({
		container: $('progress_bar'),
		startPercentage: 100,
		speed:2000,
		boxID: 'progress_bar_box',
		percentageID: 'progress_bar_perc',
		displayID: 'progress_bar_text',
		displayText: true
	});
	
	$('progress_bar_text').setStyle('opacity', '0.0');
	$('progress_bar_text').setStyle('display', 'none');

	var myFx = new Fx.Tween($('progress_bar_text'));
	myFx.start('opacity', 0, 1);
	myFx.onComplete(setTimeout("redirectToHome()", 2500));
	
	if (!$('counter')) return;
	c = $('counter'); 
	timerInterval = window.setInterval('appendCounter()', timePerInterval);
}


function redirectToHome(){
	window.location = "http://www.sunfactor.nl/sunfactor.html";
}

function initGoogleMaps(){
	if (!$('google_map')) return;
	
	if (GBrowserIsCompatible()) {

		var icon = new GIcon();
		icon.image = "/img/gm_logo.png";
		icon.iconSize = new GSize(55, 39);
		icon.iconAnchor = new GPoint(50, 15);
		icon.infoWindowAnchor = new GPoint(40, 10);

		var point = new GLatLng(52.077343,5.114329);
		var html =  '<div id="gm_balloon"><h2>Sunfactor Zonnestudio</h2><img src="/img/gm_img.jpg" width="80px" /><p>Balijelaan 18 <br/> 3521 GS Utrecht <br/> 030 2898223</p><br class="clear"/></div>';

		var marker = createMarker(point,html,icon);

		var map = new GMap2(document.getElementById("google_map"));
		map.addControl(new GSmallMapControl());  
		map.setCenter(new GLatLng(52.077343,5.114329), 13);

		map.addOverlay(marker);

	}
}

//Create marker and set up event window
function createMarker(point,html,icon){
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}


function unloadGoogleMaps(){
	if (!$('google_map')) return;

	GUnload();
}

function initHeader(data){
	if (!$('rotator')) return;	
	var myShow = new Slideshow('rotator', data, { captions: true, controller: true, delay: 9000, height: 239, hu: '', width: 665 });
}

function initLogo(){
	// als er een popup getoond wordt geen logo tonen
	if ($('popup')) return;

	//initLogoFlash();
}

function initLogoFlash(){
	return;
	swfobject.embedSWF("/flash/logo_sunfactor.swf", "logo", "180", "115", "8.0.0", "expressInstall.swf");
}

function initPopUp(){
	if (!$('popup')) return;

	// flash logo div niet tonen
	//$('logo').setStyle('display','none');

	$('popup').setStyle('display','block');
	$('background').setStyle('display','block');
	
	$('background').addEvent('click', function(){
		closePopUp();		
	});
	$('close').addEvent('click', function(){
		closePopUp();		
	});

}

function closePopUp(){
	$('popup').setStyle('display','none');
	$('background').setStyle('display','none');

	//initLogoFlash();
}

function initNavigation(){
	if (!$('navigation')) return;

	$('navigation').getElements('li.menu').each( function( elem ){
		var list = elem.getElement('ul.links');	

		if(list){
			var myFx = new Fx.Slide(list).hide();			

			elem.addEvents({
				'mouseenter' : function(){ 	
				//	console.log(myFx);
					myFx.cancel();
					myFx.slideIn();					
				},
				'mouseleave' : function(){ 
				//	console.log('out');
					myFx.cancel();
					myFx.slideOut();					
				}
			});	
		}
	})
	
	$('navigation').getElements('ul.links').each( function( elem ){
		elem.setStyle('display', 'block');
	})
}

