$(document).ready(function() {
	general.init();
	forms.init();
	search.init();
	profile.init();
	profileedit.init();

	//alert($('#email').length);

	if ($('#email').length > 0) {
		$('#email').focus(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});

		$('#email').blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});
	}

	if ($('#search-keyword').length > 0) {
		$('#search-keyword').focus(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});

		$('#search-keyword').blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});
	}


	$('#btn-contrast').click(function(e) {
		if($(this).attr('rel') == 'contrast') {
			// Switch to high contrast
			styleSwitcher.toggleContrast('high');
			$(this).attr('rel', '').text('Hoog contrast uit');
		}
		else {
			styleSwitcher.toggleContrast('normal');
			$(this).attr('rel', 'contrast').text('Hoog contrast');
		}
		e.preventDefault();
	});

	//id of element to check for and insert control
	TextResizeDetector.TARGET_ELEMENT_ID = 'header';
	//function to call once TextResizeDetector has init'd
	TextResizeDetector.USER_INIT_FUNC = fontResize.init();

	styleSwitcher.init();
});

var general = {
	init: function() {

	}
}

var fontResize = {
	init: function() {
		var iBase = TextResizeDetector.addEventListener(this.onFontResize,null);

	},

	onFontResize: function(e,args)
	{
		var header = document.getElementById('header');
		if(header){
			// if the font is larger than 20 pixels, attach the accessible stylesheet
			if((args[0].iSize)>=19) {
				styleSwitcher.toggleContrast('high');
				//styleSwitcher.toggleAccessible(false);

				styleSwitcher.toggleAccessible('yes');
			}
			else {
				styleSwitcher.toggleAccessible('no');
			}
		}
	}

}



/**
 * Switch CSS stylesheet
 * Depends on jquery.cookie.js
 *
 * @author Davy De Pauw
 */

var styleSwitcher = {
	init: function() {
		if ($.cookie('accessible') == 'yes') this.toggleAccessible($.cookie('accessible'))
		else this.toggleAccessible('no');

		if($.cookie('contrast') != '') this.toggleContrast($.cookie('contrast'));
		else {
			this.toggleContrast('normal');
			$('#btn-contrast').attr('rel', '').text('Hoog contrast uit');
		}
	},

	toggleAccessible: function(state) {
		if(state == 'yes')
		{
			$('link[title=accessible]').attr('disabled', false);
			$.cookie('accessible', 'yes', { expires: 365, path:'/', secure: false });
		}
		else
		{
			$('link[title=accessible]').attr('disabled', true);
			$.cookie('accessible', 'no', { expires: 365, path:'/', secure: false });
		}
	},

	toggleContrast: function(state)
	{
		if(state == 'high')
		{
			$('link[title=contrast]').attr('disabled', false);
			$.cookie('contrast', 'high', { expires: 365, path:'/', secure: false });
		}
		else {
			$('link[title=contrast]').attr('disabled', true);
			$.cookie('contrast', 'normal', { expires: 365, path:'/', secure: false });
		}
	}
}

var forms = {
	init: function() {
		$('.field input').focus(function(e) {
			$(e.target).parent().addClass('focus');
		});

		$('.field input').blur(function(e) {
			$(e.target).parent().removeClass('focus');
		});

		$('.textarea textarea').focus(function(e) {
			$(e.target).parent().parent().addClass('focus');
		});

		$('.textarea textarea').blur(function(e) {
			$(e.target).parent().parent().removeClass('focus');
		});

		$('select.list').focus(function(e) {
			$(e.target).addClass('focus');
		});

		$('select.list').blur(function(e) {
			$(e.target).removeClass('focus');
		});

		$('label.required').append('<abbr title="Verplicht veld">*</abbr>');

		$("#sidebar #newsletter input[type='submit']").click(function()
		{
			if ($("#email").val() == $("#email").attr('title')) {
				$("#email").val('');
			}
		});

		$("#register .policy a").click(function()
		{
			window.open($(this).attr('href'));
			return false;
		});
	}
}

var search = {
	init: function() {
		if ($('#search #q').length > 0) {
			$('#search #q').focus(function() {
				if ($(this).val() == $(this).attr('title')) {
					$(this).val('');
				}
			});

			$('#search #q').blur(function() {
				if ($(this).val() == '') {
					$(this).val($(this).attr('title'));
				}
			});
		}
	}
}

var profile = {
	init: function() {
		$('#networkall li').mouseover(function() {
			$(this).find('.delete').removeClass('hide');
		});
		
		$('#networkall li').mouseout(function() {
			$(this).find('.delete').addClass('hide');
		});
	}
}

var profileedit = {
	init: function() {
		$('#asset_id').change(function() {
			var val = $(this).val();
			$('#fakeinput').attr('value', val);
		});

        // set initial state based on current profile data
        if($('#job_sector').val()) this.update();

		$('#job_sector').change(function() {
			profileedit.update();
		});
	},

    update: function() {

            var val = $('#job_sector').val();

			//sector
			if (val == 'profit') {
				$('.sector').show();
			}
			else {
				$('.sector').hide();
			}

			//branchdetail
			if (val == 'government') {
				$('.job-sectordetail').show();
				$('.job-sectordetail').addClass('sep');
			}
			else {
				$('.job-sectordetail').hide();
				$('.job-sectordetail').removeClass('sep');
			}

			//company
			if (val == 'profit' || val == 'non-profit') {
				$('.company').show();
				if (val == 'profit') {
					$('.company label').text('Naam bedrijf');
				}
				else {
					$('.company label').text('Naam organisatie');
				}
			}
			else {
				$('.company').hide();
			}

			if (val == 'residential') {
				$('.job-sector').addClass('sep');
			}
			else {
				$('.job-sector').removeClass('sep');
			 }
    }
}
