$(document).ready(function() {
    
    // Alle links met rel="external" in een nieuw venster openen
    $("a[rel=external]").each(function(i){
        this.target="_blank";
    });
    if($('.selectbox').length > 0 ) {
    	$('.selectbox').selectbox();
    }
    
    $('#swis_check_field_comment').val('swis_check_ok');
    
    $('a[rel=linkedin]').popupWindow({ 
		height:500, 
		width:800, 
		top:50, 
		left:50 
	}).click(function(){
        var isiPad = navigator && navigator.platform && (navigator.userAgent.match(/^(iPad|iPod|iPhone)$/) || navigator.userAgent.match(/iPad/i) != null);
        
        if (isiPad) {
            $('#linkedin-close-notice').css('display', 'block');
        }
    }); 
	
	if($(".colorboxMijnCv").length > 0 )
	$(".colorboxMijnCv").colorbox({width:"700px", inline:true, href:"#inline_mijncv"});
   
	$("#slider").easySlider();
    
    $('#linkedin-close-notice').click(function() {
        
    });
	
    initVideo();
	addOptioneel();
	initTextFields();
    initSubmitbutton();
    initUitbreidingDropdown();
    initStappenSubmit();
    initClearOnFocus();
    initPrint();
    initLinkBlok();
    initRemoveImageLink();
    initNieuwscategorieen();
    
});


/**
* Nieuws-categorieen switchen
**/
function initNieuwscategorieen() {
    $('#nieuws-categorieen').change(function() {        
        if (this.value == '') {
            window.location = Website.Config.sPathNieuwsFullList;
        } else {
            window.location = Website.Config.sPathNieuwsFullList + '/categorie/' + this.value;   
        }
    });
}


/**
* Video
**/
function initVideo() {   
	$(document.body).append('<div id=\'video-content\'></div>');
	
	var isiPad = navigator && navigator.platform && (navigator.userAgent.match(/^(iPad|iPod|iPhone)$/) || navigator.userAgent.match(/iPad/i) != null);
	
     setVideo = function(id) {  
        var video;
        
        video = Website.Config.Video[id];
        
         
        
        if (!isiPad && video.video_mp4 != "" ) {
//        	$('#video-content').html("<div id='mediaplayer'></div> <script type='text/javascript'> var so = new SWFObject('js/jwplayer/player.swf','playerID','475','325','9'); so.addParam('allowfullscreen','true'); so.addParam('allowscriptaccess','always'); so.addVariable('autostart','true'); so.addVariable('file', '"+video.video_mp4+"'); so.write('mediaplayer'); </script>");    
        	$('#video-content').html("<div id='mediaplayer'></div> <script type='text/javascript'> var so = new SWFObject('js/jwplayer/player.swf?file="+video.video_mp4+"&autostart=true' ,'playerID','475','325','9'); so.addParam('allowfullscreen','true'); so.addParam('allowscriptaccess','always'); so.addVariable('autostart','true'); so.addVariable('file', '"+video.video_mp4+"'); so.write('mediaplayer'); </script>");    
		}else if(!isiPad && video.video_flv != "" ) {
			$('#video-content').html("<div id='mediaplayer'></div> <script type='text/javascript'> var so = new SWFObject('js/jwplayer/player.swf','playerID','475','325','9'); so.addParam('allowfullscreen','true'); so.addParam('allowscriptaccess','always'); so.addVariable('autostart','true'); so.addVariable('file', '"+video.video_flv+"'); so.write('mediaplayer'); </script>");
        } else { 
        	if(video.video_mp4 != '') {
        		$('#video-content').html(" <video id='player2' width='475' height='325' poster='"+video.thumbnail+"' controls> <source src='"+video.video_mp4+"' type='video/mp4' /> </video>");
        	} else {
				$('#video-content').html("Er is geen video beschikbaar voor de iPad/iPhone.<br>Excuses voor het ongemak.");
        	}
        }
	 }
	 
	 clearVideo = function() {
		  $('#video-content').html("");
	 }
    
     options = { width:"520px", 
                height:"400px", 
                inline:true, 
                href: "#video-content",
                onComplete: function(){setVideo(this.id.substr(7))},
                onClosed: function(){clearVideo(); }
                };
    
    
    
    
    
    $("a.video-box").colorbox(options);
}


/**
* Print-button
**/
function initPrint() {    
    $('a.printWindow').click(function(e) {
         window.print();
         e.preventDefault();
    });        
}

/**
* Input clear on focus 
**/
function initClearOnFocus() {
    // clear input on focus
    $('.clearOnFocus').focus(function() {
        if($(this).val() == $(this).attr('title')) {
            $(this).val('');
        }
    });

    // if field is empty afterward, add text again
    $('.clearOnFocus').blur(function() {
        if($(this).val() == '') {            
            $(this).val($(this).attr('title'));
        }
    });
    
    $('.clearOnFocus').each(function() {
		$(this).parents('form').attr('onsubmit','removeTrefwoord()');
		$(this).parents('form').submit(function(){removeTrefwoord()});
    });
}

