var newwindow = '';


$(document).ready(function() { 

	function searchButtonClick() {
		$('#searchHtml').html('<img src="/interface/ajax-loader-wit.gif" style="margin-top: 10px;margin-left: 100px;" /> ');
	
		var strSearchType = $(this).attr('rel');
		var strResultsTarget = $(this).attr('rev');
	
		switch(strSearchType)
		{
			case 'fastSearch':
				$('#fastSearch').removeClass('search-inactive').addClass('search-active');
				$('#extendedSearch').removeClass('search-active').addClass('search-inactive');
				$('#divroller_container').removeClass('hideme');
				$('#topbaantext').removeClass('hideme');
			break;
			case 'extendedSearch':
				$('#extendedSearch').removeClass('search-inactive').addClass('search-active');
				$('#fastSearch').removeClass('search-active').addClass('search-inactive');
				$('#divroller_container').addClass('hideme');
				$('#topbaantext').addClass('hideme');
			break;
		}
		
		fetchSearchBox(strSearchType,strResultsTarget);
	
		return false;
	}	
	
	$('#fastSearch').click(searchButtonClick);
	$('#extendedSearch').click(searchButtonClick);

	var $fastSearchButton = $('#fastSearch')	
	
	var strSearchType = $fastSearchButton.attr('rel');
	var strResultsTarget = $fastSearchButton.attr('rev');
	
	if ( strResultsTarget == undefined ) {
		strResultsTarget = 'left';
	}
	
	$.post('/ajax/removeAllSelectors.php',{},function (data) {
	});

	if ( $('#placevacatures').html() != null  ) {
		fastSearch(strResultsTarget,'extendedSearch')
	}

	if ( $('#placevacatures-intern').html() != null  ) {
		// add the "internal functions" selection
		var strType = 'function';	// "Function" drop down (sort of job function)
		var strValue = '12';	    // Function '12' = Internal Functions (internal job openings)
			
		$.post('/ajax/addSelector.php',{type: strType,value: strValue},function (data) {
		});
		
		fastSearch(strResultsTarget,'extendedSearch');
	}
	
	fetchSearchBox(strSearchType,strResultsTarget);
	
	function fetchSearchBox(searchType,strResultsTarget) {
		
		var fastSearchText = 'Trefwoord / functie / referentiecode';
	
		if(!searchType)
		{
			searchType = 'fastSearch';
		}	

		var strName = '';
		
		switch(searchType)
		{
			case 'fastSearch':			
				
				$.post('/ajax/fastsearch.php',{},function (data) {
			
					$('#searchHtml').html(data);
					
					
					$('#searchq').val(fastSearchText).focus(function () {
					
						if($(this).val()==fastSearchText)
						{
							$(this).val('');
						}
					
					}).blur(function () {
					
						if($(this).val()=="")
						{
							$(this).val(fastSearchText);
						}
						
					}).keyup(function () {
				
						if($(this).val()!=fastSearchText && $(this).val()!="")
						{
						
							fastSearch(strResultsTarget,$(this).val());
						}
					
					}).change(function () {
						
						if($(this).val()!=fastSearchText && $(this).val()!="")
						{						
							//fastSearch($(this).val());
						}
					
					});
				
				});
				
				
				
			break;
			case 'extendedSearch':
			
				var strSelected = '';
				var strName = '';
			
				$.post('/ajax/extendedsearch.php',{strSelected: strSelected,strName: strName},function (data) {
				
					$('#searchHtml').html(data);
					
					bindDropdowns(strResultsTarget);
					
					fastSearch(strResultsTarget,'extendedSearch');
				
				});
			break;
		}
			
	}
	
	function fastSearch(strResultsTarget,strSearch,intPage,loadPrevPage) {
		$.post('/ajax/search.php',{searchq: strSearch,page: intPage,loadprevpage: loadPrevPage},function (data) {
			document.getElementById(strResultsTarget).innerHTML=(data);
			bindPages(strResultsTarget,strSearch);
			bindJobs(strResultsTarget,strSearch);			
		});
	}
	
	function bindJobs(strResultsTarget,strSearch) {
	
		$('.openJob').click(function (event) {
			var titleTag = event.target.innerHTML;
			var jobId = $(this).attr('jobId');
			var href = $(this).attr('href');
			//alert(jobId);
			//$('#content-right').css("background","#FFFFFF").html('<img src="/interface/ajax-loader-wit.gif" style="margin-left: 346px;margin-top: 198px;" />');
			
			$.post('/ajax/vacature.php',{vacatureid: jobId,url: href},function (data) {
				document.title=titleTag;
				$('#'+strResultsTarget).html(data);
				bindJobs(strResultsTarget,strSearch);
			});
			
						
			return true;
		
		});
		
		$('.previouspage').click(function () {
			
			fastSearch(strResultsTarget,strSearch,'','ok');
			
			return false;
		
		});
		
	}
	
	
	function bindPages(strResultsTarget,strSearch) {
	
		$('.changePage').click(function () {

			var toPage = $(this).attr('page');
			
			fastSearch(strResultsTarget,strSearch,toPage);
			
			return false;
		
		});
	}
	
	function bindDropdowns(strResultsTarget) {
		
		/*
		 * Dropdowns fixen onclick word die geopend
		 */
		$('.dropdown').hover(function () {
			
			$(this).css("border","1px solid #d2d2d2");
			
			$(this).find('img').attr('src','/interface/dropdown_arrow_active.png');
			
			
		
		},function () {
		
			$(this).css("border","1px solid #d2d2d2");
			
			$(this).find('img').attr('src','/interface/dropdown_arrow_inactive.png');
		
		}).click(function () {
		
			$(this).find('img').attr('src','/interface/dropdown_arrow_active.png');
			
			
			var openDrop = $(this).attr('openDrop');
			
			var strDisplay = $('#' + openDrop).css("display");
			
			if(strDisplay=="block")
			{
				$('#' + openDrop).hide();
			}
			else 
			{		
				$('.dropdownitems:visible').hide();
				$('#' + openDrop).show();
			}
			
			
		});
		
		
		/* 
		 * clickable maken van checkboxes
		 */
		$('.selector').click(function () {

			var i = 0;
			
			var strType = $(this).attr('selecttype');
			var strValue = $(this).val();			
				
			if($(this).attr('checked'))
			{
				$.post('/ajax/addSelector.php',{type: strType,value: strValue},function (data) {
				
					//$('#debug').html(data);
					
					//alert(data);
				
				});
			}	
			else 
			{
				$.post('/ajax/removeSelector.php',{type: strType,value: strValue},function (data) {
				
					//$('#debug').html(data);
				
				});

			}
			
			fastSearch(strResultsTarget,'extendedSearch');
		
		});
		
		
		$('.subselect a.selectall').click(function () {
			
			$(this).parent().parent().find('.selector:checked').each(function () {
				
				var strValue = $(this).val();
				var strType = $(this).attr('selecttype');
				
				$(this).removeAttr('checked');
				
				$.post('/ajax/removeSelector.php',{type: strType,value: strValue},function (data) {
				
					//$('#debug').html(data);

					
				});
				
				
				fastSearch(strResultsTarget,'extendedSearch');
			});
			
			

			return false;
		
		});
		
		$('.subselect a.close').click(function () {
			
			$(this).parent().parent().hide();
			
			return false;
		})
		
	}
	
	$('.wrapper').click(function () {
	
		$('.dropdownitems:visible').hide();
	
	});		
	
	//alert(fastSearchText);

});

function popitup( url ) {
	var path = '/stuurdoor?page=' + url;

	if ( !newwindow.closed && newwindow.location ) {
		newwindow.location.href = path;
	} else {
		newwindow = window.open( path, 'stuurdoor', 'height=500, width=550, resizable=1, scrollbars=1' );
		if ( !newwindow.opener ) {
			newwindow.opener = self;
		}
	}
	if ( window.focus ) {
		newwindow.focus()
	}

	return false;
}

function bookmarksite( url, sitename ) {
	ns = "Netscape and FireFox users, use CTRL+D to bookmark this site.";

	if ( ( navigator.appName == 'Microsoft Internet Explorer' ) && ( parseInt( navigator.appVersion ) >= 4 ) ) {
		window.external.AddFavorite( url, sitename );
	} else if ( navigator.appName=='Netscape' ) {
		alert( ns );
	}
}

function printVacature( pv_intId ) {
	window.open( '/print/index.php?id=' + pv_intId, 'print', 'width=600, height=500, scrollbars=yes, toolbar=no, location=no' );
}

