var t_current;
var t_next;
var folder = '/app/uploads';

function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
 
     return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}		
		
function fadeInNext() {
	$(t_next).find("img").css("left", "20px");
	if ($(t_next).parent().attr("id") == "testimonial-big") {
		$(t_next).find(".testimonial-bubble").css("left", "-270px");
	}else {
		$(t_next).find(".testimonial-bubble").css("left", "20px");
	}
	
	$(t_next).fadeIn();
	$(t_next).find("img").animate({left: '-=20'});
	$(t_next).find(" .testimonial-bubble").delay(200).animate({left: '-=20'});
	
	t_current = t_next;
	if ( $(t_next).next().length != 0 ) t_next = $(t_next).next();
	else t_next = $(t_next).parent().find(".testimonial-single:eq(0)");	
}

function changeTestimonial() { 
	$(t_current).find("img").animate({left: '-=20'});
	$(t_current).find(".testimonial-bubble").delay(200).animate({left: '-=20'});
	$(t_current).fadeOut(800, fadeInNext);
}

function setTestimonialInterval() {

	var parent = false;
	if ($("#testimonial-big").length != 0)  {
		parent = "#testimonial-big";
	}else if ($("#testimonial-small").length != 0) parent = "#testimonial-small";
	
	if (parent) {
		var i = GetRandom(0, $(parent+" .testimonial-single").length-2);
		t_current = $(parent+" .testimonial-single:eq("+i+")");
		t_next = $(t_current).next();
	}
	
    var i = window.setInterval("changeTestimonial()", 10000);
    changeTestimonial();
}

$(document).ready(function(){

    // HEADER
    /*$("#claim").css("opacity", 0).animate({
        left: '-=40',
        opacity: 1,
        filter: ''
    }, 600);*/
		
	if (show_claimani) {
		$("#claim #claim_part01").css("opacity", 0).animate({ left: '-=40', opacity: 1, filter: ''}, 1200);
		$("#claim #claim_part02").css("opacity", 0).delay(1200).animate({ left: '-=40', opacity: 1, filter: ''}, 1200);
		$("#claim #claim_part03").css("opacity", 0).delay(2400).animate({ left: '-=40', opacity: 1, filter: ''}, 1200);
		
		// TESTIMONIALS
		var t = window.setTimeout("setTestimonialInterval()", 3600);
		
	}else {
		$("#claim #claim_part01, #claim #claim_part02, #claim #claim_part03").css("left", "-40px");
		
		// TESTIMONIALS		
		var t = window.setTimeout("setTestimonialInterval()", 1500);
	}

    // JOBS
    /*$("table.table-jobs a").live("click", function(e) {
        e.preventDefault();
        popupShow(e.target);
    });*/
    
    $(".tabbar li").each(function(e) {
        if(window.location.pathname == $(this).find("a").attr("href")) {
            $(this).addClass("active");
        }
    });
	
	var i = $('#file_upload').attr('rel');
	console.log(i);
	switch (parseInt(i)) {
		case 0 : folder = '/app/uploads/eigene_jobs'; break;
		case 1 : folder = '/app/uploads/aschaffenburg'; break;
		case 2 : folder = '/app/uploads/rosenheim'; break;
	}
	
    $('#file_upload').uploadify({
        'uploader'  : '/app/frontend/default/uploadify/uploadify.swf',
        'script'    : '/app/frontend/default/uploadify/uploadify.php',
        'cancelImg' : '/app/frontend/default/uploadify/cancel.png',
        'folder'    : folder,
        'buttonImg' : '/app/frontend/default/img/form_button_upload.jpg',
        'width'     : 170,
        'height'    : 27,
        'auto'      : true,
        'multi'     : true,
        'removeCompleted':false,
        'onComplete': function(event, ID, fileObj, response) {
            $("form.appform").append('<input type="hidden" name="uploads[]" value="'+response+'" />');
        }
    });

    // JETZT BEWERBEN
    $("form.appform input").focus(function() {
        $("form.appform label[for='"+$(this).attr("id")+"']").addClass("active");
        $("form.appform label[for='"+$(this).attr("id")+"']").removeClass("error");
        $(this).removeClass("error");
    });
    $("form.appform input").blur(function() {
        $("form.appform label[for='"+$(this).attr("id")+"']").removeClass("active");
    });

    $("form.appform").submit(function() {
        var errors = new Array();
        $("form.appform input").each(function() {
            switch($(this).attr("rel")) {
                case "str":
                    if ($(this).val() == "") {
                        errors.push($(this).attr("id"));
                    }
                    break;
                case "plz":
                    if ($(this).val().length != 5 || String(Number($(this).val())) != $(this).val()) {
                        errors.push($(this).attr("id"));
                    }
                    break;
                case "email":
                    var reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)(\\@)([a-zA-Z0-9\\-\\.]+)(\\.)([a-zA-Z]{2,4})$');
                    if (!reg.test($(this).val())) {
                        errors.push($(this).attr("id"));
                    }
                    break;
            }
        });
        

        if (errors.length == 0) {
            return true;
        } else {
            for (var i in errors) {
                $("form.appform label[for='"+errors[i]+"']").addClass("error");
                $("form.appform input[id='"+errors[i]+"']").addClass("error");
            }
            return false;
        }

    });


    // POPUP
    $("#popup .close").click(function () {
        popupClose();
    });
    function popupShow(content) {
        $("#popup-outer .popup-inner").load(content + " #content-lower-left *", function() {
            $("#popup-outer").fadeIn(300);
            Cufon.refresh();
        })
    }
    function popupClose() {
        $("#popup-outer").fadeOut(300);
    }

});
