﻿jQuery(document).ready(function() {
	// -------------------------------
	// different CSS when JS is enabled/disabled
	jQuery('body').addClass('has-js');

	// -------------------------------
	// setup fancy checboxes and radio buttons
	// (site-wide behaviour)
	setupLabel();

	// What happens when user clicks checbox or radio button:
	jQuery('.label-check, .label-radio').click(setupLabel);

	// -------------------------------
	// This initiates "sticky box" on category and search pages
	fixCategoryBox();

	// -------------------------------
	// Turn ambilight on if we’re on the homepage or videoteka section overview page
	if (jQuery('#space-shuttle-box, .videoteka-overview-body #space-shuttle-mini').length) {
		jQuery('.ambilite').addClass('ambilite-on');
	};

	// -------------------------------
	// Lightboxes

	
	/* DISABLED - use tportal gallery
	jQuery('.lightbox-gallery a').fancybox({
		padding: 0,
		overlayOpacity: 0.85,
		overlayColor: '#000'
	});
	*/

	jQuery('a.videolink').fancybox({
		titleShow: false,
		width: 560,
		height: 340,
		padding: 0,
		overlayOpacity: 0.85,
		overlayColor: '#000',
		callbackOnClose: function() {
			jQuery('#fancy_content').empty();
		}
	});

	/* DISABLED For Now
	jQuery('.login-link').fancybox({
		href: '/templates/user/user-login-popup.php?redirectto=/',
		width: 560,
		height: 340,
		padding: 0,
		overlayOpacity: 0.85,
		opacity: true,
		overlayColor: '#000',
		showCloseButton: false
	});
	*/

	// All promo links
	jQuery('.promo-link').fancybox({
//		type:'iframe',
		width: 500, height: 200, padding: 0,
		overlayOpacity: 0.85, opacity: true, overlayColor: '#000',
		titleShow: false, showCloseButton: true
	});

	// -------------------------------
	// Video section overview template has video categories drawer            

	jQuery('.open-drawer-link a').click(function() {
		if (jQuery('#categories-drawer:hidden').length) {
			jQuery('#categories-drawer').slideDown();
			jQuery(this).closest('.open-drawer-link').addClass('icon-up');
		} else {
			jQuery('#categories-drawer').slideUp();
			jQuery(this).closest('.open-drawer-link').removeClass('icon-up');
		}
	});



	// -------------------------------
	// Search box effects
	var fastSearchDelay;

	jQuery('#q').blur(function() {
		jQuery("input[type=image]").removeAttr('disabled');
		jQuery('#search-suggestions').fadeOut();
	});

	jQuery('#q').click(function() {
		jQuery("input[type=image]").attr('disabled', 'disabled');
		if (jQuery(this).val().length == 0) {
			jQuery('#search-suggestions').fadeOut();
		}
	})

	function GetFastSER(topPos, q)
	{
		jQuery.ajax({
			//url: '/templates/search/autosuggest-results.php',
			url: '/search/autosuggest',
			data: "query=" + q,
			success: function(data) {
				jQuery('#search-suggestions').css('top', topPos);
				jQuery('#search-suggestions').fadeIn('slow', function() {
					jQuery('#search-suggestions .inner').html(data);
				});
			}
		});
	}

	jQuery('#q').bind('keyup', function(event) {
		//event.preventDefault();
		event.stopPropagation();
		if (fastSearchDelay != undefined && null != fastSearchDelay)
		{ clearTimeout(fastSearchDelay); }
		jQuery('#search-scope').fadeOut('fast');
		if (jQuery(this).val().length == 0) {
			// Hide the q-suggestions box
			jQuery('#search-suggestions').fadeOut();
		} else {
			if (event.keyCode == '13' ) {
				//document.location = '/Default.aspx?q=' + jQuery(this).val()
				document.location = '/search?query=' + encodeURI(jQuery(this).val());
			} else {
				if (jQuery(this).val().length >= 2)
				{
					var fastSearchTopPos = jQuery(this).offset().top + 30;
					var query = encodeURI(jQuery(this).val());
					fastSearchDelay = setTimeout(
						function(){ GetFastSER(fastSearchTopPos, query); }
						,1000
					);
				}
			}
		}
	});

	// Search sidebar events
	jQuery('#q-sidebar').focusin(function() {
		jQuery("input[type=image]").attr('disabled', 'disabled');
	});

	jQuery('#q-sidebar').blur(function() {
		jQuery("input[type=image]").removeAttr('disabled');
	});
	
	jQuery('#q-sidebar').bind('keydown', function(event) {
		//event.preventDefault();
		event.stopPropagation();
		if (jQuery(this).val().length > 0 && event.keyCode == '13') {
			document.location = '?query=' + encodeURI(jQuery(this).val());
		}
	});


	// -------------------------------
	// Item controls

	/*
	jQuery('.remind-button a').click(function() {
		jQuery('.login-link').click();
	});

	jQuery('.reminding a').click(function() {
		update_event('remind', '', 'item-id');
	});

	jQuery('.record-button a').click(function() {
		jQuery('.login-link').click();
	});

	jQuery('.recording a').click(function() {
		update_event('record', '', 'item-id');
	});

	jQuery('.record-season-button a').click(function() {
		jQuery('.login-link').click();
	});

	jQuery('.recording-season a').click(function() {
		update_event('record-season', '', 'item-id');
	});

	jQuery('.like-button a').click(function() {
		jQuery('.login-link').click();
	});

	jQuery('.favorite a').click(function() {
		update_event('like', '', 'item-id');
	});
	*/


	// -------------------------------
	// Carousels

	jQuery('.secondary-articles .carousel').jcarousel();

	//jQuery('.videoteque-shelf .carousel').jcarousel({ scroll: 7, wrap: 'last' });
	jQuery('.videoteque-shelf .carousel').jcarousel({ scroll: 7 });

	jQuery('#space-shuttle-tabs .carousel').jcarousel({ scroll: 1 });

	jQuery('.mini-covers-carousel').jcarousel({ scroll: 2 });

	jQuery('.rotate-this').cn_crossFade({pause:6000,speed:1000});


	// -------------------------------
	// User rating sample function

	/*
	_fetch_rating = function() {
	jQuery.get('/sample_url', null, _update_rating, 'json');
	};
	_update_rating = function(data, txt) {
	if (data.success) {
	if (data.data && data.data.avg && data.data.votestotal) {
	jQuery('#rated-avg').html(data.data.avg);
	jQuery('#rated-total').html(data.data.votestotal);
	}
	} else {
	if (data.code == 401) {
	jQuery('.login-link').click();
	return false;
	}
	}
	return true;
	};

	// _fetch_rating();
	jQuery('#rate').rating('/media/rating/4590', { maxvalue: 5, increment: 1, curvalue: 4, onComplete: _update_rating } );
	*/

	//--------------------------------
	// jScrollPane
	// http://www.kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
	// Used on the Cover page + MAXtv magazin overview page

	jQuery('.editors-pick .wrapper').jScrollPane({ scrollbarWidth: 16 });
	jQuery('#theaters-shows .wrapper').jScrollPane({ scrollbarWidth: 16 });
	
	// EmbedCode in details
	jQuery('.embedCode').each(function(index){
		var ec = $(this).html();
		if(null != ec && ec.length > 0)
		{
			ec = ec.replace(/&lt;/g, "<");
			ec = ec.replace(/&gt;/g, ">");
			ec = ec.replace(/&quot;/g, "\"");
			ec = ec.replace(/&#39;/g, "'");
			ec = ec.replace(/&amp;/g, "&");
			$(this).replaceWith('<div class="video-wrapper">' + ec + '</div>');
		}
	});

	jQuery('#movie-poster a').fancybox({
		type: 'image',
		titleShow: false,
		autoScale: true,
		padding: 0,
		overlayOpacity: 0.85,
		overlayColor: '#000',
		callbackOnClose: function() {
			jQuery('#fancy_content').empty();
		}
	});

	// Wallpaper
	if(typeof artWallImage != 'undefined') {
		jQuery('body').css('background-image', 'url(' + artWallImage + ')');
		jQuery('body').css('background-color', artWallBgColor);
	}
	
});
