var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua); 

function reloadwindow(){
  //window.location.reload();
  //window.location.href=window.location.href;
  work_start();
  window.location.reload(false);
}


var eFancyBoxDefaults = {
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayOpacity' : 0.8,
		'overlayColor' : '#333333',
		'titlePosition' : 'over'
	};
if(isiPad){ $.extend(eFancyBoxDefaults,{ 'centerOnScroll' : false }); };
var eFancyBoxImage = $.extend({},eFancyBoxDefaults,{
  'hideOnOverlayClick' : true, 
  'hideOnContentClick' : true
});
if(!isiPad){ $.extend(eFancyBoxImage,{ 'centerOnScroll' : true }); };


var working=0;
function work_start(){
  working++;
  $.fancybox.showActivity();
};
function work_done(){
  working--;
  if(working<0){ working=0; };
  if(working==0){ $.fancybox.hideActivity(); };
};	

function eshop_login(uid,pwd){
    $.ajax({
      'url':'commerce/eshop/auth.php',
      'data':{'uid':uid,'pwd':pwd},
      'beforeSend':function(){ work_start(); },
      'dataType':'json',
      'complete':function(){ work_done(); },
      'success':function(response){
        if(response.auth>0){
          reloadwindow();
        } else {
          $.fancybox($("#login_error"));
        }; 
      } 
    });
}

function modalbox(url,w,h,reload){
  var fopts=$.extend({},eFancyBoxDefaults,{'type' : 'iframe' , 'hideOnOverlayClick' : false, 'hideOnContentClick' : false, 'enableEscapeButton': false  });
  if(!isiPad){ $.extend(fopts,{ 'centerOnScroll':true }); }; 
  if(w>0){ $.extend(fopts,{'width':parseInt(w)});  };
  if(h>0){ $.extend(fopts,{'height':parseInt(h)});  };
  if(reload){
    if(reload==2){
        $.extend(fopts,{'onClosed':function(){
                    $("#kosik").html("<img src='commerce/eshop/img/ajax-loader.gif'>").load("eshop_ajax.php?mod=kosik"); 
                    if(window.kosik_refreshpage){ kosik_refreshpage=false; window.location.reload(); };
                } }); 
      } else { $.extend(fopts,{'onClosed':function(){ 
        reloadwindow(); 
      } }); }; 
  }
  $.fancybox(url,fopts);
}

function ajax_pagerefresh(){
  $("a.modal").each(function(){
      var $t=$(this);
      var clickclose = false; 
      if($t.hasClass("clickclose")){ clickclose=true; };
      var fopts=$.extend({},eFancyBoxDefaults,{'type' : 'iframe' , 'hideOnOverlayClick' : clickclose, 'hideOnContentClick' : clickclose, 'enableEscapeButton': clickclose });
      if(!isiPad){ $.extend(fopts,{ 'centerOnScroll':true }); }; 
      if($t.attr('fw')>0){ $.extend(fopts,{'width':parseInt($t.attr('fw'))});  };
      if($t.attr('fh')>0){ $.extend(fopts,{'height':parseInt($t.attr('fh'))});  };
      if($t.attr('data-fw')>0){ $.extend(fopts,{'width':parseInt($t.attr('data-fw'))});  };
      if($t.attr('data-fh')>0){ $.extend(fopts,{'height':parseInt($t.attr('data-fh'))});  };
      if($t.hasClass("reload")){ $.extend(fopts,{'onClosed':function(){ reloadwindow(); } }); }
        else if($t.hasClass("kosik_refresh")){ $.extend(fopts,{'onClosed':function(){
                    $("#kosik").html("<img src='commerce/eshop/img/ajax-loader.gif'>").load("eshop_ajax.php?mod=kosik"); 
                    if(window.kosik_refreshpage){ kosik_refreshpage=false; reloadwindow(); };
                } }); };
      $t.fancybox(fopts);
  });

  $("a.refresh").click(function(){
    $.ajax({
      'url':$(this).attr('href'),
      'beforeSend':function(){ work_start(); },
      'complete':function(){ work_done(); window.location.href=window.location.href; return true; reloadwindow(); }
    });
    return false;
  });
}

$(document).ready(function(){
  $("#login_form").submit(function(){
    eshop_login($("#login_form input[name=uid]").val(),$("#login_form input[name=pwd]").val());
    return false;
  });

  ajax_pagerefresh();

  $("table.list .listrow").mouseover(function(){
    $(this).addClass("listrow_hov").removeClass("listrow_std");
  }).mouseout(function(){
    $(this).addClass("listrow_std").removeClass("listrow_hov");
  });
  
  $(".activebox").mouseover(function(){
    $(this).addClass("activebox_hov").removeClass("activebox_std");
  }).mouseout(function(){
    $(this).addClass("activebox_std").removeClass("activebox_hov");
  });  
  
  $("table.list .activerow").click(function(){
    location.href=$(this).attr("ref");
  });
  
  $(".textinput").focus(function(){ $(this).addClass("textinput_focus"); });
  $(".textinput").blur(function(){ $(this).removeClass("textinput_focus"); });
  
  $(".textinput_in").each(function(){
    var $this=$(this);
    var $parent=$(this).closest(".textinput");
    $this.width($parent.width()-2);
    $this.focus(function(){ $parent.addClass("textinput_focus"); });
    $this.blur(function(){ $parent.removeClass("textinput_focus"); });
  });
  
  $(".blinky").blinky();
  
  $("a.loading").click(function(){ work_start(); });
  $(".bottom_link a").not(".firstmenu a").click(function(){ work_start(); });
  
  $(".kosik_produkt_link").click(function(){ work_start(); window.parent.location.href=$(this).attr('data-ref'); });
  
});

(function($) {
        $.fn.blinky = function() {
          var that = this;
                function go() {
                  $(that).fadeTo(300,0,function(){ $(that).fadeTo(300,1); });
                   setTimeout(go, 3000); 
                };
                go();
        };

})(jQuery);

function keepalive(){
    $.ajax({
      'url':'eshop_ajax.php?mod=keepalive&cache='+(ka++)
    });
    setTimeout('keepalive()',60000);
}

var ka=0;
keepalive();
