$(document).ready(function () {

    if ((screen.width <= 1024) && (screen.height <= 768)) {
        $("#content-box").css({ float: "none", marginBottom: "15px" });
        $("#box1").css({ marginRight: "15px" });
    }


    // check for what is/isn't already checked and match it on the fake ones
    $("input:checkbox").each(function () {
        (this.checked) ? $("#fake" + this.id).addClass('fakechecked') : $("#fake" + this.id).removeClass('fakechecked');
    });
    // function to 'check' the fake ones and their matching checkboxes
    $(".fakecheck").click(function () {
        ($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
        $(this.hash).trigger("click");
        return false;
    });


    $('a#bewerben').click(function () {
        $('#bewerbungsformular').css({ display: "block" });
		$('.direkt_bewerben').css({ display: "none" });
        return false;
    });

    if (!$.browser.msie) {
        $('.content-roundedbox').css("-webkit-border-radius", "11px");
    }



});

function switchTab(parentID,contentID,activeID)
{
	var i = 1;	
	
	$('#'+parentID+activeID).addClass('aktive');
	
	$('#'+contentID+activeID).show();
	
	while($('#'+parentID+i).length != 0)
	{
		if( i != activeID && $('#'+parentID+i).hasClass("aktive"))
		{
			$('#'+parentID+i).removeClass('aktive');
			$('#'+contentID+i).hide();
		}
		i++;
	}	
}

function finishedPostingFile(fileName, serverName)
{
	var answer = serverName;
	if((answer.substr(0,1) == '0' && answer.length == 1) || (answer.substr(0,1) == '0' && answer.length < 32) || answer.substr(0,1) == "<")
	{
		//not uploaded
		alert('Die Datei ist zu gross oder in einem unerlaubten Format und wurde nicht hochgeladen.');
	}else{
		$('#files').append(
			$('<li>')
				.attr('id', 'fileList'+($('#files li').size()))
				.addClass('FileList')
				
				.append($('<button type="button">')
					.addClass('datei_delete')
					.click(function(){
						deleteFile($(this).parent().find('#elementIndex').val())
					})
				)
				
				.append($('<div>')
					.addClass('datei_delete_txt')
					.append(fileName)
				)
				
							
				
				.append($('<input type="hidden">')
					.attr('name', 'File'+($('#files li').size()))
					.val(serverName)
				)
				.append($('<input type="hidden">')
					.attr('name', 'FileName'+($('#files li').size()))
					.val(fileName)
				)
				.append($('<input type="hidden">')
					.attr('id','elementIndex')
					.val($('#files li').size())
				)
		);
		
	}
	
	
	//cleanup
	setTimeout(
		function(){
			$('#loading').remove();
			working = false;
		},
		100
	);
	
}

function deleteFile(index)
{
	working = true;
	$.get('/search/deleteContactFile?_cf_nodebug=true', {'Filename':$('input[name=File'+index+']').val()},
		function(data)
		{
			$('#fileList'+index).remove();
			working = false;
		}
	)
}

$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
	};


$.fn.resizeImage = function(){
	        var maxWidth = $(this).width(); // Max width for the image
	        var maxHeight = $(this).height();       // Max height for the image
	        $(this).css("width", "auto").css("height", "auto"); // Remove existing CSS
	        $(this).removeAttr("width").removeAttr("height"); // Remove HTML attributes
	        var width = $(this).width();    // Current image width
	        var height = $(this).height();  // Current image height

	        if(width/maxWidth > height/maxHeight) {
	                // Check if the current width is larger than the max
	                if(width > maxWidth){
	                        var ratio = maxWidth / width;   // get ratio for scaling image
	                        $(this).css("width", maxWidth); // Set new width
	                        $(this).css("height", height * ratio);  // Scale height based on ratio
	                        height = height * ratio;        // Reset height to match scaled image
	                }
	        } else {
	                // Check if current height is larger than max
	                if(height > maxHeight){
	                        var ratio = maxHeight / height; // get ratio for scaling image
	                        $(this).css("height", maxHeight);   // Set new height
	                        $(this).css("width", width * ratio);    // Scale width based on ratio
	                        width = width * ratio;  // Reset width to match scaled image
	                }
	        }
	}
