var accNum = 0;

$(window).unload(function() {
	var temp = 0;
});

$(document).ready(function(){

	$('#contentMain').find('#contentHeader').find('.csc-textpic-imagewrap').append('<div class="fakeRoundCorner"></div>');
	$('#contentMain').find('#contentHeader').find('.csc-textpic-imagewrap').append('<div class="fakeRoundCorner"></div>');
	
	
	$('#contentWrapBreit').find('#contentHeader').nivoSlider({
		effect:'sliceDown', //Specify sets like: 'fold,fade,sliceDown'
		slices:10,
		animSpeed:500,
		pauseTime:6000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});
	
	$('#contentHeader').toiSlider({
		delay: 4000
	});

    $("a.lightbox").lightBox();
    $("a.fancybox").lightBox();
    
    /*
    $('.videoButton').fancybox({
    	overlayOpacity: 0.7,
    	overlayColor:   '#000000',
    	scrolling: 'no'
    });
    */
	
	$("#toiSlider").toiSlider();
	
	$('.infoText').hide();
	
	$('.infoLink').hover(
		function() {
			$(this).parent().parent().find('.infoText').show();
		},
		function() {
			$(this).parent().parent().find('.infoText').hide();
		}
	);
	
	
	/* Accordions
	 ************************************************/
	$('.csc-accordion table').wrap('<div class="table"></div>');
	$('.csc-accordion').wrapAll('<div class="uiAccordion"></div>');
	$('.uiAccordion').accordion({header: 'h4', autoHeight: false, active: false, collapsible: true});
	
	
	/* Lauftext
	 ************************************************/
	$('#marquee').marquee();
	
	
	/* Experten aufklappen
	 ************************************************/
	$('.expertsWrap').find('.expert').click(function() {
		if($(this).find('.expInfo').is(':visible')) {
			$(this).find('.expInfo').slideUp();
		} else {
			$(this).find('.expInfo').slideDown();
		}
	});
	

	/* Logout hack?
	 *********************************************/
	var logoutLink = (window.location.pathname.search(/online-wahl/) == -1) ? window.location.pathname : '';
	var logoutAdd = '?logintype=logout&r=' + String(Math.random());
	logoutLink += logoutAdd;
	$('#topNavi li a[href="logout/"]').attr('href', logoutLink);
	
	/* Login hack?
	 *********************************************/
	$('#rpdFElogin').submit(function() {
		$(this).find('#username').attr('value', $(this).find('#password').val());
	});


    /* Haupt-Navigation mit Delay
     *********************************************/
    
    var lastOne = false;
    // aktuelle Seite
	var active = $('#mainNavi').find('.top.active:first');
	// Untermenü der aktuellen Seite anzeigen
	showSubMenu(active);
	
	var naviTimeout = 0;
	$('#mainNavi').find('.top').hover(function() {
	    if(naviTimeout != 0) {
	    	clearTimeout(naviTimeout);
	    }
	    
	    var activeElement = $(this);
	    naviTimeout = setTimeout(function() {
	    	naviTimeout = 0;
	    	
	    	alleAusblenden();
			showSubMenu(activeElement);
		
			lastOne = $(this).find('ul');
	    	
	    }, 500);
	    
	},
	function() {
		if(naviTimeout != 0) {
			clearTimeout(naviTimeout);
		}
		naviTimeout = setTimeout(function() {
	    	naviTimeout = 0;
	    	
	    	alleAusblenden();
			active.find('ul').show();
			active.find('ul').find('ul').hide();
	    	
	    }, 500);

	});

    
    /*
     * Zeigt ein einzelnes Menüelement an.
     */
    function showSubMenu(element)
    {
        // Untermenü komplett anzeigen
        element.find('ul').show();
        // alle weiteren Untermenüs ausblenden
	    element.find('ul').find('ul').hide();
    }
    
    
    /*
     * Blendet alle Untermenüs aus.
     */
	function alleAusblenden()
	{
	    // alle Untermenüs ausblenden
		$('#mainNavi').find('.top').find('ul').hide();
	}
	
		
	/* Countdowns
     *********************************************/
	
	// Countdown im Kopf-Bereich
	var mainDate = parseDate($('#headCountdown div.csc-default p:first').text());
	// Countdown erzeugen
	$('#headCountdown #countdownContainer').countdown({until: mainDate, format: 'dHMS'});
	// Titel und Endzeitpunkt auslesen
	var viewTitle = $('#headCountdown div.csc-default h1').text();
	var viewDate = makeViewDate(mainDate);
	// Titel und Endzeitpunkt anzeigen
	$('#headCountdown span.title').text(viewTitle);
	$('#headCountdown div.endTime').text(viewDate);
	
	// Countdown für Auktion
	if ($('#auction_countdown').length > 0) {
	    // Endzeitpunkt der Auktion
	    var ebayDate = parseDate($('#auction_countdown').text());
	    // aktueller Zeitpunkt
	    var nowDate = new Date();
	    if (ebayDate.getTime() < nowDate.getTime()) {
	        // "Beendet" anzeigen
	        $('#auction_countdown').html('<b>Auktion beendet</b>');
	        // Button zum Mitbieten ausgrauen und Link löschen
	        $('.bidButton').addClass('disabled');
	        $('.bidButton img').attr('src', 'pub/images/jetztbieten_grau.jpg');
	        $('.bidButton img').unwrap();
	    } else {
	        // Countdown zum Ende der Auktion einfügen
	        if ($('.endTime').length > 0) {
		        $('#auction_countdown').countdown({until: ebayDate, format: 'dHMS'});
	        }
	        // Countdown zum Anfang der Auktion einfügen
	        else if ($('.startTime').length > 0) {
		        $('#auction_countdown').countdown({until: ebayDate, format: '- dHMS'});
	        }
	    }
	}
		
	
	/* Live-Search Felder
	 ************************************************/
	
	// Online-Wahl
	var lsType = $('#liveSearchName').attr('value');
	$('#liveSearchName').attr('value','');
	$('#liveSearchName').liveSearch({
	    url: 'http://champion-des-jahres.de/?eID=liveSearch&&type='+lsType+'&term='
	});
	var lsType = $('#liveSearchSport').attr('value');
	$('#liveSearchSport').attr('value','');
	$('#liveSearchSport').liveSearch({
	    url: 'http://champion-des-jahres.de/?eID=liveSearch&&type='+lsType+'&term='
	});
	
	// FE-User-Listen
	var searchUrl = $('#feuserLiveSearch').attr('value');
	$('#feuserLiveSearch').attr('value','');
	$('#feuserLiveSearch').liveSearch({
	    url: searchUrl
	});
	
	// Kalender
	$('.calendarLiveSearch').liveSearch({
	    url: 'http://champion-des-jahres.de/?eID=calendarLiveSearch&term='
	});
	
		
	/* SID Ticker
	 ************************************************/
	 
	// Scrollbalken für SID-News auf der Startseite
	$('#sid-ticker-list .list').jScrollPane();
		

	/* Gästebuch
	 ************************************************/

	// Gästebuch Formular auf-/zuklappen
	$('#guestbookAdd').click(function() {
		$('#guestbookWrap').css('display', 'block');
	});
	
	$('.tx-guestbook-expand').click(function() {
		if($(this).parent().find('.tx-guestbook-previewWrap').is(':visible')) {
			$(this).parent().find('.tx-guestbook-previewWrap').hide();
			$(this).parent().find('.tx-guestbook-entryWrap').slideDown('slow');
			$(this).parent().find('.tx-guestbook-expand').css("backgroundPosition", "0 -25px");
			$(this).parent().parent().css("border-color", "#0b87c0");
		} else {
			$(this).parent().find('.tx-guestbook-entryWrap').slideUp();
			$(this).parent().find('.tx-guestbook-previewWrap').show();
			$(this).parent().find('.tx-guestbook-expand').css("backgroundPosition", "0 0");
			$(this).parent().parent().css("border-color", "#CCCCCC");
			
		}
	});
	
	
	
	/* Champion Übersicht (border Overlay)
	*************************************************/
	$('.csc-champbox').each(function() {
		$(this).append('<div class="borderStyleTop"></div>');
		$(this).find('.borderStyleTop').css({width: $(this).innerWidth(), height: '15px'});
		$(this).append('<div class="borderStyleBottom"></div>');
		$(this).find('.borderStyleBottom').css({width: $(this).innerWidth(), height: '15px'});
		var bottomValue = 50 - $(this).find('.csc-textpic-text').innerHeight() + 'px';
		$(this).find('.csc-textpic-text').animate({bottom: bottomValue}, 300);
		$(this).hover(function() {
			$(this).find('.csc-textpic-text').animate({bottom: '0'}, 500);
		}, function() {
			$(this).find('.csc-textpic-text').animate({bottom: bottomValue}, 300);
		});
	});
	
	
	/* Voting Übersicht (border Overlay)
	*************************************************/
	$('.voting_item').each(function() {
		$(this).append('<div class="borderStyleTop"></div>');
		$(this).find('.borderStyleTop').css({width: $(this).innerWidth(), height: '15px'});
		$(this).append('<div class="borderStyleBottom"></div>');
		$(this).find('.borderStyleBottom').css({width: $(this).innerWidth(), height: '15px'});
		var bottomValue = 50 - $(this).find('.csc-textpic-text').innerHeight() + 'px';
		$(this).find('.csc-textpic-text').animate({bottom: bottomValue}, 300);
		$(this).hover(function() {
			$(this).find('.csc-textpic-text').animate({bottom: '0'}, 500);
		}, function() {
			$(this).find('.csc-textpic-text').animate({bottom: bottomValue}, 300);
		});
	});
	
	/* Presse Übersicht (border Overlay)
	*************************************************/
	$('.presse_item').each(function() {
		$(this).append('<div class="borderStyleTop"></div>');
		$(this).find('.borderStyleTop').css({width: $(this).innerWidth(), height: '15px'});
		$(this).append('<div class="borderStyleBottom"></div>');
		$(this).find('.borderStyleBottom').css({width: $(this).innerWidth(), height: '15px'});
		var bottomValue = 50 - $(this).find('.csc-textpic-text').innerHeight() + 'px';
		$(this).find('.csc-textpic-text').animate({bottom: bottomValue}, 300);
		$(this).hover(function() {
			$(this).find('.csc-textpic-text').animate({bottom: '0'}, 500);
		}, function() {
			$(this).find('.csc-textpic-text').animate({bottom: bottomValue}, 300);
		});
	});
	
	
	/* Kontaktformular errors ausblenden wenn leer!
	*************************************************/
	$('.errorText').each(function() {
		if($(this).html() == '' || $(this).html() == '&nbsp;') $(this).hide();
	});
	
	
/*	$('.csc-infoBox img[alt="make-background"]').each(function(){
		var imageLink = $('.csc-infoBox img[alt="make-background"]').attr('src');
		$(this).parent('.imageWithCaption').parent('.csc-textpic-single-image').parent('.textboxAboveCenter').parent('.csc-infoBox').css({
			'background-image': 'url('+imageLink+')',
			'background-repeat': 'no-repeat',
			'padding-top': '120px'
		});
		$(this).hide();
	}); */
	
	

});


