var _items;
var NO_ITEMS_HTML = "Sorry, there are no items available for the selected category and team. Please make another selection."


function changeCategory(category, header, theelement) {
	setCategory(category); 
	writeThumbnailHdrText(header); 
	setCurrentNav(theelement); 
	getCategory();
	
}

function initFeaturedCollection(collectionElement, path)
{
  new Ajax.Request('/mlb/ContentService?jsonVar=Collection&path=' + path,
  {
    method:'get',
    onSuccess: function(transport) {
    	eval(transport.responseText);
    	buildFeaturedCollection(collectionElement, Collection, path);
    	}
  });
}

function buildFeaturedCollection(collectionElement, collection, path)
{
	collectionElement.innerHTML = "";

	var headline = document.createElement("span");
	headline.className='headline';

	if (collection.displayName == 'New Player Cards!') {
		var caption1 = document.createElement("span");
		caption1.innerHTML = "New&nbsp;";
		var img = document.createElement("img");
		img.src = "/mlb/img/topps15.gif";
		var caption2 = document.createElement("span");
		caption2.innerHTML = "&nbsp;Cards!";
		headline.appendChild(caption1);
		headline.appendChild(img);
		headline.appendChild(caption2);
//	} else if (collection.displayName == 'Topps Milestones') {
//		var img = document.createElement("img");
//		img.src = "/mlb/img/etopps15whiteback.gif";
//		var caption2 = document.createElement("span");
//		caption2.innerHTML = "&nbsp;Milestones";
//		headline.appendChild(img);
//		headline.appendChild(caption2);
	} else {
		var caption1 = document.createElement("span");
		caption1.innerHTML = collection.displayName;
		headline.appendChild(caption1); //innerHTML = collection.displayName;
	}
	collectionElement.appendChild(headline);
	
	var thumbnail = document.createElement("span");
	thumbnail.className='thumbnail';
	var img = document.createElement("img");
	img.src = collection.smallIcon_def;
	
	thumbnail.appendChild(img);
	collectionElement.appendChild(thumbnail);
	
	var blurb = document.createElement("span");
	blurb.className = 'blurb';
	blurb.innerHTML = collection.description_def;
	collectionElement.appendChild(blurb);
	
	var catLink = document.createElement("span");
	catLink.className = 'catLink';
	var link = document.createElement("a");
	link.href = "#";
	link.onclick = function() {javascript:setCategory(path); writeThumbnailHdrText(collection.displayName); getCategory();};
	link.innerHTML = 'View All';
	catLink.appendChild(link);
	collectionElement.appendChild(catLink);

}

function catalogPageInit()
{
	initFeaturedCollection($('featuredCollection1'), '/live/SpecialCollections'); 
	initFeaturedCollection($('featuredCollection2'), '/live/SpecialCollections2'); 

	if (document.URL.indexOf('toppsplayers') > -1) {
		setCategory('PLAYERS_08');
		setCurrentNav($('PLAYERS_08_ANCHOR'));
		writeThumbnailHdrText('Topps Players'); 
	} else if (document.URL.indexOf('toppsclassics') > -1) {
		setCategory('PLAYERS_GREATS');
		setCurrentNav($('PLAYERS_GREATS_ANCHOR'));
		writeThumbnailHdrText('Topps Classics'); 
	} else if (document.URL.indexOf('toppsmilestones') > -1) {
		setCategory('/live/SpecialCollections2');
		writeThumbnailHdrText('eTopps Milestones'); 
	} else if (document.URL.indexOf('toppslatest') > -1) {
		setCategory('/live/SpecialCollections2');
		writeThumbnailHdrText('eTopps Latest'); 
	} else {
		setCategory('HOME');
		setCurrentNav($('HOME_ANCHOR'));
		writeThumbnailHdrText('Top Picks'); 
	}
	getCategory();
	
	_purchaseBox.makeSureThePurchaseButtonIsInTheRightState();
	
	
}

function getCategoryPath()
{
	var path = "";
	var currentCategory = $('currentCategoryField').value;
	
	if (currentCategory.indexOf('SpecialCollection') > -1)
	    return currentCategory;
		
	var currentTeam = storefrontClub;
	if (storefrontClub == 'mlb')
		currentTeam = $('teamSelect').options[$('teamSelect').selectedIndex].value;
		
    return "/live/" + currentTeam + "/" + currentCategory;
}

