﻿var scrllAmnt = 0;
var currentId = 'i1';
var itotal = 0;
var i = 1;
var cartloop;

$(document).ready( function() {
	
	$('h4').each (function() {
		var title = $(this).html();
		title = title.replace('Available', "Buy")
				.replace('PreOrder', 'Preorder')
				.replace('AlbumDownload', 'MP3');
		
		if ( $.trim(title) == "") 
		{ 
			$(this).css('display', 'none');
		}
				
		$(this).html(title);
	});
		

	$('.page_music #album-info h4').each (  function() {
		var cur = $(this).text();
		$(this).text("Buy " + cur);
	});
	
	if ( $('.page_tours #buy_tik').text() == ""){
		$('.page_tours #tour_btn').css('display','none');
	}
	
	
	
	$(".page_news .news-obj:first").attr("id", "top-news");
	$(".page_tours.detail #more").css("display","none");
	$(".page_default .featured-store .control").css("display","none");
	$(".page_music.detail .control").css("display","none");
	$(".control").css("width", "100%").css("margin-bottom", "10px");
	
	scrllAmnt = parseInt($(".twitter #feed").css("height"));
	
	cycle_timer = setInterval("featureCycle(700)", 8000);
	
	$('.page_music #player #playlist .playlist-item:even:even').css('background-color','#333');
	$('.page_music #track-listing .track:even').css('background-color','#EEE');
	
	// add number ids to images
	i = 1;
	$('.page_gallery #img-viewer .image').each (function() {
		$(this).attr('id','i'+i++);
	});
	
	itotal = i-1;
	
	// add number ids to images
	i = 0;
	$('.page_gallery .scroll-selector .mediaitem').each (function() {
		$(this).attr('id','i'+(itotal-(i++)));
		//$(this).attr('id','i'+(i++));
	});
	
	$('.page_gallery #img-viewer .image:last').css('display','block');
	currentId = $('.page_gallery #img-viewer .image:last').attr('id');
	
	cartloop = setInterval("cartupdate()",4000);
	
	$('#mailing-list-cntnr #userEmail').bind('focus', function() {
		$(this).attr('value','');
	});
		
});

function cartupdate()
{
	var cart_count = $('#cartCount').text();
	if (parseInt(cart_count) > 0) 
	{
		$(".cart").css('background-color','#FAA');
		clearInterval(cartloop);
	}
}

function scroll(direction)
{
	// calculate bounds direction & velocity
	var min = 0;
	var max = parseInt($("#scroll-cntnt").css("width")) * -1;
	var amount = $("#scroll-ctnr").css("width");
	if (direction == "prev"){ amount = parseInt(amount) * -1; }
	
	// calculate move
	var curPos = parseInt($("#scroll-cntnt").css("left"));
	var newPos = parseInt(curPos) - parseInt(amount);
	
	//debug
	//console.log(amount, curPos, newPos, max);

	// boundry check
	if (newPos < max && direction == "next") return;
	if (newPos > min && direction == "prev") newPos = 0;
	
	// scroll
	$("#scroll-cntnt").animate({ left: newPos }, 1000, "easeOutQuad", function () {});
}


function featureCycle(speed) {

    // scroll up
    $(".twitter #feed-long").animate({ top: -scrllAmnt }, speed, "easeOutQuad", function () {
        $(".twitter #feed-long").append($(".twitter #feed-long .tweet").first());
        $(".twitter #feed-long").css("top", "0");
    });

}

function viewImage(obj)
{
	var id = $(obj).attr('id');
	
	if (currentId == id) {}
	else
	{
		$('.image#'+id+' img').attr('src', $('.page_gallery .scroll-selector .mediaitem#' + id +' img').attr('src'));
		$('.image#'+currentId).css('display','none');
		$('.image#'+id).css('display','block');
		currentId = id;
	}
	
}

function onSubmitEmail()
{
	$("#email-capture").fadeOut(500, 'easeInOutQuad');

	$.ajax({
		type: "GET",
		url: "/capture.aspx",
		data: "email=" + $("#userEmail").attr('value'),
		success: function(data) {
			
			console.log(data);
			if(data == "FAIL") {
				alert('Please enter a valid email adress!');
				$("#email-capture").fadeIn(500, 'easeInOutQuad');
			} else {
				$("#email-success").fadeIn(500, 'easeInOutQuad');
			}
			
		}
	});
}
