// 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()
{
	// Load latest tweeets
	//~ if ($("#tweets").length > 0)
	//~ {
		//~ $.ajax({
			//~ url: webroot+lang+"/start/gettweets",
			//~ cache: false,
			//~ success: function(data){
				//~ $("#tweets").html(data);
			//~ }
		//~ });
	//~ }

	if ($('#PurchaseStatus').val() == '0'){
		$('#finished-purchase').hide();
	}
	
	if ($.browser.msie){
		$('#right-side .map-links .bg').css('opacity', '0.35');
	}
	
	window.api = $("#partners").scrollable({
		loop: true
	}).mousewheel().autoscroll({ 
	    api: true 
	});
	
	$('#partners, .prevPage, .nextPage').mouseenter(function(){
		api.stop();
	});

	$('.fancy').fancybox();

	$('.main-image').click(function(){
		$('a.fancy[href="'+$(this).attr('href')+'"]').click();
		return false;
	});

	/**
	 * Display confirmation dialog whenever link
	 * with className "confirm" is slicked
	 */
	$(".confirm").click(function(){
		return confirm("Vai tiešām vēlaties veikt šo darbību?");
	});

	$('#map-areas area').mouseover(function(event){
		shop_id = $(this).attr('id').split('-')[2];

		$('#market-tooltip').html($(this).attr('alt')).show();
	});

	$('#map-areas area').click(function(event){
		$('#market-infos div').hide();
		
		shop_id = $(this).attr('id').split('-')[2];
		
		$('#market-'+shop_id).show();
		$('#market-'+shop_id+' div').show();
	});

	$('#map-areas area, #market-tooltip').mousemove(function(event){
		$('#market-tooltip').css('top', event.pageY - 30);
		$('#market-tooltip').css('left', event.pageX + 10);
	});

	$('#map-areas area, img#map').mouseleave(function(){
		$('#market-tooltip').hide();
	});


	// 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();
		}
		else
		{
			$("#cc-notice").hide();
		}
	});

	$('#PurchaseStatus').change(function(){
		if ($(this).val() == '0'){
			$('#finished-purchase').hide();
		}else{
			$('#finished-purchase').show();
		}
	});

	$('.year-group hr:last-child').hide();

	if ($("#gallery-frame").length > 0)
	{
		switch (lang){
			case 'lv':
				gallery_id = 949;
				break;

			case 'ru':
				gallery_id = 978;
				break;

			case 'en':
				gallery_id = 979;
				break;
		}
		loadGalleryImage(gallery_id, 0);
	}
});

/**
 * 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>");
}

function addPollJsField(){
	$('#js-field').html('<input type="hidden" id="PollVoteJsField" value="notempty" name="data[PollVote][js_field]">');
}


/**
 * 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'
	);
}

function showImage(elem){
	$('.gallery-pics a').removeClass('active');
	$(elem).addClass('active');
	$(elem).parent().parent().children('.main-image').attr('href', $(elem).attr('href'));
	$(elem).parent().parent().children('.main-image').css('background-image', 'url('+$(elem).attr('href').replace('large', 'blog-large')+')');
	$(elem).parent().parent().children('.main-image').html('');
	
	return false;
}

function loadGalleryImage(gallery_id, weight)
{
	$.getJSON(webroot+lang+"/gallery_images/getImage/"+gallery_id+"/"+weight,
	function(data){

		if (data != null)
		{
			$("#gallery-frame a img").fadeOut("normal", function(){
				$("#gallery-frame a").attr("href", webroot+lang+'/info/'+data.gallery_url);
				$("#gallery-frame a img").attr("src", webroot+"img/galleries/sidebar/"+data.filename).fadeIn();
			});

			$("#gallery-frame div a").fadeOut("normal", function(){
				$("#gallery-frame div a").attr("href", webroot+lang+'/info/'+data.gallery_url);
				$("#gallery-frame div a").text(data.title).fadeIn();
			});

			setTimeout("loadGalleryImage("+data.gallery_id+", "+data.weight+")", 5000);
		}
	});
}

