/**
 * $Id: common.js,v 1.1.2.3 2011-12-20 11:20:46 mmuths Exp $
 */

/**
 * get menuId
 * 
 * @param	string	menuId
 * @return	string
 */
function getMenuId(menuId) {
	
	// regenerate menuId by found HtmlElements
	if ($("form[name='create_account_form']").length != 0
		&& $("#enter_pw_qa").length == 0)
	{		
		return "600"; // current page is user registration
	
	} else if ($("#enter_pw_qa").length != 0) {
		
		return "601"; // current page is registration additional quest
		
	} else if ($("#account_created").length != 0) {
		
		return "602"; // current page is registration success
		
	} else if ($("#pw_request").length != 0) {
		
		return "610"; // current page is password forgot
		
	} else if ($("#pw_request2").length != 0) {
		
		return "611"; // current page is password forgot additional quest
		
	} else if ($("#password_sent").length != 0) {
		
		return "612"; // current page is password forgot success
		
	} else if ($("#start_appl").length != 0) {
		
		return "620"; // current page is login startpage
		
	} else if ($("#logout_done").length != 0) {
		
		return "621"; // current page is logout success
		
	} else if ($("#support_sent").length != 0) {
		
		return "630"; // current page is support success
		
	} else if (menuId == "0") {
		
		return "1"; // current page is jobofferlist or one of application process
		
	} else if (menuId != "") {
		
		// current page already have a valid menuId
		return menuId;
	}
	
	return "1"; // current page is jobofferlist or one of application process
}

/**
 * delete hidden cells in applicationform
 * 
 * @return void
 */
function delHiddenCell() {
    
	$(".appl_input_line").filter(function() {
        	
		delit = false;
			
        if ($("div.appl_input_cell > input:hidden", this).length == $("div.appl_input_cell", this).children().length) {
				
    		$(this)	.children("div.appl_input_cell").each(function() {
    			
    			delit = true;
				
    			$("input:hidden", this)	.each(function() {
    				
    				if ($(this)	.attr("value") != ""
						|| $(this).attr("name") != "felder[]") 
    				{										
    					delit = false;
					}
				});
			});
		}
			
    	return delit;
    
    }).remove();
}

/**
 * append mask for modal window to DOM
 * 
 * @return void
 */
function getModalWindowMask() {
    
    // append div element '#modal_window_mask' and div element '#modal_window_dialog' to DOM
    $('body').append('<div id="modal_window_mask" class="popup_mask"></div>');
    
    // declare screen height and width
    var maskHeight = $(document).height();
    var maskWidth  = $(document).width();
    
    // set height and width to mask to fill up screen
    $('#modal_window_mask').css({'width':maskWidth, 'height':maskHeight});
} 

/**
 * remove mask for modal window from DOM
 * 
 * @return void
 */
function closeModalWindowMask() {
    
	$('#modal_window_mask').remove();
}

/**
 * modal window getter
 * 
 * @param 	string path
 * @return void
 */
function getModalWindow(path) {
	
	getModalWindowMask();
	setModalWindowHtml();
	setModalWindowContentHtml(path);
}

/**
 * set modal window frame html
 * 
 * @param 	string content
 * @return void
 */
function setModalWindowHtml() {
	
	var html = '';
	
	html += '<div class="close_popup">';
	html += '    <a href="#" onclick="javascript:closePopUp(); closeModalWindowMask(); return false;">Schlie&szlig;en</a>';   
	html += '</div>';
	html += '<div id="modal_content" class="inner_content inner_popup ajaxloader"></div>';
	
	$("#PopUpLayer").append(html);
	
	var topPos  = $(window).scrollTop() * 1.1;
    var leftPos = ($(window).width() - $('#PopUpLayer').outerWidth()) / 2;
    
    if (topPos  < 1) topPos  = 1;
    if (leftPos < 1) leftPos = 1;
	
	$("#PopUpLayer").css('top', topPos + "px");
	$("#PopUpLayer").css('left', leftPos + "px");
	$("#PopUpLayer").css('display', 'block');
}

/**
 * set content for modal window
 * 
 * @param	string	path
 * @return void
 */
function setModalWindowContentHtml(path) {
	
	getDataByAjaxRequest(path, appendDataToModalWindow);
}

/**
 * set content for user greeting
 * 
 * @param	string	path
 * @return void
 */
