﻿jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: '../w3vlaanderen/flanders_site/css/x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};


var currentCarrouselPosition = 0;
var carrouselTimer = {};
var isPauzed = false;

function initCarrousel(){
	$("#carrousel_link1").click(function(){ gotoCarrouselItem(0); });
	$("#carrousel_link2").click(function(){ gotoCarrouselItem(1); });
	$("#carrousel_link3").click(function(){ gotoCarrouselItem(2); });	
	$("#carrousel_link4").click(function(){ gotoCarrouselItem(3); });
	$("#carrousel_link5").click(function(){ gotoCarrouselItem(4); });
	$("#carrousel_link6").click(function(){ gotoCarrouselItem(5); });			
}

function nextCarrouselItem(){
	carrouselTimer = $.timer(10000,function(){
		hideCarrouselOldItem(true);
		currentCarrouselPosition = (currentCarrouselPosition == 10) ? 0 : currentCarrouselPosition + 2;
		showCarrouselNewItem(true);
		nextCarrouselItem();
	});
}

function toggleCarrousel(){
	if (isPauzed){ startCarrousel() } else { stopCarrousel() };
}

function startCarrousel(){
  if ($("dl#carrousel_items").length) {
  nextCarrouselItem();
  $("#carrousel_button").removeClass();
  $("#carrousel_button").addClass("carrousel_pauze"); 
  isPauzed = false; }
  
}

function stopCarrousel(){
  $.clearTimer(carrouselTimer);		
  $("#carrousel_button").removeClass();
  $("#carrousel_button").addClass("carrousel_play");  
  isPauzed = true;
}

function getCurrentPhoto(){
	var carrouselPhotoLink = $($('div#carrousel_photos').children()[currentCarrouselPosition/2]);	
	return $(carrouselPhotoLink.children()[0]);
}

function hideCarrouselOldItem(isAnimated){
	if (isAnimated){
		getCurrentPhoto().fadeOut('slow');
	} else {
		getCurrentPhoto().css("display","none");
	}
	$('dl#carrousel_items').children().removeClass('active');
}

function showCarrouselNewItem(isAnimated){
	if (isAnimated){
		getCurrentPhoto().fadeIn('slow');
	} else {
		getCurrentPhoto().css("display","block");		
	}			
	$($('dl#carrousel_items').children()[currentCarrouselPosition]).addClass('active');
	$($('dl#carrousel_items').children()[currentCarrouselPosition+1]).addClass('active');	
}

function gotoCarrouselItem(index){
	stopCarrousel();
	hideCarrouselOldItem(false);
	currentCarrouselPosition = index * 2;
	showCarrouselNewItem(false);
}

// Form Validation

function initValidation(){
$('form.jform .submit').click(function(){return validateForm(this);});
$('form.jform .formelement input').each(function(){ if($($(this).siblings("label").find("span.requiredindicator")).length != 0) { $(this).blur(function() {validateField(this);}) } else { return false; } });
$('form.jform .formelement textarea').each(function(){ if($($(this).siblings("label").find("span.requiredindicator")).length != 0) { $(this).blur(function() {validateField(this);}) } else { return false; } });
$('form.jform .formelement select').each(function(){ if($($(this).siblings("label").find("span.requiredindicator")).length != 0) { $(this).blur(function() {validateField(this);}) } else { return false; } });
}


function validateField(element) {
if (element.value == "") { $(element).parent().addClass('error');}
else { if (element.className == "e-mail") { if (element.value.replace(/^\s+|\s+$/g, '') == "" || element.value.indexOf('@') == -1 || element.value.indexOf(';') != -1 || element.value.indexOf('.') == -1 || element.value.indexOf(' ') > -1 )  { $(element).parent().addClass('error'); $(element).parent().find("p.notpresent").addClass('hide'); $(element).parent().find("p.invalid").addClass('show'); } else { $(element).parent().removeClass('error'); $(element).parent().find("p.notpresent").removeClass('hide'); $(element).parent().find("p.invalid").removeClass('show'); }} else { $(element).parent().removeClass('error'); return false; }}
}

function validateForm(form) {
$('form.jform .formelement input').each(function(){ if($($(this).siblings("label").find("span.requiredindicator")).length != 0) { validateField(this);} else { }});
$('form.jform .formelement textarea').each(function(){ if($($(this).siblings("label").find("span.requiredindicator")).length != 0) { validateField(this);} else { }});
$('form.jform .formelement select').each(function(){ if($($(this).siblings("label").find("span.requiredindicator")).length != 0) { validateField(this);} else { }});
if ($('form.jform .formelement.error').length != 0 ) { return false; } else { return true; form.submit(); }
}

// IE fixes
function iefixes() {

if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
// fix bullets: add markup with inline image
$('#blHeader .wrLine .wrBullets').append('<div id="fixBullets"><img src="../w3vlaanderen/flanders_site/css/header_bullets.png" alt="" width="109" height="18" /></div>');
// fix transparance of inline png images
$('#logo').supersleight();
$('#fixBullets').supersleight();
$('#blWeatherinfo img').supersleight();
// fix external link icons
$('.blQuicklinks ul li a.external').append('<img src="../w3vlaanderen/flanders_site/css/external.png" alt="" />');
// fix float error
$('.contactelements .contactelement').not('.address').find('.detail').each(function(){ $(this).find('.type').appendTo($(this))});
}

if ($.browser.msie && $.browser.version.substring(0,1) === '7') {
// fix bullets: add markup with inline image
$('#blHeader .wrLine .wrBullets').append(' <div id="fixBullets"><img src="../w3vlaanderen/flanders_site/css/header_bullets.png" alt="" width="109" height="18" /></div>');	
// fix external link icons
$('.blQuicklinks ul li a.external').append('<img src="../w3vlaanderen/flanders_site/css/external.png" alt="" />');
// fix float error
$('.contactelements .contactelement').not('.address').find('.detail').each(function(){ $(this).find('.type').appendTo($(this))});
}}

$(document).ready(function(){
	initCarrousel();
	startCarrousel();
	initValidation();
	iefixes();
});
