(function($){
    jQuery.valentinApps = {
        defaults: {
    		brennstoffe : ["gas", "oel", "holz", "strom"],
			brennstoff : 0,
			plusminus:[{name:"floor",id:"an",min:50,max:300},{name:"area",id:"akoll",min:1,max:50},{name:"angle",id:"angle_input",min:0,max:90},{name:"brenn",id:"brenn",min:0,max:5}],
			imgPath:"/img/"
        },
        pending: false,
        prepInput: function(){
            $('.integer').numeric();
            $('.real').numeric({
                allow: "., "
            });
            $.valentinApps.setListener();
        },
        init: function(params){
			
			
		  //$.valentinApps.defaults.plusminus=[$.valentinApps.defaults.area, $.valentinApps.defaults.angle, $.valentinApps.defaults.floor];
           
		    $('select, input').removeAttr("disabled");
            
		    $.extend($.valentinApps.defaults, params);
            $.valentinApps.setListener();
            
			//$.valentinApps.tipButtons();
            $.valentinApps.animateButtons();
            $.valentinApps.setWeather();
            $.valentinApps.prepInput();
			$.valentinApps.ie6();
           $('#angle_div,#brenn_div,select,input,#submit_div,#help_button,.up,.down').tooltip({
                track: false,
                showURL: false,
                opacity: 0.2
            });
		   
        },
		ie6:function(){
			if ($.browser.version==6 ){
			$('#erg_cont').addClass('resultIE6');
			}
		},
		
        setListener: function(){
            $('#region, #state').change($.valentinApps.setWeather);
            $('#az').change($.valentinApps.kompass);
            $('select, input,.up,.down').click(function(event){
                
                
                if ($("input[@name='l']:checked").val() === 'de') {

                    please='Bitte Berechnung starten';
                }
                else
                 {

                    please='please start the calculation';
                }

                $('#tipp_div').text(please).show();
                $('#erg_div').slideUp('slow');
            });
            $('.down').click($.valentinApps.subOne);
            $('.up').click($.valentinApps.addOne);
        },
        calculate: function(path){
            if ($.valentinApps.pending === false) {
                $.valentinApps.pending = true;
                $('#help_div').hide('');
				$('#tipp_div').hide();
                $('#loading').html("<img src='/calculation/pvonline/img/loading.gif'>").show();
                $('select, input').attr("disabled", "");
                var sys = $("form").serialize();
                $('select, input').attr("disabled", "disabled");
                $.ajax({
                    type: "POST",
                   // url: "/index.php/" + path,
				    url:  path,
                    data: sys,
                    success: function(msg){
                        $.valentinApps.showResult(msg);
						
						 $(this).css("color","red");

						
                    }
                });
            }
        },
        showResult: function(msg){
            $(':input').removeAttr("disabled");
            $('#loading').html("");
            $('#erg_cont').html(msg);//.slideDown('slow');
            $('#erg_div').slideDown('slow');
            $.valentinApps.pending = false;
        },
        tipButtons: function(){
            $.each($.valentinApps.defaults.plusminus, function(){
				var id="#" + this.name + "_div";
                var top = parseInt($(id ).css("top"), 10) + 0;
                if ('brenn' == this.name) {
                    top = top + 10;
                }
                var left = parseInt($(id).css("left"), 10) - 19;
                var position = ' style="top:' + top + 'px ;left:' + left + 'px" ';
                $(id).before('<div class="buttons" ' + position + '> <img width="18px"  id="' + this.name + '_up" src="/img/button_plus.gif" title="+1" class="up"><img id="' + this.name + '_down" width="18px" src="/img/button_minus.gif" title="-1" class="down"></div>');
                
            });
        },
        setWeather: function(){
            var cities = "country=DEU&region=" + $('#region').val();
            $.ajax({
                type: "POST",
                //url: "/index.php/pv/getWeatherFiles",
				url: "/calculation/pv/getWeatherFiles",
                data: cities,
                success: function(msg){
                    $("#city").html(msg);
                }
            });
        },
        kompass: function(){
            var az = $("#az").val();
            var src = "/calculation/pvonline/img/kompass_" + az + ".gif";
            $('#needle').attr("src", src);
            var azimut = {
                s: 0,
                sw: 45,
                w: 90,
                nw: 135,
                n: 180,
                se: -45,
                e: -90,
                ne: -135
            };
            $("#azimut").val(azimut[az]);
        },
        showError: function(id, value, message){
            $(id).css("color", "red");
            $(id).animate({
                "fontSize": "14px"
            });
            $(id).animate({
                "fontSize": "11px"
            });
            $(id).val(value);
        },
        subOne: function(){
            $("#" + this.id).attr("src", "/img/button_minus_cl.gif");
            var id = this.id.replace(/_down/, "_div");
            var value = -1;
            if ("brenn_div" == id) {
                $.valentinApps.setBrennstoff(value);
            }
            else {
                value = parseInt($("#" + id + "> :text").val(), 10) + value;
                $("#" + id + "> :text").val(value);
            }
            $.valentinApps.checkMinMax();
        },
        addOne: function(){
            $("#" + this.id).attr("src", "/img/button_plus_cl.gif");
            //var id= $("#"+this.id).parents("div").get(1).id;
            var id = this.id.replace(/_up/, "_div");
            var value = 1;
            if ("brenn_div" == id) {
                $.valentinApps.setBrennstoff(value);
            }
            else {
                value = parseInt($("#" + id + "> :text").val(), 10) + value;
                $("#" + id + "> :text").val(value);
            }
            $.valentinApps.checkMinMax();
        },
        checkMinMax: function(){
			$.each($.valentinApps.defaults.plusminus, function(){
				$.valentinApps.checkValues('#'+this.id,this.min,this.max);
				});
        },
        checkValues: function(id, min, max){
            $(id).css("color", "black");
            if ($(id).val() < min) {
                $.valentinApps.showError(id, min, 'minimale');
            }
            if ($(id).val() > max) {
                $.valentinApps.showError(id, max, 'maximale');
            }
        },
        setBrennstoff: function(value){
            $.valentinApps.defaults.brennstoff = $.valentinApps.defaults.brennstoff + value;
            if ($.valentinApps.defaults.brennstoffe.length <= $.valentinApps.defaults.brennstoff) {
                $.valentinApps.defaults.brennstoff = 0;
            }
            if (0 > $.valentinApps.defaults.brennstoff) {
                $.valentinApps.defaults.brennstoff= $.valentinApps.defaults.brennstoffe.length - 1;
            }
            $('#brennstoff_img').attr("src", $.valentinApps.defaults.imgPath + "brennstoff_" + $.valentinApps.defaults.brennstoffe[$.valentinApps.defaults.brennstoff] + ".jpg");
        },
        
        animateButtons: function(){
           /* $('.up').mouseover(function(){
                $("#" + this.id).attr("src", "/img/button_plus_ro.gif");
            });
            $('.up').mouseout(function(){
                $("#" + this.id).attr("src", "/img/button_plus.gif");
            });
            $('.up').click(function(){
                $("#" + this.id).attr("src", "/img/button_plus_cl.gif");
            });
            $('.down').mouseover(function(){
                $("#" + this.id).attr("src", "/img/button_minus_ro.gif");
            });
            $('.down').mouseout(function(){
                $("#" + this.id).attr("src", "/img/button_minus.gif");
            });*/
            $('#helpImg').click(function(){
                $("#helpImg").attr("src", "/calculation/pvonline/img/button_hilfe_cl.gif");
            });
            $('#helpImg').mouseover(function(){
                $("#helpImg").attr("src", "/calculation/pvonline/img/button_hilfe_ro.gif");
            });
            $('#helpImg').mouseout(function(){
                $("#helpImg").attr("src", "/calculation/pvonline/img/button_hilfe.gif");
            });
            $('#simImg').click(function(){
                $("#simImg").attr("src", "/calculation/pvonline/img/button_simulation_cl.gif");
            });
            $('#simImg').mouseover(function(){
                $("#simImg").attr("src", "/calculation/pvonline/img/button_simulation_ro.gif");
            });
            $('#simImg').mouseout(function(){
                $("#simImg").attr("src", "/calculation/pvonline/img/button_simulation.gif");
            });
        }
    }
})(jQuery);