function setUserGreeting(path) {
	
	getDataByAjaxRequest(path, appendGreetingDataToBox);
}

/**
 * appending html data to greeting box
 * 
 * @param	string	data
 * @param	string	status
 * @param	string	request
 * @return void
 */
function appendGreetingDataToBox(data, status, request) {
	
	if (data != null && data.length != 0) {
		
		$("#user_greeting").toggleClass("ajaxloader");
		$("#user_greeting").append(data);	
	}
}

/**
 * appending html data to modal window
 * 
 * @param	string	data
 * @param	string	status
 * @param	string	request
 * @return void
 */
function appendDataToModalWindow(data, status, request) {
	
	if (data != null && data.length != 0) {
			
		$("#modal_content").toggleClass("ajaxloader");
		$("#modal_content").append(data);	
	}
}

/**
 * handle primary navigation
 * 
 * @param	string	menuID
 * @return void
 */
function handlePrimaryNav(menuID) {
	
	var listID = "";

    // define current item
    switch (menuID) {
        case "1":
        	if ($("#jobofferlist").length != 0) { // current page is jobofferlist
        		
        		listID = $("#pn_joboffers");
        	}
            break;
            
        case "83":
        	listID = $("#pn_why");
            break;
            
        case "84":
        	listID = $("#pn_how");
            break;
            
        case "85":
        	listID = $("#pn_leader");
            break;
            
        default:
        	return;
    }

    // highlight current item
    if (listID != "") {
    
    	listID.toggleClass("active");
    }
}


/**
 * handle user navigation
 * 
 * @param	string	menuID
 * @return void
 */
function handleUserNav(menuID) {
	
	$("#nav_main_l2_" + menuID).toggleClass("active");
}

/**
 * style hrefs in content box
 * 
 * @return void
 */
function styleContentLinks() {
	
	var appendTxt = "&nbsp;&raquo;";
	
	$(".norm_text a:not(a.noarrow, .link_footer a, link_header a, .pathNav a, .real_table a, #applbox a, a[name='errors'], a[name='anlagen'])").append(appendTxt);
	$("#start_reg a, #login_form a, .headlinetxt a").append(appendTxt);
}

/**
 * handle content style
 * 
 * @param	string	menuID
 * @return void
 */
function handleContent(menuID) {
	
	handleContentHeader(menuID);
	handleContentTeaser(menuID);

	$(".displaynone").remove();
	
	if ($("#main_sidebar").length == 0) {
		
		$("#content_col").toggleClass("col_left");
	}
	
	if ($("#content_header").length == 0) {
		
		$("#main_content").toggleClass("main_content_without_header");
	}
	
	if ($("#jobofferlist").length != 0) { // current page is jobofferlist
		
		$("#content_col").toggleClass("main_content_is_jobofferlist");
	}
	
	if ($(".application_group").length != 0
		|| $(".accordion").length != 0
	) { // form or accordion exists
		
		if ($("#content_col").hasClass('contains_form') == false) {
			
			$("#content_col").addClass("contains_form");
		}
	}
}

/**
 * handle content as application process
 * 
 * @return void
 */
function handleContentAsApplicationProcess() {
	
	$("#main_sidebar_login, #main_sidebar_registry").toggleClass("displaynone");
	
	if ($("#main_sidebar").hasClass("displaynone")) {
		
		$("#main_sidebar").toggleClass("displaynone");
	}
	
	
	if ($("#applbox").length != 0) { // current page is applicationform
		
		$("#main_sidebar_jobdetail, #main_sidebar_jobcontact").toggleClass("displaynone");
		$("#content_col").toggleClass("content_col_is_application");
		
	} else if ($("#bewerbung_control").length != 0) { // current page is application control
		
		$("#main_sidebar_jobdetail, #main_sidebar_jobcontact").toggleClass("displaynone");
		$("#content_col").toggleClass("content_col_is_application");

	} else if($("#employment").length != 0) { // current page is jobdetail
		
		$("#main_sidebar_jobsimilar, #main_sidebar_jobcontact, #ct_recommendation").toggleClass("displaynone");
		
		$("#employment .emp_title:first").prependTo($("#header_links"));
		
		$(".emp_descr").each(function() {
			
			if (jQuery.trim($(this).html()).length == 0) {
	            $(this).remove();
	        }
		});
		
	} else { // current page is application send
		
		return;
	}
}

