(function ($){
	var hover = {"hover": true},
		photos = $('#photo-fade li'),
		photos_size = photos.size(),
		sp = $("ul.smallphotos"),
		korting = $("#korting-button");

	// CUFON HEADINGS
	Cufon.replace	("h1, h2", { fontFamily: "Dlight" })
					(".companies h3", { fontFamily: "Dregular" })
					("#navigation li.selected a", hover, { fontFamily: "Dregular" })
					("#navigation a", hover, { fontFamily: "Dregular" })
					("h1.italic", { fontFamily: "Dlightitalic" })
					(".architectuurprijs-2011 #contents h1, .architectuurprijs-2011 #extra h2", { fontFamily: "advent"})

	if($.browser.msie && $.browser.version.substring(0,1) === '6'){
		jQuery('#navigation > li').hover(
			function(){jQuery(this).addClass('hover');},
			function(){jQuery(this).removeClass('hover');}
		);
	}

	if (photos_size > 0){
		photos.each(function(i){
			var $this = $(this), time = (i * 400);
			setTimeout(
				function(){
					$this
						.css({"display": "none", "visibility": "visible"})
						.fadeIn();
					if (korting.size() > 0 && photos_size == (i + 1) && $(".bottom-form").size() == 0){
						setTimeout(function(){
							korting.fadeIn();
						}, 400);

					}
				},
				time
			);
		});
	}

	if (sp.size() > 0){
		foto = $(".foto");
		var pics = [], i = 0;

		/* create array of available pics */
		sp.find("a")
			.each(function(){
				pics.push($(this).find("img").attr("src"));
			});

		/* Rotate function */
		swap_img = function(new_src){
			i++;
			if (i == pics.length){
				i = 0;
			}
			var src = pics[i];
			if (String(new_src).indexOf("files/attachments") != -1){ // come from click function?
				src = new_src;
			}
			foto.fadeOut('slow', function(){
				$(this).attr("src", src).fadeIn('slow');
			});
		};

		/* Start roulatie */
		var init = function(){
			/* IE6 */
			if(typeof document.body.style.maxHeight == "undefined"){
				intv = window.setInterval("swap_img()", 8000); // 10 sec
			}
			else{
				intv = setInterval(swap_img, 8000);
			}
		};
		init();
		sp.find("a").click(function(){
			clearInterval(intv);
			swap_img($(this).find("img").attr("src"));
			init();
			return false;
		});
	}

	jQuery('form#mailinglist').submit(function(){
		var valid = true, form = jQuery(this);
		jQuery('#warning').remove();
		form.find('.required').each(function(){
			var r = new RegExp('^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$');
			if(this.value == '' || (this.id == 'sEmail' && !r.test(this.value))){
				this.style.borderColor = 'red';
				valid = false;
			}else{
				this.style.borderColor = 'pink';
			}
		});
		if(!valid){
			form.prepend('<div id="warning" style="margin-bottom: 10px;"><p>Vul a.u.b. alle verplichte velden correct in.</p></div>');
			jQuery("#warning").get(0).scrollIntoView(true);
		}
		return valid;
	});

	/* Monoslideshow */
	var startImg = $("#flashContent").attr("class"),
		cat = $("#flashContent").text(),
		url = "?startImg="+startImg,
		flashvars = {dataFile: "/xml/"+cat+"monoslideshow.php"+url, showLogo: "false", showRegistration: "false"},
		params = {allowscriptaccess: "sameDomain", wmode: "transparent", allowfullscreen: "true", allownetworking: "all", bgcolor: "000000"},
		attributes = {id: "monoslideshow", name: "monoslideshow"};
	//$("body").append("/xml/"+cat+"monoslideshow.php"+url);

	if (!$("body").hasClass("no-flash")){
		swfobject.embedSWF("/flash/monoslideshow.swf", "flashContent", "999", "216", "10", false, flashvars, params, attributes, function(e){
            if (!e.success){
                $("body").addClass("flash-fallback");
                $("#flashContent").html("");
            }
        });
	}

	/* form validation */
	$.validator.addMethod("telefoonnummer", function(value, element) {
		return this.optional(element) || /^(\(){0,1}(\+[0-9]{2}|\+[0-9]{2}\s{0,1}\(0\)|\(\+[0-9]{2}\)\s{0,1}\(0\)|(\+)?00[0-9]{2}|0)(\)){0,1}([0-9\-\s]{9,16})$/.test(value);
	}, "Vul a.u.b. een geldig telefoonnummer in.");
	$.extend($.validator.messages, {
		required: "Vul aub dit verplichte veld in",
		email: "Vul aub een geldig e-mailadres in"
	});
	var f = $("form.validate").each(function(){
		var validate_options = {
			rules: {
				sPhone: {telefoonnummer: true}
			}
		}, $this = $(this), imgext = {"accept": "png|jpe?g|gif|bmp", "required": true}, pdfext = {"accept": "pdf", "required":true},
			imgmsg = "Kies aub een .jpg, .png, .gif of .bmp bestand",
			pdfmsg = "Kies aub een .pdf bestand";

		if ($this.attr("id") == "architectuurprijs-form"){
			validate_options = {
				"rules": {"sPhoto1": imgext, "sPhoto2": imgext, "sPhoto3": imgext, "sMap": pdfext, "sDetails":pdfext,"sDescription":{"maxlength":600}},
				"messages":{"sPhoto1": imgmsg, "sPhoto2": imgmsg, "sPhoto3": imgmsg, "sMap": pdfmsg, "sDetails":pdfmsg},
				"errorPlacement": function(er,el){
					if(el.attr("name")=="iAgree"){
						el.parent().append(er);
					}else{
						er.insertAfter(el);
					}
				}
			};
		}
		$this.validate(validate_options);
	});

	var n_form = $("#nominatie-form");
	if (n_form.size() > 0){
		n_form.validate({
			"rules": {
				"sEmail": {
					"required": true,
					"email": true,
					"remote": "/remote/nominatie.php"
				}
			},
			"messages": {
				"sEmail": {
					"remote": "Er is al een keer gestemd met dit e-mailadres"
				}
			},
			"errorPlacement": function(er, el){
				if(el.attr("name")=="iTAC"){
					el.parent().append(er);
				}else if(el.attr("name") == "sNominee"){
					$(".photos").next().next().after(er);
				}else{
					er.insertAfter(el);
				}
			}
		});
	}

	$("#register-form, #lost-pass-form").each(function(i,el){
		$(el).validate();
	});

	var cont = $(".toggle-content");
	if (cont.size() > 0){
		cont.hide();
		$(".toggle-header").click(function(ev){
			ev.preventDefault();
			var $this = $(this), h2 = $this.closest("h2"), div = $(".content-"+this.id);
			//cont.hide();
			h2.toggleClass("down");
			div.toggle();
		});
	}

	$("input.fake-input").focus(function(ev){
		$(this).next().click(); // probably the input[type=file]
	})
	$("input.file").change(function(ev){
		$(this).prev().val(this.value);
	});

	$(".portal-link").click(function(ev){
		ev.preventDefault();
		$(this).parent().parent().find(".inner").toggle();
	});

	$(".products .lb").fancybox();
	$(".bestelformulier .product").fancybox({
		'autoScale' : false
	}

	);
	//fancbox implement
}(jQuery));
