// public variables used by multiple function

// which slider is currently visible
var current_slider = 0;

/**
 * Executed when document is finished loading elements into DOM
 */
$(document).ready(function()
{
	/**
	 * Display confirmation dialog whenever link
	 * with className "confirm" is slicked
	 */
	$(".confirm").click(function(){
		return confirm("Vai tiešām vēlaties veikt šo darbību?");
	});

	// smooth scrolling to anchors
	$('a[href*=#]').click(function(){
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
		{
			var $target = $(this.hash);

			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');

			if ($target.length)
			{
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});

	$("#DonationPaymentType").change(function(){
		if ($(this).val() == "2")
		{
			$("#cc-notice").show();
			$('div.input.text span.not-editable').hide();
			$('div.input.text span.editable').show();
		}
		else
		{
			$("#cc-notice").hide();
			$('div.input.text span.not-editable').show();
			$('div.input.text span.editable').hide();
		}
	});

	$("#DonationPaymentType").change();

	$('.year-group hr:last-child').hide();
});

/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 * @param string name (optional) Display value
 */
function writemail(p1, p2, name)
{
	if (typeof(name) == "undefined")
	{
			name = p1+"@"+p2;
	}

	document.write("<a href='mailto:"+p1+"@"+p2+"'>"+name+"</a>");
}

/**
 * Load next slider
 *
 */
function loadNextSlider()
{
	if (++current_slider > slides.length)
	{
		current_slider = 1;
	}

	setSliderPage(current_slider, false);

	// prepare next slider

	var next_slider = current_slider + 1;

	if (next_slider > slides.length)
	{
		next_slider = 1;
	}

	slider_timer = setTimeout("loadNextSlider()", (slides[current_slider - 1] * 1000));
}

/**
 * Show specific slider
 *
 * @param int page Slider page
 */
function setSliderPage(page, manual)
{
	$("div#slider div#slider-items").animate({ top: parseInt(-(page-1) * $("#slider").height()) + "px" }, "normal");

	if (manual)
	{
		// stop automatic slider change if user manualy chooses slider
		clearTimeout(slider_timer);
	}
}

/**
 * Set or remove specified project from "My Projects"
 * list
 *
 * @param int project_id Project ID
 * @return boolean
 */
function setMyProject(project_id)
{
	if ($("#UserMyProject").is(":checked"))
	{
		$.ajax({
			url: webroot+"users/set_my_project/"+project_id,
			cache: false,
			success: function(data){
				return (data == "OK");
			}
		});
	}
	else
	{
		$.ajax({
			url: webroot+"users/del_my_project/"+project_id,
			cache: false,
			success: function(data){
				return (data == "OK");
			}
		});
	}
}

/**
 *
 *
 * @param int project_id Project ID
 * @return boolean
 */
function reportSubscription(project_id)
{
	if ($("#ReportEmail").is(":checked"))
	{
		$.ajax({
			url: webroot+"report_emails/subscribe/"+project_id,
			cache: false,
			success: function(data){
				return (data == "OK");
			}
		});
	}
	else
	{
		$.ajax({
			url: webroot+"report_emails/unsubscribe/"+project_id,
			cache: false,
			success: function(data){
				return (data == "OK");
			}
		});
	}
}

function draugiemSay( title, url, titlePrefix ){
	window.open(
		'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) +
		'&link=' + encodeURIComponent( url ) +
		( titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ),
		'',
		'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400'
	);
}