$.fn.clearValue = function() {
	return this.focus(function() {
		if (this.value == this.defaultValue) {
			this.value = "";
		}
	}).blur(function() {
		if (!this.value.length) {
			this.value = this.defaultValue;
		}
	});
};

$.fn.extend({
	widthTruncate : function(options) {
		var defaults = {
			width : 'auto',
			after : '...'
		};

		var options = $.extend(defaults, options);

		return this.each(function() {
			if (options.width == 'auto') {
				truncateWidth = $(this).width() - 8;
			} else {
				truncateWidth = options.width
			}
			if ($(this).width() > truncateWidth) {
				var smaller_text = $(this).text();
				$(this).html(
						'<span id="truncateWrapper" style="display:inline;">'
								+ options.after + '</div>');
				i = 1;
				while ($('#truncateWrapper').width() < truncateWidth) {
					$('#truncateWrapper').html(
							smaller_text.substr(0, i) + options.after);
					i++;
				}
				$(this).html($('#truncateWrapper').html());
			}
		});
	}
});


/* ** RemoteToggle ** */
function ToggleByRemote(){
	
	/* Clear */
	$(".remoteToggle").each(function(i){
		if (!$(this).is(":checked")) {
			$("." + this.id).hide();
		}
	});
	
	/* Select */
	$(".remoteToggle").change(function(){
	
		var nameId = $(this).attr("name");
		
		if ($(this).is(":checked")) {
			$(".remoteToggle").each(function(i){
				if ($(this).attr("name") == nameId) {
					$("." + this.id).hide();
				}
			});
			$("." + this.id).show();
			
		}
		else {
			$("." + this.id).hide();
		}
	});
}


function updateBerechtigungen(orgnisationId, professionId, current) {
	var id = $("#" + orgnisationId).val();
	$.ajax({
		url : "/organisation/eligibilities/" + id + ".json",
		success : function(data) {
			var options = '';
			$.each(data, function(key, val) {
				if (val.id == current){
					options += '<option value="' + val.id + '" selected="selected">' + val.name + '</option>';
				} else {
					options += '<option value="' + val.id +'">' + val.name + '</option>';
				}
			});
			$("#" + professionId).html(options);
		}
	});
}

/**
 * Holt sich per AJAX Berufe zu einer Berufsgruppe und setzt die entsprechende
 * Select-Box.
 * 
 * @param professionGroupSelectId
 *            Die ID der Berufsgruppen-Select-Box
 * @param selectId
 *            Die ID der Berufs-Select-Box
 */
function setProfessions(professionGroupSelectId, selectId, current) {
	var id = $("#" + professionGroupSelectId).val();
	$.ajax({
		url : "/applicant/profile/professions/" + id + ".json",
		success : function(data) {
			var options = '';
			var json = jQuery.parseJSON(data);
			$.each(json, function(key, val) {
				if (val.id == current){
					options += '<option value="' + val.id + '" selected="selected">' + val.name + '</option>';
				} else {
					if (val==null || val.id=='null' || val.id==null )
						options += '<option value="">' + val.name + '</option>';
					else
						options += '<option value="' + val.id + '">' + val.name + '</option>';
				}
			});
			$("#" + selectId).html(options);
			$("#" + selectId).trigger("liszt:updated");
		}
	});
}

/* ** Watchlist ** */
function refreshWatchlist(offerId) {
	var offerUrl = '/joboffers/' + offerId + '/details.json?remember=this';
	$.post(offerUrl, function(data) {
		setWatchlist(data);
	});
}

function watchProfession(profId) {
	var profUrl = '/profession/' + profId + '/view.json?remember=this';
	$.post(profUrl, function(data) {
		setWatchlist(data);
	});
}


function initWatchlist(watchlist) {
	$.getJSON(watchlist,  function(data) {	
		setWatchlist(data);
	});
}

function setWatchlist (data) {
	$('#watchCount').html(data.count);
	if (data.offers != null){
		$.each(data.offers, function() {
			$("#o"+this).attr("disabled", true).parent().removeClass("enable").addClass("disable");
		});
		$.each(data.professions, function() {
			$("#p"+this).attr("disabled", true).parent().removeClass("enable").addClass("disable");
		});
	};
	
	if (data.username != null) {
		$('#accountText').html("Konto: <a href='/profile.html' title='" + data.email + "'>" + data.username + "</a>");
		$('#accountLink').attr("href", "/logout.html");
		$('#accountLink').html("<span>abmelden</span>");
	} 
}

