var input_values = [];	// použito pro funkci incInputsHideValues

$(document).ready(function()
{
	incInputsHideValues();
	targetBlank();
// 	initSlideNaviPartner();
// 	initShowBoxNaviPartner();
 	initSlideFilesCategory();
 	initShowBoxFilesCategory();
	
// 	$("#selectSearch, #selectSearchCategory, #selectSearchPartner").sb();

	// fancy box
	$("a.fancybox").fancybox();

	// left side menu
	$('.menuItem').mouseenter(function(){
		$(this).next('div.submenuSide').show();
	});
	$('.menuItem').mouseleave(function(){
		$(this).next('div.submenuSide').hide();
	});
	$('div.submenuSide').mouseenter(function(){
        $(this).show();
	});
	$('div.submenuSide').mouseleave(function(){
        $(this).hide();
	});

	// product slider
	$('#productSlider').bxSlider({
        displaySlideQty: 2,
        moveSlideQty: 2,
        auto: true,
        speed: 1000,
        pause: 5000,
        wrapperClass: 'productSlider'
	});

	// news slider
	$('#newsSlider').bxSlider({
        displaySlideQty: 1,
        moveSlideQty: 1,
        auto: true,
        speed: 1000,
        pause: 8000,
        pager: true,
        pagerLocation: 'top',
        wrapperClass: 'newsSlider',
        controls: false
	});
	
	// sales slider
	$('#salesSlider').bxSlider({
        mode: 'vertical',
		displaySlideQty: 5,
        moveSlideQty: 5,
        auto: false,
        speed: 1000,
        infiniteLoop: false,
        wrapperClass: 'salesSlider',
        controls: true
	});
	
	// consulting show answer
	$('.showAnswer').click(function() {
	    if ($(this).next('div.answer').is(':hidden'))
	    {
			$(this).next('div.answer').show('slow');
// 			$(this).removeClass('showAnswer');
			$(this).html('&gt; <a href="#" class="noUnderline">Skrýt odpověď</a>');
// 			$(this).addClass('hideAnswer');
		}
		else
		{
			$(this).next('div.answer').hide('slow');
// 			$(this).removeClass('showAnswer');
			$(this).html('&gt; <a href="#" class="noUnderline">Celá odpověď</a>');
// 			$(this).addClass('hideAnswer');
		}
		return false;
	});
	
	// SWFobject
    var flashvars = {};
	var params = {wmode: 'transparent', base: './flash'};
	var attributes = {};

	swfobject.embedSWF("./flash/prolinacka.swf", "topFlash", "1000", "220", "9.0.0", "./flash/expressInstall.swf", flashvars, params, attributes);


	// cufon
	Cufon.replace('.cfn', {hover: true});
	
});



function initShowBoxFilesCategory()
{
	var holder = $("#filesCategorySlider");
	var row = $("div.row", holder);
	var links = $("a", row);
	var allBoxes = $("span.box", holder);
	var timer;
	
	links.mouseover(function()
	{
// 		clearTimeout(timer);

		var link = $(this);
		var rowIndent = -(parseInt(row.css("left")));
		
		if ($("a.cover", link).length == 0)
		{
			clearTimeout(timer);
			
			var thisId = this;
			
			timer = setTimeout(function()
			{
				var linkPosition = link.position();
				var box = $("span#box_"+ thisId.id.substring(5), holder);
				box.append("<a class='cover'></a>");

				var cover = $("a.cover", box);
				
				box.css("left", ( linkPosition.left - rowIndent + 42 - ((190 - link.width()) / 2) ));
				
				cover.width( link.width() +30 );
				cover.height( link.height() +5 );
				cover.css("left", (190 - cover.width()) /2 );
				cover.attr("href", link.attr("href"));
				
				cover.mouseout(function()
				{
					clearTimeout(timer);
					$("a.cover", row).remove();
// 					allBoxes.hide();
				});
				
				box.show();
			
			}, 200);
			
		}
	});

	links.mouseout(function()
	{
		clearTimeout(timer);
		allBoxes.hide();
	});

	allBoxes.mouseover(function()
	{
		$(this).show();
	});

	allBoxes.mouseout(function()
	{
		clearTimeout(timer);
		$(this).hide();
	});

}






