// +------------------------------------------------------------------------+
// | GLOBALS                                                                |
// +------------------------------------------------------------------------+
try {
    GLOBALS.ie6 == GLOBALS.ie7;
}
catch (eExc)
{
    var GLOBALS = {
        ie6: false,
        ie7: false
    };
}

var bandwidth = {
	
	iStart: 0,
	iEnd: 0,

	check: function ()
	{
		if ($.cookie('bgImage'))
		{
			$('body').css("background-image",$.cookie('bgImage'));
		}
		else
		{
		var s = new Date();
		bandwidth.iStart = s.getTime();
		jQuery.get("/media/redaktion/bilder/header/brands.jpg", null, function(){bandwidth.getBandwidth()});		
		}
	},
	
	getBandwidth: function ()
	{
		var e = new Date();
		bandwidth.iEnd = e.getTime();
		var iDiff = bandwidth.iEnd - bandwidth.iStart;	
		iDiff = iDiff / 1000;			
		if (iDiff > 2)
		{
			$.cookie("bgImage", $('body').css("background-image"));										
			$('#header_img').attr("alt",iDiff);
		}
		else
		{
			$.cookie('bgImage', null);
		}		
	}				
}

var DropDown = {

    /**
     * DropDown box stack.
     *
     * @var array
     */
    aBox : new Array(),

    /**
     * Document event observer registered or not.
     *
     * @var boolean
     */
    bDocumentEvent : false,

    /**
     * Push box to DropDown stock.
     *
     * @param  element _eLabel
     * @param  element _eItem
     * @return void
     */
    pushBox : function (_eLabel, _eItem)
    {
        $(_eLabel).bind('click',DropDown.check);

        DropDown.aBox.push(
            {
                label  : _eLabel,
                item   : _eItem,
                active : false
            }
        );
    },

    /**
     * Returns the stock-index of given box.
     *
     * @param  element         _eLabel
     * @return integer|boolean
     */
    getIndex : function (_eLabel)
    {
        var iLength = DropDown.aBox.length;
        var iIndex  = 0;

        while (iLength--)
        {
			if (DropDown.aBox[iIndex++].label == $(_eLabel).get(0))
			{
				return iIndex;
			}
        }

        return false;
    },

  /**     
     *
     * @param  object _oEvent Fired event
     * @return void
     */
    check : function (_oEvent)
    {
        var eTarget = $(this);
        var iIndex  = DropDown.getIndex(eTarget);

        if (iIndex && false == DropDown.aBox[iIndex-1].active)
        {
            DropDown.close(null);
            _oEvent.preventDefault();
			_oEvent.stopPropagation();

            DropDown.show(iIndex-1);
        }
    },

    /**
     * Show DropDown items
     *
     * @param  integer _iIndex
     * @return void
     */
    show : function (_iIndex)
    {
        // ~~~~~~~~~~~~~~~~~
        // Show DropDown box
        // ~~~~~~~~~~~~~~~~~
        DropDown.aBox[_iIndex].active = true;

        $(DropDown.aBox[_iIndex].item).css({display:'block'});

        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // If not already registered, register document event observer
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if (!DropDown.bDocumentEvent)
        {
            DropDown.bDocumentEvent = true;

            $(document).bind('click', DropDown.close);
        }
    },

    /**
     * Hide all DropDown items
     *
     * @param  object _oEvent
     * @return void
     */
    close : function (_oEvent)
    {
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // Unregister document event observer
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        DropDown.bDocumentEvent = false;

        $(document).unbind('click', DropDown.close);

        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // Run all boxes to close them
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        var iLength = DropDown.aBox.length;
        var iIndex  = 0;

        while (iLength--)
        {
            if (false == DropDown.aBox[iIndex++].active)
                continue;

            DropDown.aBox[iIndex-1].active = false;

            $(DropDown.aBox[iIndex-1].item).css({display : 'none'});
        }
    }
}