/**
 * handle content header by menuId
 * 
 * @param	string	menuID
 * @return void
 */
function handleContentHeader(menuID) {
	
	var handleTxtTeaser = true;
	
    switch (menuID) {
        case "1":
        	if ($("#jobofferlist").length == 0) { // current page is any page of application process
        		
        		$("#content_header").remove();
            	handleTxtTeaser = false;
            	
        	} else {
        		
	        	$("#content_header").addClass('content_header_img_joboffer');
	        	$("#content_header").toggleClass("displaynone");
        	}
        	break;
        	
        case "83":
        	$("#content_header").addClass('content_header_img_why');
        	$("#content_header").toggleClass("displaynone");
        	break;
        	
        case "84":
        	$("#content_header").addClass('content_header_img_how');
        	$("#content_header").toggleClass("displaynone");
            break;
        case "85":
        	$("#content_header").addClass('content_header_img_leader');
        	$("#content_header").toggleClass("displaynone");
        	break;
        	
        default:
        	$("#content_header").remove();
        	handleTxtTeaser = false;
        	break;
    }
	
    if (handleTxtTeaser) {
	
    	handleContentHeaderTxt(menuID);
    }
}

function removeErrorSign() {
	
	$("img.error_img").remove();
}

/**
 * handle content header texts by menuId
 * 
 * @param	string	menuID
 * @return void
 */
function handleContentHeaderTxt(menuID) {
	
	var textID = "";
	
	switch (menuID) {
	    case "1":
	    	textID = $("#chh_joboffers");
	    	break;
	    	
	    case "83":
	    	textID = $("#chh_why");
	    	break;
	    	
	    case "84":
	    	textID = $("#chh_how");
	    	break;
	    	
	    case "85":
	    	textID = $("#chh_leader");
	        break;
	        
	    default:
	    	textID = $("#chh_joboffers");
	    	break;
	}
	
	textID.toggleClass("displaynone");
}

/**
 * handle content teasers by menuId
 * 
 * @param	string	menuID
 * @return void
 */
function handleContentTeaser(menuID) {
	
	// display usermenu
	if ($("#main_sidebar_menu").length != 0) {
		
		if ($("#jobofferlist").length != 0) { // current page is jobofferlist
			
			$("#handle_menu").appendTo($("#content_header_login"));
			$("#content_header_login_box").toggleClass("displaynone");
			$("#content_header_sidebar").toggleClass("displaynone");
			
		} else {
			
			$("#main_sidebar_menu").toggleClass("displaynone");
			$("#main_sidebar").toggleClass("displaynone");
		}
	} 
	
	switch (menuID) {
	    case "1":
	    	if ($("#jobofferlist").length == 0) { // current page is one of application process
	    		
	    		handleContentAsApplicationProcess();
	    		
	    	} else if ($("#content_header").length != 0) { // current page is jobofferlist
	    		
	    		var headerSidebarToggle = false;
	    		
	    		if ($("#handle_login").length != 0) {
	    			
	    			$("#handle_login").appendTo($("#content_header_login"));
	    			$("#content_header_login_box").toggleClass("displaynone");
	    			headerSidebarToggle = true;
	            }
	    		
	    		if ($("#handle_registry").length != 0) {
	    			
	    			$("#handle_registry").appendTo($("#content_header_registry"));
			    	$("#content_header_registry_box").toggleClass("displaynone");
			    	headerSidebarToggle = true;
	            }
		    	
	    		if (headerSidebarToggle == true) {
		    	
	    			$("#content_header_sidebar").toggleClass("displaynone");
	    		}
	    	}
	        break;
	        
	    case "83":
	    	$("#handle_jobfeed").appendTo($("#content_header_jobofferfeed"));
	    	$("#content_header_joboffer_feed_box").toggleClass("displaynone");
	    	$("#content_header_sidebar").toggleClass("displaynone");
	    	$("#main_sidebar_login, #main_sidebar_registry").toggleClass("displaynone");
	    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
	    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	
	    	break;
	    	
	    case "84":
	    	$("#handle_jobfeed").appendTo($("#content_header_jobofferfeed"));
	    	$("#content_header_joboffer_feed_box").toggleClass("displaynone");
	    	$("#content_header_sidebar").toggleClass("displaynone");
	    	$("#main_sidebar_login, #main_sidebar_registry").toggleClass("displaynone");
	    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
	    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	break;
	    	
	    case "85":
	    	$("#handle_jobfeed").appendTo($("#content_header_jobofferfeed"));
	    	$("#content_header_joboffer_feed_box").toggleClass("displaynone");
	    	$("#content_header_sidebar").toggleClass("displaynone");
	    	$("#main_sidebar_login, #main_sidebar_registry").toggleClass("displaynone");
	    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
	    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	break;
	    	
	    case "74":
	    	$("#main_sidebar_login, #main_sidebar_registry, #main_sidebar_imprint_at").toggleClass("displaynone");
	    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	break;
	    	
	    case "34":
	    case "75":
	    	$("#main_sidebar_login, #main_sidebar_registry, #main_sidebar_imprint").toggleClass("displaynone");
    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	break;
	    	
	    case "600":
	    case "601":
	    case "610":
	    case "611":
	    case "612":
	    case "621":
	    case "30":
	    	$("#main_sidebar_login").toggleClass("displaynone");
	    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
	    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	break;
	    	
	    case "32":
	    case "630":
	    case "3":
	    case "76":
	    	$("#main_sidebar_login, #main_sidebar_registry").toggleClass("displaynone");
	    	
	    	if ($("#main_sidebar").hasClass("displaynone")) {
	    		
	    		$("#main_sidebar").toggleClass("displaynone");
	    	}
	    	break;
	    	
	    default:
	    	$("#content_header_sidebar").remove();
	    	break;
	}
	
	if ($("#content_header_sidebar .teaser").length == 1) {
		
		$("#content_header_sidebar").toggleClass("content_header_single_teaser");
	}
}

