

$(document).ready(function(){

    //tady je JS jquery pro katalog modelu
    function blabla (obj) {
        var form = $(obj).parents('form:first');
        $(obj).parent().append("<img src='http://www.avacom.cz/img/load.gif' id='imageLoadingReakce' />");
        var odpovediMailem = 0;
        if ($('input:checkbox[name="odpovediMailem"]',form).attr('checked')) {
            odpovediMailem = 1;
        }
        $.post("reakceForm_rpc.php", 
        {
            code: $('input[name="code"]',form).val(), 
            jmeno: $('input[name="jmeno"]',form).val(),
            prod: $('input[name="prod"]',form).val(),               
            uroven: $('input[name="uroven"]',form).val(),
            id_reakce: $('input[name="id_reakce"]',form).val(),
            odpovediMailem: odpovediMailem,
            obsah: $('textarea[name="obsah"]',form).val(),
            predmet: $('input[name="predmet"]',form).val(),
            email: $('input[name="email"]',form).val()
        }
        , function (xml) {
            $(xml).find('reakce').each(function(){
				
                var ok = $(this).find('reakce_inserted').text();
                if (ok=="1") {
                    var urlAdr = $(location).attr('href').split("#");
                    $(location).attr("href", urlAdr[0]);
                //$('img#imageLoadingReakce').remove();
                //return true;
                }
                else {
                    $('img#imageLoadingReakce').remove();
                    alert($(this).find('reakce_message').text());
                    $('img#reakceimg').attr("src", 'securimage_show.php?sid=' + Math.random());
                                        
                }
            });
			
                        
        },"xml");
        $('input[name="code"]',form).val("");
        //alert($('input[name="code"]',form).val());
        return false;
        
    }  

    $('#zalozky div.zalozka').css('display', 'none');
    /*$('form.formReakce input[type="submit"]').click(function () {
        return blabla(this);
    }); */
    $('div.Prispevek div.ReakOdpovedet a').click(function() {
        var uroven = parseUri($(this).attr('href')).queryKey.uroven;
        var id_reakce = parseUri($(this).attr('href')).queryKey.id_reakce;
        var predmet = $(this).parent().parent().children('div.ReakHlavicka').children('div.ReakPredmet').text();
        //var nameAndDate = $(this).parent().parent().children('div.ReakHlavicka').children('div.ReakName').text();
        //var text = $(this).parent().parent().children('div.ReakText').text();
        $('a.reagovat_btn').show();
        $('div.reagovat_form').remove();
        $(this).hide();
        var reakce = $(this).parent();
        reakce.append("<div class='reagovat_form'>"+$('div.newPrispevek').html()+"</div>");
        /*$('form.formReakce input[type="submit"]',reakce).click(function () {
            return blabla(this);
        });*/
        $('input[name="predmet"]',reakce).val("Re: "+predmet.trim());
        $('input[name="uroven"]',reakce).val(uroven);
        $('input[name="id_reakce"]',reakce).val(id_reakce);
        return false;
    });
    
    $('#zalozky h2.zalozka').css('display', 'none');
    $('#zalozky div.modelySwitch').css('display', 'block');
    $('#zalozky div.modelySwitch a').click(function() {
        $('#zalozky div.modelySwitch a').removeClass('active');
        $(this).addClass('active');
        $('#zalozky div.zalozka').css('display', 'none');
        $($(this).attr('href')).css('display', 'block');
        
        equalHeight($($(this).attr('href')+" #related div.row div.cell h4"));
        equalHeight($($(this).attr('href')+" #related div.row div.cell div.img"));
        equalHeight($($(this).attr('href')+" #related div.row div.cell div.popis"));
        equalHeight($($(this).attr('href')+" #related div.row div.cell"));
        return false;
    });
    $(window).bind('hashchange', function() {
        var myAnchor = $(location).attr('hash');
        var discussHash = myAnchor.substr(1);
        if (!isNaN(discussHash)) {
            $('#zalozky div.modelySwitch a[href="#diskuze"]').click();
            $(location).attr('hash',discussHash);            
        } else {
            $('#zalozky div.modelySwitch a[href="' + myAnchor + '"]').click();
        }
        return false;
    });
    
    var myFile = $(location).attr('hash');
    if (myFile.match('#')) { // the URL contains an anchor
        // click the navigation item corresponding to the anchor
        var myAnchor = $(location).attr('hash');
        var discussHash = myAnchor.substr(1);
        if (!isNaN(discussHash)) {
            $('#zalozky div.modelySwitch a[href="#diskuze"]').click();
            $(location).attr('hash',discussHash);
        } else {
            $('#zalozky div.modelySwitch a[href="' + myAnchor + '"]').click();
        }
        
    } else {
        // click the first navigation item
        $('#zalozky div.modelySwitch a:first').addClass('active');
        $('#zalozky div.zalozka:first').css('display', 'block');
        equalHeight($("#zalozky div.zalozka:first #related div.row div.cell h4"));
        equalHeight($("#zalozky div.zalozka:first #related div.row div.cell div.img"));
        equalHeight($("#zalozky div.zalozka:first #related div.row div.cell div.popis"));
        equalHeight($("#zalozky div.zalozka:first #related div.row div.cell"));
    }
    
    
    
    
    $('.updown').click(function() {
        var idPrvku = $(this).attr('id');
        //alert(idPrvku);
        $('.'+idPrvku).toggle(0);
        return false;
    });

    //tady je KONEC JS jquery pro katalog modelu



    //pouzito na karte
    $("a[rel='obrazky']").colorbox();
    //pouzito na karte


    $("a[rel='obrazky0']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky1']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky2']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky3']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky4']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky5']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky6']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky7']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky8']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky9']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky10']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky11']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky12']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky13']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky14']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky15']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky16']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky17']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky18']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky19']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky20']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky21']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky22']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky23']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky24']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky25']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky26']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky27']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky28']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky29']").colorbox({
        preloading:true
    });
    $("a[rel='obrazky30']").colorbox({
        preloading:true
    });





    equalHeight($(".akcniNabidka h3"));
    equalHeight($(".akcniNabidka .img"));
    equalHeight($("#catalogue div.row div.cell h2"));
    equalHeight($("#catalogue div.row div.cell div.img"));
    equalHeight($("#catalogue div.row div.cell div.popis"));
    equalHeight($("#catalogue div.row div.cell"));
    /*
    equalHeight($("#related div.row div.cell h4"));
    equalHeight($("#related div.row div.cell div.img"));
    equalHeight($("#related div.row div.cell div.popis"));
    equalHeight($("#related div.row div.cell"));*/
    //$(".baf").css("color","red");
    //$(".akcniNabidky").css("overflow", "hidden");

    var div = $('div.akcniNabidky'),
    ul = $('ul.contentHolder'),
    // unordered list's left margin
    ulPadding = 15;

    //Get menu width
    var divWidth = div.width();
    //Remove scrollbars
    div.css({
        overflow: 'hidden'
    });
    div.scrollLeft(0);

    //Find last image container
    var lastLi = ul.find('li:last-child');
    var canMove = true;
    var mouseIsOver = false;
    //When user move mouse over menu
    div.mouseenter(function(e){
      
        if (!mouseIsOver) {
    

            if (canMove) {
                mouseIsOver = true;
   

                //As images are loaded ul width increases,
                //so we recalculate it each time
                var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

                var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
                //if (Math.abs(Math.round(div.scrollLeft()-left))>50) {
                canMove = false;
                div.animate({
                    scrollLeft: left
                },"fast",function() {
                    canMove = true;
    
                });
            //  }
            }
        }

    });
  
    div.mouseleave(function(e) {
        mouseIsOver = false;
    });

    div.mousemove(function(e){
        if (canMove && mouseIsOver) {


            //As images are loaded ul width increases,
            //so we recalculate it each time
            var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

            var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
   
            div.scrollLeft(left);

        }

    });

});
   