function initSlideFilesCategory()
{
	var MOUSE_OFFSET = 70;
	
	var holder = $("#filesCategorySlider");
	var holderOffset = holder.offset();
	
	var holderRow = $("div.row", holder);
	
	var arrowLeft = $("div.arrowLeft", holder);
	var arrowRight = $("div.arrowRight", holder);
	
	var coordX = 0;
	var holderWidth = holder.width() - MOUSE_OFFSET - MOUSE_OFFSET;
	
	var holderPercentPos = 0;
	var holderRowPixelPos = 0;
	var holderRowPixelPreviousPos = 0;
	
	
	// zjistime šířku posuvného řádku
	var elements = $("a, span.btw", holderRow);
	var tempWidth = 8;
	
	$.each(elements, function(i, val)
	{
		val = $(val);
		tempWidth += val.width() + parseInt(val.css("padding-left")) + parseInt(val.css("padding-right"));
	});
	
	holderRow.width(tempWidth);
	
	var holderRowWidth = holderRow.width() - holderWidth - MOUSE_OFFSET;
	
	arrowLeft.mousedown(function()
	{
		if (parseInt(holderRow.css("left")) >= 0)
			return;

		holderRow.animate(
		{
			left: "+=2000"
		},
		{
			duration: 4000,
			specialEasing:
			{
				left: "linear"
			},
			step: function(now, fx)
			{
				if (now >= 0)
				{
					holderRow.stop();
				}
			}
		});
	});
	arrowRight.mousedown(function()
	{
		if (parseInt(holderRow.css("left")) <= -(holderRowWidth))
			return;

		holderRow.animate(
		{
			left: "-=2000"
		},
		{
			duration: 4000,
			specialEasing:
			{
				left: "linear"
			},
			step: function(now, fx)
			{
				if (now <= -(holderRowWidth))
				{
					holderRow.stop();
				}
			}
		});
	});

	arrowLeft.mouseup(function()
	{
		holderRow.stop();
	});
	arrowRight.mouseup(function()
	{
		holderRow.stop();
	});


	
// 	holder.mousemove(function(e)
// 	{
// 		if ((holderRow.width() - MOUSE_OFFSET) > holderWidth)
// 		{
// 			coordX = e.pageX - holderOffset.left - MOUSE_OFFSET;
// 			
// 			holderPercentPos = coordX / holderWidth * 100;
// 			
// 			if (holderPercentPos < 0)
// 			{
// 				holderPercentPos = 0;
// 			}
// 			else if (holderPercentPos > 100)
// 			{
// 				holderPercentPos = 100;
// 			}
// 			
// 			holderRowPixelPos = holderRowWidth / 100 * holderPercentPos;
// 			
// 			holderRow.css("left", -(holderRowPixelPos) );
// 		}
// 	});
	
	
}




function targetBlank()
{
	var url_match = null;
	var target_domain = null;
	var this_domain = window.location.hostname;
 
	// funkce vrátí doménu z odkazu
	var get_hostname_from_url = function(url)
	{
		if (typeof url == "undefined")
		{
			return null;
		}
 
		url_match = url.match(/:\/\/(.[^/]+)/);
 
		if (url_match != null)
		{
			return url_match[1];
		}
 
		return null;
	}
 
	// vybereme všechny odkazy
	$("a").click(function(event)
	{
		var href = $(this).attr("href");
 
		// získáme doménu cíle odkazu
		target_domain = get_hostname_from_url(href);
 
		// pokud je doména jiná než aktuální, nebo je nastaveno class="target_blank", otevřeme v novém    (pokud se nejedná o fancybox)
		if ($(this).hasClass("target_blank") == true || ($(this).hasClass("fancybox") == false && (target_domain != null && target_domain != this_domain)))
		{
			window.open($(this).attr("href"), "");
			event.preventDefault();
			return false;
		}
	});
}






// u inputů s třídou hide_value_onclick nastavá skrývání textu prí focus
function incInputsHideValues()
{
	var elements = $("input.hideValueOnClick");
	
	$.each(elements, function(key, val)
	{
		var this_value = val.value;
		input_values[key] = this_value;
		
		$(val).focus(function()
		{
			if ($(this).attr("value") == this_value)
			{
				$(this).attr("value", "");
			}
		});

		$(val).blur(function()
		{
			if ($(this).attr("value") == "")
			{
				$(this).attr("value", this_value);
			}
		});
		
	});

}


// nastaví výběr selectboxu podle hodnoty value
function setSelectedByValue(selectbox, seekValue)
{
    if (selectbox == null || typeof selectbox == "undefined")
        return false;
    
    for (var i=0; i < selectbox.length; i++)
	{
        if (selectbox[i].value == seekValue)
		{
            selectbox[i].selected = true;
            return true;
        }
    }
    
    return false;
}
