﻿$(document).ready(function() {
	
  initStepstoneJobs();
	//initArbeitenDEJobs();

	/* Bugfix fuer IEx bei Hintergrund-Bildern */
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch (e) {}
	
	/* Hack, der jedes erste Bild pro Artikel beseitigt
	 zu loeschen wenn Artikel ueberarbeitet wurden 
	$(".article").each(function(){
		$(this).find("img:first").css("display","none");
	});*/

	/* Mouseover Effekte fuer Hauptnavigation */
	$(".naviButton").hover(
		function(){
			if(!$(this).hasClass("active")){
				$(this).children("span").animate({opacity: "1"}, 250);
				$(this).mousedown(function(){
					$(this).children("span").css("background-position","0 -56px");
				});
				$(this).mouseup(function(){
					$(this).children("span").css("background-position","0 -28px");
				});
			}
		},
		function(){
			if(!$(this).hasClass("active")){
				$(this).children("span").animate({opacity: "0"}, 250);
			}
		});

		$(".naviButton").mousedown(function(){
			$(this).css("outline","none");
		});
		$(".naviButton").mouseup(function(){
			$(this).removeAttr("style");
			$(this).blur();
	});
		
	/* Tabbox */
	if($("#tabnavi").length > 0){
	$("#newPosts").noOutline();
	$("#newPosts").click(function(){
		$("#newPostsDiv").css("display","block");
		$("#top5Div, #tagcloudDiv, #authorDiv").css("display","none");
		$(this).addClass("active");
		$("#top5, #tagcloud, #author").removeClass("active");
		return false;
	});
	$("#top5").noOutline();
	$("#top5").click(function(){
		$("#top5Div").css("display","block");
		$("#newPostsDiv, #tagcloudDiv, #authorDiv").css("display","none");
		$(this).addClass("active");
		$("#newPosts, #tagcloud, #author").removeClass("active");
		return false;
	});
	$("#tagcloud").noOutline();
	$("#tagcloud").click(function(){
		$("#tagcloudDiv").css("display","block");
		$("#newPostsDiv, #top5Div, #authorDiv").css("display","none");
		$(this).addClass("active");
		$("#newPosts, #top5, #author").removeClass("active");
		return false;
	});
	$("#author").noOutline();
	$("#author").click(function(){
		$("#authorDiv").css("display","block");
		$("#top5Div, #tagcloudDiv, #newPostsDiv").css("display","none");
		$(this).addClass("active");
		$("#top5, #tagcloud, #newPosts").removeClass("active");
		return false;
	});
	}
	
	/* Wert fuer Suchfeld setzen */
	var old = "";
	$("#search input, #keyword, #jobsearch").focus(function(){
		if($(this).attr("value") == "Suchbegriff" || $(this).attr("value") == "Stichwortsuche"){
			old = $(this).attr("value");
			$(this).attr("value","");
		}
	});
	$("#search input, #keyword, #jobsearch").blur(function(){
		if($(this).attr("value") == ""){
			$(this).attr("value", old);
		}
	});
	
	/* Teaser auf Start- und Portalseiten komplett verlinken */
	$("#teaser div").click(function(){
		window.location.href = $(this).children("a:first").attr("href");
	});
  /* Hintergrundbilder (Container und Link) verschieben */
	$("#teaser div").hover(function(){
		$(this).css("background-position", "0px -120px").children("a:first").css("background-position", "0px -13px");
	}, function(){
		$(this).css("background-position", "0px 0px").children("a:first").css("background-position", "0px 0px");
	});

	/* Filterfunktion auf- und zuklappen */
	if($("#filter").length > 0){
		$("#filter h3").click(function(){
			if($(this).text() == "Filterfunktionen ausblenden"){
				$(this).text("Filterfunktionen einblenden");
			}
			else {
				$(this).text("Filterfunktionen ausblenden");				
			}
			$("#filter").toggleClass("close");
			$("#filter form").toggle();
		});
	}
	
	/* Filterfunktion Dropdowns auf- und zuklappen und Verhalten*/
	if($("#filter").length > 0){
		var text = "";
		$(".dropdown").click(function(){
			$(this).next(".dropdownSelect").toggle();
		});
		$(".close").click(function(){
			$(this).parent().parent().hide();
		});
		$(".remove").click(function(){
			$(this).parent().parent().find(":checkbox:checked").each(function(){
				$(this).attr("checked", false);
			});
			$(this).parent().parent().prev().text("Keine Optionen ausgewählt");
		});

	
		var tog = false; 
		$(".dropdownSelect ul li").click(function(){
			if($(this).children(":checkbox").attr("checked") == true){
				$(this).children(":checkbox").attr("checked", false);
			}
			else {
				$(this).children(":checkbox").attr("checked", true);
			}
      
      // Sonderfall "Schulabschluss": hier sollen die vorangegangne Abschluesse auch markieren
      /*$(this).prevAll().each(
        function() {
          trigger("click");
        }
      );*/
      
      // Anzahl der gewaehlten Optionen
      var count_options = $(this).parent().find(":checkbox:checked").length;
      
      // Bezeichner fuer die Selectbox setzen
      var option_name = "Option";
      if ( $(this).parent().parent().attr("id") == "area" ) {
        option_name = (count_options == 1) ? "Berufsfeld" : "Berufsfelder";
      } else if ( $(this).parent().parent().attr("id") == "school" ) {
        option_name = (count_options == 1) ? "Abschluss" : "Abschlüsse";
      }
      
			$(this).parent().parent().prev().text(count_options + " " + option_name + " ausgewählt");
		});	
		$(".dropdownSelect ul li input:checkbox").click(function(){
			if($(this).attr("checked") == true){
				$(this).attr("checked", false);
			}
			else {
				$(this).attr("checked", true);
			}
		});
	}
	
	/* Jobsuche auf Portalseite "Die Jobsuche" */
	if($("#jobsearch").length > 0){
		$("#jobsearchButton").click(function(){
			$("#jobAd li, #jobAdStep li").remove();
      var search_item = encodeURI( $("#jobsearch").attr("value") );
			searchStepstone( search_item );
			searchArbeitenDE( search_item );
			$(".headline").show();
      return false;
		});
	}
		
  /* Filtereinstellungen nach der Suche wieder auswaehlen */
  if (select_tags.length) {
    $("#area input").each(
      function() {
        for ( i=0; i < select_tags.length; i++ ) {
          if( $(this).val() == select_tags[i] ) {
            $(this).parent().trigger("click");
          }
        }
      }
    );
  }
  if (select_schools.length) {
    $("#school input").each(
      function() {
        for ( i=0; i < select_schools.length; i++ ) {
          if( $(this).val() == select_schools[i] ) {
            $(this).parent().trigger("click");
          }
        }
      }
    );
  }
  if (keywords != "") {
    $("#keyword").val(keywords);
  }
  
  $("#filter_pages a").click(
    function() {
      // ID der angeklickten Seite in das hidden field des Formulars schreiben
      $("#filter_page").val( $(this).attr("id").replace(/filter_page_/, "") );
      $("#sendFilter").trigger("click");
      return false;
    }
  );

  
  $("#RES_ID_fb_login_image").hover(function(){
		$(this).attr('src', '/wp-content/themes/bewerbung-de/images/article/button_facebook_hover.gif');
	}, function(){
		$(this).attr('src', '/wp-content/themes/bewerbung-de/images/article/button_facebook.gif');
	});	
  window.setTimeout("changeImage()", 100);
  window.setTimeout("showImage()", 1000);
  
  $("#facebook_connect_button").hover(function(){
		$(this).attr('src', '/wp-content/themes/bewerbung-de/images/article/button_facebook_hover.gif');
	}, function(){
		$(this).attr('src', '/wp-content/themes/bewerbung-de/images/article/button_facebook.gif');
	});	

  $('input[name=fbc_submit_hack]').attr('class', 'facebook_submit').val('');
  
  if ($('input[name=fbc_submit_hack]').length == 1) {
    $('a#logout').attr('href', '#').attr('onClick', 'FBConnect.logout(); return false;');
  }	
	
	if ($('a[name=twcbutton]').text() == '') {
    $('a[name=twcbutton]').parent('p').css('display', 'none');
  }
	
  /* Kommentar-Formular: Validierung */
  $("#commentform #submit").click(
    function() {
      $("#commentform_error").remove();

      // Bei nicht-eingeloggten Usern Noch Name und E-Mail abfragen
      if ($("#commentform #loggedIn").length == 0) {
        // Pflichtfeld Name pruefen
        if ( $("#commentform #author").val().replace(/ /g, "") == ""  ) {
          $("#commentform").append('<div id="commentform_error">* Bitte füllen Sie die erforderlichen Felder korrekt aus.</div>');
          return false;
        }

        // E-Mail-Pruefung
        var email_regex = /^.+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})$/;
        var check_email = $("#commentform #email").val().search(email_regex);
        if (check_email == -1) {
          $("#commentform").append('<div id="commentform_error">* Bitte tragen Sie eine korrekte E-Mail-Adresse ein.</div>');
          return false;
        }
      }

      // Pflichtfeld Kommentar pruefen
      if ( $.trim( $("#commentform #comment").val() ).length == 0 ) {
        $("#commentform").append('<div id="commentform_error">* Bitte füllen Sie das Kommentarfeld aus.</div>');
        return false;
      }
    }
  );
});//ende document ready