var browserName=navigator.appName;var browserVer=parseInt(navigator.appVersion);var version="";var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){version="n3";}else{version="n2";}
function blurLink(theObject){if(msie4){theObject.blur();} else {theObject.blur();}}
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-2);}


function addToBookmarks()
{
    if (document.all) {
        window.external.AddFavorite(location.href,document.title);
    }
    else if (window.sidebar) {
        window.sidebar.addPanel(document.title,location.href,'');
    }
}


/**
 * Erzeugt ein Date-Objekt aus einem String mit dem Format 'Tag,Monat,Jahr,Stunde,Minute,Sekunde'.
 * @param string text
 * @return Date
 */
function parseDate(text)
{
    // Endzeitpunkt auslesen
    var numbers = text.split(',');
    // Datum erzeugen
    var date = new Date(
    	Number(numbers[0]),
    	(Number(numbers[1]) - 1),
    	Number(numbers[2]),
    	Number(numbers[3]),
    	Number(numbers[4]),
    	Number(numbers[5])
    );
    // Date-Objekt zurückgeben
    return date;
}


/**
 * Erzeugt einen Datums-String mit dem Format 'dd. M Y, H:i:s'
 * @param Date date
 * @param boolean showTime
 * @return string
 */
function makeViewDate(date, showTime)
{
	var months = new Array('Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
	var day = (date.getDate() <= 9) ? '0'+date.getDate() : date.getDate();
	var titleDate = day + '. ' + months[date.getMonth()] + ' ' + date.getFullYear();
	
	if (showTime == true) {
		var hours = (date.getHours() <= 9) ? '0'+date.getHours() : date.getHours();
		var minutes = (date.getMinutes() <= 9) ? '0'+date.getMinutes() : date.getMinutes();
		var seconds = (date.getSeconds() <= 9) ? '0'+date.getSeconds() : date.getSeconds();
		titleDate += ', ' + hours + ':' + minutes + ':' + seconds;
	}

	return titleDate;
}


function votingConfirm(link)
{
	var check = confirm('Sie sind im Begriff Ihre Stimme abzugeben. Wollen Sie das wirklich tun?');
	if (check == true) {
		window.location = link;
	}
	return false;
}
