/* this prevents dom flickering, needs to be outside of dom.ready event: */
document.documentElement.className += 'js_active';
/*end dom flickering =) */


jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};
	
	
function menu() {
	jQuery(function(){
		jQuery('ul.sf-menu').superfish({ 
            hoverClass:    'sfHover',          // the class applied to hovered list items 
			pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page 
			pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
			delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
			animation:     {height: 'toggle'}, // an object equivalent to first parameter of jQuery’s .animate() method 
			speed:         'normal',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
			autoArrows:    true,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
			dropShadows:   true,               // completely disable drop shadows by setting this to false 
			disableHI:     true,               // set to true to disable hoverIntent detection 
			onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul 
			onBeforeShow:  function(){},       // callback function fires just before reveal animation begins – 'this' is the ul about to open 
			onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul 
			onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
        }); 
	});
}


function topnav(){
	jQuery("#topnav .search_field div").css({display: "none"}); // Opera Fix
	jQuery("#topnav .search_link").removeAttr("href");
	jQuery("#topnav .search_link").click(function()
		{	
			jQuery("#topnav .search_field div").css({overflow:"hidden", height:"auto", width:"auto", display:"none"}).slideFadeToggle(400, function()
			{ 
				jQuery(this).css({overflow:"visible", width:"auto", height:"auto"});
				jQuery("#topnav input[type=text]").select();   
			});	
			jQuery("#topnav .search_link").css({overflow:"hidden", height:"auto", width:"auto", display:"none"}).slideUp(500, function() {	jQuery(this).css({overflow:"hidden", display:"none"}); });
		} );
}


function searchfield() {
	$("input[type=text]").focus(function()
    {
    	$(this).select();   
	});
}


function slider() {
	if(typeof jQuery().nivoSlider == 'function') {
		$('#slider').nivoSlider({
			effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
			slices:15,
			animSpeed:500,
			pauseTime:3000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:true, //Next & Prev
			directionNavHide:true, //Only show on hover
			controlNav:true, //1,2,3...
			controlNavThumbs:false, //Use thumbnails for Control Nav
	        controlNavThumbsFromRel:false, //Use image rel for thumbs
			controlNavThumbsSearch: '.jpg', //Replace this with...
			controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
			keyboardNav:true, //Use left & right arrows
			pauseOnHover:true, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:0.8, //Universal caption opacity
			beforeChange: function(){},
			afterChange: function(){},
			slideshowEnd: function(){} //Triggers after all slides have been shown
		});
	}
}

function hover_shadow() {

		$(".gallery").children("a").hover( function () { 
			$(this).append('<span class="overlay"></span>');
			$(this).find(".overlay").css({ opacity: 0 }).animate({ opacity: 0.1 });
		}, function () { 
			$(this).find(".overlay").stop(true, true).animate({ opacity: 0 }, function () { $(this).remove();});
		});
		
}


function indexInArray(array, obj) {
       for(var i=0; i<array.length; i++){
            if(array[i]==obj){
                return i;
            }
        }
        return -1;
}


var mapping = new Array();
var verwijderdeIds = new Array();
var bestanden = new Array();
var verwijderd = new Array();
var aantalElementen = 0;

function contactform() {
		/* contact-form handler */
		$("#formSubmit").click(function () {
				
				for (var id in mapping) {
					if (indexInArray(verwijderdeIds, id) < 0) 
						bestanden.push(mapping[id]);	
					else 
						verwijderd.push(mapping[id]);	
				}
				
				var that = $(this);
				that.css ({ display: "none" });
				$(this).parent().append('<img src="./css/images/loader.gif" alt="Loading..." id="formLoad" style="margin: 10px 10px 10px 200px;" />');
				
				/* Init variables */
				var name 	= $("#formName").val();
				var mail	= $("#formMail").val();
				var website = $("#formWebsite").val();
				var text 	= $("#formText").val();
				//var captcha = $("#formCaptcha").val();
				
				/* Load form-function asynchron */
				
				$("#formReturn").load("ajax/contact.php", {send: true, name: name, mail: mail, website: website, text: text, /*captcha: captcha, */bestanden: bestanden, verwijderd: verwijderd},
					function () {
						if($("#formReturn").html() == "OK") {
							window.location = "bedankt.html";
						}
						else {
							$("#formReturn p.error").animate({ top:"-=10px" }, 100).animate({ top:"+=10px" }, 100).animate({ top:"-=10px" }, 100).animate({ top:"+=10px" }, 100);
								that.parent().find("#formLoad").remove();
								that.css({ display: "block" });
						}
					});
				
				return false;				
				
			
			});
	  
    $('#file_upload').uploadify({
        'uploader'        : 'js/uploadify/uploadify.swf',
        'script'          : 'ajax/uploadify.php',
        'cancelImg'       : 'js/uploadify/cancel.png',
		'folder'		  : 'files',
        'removeCompleted' : false,
        'multi'           : true,
		'buttonText'	  : 'Bladeren',
		'wmode'       	  : 'transparent',
		/*'hideButton'      : true,*/
		'buttonImg'		  : " ",
		'queueSizeLimit'  : 10,
		'height'  		  : 28,
		'width'  		  : 51,
		'auto'            : true, 
		'fileExt'     	  : '*.jpg; *.gif; *.png; *.wrd; *.ai; *.psd; *.pdf; *.eps',
  		'fileDesc'    	  : 'Bestanden'	,
		'onComplete'  	  : function(event, ID, fileObj, response, data) {
      							//document.getElementById("bestanden").value = document.getElementById("bestanden").value + ";" + fileObj.name; 
      							//bestanden.push(fileObj.name);
      							mapping[ID] = (fileObj.name);
      							aantalElementen++;


 						    },	
		'onCancel'  	  : function(event, ID, fileObj, data) {
      							verwijderdeIds.push(ID);
      							aantalElementen--;
 						    }	
      });
      
     var $buttonWrapper = $(".UploadifyButtonWrapper"); 
     var $objectWrapper = $(".UploadifyObjectWrapper"); 
     var $object = $("object", self); 
     var $fakeButton = $(".button", self); 
     var width = $fakeButton.outerWidth(); 
     var height = $fakeButton.outerHeight(); 
     $object.attr("width", width).attr("height", height); 
     $buttonWrapper.css("width", width + "px").css("height", height + "px") 
     $(".UploadifyObjectWrapper").hover(function() {     $(".button").addClass("hover"); }, function() {    $(".button").removeClass("hover"); }); 
	
}


/*function refreshCaptcha()
{
	var img = document.images['captchaimg'];
	img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}*/

jQuery(document).ready(function(){

	menu();
	slider();
	topnav();
	searchfield();
	hover_shadow();
	contactform();
			
});
