// -- mod: Datei hinzugefügt

//Seitengröße an Fenstergröße anpassen
function setPageSize() {
	$('#mainContent').height(($(window).height() - $('#footer').outerHeight(true) - $('#header').outerHeight(true) - $('#mainContent').outerHeight(true) + $('#mainContent').height()) + 'px');
	$('#mainContent').width(($(window).width() - $('#leftContent').outerWidth(true) - $('#mainContent').outerWidth(true) + $('#mainContent').width()) + 'px');
	
	$('#categories').height('auto');
	var newCategoriesHeight = $(window).height() - $('#footer').outerHeight(true) - $('#loginHeader').outerHeight(true) - $('#categories').outerHeight(true) + $('#categories').height();
	if ($('#categories').height() > newCategoriesHeight) {
		$('#categories').height(newCategoriesHeight + 'px');
	} else {
		$('#categories').height($('#categories').height() + 'px');
	}
}

//Seitengröße an Fenstergröße anpassen
$(window).resize(function(){
	setPageSize();
});

// Called on page load
function setVisuals(isVeda, isArticle, isContactActive, isAuthenticated, catId)
{
	var width = $(window).width();
	
    if ($('#mainNewsSubArticles_data').length)
    {
		if ($('#ribbon_sub_invisdata').length && $('#ribbon_sub_invisdata').html() != '')
		{
			$('#ribbon_sub_background').html($('#ribbon_sub_invisdata').html());
		}
        if ($('#mainNewsSubArticles_data').html() != '')
        {
            $('#mainNewsSubArticles').html($('#mainNewsSubArticles_data').html());
        }
    }

    if (isArticle == 'true')
    {
		if ($('#ribbon_sub_invisdata').length && $('#ribbon_sub_invisdata').html() != '')
		{
			$('#ribbon_sub_background').html($('#ribbon_sub_invisdata').html());
		}
		
		// Bilder in Artikeln verkleinern falls erforderlich
		$('#mainContent img').each(function() {
			if ($(this).width() > 300 && ($(window).width() - $(this).offset().left < $(this).outerWidth(true))) {
				// Neue Breite = Fensterbreite - Bild_Linke_Position - Bildrand - Halber_MainContent_Rand
				var newWidth = $(window).width() - $(this).offset().left - ($(this).outerWidth(true) - $(this).width()) - (($('#mainContent').outerWidth(true) - $('#mainContent').width()) / 2);
				if (newWidth >= 300) {
					$(this).height($(this).height() * newWidth / $(this).width());
					$(this).width(newWidth);
				}
			}
		});
    }

    if (isContactActive == 'true')
    {
        setActive('contact', isVeda, isArticle, isContactActive, isAuthenticated, catId);
    }

	if ($('#importantArticleInvisContainer').length)
		if ($('#importantArticleInvisContainer').html() != '')
			$('#ribbon_important_article').css('display', 'inline');

	if ($('#toptenArticleInvisContainer').length)
		if ($('#toptenArticleInvisContainer').html() != '')
			$('#ribbon_famous_article').css('display', 'inline');

	if ($('#latestArticleInvisContainer').length)
		if ($('#latestArticleInvisContainer').html() != '')
			$('#ribbon_new_article').css('display', 'inline');

	$('#ribbon_article').css('display', 'inline');

	// In case someone takes a look at contact
	if (isContactActive != 'true')
		setActive('article', isVeda, isArticle, isContactActive, isAuthenticated, catId);
	
	// Table sortierbar machen, falls die Klasse "sortable" vergeben wurde.
	sortables_init();

	//Seitengröße an Fenstergröße anpassen
	setPageSize();

	// Scrollbars austauschen
	$('#categories, #mainContent').lionbars('dark', true, false, false);
	
	// Voting aktivieren
	if ($('#votingstars').length) {
		$('.voting').rating({
			callback: function(value, link){
				saveVoting('faq', $('#votingForm input[name=artikel]').attr('value'), value);
			}
		});
		$(function() {
			$('#submitcomment').click(function() {
				saveFormValues('savecomment', 'comment');
			});
			$('form#formValues').submit(function() { return false; });
		});
	}
	
	// Image-Overlay erstellen
	$('#mainContent a > img').each(function() {
		var img = $(this),
			a = img.parent(),
			div = a.parent(),
			title = '';
		
		if (a.attr('href') == img.attr('src')) {
			// Titel ermitteln und setzen
			if (typeof div.attr('class') != 'undefined' && div.attr('class').indexOf('veda_image_box_') > -1) {
				title = div.children('div[class=' + div.attr('class') + '_sub]').html();
			} else if (typeof img.attr('alt') != 'undefined' && img.attr('alt') != '') {
				title = img.attr('alt');
			} else if (typeof a.attr('title') != 'undefined' && a.attr('title') != '') {
				title = a.attr('title');
			}
			
			a.attr('title', $.trim(title));
			
			// Image-Effekt hinzufügen
			a.lightBox({
				containerResizeSpeed: 200,
				maxWidth: $('#vedapage').outerWidth(true) - $('#vedapage').outerWidth(true) / 5 - 20,
				maxHeight: $('#vedapage').outerHeight(true) - $('#vedapage').outerHeight(true) / 5 - 52, // 32px zusätzlich zum unteren Rand
				imageBtnClose: 'template/default/images/veda/lightbox-btn-close.gif'
			});
		}
	});
	
	// Anker nochmal anspringen
	var lHash = location.hash;
	
	if (lHash != '') {
		location.hash = '';
		location.hash = lHash;
	}
	
	// Anker bei Loginform setzen
	$('#loginForm > form').get(0).setAttribute('action', $('#loginForm > form').get(0).getAttribute('action') + lHash);
}

