$(document).ready(function() {

	$(function() { 
        // Setup the player to autoplay the next track
        var as = audiojs.createAll({
          trackEnded: function() {
			var next = $('ol li.playing').next();
			if (!next.length) {next = $('ol li').first();}
			var nexta = next.children('a');
			var nextSong = nexta.attr('data-src');
			var t = setTimeout(function() {
				audio.load(nextSong);
				audio.play();
				next.addClass('playing').siblings().removeClass('playing');
				$(".trackName").text($('li.playing').text())
			},500);
          }
        });
		
        // Load in the first track
        var audio = as[0];
        first = $('ol a').attr('data-src');
        $('ol li').first().addClass('playing');
        var b = setTimeout(function() {
			audio.load(first);
		}, 2000);
		
        // Load in a track on click
        $('ol li').click(function(e) {
          e.preventDefault();
          $(this).addClass('playing').siblings().removeClass('playing');
		  audio.load($('a', this).attr('data-src'));
		  audio.play();
		  $(".trackName").text($('li.playing').text())
        });
        // Keyboard shortcuts
        $(document).keyup(function(e) {
          var unicode = e.charCode ? e.charCode : e.keyCode;
             // right arrow
          if (unicode == 39) {
            var next = $('li.playing').next();
            $(".trackName").text($('li.playing').text())
			if (!next.length) next = $('ol li').first();
            next.click();
			// back arrow
          } else if (unicode == 37) {
            var prev = $('li.playing').prev();
            $(".trackName").text($('li.playing').text())
			if (!prev.length) prev = $('ol li').last();
            prev.click();
            // spacebar
          } 
        })
		
		$(".next").click(function() {
		    var next = $('li.playing').next();
            if (!next.length) next = $('ol li').first();
            next.click();
			$(".trackName").text($('li.playing').text());
		});
		
		$(".prev").click(function() {
		    var prev = $('li.playing').prev();
            if (!prev.length) prev = $('ol li').last();
            prev.click();
			$(".trackName").text($('li.playing').text())		
		});
		
	});

	var images = new Array();
	
	function pickRandomImages() {
		$(".showcaseimg").hide();
		var uniqueNums = new Array;
		for (var i=0; i<3; i++) {
			var randomNum = Math.floor(Math.random() * 23)
			var arrayHas = uniqueNums.indexOf(randomNum)
			while (arrayHas > -1) {
				randomNum = Math.floor(Math.random() * 23)
				arrayHas = uniqueNums.indexOf(randomNum)
			}
			uniqueNums.push(randomNum);
			var randomImgSRC = "http://www.zongojunction.net/img/zongoimg/zongoimg" + randomNum + ".jpg"
			var randomImgID = "zongoimg" + randomNum
			if (images.indexOf(randomImgID) > -1 ) {			
				$("#" + randomImgID).show();
			} else {
				$('.showcase').append('<img class="showcaseimg" id=' + randomImgID + ' src=' + randomImgSRC + '></img>');	
				}	
			images.push(randomImgID);
			}
		}
	
	pickRandomImages()
	
	$(".navLink").click(function() {
		var thisLinkID = $(this).attr("id")
		var thisSection = thisLinkID.slice(0,thisLinkID.length-4)
		var thisSectionID = "#" + thisSection
		if ($(thisSectionID).length == 0) {
			var thisPage = "/" + thisSection
			$.get(thisPage, function(data) {
				//var newSection = $(data).find(thisSectionID)
				$(".panel").hide();
				$("#content").append(data);
			}, "html");
			pickRandomImages()
		} else {
			$(".panel").hide();
			$(thisSectionID).show();
			pickRandomImages()
		}
	})
	
	$(".logo").click(function() {
			$(".panel").hide();
			$("#home").show();
			pickRandomImages()		
	});
	
	$(".gallery").prettyPhoto({
	});

	$(".productFilter").hover(function() {
		$(this).css("background-color","#9d7629");
	}, function() {
		$(this).css("background-color","transparent");	
	});
	
	function autorotate() {
		var marginLeftPX = $(".carouselRibbon").css("margin-left");
		var marginLeft = parseInt(marginLeftPX);
		var thisDotNumber;
		if (marginLeft == -1200) {
			marginLeft = -1800;
			thisDotNumber = 4;
		} else if (marginLeft == -600) {
			marginLeft = -1200;
			thisDotNumber = 3;
		} else if (marginLeft == 0) {
			marginLeft = -600;
			thisDotNumber = 2;		
		} else if (marginLeft == -1800) {
			marginLeft = 0;
			thisDotNumber = 1;
		}
		var thisDot = "#dot" + thisDotNumber;
		$(".carouselRibbon").animate({
			"marginLeft" : marginLeft
		}, {duration: 1000, 
		step: 
			function() {
				$(".dot").removeClass("dotSelected");
				$(".dot").addClass("dotUnselected");
				$(thisDot).removeClass("dotUnselected");
				$(thisDot).addClass("dotSelected")
			}
		});
	}
	
	var rotator = setInterval(autorotate, 5000);
	
	$(".dot").click(function() {	
		rotator = window.clearInterval(rotator);		
		var thisNum = $(this).attr("id").substring(3);
		var shotID = "#featured" + thisNum;
		if (thisNum == 1) {
			$(".carouselRibbon").animate({
				"margin-left":"0px"},
				1000
				);
		} else if (thisNum == 2) {
			$(".carouselRibbon").animate({
				"margin-left":"-600px"},
				1000
				);
		} else if (thisNum == 3) {
			$(".carouselRibbon").animate({
				"margin-left":"-1200px"},
				1000
			);
		} else {
			$(".carouselRibbon").animate({
				"margin-left":"-1800px"},
				1000
			);		
		}
		var otherDots = $(".dot").not($(this));
		otherDots.removeClass("dotSelected");
		otherDots.addClass("dotUnselected");
		$(this).removeClass("dotUnselected");
		$(this).addClass("dotSelected");
		rotator = window.setInterval(autorotate, 5000);
	});	
	
});
