var map;


$(function() {

	// spamfilter
	$('#frm-contact').append('<input type="hidden" class="written-with-javascript" name="__no-spam__" value="' + Math.random() + '" />');
	$('.hidespamfilter').hide();
	$('.hidespamfilter input').attr('value', 'concept7');


	$('.previous-block').click(function(){

		history.go(-1);
		return false;

	});

	// place c7flow
	if (el = document.getElementById('filler-black')){

		create_flash_controllers(el.parentNode);



		el.appendChild(div = document.createElement('div'));
		div.id='flash';
		div.style.overflow='hidden';
		div.style.height = '286px';



		var so = new SWFObject('../../flash/c7flow.swf', 'c7flow', '550', '287	', '9', "#000000");
		so.addParam('wmode', 'transparent');
		so.write('flash');

		$('#teaser .grid-grey a').click(function(){

			if (this.parentNode.className == 'next')
				document.getElementById('c7flow').next();
			else
				document.getElementById('c7flow').previous();

			return false;

		});

		dl = document.createElement('dl');
		document.getElementById('filler-black').appendChild(dl);

		dl.id='contact-item';
		dl.innerHTML = '';

	}

	$('p a').each(function(){

		rel = $(this).attr('rel');
		if (rel!=''){


			tmp = rel.split(/\|/);
			if (tmp.length>1){
				tmp = tmp[1];

				tmp = tmp.replace(/^\s*([a-z]{4})|([a-z]{4})\s*$/gi, '');
				tmp = tmp.replace(/[a-z]{2}\@[a-z]{2}/gi, '@');
				tmp = tmp.replace(/[a-z]{2}\.[a-z]{2}/gi, '.');


				$(this).attr('href', 'mailto:' + tmp);
				$(this).attr('rel', '');
			}
		}


	});

	$('#google-maps').each(function(){


   		map = new google.maps.Map2(document.getElementById(this.id));

		map.setCenter(point = new google.maps.LatLng(53.144427998155386, 6.569030284881592), 16);

	    var icon = new GIcon();
		icon.image = '/images/bullets-icons/marker-shadow.png';
		icon.iconSize = new GSize(39, 55);
		icon.iconAnchor = new GPoint(19, 48);
		icon.infoWindowAnchor = new GPoint(5, 1);

		marker = new GMarker(point, icon);
        GEvent.addListener(marker, "click", function() {
            var thehtml = '<div> \
                               <strong style="padding-bottom: 10px;">Concept7</strong> \
                               <div> \
                               <img src="/images/globals/pand-c7.jpg" alt="Voorkant kantoor Concept7" class="media-left" /> \
                               Prinses Irenelaan 1A \
                               <br /> \
                               9765 AL \
                               <br /> \
                               Paterswolde \
                               <br /> \
                               <br /> \
                               <br /> \
                               </div> \
                           </div>';
            var giwo = new Object();
            giwo.pixelOffset = new GSize(-1, 3);
            map.openInfoWindowHtml(point, thehtml, giwo);
        });
		map.addOverlay(marker);

		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GLargeMapControl());

	});

	$('#frm-newsletter').submit(function(){



		$('#frm-newsletter label').attr('id', '');
		$('#frm-newsletter label').html('Even geduld aub. <br />We voegen je gelijk toe aan de lijst!');

		email = this.emailaddress.value.replace(/\s\/\\/, '');
		// _rn = this.rn.value;
        _rn = 'weetniet';

		if (email != 'naam@email.nl'){
		$.ajax({
		    url: '/nl/nieuwsbrief/silent/' + email + '/',
		    type: 'GET',
		    dataType: 'html',
		    timeout: 5000,
		    error: function(){
		        appendNewsletterMsg('error', 'Kan niet aanmelden voor de nieuwsbrief.', 'Neemt u contact op met <a href="mailto:website@concept7.nl">website@concept7.nl</a>');
				_gaq.push(['_trackPageview', '/goal/newsletter/error/']);
		    },
		    success: function(html){
				html = html.replace(/^\s+|\s+$/, '');
				if (html.substring(0,1) == '+'){
					appendNewsletterMsg('sent', 'Je e-mailadres is succesvol verzonden', 'Je ontvangt een e-mail ter bevestiging.');
					_gaq.push(['_trackPageview', '/goal/newsletter/']);
				}
				else{
					if (html.substring(2,3) + '' == '2')
						appendNewsletterMsg('error', 'Kan niet aanmelden voor de nieuwsbrief.', 'Je staat al op de lijst!');
					else
						appendNewsletterMsg('error', 'Kan niet aanmelden voor de nieuwsbrief.', 'Neem contact op met <a href="mailto:website@concept7.nl">website@concept7.nl</a>');
						_gaq.push(['_trackPageview', '/goal/newsletter/error/']);
				}
		    }
		});


		}
		else{

			appendNewsletterMsg('error', 'Kan niet aanmelden voor de nieuwsbrief.', 'Ongeldig e-mailadres ingevuld');
		}



		return false;


	});

	// Animate links in doormat
	$('#doormat dd a').hover(function() {
		$(this).stop().animate({ paddingLeft: '20px' }, 300);
	}, function() {
		$(this).stop().animate({ paddingLeft: 0 }, 300);
	});

	// Clear inputs on blur
	$("#doormat input").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});


	$('input,textarea').focus(function(){
		$(this).parent().children('span').css('display', 'block');
	}).blur(function(){
		$(this).parent().children('span').css('display', 'none');
	});

	$('#select-branche').change(f = function(){
		e = document.getElementById('select-branche');
		if (!e) return;
		v = e.options[e.selectedIndex].value;

		if (v==0)
			$('h2.result-title').html('Alle branches');
		else
			$('h2.result-title').html(e.options[e.selectedIndex].text);

		$('.portfolio').each(function(){

			show = (v==0 || this.className.indexOf('branche' + v) != -1);
			if (document.getElementById('only-cases').checked){
				if ($(this).children('a').length==0)
					show = false;
			}

			if (show)
				$(this).animate({ opacity: 'show' }, "slow");
			else
				$(this).animate({ opacity: 'hide' }, "slow");
		});

	});

	$('#only-cases').click(f);
	f();

	// Write items when javascript is enabled
	$(".vcard-info").prepend("<a href='javascript:;' class='explain-vcard'>Wat is een vCard?</a>");


	$(".team-list:nth-child(odd)").addClass("margin-right20");

	$(".explain-vcard").click(function() {
		$(".tooltip-vcard").animate({ opacity: 'show' }, "slow")
	});

	$(".close-tooltip").click(function(){
		$(this).parents(".tooltip-vcard").animate({ opacity: 'hide' }, "slow");
	});

	// Open link in new window
	$(".external").attr("target","_blank");

	// Click entire block team-list
	$(".team-list").click(function(){
		window.location=$(this).find("a").attr("href");return false; // href attribute value
	});
	$(".team-list, .portfolio").addClass('selected'); // Add a class on the li
	$(".team-list, .portfolio ").hover(function(){
		$(this).addClass('team-hover'); // Place a extra class when hover
	},function(){
	$(this).removeClass('team-hover'); // Remove a class on mouseout
	});

	$(".portfolio").hover(function(){
		$(this).addClass('folio-hover'); // Place a extra class when hover
	},function(){
		$(this).removeClass('folio-hover'); // Remove a class on mouseout
	});

	$(".client").hover(function(){
		$(this).addClass('client-hover'); // Place a extra class when hover
	},function(){
		$(this).removeClass('client-hover'); // Remove a class on mouseout
	});
	// Write print link when javascript is enabled
  	$('.page-options').prepend('<li class="print"><a href="#print">Print pagina</a></li>');
	$('.page-options li.print a').click(function() {
		window.print();
		return false;
	});

	$("#header ul li:last-child").addClass("last-item");

	// Highlight form
	$('.contactform input, .contactform textarea').focus(function(){
		$(this).parents('p.row').addClass("current-form");
	}).blur(function(){
		$(this).parents('p.row').removeClass("current-form");
	});


    // Show or hide case studies
    $('.delete').click(function(){
		if (this.checked)
			$('.no-case-studie').animate({ opacity: 'hide' }, 'slow');
		else
			$('.no-case-studie').animate({ opacity: 'show' }, 'slow');
	});

	if ($(document).prettyPhoto)
		$("a[rel^='prettyPhoto']").prettyPhoto();

	// Set the scroll to anchor
	anchor.init()

	$('#frm-contact #email').keyup(email_password = function(){
		t = document.getElementById('email');
		if(t && t.form.author_email){
			if (t.value.toLowerCase() == t.form.author_email.value.toLowerCase()){
				// show password field!
				el = document.getElementById('email_password_div');
				if (!el){
					el = document.createElement('p');
					el.style.display = 'none';
					el.id = 'email_password_div';

					label = document.createElement('label');
					//label = 'passwrd';
					label.innerHTML = 'Wachtwoord';
					el.appendChild(label);

					input = document.createElement('input');
					input.name = 'password';
					input.id = 'password';
					input.type = 'password';
					el.appendChild(input);

					em = document.createElement('em');
					em.innerHTML = 'Dit wachtwoord is vereist om met dit e-mailadres te kunnen reageren';
					el.appendChild(em);

					t.parentNode.parentNode.insertBefore(el, document.getElementById('url').parentNode);
				}

				$(el).animate({ opacity: 'show' }, "slow")

			}
			else{
				el = document.getElementById('email_password_div');
				if (el){
					$(el).animate({ opacity: 'hide' }, "slow")
				}
			}
		}
	});

	email_password();

});

