/**
 * JS Init
 *
 * @project		-
 * @author		Boris Bojic <boris.bojic@vanato.com>
 * @copyright	-
 * @version		Thu, 11 Aug 2011 18:10:33 +0200
 * @updated		Thu, 01 Sep 2011 18:19:55 +0200
 *
 */

 function FensterOeffnen (Adresse) {
      MeinFenster = window.open(Adresse, "_blank");
      MeinFenster.focus();
}

function changePage(page_par){
    $('#pagechoose').val(page_par);
    $('#jobsearchBig').submit();
}

function changePageNL(page_par){
    $('#pagechoose').val(page_par);
    $('#NLSearch').submit();
}

function changePageNLFB(page_par){
    $('#pagechoose').val(page_par);
    $('#FBNLSearch').submit();
}

function changePageNLComponent(page_par){
    $('#pagechoose').val(page_par);
    $('#NLSearchComponent').submit();
}

function showMapStandorteComponent(){
    $('#showMapStandorteInputComponent').val('1');
    $('#NLSearchComponent').submit();
}

function showListStandorteComponent(){
    $('#showMapStandorteInputComponent').val('0');
    $('#NLSearchComponent').submit();
}


function showMapJobs(){
    $('#showMapJobsInput').val('1');
    $('#jobsearchBig').submit();
}

function showListJobs(){
    $('#showMapJobsInput').val('0');
    $('#jobsearchBig').submit();
}

function showMapStandorte(){
    $('#showMapStandorteInput').val('1');
    $('#NLSearch').submit();
}

function fbShowMapStandorte(){
    $('#showMapStandorteInput').val('1');
    $('#FBNLSearch').submit();
}

function showListStandorte(){
    $('#showMapStandorteInput').val('0');
    $('#NLSearch').submit();
}

function fbShowListStandorte(){
    $('#showMapStandorteInput').val('0');
    $('#FBNLSearch').submit();
}



