var welkFormulier = 381;

$(function() {

	//RPa 12-09-2011: Maatwerk vacaturebank Servex, [OpStationSelect] op reactie formulier 
	//alert( "document.location.href.toLowerCase()=" + document.location.href.toLowerCase() );
	if ( document.location.href.toLowerCase().indexOf("formgenb_module.asp")>0 && typeof(vitemid) != "undefined" ) {
		//alert( '$("#formgen_form").size()=' + $("#formgen_form").size() );
		if ( $("#formgen_form").size()==1 ) {
			if ( document.location.hash && document.location.hash!='#' ) {
				//Voor formule-hash in action zetten 
				$("#formgen_form").attr("action", $("#formgen_form").attr("action")+document.location.hash );
			}
			var jqOpStationSelectElement = $("#formgen_form td:contains('[OpStationSelect]'):last");
			//alert( "jqOpStationSelectElement.size()=" + jqOpStationSelectElement.size() );
			if ( jqOpStationSelectElement.size()==1 ) {
				//alert( "rpa2=" + jqOpStationSelectElement.html() + " vitemid=" + vitemid );
				//Haal gekoppelde op-station-categorieen.
				var sDataUrl = "/" + sInitCustomerDir + "/ceData.asp"
					+ "?CustID=" + sInitCustomerID
					+ "&ComID=" + sInitCommunityID
					+ "&ModID=" + vmodid
					+ "&ItemID=" + vitemid
					+ "&SessionID=" + sInitSessionID
					+ "&Time=" + sInitTime
					+ "&Action=ItemBankCCategorieOpStation"
					+ "&StyleID=1067315"
					+ "&Output=JSON"
				;
				//alert( "sDataUrl=" + sDataUrl );
				$.getJSON(
					sDataUrl
				,	function ( jsonData ) {
						var sOpStationsHtml = "";
						//alert( typeof(jsonData) );
						//alert( jqOpStationSelectElement.html() );
						if ( !jsonData ) {
							//alert("-");
							jqOpStationSelectElement.find( "input:first" ).attr( "value", "-" ).attr("readonly","readonly");
						} else {
							if ( jsonData.length==1 ) {
								//alert( jsonData[0].CatNaam );
								jqOpStationSelectElement.find( "input:first" ).attr( "value", jsonData[0].CatNaam ).attr("readonly","readonly");
							}
							if ( jsonData.length>1 ) {
								sOpStationsHtml = "<select name='OpStation' id='OpStation'  multiple='true' onchange='opStationChange(this)' style='width:60%;'>\n<option value='-'>Selecteer station</option>";
								for ( var i=0; i<jsonData.length; i++) {
									sOpStationsHtml += "<option value='" + jsonData[i].CatNaam + "'>" + jsonData[i].CatNaam + "</option>\n";
								}
								sOpStationsHtml += "</select>";
								//alert( "sOpStationsHtml=" + sOpStationsHtml );
								jqOpStationSelectElement
									.find( "input:first" )
									.css({"width":"0px","margin":"0px","padding":"0px","padding-bottom":"12px"})
									.focus(
										function(){
											//alert("focus");
											$("#OpStation").focus();
										}
									)
								;
							}
						}
						var sHtml = jqOpStationSelectElement.html();
						sHtml = sHtml.replace("[OpStationSelect]",sOpStationsHtml);
						jqOpStationSelectElement.html( sHtml );
						$("#OpStation").chosen();
						$("#OpStation_chzn").css("margin-top","5px");
					}
				);
			}
		}
	}

	$('body').addClass('js-on');  
	
	$("input").focus(function () {
	if ($(this).val() == $(this).attr("title")){
		$(this).val("");
	}
	});
	$("input").blur(function () {
		if ($(this).val() == ""){
			$(this).val($(this).attr("title"));
		}
	});

	$(".postzegel").each(function(){
		var sOnclick=this.onclick.toString();
		sOnclick = sOnclick.substring( sOnclick.indexOf(", '")+3 );
		sOnclick = sOnclick.substring( 0, sOnclick.indexOf("'") );
		$(this).attr("onclick","$.fancybox('" + sOnclick + "',{'type':'image'});");
	});
	
	$("a#open-login").fancybox({
		'scrolling'	:	'no'
	});
	
	/* fotoboek */
	$(".fancybox-view-01 ul a.photo-link").fancybox();
	
	$(".fancybox-view-02 ul a").fancybox({
			titlePosition: 'over'
	});
	
	if($("table.maintenanceButtons").length > 0) {
		$("span.delete").css('display','block');
		$("span.edit").css('display','block');
		$("a.add-item").css('display','block');
	}

	/* smoelenboek */
	$(".smoelenboek a").fancybox();
	

	/* nieuws slide */
	$(".news .item h3").click(function () { 
		$(this).next('a').slideToggle("slow").next('div').slideToggle("slow");
	});
   
	$(".uitklappen").click(function () { 
		$(".item div").slideDown("slow");
		$(".item a").slideUp("slow");
		
		/* Alleen voor itembank slide */
		if($(".itembank-slide").length > 0){
			$(".item .btn-delete").slideDown("slow");
		}
	});
   
	$(".inklappen").click(function () { 
		$(".item div").slideUp("slow");
		$(".item a").slideDown("slow");
		
		/* Alleen voor itembank slide */
		if($(".itembank-slide").length > 0){
			$(".item .btn-delete").slideUp("slow");
		}
	});
	
	/* faq slide */
	$(".faq .item h3").click(function () { 
		$(this).next('div').slideToggle("slow");
	});
	
	/*itembank slide*/
	$(".itembank-slide .item h3").click(function () { 
		$(this).next('div').slideToggle("slow");
	});
	
	
	/*itembank pop-up*/
	$("a.close-button").click(function() {
		$(".item-container").slideUp("fast");
	});
	$('a.more-info').click(function() {
		$(".item-container").slideDown("fast");
		$(".item-container .clone-text").remove();
		$(this).next().clone().removeClass().addClass("clone-text").insertAfter(".close-button");
	});

	
	/* Poll */
	$("#PollItem").attr("allowTransparency",true).after("<div class='poll-container rounded' id='poll-container'></div>");
	$("#PollItem").appendTo($("#poll-container"));

	/* catch IE console.log error */
	if (!window.console) {
		console = { log : function(){} };
	}
	
	var errorCount = 0;
	$('.carousel img').error(function() {
		console.log("er is nog geen afbeelding gekoppeld");
		// imgUrl wordt in styleframeset gezet;
		if(errorCount == 0){
			errorCount++;
			$('.carousel img').attr('src', imgDefault).show();
		}
	});
	if ( typeof(imgUrl) != 'undefined' ){
		$('.carousel img').attr('src', imgUrl).show();
	}
	
	
	$(".arrowed").before('<div class="arrow-right" />');
	$(".arrowed-in, #primary-sub-level a").prepend('<div class="arrow-right" />');
	
	//remove specific submenu's
	$('#primary>li:eq(1)>ul, #secondary>li:eq(1)>ul').remove();
	
	$('iframe[src*="youtube"]').each(function(){
		var oriURL = $(this).attr('src');
		var oriSrc = $(this).attr('src');
		oriSrc = oriSrc.split('/');
		oriSrc = oriSrc[oriSrc.length-1];
		//alert(oriSrc);
		$(this).after('<img class="fY" src="http://img.youtube.com/vi/' + oriSrc + '/0.jpg" width="200" rel="' + oriURL + '">');
		$(this).remove();
	});
	
	$('.fY').each(function(){
		$(this).click(function(){
			$.fancybox(
				$(this).attr('rel')
			,	{
					'type':'iframe'
				,	'height':400
				,	'width':400
				,	'autoDimensions':false
				,	'hideOnOverlayClick':false
				}
			);
		});
	});
});