function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
    return tallest;
}

function initMenu(menuid) {
    if (document.all && document.getElementById) {
        navRoot = document.getElementById(menuid);
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI") {
                node.onmouseover = function() {
                    this.className += " over";
                }
                node.onmouseout = function() {
                    this.className = this.className.replace(" over", "");
                }
            }
        }
    }
}

tmpopt = null;
/*function checkHotove(val)
{
	sel = document.getElementsByName('platba')[0];
	prevopt = null;
	opt = null;
	
	for(var i = 0; i < sel.options.length; i++)
	{
		if(sel.options[i].value == '1')
		{
		tmpopt = sel.options[i];
		opt = i;
		}
		else
		{
		if(prevopt == null)prevopt = i;
		}
	}
	try
	{
	if(val=="2482"||val=="3577")
	{
		sel.options.add(tmpopt);
	}
	else
	{
		sel.options[opt] = null;
	}
	}
	catch(ex){}
}*/

function externalLinks() { 
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

window.onload = function() {
    initMenu("topmenu");
    externalLinks();
}

function send_xmlhttprequest(obsluha, method, url, content, headers) 
{ 
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp)
    {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers)
    {
        for (var key in headers)
        {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

function ukazSklad(id) 
{ 
    if (!send_xmlhttprequest(sklad_obsluha, 'GET', 'http://www.avacom.cz/data_rpc.php?prod=' + id))
    {
        return false;
    }
  
    // document.getElementById('pocet' + hlas).innerHTML++;
    // zobrazení hlasu u klienta
    // znemožnění opětovného hlasování smazáním odkazů
    document.getElementById('popup'+id).innerHTML = '<div style="position:absolute; display: block; height: 50px; max-width:250px; border: 1px solid #FFCC00; background-color:#ffffff; Z-index: 6;" onclick="return !hidePopup('+id+');" id="Window'+id+'"></div>';
    document.getElementById('Window'+id).innerHTML = 'Načítá se';
    return true;
}

function sklad_obsluha(xmlhttp) 
{ 
    if (xmlhttp.readyState == 4)
    {
        // aktualizace odpovědí na základě aktuálního stavu
        var pocet = xmlhttp.responseXML.getElementsByTagName('pocet');
        var windowText = '<table>';
        for (var i=0; i < pocet.length; i++)
        {
            //document.getElementById(odpovedi[i].getAttribute('id')).innerHTML = odpovedi[i].firstChild.data;
      
            windowText += '<tr><td>';
            windowText += pocet[i].getAttribute('id')+'</td><td>';
            windowText += pocet[i].firstChild.data+'</td></tr>';
      
        }
        windowText += '</table>';
        var id = xmlhttp.responseXML.getElementsByTagName('produkt')[0].firstChild.data;
    
        if(document.getElementById('Window'+id))
        {
            document.getElementById('Window'+id).innerHTML = windowText;
        }
    }
}

function hidePopup(id) 
{    
    document.getElementById('Window'+id).parentNode.removeChild(document.getElementById('Window'+id));  
    return true;
}
/*
function nactiDopravu(id) 
{ 
  if (!send_xmlhttprequest(doprava_obsluha, 'GET', 'http://www.avacom.cz/doprava_rpc.php?PID=' + id)) 
  { 
    return false; 
  } 
  return true;
}

function doprava_obsluha(xmlhttp) 
{ 
  if (xmlhttp.readyState == 4) 
  { 
    var enabled = xmlhttp.responseXML.getElementsByTagName('enabled');
    var id,usable;
    for (var i=0; i < document.form_obj.doruceni.length; i++)
    {
      for (var j=0; j < enabled.length; j++) 
      { 
        id = enabled[j].getAttribute('id');
        usable = enabled[j].firstChild.data;
        if (id == document.form_obj.doruceni[i].value)
        {
          break;
        }
      
      }
      if (usable==1)
      {
        document.form_obj.doruceni[i].disabled = false;
      }
      else
      {
        document.form_obj.doruceni[i].disabled = true;
      }
    } 
  } 
}
 */
function nactiCenuPlatby(pid)
{
    var did = getSelected(document.getElementsByName('doruceni'));
    if (!send_xmlhttprequest(platba_obsluha, 'GET', 'http://www.avacom.cz/platby_rpc.php?DID='+ did +'&PID=' + pid))
    {
        return false;
    }
    return true;
}

function nactiPlatbu(did) 
{ 
    var pid = getSelected(document.getElementsByName('platba'));
    if (!send_xmlhttprequest(platba_obsluha, 'GET', 'http://www.avacom.cz/platby_rpc.php?DID='+ did +'&PID=' + pid))
    {
        return false;
    }
    return true;
}


function platba_obsluha(xmlhttp) 
{ 
    if (xmlhttp.readyState == 4)
    {
        var el;
        var enabled = xmlhttp.responseXML.getElementsByTagName('enabled');
        var id,usable;
        var vychozi = xmlhttp.responseXML.getElementsByTagName('platba_vychozi')[0].firstChild.data;
        var hodnota = xmlhttp.responseXML.getElementsByTagName('platba_hodnota')[0].firstChild.data;
        document.getElementById('div_cena').innerHTML = hodnota;
        for (var i=0; i < document.form_obj.platba.length; i++)
        {
            for (var j=0; j < enabled.length; j++)
            {
                id = enabled[j].getAttribute('id');
                usable = enabled[j].firstChild.data;
                if (id == document.form_obj.platba[i].value)
                {
                    break;
                }
            }
            if (document.form_obj.platba[i].value==vychozi)
            {
                document.form_obj.platba[i].checked = true;
            }
            el = document.getElementById('p'+document.form_obj.platba[i].value);
            if (usable==1)
            {
                document.form_obj.platba[i].disabled = false;
                if (el!=null)
                {
                    el.style.color='';
                }
            }
            else
            {
                document.form_obj.platba[i].disabled = true;
                if (el!=null)
                {
                    el.style.color='#cccccc';
                }
            }
        }
    }
}

function getSelected(radiogroup)
{
    var j=0;
    for (var i=0; i < radiogroup.length; i++)
    {
        if ((radiogroup[i].checked) && (radiogroup[i].value!=0))
        {
            j=radiogroup[i].value;
            break;
        }
    }
    return j;
}

function onLoadNactiPlatbu() 
{ 
    var did = getSelected(document.getElementsByName('doruceni'));
    var pid = getSelected(document.getElementsByName('platba'));
    if (!send_xmlhttprequest(platba_obsluha, 'GET', 'http://www.avacom.cz/platby_rpc.php?DID='+ did +'&PID=' + pid))
    {
        return false;
    }
    return true;
}

/*
function onLoadPlatba_obsluha(xmlhttp) 
{ 
  if (xmlhttp.readyState == 4) 
  { 
    var el;
    var enabled = xmlhttp.responseXML.getElementsByTagName('enabled');
    var id,usable;
    var hodnota = xmlhttp.responseXML.getElementsByTagName('platba_hodnota')[0].firstChild.data;
    document.getElementById('div_cena').innerHTML = hodnota;
    for (var i=0; i < document.form_obj.platba.length; i++)
    {
      for (var j=0; j < enabled.length; j++) 
      { 
        id = enabled[j].getAttribute('id');
        usable = enabled[j].firstChild.data;
        if (id == document.form_obj.platba[i].value)
        {
          break;
        }
      }
      el = document.getElementById('p'+document.form_obj.platba[i].value);
      if (usable==1)
      {
        document.form_obj.platba[i].disabled = false;
        if (el!=null)
        {
          el.style.color='';
        }
      }
      else
      {
        document.form_obj.platba[i].disabled = true;
        if (el!=null)
        {
          el.style.color='#cccccc';
        }
      }
    } 
  } 
}
 */











/* tady je rozbalovani Ukazat vice/ulazat mene ve vyhledavani pro katalog modelu */
$(document).ready(function() {
    var s = $('.myContent').height();

    if (s > 150) {
        $('.myContent').height(150);
        $('.myContent').css("overflow","hidden");

    }
    if (s < 10){
        $('.myContent').css("display","none");
        $('.LessMoreButton').css("display","none");
    }
    








    $('.LessMoreButton').click(function (){
        if (s == $('.myContent').height()) {
            $('.myContent').animate({
                height: 150
            }, 1000, function() {
                // Animation complete.
                $('.myContent').css("overflow","hidden");                
                $('.LessMoreButton').removeClass("LessMoreButtonUp");
                $('.LessMoreButton').html("Zobrazit více...");                
            });
        } else {            
            $('.myContent').animate({
                height: s
            }, 1000, function() {                
                // Animation complete.
                $('.LessMoreButton').addClass("LessMoreButtonUp");
                $('.LessMoreButton').html("Zobrazit méně...");
            });
        }
    }
    )

}); 
/* tady je rozbalovani Ukazat vice/ulazat mene ve vyhledavani pro katalog modelu */








/* konec Ukazat vice mene v kategorii */
$(document).ready(function() {
    $('.ViceMene').click(function() {
        $('.Rozbalovatko').toggle(400);
        return false;
    });
}); 
/* konec Ukazat vice mene v kategorii */