// Called on click or page load
function setActive(name, isVeda, isArticle, isContactActive, isAuthenticated, catId)
{
	// Reset everything
    $('#ribbon_article').attr('class', 'ribbon_tab');
    $('#ribbon_important_article').attr('class', 'ribbon_tab');
    $('#ribbon_famous_article').attr('class', 'ribbon_tab');
    $('#ribbon_new_article').attr('class', 'ribbon_tab');
    $('#ribbon_contact').attr('class', 'ribbon_tab');
	
    switch(name)
    {
        case 'contact':
            $('#ribbon_contact').attr('class', 'ribbon_tab_active');
        case 'article':
			if (name == 'article') {
				$('#ribbon_article').attr('class', 'ribbon_tab_active');
			}
            $('#ribbon_sub_background').css('display', 'block');

            if ($('#ribbon_background').length)
            {
                var finalText = '<div id="ribbon_article_main_div">';

                $('#ribbon_background').css('display', 'block');

                if (isVeda == 'true')
                {
                    finalText = finalText + '<div id="ribbon_editieren" class="ribbon_article_tab"><table class="ribbon_table_content"><tbody><tr class="ribbon_article_main_tr"><td><a href="admin/index.php?action=editentry" alt="Einen neuen FAQ-Eintrag erstellen" title="Einen neuen FAQ-Eintrag erstellen" onfocus="this.blur();"><div class="ribbon_button_hover"><img src="template/default/images/veda/document-text-32.png"><br>Erfassen</div></a></td>';

                    if ($('#ribbon_editieren_invisdata').length)
                    {
                        finalText = finalText + '<td><a alt="Den angezeigten FAQ-Eintrag bearbeiten" title="Den angezeigten FAQ-Eintrag bearbeiten" href="' + $('#ribbon_editieren_invisdata').html() + '" onfocus="this.blur();"><div class="ribbon_button_hover"><img src="template/default/images/veda/edit-32.png"><br>Bearbeiten</div></a></td>';
                    }

                    finalText = finalText + '</tr><tr class="ribbon_article_bottom"><td colspan="2">Pflege</td></tr></tbody></table></div>';
                }
				
                if ($('#ribbon_bereitstellung_invisdata').length)
                {
                    finalText = finalText + $('#ribbon_bereitstellung_invisdata').html();
                }

                if ($('#searchInvisContainer').length)
                {
                    finalText = finalText + $('#searchInvisContainer').html();
                }

				// Hauptkategorie ermitteln und setzen
				//imgsubName = $('#categories').find('img[src*=filled]:first').closest('tr').find('td:nth-child(2) a').html().replace(/.*VEDA /, '');
				
				finalText = finalText + '<div id="ribbon_float_right_div"><img src="template/default/images/veda/VEDA-Logo_png.jpg"></div></div>';

                $('#ribbon_background').html(finalText);
            }
            break;
        case 'important_article':
            $('#ribbon_important_article').attr('class', 'ribbon_tab_active');
            $('#ribbon_sub_background').css('display', 'block');

            if ($('#importantArticleInvisContainer').length)
            {
                $('#ribbon_background').css('display', 'block');
                $('#ribbon_background').html($('#importantArticleInvisContainer').html());
            }

            break;

        case 'new_article':
            $('#ribbon_new_article').attr('class', 'ribbon_tab_active');
            $('#ribbon_sub_background').css('display', 'block');

            if ($('#latestArticleInvisContainer').length)
            {
                $('#ribbon_background').css('display', 'block');
                $('#ribbon_background').html($('#latestArticleInvisContainer').html());
            }

            break;

        case 'famous_article':
            $('#ribbon_famous_article').attr('class', 'ribbon_tab_active');
            $('#ribbon_sub_background').css('display', 'block');

            if ($('#toptenArticleInvisContainer').length)
            {
                $('#ribbon_background').css('display', 'block');
                $('#ribbon_background').html($('#toptenArticleInvisContainer').html());
            }

            break;
    }
	
	//Seitengröße an Fenstergröße anpassen
	setPageSize();
}