/* Ausgelagerte Funktionen: */

function changeImage() {
	$('#RES_ID_fb_login_image').attr('src', '/wp-content/themes/bewerbung-de/images/article/button_facebook.gif');
}

/* Facebook Connect Button einblenden */
function showImage() {
	$('#RES_ID_fb_login_image').css('visibility', 'visible');
}

/* Tickerfunktion fuer Neue Jobs Box, Jobs von stepstone.de */
var oldJobs = [$([]), $([]), $([])];
var count = 0;
function getStepstoneJobs() {
	$.ajax({
		type: "GET",
		url: "/?jobs_uri=http://www.stepstone.de/services/search/index.cfm?lang=de%26amount=5%26api_key=5329AC9709F8A4BCD293D82D18DFC5",
		success: function(result){
			count = 0;
			//$("#newJobs ul .load").remove();
			
			$(result).find("job").each(function(){
				
				//alert(oldJobs[0].attr("id")+" und "+$(this).attr("id"));
				
				if(oldJobs[0].equals($(this))){
					//alert("gleich");					
					return false;
				}
				else {
					//alert("ungleich");
					oldJobs[count] = $(this);
					count++;
					
					var title = $(this).children("title").text();
					if(title.length > 35){
						title = title.substr(0, 35)+" [...]";
					}
					var link = $(this).children("link").text();
					link = link.replace(/cid=.*?&/g,"cid=bewerbungde&");
					$("#newJobs ul").append(
					"<li style='display: none;'><a href='" + link + "' target='_blank' title='"+ $(this).children("title").text() +"'>" + title + "</a></li>"
					);
				}				
			});
			for (var i = 0; i < count; i++) {
				$("#newJobs ul li:first").slideUp(1000,function(){
          $("#newJobs ul li:first").remove();
				});
				$("#newJobs ul li:hidden:first").slideDown(1000);
			}
		}
	});
}

