$(document).ready(function(){

    currentshownsocialcounter = 1;
    productfilecounter = hideUploadFormApplicationAndGiveNumberOfPossibleUploads();
    socialcounter = hideSocialnetworksAndGiveNumberOfPossibleUploads();
    currentshownfilecounter = 1;
    
    if (productfilecounter > 0) {
        lastfileid = productfilecounter+1;
        jQuery('div.form_file_productfile'+lastfileid+'_description').after('<div class="html-input"><div class="more-link"><a href="#" onclick="return addUploadLine();">weitere Datei hinzufügen</a></div></div>');
    }

    if (socialcounter > 0) {
        lastsocial = socialcounter+1;
        jQuery('div.form_socialprofile'+lastsocial+'_username').after('<div class="html-input"><div class="more-link"><a href="#" onclick="return addSocialnetworkLine();">weiteres Netwerk hinzufügen</a></div></div>');
    }
    
    
    jQuery('div.form_become_partner input[type=radio]').change(function() {
        checkForShowingNoticeNotBecomingPartner();
    });
    
    jQuery('div.form_member_in_industrieverband input[type=radio]').change(function() {
        showHideIndustrieVerband();
    });
    
    /* Application */
    showHideIndustrieVerband();
    
    
    if (jQuery('div.banner-overlay').length > 0) {
        jQuery('body').append('<div id="overlay-div"></div>');
        
        jQuery('div#overlay-div').css('display', 'none');
        jQuery('div#overlay-div').css('height', jQuery('html').height()); //jQuery(document).height);
        jQuery('div#overlay-div').css('width', jQuery('body').width()); // jQuery(document).width);
        jQuery('div#overlay-div').css('background', '#000');
        jQuery('div#overlay-div').css('position', 'absolute');
        jQuery('div#overlay-div').css('left', 0);
        jQuery('div#overlay-div').css('opacity', 0.6);
        jQuery('div#overlay-div').css('top', 0);
        jQuery('div#overlay-div').css('z-index', 10000);
        
        jQuery('div#overlay-div').delay(1000).fadeIn().delay(1500).fadeOut();
    }
    
    checkForShowingNoticeNotBecomingPartner();
    
    if (jQuery('div#quote div.loading').length > 0) {
        getRandomJuryQuote();
    }
    
    $('[rel=tooltip]').HuberTooltip();
    $('[rel=tooltip_bottom]').HuberTooltip('bottom');
    
    
    
    $('#av_news ul.fader').innerfade({
            animationtype: 'slide',
            speed: 400,
            timeout: 7000,
            type: 'random',
            containerheight: '1em'
    });
    
    jQuery('li.mediapartner-listing img').mouseover(function() {
        grayscale.reset(jQuery(this));
    });
    
    jQuery('li.mediapartner-listing img').mouseout(function() {
        grayscale(jQuery(this));
    });


    if (!cookiesEnabled() && showCookiesNotice === true) {
        jQuery('div.enable_cookies').show();
    }
    
    jQuery('li.mediapartner-listing img').mouseover(function() {
        grayscale.reset(jQuery(this));
    });
    
    jQuery('li.mediapartner-listing img').mouseout(function() {
        grayscale(jQuery(this));
    });
    
    
});

// per default: do not show cookies-notices if they're disabled by user
var showCookiesNotice = false;

/* Application */
function showHideIndustrieVerband() {
    
    var currentValue = jQuery("div.form_member_in_industrieverband input[type=radio]:checked").val();
    if (currentValue < 1) {
        jQuery('div.form_name_industrieverband').hide();
    } else {
        jQuery('div.form_name_industrieverband').show();
    }
}

function checkForShowingNoticeNotBecomingPartner()
{
    var currentValue = jQuery("div.form_become_partner input[type=radio]:checked").val();
    if (currentValue < 1) {
        jQuery('div.notice_become_partner').fadeIn();
    } else {
        jQuery('div.notice_become_partner').hide();
    }
}
function hideUploadFormApplicationAndGiveNumberOfPossibleUploads()
{
    
    var productfilecounter = 0;
    
    for($i=2;$i<20;$i++) {
        jQuery('div.form_file_productfile'+$i+'_file, div.form_file_productfile'+$i+'_description').hide();
        if (jQuery('div.form_file_productfile'+$i+'_file').length > 0) {
            productfilecounter++;
        }
    }
    
    return productfilecounter;
    
}


