/* * * * * * * * * * * * * * * * * * * * * * *
 *   C A R E N Z A  S C R I P T  F I L E     *
 *                                           *
 *   (c) Copyright 2008 - CARENZA            *
 *   http://www.carenza.nl/                  *
 *                                           *
 * * * * * * * * * * * * * * * * * * * * * * */

var numPages;
var currentPage = 0;
var interval = null;
var intervalTime = 10000;

$(function() {
	if($('#twitterfeed').length > 0) {
		$('#twitterfeed').load('/includes/twitter.inc.php');
	}

	numPages = $('#sliderContainer img').length-1;
	interval = setInterval("nextPage()", intervalTime);
	
	var submenu = document.getElementById("submenu");
	
	if(links = document.getElementById("menu").getElementsByTagName("a")) {
		loc1 = location.href.lastIndexOf("/");
		loc2 = location.href.lastIndexOf("/",loc1-1);
		foldername = location.href.substring(loc2+1,loc1);
		filename = location.href.substring(loc1+1);
		
		for (var i=0; i<links.length; i++) {
			if (i == 0 && links[i].href == location.href) { links[i].parentNode.className = "active"; break; }
			//if ( i == 0) alert(links[i].href.length+' '+links[i].href.lastIndexOf("/"));
			if ( i == 0 && links[i].href.lastIndexOf("/") == links[i].href.length - 2) { links[i].parentNode.className = "active"; }
			if (links[i].href.substring(links[i].href.lastIndexOf("/"),links[i].href.lastIndexOf("/",links[i].href.lastIndexOf("/")-1)+1) == foldername) {
				if(links[i].parentNode.parentNode.id == "menu") links[i].parentNode.className = "active";				
			}
			if (links[i].href.substring(links[i].href.lastIndexOf("/")+1) == filename) {
				if(links[i].parentNode.parentNode.parentNode.className == "active") links[i].parentNode.className = "activesub";
			}
		}
	}
	if(submenu) {
		links2 = submenu.getElementsByTagName("a");
		for (var i=0; i<links2.length; i++) {
			if (links2[i].href.substring(links2[i].href.lastIndexOf("/")+1) == filename) {
				links2[i].className = "active";
			}
		}
	}
	sIFR.replaceElement(named({sSelector:"#content h1, #content h2, #content_r h2, h3", sFlashSrc:"../includes/cooper.swf", sWmode:"transparent", sColor:"#99CC00", sFlashVars:"textalign=left&offsetTop=0"}));
	$(".film").click(function() { clearInterval(interval); });
	$("#news input").focus(function () { if($(this).val()==$(this).attr("title")) $(this).val(""); });
	$("#news input").blur(function () { if($(this).val()=='') $(this).val($(this).attr("title")); });	
});

function checkContactForm() {
	if(ccscheck('naam', '') + ccscheck('mail','mail') + ccscheck('mssg','') == 3) return true; else return false;
}
function checkNewsForm() {
	if(ccscheck('newsnaam', '') + ccscheck('newsmail','mail') == 2) return true; else return false; // mail, post, phone or other
}

function checkBrochureForm() {
	check = ccscheck('naam', '') + ccscheck('mail','mail');
	//alert(check);
	if(check == 2) return true; else return false;
}

function ccscheck(inputId, type) {
	var failCSS = 'invalid';
	if(check(inputId,type)==1) {
		document.getElementById(inputId).className = 'goodCSS';
		return 1;
	} else {
		document.getElementById(inputId).className = 'failCSS';
		document.getElementById(inputId).onkeyup = new Function("ccscheck('"+inputId+"', '"+type+"');");
		return 0;
	}
}

function check(inputId, type) {
	var id = document.getElementById(inputId);
	var v = id.value;
	var t = id.title;
	switch (type) {
		
		case 'mail':
			var atIndex = v.indexOf('@');
			var dotIndex = v.lastIndexOf('.');
			if (atIndex < 1 || atIndex>=(v.length-2) || dotIndex < 1 || dotIndex>=(v.length-2) || atIndex > dotIndex - 1)
				return 0;
			else
				return 1;
			break
	
		case 'phone':
			var re = new RegExp('[^\\d]', "g");
			v = v.replace(re, "");
			if( parseInt(v,10) >= 100000000 && parseInt(v,10) <= 800000000 ) {
				v = '0' + parseInt(v,10);
				// id.value = v;
				return 1;
			} else return 0;
			break
		
		case 'postc':
			var re = new RegExp('(\\d{4})[\\s]*?([A-Z]{2}).*', "i");
			v = v.replace(re, "$1$2");
			v = v.toUpperCase();
			// id.value = v;
			if(v.length == 6 && re.test(v)) return 1; else return 0;
			break
		
		default:
			var length = v.length;
			if(length >= 2 && v != t) return 1; else return 0;
			break
	}

}

function nextPage() {
	gotoPage((currentPage + 1) % (numPages+1), false);
}

function gotoPage(index, resetInterval) {
	if (index == 0 && currentPage >= numPages) {
		index = 1;
		$('#sliderContainer').css('marginLeft','0px');
	}	
	if (index >= (numPages+1)) {
		return false;
	}
	
	if(resetInterval === undefined) {
		clearInterval(interval);
		interval = setInterval("nextPage()", intervalTime);
	}
	
	currentPage = index;
	var offset = -index * parseInt($('#headerimage').css('width'));
	$('#slidernav a').removeClass('active');
	$('#slidernav a').eq(index % numPages).addClass('active');
	if($('#sliderContainer img').eq(index).attr("title") != undefined) $('#sliderText').html('<h2>'+$('#sliderContainer img').eq(index).attr("title")+'</h2><p>'+$('#sliderContainer img').eq(index).attr("alt")+'</p>');
	speed = Math.abs(parseInt($('#sliderContainer').css('marginLeft')) - offset);
	$('#sliderContainer').animate({marginLeft: offset}, speed);
	return false;
}

