function confirmStarRating(data) {
	if(data.substring(0,5) != 'rated') { 
		$(".starRating").data("stars").select(0);
		alert(data);
	}
}


$(document).ready(function() {
	
	if($("#search #query").length) {
		if($("#search #query").val() == '') {
			$("#search #query").val('Search');
		}
		$("#search #query").focus(function(){
			if (this.value == 'Search') {
				this.value='';
			}
		});
		
		$("#search #query").blur(function(){
			if (this.value == '') {
				this.value='Search';
			}
		});
	}
	
	// Add pdf icons to pdf links
	$("a[href$='.pdf']").addClass("pdf");
	
	// Add pdf icons to pdf links
	$("a[href*='.doc']").addClass("doc");
	 
	// Add zip icons to ppt links
	$("a[href*='.ppt']").addClass("ppt");
	
	// Add email icons to email links
	$("a[href^='mailto:']").addClass("email");

	//Add external link icon to external links - 
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname && this.hostname !== location.hostname;
	}).addClass("external").attr({ target: "_blank" });

	if($('#slideshow').length > 0) {
		$('#slideshow').cycle({
			fx:     'fade',
			speed:   400,
			timeout: 5000,
			pager:  '#pager',
			pause: true,
			pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
				return '#pager li:eq(' + (idx) + ') a';
			}
		});

		setTimeout(function(){
			$(".success").fadeOut("slow", function () {
				$(".success").remove();
			});
		}, 3000);
	}
 
 
 	$(".subcategories").each(function(){
        $(this).css("height", $(this).height()+"px");
        $(this).hide();
    });
 
	$(".trigger").click(function(){
		$(this).nextAll(".subcategories").eq(0).slideToggle("normal");
		$(this).toggleClass("active");
	});
	
	$('#commentForm').hide();
	
	$('.addComment').focus(function(){
		$('#commentForm').slideToggle('fast');
		$(this).hide();
		return false;		
	});
	
	$('.submitComment').submit(function(){
		$('#commentForm').hide();
		$('.addComment').show();
		return false;		
	});
	
	$('.cancelComment').click(function(){
		$('#commentForm').hide();
		$('.addComment').show();
		return false;		
	});
	
	$("#controls, .edit").hide();
	
	$("#share .article").hover(function() {
			$(this).find(".edit, .delete a").fadeIn();
				}, function() {
				$(this).find(".edit, .delete a").fadeOut();
	});

    
    $('#slideshowSub').hover(
        function() { $("#controls").fadeIn(); },
        function() { $("#controls").fadeOut(); }
    );
    
	if($('.slides').length > 0) {
		$('.slides').cycle({
			fx:     'fade',
			speed:   400,
			timeout: 5000,
			next:   '.next',
			prev:   '.prev'
		});
	}
	
	$(".colorbox").colorbox({href: function(){
     $(this).attr('href');}, initialHeight:"50px", initialWidth:"50px", maxHeight:"70%", maxWidth:"600px", transition:"none", opacity:.8});
	
	$(".emailThis").colorbox({inline:true, href:"#emailThis", transition:"none", opacity:.8});

	if($('form[name="star_rating"]').length > 0) 
	{
		$('form[name="star_rating"]').ajaxForm({ success: confirmStarRating });
		
		$(".starRating").stars({
			oneVoteOnly: true,
			inputType: "select",
			callback: function(ui, type, value)
			{
				//return false;
				$('form[name="star_rating"]').submit();
			}
		});
	}
	
	$('.favorites a, .favoritesRemove a') .click (function() {
		var caller = $(this);
		$.get(this.href, function(data) {
			data = $.trim(data);
			var count = 0;
			var link = $(caller).attr("href");
			var text = $(caller).text();
			text = $.trim(text);
			
			count = text.substring(text.indexOf('(')+1,text.indexOf(')'));
			
			if(data.substring(0,5) == "added") {
				count++;
				$(caller).parent().removeClass('favorites').addClass('favoritesRemove');
				$(caller).attr("href", link+'delete/');
				$(caller).text('Remove from Favorites ('+count+') ');
			} else if (data.substring(0,7) == "deleted") {
				count--;
				$(caller).parent().removeClass('favoritesRemove').addClass('favorites');
				$(caller).attr("href", link.substring(0, link.length-7));
				$(caller).text('Add to Favorites ('+count+') ');
			} else {
				alert(data);
			}
		});
		return false;
	});
});

function updateFilters(cat_id, action)
{
	var filter_list = $.cookie('filter_list');
	var filter_list_array = new Array();
	var separator = "";
	var updated_filter_list = "";
	if(filter_list != null && filter_list != "" && filter_list != "|")
	{
		filter_list_array = filter_list.split('|');
	}
	
	for (var i = 0; i < filter_list_array.length; i++)
	{ 
        if(cat_id != filter_list_array[i])
		{
			updated_filter_list = updated_filter_list + separator + filter_list_array[i];
			separator = "|";
		}
    }
	if(action == "add")
	{
		updated_filter_list = updated_filter_list + separator + cat_id;
		$("#link_"+cat_id).html('<a href="/schedule/filter_list/remove" onclick="updatefilter_list(\''+cat_id+'\', \'remove\'); return false;">Remove from filter_list</a>');
	} else {
		$("#link_"+cat_id).html('<a href="/schedule/filter_list/add" onclick="updatefilter_list(\''+cat_id+'\', \'add\'); return false;">Add to filter_list</a>');
		if($("#row_"+cat_id).length>0)
		{
			$("#row_"+cat_id).remove();
			$('#filter_list_table').alternateRowColors();
			if(!$('tbody tr').length>0)
			{
				$("#filter_list_table > tbody").append("<tr><td><p>You have no events added to your filter_list</p></td></tr>");
			}
		}
	}
	$.cookie('filter_list', updated_filter_list, { expires: 365, path: '/' });
}
