var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
	// alert('hallo Welt');
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{
	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(document).ready(function()
{
	$('#Mainmenue > li').bind('mouseover', jsddm_open)
	$('#Mainmenue > li').bind('mouseout',  jsddm_timer)
});

document.onclick = jsddm_close;

var iCycleMainspot = 0;
var iCycleSeconds = 5;
var iCycleTimer = iCycleSeconds;

var cycleMainspot =function() {

	window.setTimeout( 'cycleMainspot()', 1000 );
	
	if( iCycleTimer-->0 )
		return;

	iCycleTimer = iCycleSeconds;

	if( iCycleMainspot++>=3 )
		iCycleMainspot = 0;
	
	$('.accordion').children('li:nth-child('+(iCycleMainspot+1)+')').trigger('click');
	focusFunction( iCycleMainspot+1 );

};



$(function(){

	
	var iCurMainspot = 0;
	focusFunction = function( iSetIndex )
	{
		var iNewIdx;
		var oAcc;
		if( iSetIndex>0 )
		{
			iNewIdx = iSetIndex-1;
			oAcc = $('.accordion');
		}	
		else
		{
			// user driven event .. reset timer and cycle index
			iNewIdx = ( $('li',$(this).parent() ).index(this) );
			oAcc = $(this).parent();

			iCycleTimer = iCycleSeconds;
			iCycleMainspot = iNewIdx;
			
		}
		if( iCurMainspot != iNewIdx )
		{
			// condense current active mainspot image
			oAcc.find('li:nth-child('+(iCurMainspot+1)+')').find('div.bigimage').fadeOut( 300 ); // attr('id') );

			// expand to be activated mainspot image
			oAcc.find('li:nth-child('+(iNewIdx+1)+')').find('div.bigimage').fadeIn( 300 ); // attr('id') );
			// $(this).children('div.bigimage').fadeIn( 300 );
			
			// remember new index
			iCurMainspot = iNewIdx;
			
		}

	}
		
	blurFunction = function()	{}

	window.setTimeout( 'cycleMainspot()', 1000 );


	$('.accordion').hSlides(
	{
		totalWidth: 950, 
		totalHeight: 166, 
		minPanelWidth: 103, 
		maxPanelWidth: 642,
		eventHandler: 'click',
		activeClass: 'active',
		onLeave: blurFunction,
		onEnter: focusFunction,
		speed: 500
	});
});




$(document).ready(function(){
	

	/*
	$('.Slideshow a').qtip({
	   content: false,
	   show: 'mouseover',
	   hide: 'mouseout'
	})
	*/
	
	// Colorbox
	$("a[rel='example1']").colorbox({
		transition:"elastic", 
		width:"700", 
		height:"600"
	});
	
	/*			
	// Ergebnisse
	$(function() {
		$("#Ergebnisse").tabs("#Ergebnisse div.Fold", {
			tabs: 'h2', 
			effect: 'slide'
		});
	});
	*/
	
	// Ergebnisse neu
	$(function() {
		$("#Ergebnisse ul.Tabs").tabs("#Ergebnisse div.Panes > div.Fold", { 
			// configuration variables 
			current: 'current', 
			effect: 'fade',
			fadeInSpeed:100,
			fadeOutSpeed:100
 		});
	});
	
	// Ergebnisse neu
	$(function() {
		$("#Topvideos ul.Tabs").tabs("#Topvideos div.Panes > div.Fold", { 
			// configuration variables 
			current: 'current', 
			effect: 'fade',
			fadeInSpeed:100,
			fadeOutSpeed:100
 		});
	});
	
	/*
	// Die Woche
	$(function() {
		$("#Diewoche").tabs("#Diewoche div.Fold", {
			tabs: 'h2', 
			effect: 'slide'
		});
	});
	*/
	
	// Die Woche neu
	// moved to onAjax Load Finished in sidebar.diewoche.ajax.php
	/*
	$(function() {
		$("#Diewoche ul.Tabs").tabs("#Diewoche div.Panes > div.Fold", { 
			// configuration variables 
			current: 'current', 
			effect: 'fade',
			fadeInSpeed:100,
			fadeOutSpeed:100
 		});
	});
	*/

	
	// Slider
	$(function() {
		$("#TVplayer div.Slider").scrollable({ 
			size: 			6,
			globalNav: 		'false',
			disabledClass: 'Inactive',
			items: 			'.Items',
			item: 			'div',
			speed:			500
		});
	});
	
	
	
	// Slider
	$(function() {
		$("#Sidebar div.Slider").scrollable({ 
			size: 			1,
			globalNav: 		'false',
			disabledClass: 'Inactive',
			items: 			'.Items',
			item: 			'div',
			speed:			500
		});
	});

	
	// Slider
	$(function() {
		$("#Mainbar div.Slider").scrollable({ 
			size: 			6,
			globalNav: 		'false',
			disabledClass: 'Inactive',
			items: 			'.Items',
			item: 			'div',
			speed:			500
		});
	});
	
	
	// letztes Mainmenu-Tab bekommt anderes inner.bg
	$("#Navigation .Tabs li:last a").css({
		'background-image':'url(../img/mt.gif)', 
		'padding-right':'23px'
	});
	
	/*
	// letztes Tab News und Slideshows bekommt anderes inner.bg
	// $('.Slideshows li:last a, .News li:last a').removeClass('myClass noClass').addClass('yourClass');
	$(".Slideshows li:last a, .News li:last a").class({
		'background-image':'url(../img/Tab.Inner.Last.Child.png)', 
		'padding-right':'23px'
	});
	*/
	$("#Mainbar .Tabs li:last-child a").css({
		'background-image':'url(../img/Tab.Inner.Last.Child.png)',
		'padding-right':'13px'
	});

	
	// Neueste News
	$(function() {
		$("#NeuesteNews").tabs("#NeuesteNews div.Fold", {
			tabs: 'h2', 
			effect: 'slide'
		});
	});
	
	// Affiliate Links
	$(function() {
		$("#Affiliate").tabs("#Affiliate div.Fold", {
			tabs: 'h2', 
			effect: 'slide'
		});
	});
	
	// Features
	$(function() {
		$("#Features").tabs("#Features div.Fold", {
			tabs: 'h2', 
			effect: 'slide'
		});
	});
	
	// Umfrage
	$(function() {
		$("#Umfrage").tabs("#Umfrage div.Fold", {
			tabs: 'h2', 
			effect: 'slide'
		});
	});
	
	
	// Aktivierung der gestylten Radio Buttons
	// $('input').customInput();

	
	// hover Hauptmenue
	/*$("#Navigation li").hover(
		function () {
			rel='#Previews .'+this.getAttribute('title');
			$(rel).css({'display' : 'block'});
		}, 
		function () {
			rel='#Previews .'+this.getAttribute('title');
			$(rel).css({'display' : 'none'});
		}
	);
	*/
	
	var Timer, Tab, Tab_Alt, preview, preview_Alt;
	
	$("#Navigation li:not(#Navigation li[class*='active']):not(#Navigation li[title='Home'])").hover(
	
		function(){
			
			clearTimeout(Timer);
			
			// setze Variablen 
			preview_Alt = preview;
			preview='#Previews .'+this.getAttribute('title');
			
			Tab_Alt = Tab;
			Tab = this.getAttribute('title');
			
			// Aktive Area wird sichbar
			
			// Die bislang aktive Preview wird ausgeschaltet
			$(preview_Alt).css({
				'display' : 'none'
			});
			
			// die aktuelle Preview wird eingeschaltet
			$(preview).css({
				'display' : 'block'
			});
			
			// Aktives Tab wird hervorgehoben 
			$("#Navigation li[title="+Tab_Alt+"]:not(#Navigation li[class*='active'])").css({
				"background-position": "0 0"
			});
			
			$("#Navigation li[title="+Tab+"]:not(#Navigation li[class*='active']):not(#Navigation li[class*='noarrow'])").css({
				"background-position": "0 -85px"
			});
			
			$("#Navigation li[class*='noarrow'][title="+Tab+"]").css({
				"background-position": "0 -255px"
			});
			
			$("#Navigation li[class*='noarrow'][class*='active'][title="+Tab+"]").css({
				"background-position": "0 -170px"
			});
			
		},
		
		function(){

			Timer=setTimeout("Reset_Mainspot()", 500);

		}
	);
	
	$(".Area").hover(
		function() {
			
			clearTimeout(Timer);

		},
		
		function(){
			
			Timer=setTimeout("Reset_Mainspot()", 500);
		
		}
		
	);
	
	Reset_Mainspot = function() 
	{
		
		$(preview).fadeOut(250);
	
	
		$('#Navigation li[title='+Tab+']:not(#Navigation li.active)').css({
			"background-position": "0 0"
		});
		
	};
	
	$('#searchfield').bind( 'click', function() {
		if( $('#searchfield').val() == 'Suchbegriff eingeben' )
			$('#searchfield').val( '' );
	});
	$('#searchfield').bind( 'blur', function() {
		if( $('#searchfield').val() == '' )
			$('#searchfield').val( 'Suchbegriff eingeben' );
	});
		
});




function backtotop()
{
	offset=window.pageYOffset;
	if(document.documentElement.scrollTop) offset=document.documentElement.scrollTop; // MSIE
	one(offset);
}


function one(offset)
{
	new_offset=Math.abs(Math.floor(offset/3*2));
	window.scrollTo(0,new_offset);
	if(new_offset>0) two(new_offset);
}


function two(offset)
{
	setTimeout("one("+offset+")",10);
}


function go_url(url)
{
	document.location.href=url;
}


function go_flashurl(id)
{
	switch(id)
	{
		case 1:
			url='http://www.spiegel.de';
			break;
		case 2:
			url='http://www.okamo.de';
			break;
		case 3:
			url='http://www.minkenberg.de';
			break;
		case 4:
			url='http://www.stern.de';
			break;
	}
	document.location.href=url;
}


function clearfield(id){
	if(id=='name' && document.getElementById(id).value=='Name'){
		document.getElementById(id).value='';
	}
	if(id=='email' && document.getElementById(id).value=='eMail'){
		document.getElementById(id).value='';
	}
	if(id=='number' && document.getElementById(id).value=='eZahl wiederholen'){
		document.getElementById(id).value='';
	}
	if(id=='text' && document.getElementById(id).value=='Kommentar'){
		document.getElementById(id).value='';
	}
}


function restorefield(id){
	if(id=='name' && document.getElementById(id).value==''){
		document.getElementById(id).value='Name';
	}
	if(id=='email' && document.getElementById(id).value==''){
		document.getElementById(id).value='eMail';
	}
	if(id=='number' && document.getElementById(id).value==''){
		document.getElementById(id).value='eZahl wiederholen';
	}
	if(id=='text' && document.getElementById(id).value==''){
		document.getElementById(id).value='Kommentar';
	}
}


function go_url(url)
{
	document.location.href=url;
}


function playVideo( sID, sEmbedCode )
{
	document.getElementById(sID).setQueryStringParameters({embedCode:sEmbedCode});
	document.getElementById(sID).playMovie();		
}