function removeTrefwoord() {
	$('.clearOnFocus').each(function() {
        if($(this).val() == $(this).attr('title')) {
            $(this).val('');
        }
    });
}

function initStappenSubmit() {
	$('div.stappen .stap a').click(function() {
		if($('#laatstestap').attr('rel') != 'true') {
			$('input[name="redirect"]').val($(this).attr("href"));
			removeOptioneel();
			$('#TalentAanmelden').submit();
			return false;
		} else {
			window.location= Website.Config.sBaseUrl+'talenten/inschrijven/stap/'+$(this).attr("href");
			return false;
		}
	});
}

function initUitbreidingDropdown() {
	 $('.uitbreiding').each(function() {
		$(this).change(function () {
			if($(this).val() == $(this).attr('rel')) {
				$('.uitbreidingtext[rel='+$(this).attr('name')+']').parent().parent().css('display','');
			} else {
				$('.uitbreidingtext[rel='+$(this).attr('name')+']').parent().parent().css('display','none');
			}
		});
    });
    
    uitbreidingFirstInit();
}

function uitbreidingFirstInit() {
	$('.uitbreiding').each(function() {
		if($(this).val() == $(this).attr('rel')) {
			$('.uitbreidingtext[rel='+$(this).attr('name')+']').parent().parent().css('display','');
		} else {
			$('.uitbreidingtext[rel='+$(this).attr('name')+']').parent().parent().css('display','none');
		}
    });
}

function removeOptioneel() {
	$('.inschrijvenTable input[type="text"]').each(function() {
		if($(this).val() == 'Optioneel') {
			$(this).val('');
		}
	});
}

function addOptioneel() {
	$('.inschrijvenTable input[type="text"]').each(function() {
		if($(this).val() == '' && $(this).attr('name') != 'achternaam' && $(this).attr('name') != 'voornaam' && $(this).attr('name') != 'email' && $(this).attr('name') != 'email2' ) {
			$(this).val('Optioneel');
		}
	});
}

function initTextFields() {
	$('input[type="text"]').each(function() {
		if($(this).val() == 'Optioneel') {
			$(this).addClass('idleOptioneel');
		}
	});
	
	$('input[type="text"],textarea,input[type="password"]').addClass("idleField");
    $('input[type="text"],textarea,input[type="password"]').focus(function() {
       	$(this).removeClass("idleField").removeClass('idleOptioneel').addClass("focusField");
    	if (this.value == 'Optioneel'){ 
    		this.value = '';
		}
		if(this.value != 'Optioneel'){
	    	/*this.select();*/
	    }
    });
    $('input[type="text"],textarea,input[type="password"]').blur(function() {
    	$(this).removeClass("focusField").addClass("idleField");
    });
}

function initSubmitbutton() {
	// Submit knop functies
    $('.submitButton').parent('button').mousedown(function() {
		$(this).css('background-image','url("images/button-background-pressed.jpg")');
	});
	$('.submitButton').parent('button').mouseup(function() {
		$(this).css('background-image','url("images/button-background.jpg")');
	});
	$('.submitButton').parent('button').mouseout(function() {
		$(this).css('background-image','url("images/button-background.jpg")');
	});
	$('.submitButton').parent('button').click(function() {
		removeOptioneel();
		$('.backHidden').val('0');
		var form = $(this).parents('form:first');
		form.submit();
	});
	
	$('.backButton').parent('button').click(function() {
		if(!$(this).hasClass('buttonLink')) {
			removeOptioneel();
			$('.backHidden').val('1');
			var form = $(this).parents('form:first');
			form.submit();
		} else {
			window.location=$(this).attr('rel');
			return false;
			
		}
	});
	
	if($('#laatstestap').attr('rel') == 'true') {
		$('.backButton').click(function() {
			window.location=Website.Config.sBaseUrl+'talenten/inschrijven/stap/'+$('.stappen').children('.done:last').children('a').attr('href');
			return false;
		});
		
	}
}

