/*!
 * NAGRA KUDELSKI v1.0
 * http://www.nagra.com
 *
 * © 2011 DIABOLO DESIGN SA / MS
 * http://www.diabolo.com
 * 19 April 2011
 */
 
// SUPERFISH MENU
 
 jQuery(function(){
	jQuery('#menu').superfish({
			autoArrows:    false,
			dropShadows:   false,
			delay:         800
		});
});

 var PRid = HLid = NEWSid = NR_HLid = NR_WEid = NR_VIDid = 0;
 var PRint 		= 8000;
 var HLint 		= 5000;
 var NEWSint 	= 9000;
 var NR_HLint	= 5000;
 var NR_WEint 	= 5500;
 var NR_VIDint 	= 6000;

$(document).ready(function(){
	
	// HIGHLIGHTS
	
	if($('#hls .hl').length > 1){
		$('#hls .hl').css('position', 'absolute');
		$('#hls .hl').slice(1).hide();
		$('#hls .hl').slice(1).find('h1').hide();
		$('#hl_list').html('');
		$('#hls .hl').each(function(i) {
			$('#hl_list').append('<li><a href="#" onclick="return RotateHL('+ i +')"></a></li>');
		});
		//HLid = Math.floor(Math.random() * $('#hls .hl').length); // randomize 1st item
		$('#hl_list li a').eq(HLid).addClass('sel');		
		$('#hls .hl').eq(HLid).fadeIn('slow');
		HL = window.setTimeout('RotateHL(null)',HLint);	
	}	
	
	// PRESS RELEASES
	
	if($('#pr li').length > 1){
		$('#pr li').slice(PRid + 1).css('display', 'none');
		PR = window.setTimeout('RotatePR()',PRint);	
	}
	
	// IN THE NEWS
	
	if($('#inthenews li').length > 1){
		$('#inthenews li').slice(NEWSid + 1).css('display', 'none');
		$('#inthenews_list').html('');
		$('#inthenews li').each(function(i) {
			$('#inthenews_list').append('<li><a href="#" onclick="return RotateNEWS('+ i +')"></a></li>');
		});
		$('#inthenews_list li a').eq(NEWSid).addClass('sel');
		NEWS = window.setTimeout('RotateNEWS(null)',NEWSint);	
	}	
	
	// SUPERFISH ARROWS (CUSTOM)
	
	$('#menu li li:has(li)').each(function(){
		$(this).find('a').css('padding-right','20px');
		$(this).append('<span class="arrow"></span>');
	});
	
	// FANCYBOX
	
	$('.fancy').fancybox({
		'overlayOpacity': 0.6,
		'overlayColor':  '#000',
		'padding':0,
		'showCloseButton': 0,
		'titlePosition': 'over',
		'cyclic' : true,
		'titleFormat'  : function(title, currentArray, currentIndex, currentOpts) { return (currentArray.length > 1) ? '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + ' &nbsp;&nbsp; ' + title + '</span>' : '<span id="fancybox-title-over">' + title + '</span>'  ; 
} 
	});
	$(".iframe").fancybox({
		'width'				: 600,
		'height'			: 700,
        'autoScale'     	: true,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'padding'			: 0,
		'margin'			: 0,
		'showCloseButton'	: false
	});
	
	// NETWORK
	$("#network").hover( function () {
		var timeout = $(this).data("timeout");
    	if(timeout) clearTimeout(timeout);
		$('#subsid').slideDown();
		$('.nk').addClass('hover');
	}, function () {
		$('.nk').removeClass('hover');
		$(this).data("timeout", setTimeout( function(){
        	$('#subsid').slideUp('fast');
      	}, 800));
		
	});	
	
	// PARTNERS
	
	if(!$('body').hasClass('edit')){
		if($('.nagra_partners_container').not('.affiliations').length > 1){
			$('.nagra_partners_container').hide();
			$('h2.list').addClass('off');	
		}		
	}	
	$('.nagra_partners_image').hover(function(){
		var outtimeout = $(this).data("outtimeout");		
    	if(outtimeout) clearTimeout(outtimeout);
		$(this).addClass('on');
		$('.nagra_partners_image .desc').hide();
		$(this).data("in_timeout", setTimeout( function(){
			$('.nagra_partners_image').css("z-index", 1);
			$('.nagra_partners_image.on').find('.desc').fadeIn('fast');
			$('.nagra_partners_image.on').css('z-index', 2);
      	}, 500));
		
	}, function () {
		var in_timeout = $(this).data("in_timeout");
		if(in_timeout) clearTimeout(in_timeout);
		$(this).removeClass('on');
		$(this).data("outtimeout", setTimeout( function(){
			$('.nagra_partners_image').not('.on').find('.desc').hide();
			$('.nagra_partners_image').not('.on').css("z-index", 1);
      	}, 50));
		
	});
	
	$('h2.list').each(function(index, element) {
		var i = index;		
		$(this).find('a').attr('id','list-' + index);
		$(this).find('a').click(function(){
			var id = $(this).attr('id').substr(5);
			if($('h2.list').eq(id).hasClass('off')){
				$('h2.list').eq(id).removeClass('off');
			}else{
				$('h2.list').eq(id).addClass('off');				
			}			
			$('.nagra_partners_container').eq(id).slideToggle('fast');
			$('.nagra_partners_container').not(':eq(' + id + ')').slideUp('fast');
			$('h2.list').not(':eq(' + id + ')').addClass('off')	;	
			return false;
		});     
    });	
	
	// NEWSROOM HEADLINES
	
	if($('#headlines .titles li').length > 1){
		NRHL = window.setTimeout('NR_HL(null)',NR_HLint);
		$('#headlines .titles li').each(function(index){	
			
			$(this).hover( function () {
				$(this).data("nrhlto", setTimeout( function(){
					NR_HL(index);
				}, 300));
			}, function () {
				var nrhlto = $(this).data("nrhlto");
				if(nrhlto) clearTimeout(nrhlto);			
			});				
		});		
	}
	
	// ON THE WEB
	
	if($('#ontheweb .titles .we').length > 1){
		NRWE = window.setTimeout('NR_WE(null)',NR_WEint);
		$('#ontheweb .titles .we').each(function(index){	
			$(this).hover( function () {
				$(this).data("nrweto", setTimeout( function(){
					NR_WE(index);
				}, 300));
			}, function () {
				var nrweto = $(this).data("nrweto");
				if(nrweto) clearTimeout(nrweto);			
			});	
			
			
		});		
	}
	
	// VIDEO
	
	if($('#video li').length > 1){
		$('#vid_list').html('');
		$('.vids li').each(function(i) {
			$('#vid_list').append('<li><a href="#" onclick="return NR_VID('+ i +')"></a></li>');
		});
		$('#vid_list li a').eq(NR_VIDid).addClass('sel');
		VID = window.setTimeout('NR_VID(null)',NR_VIDint);	
	}	
	
});