$(function(){

                 $('#showMapJobs').click(function(){
                     showMapJobs();
                 });
                 $('#showListJobs').click(function(){
                     showListJobs();
                 });

                 $('#showListStandorteComponent').click(function(){
                     showListStandorteComponent();
                 });
                 $('#showMapStandorteComponent').click(function(){
                     showMapStandorteComponent();
                 });

                 $('#showListStandorte').click(function(){
                     showListStandorte();
                 });

                 $('#fbShowListStandorte').click(function(){
                     fbShowListStandorte();
                 });

                 $('#showMapStandorte').click(function(){
                     showMapStandorte();
                 });
                 $('#fbShowMapStandorte').click(function(){
                     fbShowMapStandorte();
                 });
                 $('.mapChooseLandStandorte').click(function(){
                     var land= $(this).attr('rel');
                     $('#land_filter').val(land);
                     $('#NLSearch').submit();
                 });

                  $('.contact_popup').click(function(){
                         $.colorbox({inline:true, href:"#contact_form_popup"});
                         return false;
                  });

                  $('#processRegister, #processRegisterAccountBox').click(function(){
                         $.colorbox({inline:true, href:"#registrationForm"});
                         return false;
                  });

					/* Newsletter Popup öffnen */
					$('.opennlinoutbox').click(function(event){
						event.preventDefault();
						$.colorbox({inline:true, href:".newsletterinoutbox"});
					});
					
					/* Newsletter Popup verarbeiten */
					$('.processNewsletterForm').click(function(event){
						event.preventDefault();
						$('#nlstatusmessages').html('');
						$.post("/frontend/doNewsletterInOutForm", $("#newsletterInOutFormular").serialize(), function(resultData){
							$('#nlstatusmessages').html(resultData);
							$.colorbox({inline:true, href:".newsletterinoutbox"}).resize();
						});
					});

                  $('.notLoggedInApply').click(function(event){
					  event.preventDefault();
                      $.colorbox({inline:true, href:"#loginOrRegistration"});
                      return false;
                  });
                  var wanted_title = "";
                  $('.notLoggedInApplyWanted').click(function(){
                      wanted_title = $(this).attr('rel');
                      $.colorbox({inline:true, href:"#loginOrRegistrationWanted"});
                      return false;
                  });

                  $('#submitRegistration').click(function(){
                      $('#registrationError').slideUp('fast',function(){
                          $.post("/frontend/doRegistration", $("#registrationFormular").serialize(), function(resultData){
                              if(resultData.substr(0, 7) != "allDone"){
                                  $('#registrationError').html(resultData);
                                  $('#registrationError').slideDown('fast',function(){
                                       $.colorbox({inline:true, href:"#registrationForm"}).resize();
                                  });
                              }else{
	                        	   var jobId = $('#processRegister').attr('rel');
	                        	   if(typeof jobId !== 'undefined' && jobId !== false){
	                        		   if(jobId == "wanted"){
		                            	   var bewLink = "";
			                           	   var hash = resultData.substr(7);
		                                   if(window.location.hostname == "homepage-test.mein-tempo-team.de"){
		                                	   bewLink ="https://test.mein-tempo-team.de/bewerbung_wanted.html?wanted_title=" + wanted_title + "&wanted_location=" + "Bundesweit" + "&wanted_email=" + "info%40de.tempo-team.com" + "&hash=" + hash;
		                                   }else{
		                                	   bewLink ="https://mein-tempo-team.de/bewerbung_wanted.html?wanted_title=" + wanted_title + "&wanted_location=" + "Bundesweit" + "&wanted_email=" + "info%40de.tempo-team.com" + "&hash=" + hash;
		                                   }
		                            	   $('#LinkBewForm a').attr('href',bewLink);
		                            	   setTimeout(function(){window.location.href = bewLink;},10000);

	                        		   }else{
                                                                                   var bewLink = "";
                                                                                   var hash = resultData.substr(7);
                                                                                   var portal_ref = $('#portal_ref_hidden').attr('rel');
                                                                                   if(portal_ref.length > 0){
                                                                                       portal_ref = "&p=" + portal_ref;
                                                                                   }else{
                                                                                       portal_ref = "";
                                                                                   }
                                                                                   if(window.location.hostname == "homepage-test.mein-tempo-team.de"){
                                                                                           bewLink = "https://test.mein-tempo-team.de/bewerbung." + jobId + ".html?hash=" + hash + portal_ref;
                                                                                   }else{
                                                                                           bewLink = "https://mein-tempo-team.de/bewerbung." + jobId + ".html?hash=" + hash + portal_ref;
                                                                                   }
                                                                                   $('#LinkBewForm a').attr('href',bewLink);
                                                                                   setTimeout(function(){window.location.href = bewLink;},10000);
	                        		   }
	                        	   }else{
	                        		   $('#LinkBewForm').html("");
	                        	   }
                            	   if(window.location.hostname != "homepage-test.mein-tempo-team.de" && window.location.hostname != "tempo-team_hp.local"){
                            		   $('#registrationConvTrack').html("<iframe src='http://tempo-team.com/google_conv_codes/conv_registrieren.html' height='0' width='0'  frameborder='0' TITLE='Tracking'></iframe> ");
                            	   }
                            	   $('#registrationForm').html("");
                                   $.colorbox({inline:true, href:"#registrationSuccess"}).resize();
                              }
                          });
                      });
                  });

                  $('#submitPersonalanfrage').click(function(){
                      $('#personalanfrageError').slideUp('fast',function(){
                          $.post("/frontend/submitPersonalanfrage", $("#personalanfrageFormular").serialize(), function(resultData){
                              if(resultData != "allDone"){
                                  $('#personalanfrageError').html(resultData);
                                  $('#personalanfrageError').slideDown('fast');
                              }else{
                            	  if(window.location.hostname != "homepage-test.mein-tempo-team.de" && window.location.hostname != "tempo-team_hp.local"){
                           		   	  $('#personalanfrageConvTrack').html("<iframe src='http://tempo-team.com/google_conv_codes/conv_personalanfrage.html' height='0' width='0'  frameborder='0' TITLE='Tracking'></iframe> ");
                            	  }
                           	   	  $('#personalanfrageError').slideUp('fast');
                                  $('#personalanfrageForm').slideUp('fast');
                                  $('#personalanfrageSuccess').slideDown('slow');
                                  $("#personalanfrageFormular input, #personalanfrageFormular textarea").val('');
                              }
                          });
                      });

                  });

                  $('#submitKontaktformular, #submitKontaktformularPopup').click(function(){
                      var popup = '';
                      if($(this).attr('rel') == "popup"){
                          popup = 'Popup';
                      }
                      $('#kontaktformularError' + popup).slideUp('fast',function(){
                          $.post("/frontend/submitKontaktformular", $("#kontaktformularFormular" + popup).serialize(), function(resultData){
                              if(resultData != "allDone"){
                                  $('#kontaktformularError' + popup).html(resultData);
                                  $('#kontaktformularError' + popup).slideDown('fast',function(){
                                       if(popup != ''){$.colorbox({inline:true, href:"#contact_form_popup"}).resize();}
                                  });
                              }else{
                            	  if(window.location.hostname != "homepage-test.mein-tempo-team.de" && window.location.hostname != "tempo-team_hp.local"){
                           		   	  $('#kontaktformularConvTrack' + popup).html("<iframe src='http://tempo-team.com/google_conv_codes/conv_kontakt.html' height='0' width='0'  frameborder='0' TITLE='Tracking'></iframe> ");
                            	  }
                           	      $('#kontaktformularError' + popup).slideUp('fast');
                                  $('#kontaktformularForm' + popup).slideUp('fast');
                                  $('#kontaktformularSuccess' + popup).slideDown('slow',function(){
                                       if(popup != ''){$.colorbox({inline:true, href:"#contact_form_popup"}).resize();}
                                  });
                                  $("#kontaktformularFormular input, #kontaktformularFormular textarea, #kontaktformularFormularPopup input, #kontaktformularFormularPopup textarea").val('');
                              }
                          });
                      });

                  });

                  $('#processLogin').click(function(){
                       $.post("/frontend/doLogin", $("#loginFormular").serialize(), function(resultData){
                              if(resultData == "false"){
                                  alert("Ihre Logindaten sind falsch oder Sie haben Ihre Registrierung noch nicht bestätigt.");
                              }else if(resultData == "newPW"){
                                  $.colorbox({inline:true, href:"#setNewPwForm"});
                              }else if(resultData == "true"){
                                  alert("Sie wurden erfolgreich eingeloggt.");
                                  var act_page = window.location.href;
                                  act_page += '?multi_login=true';
                                  window.location.href = act_page;
                              }
                      });
                      return false;
                  });
                  $('#processLoginJob').click(function(){
                      var jobId = $(this).attr("rel");
                       $.post("/frontend/doLogin?job=job", $("#loginFormularJob").serialize(), function(resultData){
                              if(resultData == "false"){
                                  alert("Ihre Logindaten sind falsch oder Sie haben Ihre Registrierung noch nicht bestätigt.");
                              }else{
                                  var portal_ref = $('#portal_ref_hidden').attr('rel');
                                  if(portal_ref.length  > 0){
                                      portal_ref = "&p=" + portal_ref;
                                  }else{
                                      portal_ref = "";
                                  }
                                  if(window.location.hostname == "homepage-test.mein-tempo-team.de"){
                                        location.replace("https://test.mein-tempo-team.de/bewerbung." + jobId + ".html?hash=" + resultData + portal_ref);
                                  }else{
                                        location.replace("https://mein-tempo-team.de/bewerbung." + jobId + ".html?hash=" + resultData + portal_ref);
                                  }
                              }
                      });
                      return false;
                  });
                  $('#processLoginWanted').click(function(){
                      var jobId = $(this).attr("rel");
                       $.post("/frontend/doLogin?job=job", $("#loginFormularWanted").serialize(), function(resultData){
                              if(resultData == "false"){
                                  alert("Ihre Logindaten sind falsch oder Sie haben Ihre Registrierung noch nicht bestätigt.");
                              }else{
                                   if(window.location.hostname == "homepage-test.mein-tempo-team.de"){
                                        location.href ="https://test.mein-tempo-team.de/bewerbung_wanted.html?wanted_title=" + wanted_title + "&wanted_location=" + "Bundesweit" + "&wanted_email=" + "info%40de.tempo-team.com" + "&hash=" + resultData;
                                   }else{
                                        location.href ="https://mein-tempo-team.de/bewerbung_wanted.html?wanted_title=" + wanted_title + "&wanted_location=" + "Bundesweit" + "&wanted_email=" + "info%40de.tempo-team.com" + "&hash=" + resultData;
                                   }
                              }
                      });
                      return false;
                  });
                  $('#processLogout').click(function(){
                      $.get("/frontend/doLogout", function(){
                          var act_page = window.location.href;
                          act_page += '?multi_logout=true';
                          window.location.href = act_page;
                          setTimeout(function(){window.location.href=act_page;},500);
                      });
                  });
                  $('#pwVergessenButton').click(function(){
                       $.colorbox({inline:true, href:"#pwVergessenForm"});
                      return false;
                  });
                  $('#submitpwVergessen').click(function(){
                       $.post("/frontend/dopwVergessen", $("#pwVergessenFormular").serialize(), function(){
                              alert("Ihnen wurde ein neues Passwort per E-Mail zugeschickt.");
                              window.location.reload();
                      });
                      return false;
                  });
                    $('#setNewPw').click(function(){
                      $('#setNewPwError').slideUp('fast',function(){
                           $.post("/frontend/doResetPw", $("#setNewPwFormular").serialize(), function(resultData){
                              if(resultData != "allDone"){
                                  $('#setNewPwError').html(resultData);
                                  $('#setNewPwError').slideDown('fast',function(){
                                       $.colorbox({inline:true, href:"#setNewPwForm"}).resize();
                                  });
                              }else{
                                   window.location.reload();
                              }
                          });
                      });
                      return false;
                  });

        /* SHARE JOBs */
        $('.share').click(function(){
            var id = $(this).attr('rel');
            $('#shareJob input.jobId').val(id);
            $.colorbox({inline:true, href:"#shareJob"});
                         return false;
                  });

        $('#submitJob').click(function(){
                      $('#shareError').slideUp('fast',function(){
                          $.post("/frontend/mailJob", $("#shareJob").serialize(), function(resultData){
                              if(resultData != "allDone"){

                                  $('#shareError').slideDown('fast',function(){
                                       $.colorbox({inline:true, href:"#shareJob"}).resize();
                                  });
                              }else{
                                   $.colorbox({inline:true, href:"#shareSuccess"}).resize();

                              }
                          });
                      });
                  });

	/* Kleine Menüanimation */
	$('#nav ul ul.scnd ul').hide(); /* :hover im CSS überschreiben */

	$('#nav ul ul.scnd li').hover(
		function(){
			$(this).find('ul').slideDown('fast');
		},
		function(){
			$(this).find('ul').hide();
		}
	);

	$(".chzn-select").chosen();

                  $('.mapChooseLand').click(function(){
                      var land = $(this).attr('rel');
                      $('.land_checker').each(function(){
                            if($(this).find('input').val() == land){
                              $(this).find('input').attr('checked', true);
                              $(this).addClass('jobSearchChecked');
                              $(this).find('img').show();
                            }
                       });
                       $('#jobsearchBig').submit();
                  });

                  $('.mapChooseCity').click(function(){
                      var city = $(this).attr('rel');
                      $('.city_checker').each(function(){
                            if($(this).find('input').val() == city){
                              $(this).find('input').attr('checked', true);
                              $(this).addClass('jobSearchChecked');
                              $(this).find('img').show();
                            }
                       });
                       $('#jobsearchBig').submit();
                  });

                  $('.jobsearch input').css('position','relative');
                  $('.jobsearch input').css('left','-9999px');
                  $('.jobsearch').click(function(){
                      if($(this).find('input').attr('checked')){
                             $(this).find('input').attr('checked', false);
                             $(this).removeClass('jobSearchChecked');
                             $(this).find('img').hide();
                             $('#jobsearchBig').submit();
                      }else{
                             if($(this).hasClass('veroeffentlicht_checker')){
                                 $('.veroeffentlicht_checker').each(function(){
                                      $(this).find('input').attr('checked', false);
                                      $(this).removeClass('jobSearchChecked');
                                      $(this).find('img').hide();
                                 });
                             }
                             $(this).find('input').attr('checked', true);
                             $(this).addClass('jobSearchChecked');
                             $(this).find('img').show();
                             $('#jobsearchBig').submit();
                      }
                  });
                   $('.jobsearch').each(function(){
                      if($(this).find('input').attr('checked')){
                             $(this).find('input').attr('checked', true);
                             $(this).addClass('jobSearchChecked');
                             $(this).find('img').show();
                      }
                  });

	$('.jobResults_blaettern').click(function(){
		var page=$(this).attr('rel');
		$('#jobsuccess_pagevar').val(page);
		document.Jobsuche.submit();
	});

                $('#niederlassungen_button, #sprache_button').find('button').click(function(){
                    $(this).trigger('blur');
                    if($(this).hasClass('is_open')){
                         $(this).removeClass('is_open');
                         $(this).parent().find('.slider').slideUp('fast');
                    }else{
                         $(this).addClass('is_open');
                         $(this).parent().find('.slider').slideDown('fast');
                    }
                });

                  //var accountboxtimeout;
	/* Accountbox öffnen und schließen */
	$('#accountbutton').click(function(){
		$('#accountbox').toggle();
	});
	/* Accountbox öffnen und schließen */
	$('#accountbutton').mouseenter(function(){
		$('#accountbox').show();
//                                    accountboxtimeout = setTimeout(function(){$('#accountbox').hide();},3000);
	});
//                  $('#accountbox').mouseenter(function(){
//                      clearTimeout(accountboxtimeout);
//                  });
//                  $('#accountbox').mouseleave(function(){
//                      accountboxtimeout = setTimeout(function(){$('#accountbox').hide();},3000);
//                  });

                  var mouseisover = false;
	$('.scroll_left').fadeTo(1, 0.4);
	var scrolling_is_active = false;
	function scrollBox(scroller_elem, direction, id){
		var elem_width = $(scroller_elem).width()/($(scroller_elem).children().length);
		var max_scroll = (($(scroller_elem).width() - $(scroller_elem).parent().parent().width()) * (-1)) + 10;
		var scroll_left = parseInt($(scroller_elem).css('left'))*(-1);
		if(!scrolling_is_active && ((((scroll_left > max_scroll) && direction == '-')) || (((scroll_left > 0) && direction == '+')))){
			scrolling_is_active = true;
			var pos_to_scroll = 0;

			if(direction == '+'){
				pos_to_scroll = parseInt($(scroller_elem).css('left')) + elem_width;
			}else{
				pos_to_scroll = parseInt($(scroller_elem).css('left')) - elem_width;

			}
			if(pos_to_scroll < max_scroll){
				pos_to_scroll = max_scroll;
				setTimeout(function(){$('#scrollrightbutton' + id).fadeTo(200, 0.4)},4000);
				setTimeout(function(){$('.scrollBox .scroll_right').trigger('mouseleave')},3000);
				setTimeout(function(){$('.scrollBox .scroll_left').trigger('mouseenter')},5000);
			}else{
				$('#scrollrightbutton' + id).fadeTo(200, 1);
			}
			if(pos_to_scroll > 0){
				pos_to_scroll = 0;
				setTimeout(function(){$('#scrollleftbutton' + id).fadeTo(200, 0.4)},4000);
				setTimeout(function(){$('.scrollBox .scroll_left').trigger('mouseleave')},3000);
				setTimeout(function(){$('.scrollBox .scroll_right').trigger('mouseenter')},5000);
			}else{
				$('#scrollleftbutton' + id).fadeTo(200, 1);
			}
			$(scroller_elem).animate({
				left: [pos_to_scroll, 'swing'],
			}, 4000, function(){
                                                            scrolling_is_active=false;
                                                            if(mouseisover){
                                                                scrollBox(scroller_elem, direction, id);
                                                            }
                                                      });
		}
	}
	$('.scrollBox .scroll_right').mouseenter(function(){
                                    mouseisover = true;

		var id= $(this).attr('rel');
		scrollBox('#scrollBox' + id + ' .scrollwrapper', '-', id);
	});
	$('.scrollBox .scroll_left').mouseenter(function(){
                                    mouseisover = true;
		var id= $(this).attr('rel');
		scrollBox('#scrollBox' + id + ' .scrollwrapper', '+', id);
	});
	$('.scrollBox .scroll_right').mouseleave(function(){
                                    mouseisover = false;
	});
	$('.scrollBox .scroll_left').mouseleave(function(){
                                    mouseisover = false;
	});

                   if($("#left_col > div").last().html() == $('.new_jobs').parent().html()){
                      var max_height = $('#right_col').height();
                      $('.new_jobs .hidden').each(function(){
                          var to_show = $(this);
                          if($('#left_col').height() < max_height){
                              to_show.show();
                          }
                      });
                   }

});


