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

var paginationCurrentPage = 0;
var paginationMaxPages = 0;
var url = "http://www.carenza.nl/";

$(document).ready(function() {

	if($('#paginationSlider').size() > 0) {
		resetNavigation();
	}

	if($('#twitterfeed').length > 0) {
		$('#twitterfeed').load('/twitter.php');
	}
	
	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);
		if(filename.lastIndexOf("?")>0) filename = filename.substring(0,filename.lastIndexOf("?"));
		for ( var i = 0; i < links.length; i++) {
			//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].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].href.substring(links[i].href.lastIndexOf("/")+1) == filename) {
				links[i].className = "active"; break;
			}
		}
	}
	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";
			}
		}
	}


	$("#header").hover(
	    function(){ $("#header").stop(); $("#header").animate({height:"141px" }, 500); }, 
	    function(){ $("#header").stop(); $("#header").animate({height: "40px" }, 500); }
	);

	$(".pfl > span").hover(
    		function(){ $(this).stop(); $(this).animate({marginLeft:"-195px" }, 300); }, 
    		function(){ $(this).stop(); $(this).animate({marginLeft:"0px" }, 300); }
    	);
	
	jQuery.event.add(window, "resize", adjustBackground);

    //$("#menu a").hover(
    //  function(){ $(this).stop(); $(this).animate({paddingLeft:"20px" }, 200); }, 
    //  function(){ $(this).stop(); $(this).animate({paddingLeft: "5px" }, 200); }
    //);
	
	
	//sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:url+"includes/carenza.swf", sWmode:"transparent", sColor:"#33ff00", sFlashVars:"textalign=left&offsetTop=0"}));
	//sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:url+"includes/carenza.swf", sWmode:"transparent", sColor:"#33ff00", sFlashVars:"textalign=left&offsetTop=0"}));
	sIFR.replaceElement(named({sSelector:".item h3", sFlashSrc:url+"includes/carenza.swf", sWmode:"transparent", sColor:"#00CC00", sFlashVars:"textalign=left&offsetTop=0"}));
	sIFR.replaceElement(named({sSelector:"h3", sFlashSrc:url+"includes/carenza.swf", sWmode:"transparent", sColor:"#666666", sFlashVars:"textalign=left&offsetTop=0"}));
	sIFR.replaceElement(named({sSelector:"#header h4", sFlashSrc:url+"includes/carenza.swf", sWmode:"transparent", sColor:"#666666", sFlashVars:"textalign=left&offsetTop=0"}));
	if(typeof slider    == 'function') { slider(); }
	if(typeof sliderref == 'function') { sliderref(); }	
	if(foldername == "contact" && filename=="route.html") { googlemaps();}	

	document.getElementById("menu").onmouseover = function() {
	  var a = document.getElementById("menu").getElementsByTagName("a");
	  for (var i=0; i<a.length; i++) { if(a[i].className == "active") { a[i].className="activ"; } }
	}

	document.getElementById("menu").onmouseout = function() {
	  var a = document.getElementById("menu").getElementsByTagName("a");
	  for (var i=0; i<a.length; i++) { if(a[i].className == "activ") { a[i].className="active"; } }
	}

	$('#beesie').hover (function() {
		$(this).stop().animate({ bottom: "-10" }, 'slow');
	}, function() {
		$(this).stop().animate({ bottom: "-90" }, 'slow', 'easeOutBack');
	});

});

function adjustBackground() {
	var w = $(window).width();
	var h = $(window).height();
	var currentImg = $("body").css('background-image');
	var re = new RegExp('_[0-9]+\.jpg', "g");

	currentImg = currentImg.replace(re, '_1280.jpg');

	if(h<800 && w<1280) currentImg = currentImg.replace(re, '_1280.jpg');
	else if(h<1050 && w<1680) currentImg = currentImg.replace(re, '_1680.jpg');
	else currentImg = currentImg.replace(re, '_1920.jpg');                
	$("body").css('background-image',currentImg);
}

