/**
	THIS FILE REQUIRES:
		jquery.js
		jquery.cycle.all.min.js
*/
var HPCarousels = {
	init: function(){
		// start carousels
		HPCarousels.heroCarousel.init();
		HPCarousels.hpSmallCarousel.init();
	},
	
	// Hero carousel on homepage
	heroCarousel: {
		init: function(){
			hpHero = $jq('#hero-carousel');
			HPCarousels.heroCarousel.loadContent();
		},
		
		activate: function(){
			// start hero cycle
			hpHero.cycle({
			    prev:   '#hp-hero-controls a.btn-prev', 
			    next:   '#hp-hero-controls a.btn-next', 
			    timeout: 9000,
			    speed: 600,
			    pause: true,
				before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					if($jq(nextSlideElement).hasClass('light')){
						$jq('#hp-hero-controls').toggleClass('light');
						$jq('#hp-hero-controls').removeClass('dark');
					} else if($jq(nextSlideElement).hasClass('dark')){
						$jq('#hp-hero-controls').addClass('dark');
						$jq('#hp-hero-controls').removeClass('light');
					} else {
						$jq('#hp-hero-controls').addClass('light');
						$jq('#hp-hero-controls').removeClass('dark');
					}
				},
			    after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					//sIFR.replace(gothamlight, {
					//  selector: 'h3.sifr-me',
					//  css: '.sIFR-root { background-color: #000000; color: #F73210}',
					//  transparent: true
					//});
				},				
			    activePagerClass: 'active-slide',
			    nowrap: false,
			    pager: '#hp-hero-controls ul',
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li><a href="#">'+ idx +'</a></li>'; 
				}
			});
			
			// activate mouse events
			HPCarousels.heroCarousel.heroMouseEvents();
		},
		
		heroMouseEvents: function(){
			// mouse events for mousing over the carousel
			$jq('#header, #hero-carousel-wrapper, .grid-nav, .upper-nav, #hp-hero-controls').hover(function(){
				hpHero.cycle('pause');
			}, function(){
				hpHero.cycle('resume');
			});
		},
		
		loadContent: function(){
			// start magic to load in xml data from variable largeCarouselDate in the prepareDatedFiles.php file
			$jq.ajax({
				type: "GET",
				url: "_xml/carousel/home/carousel_large_"+largeCarouselDate+".xml",
				dataType: "xml",
				success: HPCarousels.heroCarousel.parseXML,
				error: function(){
					alert('Error loading XML, File with this date doesnt exist');
				}
			});	
			
		},
		
		parseXML: function(xml){
			/* PARSE XML and create each item */
			$jq(xml).find('set').each(function(){
				// variables
				var set = $jq(this),
					imgURL = set.find('item').attr('imgURL'),
					d = set.find('item').attr('imgURL').lastIndexOf('.'),
					showID = imgURL.substring(imgURL.lastIndexOf('/') + 1, d < 0 ? imgURL.length : d),
					showName = set.find('tag').attr('txt'),
					tag2 = set.find('tag2').attr('txt'),
					tuneIn = set.find('tag3').attr('txt'),
					desc = set.find('desc').attr('txt'),
					color = set.find('color').attr('txt'),
					linksList = set.find('links');
				
				// create each item
				$jq('<div class="carousel-item '+color+'"></div>').html('<div class="item-content"><img src="'+imgURL+'"><div class="item-feature"><h2>'+showName+'</h2><h3>'+tuneIn+'</h3><ul class="'+showID+'-list-items"></ul></div></div>').appendTo('#hero-carousel');
				
				// build link list items in each set
				linksList.children().each(function(){
					 linkText = $jq(this).attr('txt');
					 linkURL = $jq(this).attr('linkURL');
					//console.log(showName);
					//console.log(text);
					$jq('<li><a href="'+linkURL+'">'+linkText+'</a></li>').appendTo('.'+showID+'-list-items');
				});
				
			});

			// activate hero carousel
			HPCarousels.heroCarousel.activate();
		}
	},
		
	// FX has the movies carousel on homepage
	hpSmallCarousel: {
		init: function(){
			hpMiniCarousel = $jq('#fx-small-carousel ul');
			HPCarousels.hpSmallCarousel.loadContent();	
		},
		
		activate: function(){
			// start hp small carousel cycle
			hpMiniCarousel.cycle({
			    prev:   '#hp-small-carousel-controls a.btn-prev', 
			    next:   '#hp-small-carousel-controls a.btn-next', 
			    timeout: 7000,
			    speed: 600,
			    pause: true,
			    after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					sIFR.replace(gothamlight, {
					  selector: 'h3.sifr-head-light-now',
					  css: '.sIFR-root { background-color: #000000; color: #ffffff}',
					  transparent: true
					});	
				},
			    activePagerClass: 'active-slide',
			    nowrap: false,
			    pager: '#hp-small-carousel-controls ul',
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li><a href="#">'+ idx +'</a></li>'; 
				}
			});	
		},
	
		loadContent: function(){		
			// start magic to load in xml data from variable smallCarouselDate in the prepareDatedFiles.php file
			$jq.ajax({
				type: "GET",
				url: "_xml/carousel/home/carousel_small_"+smallCarouselDate+".xml",
				dataType: "xml",
				success: HPCarousels.hpSmallCarousel.parseXML,
				error: function(){
					alert('Error loading XML, File with this date doesnt exist');
				}
			});
			
		},
		
		parseXML: function(xml){
			$jq(xml).find('set').each(function(){
				// set variables for each node/item
				var set = $jq(this),
					imgURL = set.find('item').attr('imgURL'),
					//d = set.find('item').attr('imgURL').lastIndexOf('.'),
					//showID = imgURL.substring(imgURL.lastIndexOf('/') + 1, d < 0 ? imgURL.length : d),
					moduleTitle = set.find('tag').attr('txt'),
					desc = set.find('tag2').attr('txt'),
					tuneInDay = set.find('day').attr('txt'),
					tuneInTime = set.find('tunein').attr('time'),
					linksList = set.find('links').find('link'),
					linkText = linksList.attr('txt'),
					linkURL = linksList.attr('linkURL');
				
				// create each item
				$jq('<li class="small-carousel-item"></li>').html('<h3 class="sifr-head-light-now">'+moduleTitle+'</h3><a href="'+linkURL+'"><img src="'+imgURL+'"></a><a href="'+linkURL+'">'+linkText+'</a><p>'+desc+'</p><h4>'+tuneInDay+', '+tuneInTime+'</h4>').appendTo('#fx-small-carousel ul');
				
				/*/ build link list items in each set
				linksList.children().each(function(){
					 linkText = $jq(this).attr('txt');
					 linkURL = $jq(this).attr('linkURL');
					//console.log(showName);
					//console.log(text);
					$jq('<li><a href="'+linkURL+'">'+linkText+'</a></li>').appendTo('.'+showID+'-list-items');
				});
				*/
			});

			// activate hero carousel
			HPCarousels.hpSmallCarousel.activate();
		}
	}
	
}; //END: HPCarousels

$jq(function(){
	// activate homepage carousels
	HPCarousels.init();
});
