var locURL = window.location.href;
var locSRCH = window.location.search;

jQuery(function($) {

	$('body').addClass('js');
	
	var dpdCore = (function() {
			
				// collect all functions
				function initialize() {
					
					// adding event-handler to the close-button of the breaking news
					handleBreakingNews();
					
					// loading accordion
					initiateAccordionOnHomepage();
					
					// adding scrollpane to the first, initial opened Accordion Pane (if necessary)
					initiateScrollpane();
					
					// loading focus box					
					initiateCycleOnHomepage();
					
					//numbersOnly($('.numbersOnly'));
					
					$( ".visHidden" ).removeClass('visHidden');
					
					var $hp_browser = parseInt($.browser.version);
					
					if ( $.browser.msie && ( $hp_browser=="7" || $hp_browser=="6" ) ) {						
						$( "#homePageAccordionWrapper" ).css('visibility', 'visible');
						$( ".redTeaser" ).css('visibility', 'visible');
					}
					
				}
				
				// Validation of form fields - MR: not used
				function numbersOnly(elems) {
					elems.keyup(function() { 
					    this.value = this.value.replace(/[^0-9\.]/g,'');
					});
				}
				
				
				/*** Breaking News ***/
				function handleBreakingNews() {
					
					if ( $('div.breakingNewsBox').length > 0 ) {
					
						// proof period of time for displaying
						var bnToday = new Date();
						bnToday = String(bnToday.getTime());
						bnToday = parseInt(bnToday.substring(0, 10));						
						bnFrom = parseInt($('body').data('bnFrom'));
						bnUntil = parseInt($('body').data('bnUntil'));
						
						if (bnFrom != 0 && bnUntil != 0) {
								if ( bnToday > bnFrom && bnToday < bnUntil+86400 ) {
									var showNews = new Boolean(true);
								}
								else {
									var showNews = new Boolean(false);
								}
						}
						else {
							if (bnFrom != 0) {
								if ( bnToday > bnFrom ) {
									var showNews = new Boolean(true);
								}
								else {
									var showNews = new Boolean(false);
								}
							}
							else {
								if (bnUntil != 0) {
									if ( bnToday < bnUntil+86400 ) {
										var showNews = new Boolean(true);
									}
									else {
										var showNews = new Boolean(false);
									}
								}
								else {
									var showNews = new Boolean(true);
								}
							}
						}
						
						if (showNews == true) {
							
							//proof if cookie for hiding is active
							if(document.cookie) {
												
								var bnAllCookies = document.cookie;
								var bnCookie = document.cookie.search('hideBreakingNews');
								var bnCookieWert = bnAllCookies.substring(bnCookie+17,bnCookie+49);
								
								//if( bnCookieWert != $('#breakingNewsHash').html() ) {
								if( bnCookieWert != $('body').data('bnHash') ) {
									$('.breakingNewsBox').css('display', 'block');									
								}
							}
							else {
								$('.breakingNewsBox').css('display', 'block');
							}
							
							// adding event-handler to the close-button of the breaking news
							$('.closeBreakingNews').each(
								function() {
									$(this).click(function() {
										$(this).parent('.breakingNews').slideUp(300);
										
										var bnHash = $('body').data('bnHash');
										
										var breakingNewToday = new Date();
										var breakingNewExpireTime = new Date(breakingNewToday.getTime() + 365*24*60*60*1000);
							        	document.cookie = 'hideBreakingNews=' + bnHash + '; expires=' + breakingNewExpireTime.toGMTString() + ';';
									});
								}
							);
						}
					}
				}
				
				/*** Accordion ***/
				function initiateAccordionOnHomepage() {
					$( ".accordion" ).accordion({
						fillSpace: true,
						autoHeight : false,
						icons: {
							'header': 'iconDown',
							'headerSelected': 'iconUp'
						},
						header: 'h3.pane_head',
						change: function(event, ui) {	// adding scroll pane to the open pane (if necessary), by using change-event
							initiateScrollpane();
						}
					});
		
				}
				
				// adding the scroll panes (if necessary)
				function initiateScrollpane() {
					$('.ui-accordion-content-active').jScrollPane();
				}
				
								
				/*** Focus Box ***/
				//caption handling
				function showCaption() {
					$( '#hpFocusBoxStage .hpSliderPagination' ).fadeOut();
					
					var sHeight = $(this).parents( '.hpSlider div.focusSlide' ).height();
					var sCHeight = $(this).parents( '.hpSlideCaption' ).outerHeight();
					
					var sCTop = sHeight-sCHeight+1;
					
					$(this).parents( '.hpSlideCaption' ).delay(250).animate({
						top: sCTop
					}, function() {
						$(this).addClass( 'open' );
					});
				}
				
				function hideCaption() {
				    $(this).find( '#hpFocusBoxStageSlider ').find( '.hpSlideCaption' ).animate({
						top : 243
					},function() {
						$(this).removeClass( 'open' );
						$( '#hpFocusBoxStage .hpSliderPagination' ).fadeIn();
					});
				}
			  
				//determine specific timeout-time for the active slide
				function calculateTimeout(currElement, nextElement, opts, isForward) {
				    return $(currElement).data( 'timeout' );
				}
			  
				// loading focus box
				function initiateCycleOnHomepage() {
					$('#hpFocusBoxStageSlider').cycle({
						fx: 	      	'scrollHorz',
						timeoutFn:		calculateTimeout,
						pause:			1,
						easing:			'easeInOutExpo',
						speed:			480,
						height:			'278',
						slideExpr: 		'div.focusSlide',
						pager:			'.hpSliderPagination',
						before: function() { $( '#hpFocusBoxStage .hpSliderPagination' ).fadeOut(); },
						after: function() { $( '#hpFocusBoxStage .hpSliderPagination' ).fadeIn(); },
						pagerAnchorBuilder: function(idx) { 
							return "<li>" + idx + "</li>"; 
					    },
						cleartype:   	true,
						cleartypeNoBg:  true
					}).find( '.hpSlideCaption strong p' ).mouseenter( showCaption );
					$( '#hpFocusBoxStage' ).mouseleave( hideCaption );					
					
					// loading interactive Teaser Box
					$('.interactiveTeaser').each(function () {
						var currentInteractiveTeaser = $(this);
						$(this).cycle({
							fx: 	      	'scrollHorz',
							timeout:		0,
							easing:			'easeInOutExpo',
							speed:			480,
							height:			'144',
							slideExpr: 		'a.page',
							pager:			currentInteractiveTeaser.find( '.teaserPagination' ),
							before: function() { currentInteractiveTeaser.find( '.teaserPagination' ).fadeOut(); },
							after: function() { currentInteractiveTeaser.find( '.teaserPagination' ).fadeIn(); },
							pagerAnchorBuilder: function(idx, elm) { 
								return "<li>" + idx + "</li>"; 
							   },
							cleartype:   	true,
							cleartypeNoBg:  true
						});
					}); 
				}		

				//return to the public
				return {
					init : initialize
				};
	}());

	//do the magic
	dpdCore.init();
});

        function handlePrefilledField(field, prefillText, prefillClass) 
        {
						
                       
                        
                        if(field.val()) {
							prefillText = field.val();
						}

            field
								.attr('title',prefillText)
                .val(prefillText)
                .addClass(prefillClass);
            field
                .addClass('prefilled')
                .val(prefillText);
            field
                .focus(
                    function() {
                        field.removeClass(prefillClass);
                        if (field.val() === prefillText) {
                            field.val("");
                        }
                    }
                )
                .blur(
                    function() {
                        if (!((field.val() !== prefillText) && (field.val().length > 0))) {
                            field
                                .val(prefillText)
                                .addClass(prefillClass);
												}
                    }
                );
        }
