﻿
(function ($) {


	/* Popunder will be triggered only when a link on home page is clicked. It should not be triggered when the 
	jcarousel left, right arrows are clicked on page.  The class for these elements start with "jcarousel". If the class changes,
	below check need to be updated. Also, popunder should not trigger on search submit. So the functionality is removed from HomepageHelper JS.
	*/
	$(document).ready(function () {

		$("a").click(function (e) {
			var target = e.target.parentNode.className;

			if (target.indexOf('jcarousel') == -1) {
				HomepageHelper.loadPopUnderOnClick();
			}
		});

	});


	$(window).load(function () {
		HomepageHelper.hasImages();
		//HomepageHelper.getPhotoupload();
		//HomepageHelper.formatBrandTiles(7);
		HomepageHelper.formatPlaytimeTiles(7);



		jQuery("#top-tiles").jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'circular',
			boxid: 'top-tiles',
			initCallback: mycarousel_initCallback,
			//itemLoadCallback: { onBeforeAnimation: mycarousel_itemLoadCallback }, //mycarousel_itemLoadCallback,
			itemFirstInCallback: mycarousel_firstInCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		/**
		* We use the initCallback callback
		* to assign functionality to the controls
		*/
		function prevShowHide(boxid) {
			if ($('#' + boxid + ' .jcarousel-control a:first-child').is(".active")) { $('#' + boxid + ' .mycarousel-prev').hide(); }
			else { $('#' + boxid + ' .mycarousel-prev').show(); }
		}
		function nextShowHide(boxid) {
			if ($('#' + boxid + ' .jcarousel-control a:last-child').is(".active")) { $('#' + boxid + ' .mycarousel-next').hide(); }
			else { $('#' + boxid + ' .mycarousel-next').show(); }
		}

		function mycarousel_itemLoadCallback(carousel, state) {
			for (var i = carousel.first; i <= carousel.last; i++) {
				if (carousel.has(i)) {
					if (!carousel.get(i).find('img').get(0).complete) {
						$("#currentImg").html('loading ' + carousel.first);
						carousel.stopAuto();
						carousel.get(i).find('img')
						.one('load', function () { // when it finishes loading 
							$(this).fadeIn(); // Fade it in when loaded 
							carousel.startAuto();
							$("#currentImg").html('loaded ' + carousel.first);
						})
						//.attr('src', newImage) //Set the source so it begins fetching 
						.each(function () {
							// Cache fix for browsers that don't trigger .load() 
							if (this.complete) $(this).trigger('load');
						});
					} else {
						$("#currentImg").html(carousel.first);
					}
				}
			}
		};

		function trigger(carousel, state) {
			$("#currentImg").html(carousel.first);
		}

		function mycarousel_firstInCallback(carousel, li_object, index, state) {
			var boxid = carousel.options.boxid;
			if (!carousel.autoStopped) {
				if ($('#' + boxid + ' .jcarousel-control a.active').length == 0) {
					$('#' + boxid + ' .jcarousel-control a:first-child').addClass('active');
				} else {
					if ($('#' + boxid + ' .jcarousel-control a:last-child').hasClass('active')) {
						$('#' + boxid + ' .jcarousel-control a:first-child').addClass('active');
						$('#' + boxid + ' .jcarousel-control a.active:not(:first-child)').removeClass('active');
					} else {
						$('#' + boxid + ' .jcarousel-control a.active:not(:last-child)').next().addClass('active');
						$('#' + boxid + ' .jcarousel-control a.active + a.active').prev().removeClass('active');
					}
				}
			}
		}
		function mycarousel_initCallback(carousel) {
			var boxid = this.boxid;
			var pagesize = 1;

			// Disable autoscrolling if the user clicks the prev, next button or pagination buttons.
			$('#' + boxid + ' .jcarousel-control a').bind('click', function () {
				carousel.startAuto(0);
				var index = $(this).text();
				carousel.scroll(jQuery.jcarousel.intval((index - 1) * pagesize + 1));
				if (carousel.autoStopped) {
					$('#' + boxid + ' .jcarousel-control a').removeClass('active'); //Remove any "active" class
					$(this).addClass('active');
				}
				//prevShowHide(boxid);
				//nextShowHide(boxid);
				return false;
			});

			$('#' + boxid + ' .mycarousel-next').bind('click', function () {
				carousel.startAuto(0);
				carousel.next();
				if (carousel.autoStopped) {
					if ($('#' + boxid + ' .jcarousel-control a:last-child').hasClass('active')) {
						$('#' + boxid + ' .jcarousel-control a:first-child').addClass('active');
						$('#' + boxid + ' .jcarousel-control a.active:not(:first-child)').removeClass('active');
					} else {
						$('#' + boxid + ' .jcarousel-control a.active:not(:last-child)').next().addClass('active');
						$('#' + boxid + ' .jcarousel-control a.active + a.active').prev().removeClass('active');
					}
				}
				//prevShowHide(boxid);
				//nextShowHide(boxid);
				return false;
			});

			$('#' + boxid + ' .mycarousel-prev').bind('click', function () {
				carousel.startAuto(0);
				carousel.prev();
				if (carousel.autoStopped) {
					if ($('#' + boxid + ' .jcarousel-control a:first-child').hasClass('active')) {
						$('#' + boxid + ' .jcarousel-control a:last-child').addClass('active');
						$('#' + boxid + ' .jcarousel-control a.active:not(:last-child)').removeClass('active');
					} else {
						$('#' + boxid + ' .jcarousel-control a.active:not(:first-child)').prev().addClass('active');
						$('#' + boxid + ' .jcarousel-control a.active + a.active').removeClass('active');
					}
				}
				//prevShowHide(boxid);
				//nextShowHide(boxid);
				return false;
			});
			// Pause autoscrolling if the user moves with the cursor over the clip.
			carousel.clip.hover(function () {
				carousel.stopAuto();
			}, function () {
				carousel.startAuto();
			});
			//$('#' + boxid + ' .jcarousel-control a:first-child').click();
			//$('#' + boxid + ' .jcarousel-control a:eq(0)').click();
		};
	});

})(jQuery);

//window.alert = function() {return;};