/**
 * html fix for table paginator
 * 
 * @return void
 */
function paginatorStyleFix() {
	
	$("ul.pathNav li:empty").remove();
	
	$("ul.pathNav li.nav_item:last").css('border', '0');
}

/**
 * data getter by ajax request
 * 
 * @param	string		urlpath
 * @param	resource	callback
 * @return mixed
 */
function getDataByAjaxRequest(urlpath, callback) {
	
	$.ajax({
		url: urlpath,
		cache: true,
		success: callback,
		error: function(request, status, error) {
			return false;
		}
	});
}

/**
 * generate jobfeed
 * 
 * @param	string	path
 * @return void
 */
function generateJobfeed(path) {
	
	getDataByAjaxRequest(path, generateJobfeedHtml);
}

/**
 * generate jobsimilar
 * 
 * @param	string	path
 * @return void
 */
function generateJobsimilar(path) {
	
	getDataByAjaxRequest(path, generateJobsimilarHtml);
}

/**
 * generate jobcontact
 * 
 * @param	string	path
 * @return void
 */
function generateJobcontact(path) {
	
	getDataByAjaxRequest(path, generateJobcontactHtml);
}

/**
 * generate jobdetail
 * 
 * @param	string	path
 * @return void
 */
function generateJobdetail(path) {
	
	getDataByAjaxRequest(path, generateJobdetailHtml);
}

/**
 * generate jobsimilar html
 * 
 * @param	string	data
 * @param	string	status
 * @param	string	request
 * @return void
 */
function generateJobsimilarHtml(data, status, request) {
	
	if ($("#handle_jobsimilar").length != 0) {
		
		if (data != null && data.length != 0) {
			
			$("#jobsimilar").toggleClass("ajaxloader");
			$("#jobsimilar").append(data);
			
		} else {
			
			$("#main_sidebar_jobsimilar").remove();
		}
	}
}

/**
 * generate jobfeed html
 * 
 * @param	string	data
 * @param	string	status
 * @param	string	request
 * @return void
 */
function generateJobfeedHtml(data, status, request) {
	
	if ($("#handle_jobfeed").length != 0) {
		
		if (data != null && data.length != 0) {
			
			$("#jobfeed").toggleClass("ajaxloader");
			$("#jobfeed").append(data);
			
		} else {
			
			$("#content_header_joboffer_feed_box").remove();
		}
	}
}

/**
 * generate jobcontact html
 * 
 * @param	string	data
 * @param	string	status
 * @param	string	request
 * @return void
 */
function generateJobcontactHtml(data, status, request) {
	
	if ($("#handle_jobcontact").length != 0) {
		
		if (data != null && data.length != 0) {
			
			$("#jobcontact").toggleClass("ajaxloader");
			$("#jobcontact").append(data);
			
		} else {
			
			$("#content_header_joboffer_contact_box").remove();
		}
	}
}

