// default values in inputs from title-Tags
function inputDefaultText() {
    jQuery('input[type=text]').each(function() {
        // setting the defaultValue
        var defaultValue = jQuery(this).attr('title');
        jQuery(this).val(defaultValue);
        // check if defaultValue or not
        if (jQuery(this).val() != defaultValue) {
            jQuery(this).addClass('notDefault').removeClass('required');
        }
        // change the value and class on focus
        jQuery(this).focus(function() {
            if (jQuery(this).val() == defaultValue) {
                jQuery(this).val('').removeClass('required notDefault').addClass('focused');
            }
            if (jQuery(this).val() != defaultValue) {
                jQuery(this).removeClass('required').addClass('focused notDefault');
            }
        });
        // change the value and class on blur
        jQuery(this).blur(function() {
            if (jQuery(this).val() == '') {
                jQuery(this).val(defaultValue).removeClass('focused notDefault').addClass('required');
            } else {
                jQuery(this).removeClass('focused').addClass('notDefault');
            }
        });
    });

    jQuery('form').submit(function() {
        jQuery('input[type=text]').each(function() {
            var defaultValue = jQuery(this).attr('title');
            if (jQuery(this).val() == defaultValue) {
                jQuery(this).val('');
            }
        });
    });
}


function initFacebookFeeds() {

    // Anzahl der Feeds
    var feedCount = 2;

    // Feeds auslesen
    FB.api('/pluspol/feed', {date_format: 'U', limit: feedCount}, function(response) {

                var maxTitleLength = 34;
                var maxTextLength = 300;
//		var maxPictureHeight = 200;
                var feedData = new Array();

                if (response['data']) {
                    for (var i = 0; i < response['data'].length; i++) {
                        data = response['data'][i];
                        if (!data.name) {
                            data.name = '';
                        }
                        else if (data.name.length > maxTitleLength - 3) {
                            data.name = data.name.substr(0, maxTitleLength) + '...';
                        }
                        if (!data.message) {
                            data.message = '';
                        }
                        if (data.message.length > maxTextLength) {
                            data.message = data.message.substr(0, maxTextLength - 3) + '...';
                        }

                        feedData.push({
                            id: 'facebook_picture_' + i,
                            date: dateFormat(data.updated_time * 1000, 'dd. mmmm "um" HH:MM'),
                            title: data.name,
                            text: data.message,
                            link: 'http://www.facebook.com/pluspol?sk=wall',
                            image: data.picture
                        });
                    }
                }

                /*
                 * Feeds an jQuery-Template �bergeben
                 * jQuery-Template wurde auf Grund der Validierung in eine externe Datei ausgelagert "js/templates/facebook_feed_template.js"
                 */
                jQuery.get('http://www.brandevangelist.de/templates/brandevangelist/js/templates/facebook_feed_template.js', function (template) {
                    jQuery.tmpl(template, feedData).appendTo("#fbFeedWrapper");
//			resizePictures(feedCount, maxPictureHeight); // Bild auf maximale H�he beschr�nken
                });

            });

}

function resizePictures(numPictures, maxHeight) {

    var height = 0;
    var width = 0;

    for (var i = 0; i < numPictures; i++) {

        height = jQuery('#facebook_picture_' + i).attr('height');
        width = jQuery('#facebook_picture_' + i).attr('width');

        if (height > maxHeight) {
            width = Math.round(maxHeight * width / height);
            height = maxHeight;

            jQuery('#facebook_picture_' + i).attr({
                height: height,
                width: width
            });
        }
    }
}


jQuery(function() {
    jQuery(window).bind('load', function() {
        initFacebookFeeds();
    });
});

jQuery(document).ready(function() {
    inputDefaultText();
});