function getCategory()
{  
  var path = getCategoryPath();
  
  
  $('catalogSpinner').style.display='block';
  $('thumbnailSection').innerHTML = "Loading...";
  new Ajax.Request('/mlb/ContentService?jsonVar=Category&path=' + path,
  {
    method:'get',
    onSuccess: function(transport) {
    	eval(transport.responseText);
    	displayCategory(Category.items);
    	},
    onFailure: function(){ $('thumbnailSection').innerHTML = NO_ITEMS_HTML; },
    onComplete: function() { $('catalogSpinner').style.display='none'; }
  });
  trackClick("/browse" + path,null);
}

function setCategory(category)
{
	$('currentCategoryField').value=category;	
	if ($('selectClubDiv') && isFilterableByTeam(category))
		$('selectClubDiv').style.display = 'block';
	else 
		$('selectClubDiv').style.display = 'none';
}	



function displayCategory(items)
{
	_items = items;
	var thumbnailSection = document.getElementById("thumbnailSection");

	thumbnailSection.innerHTML="";
	thumbnailSection.scrollTop = 0;
	
	if (items.length == 0)
	{
		thumbnailSection.innerHTML=NO_ITEMS_HTML;
		return;
	}
	
	var currentHolderDiv;
	var contentCount = 0;
	for (var i=0; i<items.length; i++)
	{
		if (contentCount % 5 == 0) {
			currentHolderDiv = document.createElement("div");
			currentHolderDiv.className="thumbnailholder";
			currentHolderDiv.id="thumbnailholder_" + contentCount;
			thumbnailSection.appendChild(currentHolderDiv);
		}
		if (items[i].type == "wallpaper")
		{
		    contentCount++;
			var thumbnail = document.createElement("div");
			thumbnail.className="thumbnail";
			var img = document.createElement("img");
			img.src = items[i].smallIcon_def;
			img.id = "thumbnail_" + i;
			img.onclick = function() {previewImage(this)};
			thumbnail.appendChild(img);
			
			var caption = document.createElement("p");
			if (items[i].displayName.length > 30) {
				caption.innerHTML=items[i].displayName.substring(0,27) + "...";
			} else {
				caption.innerHTML=items[i].displayName
			}
			thumbnail.appendChild(caption);
			currentHolderDiv.appendChild(thumbnail);
		}
		else if (items[i].type == "ringtones")	
		{
		    contentCount++;
			var thumbnail = document.createElement("div");
			thumbnail.className="thumbnail";
			var img = document.createElement("img");
			if (items[i].smallIcon_def) {
				img.src = items[i].smallIcon_def;
			} else {
				img.src = "/mlb/img/musicnoteMUSIC_80x80.jpg";
			}
			img.id = "thumbnail_" + i;
			img.onclick = function() {previewRingtone(this)};
			thumbnail.appendChild(img);
			
			var caption = document.createElement("p");
			if (items[i].displayName.length > 30) {
				caption.innerHTML=items[i].displayName.substring(0,27) + "...";
			} else {
				caption.innerHTML=items[i].displayName
			}
			thumbnail.appendChild(caption);
		
			currentHolderDiv.appendChild(thumbnail);
		}
		
	}
}