function opStationChange( oSelect ) {
	//alert( $(oSelect).val() );
	$(oSelect).parent().find("input:first").val( $(oSelect).val() );
}

/*smoelenboek*/
/* mijn functie om de hoogte van het kaartje te zetten naar de hoogte van het hoogste kaartje */                                                               
$.fn.hoooogte = function() {
	var h = 0;
	$(this).each(function(){
		if ($(this).height() > h) { 
			h = $(this).height(); 
		}
	});
	$(this).each(function(){
		$(this).css({'height': h});
	});
};

function showVacatureItemForm( iVacatureComID, iVacatureModuleID, iVacatureItemID ) {
	//alert( 'showVacatureItemForm( dit moet in fancybox openen: ' + iVacatureComID + ',' + iVacatureModuleID +',' + iVacatureItemID + ')' );
	//alert( 'comid=1&modid=381&vcomid=' + iVacatureComID + '&vmodid=' + iVacatureModuleID + '&vitemid=' + iVacatureItemID + '&sessionid=' + sInitSessionID);
	
	$.fancybox(
		'/servex_c01?comid=1&modid=' + welkFormulier + '&vcomid=' + iVacatureComID + '&vmodid=' + iVacatureModuleID + '&vitemid=' + iVacatureItemID + '&sessionid=' + sInitSessionID + '&Geenframeset=1'
	,	{
			'type':'iframe'
		,	'height':600
		,	'width':600
		,	'autoDimensions':false
		,	'hideOnOverlayClick':false
		}
	);
}

function fancyYoutube(sUrl) {
	$.fancybox(
		sUrl
	,	{
			'type':'iframe'
		,	'height':400
		,	'width':400
		,	'autoDimensions':false
		,	'hideOnOverlayClick':false
		}
	);
}

