//makes requests for urls passed from flash
function flashImageTrack(urls)
{
	var img;
	for (var i=0;i<urls.length;i++)
	{
		img = new Image();
		img.src = urls[i];
	}
}

function initFilter()
{
	$('.filter').bind('click', function() {
		//alert( $(this).attr('id') );
		filterList( $(this).attr('id') );
	});
	var cat = getHashParamByName('cat').replace("/","");
	//if there is no query string, check the cookie
	if (cat == "")
	{
		var referringPage = document.referrer;
		var currPageUri = window.location.pathname;
		//if we are coming from a child of the current page or we are on the concerts page
		//and coming from the events details page and we check to see if a filter cookie is set
		if (referringPage.indexOf(currPageUri) != -1 || (currPageUri == "/listen/concerts/" && referringPage.indexOf("/events/") != -1))
		{
			cat = getFilterCookie();
			cat = (cat != null) ? cat : "";
		}
	}
	
	filterList( cat );
}

function getFilterCookieName()
{
	var path = window.location.pathname.split("/");
	var pageName = (path[path.length-1] != "") ?  path[path.length-1] : path[path.length-2];
	var cookieName = "filter" + pageName.replace("-","");
	return cookieName;
}

function checkCategory(categoryId)
{
	var path = window.location.pathname.split("/");
	var parentPageName = (path[path.length-1] != "") ?  path[path.length-2] : path[path.length-3];
	var cookieName = "filter" + parentPageName.replace("-","");
	
	if ($.cookie(cookieName) != categoryId)
	{
		$.cookie(cookieName, "", {path:"/"})
	}
}

function getFilterCookie()
{
	var name = getFilterCookieName();
	return $.cookie(name);
}
function setFilterCookie(cookieVal)
{
	var name = getFilterCookieName();
	$.cookie(name, cookieVal, {path:"/"});
}

function setEventBreadcrumbType(label,uri)
{
	$.cookie("eventBreadcrumbType", label + "|" + uri, {path:"/"});
}

function getEventBreadcrumb()
{
	var data = $.cookie("eventBreadcrumbType");
	var link;
	if (data != null && data != "")
	{
		data = data.split("|");
		link = "<a href='" + data[1] + "'>" + data[0] + "</a>";
	}
	else
	{
		link = "<a href='/events/'>Events</a>";
	}
	$("#breadcrumb").html("&lt; " + link);
}

function getParameterByName( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function getHashParamByName( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.hash );
	if( results == null )
		return "";
	else
		return results[1];
}

			
function filterList(filterName){
	
	setFilterCookie(filterName);
	
	//update filter selection
	$(".filter").removeClass('selected');
	if (filterName == "")
	{
		$("#all").addClass('selected');
	}
	else
	{
		$("#"+filterName).addClass('selected');
	}
	
	
	if (filterName == "")
	{
		$("h3.dateHead").show();
		$(".listItem").show();
		$(".featured").show();
		$(".sponsorLogo").show();
	}
	else
	{
		
		$(".featured").hide();
		$(".listItem").hide();
		$(".sponsorLogo").hide();
		$("."+filterName).show();
		
		
		//code below only used on the events page
		$("h3.dateHead").show();
		
		$("h3.dateHead").each(function() {
			
			var dateHead = $(this);
			var sibs = getSibs(dateHead);
			
			var hidden = true;
			for (var i = 0; i<sibs.length; i++)
			{
				if ($(sibs[i]).is(':visible'))
				{
					hidden = false;
					return;
				}
			}
			if (hidden)
			{
				dateHead.hide();
			}
		});
	}
}
		
		
function getSibs( elem ) {
	var sibs = [];
    $(elem).nextAll().each( function() {
        if (!$(this).hasClass('dateHead')) {
            sibs.push(this);
        }
        else {
            return false;
        }
    });
    return $(sibs);
}