// JavaScript Document

function openContent(id) {
	
	$('.colorboxbutton').colorbox({ innerWidth:'896px', inline:true, href:'#extracolorbox-'+id });

}

function openPostimg(lnk, tit) {
	
	$('.postimg').colorbox({ href:''+lnk+'', title: ''+tit+'', maxHeight : '90%', maxWidth:'896px', scrolling: false, scalePhotos: true, photo: true });

}

function checkSlide(current, pages) {
			
	if (current != 1 && current != pages) {
		
		//console.log ('on-on');
		$('.submenul_prev').addClass('pointer');
		$('.submenul_prev').attr('src', template_directory+'/images/icons/blue-arrow-left-on.png');
		$('.submenul_next').addClass('pointer');
		$('.submenul_next').attr('src', template_directory+'/images/icons/blue-arrow-right-on.png');
		
	} else if (current == 1 && current != pages) {
		
		//console.log ('off-on');
		$('.submenul_prev').removeClass('pointer');
		$('.submenul_prev').attr('src', template_directory+'/images/icons/blue-arrow-left-off.png');
		$('.submenul_next').addClass('pointer');
		$('.submenul_next').attr('src', template_directory+'/images/icons/blue-arrow-right-on.png');
		
	}  else if (current != 1 && current == pages) {
		
		//console.log ('on-off');
		$('.submenul_prev').addClass('pointer');
		$('.submenul_prev').attr('src', template_directory+'/images/icons/blue-arrow-left-on.png');
		$('.submenul_next').removeClass('pointer');
		$('.submenul_next').attr('src', template_directory+'/images/icons/blue-arrow-right-off.png');
		
	} else if (current == 1 && current == pages) {
		
		//console.log ('off-off');
		$('.submenul_prev').removeClass('pointer');
		$('.submenul_prev').attr('src', template_directory+'/images/icons/blue-arrow-left-off.png');
		$('.submenul_next').removeClass('pointer');
		$('.submenul_next').attr('src', template_directory+'/images/icons/blue-arrow-right-off.png');

	} 

}

function openLanguages() {

	if (language != 'open') {
		$('.language').animate({ height: '66px', top: '-50px' }, 400);
		language = 'open';
		$('#mainflag').attr('src', template_directory+'/images/icons/blue-arrow-down-small.png');
		$('.language').removeClass('pointer');
	} else {
		$('.language').animate({ height: '16px', top: '0px' }, 400); // Let op: 
		language = 'closed'; // Deze 3 regels staat 
		$('.language').addClass('pointer'); // ook zo op de mouseout
		$('#mainflag').attr('src', template_directory+'/images/icons/blue-arrow-up-small.png');
	}
	
}

function get_cookie ( cookie_name )
{
	
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
	return ( unescape ( results[2] ) );
  else
	return null;
	
}


function days_between(date1, date2) {

  // The number of milliseconds in one day
  var ONE_DAY = 1000 * 60 * 60 * 24

  // Convert both dates to milliseconds
  var date1_ms = date1.getTime()
  var date2_ms = date2.getTime()

  // Calculate the difference in milliseconds
  var difference_ms = Math.abs(date1_ms - date2_ms)

  // Convert back to days and return
  return Math.round(difference_ms/ONE_DAY)

}


