$(document).ready(function(){
  $.ga.load($("#ga_id").val());
  update_kosik();
  $("a[rel='external']").each(function (i) {
    $(this).attr("target","_blank");
  });
  $("a[rel='slideshow']").lightBox({
  	overlayBgColor: '#000',
  	overlayOpacity: 0.8,
  	imageLoading: 'html/images/loading.gif',
  	imageBtnClose: 'html/images/close.gif',
  	imageBtnPrev: 'html/images/prev.png',
  	imageBtnNext: 'html/images/next.png',
  	containerResizeSpeed: 350,
  	txtImage: 'Obrázek',
  	txtOf: 'z'
   });

   $('.galerie')
      .bind("click", function(){
            $('#g1').mbGallery({
            galleryWidth: 300,
            galleryHeight: 300,
            galleryMaxWidth: 0,
            galleryColor: "#333",
            galleryFrameBorder: 10,
            galleryFrameColor: "#fff",
            maskOpacity:.8,
            maskBgnd:"#000",

            startFrom: 0,
            headerOpacity: 0.8,
            thumbsBorder: 4,
            thumbHeight: 50,
            thumbStripWidth:250,
            thumbStripColor: "#333333",
            thumbStripPos: "right",
            thumbSelectColor: "white",
            thumbOverColor: "#cccccc",
            imageSelector: ".imgFull",
            thumbnailSelector: ".imgThumb",
            descSelector: ".imgDesc",
            descriptionWidth:300,

            labelColor: "#333",
            labelColorDisactive: "#333",
            labelTextColor: "#fff",
            labelTextSize: "11px",
            labelHeight: 20,

            iconFolder: "html/images/gallery/white",
            fadeTime: 500,
            autoSlide: true,
            slideTimer: 6000,
            autoSize: true,
            startTimer:0
            });
  });

  if ($("#login_name").val()!="")
  {
    $("#login_name").css("backgroundPosition","0 0");
  }
  if ($("#login_pasw").val()!="")
  {
    $("#login_pasw").css("backgroundPosition","0 0");
  }
  if ($("#search_text").val()!="")
  {
    $("#search_text").css("backgroundPosition","0 0");
  }

	$("input.dalsi-krok").click( function () {
    $.ajax({
      async: false,
      dataType: "text",
      type: "POST",
      url: "ctl/registrace/check-login/",
      data: "login="+$("#reg_email").val(),
      success: function(data){
        $("#reg_check_login").val(data);
      }
    });
	  $("span.ValidationErrors").remove();
	  $("*").removeClass("ErrorField");
	});
	$("input.checkinputl").click( function () {
    if ($(this).attr("checked"))
    {
      $("#reg_dic_show").show();
    }else{
      $("#reg_dic_show").hide();
    }
	});

  $("form.ajax").submit(function (e){
    var url = $(this).attr("action");
    var data = $(this).serialize();
    var response = "."+$(this).attr("id")+"-response";
    $.post(url,data, function(output){
     $(response).html(output);
    },"html");
    return false;
  });

  $("form.formbuy").submit(function (e){
    var url = $(this).attr("action");
    var data = $(this).serialize();
    var $curr = $(this);
    $.post(url,data, function(output){
      $curr.find(".buy_info").find("span").html("Do košíku bylo vloženo "+$curr.find(".mnozstvi input").val());
      $curr.find(".buy_info").animate({"top": "-=24px"}, "slow","swing",function () {
        update_kosik();
        $curr.find(".buy_info").animate({dummy:1}, 1000);
        $curr.find(".buy_info").animate({"top": "+=24px"}, "slow","swing");
      });

    },"html");
    return false;
  });

	$(".obj").focus( function () {
	  $("#nova_dodaci_adresa").attr("checked","checked");
	});
	$("input.dalsi-krok").mouseover( function () {
	  $(this).css("backgroundPosition","0 -21px");
	});
	$("input.dalsi-krok").mouseout( function () {
	  $(this).css("backgroundPosition","0 0");
	});

	$("#navigace li a").mouseover( function () {
	  var header = $(this).attr("rel");
    headerShow(header,500);
	});
	$("#navigace li a").mouseout( function () {
	  var header = $(this).attr("rel");
    headerHide(header,500);
	});
	$("#login_name").focus( function () {
    $(this).css("backgroundPosition","0 0");
	});
	$("#login_name").blur( function () {
    if ($(this).val()=="")
    {
      $(this).css("backgroundPosition","0 -22px");
    }
	});
	$("#login_pasw").focus( function () {
    $(this).css("backgroundPosition","0 0");
	});
	$("#login_pasw").blur( function () {
    if ($(this).val()=="")
    {
      $(this).css("backgroundPosition","0 -22px");
    }
	});
	$("#search_text").keyup( function () {
	  if ($(this).val().length>2)
	  {
	    search_helper($(this).val());
	    $("#search-helper").show();
	  }else{
	    $("#search-helper").hide();
	  }
	});
	$("#search_text").click( function () {
	  if ($(this).val().length>2)
	  {
	    search_helper($(this).val());
	    $("#search-helper").show();
	  }else{
	    $("#search-helper").hide();
	  }
	});

	$("#search_text").focus( function () {
    $(this).css("backgroundPosition","0 0");
	});
	$("#search_text").blur( function () {
    if ($(this).val()=="")
    {
      $(this).css("backgroundPosition","0 -22px");
    }
	});
	$("#search-helper").mouseleave( function () {
    $("#search-helper").hide();
	});
	$("input.go").mouseover( function () {
    $(this).css("backgroundPosition","0 -22px");
	});
	$("input.go").mouseout( function () {
    $(this).css("backgroundPosition","0 0");
	});

	$("#img_alt_sipky .left").mouseover( function () {
    $(this).css("backgroundColor","#808080");
    $(this).css("backgroundPosition","0 -16px");
    $(this).css("cursor","w-resize");
    $(document).everyTime(30,"slider",function(i) {
      var pozice = parseInt($("#img_alt_belt").css("marginLeft"))+5;
      if (pozice <= 0)
        $("#img_alt_belt").css("marginLeft",pozice);
    });
	});
	$("#img_alt_sipky .left").mouseout( function () {
	  $(document).stopTime("slider");
    $(this).css("backgroundColor","#d0d0d0");
    $(this).css("backgroundPosition","0 0");
    $(this).css("cursor","default");
	});
	$("#img_alt_sipky .right").mouseover( function () {
    $(this).css("backgroundColor","#808080");
    $(this).css("backgroundPosition","-162px -16px");
    $(this).css("cursor","w-resize");
    $(document).everyTime(30,"slider",function(i) {
      var pozice = parseInt($("#img_alt_belt").css("marginLeft"))-5;
      if (pozice >= -1*(115))
        $("#img_alt_belt").css("marginLeft",pozice);
    });
	});
	$("#img_alt_sipky .right").mouseout( function () {
	  $(document).stopTime("slider");
    $(this).css("backgroundColor","#d0d0d0");
    $(this).css("backgroundPosition","-162px 0");
    $(this).css("cursor","default");
	});

	$(".kosik_truncate").live("click", function() {
    $.get($(this).attr("href"));
    update_kosik();
	  return false;
	});

	$(".tr a").live("click", function() {
    $.post($(this).attr("href"),{ product_id: $(this).attr("rel")});
    update_kosik();
	  return false;
	});
	$("a[rel=comment].odeslat").live("click", function() {
    var form = $(this).closest("form");
    $(".htmlarea").htmlarea("updateTextArea");
    form.submit();
	  return false;
	});
	$("a[rel=nickname].odeslat").live("click", function() {
    var form = $(this).closest("form");
    var nickname = form.find("input").val();
    $.post("ctl/forum/check_nick/",{ nickname: nickname}, function(data) {
      if(data==1)
      {
        form.submit();
      }else{
        alert("Existuje");
      }
    });

//    form.submit();
	  return false;
	});
	$("a.nove-tema").live("click", function() {
	  $("tr.reply").html("");
	  create_new_topic();
    $("tr.reply").show();
    html_area();
	  return false;
	});
	$("a.reagovat").live("click", function() {
	  $("tr.reply").html("");
	  create_reply();
    $("tr.reply").show();
    html_area();
	  return false;
	});
	$("a.citovat").live("click", function() {
	  $("tr.reply").html("");
	  create_reply();
	  $("tr.reply textarea").html("<div class='cite'>"+$(this).closest("tr").prev().children("td.group").children("div").html()+" "+$(this).closest("tr").prev().children("td.group").children("p").html()+" napsal/a:</div><div class='cited'>"+$(this).closest("tr").prev().children("td.comment").html()+"</div><br />");
    $("tr.reply").show();
    html_area();
	  return false;
	});

});