function initTabs() {
	tabContainers = $('div.tabpages > div');
	formulierHtml = $('.inschrijvenTable').html();
	$('.inschrijvenTable').html('');
	$('#nr1').html("<table class='inschrijvenTable'>"+formulierHtml+"</table>");
	initTextFields();
	tabContainers.hide().filter(':first').show();
	initTabClicks();
	$('div.tabs ul.tabNavigation a').filter(':first').click();
	$('div.tabs ul.tabDeletation a').click(function () {
		var activeTab = $('div.tabs ul.tabNavigation .active');
		var tabNr = activeTab.attr('href');
		var tabs = $('div.tabs ul.tabNavigation li').length;
		if(tabs > 4) {
			$('div.tabpages '+activeTab.attr('href')).remove();
			activeTab.parent().remove();
			
			if(tabs < 14 && $('div.tabs ul.tabNavigation li .add').html() != '+') {
				$("div.tabs ul.tabNavigation").append("<li><a href='#extra' class='noDeco add'>+</a></li>");
				bExtra = true;
				initTabClicks();
			}
		} else {
			alert('De eerste tab kan je niet verwijderen.');
		}
		$('div.tabs ul.tabNavigation a').filter(':first').click();
		return false;
	});
	return false;
}

function initTabClicks() {
	$('div.tabs ul.tabNavigation a').click(function () {
		bFilled = true;
		if(bExtra != true) {
			var bExtra = false;
		}
		 
		if($(this).attr('hash') == '#extra' && bExtra != true) {
			
		    var iId = tabContainers.filter(':last').attr('id');
		    $('#'+iId+' input, #'+iId+' select, #'+iId+' textarea').each(function() {
				if($(this).val() == 'Optioneel' || $(this).val() == '') {
					bFilled = false;
				}
				if($(this).attr('type') == 'radio') {
					bRadio = false;
					$('input[name="'+$(this).attr('name')+'"]').each(function() {
						if($(this).attr('checked') == true) {
							bRadio = true;
						}
					});
					if(bFilled == true) {
						bFilled = bRadio;
					}
				}
		    });
		    if(bFilled == true || $(this).attr('rel') == 'init') {
		    	$('#'+iId+' input[type="radio"]').each(function() {
		    		if($(this).attr('type') == 'radio') {
						$(this).attr('name',$(this).attr('name').substr(0,($(this).attr('name').length-2))+'['+(teller-1)+']');
						var iIdInput = $(this).attr('id').substr(0,($(this).attr('id').length-4))+'['+(teller-1)+']'+$(this).attr('id').substr(($(this).attr('id').length-2),2)
						$('label[for='+$(this).attr('id')+']').attr('for', iIdInput);
						$(this).attr('id', iIdInput);
					}
				});
				
				 /*if(($('div.tabs ul.tabNavigation li').length == 4 && teller > 2) || $('div.tabs ul.tabNavigation li').length > 4 )*/
				
				if($('div.tabs ul.tabNavigation li').length == 4 && $('div.tabs ul.tabNavigation li a:first').attr('href') == '#nr1') {
					teller = 2;
			    } else {
		    		teller = teller+1;	
			    }
			    
			    //$('a[hash="#extra"]').attr('href','#nr'+teller).html(teller).removeClass('add');
				$.each($("ul.tabNavigation a"), function(i, object) {
					if(object.hash == "#extra") {
						$(this).attr('href','#nr'+teller).html(teller).removeClass('add');
					}					
				});			    
			    //$('a[href="#extra"]').attr('href','#nr'+teller).html(teller).removeClass('add');
			    /*$(this).attr('href','#nr'+teller).html(teller).removeClass('add');*/
			
				$('.tabpages').append("<div id='nr"+teller+"'><table class='inschrijvenTable'>"+formulierHtml+"</table></div>");
				tabContainers = $('div.tabpages > div');
				initTextFields();
				if($('div.tabs ul.tabNavigation li').length < 23) {
					$("div.tabs ul.tabNavigation").append("<li><a href='#extra' class='noDeco add' rel='"+$(this).attr('rel')+"'>+</a></li>");
					bExtra = true;
				}
				$('#nr'+teller+' input[type=text], #nr'+teller+' textarea, #nr'+teller+' select').val('');
				$('#nr'+teller+' input[type=radio]').attr('checked','');
			} else {
				alert('De laatste tab is niet totaal ingevuld.');
			}
		}
		
		if(bFilled == true || $(this).attr('rel') == 'init') {
		    tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('div.tabs ul.tabNavigation a').removeClass('active');
			$('a[href="'+$(this).attr('href')+'"]').addClass('active');
			
			if(bExtra == true) {
				initTabClicks();
			}
		}
		return false;
	});
	return false;
}

function initLinkBlok() {
	$('.linkBlok').each(function() {
		$(this).click(function() {
			window.location=Website.Config.sBaseUrl+$(this).find('a:first').attr('href');
		});
	});
	
	$('.linkBlokIgnore').click(function(e) {
		e.stopPropagation();
	}) 
}

function initRemoveImageLink() {
	$('.removeImageLink').click(function() {
		$('input[name="'+$(this).attr('id')+'"]').remove();
		$(this).closest('form').append('<input type="hidden" name="'+$(this).attr('id')+'">');
		$('input[name="'+$(this).attr('id')+'"]').attr('value','default');
		$(this).closest('form').submit();
		return false;
	});
}