$(document).ready(function() {

	/* ******* Toggle-Functions ******* */
	
	ToggleByRemote();
	
	
	// **************Plugin Aufrufe*********************/
	// style input fields
	$('form.formYellow').jqTransform({
		imgPath : '../images/forms/'
	});

	// style Scrollbar
	$('.scrollList').jScrollPane({
		showArrows : true
	});

	// clear default value from input on focus
	$(".clearValue input[type='text']").clearValue();
	$(".clearValue input[type='password']").clearValue();
	
	// select default value from input on click
	$(".selectValue input[type='text']").click(function(){
	    $(this).select();
	});

	$(".topTenBox a.boxContentChange").click(function() {

		if ($(this).parents(".selection_1").length) {
			$(".selection_1").hide();
			$(".selection_2").show();
		}
		;

		if ($(this).parents(".selection_2").length) {
			$(".selection_2").hide();
			$(".selection_1").show();
		}
		;
	})
	
	// PickList
	$(".pickListSelected select").multiSelect(".pickListAvailable select", {
		autoSort: false,
		sortOptions: false,
		trigger: "button[name='remove']",
		triggerAll: "button[name='removeAll']"
		});

	$(".pickListAvailable select").multiSelect(".pickListSelected select", {
		autoSort: false,
		sortOptions: false,
		trigger: "button[name='add']",
		triggerAll: "button[name='addAll']"
		});

	// Chosen
	$(".autocomplete-select").chosen();
	
	// Datepicker
	$( ".datepicker_future" ).datepicker({
		changeMonth: true,
		changeYear: true,
		altField: "#actualDate",
		showOn: "both",
		buttonImage: "/images/icon_date-picker.gif",
		buttonImageOnly: true,
		dateFormat: "dd.mm.yy",
		constrainInput: true,
		yearRange: "2010:2050"
	});
	
	// **************Funktionen*********************/
	// Navigation
	$("#main-nav li").hover(function() {
		$(this).children("ul").css({
			left : "0px"
		});
	}, function() {
		$(this).children("ul").css({
			left : "-5000px"
		});
		$(this).children("a").removeClass("active");
		$(this).removeClass("active");
	});

	$("#main-nav li a").focus(function() {
		$(this).parent().children("ul").css({
			left : "0px",
			display : "block"
		});
		$(this).parent().children("a").addClass("active");
		$(this).parent().addClass("active");
	});

	$("#main-nav li a").blur(function() {
		$(this).parent().children("ul").css({
			left : "-5000px"
		});
		$(this).parent().removeClass("active");
		$(this).removeClass("active");
	});

	$("#main-nav li ul li a").focus(function() {
		$(this).parent().parent().parent().children("ul").css({
			left : "0px",
			display : "block"
		});
		$(this).parent().parent().parent().children("a").addClass("active");
		$(this).parent().parent().parent().addClass("active");
	});

	$("#main-nav li ul li a").blur(function() {
		$(this).parent().parent().parent().children("ul").css({
			left : "-5000px"
		});
		$(this).parent().parent().parent().children("a").removeClass("active");
		$(this).parent().parent().parent().removeClass("active");
	});

	// TabbedContainer
	function setTabbedHeight() {
		var currentHeight = $(".tabbedContainer.active").find(".tabbedContent").height();
		if (currentHeight > 500 ){
			$(".main_content").height(currentHeight + 40);
		}
	}
	
	
	  
	  if ($(".tabbedContainer").length){
		if ($(".tabbedContainer.active").length == 0){
			$(".tabbedContainer:first").addClass("active");	
		}
		$(".tabbedContainer.active").find(".trustinfo").attr("src", "/data/trustme.jpg?" + new Date().getTime());
		//setTabbedHeight();

		
		$(".tabbedContainer .tabbedHead").click(function(){
			$(".tabbedContainer.active").removeClass("active");
			$(this).parent(".tabbedContainer").addClass("active");
			$(this).parent(".tabbedContainer").find(".trustinfo").attr("src", "/data/trustme.jpg?" + new Date().getTime());
			//setTabbedHeight();
		});
	}
	
	
	
	
	// Captcha Refresh
	$(".captchaRefresh").click(function() {
		$(this).parent("div").find(".trustinfo").attr("src", "/data/trustme.jpg?" + new Date().getTime());
		return false;
	});
	
	
	
	
});