function headerChange(header1,header2,duration)
{
  $(header2).css("z-index","2");
  $(header2).fadeIn(duration, function () {
    $(header1).hide();
    $(header1).css("z-index","0");
    $(header2).css("z-index","1");
  });
}

function headerHide(header,duration)
{
  var style = header;
  header = "span."+header;
  $(header).show();
  var main_class = $("#logo a").attr("rel");
  $("span."+main_class).show();
  $("#header-banner div").removeClass(style);
  $(header).fadeOut(duration, function () {
    $(header).hide();
    $(header).css("z-index","0");
  });
}

function headerShow(header,duration)
{
  var style = header;
  header = "span."+header;
  var main_class = $("#logo a").attr("rel");
  $("span."+main_class).show();
  $("#header-banner div").addClass(style);
  $("#header-banner span").fadeOut(duration);
  $("#header-banner span").css("z-index","0");
}

function update_kosik()
{
  $("#kosik-nahled").load("ctl/kosik/view/");
}

function search_helper(search)
{
  $("#search-helper").html("Hledám...");
  $.get("ctl/search/helper/", { hledat: search },
  function(data){
    var pageTracker = _gat._getTracker($("#gaid").val());
    pageTracker._trackPageview("/?hledat="+search);
    $("#search-helper").html(data);
  });
}

