﻿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); });		
}

function initTooltips(){
	$('ul#channels_list li').each(function(){
		var description = $(this).children()[1].innerHTML;
		$(this).qtip({
			content: {text: description},
			show: { 
				effect: 'slide',				
				when: { event: 'mouseover' }
			},
			hide: { 
				effect: 'fade',				
				when: { event: 'mouseout' }
			},
			position: {
				corner: {
					target: 'bottomMiddle',
					tooltip: 'topMiddle'				
				}
			},
			style: {
				tip: {
					color: '#FFEE80',
					corner: 'topMiddle',
					size: {
						x: 13,
						y: 7
					}
				},
				background: '#FFEE80',
				color: '#565656',
				padding: '10px',
				fontWeight: 'bold',
				border: {
					width: 0,
					radius: 1,
					color: '#FFEE80'
				}
			}				
		});
	});	
}

function nextCarrouselItem(){
	carrouselTimer = $.timer(10000,function(){
		hideCarrouselOldItem(true);
		currentCarrouselPosition = (currentCarrouselPosition == 4) ? 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);
}

function changeFontSize(relativeSize){
	switch (relativeSize){
		case 1: newFontSize = '80%'; break;
		case 2: newFontSize = '90%'; break;
		case 3: newFontSize = '100%'; break;
		default: newFontSize = '80%';
	}
	$('ol#font_sizer li').removeClass('active');
	$('ol#font_sizer li a#font_size_'+relativeSize).parent().addClass('active');
	$('body').css('fontSize',newFontSize);
}


// 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(); }
}


// IE6 fixes
function ie6fixes() {

if ($.browser.msie && $.browser.version.substring(0,1) === '6') {

// fix watermerk in header
$('#header').append('<div id="logo_leeuw"></div>');

// fix left border when item is first and active

$('#blNavbar .wrPixel ul li.first.active').parents('#blNavbar .wrPixel').addClass('borderfix');

}}


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