$(document).ready(function() {
	// Ellipse it so
	$(".ellipse").ellipsis(true);
	
	// Feature Module
	$('#FeatureModule .FeatureNavItem').each(function(i) {
		$(this).bind('click', function() {
			$(document).stopTime("featurerotate");
			bottom = (-186) * i;
			$('#FeatureItems').animate({
				marginTop: bottom + 'px'
			},800);
		});
	});
	
	$('.FeatureNavItem').hover(
		function() {
			$(this).toggleClass('FeatureNavItemHover');
		},
		function() {
			//$(this).removeClass('FeatureNavItemHover');
			$(this).toggleClass('FeatureNavItemHover');
		}
	);
	
	// Feature Module timer
	if ($('#FeatureItems').length > 0) {
		$(document).everyTime(5000, "featurerotate", function(i) {
			if (i % 3 == 0) {
				bottom = 0;
			}
			else if (i % 3 == 1) {
				bottom = (-186);
			}
			else {
				bottom = (-186) * 2;
			}
			$('#FeatureItems').animate({
				marginTop: bottom + 'px'
			},800);
		}, 0);
	}
	
	maxpage = $('.PageContainer .PageContent').length;
	totalpages = $('.Pager .Pages .Page').length;
	if (typeof pager_perpage != "undefined") perpage = pager_perpage;
	
	var currenthash = location.hash.toString();
	
	if (currenthash != ""){
		currentpage = currenthash.split('page')[1];
		if(currentpage <= totalpages) {
			
			GetPageData(currentpage, $($('.Pager .Pages .Page')[currentpage-1]));
			MovePage(currentpage, $($('.Pager .Pages .Page')[currentpage-1]));
		}
	} else
		currentpage = 1;
	
	// Movies
	moviefeatures = $('#movies .MainContentModule .VideoPlayer .Container .Inner').children();
	$("#movies .MainContentModule .VideoPlayer .Overlay").bind('click', function() {
		$(document).stopTime("moviefeaturerotate");
		MovieFeatureOnClick($(moviefeatures[0]));
	});
	
	movieindex = 0;
	$("#movies .MainContentModule .VideoPlayer .Carousel .Items .Item").each(function(i) {
		$(this).bind('click', function() {
			$(document).stopTime("moviefeaturerotate");
			movieindex = i;
			MoveMovieFeature(this,i);
		 });
	});
	
	$("#movies-carousel-prev").bind('click',function() {
		var sel = "#movies .MainContentModule .VideoPlayer .Carousel .Items .Item";
		if(movieindex != 0) {
			$(document).stopTime("moviefeaturerotate");
			var item = $(sel)[movieindex-1];
			MoveMovieFeature(item,movieindex-1);
			--movieindex;
		}
	});
	
	$("#movies-carousel-next").bind('click',function() {
		var sel = "#movies .MainContentModule .VideoPlayer .Carousel .Items .Item";
		if(movieindex < $(sel).length-1) {
			$(document).stopTime("moviefeaturerotate");
			var item = $(sel)[movieindex+1];
			MoveMovieFeature(item,movieindex+1);
			++movieindex;
		}
	});
	
	// Feature Module timer
	if (moviefeatures.length > 0) {
		$(document).everyTime(5000, "moviefeaturerotate", function(j) {
			var sel = "#movies .MainContentModule .VideoPlayer .Carousel .Items .Item";
			movieindex = j % 4;			
			if(movieindex < $(sel).length) {
				var item = $(sel)[movieindex];
				MoveMovieFeature(item,movieindex);
			}
		}, 0);
	}
	
	// Pager
	$('.Pager .Pages .Page').each(function(i) {
		$(this).bind('click', function(e) {
			button = $(this);
			if(maxpage < (i+1)) {
				GetPageData(i+1,button);
				MovePage(i+1,button);
			}
			else {
				MovePage(i+1,button);
			}
		});
	});
	
	$('.Pager .Pages .Next').bind('click', function(e) {
		if(maxpage < (currentpage+1)) {
			GetPageData(currentpage+1,$($('.Pager .Pages .Page')[currentpage]));
			MovePage(currentpage+1,$($('.Pager .Pages .Page')[currentpage]));
		}
		else {
			MovePage(currentpage+1,$($('.Pager .Pages .Page')[currentpage]));
		}
		window.location.hash = "#page" + currentpage;
		$('.Pager .Pages .Prev').css('visibility','visible');
	});
	
	$('.Pager .Pages .Prev').bind('click', function(e) {
		MovePage(currentpage-1,$($('.Pager .Pages .Page')[currentpage-2]));
		if(currentpage == 1) $(this).css('visiblity','hidden');		
	});
});