// +------------------------------------------------------------------------+
// | Page                                                                   |
// +------------------------------------------------------------------------+
var Page = {

    /**
     * @var integer
     */
    iScope : 0,

    /**
     * @var element
     */
    eSection : null,

    /**
     * Initialize website.
     *
     * @return void
     */
    init : function ()
    {
        GLOBALS['page'] = env.getPageSize();

        Behaviour.apply();
		/* Page.scrolling nur bei screen.css ausfuehren (bei print.css ausgeblendet) */
		
		var eFooter = $('#footer:visible');
		if (eFooter.length != 0)
			{
				Page.scrolling();		
			}
			else
			{
				/*$('#ContentBox').insertBefore('#container');*/
			}
		
        if (document.location.hash.match(/^#([a-z0-9_-]+)/i))
            Page.scrollTo(RegExp.$1);
    },
    
    /**
     * Open external links in a new window.
     *
     * @param  object _oEvent Fired event
     * @return void
     */
    externalLinks : function (_oEvent)
    {
        var oWindow = window.open(this.readAttribute('href'));
        oWindow.focus();

        if (_oEvent)
		{
            _oEvent.preventDefault();
			_oEvent.stopPropagation();
		}
    },

    /**
     * Window resize event handler.
     *
     * @return void
     */
    resize : function ()
    {
        GLOBALS.page = env.getPageSize();
		/* Page.scrolling nur bei screen.css ausfuehren (bei print.css ausgeblendet) */
		var eFooter = $('#footer:visible');
		if (eFooter.length != 0)
			{
				Page.scrolling();		
			}
			else
			{
				/*$('#ContentBox').insertBefore('#container');*/
			}
    },

    /**
     * Check page scrolling. Fix header and footer if neccessary.
     *
     * @return void
     */
    scrolling : function ()
    {
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // Best pal IE6 needs special treatment
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
		/*if (GLOBALS.ie6)
        {
            Page.scrollingIE6();
            return;
        }*/
		
		var eHtml      = document.getElementsByTagName('html')[0];
        		
        var eBody       = document.getElementsByTagName('body')[0];
		var eIdBody     = document.getElementById('body');
        var eFooter     = $('#footer');		
		var eSkirt		= $('#footerSkirt');
		var eContentBox = $('#ContentBox');
		var eVolltext   = $('#volltext');
		var eHeaderFix  = $('#headerfix');
		var eSideBar    = $('#rightSide');
				
		/*$(eHtml).addClass('fixItems');			
		$(eBody).addClass('fixItems');*/
		
			/* viewport bigger than header + body + footer */
		if(GLOBALS.page[3] > 630)
		{
			/* fix footer position, show "skirt", add margin-bottom to Content */
		
			$(eFooter).css({top:'609px'});	
			$(eSkirt).css({display:'block'});
			$(eSkirt).css({top:'630px'});					
					
			var iHeightSkirt = GLOBALS.page[3] - 630;
			$(eSkirt).height(iHeightSkirt);
			iHeightSkirt = iHeightSkirt - 16;
			$(eContentBox).css("margin-bottom", iHeightSkirt+'px'); 
			$(eSideBar).css("margin-bottom", iHeightSkirt+'px');
			if(eVolltext)
				$(eVolltext).css("margin-bottom", iHeightSkirt+'px');			
		}	        
		else
		{
			$(eFooter).css({top:''});
			$(eFooter).css({bottom:'0px'});		
			$(eSkirt).css({display:'none'});
			$(eContentBox).css("margin-bottom", '0px');			
		}		
		
		/*$('#header').insertBefore('#container');
		$('#footer').insertBefore('#container');
		$('#footerSkirt').insertBefore('#container');*/
		if (GLOBALS.ie6)
		{
			$('#body_left').insertBefore('#container');
		}	
    },

    /**
     * Check page scrolling for IE6. Fix header and footer if neccessary.
     *
     * @return void
     */
    scrollingIE6 : function ()
    {
        var eHtml      = document.getElementsByTagName('html')[0];
        var eBody      = document.getElementsByTagName('body')[0];
        
		var eContentBox = $('#ContentBox');
		var eVolltext   = $('#volltext');
		
        var eFooter     = $('#footer');		
		var eSkirt		= $('#footerSkirt');
		
        var iScope = $('#container').height();
		
		$(eFooter).addClass('fixedJS');		
		
		$(eFooter).css({top:'609px'});	
		var iHeightSkirt = GLOBALS.page[3] - 630;
		$(eSkirt).height(iHeightSkirt);		
		$(eSkirt).css({display:'block'});
		$(eSkirt).css({top:'630px'});					
	
		$(eContentBox).css("margin-bottom", iHeightSkirt+'px'); 
		if(eVolltext)
		  $(eVolltext).css("margin-bottom", iHeightSkirt+'px');							
				
		$('#footer').insertBefore('#container');
		$('#footerSkirt').insertBefore('#container');
		$('#body_left').insertBefore('#container');
    },	
    
    /**
     * Scroll to element
     *
     * @param string
     * @return void
     */
    scrollTo : function (_sIdentifier)
    {
        var aOffset = Position.cumulativeOffset($(_sIdentifier));

        document.location.hash = _sIdentifier;

        if ($(document.getElementsByTagName('body')[0]).hasClass('fixItems'))
            window.scrollTo(0, aOffset[1] - 132);
    }
}


// +------------------------------------------------------------------------+
// | Quickfinder                                                            |
// +------------------------------------------------------------------------+

var Quickfinder = {

  switchLinks : function (_id)
  {
    eBereichLabel   = $('#quickfinderBereicheDropdown').find("span");
    eDropdownList   = $('#quickfinderLinks > *');
    aLinkDropdowns  = new Array ();

    for (var i = 0; i < eDropdownList.length; i++)
    {
      if (eDropdownList[i].nodeType == 1)
      {
        aLinkDropdowns.push (eDropdownList[i]);
      }
    }

    for (var i = 0; i < aLinkDropdowns.length; i++)
    {
      if ($(aLinkDropdowns[i]).attr("id") == _id)
      {
        $(aLinkDropdowns[i]).removeClass('hidden');
      }
      else
      {
        $(aLinkDropdowns[i]).addClass('hidden');
      }
    }
	
    eBereichLabel[0].firstChild.nodeValue = $('#'+_id+'_a').text();	
  }

}

// +------------------------------------------------------------------------+
// | Bildergalerie							       |
// +------------------------------------------------------------------------+


var Gallery = {

   /**
     * Switch image.
     *
     * @param  object _oEvent
     * @return void
     */
    switchImage : function (_oEvent)
    {
		_oEvent.preventDefault();
		_oEvent.stopPropagation();

        var eTarget     = $(this);
		var sIdentifier = '#'+eTarget.attr("rel");				
		var sIdentTexts = sIdentifier+'_texts';
        var eNavigation = eTarget.parent().parent();
        var aNavigation = eNavigation.find("a");		
		var iLength     = aNavigation.length;
        var iIndex      = 0;
		
		var eTexts		 = $(sIdentTexts).get(0);		
		var aTexts       = $(eTexts).find("div");
		var iLengthTexts = aTexts.length;
		var iIndexTexts  = 0;
				
        while (iLength--)
        {
            var eItem      = aNavigation.get(iIndex++);						
									
            if (eItem == eTarget.get(0) && $(eItem).hasClass('active'))
			{			
				var sIdentText = sIdentifier+$(eItem).attr("id");		
                return;
			}
			
            $(eItem).removeClass('active');
			
            if (eItem == eTarget.get(0))
			{
                $(eItem).addClass('active');
				var sIdentText = sIdentifier+$(eItem).attr("id");		
			}	
        }

        $(sIdentifier).get(0).src = eTarget.get(0);
		
		while (iLengthTexts--)
		{
			var eTextItem = aTexts.get(iIndexTexts++);			
			
			if('#'+$(eTextItem).attr("id") == sIdentText && $(eTextItem).hasClass('vis'))
			{
				return;
			}
			
			if ($(eTextItem).hasClass('vis'))
			{
				$(eTextItem).removeClass('vis');
				$(eTextItem).addClass('invis');				
			}
						
			if ('#'+$(eTextItem).attr("id") == sIdentText)
			{
				$(eTextItem).removeClass('invis');
				$(eTextItem).addClass('vis');						
			}	
		}
    }
}

// +------------------------------------------------------------------------+
// | Messen und Veranstaltungen                                               |
// +------------------------------------------------------------------------+

var Fairs = {
  
  switchYear : function (_iYear)
  {
    eDropdown = $('#fairYear').find('span:first');    
    eDropdown.html(_iYear);
  },
  
  switchMonth : function (_sID,_anker)
  {
    eDropdown = $('#fairMonth').find('span:first');
    eDropdown.html($('#'+_sID).text());
	eDropdown.attr("title",_anker);
  },
  
  jump : function (_sUrl)
  {
    iMonth = $('#fairMonth').find('span:first').attr("title"); 
    iYear = $('#fairYear').find('span:first').html();

    try
    {
		var sNode = "#" + iMonth + iYear;
        var aOffset = $(sNode).offset();
		document.location.hash = iMonth + iYear;
		$('#container').get(0).scrollTop = aOffset.top - 160;
    }
    catch (exc) {}
  }
  
}

// +------------------------------------------------------------------------+
// | Kontaktformular                                                                    |
// +------------------------------------------------------------------------+

var Contact = {
  
  switchBranch : function (_id)
  {
    eDropdown = $('#Branch').find('span:first');    
    eDropdown.html($('#'+_id).text());
	eDropdownInput = $('#Branch').find('input:first');    
    eDropdownInput.attr("value", $('#'+_id).text());
  },
  
  switchRecipient : function (_id,_sEmail)
  {
    eDropdown = $('#recipient').find('span:first');
    eDropdown.html($('#'+_id).text());	
	eDropdownInput = $('#recipient').find('input:first');    
    eDropdownInput.attr("value", _sEmail);
	$('#hidden_name').attr("value", $('#'+_id).text());
  }  
}

// +------------------------------------------------------------------------+
// | Page Content                                                                         |
// +------------------------------------------------------------------------+

var Content = {

	init : function() 
	{	
		if ($("#ContentBox").is(":hidden")) 
		{}
		else
		{
			if ($("#Teaser").is("div"))
			{
				$("#ContentBox").hide();
				$("span.moreTeaser").css("display", "block");
			}	
		}
	
	}

}

// +------------------------------------------------------------------------+
// | Common ruleset                                                                     |
// +------------------------------------------------------------------------+

var RuleSet = {

  /**
     * Drop down boxes
     *
     * @param  element _eElement Anchor element.
     * @return void
     */
    'span.dropDownLabel' : function (_eElement)
    {
		eLanguages = _eElement.parentNode.getElementsByTagName('div')[0];		
        DropDown.pushBox(_eElement, eLanguages);
    },
	
	/**
	* gallery
           *
	* @param  element _eElement Anchor element.
	* @return void
	*/	
	'div#contentGallery a' : function (_eElement)
    {
		$(_eElement).bind('click', Gallery.switchImage);
    },
	
	'span.moreTeaser' : function (_eElement)
	{
		$(_eElement).bind('click', function () {
			if ($("#ContentBox").is(":hidden")) 
			{
				$("#ContentBox").slideDown("slow", function() {$("span.back").css("display", "block");});
				$("span.moreTeaser").hide();
				}});
    },
	
	'span.back' : function (_eElement)
	{
		$(_eElement).bind('click', function () {
			if ($("#ContentBox").is(":hidden")) 
			{}
			else
			{
				$("#ContentBox").slideUp("slow", function() {$("span.moreTeaser").css("display", "block");});
				$("span.back").hide();				
				$("#container").get(0).scrollTop = 0;
				
			}});
    },
	
	'span.backBehave' : function (_eElement)
	{
		$(_eElement).bind('click', function () {history.back();} );
	}	
}

Behaviour.register(RuleSet);

$(document).ready(Content.init);
$(document).ready(Page.init);
/*$(document).ready(bandwidth.check);*/
$(window).resize(Page.resize);

function SideBarPicChange(_picURL, _picID)
{	
	$(_picID).get(0).src = _picURL;
}

// +------------------------------------------------------------------------+
// | FormKit                                                                |
// +------------------------------------------------------------------------+

var FormKit = {
  
  switchSelectBox : function (_id,_number)
  {
    eDropdown = $('#'+_id).find('span:first');    
    eDropdown.html($('#'+_id+_number).text());
	eDropdownInput = $('#'+_id).find('input:first');    
    eDropdownInput.attr("value", $('#'+_id+_number).text());
  }
}

