var debug = true;

//Init FirebugConsole
if(window.loadFirebugConsole) {
    window.loadFirebugConsole();
}

//DEBUG-function
function DEBUG(string) {
    if(window.loadFirebugConsole && debug) {
        console.log(string);
    }
}


(function($){
    var dw = {

        preview: function() {

            $('form').find('input').bind('keyup', function(){
                switch ($(this).attr('id')) {
                    case "InputFirstname":
                        $('#PreviewFirstname').text($(this).val());
                        break;
                    case "InputLastname":
                        $('#PreviewLastname').text($(this).val());
                        break;
                    case "InputCity":
                        $('#PreviewCity').text($(this).val());
                        break;
                }
            })

        },



        send: function() {

        },
        init: function() {
            this.events();
            this.preview();
        },

        events: function() {
            $('.participate a').bind('click', function() {
                transparentLayer.open();
                $('#Layer').show();
                
                // check for ie6 via browser support detection, cause ie6 doesnt suppoert css "position:fixed" 
                if(!!(window.attachEvent && !window.opera) && typeof window.XMLHttpRequest == "undefined"){
                    
                    $("#Layer").css({"position": "absolute", "top": $(document).scrollTop() + 100});
                    
                    $(window).bind("scroll", function() {
                        $("#Layer").css("top", $(document).scrollTop() + 100);
                    });
                    
                    
                    $(window).bind("resize", function() {
                        $("#Layer").css("top", $(document).scrollTop() + 100);
                    });
                    
                    
                }else{
                    $("#Layer").css({"position": "absolute", "top": $(document).scrollTop() + Math.ceil($(window).height() / 2) - ($("#Layer").height() / 2)});
                    
                    $(window).bind("resize", function() {                       
                        $("#Layer").css("top", $(document).scrollTop() + Math.ceil($(window).height() / 2) - ($("#Layer").height() / 2));
                    });
                }
                
                $('#Layer a.close, #Overlay').bind('click', function() {
                    $('#Layer').hide();
                    transparentLayer.close();
                    return false;
                });

                return false;
            });

            $('.sub a').bind('click', function() {
                var link = $(this);
                $("#participants").css({'height' : '100%'});
                $('#participants').slideDown();
                link.hide();

                $('#participants .close').bind('click', function() {
                    $("#participants").css({'height' : '14px'});
                    $('#participants').removeClass('open');
                    link.show();
                    return false;
                });

                $('#participants').addClass('open');

                return false;
            });
        }
    };

    $(document).ready( function(){
        dw.init();
    });

})(jQuery);


$(document).ready( function(){
    $('#TeaserList').find('textarea').click(function() {
        $(this).select();
    });
});


function SetSupporterCount() {
    $.ajax({
        url: "/index/count",
        cache: false,
        type: "GET",
        dataType: 'html',
        success: function(count){
            DEBUG(count);
            $('#SupporterCount').text((count*1));
            setTimeout(function(){SetSupporterCount();}, 15000);
        }
    });
}


var transparentLayer = {

    pageWidth: false,
    pageheight: false,
    windowWidth: false,
    windowHeight: false,
    init: function () {
        scrollWidth = document.getElementsByTagName('body')[0].offsetWidth;
        scrollHeight = document.getElementsByTagName('body')[0].offsetHeight;
        documentWidth = document.getElementsByTagName('body')[0].scrollLeft;
        documentHeight = document.getElementsByTagName('body')[0].scrollTop;
        if(window.innerWidth) {
            this.windowWidth =  window.innerWidth;
            this.windowHeight = window.innerHeight;
        }
        else {
            this.windowWidth =  document.body.clientWidth;
            this.windowHeight = document.body.clientHeight;
        }

        if(document.documentElement.scrollHeight) {
            this.pageHeight = document.documentElement.scrollHeight;
        }
        else if(document.body.scrollHeight) {
            this.pageHeight = document.body.scrollHeight;
        }

        this.pageHeight = $(document).height();
        //this.pageWidth = '100%'; //document.getElementsByTagName('body')[0].offsetWidth - sL;
        //this.pageHeight = document.getElementsByTagName('body')[0].offsetHeight - sT;
    },



    open: function () {

        if(document.getElementById('Overlay')) {
            this.init();
            $('#Overlay').css({
                'width' : '100%',
                'height' : this.pageHeight + 'px',
                'z-index' : 99,
                'visibility' :'visible'
            });
        }
    },

    close: function () {
        if(document.getElementById('Overlay')) {
            $('#Overlay').css({
                'visibility' : 'hidden'
            });
        }
    },

    debug: function () {
        alert(this.pageWidth + " x " + this.pageHeight);
    }
};


var loading = {

	img: '<div id="ajaxLoderAnimation" style="position:absolute;width:128px;height:15px;bottom:100px;left:400px;z-index:20000;"><img src="/layout/images/ajax-loader.gif" /></div>',

	open: function(selector) {
                loading.close(); // für alle Fälle
		$(selector).append(this.img);
		//y = ($(document).scrollTop() - Math.ceil($('div#ajaxLoderAnimation').height()/2)  + ($(window).height()/2));
		//x = ($(document).width()/2) -  Math.ceil($('div#ajaxLoderAnimation').width() / 2);
                //$('div#ajaxLoderAnimation').css({top: y + 'px',left: x +'px', visibility:'visible'});

	},

	close: function() {
                $('div#ajaxLoderAnimation').remove();

	}


}
