var globalFeat = '';
var globalSearch = '';
var globalArtist = '';
var globalFormat = '';

function s3store(page, feature, search, artist, format)
{
	globalFeat = feature;
	globalSearch = search;
	globalArtist = artist;
	globalFormat = format;
	var postString = 'page='+page+'&feature='+feature+'&search='+search+'&artist='+artist+'&format='+format;
	var index = AjaxHandler.AddRequest('/s3/ajax/request/store.php', s3storeprocess);
	AjaxHandler.CallUpdate(index, postString, 'POST');
}

function s3storeprocess(responseText, responseStatus)
{
	if (responseStatus==200)
	{
		//alert(responseText);
		var store = JSON.parse(responseText);
		
		Scroller.goToTop();
		
		switch (store.result)
		{
			case 'good':
			
			var rowCount = 0;
			var htmlNavigation = '';
			for (var i = 0; i < store.pagination.pagenav.length; i++)
			{
				if (store.pagination.pagenav[i].link!='')
				{
					htmlNavigation += '<a class="lnk-orange9" href="'+store.pagination.pagenav[i].link+'" title="'+store.pagination.pagenav[i].title+'">'+store.pagination.pagenav[i].title+'</a>';
				}
				else
				{
					htmlNavigation += '<span class="txt-white9">'+store.pagination.pagenav[i].title+'</span>';	
				}
				
				rowCount++;
				
				if (rowCount < store.pagination.pagenav.length)
				{
					htmlNavigation += '<span class="txt-white9">&nbsp;&gt;&nbsp;</span>'
				}
			}

			var htmlPagination1 = '<table cellpadding="0" cellspacing="0" align="left" style="height:10px;border:0px;">';
			htmlPagination1 += '<tr>';
			htmlPagination1 += '<td style="width:7px;">';
			htmlPagination1 += '<a href="javascript:;" onmouseover="doSwap(\'icoprev1\', 1)" onmouseout="doSwap(\'icoprev1\', 0)" onclick="s3store('+store.pagination.previous+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load previous store page">';
			htmlPagination1 += '<img id="icoprev1" src="/images/ico-prev-0.gif" width="4" height="10" border="0" align="middle" class="img-ico" /></a>';
			htmlPagination1 += '</td>';
			htmlPagination1 += '<td>';
			htmlPagination1 += '<a href="javascript:;" onclick="s3store('+store.pagination.previous+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load previous store page" class="lnk-orange9">prev</a>';
			htmlPagination1 += '<span class="txt-white9">&nbsp;['+store.pagination.page+' of '+store.pagination.total+']&nbsp;</span>';
			htmlPagination1 += '<a href="javascript:;" onclick="s3store('+store.pagination.next+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load next store page" class="lnk-orange9">next</a>';
			htmlPagination1 += '</td>';
			htmlPagination1 += '<td style="width:10px;text-align:right;">';
			htmlPagination1 += '<a href="javascript:;" onmouseover="doSwap(\'iconext1\', 3)" onmouseout="doSwap(\'iconext1\', 2)" onclick="s3store('+store.pagination.previous+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load next store page">';
			htmlPagination1 += '<img id="iconext1" src="/images/ico-next-0.gif" width="4" height="10" border="0" align="right" class="img-ico" /></a>';
			htmlPagination1 += '</td>';
			htmlPagination1 += '</tr>';
			htmlPagination1 += '</table>';
			
			var htmlPagination2 = '<table cellpadding="0" cellspacing="0" align="left" style="height:10px;border:0px;">';
			htmlPagination2 += '<tr>';
			htmlPagination2 += '<td style="width:7px;">';
			htmlPagination2 += '<a href="javascript:;" onmouseover="doSwap(\'icoprev2\', 1)" onmouseout="doSwap(\'icoprev2\', 0)" onclick="s3store('+store.pagination.previous+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load previous store page">';
			htmlPagination2 += '<img id="icoprev2" src="/images/ico-prev-0.gif" width="4" height="10" border="0" align="middle" class="img-ico" /></a>';
			htmlPagination2 += '</td>';
			htmlPagination2 += '<td>';
			htmlPagination2 += '<a href="javascript:;" onclick="s3store('+store.pagination.previous+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load previous store page" class="lnk-orange9">prev</a>';
			htmlPagination2 += '<span class="txt-white9">&nbsp;['+store.pagination.page+' of '+store.pagination.total+']&nbsp;</span>';
			htmlPagination2 += '<a href="javascript:;" onclick="s3store('+store.pagination.next+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load next store page" class="lnk-orange9">next</a>';
			htmlPagination2 += '</td>';
			htmlPagination2 += '<td style="width:10px;text-align:right;">';
			htmlPagination2 += '<a href="javascript:;" onmouseover="doSwap(\'iconext2\', 3)" onmouseout="doSwap(\'iconext2\', 2)" onclick="s3store('+store.pagination.previous+', \''+globalFeat+'\', \''+globalSearch+'\', \''+globalArtist+'\', \''+globalFormat+'\')" title="Load next store page">';
			htmlPagination2 += '<img id="iconext2" src="/images/ico-next-0.gif" width="4" height="10" border="0" align="right" class="img-ico" /></a>';
			htmlPagination2 += '</td>';
			htmlPagination2 += '</tr>';
			htmlPagination2 += '</table>';
						
			var htmlStore = '';
			if (store.items.length == 0)
			{
				htmlStore += '<div class="box-nobgr-wide" style="margin-bottom:20px;">\n';
				htmlStore += 'There are no items under this features at the moment. Please try again soon, as we regularly add new items.\n';
				htmlStore += '</div>\n<div class="clear"></div>\n';
			}
			else
			{
				rowCount = 0;
				var itemCount = 0;
				var spnCnt = 0;
				var btnCount = 0;
				
				for (var i = 0; i < store.items.length; i++)
				{
					htmlStore += '<div class="box-nobgr-list" style="width:138px;margin-bottom:'+((store.items.length-itemCount)>3?'40':'20')+'px;">\n';	
					htmlStore += '<a href="'+store.items[i].digilink+'" title="View digital media for '+store.items[i].title+'">';
					htmlStore += '<img class="img-list" src="'+store.items[i].image+'" width="'+store.items[i].width+
					'" height="'+store.items[i].height+'" border="0" alt="'+store.items[i].title+'" /></a>\n';
					
					if (store.items[i].artist_collection=='y')
					{
						if (store.items[i].collection_show=='t')
						{
							htmlStore += '<span class="txt-white11">'+store.items[i].collection_title+'</span><br /><span class="txt-light11">'+store.items[i].title+'</span>';
						}
						else
						{
							for (var x = 0; x < store.items[i].collection_artists.length; x++)
							{
								if (store.items[i].collection_artists[x].display=='y' && store.items[i].collection_artists[x].deleted=='n')
								{
									if (store.items[i].collection_artists[x].previous=='y')
									{
										htmlStore += '<a href="/artists/previous/'+store.items[i].collection_artists[x].page+'" title="View '+store.items[i].collection_artists[x].title+' complete profile" />';
										htmlStore += store.items[i].collection_artists[x].title+'</a>';	
									}
									else
									{
										htmlStore += '<a href="/artists/'+store.items[i].collection_artists[x].page+'" title="View '+store.items[i].collection_artists[x].title+' complete profile" />';
										htmlStore += store.items[i].collection_artists[x].title+'</a>';
									}
								} 
								else
								{ 
									htmlStore += '<span class="txt-white11">'+store.items[i].collection_artists[x].title+'</span>';
								}
								
								if ((x+1) < store.items[i].collection_artists.length)
								{
									htmlStore += '<span class="txt-orange11">'+Globals.CollectionSeperator+'</span>';
								}
							}
							
							htmlStore += '<br /><span class="txt-light11">'+store.items[i].title+'</span>';
						}
					}
					else
					{
						if (store.items[i].artist_page != '')
						{
							htmlStore += '<a href="'+store.items[i].artist_page+'" title="View the complete profile for '+store.items[i].artist_title+'">'+store.items[i].artist_title+'</a>\n';
						}
						else
						{
							htmlStore += '<span class="txt-white11">'+store.items[i].artist_title+'</span>\n';	
						}
						
						htmlStore += '<br /><span class="txt-light11">'+store.items[i].title+'</span>\n';
					}
					
					if (store.items[i].notes!=''){
						htmlStore += '<p class="txt-store-note">'+store.items[i].notes+'</p>\n';
					}
					else
					{
						htmlStore += '<p style="padding:0px;margin:0px;">&nbsp;</p>';
					}
					
					if (store.items[i].fsp.length>0)
					{
						var fspItemsCount = store.items[i].fsp.length;
						var fspStoresCount = store.items[i].storesfp.length;
						/*
						var fspInStockItemCount = 0;
						
						for (var x = 0; x < fspItemsCount; x++)
						{
							if (store.items[i].fsp[x].stock!='0')
							{
								fspInStockItemCount++;	
							}	
						}
						*/
						
						if(store.items[i].hasfree=='y')
						{
							// Do we have any complete downloads?
							var fspDlItemCount = 0;
							for (var x = 0; x < fspItemsCount; x++)
							{
								if (store.items[i].fsp[x].free=='y' && store.items[i].fsp[x].file!=''){
									fspDlItemCount++;	
								}	
							}
							
							if(fspDlItemCount!=0)
							{
								htmlStore += '<div class="dlalbum">';
								htmlStore += '<a href="javascript:;" id="btn-album-'+itemCount+'" rel="album-content'+itemCount+'"></a>';
								htmlStore += '</div>';
								htmlStore += '<div id="album-content'+itemCount+'" class="box-store-menu">';
								htmlStore += '<ul>';
								
								for (var x = 0; x < fspItemsCount; x++)
								{
									if (store.items[i].fsp[x].file!='')
									{					
										if(store.items[i].fsp[x].free=='y')
										{
											htmlStore += '<li>';
											//htmlStore += '<a href="'+store.items[i].digilink+'" title="Download '+store.items[i].fsp[x].format+' for '+store.items[i].title+'">';
											htmlStore += '<a href="http://www.sunseasky.com/download/'+rawurldecode(store.items[i].fsp[x].enct)+'" title="Download '+store.items[i].fsp[x].format+' for '+strip_br(store.items[i].title)+'">';
											htmlStore += store.items[i].fsp[x].format+'</a></li>';
										}
									}
								}
								
								htmlStore += '</ul>';
								htmlStore += '</div>';
								
								itemCount++;
							}
							else
							{
								htmlStore += '<div class="dlalbum-dis">&nbsp;</div>';
							}
						}
						else
						{
							htmlStore += '<div class="buyalbum">';
							htmlStore += '<a href="javascript:;" id="btn-album-'+itemCount+'" rel="album-content'+itemCount+'"></a>';
							htmlStore += '</div>';
							htmlStore += '<div id="album-content'+itemCount+'" class="box-store-menu">';
							htmlStore += '<ul>';
							
							for (var x = 0; x < fspItemsCount; x++)
							{
								if (store.items[i].fsp[x].stock!='0')
								{
									var JSONItemData = ' { "key" : "'+store.items[i].fsp[x].key+'", "price" : "'+
									(store.items[i].fsp[x].special=='0'?store.items[i].fsp[x].price:store.items[i].fsp[x].special)+
									'", "type" : "'+store.items[i].fsp[x].type+'", "quantity" : "1" } ';
																										
									htmlStore += '<li>';
									htmlStore += '<a href="javascript:;" onmouseover="Cart.PriceHighlight(\'pr_'+spnCnt+'\', true)" onmouseout="Cart.PriceHighlight(\'pr_'+spnCnt+'\', false)" onclick="Cart.addItem(1, 0, \''+encodeURIComponent(JSONItemData);
									htmlStore += '\')" title="buy '+store.items[i].title+' ('+store.items[i].fsp[x].format+')">'+store.items[i].fsp[x].format+'&nbsp;'; 
								
									htmlStore += (store.items[i].fsp[x].special!='0'?
									'<s class="txt-dark9">&#36;'+store.items[i].fsp[x].price+
									'</s>&nbsp;<span id="pr_'+spnCnt+'">&#36;'+store.items[i].fsp[x].special+'</span>':
									'<span id="pr_'+spnCnt+'">&#36;'+store.items[i].fsp[x].price)+'</span>\n';
									
									htmlStore += '</a></li>';					
									
									spnCnt++;
								}
							}
							
							if (store.items[i].storesfp.length>0)
							{
								htmlStore += '<li><div style="height: 1px; border-top: 1px dotted #BBBBBB; margin-top: 3px; margin-bottom: 3px; margin-left: 5px; margin-right: 5px;" />&nbsp;</div></li>';
								
								for (var x = 0; x < fspStoresCount; x++)
								{
									spnCnt++;
									htmlStore += '<li><a href="'+store.items[i].storesfp[x].url+'" onmouseover="Cart.PriceHighlight(\'pr_'+spnCnt+'\', true)" onmouseout="Cart.PriceHighlight(\'pr_'+spnCnt+'\', false)" title="View '+store.items[i].storesfp[x].name;
									htmlStore += ' - opens in a new window" target="_blank">'+store.items[i].storesfp[x].name;
									htmlStore += '&nbsp;<span id="pr_'+spnCnt+'">&#36;'+store.items[i].storesfp[x].price+'</span></a></li>';
								}
							}
							
							htmlStore += '</ul>';
							htmlStore += '</div>';
							
							itemCount++;
						}
					}
					
					htmlStore += '</div>\n';
					
					rowCount++;
					
					if (rowCount<3)
					{
						htmlStore += '<div class="box-nobgr-list-spacer" style="width:25px;">&nbsp;</div>\n';
					}
					
					if (rowCount==3)
					{
						htmlStore += '<div class="clear"></div>\n';
						rowCount = 0;
					}
				}
				
				if (rowCount>0 && rowCount<3)
				{
					htmlStore += '<div class="clear"></div>\n';
				}
			}
			
			elPopulate('nav1', htmlNavigation);
			elPopulate('pagi1', htmlPagination1);
			elPopulate('ajxstore', htmlStore);
			elPopulate('nav2', htmlNavigation);
			elPopulate('pagi2', htmlPagination2);
			
			for(i=0;i<itemCount;i++){
				CartMenu.Init("btn-album-"+i, "right-bottom", 200, 'click', 72);
			}
			
			break;
			
			case 'error':
			
			htmlStore = '<div class="box-bgr-error-wide" style="margin:0px 0px 20px 0px;">'+store.message+'</div><div class="clear"></div>';
			elAll('ajxstore', htmlStore, 'box-nobgr-wide', 'inline-block');
			
			break;
		}
	}
	else
	{
		elAll('ajxstore', responseText + ' -- Error Processing Request', 'box-bgr-error-wide', 'inline-block');	
	}
}