function checkContactForm() {
	if(ccscheck('naam', '') + ccscheck('mail','mail') + ccscheck('mssg','') == 3) return true;
	else return false;
}

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

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;
	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) return 1; else return 0;
			break
	}

}

function googlemaps() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("googlemaps"));
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(
      'Schiehavenkade 262 Rotterdam',
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 14);
          var marker = new GMarker(point);
          map.addOverlay(marker);
        }
      }
    );
	map.addControl(new GSmallMapControl());
  }
}

function setDirection() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("googlemaps"));
    directions = new GDirections(map);
    directions.load("from: "+document.getElementById('straat').value+" "+document.getElementById('plaats').value+" to: Schiehavenkade 262 Rotterdam");
    map.addControl(new GSmallMapControl());
  }
}


function removeDummyText(inputId, dummyText) {
	var elem = $('#' + inputId);
	var value = elem.val();

	if(value == dummyText) {
		elem.val('');
		elem.removeClass('nfi');
	}
}

function addDummyText(inputId, dummyText) {
	var elem = $('#' + inputId);
	var value = elem.val();

	if(value == '') {
		elem.val(dummyText);
		elem.addClass('nfi');
	}
}

function resetNavigation() {
	// Get number of pages for the pagination
	var numElems = $('#paginationSlider div:visible').size();
	paginationMaxPages = Math.floor((numElems - 1) / 9);

	// create navigation
	var navigationHtml = '<a href="Javascript: previousPage();">vorige</a>';
	for(i = 0; i < paginationMaxPages + 1; i++) {
		if (i == 0)
			navigationHtml += ' <a id="paginationNavigate' + i + '" class="activePage" href="Javascript: gotoPage('+i+');">' + (i + 1) + '</a>';
		else
			navigationHtml += ' <a id="paginationNavigate' + i + '" href="Javascript: gotoPage('+i+');">' + (i + 1) + '</a>';
	}
	navigationHtml += ' <a href="Javascript: nextPage();">volgende</a>';
	$('#paginationNavigation').html(navigationHtml);
}

function nextPage() {
	gotoPage(paginationCurrentPage + 1);
}


function previousPage() {
	gotoPage(paginationCurrentPage - 1);
}

function gotoPage(page) {
	if(page >= 0 && page <= paginationMaxPages) {
		$('#paginationNavigate' + paginationCurrentPage).removeClass('activePage');
		$('#paginationNavigate' + page).addClass('activePage');
		var topMargin = (-480 * page) + 'px';
		$('#paginationSlider').animate({marginTop: topMargin}, 500);
		paginationCurrentPage = page;
	}
}

function filterElements() {
	// get classes to filter on
	var checkBoxes = $('#filter input:checkbox:checked');
	var selectedClasses = new Array(/*checkBoxes.size()*/ 5);
	var arrayIndex = 0;
	for(i = 0; i < checkBoxes.size(); i++) {
		var checkBox = $(checkBoxes.get(i));
		if(checkBox.val() !== null) {
			selectedClasses[arrayIndex] = checkBox.attr('class');
			arrayIndex++;
		}
	}

	// get elements to select
	var selector = '';

	for(i = 0; i < arrayIndex; i++) {
		var className = selectedClasses[i];
		selector += '#paginationSlider div.' + className + ', ';
	}
	$('#paginationSlider div a, #paginationSlider div p').fadeOut(200);
	if(selector == '') {
		$('#paginationSlider div').hide();
		$('#paginationSlider div#noSelection').show();
	} else {
		setTimeout('showSelectorHelperFunction(\'' + selector + '\');', 200);
	}
	setTimeout('resetNavigation();', 500);
}

function showSelectorHelperFunction(selector) {
	// Set page to 0
	$('#paginationSlider').css({marginTop: "0px"});
	paginationCurrentPage = 0;
	
	$(selector).show();
	$('#paginationSlider div a, #paginationSlider div p').fadeIn(200);
	$('#paginationSlider div#noSelection').hide();
	$('#paginationSlider div:not(' + selector + ')').hide();
}
	


