$(document).ready(function()
{
    var search_txt = Array();
    search_txt["lv"] = "meklēt";
    search_txt["en"] = "search";
    search_txt["ru"] = "поиск";

    $("#SearchKeyword").focus(function(){
        if ($(this).val() == search_txt[lang])
        {
            $(this).val("");
        }
    });

    $("#SearchKeyword").blur(function(){
        if ($(this).val() == "")
        {
            $(this).val(search_txt[lang]);
        }
    });

    blurLinks();
});

/**
* Hook every A element so blur() function is called
* when onclick event is triggered
*/
function blurLinks()
{
	$("a").focus(function()
	{
		this.blur();
	});
}

/**
 * Display confirmation dialog and return results
 */
function confirmAction()
{
	return confirm("Vai tiešām vēlaties veikt šo darbību?");
}

/**
 * Open print in popup window
 *
 * @param int id News ID
 * @return boolean (always false)
 */
function openPrint(id)
{
	var wnd = window.open(webroot+lang+"/news/printview/"+id, "print", "menubar=1,location=0,status=1,scrollbars=1,width=800,height=700");

	return false;
}

/**
 * Open selected photo gallery in popup window
 *
 * @param int id Gallery ID
 * @return boolean (always false)
 */
function openGallery(id)
{
	var wnd = window.open(webroot+lang+"/galleries/view/"+id, "gallery", "location=0,status=1,scrollbars=0,width=1010,height=700");

	return false;
}

/**
 * Change current location to selected photo gallery
 *
 * @param int id Gallery ID
 */
function changeGallery(id)
{
	window.location = webroot+lang+"/galleries/view/"+id;
}

/**
 * Open selected video gallery in popup window
 *
 * @param int id Gallery ID
 * @param int video_id Video ID (optional)
 * @return boolean (always false)
 */
function openVgallery(id, video_id)
{
	if (typeof(video_id) == "undefined")
	{
		var video_id = "";
	}

	var wnd = window.open(webroot+lang+"/vgalleries/view/"+id+"/"+video_id, "vgallery", "location=0,status=1,scrollbars=0,width=1010,height=700");

	return false;
}

/**
 * Change current location to selected video gallery
 *
 * @param int id Gallery ID

 */
function changeVgallery(id)
{
	window.location = webroot+lang+"/vgalleries/view/"+id;
}

/**
 * Change current location to display only galleries from specific main gallery
 *
 * @param int id Main gallery ID
 */
function filterGallery(id)
{
	window.location = webroot+lang+"/galleries/filter/"+id;
}

/**
 * Change current location to display only videos from specific gallery
 *
 * @param int id Gallery ID
 */
function filterVgallery(id)
{
	window.location = webroot+lang+"/vgalleries/filter/"+id;
}

/**
 * Change current location to display diferent trainer
 *
 * @param int id Gallery ID
 */
function filterTrainers(id)
{
	window.location = webroot+lang+"/trainers/view/"+id;
}

/**
 * Change slide gallery
 *
 * @param int id Slide gallery ID
 * @param int slide_id (optional) Slide ID
 * return boolean (always false)
 */
function changeSlideGallery(id, slide_id)
{
	slide_gallery_id = id;

	$("#slide_controls").html($("#slide_gallery_controls_"+id).html());

	$("#slide_top a").removeClass("active");
	$("#slide_top a#slidenav"+id).addClass("active");

	if (typeof(slide_id) != "undefined")
	{
		changeSlide(slide_id, 1);
	}

	return false;
}

/**
 * Change slide
 *
 * @param int id Slide ID
 * @param int positiom Slide position
 * return boolean (always false)
 */
function changeSlide(id, position)
{
	$("#slide_content").html("").hide();
	$("#slide_content").html($("#slide_content_"+id).html()).fadeIn("normal");





	slide_pointer = position;

	$("#slide_controls a").removeClass("active");
	$("#slide_controls a.slidenum"+position).addClass("active");

	return false;
}

/**
 * Change to next slide
 *
 * param boolean force_change If true, change slide even if slide changing is paused
 * return boolean (always false)
 */
function changeToNextSlide(force_change)
{
	if (force_change)
	{
		pauseSlide();
	}

	if (force_change || !slide_paused)
	{
		// if next slide exists, show it
		if (slide_pointer < slides_cnt[slide_gallery_id])
		{
			changeSlide($("#slide_gallery a.slidenum"+(slide_pointer+1)).attr("rel"), slide_pointer+1);
		}
		// else show 1st slide
		else
		{
			changeSlide($("#slide_gallery a.slidenum1").attr("rel"), 1);
		}

		if (!force_change)
		{
			setTimeout("changeToNextSlide(false)", slide_interval);
		}
	}

	return false;
}

/**
 * Change to prev slide
 *
 * param boolean force_change If true, change slide even if slide changing is paused
 * return boolean (always false)
 */