function create_reply()
{
  $("tr.reply").html("<td colspan='5'><form action='ctl/forum/reply/' method='post'><fieldset><textarea cols='20' rows='20' class='htmlarea' name='reakce'></textarea><a rel='comment' class='odeslat cloak' href='#' title='odeslat'>ODESLAT<span></span></a></fieldset></form></td>");
}
function create_new_topic()
{
  $("tr.reply").html("<td colspan='5'><form action='ctl/forum/new_topic/' method='post'><fieldset><div id='topic_nadpis'><input name='nadpis' type='text' /><span>Téma:</span><div class='clear'></div></div><textarea cols='20' rows='20' class='htmlarea' name='reakce'></textarea><a rel='comment' class='odeslat cloak' href='#' title='odeslat'>ODESLAT<span></span></a></fieldset></form></td>");
}

function html_area()
{
  $(".htmlarea").htmlarea({
                // Override/Specify the Toolbar buttons to show
                toolbar: [
                    ["bold", "italic", "underline", "|","link", "unlink"]

                ],

                // Override any of the toolbarText values - these are the Alt Text / Tooltips shown
                // when the user hovers the mouse over the Toolbar Buttons
                // Here are a couple translated to German, thanks to Google Translate.
                toolbarText: $.extend({}, jHtmlArea.defaultOptions.toolbarText, {
                        "bold": "tučně",
                        "italic": "kurzíva",
                        "underline": "podtržení",
                        "link": "odkaz",
                        "unlink": "zrušit odkaz"
                    }),

                // Specify a specific CSS file to use for the Editor
                css: "html/templates/www/css/main.php",

                // Do something once the editor has finished loading
                loaded: function() {
                    //// 'this' is equal to the jHtmlArea object
                    //alert("jHtmlArea has loaded!");
//                    this.showHTMLView(); // show the HTML view once the editor has finished loading
                }
            });
}