/* Stepstone Jobs beim laden der Seite holen */
function initStepstoneJobs() {
	$.ajax({
		type: "GET",
		url: "/?jobs_uri=http://www.stepstone.de/services/search/index.cfm?lang=de%26amount=5%26api_key=5329AC9709F8A4BCD293D82D18DFC5",
		success: function(result){

			$("#newJobs ul .load").remove();
			$(result).find("job").each(function(){
				
				oldJobs[count] = $(this);
				count++;
				var title = $(this).children("title").text();
				if(title.length > 35){
					title = title.substr(0, 35)+" [...]";
				}
				var link = $(this).children("link").text();
				link = link.replace(/cid=.*?&/g,"cid=bewerbungde&");
				$("#newJobs ul").append(
				"<li><a href='" + link + "' target='_blank' title='"+ $(this).children("title").text()  +"'>" + title + "</a></li>"
				);
        // Copyright-Position anpassen (sonst IEx-Problem)
        $("#copyright").css("bottom", "55px");
			});
			$("#newJobs div").css("height","150px");
		}
	});	
}

/* arbeiten.de Jobs beim laden der Seite holen */
function initArbeitenDEJobs() {
$.ajax({
	type: "GET",
	url: "/?jobs_uri=http%3A%2F%2Fwww.arbeiten.de%2Flistings%2Findex.step%3Fcid%3Dbewerbungde&type=text/plain&encode=iso-8859-1&imgrepl=1",
	success: function(result){
		$("#newJobs ul .load").remove();
		$(result).find(".job-title-list h3").each(function(){
			var longLinkText = $(this).children("a:first").text();
			if(longLinkText.length > 35){
				var linkText = longLinkText.substr(0, 35)+" [...]";
				$(this).children("a:first").text(linkText);
			}
			$("#newJobs ul").append("<li>"+ $(this).html().replace(/href="/, 'target="_blank" title="'+ longLinkText +'" href="http://www.arbeiten.de') +"</li>");
		});
		$("#newJobs div").css("height","150px");
	}
	});
}

/* Tickerfunktion fuer Neue Jobs Box, Jobs von arbeiten.de */
function scrollArbeitenDeJobs() {
		$("#newJobs ul li:first").slideUp(1000,function(){
			$("#newJobs ul li:last").after($("#newJobs ul li:first"));
			$("#newJobs ul li:last").css("display", "block");
		});
}

/* Stepstone Jobs in JQuery Objekten vergleichen */
$.fn.equals = function(compareTo) {
	if (!compareTo || !compareTo.length || this.length!=compareTo.length){
		return false;
	}
	else {
		if(this.attr("id") == compareTo.attr("id")) {
			return true;
		}
		else {
			return false;
		}
	}
};

/* Outline bei Mausklick nicht anzeigen */
$.fn.noOutline = function() {
	$(this).mousedown(function(){
		$(this).css("outline","none");
	});
	$(this).mouseup(function(){
		$(this).removeAttr("style");
		$(this).blur();
	});
	return false;
};

function searchStepstone(terms) {
var terms = terms;
$.ajax({
	type: "GET",
	url: "/?jobs_uri=http://www.stepstone.de/services/search/index.cfm?lang=de%26amount=5%26api_key=5329AC9709F8A4BCD293D82D18DFC5%26keyword="+terms,
	success: function(result){
		$(result).find("job").each(function(){
			var link = $(this).children("link").text();
			link = link.replace(/cid=.*?&/g,"cid=bewerbungde&");
			$("#jobAdStep").append(
        "<li><a href='" + link + "' target='_blank' title='"+ $(this).children("title").text()  +"'>" + $(this).children("title").text() + "</a></li>"
			);
		});
    
    // falls keine Ergebnisse gefunden wurden, entsprechenden Hinweis ausgeben
    if ( $("#jobAdStep").html() == "" ) {
      $("#jobAdStep").append("<li>Hier konnten leider keine Ergebnisse gefunden werden.</li>");
    }
	}
	});
}


/* arbeiten.de Suche */
function searchArbeitenDE(terms) {
var terms = terms;
var counter = 0;
$.ajax({
	type: "GET",
	url: "/?jobs_uri=http%3A%2F%2Fwww.arbeiten.de%2Flistings%2Flist.step%3Farbeiten_als%3D"+ terms +"%26search%3Dnow%26cid%3Dbewerbungde&type=text/plain&encode=iso-8859-1&imgrepl=1",
	success: function(result){
		$(result).find(".job-title-list h3").each(function(){
			if (counter == 5){ return false; }
			counter++;
			$("#jobAd").append("<li>"+
			$(this).html().replace(/href="/, 'target="_blank" href="http://www.arbeiten.de')+"</li>");
		});

    // falls keine Ergebnisse gefunden wurden, entsprechenden Hinweis ausgeben
    if ( $("#jobAd").html() == "" ) {
      $("#jobAd").append("<li>Hier konnten leider keine Ergebnisse gefunden werden.</li>");
    }
	}
	});
}