/* --- all searchforms control --- */
for(var i = 0, id; id = ["search", "geo_search"][i]; i++)
{
  if(!document.getElementById(id)) continue;
  document.getElementById(id).onfocus = function()
    {
       if (this.value== this.defaultValue) this.value=''
    }
  document.getElementById(id).onblur = function()
    {
       if (this.value=='') this.value = this.defaultValue
    }
}

	$("#searchform").submit(function(e) 
	{
//		alert($("#search").val());
//		alert($("#search").attr('defaultValue'));
		if ($("#search").val()==$("#search").attr('defaultValue'))
		{
			alert("Search Error\nPlease enter the search term before clicking on the Search button!");
			return false;
		}
		return true;
	});



/* --- print control --- */
if (document.getElementById("sidebar_link_print")) {
	document.getElementById("sidebar_link_print").onclick = function()
	 {
    	window.print();
    	return false;
 	 }
}
if(location.hash.substr(1) == "print") window.print();

/* --- sidebar_link control --- */
/*
var oldPopup = null;
for(var i = 0, p; p = ["map", "email", "share", "bookmark"][i]; i++)
{
  if (document.getElementById("sidebar_link_" + p)) {
		document.getElementById("sidebar_link_" + p).onclick = function()
  		{
    		if(oldPopup) oldPopup.style.display = "";
   			oldPopup = document.getElementById(this.id + "_box");
    		oldPopup.style.display = "block";
    		return false;
  		}
  }
  if (document.getElementById("close_" + p)) {
  	document.getElementById("close_" + p).onclick = function()
  		{
    		oldPopup.style.display = "";
    		return false;
  		}
	}
  if(location.hash.substr(1) == p)
  {
  document.getElementById("sidebar_link_" + p).onclick();
  }
}
*/


/* --- add to favorites control --- */
if (document.getElementById("add_to_fav")) {
	document.getElementById("add_to_fav").onclick = function()
		{
	 		if (window.sidebar)
	    	{
	    		window.sidebar.addPanel(document.title,location.href,"");
				}
	   			else if( window.external )
				{
					window.external.AddFavorite(location.href,document.title);
				}
			return false;
		}
}

var IMAGEWIDTH = 192;