// PRESS RELEASES

function RotatePR(){
	clearTimeout(PR);
	$('#pr li').eq(PRid).fadeOut(200);
	PRid++;
	PRid = ($('#pr li').get(PRid)) ? PRid : 0 ;
	$('#pr li').eq(PRid).delay(200).fadeIn(300);	
	PR = window.setTimeout('RotatePR()',PRint);
}

// IN THE NEWS

function RotateNEWS(id){
	if(id != NEWSid){	
		clearTimeout(NEWS);
		$('#inthenews li').eq(NEWSid).fadeOut(200);
		$('#inthenews_list li a').eq(NEWSid).removeClass('sel');
		if(id != null){
			NEWSid = id;
		}else{
			NEWSid++;
			NEWSid = ($('#inthenews li').get(NEWSid)) ? NEWSid : 0 ;	
		}	
		$('#inthenews li').eq(NEWSid).delay(200).fadeIn(300);	
		$('#inthenews_list li a').eq(NEWSid).addClass('sel');
		NEWS = window.setTimeout('RotateNEWS(null)',NEWSint);
	}
	return false;
}

// HIGHLIGHTS

function RotateHL(id){
	clearTimeout(HL);
	if(id != HLid){
		$('#hls .hl').eq(HLid).fadeOut(300);
		$('#hls .hl').eq(HLid).find('h1, p').hide();
		$('#hl_list li a').eq(HLid).removeClass('sel');
		if(id != null){
			HLid = id;
		}else{
			HLid++;
			HLid = ($('#hls .hl').get(HLid)) ? HLid : 0 ;	
		}
		$('#hls .hl').eq(HLid).fadeIn(1000);
		$('#hls .hl').eq(HLid).find('h1').delay(300).fadeIn('fast');
		$('#hls .hl').eq(HLid).find('p').show();
		$('#hl_list li a').eq(HLid).addClass('sel')
	}
	HL = window.setTimeout('RotateHL(null)',HLint);
	return false;
}

// NEWSROOM HEADLINES

function NR_HL(id){
	if($('#headlines .titles li').length > 1){
		clearTimeout(NRHL);
		$('#headlines .titles li').removeClass('sel');
		$('#headlines .hl_img li').fadeOut(600);
		if(id == null){
			NR_HLid++;
			NR_HLid = ($('#headlines .titles li').get(NR_HLid)) ? NR_HLid : 0 ;	
		}else{
			NR_HLid = id;
		}	
		$('#headlines .titles li').eq(NR_HLid).addClass('sel');
		$('#hl_p').animate({ 'top': (64 + 1) * NR_HLid }, 300);
		$('#headlines .hl_img li').eq(NR_HLid).fadeIn(300);
		NRHL = window.setTimeout('NR_HL(null)',NR_HLint);	
	}
	
}

// NEWSROOM ON THE WEB

function NR_WE(id){
	if($('#ontheweb .we').length > 1){
		clearTimeout(NRWE);
		$('#ontheweb .we').removeClass('sel');
		$('#ontheweb .we_img li').fadeOut(600);
		if(id == null){
			NR_WEid++;
			NR_WEid = ($('#ontheweb .we').get(NR_WEid)) ? NR_WEid : 0 ;	
		}else{
			NR_WEid = id;
		}	
		$('#ontheweb .we').eq(NR_WEid).addClass('sel');
		$('#hl_w').animate({ 'top': (64 + 1) * NR_WEid }, 300);
		$('#ontheweb .we_img li').eq(NR_WEid).fadeIn(300);
		NRWE = window.setTimeout('NR_WE(null)',NR_WEint);
	}
}

// NEWSROOM VIDEOS

function NR_VID(id){
	clearTimeout(VID);
	if(id != NR_VIDid){
		$('#vid_list li').eq(NR_VIDid).find('a').removeClass('sel');
		if(id == null){			
			NR_VIDid++;
			NR_VIDid = ($('.vids li').get(NR_VIDid)) ? NR_VIDid : 0 ;	
		}else{
			NR_VIDid = id;
		}
		
		$('.vids').animate({
			'left' : (-NR_VIDid) * 330
		}, 300);		
	}
	$('#vid_list li').eq(NR_VIDid).find('a').addClass('sel');
	VID = window.setTimeout('NR_VID(null)',NR_VIDint);
	return false;
}