function addUploadLine()
{
    
    jQuery('div.form_file_productfile'+(2+currentshownfilecounter)+'_file, div.form_file_productfile'+(2+currentshownfilecounter)+'_description').show();
    
    currentshownfilecounter++;
    if (productfilecounter < currentshownfilecounter ) {
        jQuery('div.html-input div.more-link').html('weitere Datei hinzufügen').addClass('disabled-more-link');
    }
    
    return false;
}



function cookiesEnabled()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;
 
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    { 
        document.cookie="testcookie";
        cookieEnabled = 
            (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return (cookieEnabled);
}


function getRandomJuryQuote()
{
    jQuery('div#quote div.loading').show();
    jQuery('div#quote div.quote').hide();
    
    jQuery.ajax({
        url: juryRandomAjaxUrl,
        success: function(jurymemberObj) {
            jQuery('div#quote div.loading').hide();
            jQuery('div#quote div.quote p em.name').html(jurymemberObj.name);
            jQuery('div#quote div.quote img').attr('src', jurymemberObj.img_src);
            jQuery('div#quote div.quote p.cite span').html(jurymemberObj.quote);
            jQuery('div#quote div.quote').show();
        }
    });
    
    return false;
}

function initRandomJuryQuote()
{
    jQuery.facebox({ajax: juryInitAjaxUrl, width: 715});
    return false;
}

function getFullJurymemberQuote(memberId) {
    jQuery.ajax({
        url: jurySpecialIdAjaxUrl + memberId,
        success: function(jurymemberObj) {
            jQuery('div#quote div.loading').hide();
            jQuery('div.juryrandom-popup div.popup-content h3').html(jurymemberObj.name);
            jQuery('div.juryrandom-popup div.popup-content img.profile').attr('src', jurymemberObj.img_src);
            jQuery('div.juryrandom-popup div.popup-content blockquote span.quote').html(jurymemberObj.quote);
            jQuery('div.juryrandom-popup div.popup-listing ul li').removeClass('current');
            jQuery('div.juryrandom-popup div.popup-listing ul li.jurymember_'+memberId).addClass('current');
        }
    });
}


window.onload = function() {
    jQuery('li.mediapartner-listing img').each(function() {
        
        jQuery(this).fadeIn();
        
        imageHeight = this.clientHeight;
        divContainerHeight = jQuery(this).parents('li').height();        
        imagePaddingTop = Math.round((divContainerHeight-imageHeight)/2);

        if (imagePaddingTop > 0) {
            jQuery(this).css('padding-top', imagePaddingTop);
        }
        
        grayscale(jQuery(this));
        jQuery(this).parents('li').addClass('loaded');
        
    });
    
    jQuery('img.jurymember').each(function() {
        grayscale(jQuery(this));
    });
    
    
    
}

        /* Socialnetwork-Profiles */
        function hideSocialnetworksAndGiveNumberOfPossibleUploads()
        {

            var socialcounter = 0;
            var currentshownsocialcounter = 0;

            for($i=2;$i<25;$i++) {

                if (jQuery('div.form_socialprofile'+$i+'_username input').val() == '') {
                    jQuery('div.form_socialprofile'+$i+'_username, div.form_socialprofile'+$i+'_network').hide().addClass('hidden');
                } else {
                    currentshownsocialcounter++;
                }
                if (jQuery('div.form_socialprofile'+$i+'_username').length > 0) {
                    socialcounter++;
                }
            }

            return socialcounter;

        }


        function addSocialnetworkLine()
        {

            currentshownsocialcounter = 0;
            for($i=2;$i<25;$i++) {

                if (!jQuery('div.form_socialprofile'+$i+'_network').hasClass('hidden')) {
                    currentshownsocialcounter++;
                }
            }

            jQuery('div.form_socialprofile'+(1+currentshownsocialcounter)+'_username, div.form_socialprofile'+(1+currentshownsocialcounter)+'_network').show().removeClass('hidden');
            if (socialcounter < currentshownsocialcounter ) {
                jQuery('div.html-input div.more-link').html('weiteres Netwerk hinzufügen').addClass('disabled-more-link');
            }

            return false;
        }