function applysrch(keywords)
{
	document.s3srch.s3srchstr.style.color = '#000';
	document.s3srch.s3srchstr.value = keywords;
	s3dosrch();
}

function s3dosrch()
{
	document.s3srch.submit();
}

function s3applyfilter(uri)
{
	if (uri!='')
		top.location.href = uri;
}

function verifyCoupon()
{
	var formobj = document.forms['coupon_form'];
	var erMsg = '<strong>Please correct the following:</strong><br /><br />';
	var erInput = false;
	
	if (formobj.c_promo.value=='')
	{
		erMsg += 'Promotion ID cannot be empty.<br />'
		erInput = true;
		elHighlight('sp_promo', 'txt-red11');
	}
	else
	{
		elHighlight('sp_promo', 'txt-light11');
	}
	
	if (formobj.c_code.value=='')
	{
		erMsg += 'Coupon code cannot be empty.<br />'
		erInput = true;
		elHighlight('sp_code', 'txt-red11');
	}
	else
	{
		elHighlight('sp_code', 'txt-light11');
	}
	
	if (erInput)
	{
		elAll('f_c_er_dis', erMsg, 'box-bgr-er', 'inline-block');
		return false;
	}
	else
	{
		elDisplay('f_c_er_dis', 'none');
		disableBtn('btn-c-sbmt', 'btn-disabled', 'Working...');
		formobj.submit();
		return true;
	} 
}

function rncm(dir)
{
	var postString = 'dir='+dir;
	var index = AjaxHandler.AddRequest('/s3/ajax/request/clrhta.php', rncmpro);
	AjaxHandler.CallUpdate(index, postString, 'GET');
}

function rncmpro(responseText, responseStatus)
{
	if (responseStatus==200)
	{
		//alert(responseText);
		/*
		var result = JSON.parse(responseText);
		
		switch (store.result)
		{
			case 'good':
			// Do Nothing
			break;
			
			case 'error':
			// Do Nothing		
			break;
		}
		*/
	}
	else{
		// Do Nothing	
	}
}