function MovePage(page,button) {
	total = pager_itemtotal;
	types  = pager_itemtypes;
	start = (page -1) * perpage + 1;
	end   = (((start-1) + perpage) > total) ? total : (start-1) + perpage;	
	
	$('.Pager .Pages .Page.Selected').removeClass('Selected');
	button.addClass('Selected');
	currentpage = page;
	$('.Pager .Pages .Prev').css('visibility',((page == 1) ? 'hidden' : 'visible'));
	$('.Pager .Pages .Next').css('visibility',((page == totalpages) ? 'hidden' : 'visible'));
						
	left = (-545) * (page-1);
	$('.PageContainer').animate({
		marginLeft: left + 'px'
	},800);
	
	$('.Pager .Information').html('<span>Showing <strong>' + start + ' - ' + end + ' of ' + total + ' ' + types + '</strong></span>');
}

function MoveMovieFeature(item,i) {
	x = (-495) * i;
	$('#movies .MainContentModule .VideoPlayer .Container .Inner').animate({
		marginLeft: x + 'px'
	},800);
	$("#movies .MainContentModule .VideoPlayer .Carousel .Items .Item").removeClass("Selected");
	$("#VideoPlayerTitle").text($(item).children("img").attr("alt"));
	$("#movies .MainContentModule .VideoPlayer .Overlay").unbind();
	$("#movies .MainContentModule .VideoPlayer .Overlay").bind('click', function() {
		MovieFeatureOnClick($(moviefeatures[i]));
	});
	$(item).addClass("Selected");
}

function MovieFeatureOnClick(feature) {
	h = $(feature).attr("href");
	t = $(feature).attr("target");
	if(t == "") {
		document.location.href = h;
		return false;
	}
	else {
		var x = window.open(h);
		return false;
	}
}

function GetPageData(page,button) {
	$.ajax({
		cache: true,
		data: 'start=' + maxpage + '&end=' + (page+1),
		dataType: 'html',
		url: pager_itemupdate,
		success: function(response,status) {
			$('.PageContainerModule .PageContainer').append(response);
			maxpage = (page+1);
			return true;
		},
		type: 'POST'
	});
}

// via: http://ajaxian.com/archives/text-overflow-for-firefox-via-jquery
(function($) {
	$.fn.ellipsis = function(enableUpdating){
		var s = document.documentElement.style;
		if (!('textOverflow' in s || 'OTextOverflow' in s)) {
			return this.each(function(){
				var el = $(this);
				if(el.css("overflow") == "hidden"){
					var originalText = el.html();
			        var w = el.width();
			        var t = $("#_ellipsis_calc");
			        var ex = t.length < 1;
					
			        if(ex){
			        	t = $("<span id='_ellipsis_calc'/>").html(originalText).hide().appendTo("body");
			        }
					
					var text = originalText;
			        while(text.length > 0 && t.width() > el.width()){
			        	text = text.substr(0, text.length - 1);
			        	t.html(text + "...");
			        }
			        el.html(t.html());
			        t.remove();
			        
			        if(enableUpdating == true){
						var oldW = el.width();
			       		setInterval(function(){
				      		if(el.width() != oldW){
				      			oldW = el.width();
				      			el.html(originalText);
				      			el.ellipsis();
				      		}
				      	}, 200);
			        }
			    }  
			});   
	    } else return this;   
	};
})(jQuery);	

function PlayTopTrackSample(id,f) {
	$.ajax({
		cache: true,
		data: 'id=' + id + '&f=' + f,
		dataType: 'json',
		url: '/ajax/musiclisten.php',
		success: function(response,status) {
			if(response != undefined || response != null) {
				$('#TrackListenContainer').css('visibility','visible');
				$('#TrackDetailsListenInfo').css('display','none');
				$('#TrackDetailsInfo').css('visibility','visible');
				$('#TrackDetailsArtist').html(response.artist_name).ellipsis();
				$('#TrackDetailsAlbum').html(response.album_name).ellipsis();
				$('#TrackDetailsTrack').html(response.track_name).ellipsis();
				$('#TrackDetailsBuy').html('<a class="Buy" href="' + response.url + '">Info</a>');
				$('#TrackDetailsAlbumBuy').html('<a class="Buy" href="' + response.url + '">Info</a>');
				$('#TrackDetailsAlbumPrice').html("$" + (response.album_price/100).toFixed(2));
				$('#TrackDetailsPrice').html("$" + (response.track_price/100).toFixed(2));
				$('.LineItemHidden').css('visibility','visible');
				playMp3AudioSample(response.id,response.listen_url);		
				
			}
			return true;
		},
		type: 'POST'
	});	
}