anchor = {
	init : function()  {
		$("a.anchorLink").click(function () {
			elementClick = $(this).attr("href")
			destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		  	return false;
		})
	}
}

function showText(text){
	tmp = text.split(/\|\|\|/);
	$('#teaser cite')[0].innerHTML = tmp[0].replace(/\&apos\;/g, '\'');

	if (tmp[1]){
		// link naar case
		$('#contact-item').html('<dt><a href="' + tmp[1] + '">Bekijk dit project</a></dt><dd>Of ga naar <a href="javascript: document.getElementById(\'c7flow\').next();">het volgende project</a> (' + tmp[2] + ')</dd>');
	}
	else{
		// geen link naar case
		$('#contact-item').html('Of ga naar <a href="javascript: document.getElementById(\'c7flow\').next();">het volgende project</a> (' + tmp[2] + ')');
	}
}

function openURL(url){
	document.location.href=url;
}

function appendNewsletterMsg(clss, msg, msg2){

	$('#frm-newsletter label').attr('id', 'newsletter-' + clss);
	$('#frm-newsletter label').html(msg + '<br />' + msg2);

}

function create_flash_controllers(p){
//el.parentNode.appendChild(document.createElement("	<div class='grid-grey'><ul><li><a href=\"/nl/home/\">Vorige</a></li><li class=\"next\"><a href=\"/nl/home/\">Volgende</a></li></ul><cite>&nbsp;</cite></div>	"));s

	// <div class="grid-grey">
	div = document.createElement('div');
	div.className = 'grid-grey';

		// <em>Pijl: welk item is actief</em>
		div.appendChild(em = document.createElement('em'));
		em.appendChild(document.createTextNode('Pijl: welk item is actief'));

		// <ul>
		div.appendChild(ul = document.createElement('ul'));

			// <li>
			ul.appendChild(li = document.createElement('li'));

				// <a href="">Vorige</a>
				li.appendChild(a = document.createElement('a'));
				a.href = '';
				a.appendChild(document.createTextNode('Vorige'));


			// <li href="" class="next">
			ul.appendChild(li = document.createElement('li'));
			li.className = 'next';

				// <a>Vorige</a>
				li.appendChild(a = document.createElement('a'));
				a.href = '';

				a.appendChild(document.createTextNode('Volgende'));

		// <cite>
		div.appendChild(document.createElement('cite'));
	p.appendChild(div);
}