// write thumbnail header using sifr
function writeThumbnailHdrText(name) {
	 document.getElementById('thumbnailHdrText').className = '';
	 if (name == 'Topps Players') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var img = document.createElement("img");
		img.src = "/mlb/img/topps15.gif";
		var caption = document.createElement("span");
		caption.innerHTML = "&nbsp;Players";
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption);
	 } else if (name == 'Topps Players 2007') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var img = document.createElement("img");
		img.src = "/mlb/img/topps15.gif";
		var caption = document.createElement("span");
		caption.innerHTML = "&nbsp;Players 2007";
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption);
	 } else if (name == 'Topps Players 2008') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var img = document.createElement("img");
		img.src = "/mlb/img/topps15.gif";
		var caption = document.createElement("span");
		caption.innerHTML = "&nbsp;Players 2008";
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption);
	 } else if (name == 'Topps Classics') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var img = document.createElement("img");
		img.src = "/mlb/img/topps15.gif";
		var caption = document.createElement("span");
		caption.innerHTML = "&nbsp;Classics";
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption);
	 	// document.getElementById('thumbnailHdrText').innerHTML = "<img src=\"/mlb/img/topps20.gif\"> Classics";
	 } else if (name == 'eTopps Milestones') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var img = document.createElement("img");
		img.src = "/mlb/img/etopps15.gif";
		var caption = document.createElement("span");
		caption.innerHTML = "&nbsp;Milestones";
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption);
	 	// document.getElementById('thumbnailHdrText').innerHTML = "<img src=\"/mlb/img/topps20.gif\"> Classics";
	 } else if (name == 'eTopps Latest') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var img = document.createElement("img");
		img.src = "/mlb/img/etopps15.gif";
		var caption = document.createElement("span");
		caption.innerHTML = "&nbsp;Latest";
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption);
	 	// document.getElementById('thumbnailHdrText').innerHTML = "<img src=\"/mlb/img/topps20.gif\"> Classics";
	 } else if (name == 'New Player Cards!') {
	 	document.getElementById('thumbnailHdrText').innerHTML = "";
		var caption1 = document.createElement("span");
		caption1.innerHTML = "New&nbsp;";
		var img = document.createElement("img");
		img.src = "/mlb/img/topps15.gif";
		var caption2 = document.createElement("span");
		caption2.innerHTML = "&nbsp;Cards!";
		document.getElementById('thumbnailHdrText').appendChild(caption1);
		document.getElementById('thumbnailHdrText').appendChild(img);
		document.getElementById('thumbnailHdrText').appendChild(caption2);
	 	// document.getElementById('thumbnailHdrText').innerHTML = "<img src=\"/mlb/img/topps20.gif\"> Classics";
	 } else {
	 	 document.getElementById('thumbnailHdrText').innerHTML = name;
	 } 
}


function setCurrentNav(anchor) {
	var thisCategoryName;
	var _catalogLinks = document.getElementById('categoryNavTop').getElementsByTagName('a');
	for ( var i=0; i<_catalogLinks.length; i++)
	{
		_catalogLinks[i].className = '';
	}
	_catalogLinks = document.getElementById('categoryNavWall').getElementsByTagName('a');
	for ( var i=0; i<_catalogLinks.length; i++)
	{
		_catalogLinks[i].className = '';
	}
	_catalogLinks = document.getElementById('categoryNavRing').getElementsByTagName('a');
	for ( var i=0; i<_catalogLinks.length; i++)
	{
		_catalogLinks[i].className = '';
	}
	anchor.className = 'current';
	anchor.blur();
}

function previewImage(thumbnail)
{	

	// hide preview text, mp3 player
	document.getElementById('previewText').style.display     = 'none';
	document.getElementById('flashcontent').style.visibility = 'hidden';
	
	document.getElementById("fullSizePreview").style.visibility = 'visible';
	var itemIdx = thumbnail.id.substring("thumbnail_".length, thumbnail.id.length);
	document.getElementById("fullSizePreview").src = _items[itemIdx].largeIcon_def;
	_purchaseBox.setItemToPurchase(_items[itemIdx]);
	trackClick("/preview/image" + _items[itemIdx].discoveryPath,null);
}

function previewRingtone(thumbnail)
{
	document.getElementById('previewText').style.display = 'none';

	document.getElementById("fullSizePreview").style.visibility = 'visible';
	var itemIdx = thumbnail.id.substring("thumbnail_".length, thumbnail.id.length);
	document.getElementById("fullSizePreview").src = "/mlb/img/176x220_mlbmobile_tones.jpg";
	playSong(_items[itemIdx].displayName, _items[itemIdx].largeIcon_def, 'pathid');		
	_purchaseBox.setItemToPurchase(_items[itemIdx]);
}

function isFilterableByTeam(category)
{
	return category == 'LOGOS' 
	|| category == 'HOME'
	|| category == 'VINTAGE'
	|| category == 'JERSEYS'
	|| category == 'PLAYERS_CURRENT'
	|| category == 'PLAYERS_08'
	|| category == 'PLAYERS_GREATS'
	|| category == 'MASCOTS'
	|| category == 'MOMENTS';
}

