var nu13thstBaseUrl = '';
var nu13thstLang = '';
var nu13thstXmlPath = 'fileadmin/_temp_xml_/';
var nu13thstPlayer;
var nu13thstPlayVideo = false;
var nu13thstInfoTimeout;
var datepickerDefaultDate;
var datepickerOptions;

var NU13THST = {
	_LOCAL_LANG : {
		en : {
			previousImage: 'Previous',
			nextImage: 'Next',
			previous: 'Previous',
			next: 'Next',
			videoInfoText1: 'We&lsquo;re sorry, due to legal restrictions 13thstreet.com.au is only allowed to show videos to users from Australia.<br /><br />',
			videoInfoText2: ''
		},	
		de : {
			previousImage: 'Vorheriges Bild',
			nextImage: 'N&auml;chstes Bild',			
			previous: 'Zur&uuml;ck',
			next: 'Vor',
			videoInfoText1: 'Bitte entschuldigen Sie, leider darf 13thstreet.de aus lizenzrechtlichen Gründen dieses Video in Ihrem Land nicht zeigen.<br /><br />',
			videoInfoText2: 'Eine Auswahl an Videos wie etwa Kurzfilme und Kurzgeschichten finden Sie jedoch auf den DVDs im <a href="' + nu13thstBaseUrl + 'shop/" target="_blank">13TH STREET-Shop</a>.'
		}	
	}
};

$(document).ready(function () {

	nu13thstLang = $('html').attr('lang');

	nu13thstBaseUrl = $('base').attr('href');

	$('.box').corner('6px');

	$('.tabs-nav-container').tabs();
	
	$('.carousel-nav-container').tabs({ event: 'mouseover' });
	
	$('.carousel-nav-container').corner('bottom 6px');	

	$('.mainnav-with-subnav').hover(
		function() {
			$(this).addClass('ro');
			var subnav = '#subnav-' + $(this).attr('id').split('-').pop();
			$(subnav).show();
		},
		function(event) {
			if($(event.relatedTarget).attr('class') != 'subnav') {
				$(this).removeClass('ro');
				$('.subnav-container').hide();
			}
		}
	);

	// Roll over effect for logos and flags
	$('#footer li img').hover(
		function() { $(this).attr('src', $(this).attr('src').substr(0, $(this).attr('src').length-4) + '-ro.gif'); },
		function() { $(this).attr('src', $(this).attr('src').substr(0, $(this).attr('src').length-7) + '.gif'); }	
	);

	$('.subnav-container').mouseleave(
		function() {
			$('.mainnav-with-subnav').removeClass('ro');
			$('.subnav-container').hide();
		}
	);
	
	$('#newsletter-subscription-email').focus(
		function() {
			if ($(this).val() == 'E-Mail-Adresse eingeben') {
				$(this).val('');
			}
		}
	);
	
	$('#newsletter-subscription-email').blur(
		function() {
			if ($(this).val() == '') {
				$(this).val('E-Mail-Adresse eingeben');
			}
		}
	);
	
	$('.playlist li').hover (
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }		
	);
	
	$('.playlist li.playlist-item').click(playVideo);
	$('.playlist li.playlist-item-disabled').click(showVideoInfo);

	$.ifixpng('clear.gif');
	$('#logo img, .subnav-container img, .tvguide-category img, #footer-flags img').ifixpng();
	
	if($('.player').length > 0) {	
		embedPlayer(false);
	}
	if($('.bigplayer').length > 0) {	
		embedPlayer(true);
	}	
	
	$('.tvguide-infoopen').click( function() { $(this).closest('tr').addClass('act'); $(this).closest('td').addClass('open'); }	);
	$('.tvguide-infoclose').click( function() { $(this).closest('tr').removeClass('act'); $(this).closest('td').removeClass('open'); }	);
	
	$('.button').each(
		function () {
			if ($(this).children('img').length == 0) {
				$(this).append('<img alt="" src="typo3conf/ext/nu13thst_sys/gfx/icons/arrow-dark-right.gif" />');
			}
		}
	);
	
	$('#newsletter-subscription .button').click( function() { $('#newsletter-subscription').submit(); return false; } );

	if($('#datepicker').length == 1) {
		$('#datepicker').datepicker( datepickerOptions );
	}	
	
	initGallery();
	initSubnav();
	
	// open tv guide details
	if($(location.hash).length) { $(location.hash).closest('tr').addClass('act'); $(location.hash).closest('td').addClass('open'); }
	$('#now-and-next td a').click( function() { 
		var itemId = '#' + $(this).attr('id').substr(5);		
		$(itemId).closest('tr').addClass('act'); $(itemId).closest('td').addClass('open'); 
	} );
	
	/* min-height for ie6 */
	if($.browser.msie) {		
		if($('#main-col').height() < 600) { $('#main-col').height(600); }		
	}

});

function tvguideDateSelected(dateText) {
	window.location.href = nu13thstBaseUrl + 'tv-guide/datum/' + dateText + '/';
}

function getLL(key) {
	var ll = '';
	if (typeof(NU13THST._LOCAL_LANG[nu13thstLang]) != "undefined" &&
		typeof(NU13THST._LOCAL_LANG[nu13thstLang][key]) != "undefined") {
		ll = NU13THST._LOCAL_LANG[nu13thstLang][key];
	}
	return ll;
}