function changeToPrevSlide(force_change)
{
	if (force_change)
	{
		pauseSlide();
	}

	if (force_change || !slide_paused)
	{
		// if prev slide exists, show it
		if (slide_pointer > 1)
		{
			changeSlide($("#slide_gallery a.slidenum"+(slide_pointer-1)).attr("rel"), slide_pointer-1);
		}
		// else show last slide
		else
		{
			changeSlide($("#slide_gallery a.slidenum"+slides_cnt[slide_gallery_id]).attr("rel"), slides_cnt[slide_gallery_id]);
		}

		if (!force_change)
		{
			setTimeout("changeToPrevSlide(false)", slide_interval);
		}
	}

	return false;
}

/**
 * Change to first slide
 *
 * param boolean force_change If true, change slide even if slide changing is paused
 * return boolean (always false)
 */
function changeToFirstSlide(force_change)
{
	if (force_change)
	{
		pauseSlide();
	}

	if (force_change || !slide_paused)
	{
		changeSlide($("#slide_gallery a.slidenum1").attr("rel"), 1);
	}

	return false;
}

/**
 * Change to last slide
 *
 * param boolean force_change If true, change slide even if slide changing is paused
 * return boolean (always false)
 */
function changeToLastSlide(force_change)
{
	if (force_change)
	{
		pauseSlide();
	}

	if (force_change || !slide_paused)
	{
		changeSlide($("#slide_gallery a.slidenum"+slides_cnt[slide_gallery_id]).attr("rel"), slides_cnt[slide_gallery_id]);
	}

	return false;
}

/**
 * Pause/resume slide changing
 *
 * return boolean (always false)
 */
function toggleSlidePause()
{
	if (slide_paused)
	{
		resumeSlide();
	}
	else
	{
		pauseSlide();
	}

	return false;
}


/**
 * Pause slide changing
 *
 * return boolean (always false)
 */
function pauseSlide()
{
	slide_paused = true;

	$("a.slide-pause").addClass("slide-resume");
	$("a.slide-pause").removeClass("slide-pause");

	return false;
}

/**
 * Resume slide changing
 *
 * return boolean (always false)
 */
function resumeSlide()
{
	slide_paused = false;

	$("a.slide-resume").addClass("slide-pause");
	$("a.slide-resume").removeClass("slide-resume");

	setTimeout("changeToNextSlide(false)", slide_interval);

	return false;
}

/**
 * Open player profile
 *
 * @param string id Player slug or id
 */
function openPlayerProfile(id)
{
	document.location = webroot+lang+"/players/view/"+id;
}


/**
 *
 * @param obj el Link object
 * @param string id Description ID
 *
 * return boolean (always false)
 */
function showPlayerDescription(el, id)
{
	$("#player-descriptions a").removeClass("active");
	$(el).addClass("active");

	$("#player-description > div").hide();
	$("#player-description-"+id).show();

	return false;
}

/**
 * Open specified frontpage news tab
 *
 * @param object el Link
 * @param string containerClass Container class
 * @param int id Category ID (1-4)
 * return boolean (always false)
 */
function fpSelectNewsTab(el, containerClass, id)
{
	$("."+containerClass+" .top a").removeClass("active");
	$(el).addClass("active");

	switch (id)
	{
		case 1: $("#category-content-2").hide(); break;
		case 2: $("#category-content-1").hide(); break;
		case 3: $("#category-content-4").hide(); break;
		case 4: $("#category-content-5").hide(); break;
		case 5: $("#category-content-4").hide(); break;
		
		case 6: $("#category-content-7").hide(); break;
		case 7: $("#category-content-6").hide(); break;
		case 8: $("#category-content-9").hide(); break;
		case 9: $("#category-content-8").hide(); break;
		
		case 10: $("#category-content-11").hide(); break;
		case 11: $("#category-content-10").hide(); break;
		case 12: $("#category-content-13").hide(); break;
		case 13: $("#category-content-12").hide(); break;
	}

	$("#category-content-"+id).show();

	return false;
}


function setBgImage(el, img)
{
$(el).css("background-image", "url("+img+")")
}


/**
 * Show more info about specified schedule
 *
 * @param int id Schedule ID
 * @return boolean (always false)
 */
function showScheduleInfo(id)
{
	if ($("tr#schedules_info"+id).is(":visible"))
	{
		$("tr#schedules_info"+id).css("display", "none");
	}
	else
	{
		$("tr#schedules_info"+id).css("display", $("tr").css("display"));
	}

	return false;
}

/**
 * Add/remove new condition for schedule list
 *
 * Actual filtering is done in controller. Here we only
 * redirect
 *
 * @param string key What to filter
 * @param string value Value to filter by
 */
function scheduleFilter(key, val)
{
	document.location = webroot+lang+"/schedules/filter/"+key+"/"+val;
}
