/**
 * @author Innovatics GmbH © 2009
 */

function removeAllSwitchClasses()
{
	$("a#switch_tab1").removeClass('first_1');
	$("a#switch_tab1").removeClass('first_2');
	$("a#switch_tab1").removeClass('first_3');
	$("a#switch_tab2").removeClass('second_1');
	$("a#switch_tab2").removeClass('second_2');
	$("a#switch_tab2").removeClass('second_3');
	$("a#switch_tab3").removeClass('third_1');
	$("a#switch_tab3").removeClass('third_2');
	$("a#switch_tab3").removeClass('third_3');
}

function registerToggle()
{
	var check1 = $('input#check1').is(':checked');
	var check2 = $('input#check2').is(':checked');
	
	if (check1 && check2) {
		$("div#register").slideDown();
	}
	else {
		$("div#register").slideUp();
	}
}

function loadMainContent(currentElement)
{
    var child = currentElement.find('a:first');
    if (child.hasClass('hidden_content_link')) {
        jQuery('#main_content_loader').show();
        jQuery.get(child.attr('href'),function(data) {
			jQuery('#home_main_content').empty();
            jQuery('#home_main_content').attr('innerHTML', data);
            jQuery('#main_content_loader').hide();
		});
    }
}

$(document).ready(function()
{
    $.ajaxSetup({
        cache: false
    });

	$('input[type="text"]').focus(function()
	{
		$(this).addClass("focusField");
	});
	
	
	$('input[type="text"]').blur(function()
	{
		$(this).removeClass("focusField");
	});
	
	$('input[type="password"]').focus(function()
	{
		$(this).addClass("focusField");
	});
	
	
	$('input[type="password"]').blur(function()
	{
		$(this).removeClass("focusField");
	});
	
	$('textarea').focus(function()
	{
		$(this).addClass("focusField");
	});
	
	
	$('textarea').blur(function()
	{
		$(this).removeClass("focusField");
	});
	 
	// Content Switch Startseite
	$("a#switch_tab1").click(function()
	{
		$("div#tab2").hide();
		$("div#tab3").hide();
		$("div#tab1").show();
		
		removeAllSwitchClasses();
		
		$("a#switch_tab1").addClass('first_1');
		$("a#switch_tab2").addClass('second_1');
		$("a#switch_tab3").addClass('third_1');
		
		return false;
	});
	
	$("a#switch_tab2").click(function()
	{
		$("div#tab1").hide();
		$("div#tab3").hide();
		$("div#tab2").show();
		
		removeAllSwitchClasses();
		
		$("a#switch_tab1").addClass('first_2');
		$("a#switch_tab2").addClass('second_2');
		$("a#switch_tab3").addClass('third_2');
				
		return false;
	});
	
	$("a#switch_tab3").click(function()
	{
		$("div#tab2").hide();
		$("div#tab1").hide();
		$("div#tab3").show();
		
		removeAllSwitchClasses();
		
		$("a#switch_tab1").addClass('first_3');
		$("a#switch_tab2").addClass('second_3');
		$("a#switch_tab3").addClass('third_3');
		
		return false;
	});
	
	// Status Nachricht anzeigen
	$('#message').slideDown('slow').animate({opacity: 1.0}, 4000).slideUp('slow');

    // navigation footer home
    $('#footer_navigation_prev').click(function() {
       var currentElement = jQuery('div.footer_navigation_tab.active:first');
       var previousElement = currentElement.prev('div');
       var prevTabOffset = previousElement.offset();
       //alert('prev');
       if (previousElement.hasClass('footer_navigation_tab')) {
           jQuery('#footer_navigation_home').stop();
           currentElement.removeClass('active');
           previousElement.addClass('active');

           var prevSelectElement = jQuery('#footer_navigation_prev');
           var prevOffset = prevSelectElement.offset();
           var prevWidth = prevSelectElement.width();

           if (prevTabOffset.left < prevOffset.left+prevWidth) {
               jQuery('#footer_navigation_home').animate({left: '+='+(prevOffset.left+prevWidth-prevTabOffset.left+10)})
           }

           jQuery('#home_header_title').attr('innerHTML',previousElement.attr('innerHTML'));
           loadMainContent(previousElement);
       }
    });

    $('#footer_navigation_prev').unbind('hover').hover(
        function() {jQuery(this).addClass('hover');},
        function() {jQuery(this).removeClass('hover');}
    );

    $('#footer_navigation_next').click(function() {
       var currentElement = jQuery('div.footer_navigation_tab.active:first');
       var nextElement = currentElement.next('div');
       var nextTabOffset = nextElement.offset();
       //alert("next");
       if (nextElement.hasClass('footer_navigation_tab')) {
           jQuery('#footer_navigation_home').stop();
           currentElement.removeClass('active');
           nextElement.addClass('active');

           var nextSelectElement = jQuery('#footer_navigation_next');
           var nextOffset = nextSelectElement.offset();
           var nextWidth = nextSelectElement.width();

           if (nextTabOffset.left+nextElement.width() > nextOffset.left) {
               jQuery('#footer_navigation_home').animate({left: '-='+(nextTabOffset.left+nextElement.width() - (nextOffset.left-nextSelectElement.width()))})
           }

           jQuery('#home_header_title').attr('innerHTML',nextElement.attr('innerHTML'));
           loadMainContent(nextElement);
       }
    });

    $('#footer_navigation_next').unbind('hover').hover(
        function() {
        jQuery(this).addClass('hover');},
        function() {jQuery(this).removeClass('hover');}
    )

    $('.footer_navigation_tab').click(function() {
        var currentElement = jQuery(this);
        //alert(currentElement.attr('innerHTML'));
        if (!currentElement.hasClass('active')) {
            jQuery('.footer_navigation_tab').removeClass('active');
            currentElement.addClass('active');
            loadMainContent(currentElement);
            //jQuery('#footer_navigation_home').stop();
            var offset = currentElement.offset();

            var text = "cleft: "+offset.left+"; cwidth: "+currentElement.width();

            var nextSelectElement = jQuery('#footer_navigation_next');
            var nextOffset = nextSelectElement.offset();

            text += "\nnleft: "+nextOffset.left+"; nwidth: "+nextSelectElement.width();

            var prevSelectElement = jQuery('#footer_navigation_prev');
            var prevOffset = prevSelectElement.offset();
            var prevWidth = prevSelectElement.width();

            text += "\npleft: "+prevOffset.left+"; pwidth: "+prevSelectElement.width();

            //alert(text);

            if (offset.left+currentElement.width() > nextOffset.left) {
               jQuery('#footer_navigation_home').animate({left: '-='+(offset.left+currentElement.width() - (nextOffset.left-prevWidth))})
            }
            else if (offset.left < prevOffset.left+prevWidth) {
               jQuery('#footer_navigation_home').animate({left: '+='+(prevOffset.left+prevWidth-offset.left+10)});
            }

            jQuery('#home_header_title').attr('innerHTML',currentElement.attr('innerHTML'));
        }
    });
    $('.footer_navigation_tab').unbind('hover').hover(
        function() {jQuery(this).addClass('hover');},
        function() {jQuery(this).removeClass('hover');}
    
    );
});