function search_result_gallery_arrows()
{
	$(".search-result-gallery-arrow-right").each(
		function()
		{
			$(this).click(
				function()
				{
					//this = button, 
					imagebox = $(this).parent(); 
					images = $(".search-result-gallery-images-container:first",imagebox);
					scrollbar = $(".search-result-gallery-scroller:first",$(this).parent());

					pos = Math.floor($(this).parent().scrollLeft() / IMAGEWIDTH + 1) * IMAGEWIDTH;
					if (pos>images.width()-imagebox.width()) pos=images.width()-imagebox.width();
					rpos = pos / (images.width());
					
					$(this).parent().animate({scrollLeft: pos},500); // -=188
					scrollbar.animate({"left" : rpos*scrollbar.parent().width()},500);
				}
			);
		}
	);

	$(".search-result-gallery-arrow-left").each(
		function()
		{
			$(this).click(
				function()
				{
					//this = button, 
					imagebox = $(this).parent(); 
					images = $(".search-result-gallery-images-container:first",imagebox);
					scrollbar = $(".search-result-gallery-scroller:first",$(this).parent());

					pos = Math.floor((imagebox.scrollLeft() - 1) / IMAGEWIDTH) * IMAGEWIDTH;
					if (pos<0) pos=0;
					rpos = pos / (images.width());
					
					imagebox.animate({scrollLeft: pos},500); // -=188
					scrollbar.animate({"left" : rpos*scrollbar.parent().width()},500);
					// (vyse uvedene funguje za podminky proporcialniho zobrazeni, kdy sirka soupatka odpovida pomeru viditelne casti soupatka k celku)
				}
			);
		}
	);
	
	$(".search-result-gallery-scroller").each(
		function()
		{
		$(this).draggable({ containment: 'parent', axis: 'x' });
		$(this).bind("drag", function(event, ui)
		    {
			imagebox = ui.helper.parent().parent(); // viditelny kousek na tri obrazky
			images = $(".search-result-gallery-images-container:first",imagebox);
			rpos = ui.position.left / (ui.helper.parent().width() - ui.helper.width());
			apos = rpos * (images.width() - imagebox.width());
			imagebox.scrollLeft(apos);
		    }
		);

		// a nastavime relativni sirku
		scroll_container = $(this).parent();
		imagebox = scroll_container.parent(); // viditelny kousek na tri obrazky
		images = $(".search-result-gallery-images-container:first",imagebox);
		$(this).width(scroll_container.width() * imagebox.width() / images.width());
		}
	);

	$(".search-result-gallery-scroller-container").each(
		function()
		{
			$(this).click(
				function(e)
				{
					scroller = $(".search-result-gallery-scroller:first",$(this));
					//alert('a');
					var click_x = e.pageX - $(this).offset().left;
					if (click_x > scroller.position().left && click_x < scroller.position().left+scroller.width()) return; // jsme uvnitr scrolleru
					//scroller.hide();
					//alert(scroller.position().left);
					if (click_x < scroller.position().left) 
						$(".search-result-gallery-arrow-left:first",$(this).parent()).click();
					else 
						$(".search-result-gallery-arrow-right:first",$(this).parent()).click();
				}
			)
		}
	);
	
	// pokud jsme na detailu, tak obrazky maj kolem sebe odkazy -> ty je treba ozivit
	if ($(".search-result-gallery-images-container").length==1)
	{
		links = $(".search-result-gallery-images-container a");
		links.bind('click', function() {
			// tady je mozno doplnit, ze kdyz neni aktivni zalozka obrazek, tak neprolinat obrazek a predchozi obrazek
			fade_main_image($(this).attr("href"));
			activate_popup('close');
			//$("#main_image").attr("src",$(this).attr("href"));
			return false;
		});
		
		jQuery.fx.off = true;  // docasne vyradime animace
		$(".search-result-gallery-arrow-right:first").click();
		jQuery.fx.off = false; 
		
		$("#profile_gallery_previous").click( function() {
		    change_main_image(-1);
		    return false;
		});

		$("#profile_gallery_next").click( function() {
		    change_main_image(1);
		    return false;
		});
	}
	
}

function fade_main_image(new_href)
{
    if (!new_href) return;
    //provede prolnuti stareho a noveho obrazku
    //alert(new_href);
//    $("#main_image_2").attr("src",$("#main_image_1").attr("src")).css({opacity: 1.0}).animate({opacity: 1.0},400);
    $("#main_image_2").attr("src",$("#main_image_1").attr("src")).css({opacity: 1.0}); //.animate({opacity: 1.0},400);
    $("#main_image_1").attr("src",new_href).css({opacity: 0.0}).animate({opacity: 1.0},400);
}

//        .animate({opacity: 1.0}, 1000, function() {


function change_main_image(val)
{
	activate_popup('close');
	var current_image = $("#main_image_1").attr("src");
	var links = $(".search-result-gallery-images-container a");
	links.each(function (i) {
		if ($(this).attr("href") === current_image)
		{
			imagebox = $(this).parent().parent(); // viditelny kousek na tri obrazky
			images = $(".search-result-gallery-images-container:first",imagebox);

			if (val<0) sel=$(this).prev(); else sel=$(this).next();
			fade_main_image(sel.attr("href"));
			
			//a ted jeste posun obrazku ve "filmu"
			idx = i+val; // ted je idx cislo zobrazeneho prvku, jenze chceme mit horni obrazek dole uprostred
			if (idx>0) idx--; else idx=0; 
			
			cnt = Math.round(images.width() / IMAGEWIDTH,0); // nic nepsiho mne nenapadlo
			if (idx>cnt-3) idx=cnt-3;
			
			apos = idx * IMAGEWIDTH;
//			imagebox.scrollLeft(apos);
			imagebox.animate({scrollLeft: apos},400); // -=188
			
			//a soupnuti slideru
			scrollbar = $(".search-result-gallery-scroller:first");
			rpos = apos / (images.width());
			if (rpos<1) scrollbar.animate({"left" : rpos*scrollbar.parent().width()},400);
			
		}
	});
}

search_result_gallery_arrows();