function initSubnav() {

	var boxSubnav = $('#box-items').nu13thstSubnav('.box-subnav', {
		delay:                  2000,
		numThumbs:              4,
		preloadAhead:           4,
		enableTopPager:         true,
		enableBottomPager:      false,
		imageContainerSel:      '#nav-slideshow',
		controlsContainerSel:   '#nav-controls',
		captionContainerSel:    '#nav-caption',
		loadingContainerSel:    '#nav-loading',
		renderSSControls:       false,
		prevLinkText:           getLL('previousImage'),
		nextLinkText:           getLL('nextImage'),
		prevPageLinkText:       getLL('previous'),		
		nextPageLinkText:       getLL('next'),		
		onPageTransitionOut:    function(callback) {
			$('#box-items ul').fadeOut('fast', callback);
		},
		onPageTransitionIn:     function() {
			$('#box-items ul').fadeIn('fast');
		}
	});

}

function initGallery() {

	var gallery = $('#gallery').galleriffic('#thumbs', {
		delay:                  2000,
		numThumbs:              4,
		preloadAhead:           4,
		enableTopPager:         true,
		enableBottomPager:      false,
		imageContainerSel:      '#gallery-slideshow',
		controlsContainerSel:   '#gallery-controls',
		captionContainerSel:    '#gallery-caption',
		loadingContainerSel:    '#gallery-loading',
		renderSSControls:       false,
		prevLinkText:           getLL('previousImage'),
		nextLinkText:           getLL('nextImage'),		
		prevPageLinkText:       getLL('previous'),
		nextPageLinkText:       getLL('next'),
		onTransitionOut:        function(callback) {
			$('#gallery-slideshow, #gallery-caption').fadeOut('fast', callback);
		},
		onTransitionIn:         function() {
			$('#gallery-slideshow, #gallery-caption').fadeIn('fast');
		},
		onPageTransitionOut:    function(callback) {
			$('#thumbs ul.thumbs').fadeOut('fast', callback);
		},
		onPageTransitionIn:     function() {
			$('#thumbs ul.thumbs').fadeIn('fast');
		}
	});

}

function embedPlayer(bigPlayer) {

	if (bigPlayer) {
		var width = 500;
		var height = 330;
	} else {
		var width = 330;
		var height = 233;
	}

	var id = $('.playlist .playlist-item.act:first').attr('id');
	
	$('.tabs-nav-container').tabs();
	
	var panelId = $('.playlist .playlist-item.act:first').parents('.ui-tabs-panel').attr('id');	
	$('.tabs-nav-container').tabs( 'select', panelId);
	
	if(id) {
		var filePath = nu13thstBaseUrl + nu13thstXmlPath + id + '.xml';
	} else {
		var filePath = '';
	}

	var flashvars = {
		file: filePath,
		bufferlength: 3,
		skin: 'typo3conf/ext/nu13thst_sys/swf/modieus.swf',
		stretching: 'fill'
		//plugins: 'adtonomy',
		//'adtonomy.config': 'http://www.utarget.co.uk/utpod/chooseAd.aspx?cid=1402&pick=true&impCount=true&of=adtonomy'		
	}
	var params = {
		allowfullscreen: 'true', 
		allowscriptaccess: 'always',
		wmode: 'transparent'
	}
	var attributes = {
		id: 'jwplayer',
		name: 'jwplayer'
	}

	swfobject.embedSWF('typo3conf/ext/nu13thst_sys/swf/player-licensed.swf', 'jwplayer', width, height, '9.0.115', false, flashvars, params, attributes);

}

function playerReady(obj) {
	
	nu13thstPlayer = document.getElementById(obj['id']);
	nu13thstPlayer.addControllerListener('PLAYLIST', 'loadedVideo');
	
	setVideoDetails(nu13thstPlayer.getPlaylist()[0]);

};

function loadedVideo (id) {

		setVideoDetails(nu13thstPlayer.getPlaylist()[0])

		if (nu13thstPlayVideo) {
			nu13thstPlayer.sendEvent('PLAY', true);
		}
}

function setVideoDetails(video) {

	// video title
	if(video.title) { var title = video.title } else { var title = ''; }
	$('#video-details-header').html(title);	

	// video description
	if(video.description) { var description = video.description } else { var description = ''; }	
	$('#video-details-description').html(description);

	// video author == button to details
	if(video.author) {
		$('#video-details-link').html(video.author).show();
	} else {
		$('#video-details-link').hide();
	}

}

function playVideo() {

	$('#playerinfo').hide();
	$('.playlist .act').removeClass('act');
	$(this).addClass('act');
	
	nu13thstPlayVideo = true;
	
	var id = $(this).attr('id');
	nu13thstPlayer.sendEvent('LOAD', nu13thstBaseUrl + nu13thstXmlPath + id + '.xml');

}

function showVideoInfo() {

	window.clearTimeout(nu13thstInfoTimeout); 
	var infoText = getLL('videoInfoText1');
	infoText += getLL('videoInfoText2');
	$('#playerinfo div').html(infoText)
	$('#playerinfo').fadeIn('fast');
	nu13thstInfoTimeout= window.setTimeout("$('#playerinfo').fadeOut();", 10000);

}

function track_submitquerybutton(obj){var s=s_gi('nbcuintl13thstreetde');s.linkTrackVars="events";s.linkTrackEvents="event1";s.events="event1";s.tl(obj,'o','Submit Query');}

jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};