/**
 * modify error view
 * 
 * @return void
 */
function styleErrorView() {
	
	var elementSelector = "input[type='text'], input[type='file'], input[type='password'], select, textarea";
	
	$(".error_img").next(elementSelector).toggleClass('errorborder');
	
	var errorTxt = $("table td.error_text").html();
	
	$("table td.error_text").parents("table:first").replaceWith('<div class="error_message">' + errorTxt + '</div>');
	
	removeErrorSign();
}

/**
 * generate jobdetail html
 * 
 * @param	string	data
 * @param	string	status
 * @param	string	request
 * @return void
 */
function generateJobdetailHtml(data, status, request) {
	
	if ($("#handle_jobdetail").length != 0) {
		
		if (data != null && data.length != 0) {
			
			$("#jobdetail").toggleClass("ajaxloader");
			$("#jobdetail").append(data);
			
		} else {
			
			$("#content_header_joboffer_detail_box").remove();
		}
	}
}

/**
 * replacing ArrayTable with RealTable
 * 
 * @return void
 */
function replaceArrayTableHtml() {
	
	if ($("table td.list_head").length == 0) {
		
		return;
	}
	
	var arrayTable = $("table td.list_head:first").parents("table:first");
	
	arrayTable.attr('class', 'real_table');
	arrayTable.attr('id', 'array_table');
	arrayTable.removeAttr('width');
	arrayTable.removeAttr('border');
	arrayTable.removeAttr('cellpadding');
	arrayTable.removeAttr('cellspacing');
	$("td", arrayTable).removeAttr('style');
	$("td", arrayTable).removeAttr('align');
	$("td", arrayTable).removeAttr('valign');
	$("td", arrayTable).removeAttr('colspan');
	
	$("td", arrayTable).each(function() {
		
		if ($("table", this).length != 0) {
			
			$("table", this).replaceWith($("table a", this));
		}
	});
	
	var theadRow = arrayTable.find("tr:first");
	var thead = theadRow.html().replace(/<td/g, "<th").replace(/<\/td>/g, "</th>");

	theadRow.remove();
	
	arrayTable.prepend("<thead><tr>" + thead + "</tr></thead>");

	$("th a img", arrayTable).remove();
}

/**
 * replacing MessageTable with DivMessage
 * 
 * @return void
 */
function replaceMessageTableHtml() {
	
	if ($("table .hilite_text_blau").length == 0
		&& $("table .error_text").length == 0) 
	{		
		return;
	}
	
	if ($("table .hilite_text_blau").length != 0) {
			
		var messageTable = $("table .hilite_text_blau").parents("table:first");
		messageTable.replaceWith('<div class="success_message">' + $("table .hilite_text_blau:first").html() + '</div>');
		
	} else {
		
		var messageTable = $("table .error_text").parents("table:first");
		messageTable.replaceWith('<div class="error_message">' + $("table .error_text:first").html() + '</div>');
	}
}

/**
 * check filter for display reset link
 * 
 * @return void
 */
function checkSearchReset() {
	
	if ($("#volltext").attr('value') != ''
		|| $("#countr option:selected").attr('value') != ''
		|| $("#taetigkeit option:selected").attr('value') != ''
		|| $("#aufgabengebiet option:selected").attr('value') != '')
	{		
		$("#reset_filter").toggleClass('displaynone');
	}
	
}

/**
 * removing html brakes
 * 
 * @return void
 */
function removeBrakes() {
	
	$("div.norm_text br:not(p br, .inner_content br)").remove();
}

/**
 * style headline
 * 
 * @param	string	menuID
 * @return void
 */
function styleHeadline(menuId) {
	
	if ($("h1").length == 0 
		&& $("h2").length != 0 
		&& $("#my_data").length == 0
		&& $("#my_data").length == 0
		&& $("#application_overview").length == 0)
	{
		$("h2:first").replaceWith('<h1 id="title_h1">' + $("h2:first").html() + '</h1>');
	}
	
	if ($("h1 ins.stellen_titel").length == 1) {
		
		$("#title_h1").toggleClass('h1_smaller');
	}
}

/**
 * removing toTopButton in Popup
 * 
 * @return void
 */
function removeTopButton() {
	
	$("#footer_links .css_button:last").remove();
}