$(document).ready(function(){ 

	template_directory = 'http://quartas.nl/wp-content/themes/quartas-2011';

	// Tel alle slides in de slidecontainer 
	slides = $(".subcontainer > .subitem").size();
		
	// Bereken hoeveel pagina's er zijn
	pages = Math.ceil(slides/3);
	if (pages == 0) { pages = 1; }
	
	// Bereken de breedte van de container
	containerwidth = pages * 832;
	$('.subcontainer').css({ width: containerwidth+'px'});
	
	// Bereken de huidige pagina
	if ($('.subitem').hasClass('current')) { // Als er een huidige pagina is
		current = $(".subcontainer").find('.current').attr('id').split('_')[1]; 
	} else { 
		current = 1;
	}
	
	// Ze de slider op de juiste positie
	if (current != 1) {
		
		move = (current-1)*832;
		$('.subcontainer').animate({ left: '-='+move }, 0);
		
	}
	
	// Zorg dat de pijltjes actief of niet actief zijn
	checkSlide(current, pages);
	
	$('.submenul_next').live('click', function() {
		
		if (current != pages) {
		
			current++; checkSlide(current, pages);
			$('.subcontainer').animate({ left: '-=832' }, 800);
			
		}
		
	});
	
	$('.submenul_prev').live('click', function() {
		
		if (current != 1) {
		
			current--; checkSlide(current, pages);
			$('.subcontainer').animate({ left: '+=832' }, 800);
			
		}

	});
	
	jQuery('.subitem').live('mouseenter mouseleave', function(event) { 
		
		if (event.type == 'mouseenter') {
	
			$(this).addClass('active');
			//console.log(this);
	
		} else { // Mouseleave 

			$(this).removeClass('active');
			//console.log(this);

		}


	});
	
	$('.colorbox').colorbox({ maxHeight : '90%', maxWidth:'896px', scrolling: false, scalePhotos: true, photo: true });
	
	language = 'closed';
	
	jQuery('.language').live('mouseenter mouseleave', function(event) { 
		
		if (event.type == 'mouseenter') {
	
			$('#mainflag').attr('src', template_directory+'/images/icons/blue-arrow-up-small.png');
			$("#mainflagtext").html("Change country");
	
		} else { // Mouseleave 

			$('#mainflag').attr('src', template_directory+'/images/icons/flag-nl.png');
			$("#mainflagtext").html("Nederland");
			
			$('.language').animate({ height: '16px', top: '0px' }, 400); // Let op: 
			language = 'closed'; // Deze 3 regels staat 
			$('.language').addClass('pointer'); // ook zo in de functie

		}


	});
	
	// Cookies voor de Colorboxes van de Forms
	$('.offerteknop').colorbox({ innerWidth:'896px', inline:true, href:'#form-offerte' });
	$('.brochureknop').colorbox({ innerWidth:'896px', inline:true, href:'#form-brochure' });

	$openbox = get_cookie ( "openbox" );

	if ($openbox == 'offerte') {
		
		$.colorbox({ innerWidth:'896px', inline:true, href:'#form-offerte' });
	}
	
	if ($openbox == 'brochure') {
		
		$.colorbox({ innerWidth:'896px', inline:true, href:'#form-brochure' });
	}
	
	<!-- Begin
	// Store the current date and time
	var current_date = new Date()
	
	// Store the date of the next New Year's Day
	var new_years_date = new Date()
	new_years_date.setYear(new_years_date.getFullYear() + 1)
	new_years_date.setMonth(0)
	new_years_date.setDate(1)
	
	// Call the days_between function
	var days_left = days_between(current_date, new_years_date)

	var currentday = 365;
	var daysleft = days_left;
	
	// Countdown
	$("#counter").flipCounter({
		
		number: currentday, // the initial number the counter should display, overrides the hidden field
		numIntegralDigits:3, // number of places left of the decimal point to maintain
		numFractionalDigits:0, // number of places right of the decimal point to maintain
		digitClass:"counter-digit", // class of the counter digits
		counterFieldName:"counter-value", // name of the hidden field
		digitHeight:40, // the height of each digit in the flipCounter-medium.png sprite image
		digitWidth:30, // the width of each digit in the flipCounter-medium.png sprite image
		imagePath: template_directory+"/scripts/countdown/flipCounter-medium.png", // the path to the sprite image relative to your html document
		easing: jQuery.easing.easeOutCubic, // the easing function to apply to animations, you can override this with a jQuery.easing method
		duration: 5000, // duration of animations
		onAnimationStarted:false, // call back for animation upon starting
		onAnimationStopped:false, // call back for animation upon stopping
		onAnimationPaused:false // call back for animation upon pausing
		
	});
	
	$("#counter").flipCounter(
        "startAnimation", // scroll counter from the current number to the specified number
        {
                number: daysleft, // the number we want the counter to scroll to
                easing: jQuery.easing.easeOutCubic, // this easing function to apply to the scroll.
                duration: 5000, // number of ms animation should take to complete
                onAnimationStarted: false, // the function to call when animation starts
                onAnimationStopped: false, // the function to call when animation stops
                onAnimationPaused: false // the function to call when animation pauses
        }
	);
	
	
	// Login Popup
	$('.loginform').submit(function() {
		var centerWidth = (window.screen.width - 840) / 2;
		var centerHeight = (window.screen.height - 570) / 2;
								
		window.open('', 'formpopup', 'width=840,height=580,resizeable=no,scrollbars=no,location=no,left=' + centerWidth + ',top=' + centerHeight);
		this.target = 'formpopup';
	});
	
	// Check of er formulieren zijn die de .captcha div bevatten, en stop deze in een het forms object
	forms = $("form").find('.captcha');
		
	// Loop door het forms object
	var i=0;
	while (i <= (forms.length-1)) {
		
		// Haal het formid uit het gevonden .captcha div
		formid = $(forms[i]).attr('id').split('-')[1];
		
		// Voer de captcha functie uit met het bijbehorende formid: information, offerte, brochure	
		captcha(template_directory, formid);
		
	i++; }
		
});
