$(document).ready(function(){  
  var self = this;
  this.win = "";
  this.status = 0;
  // okno na center
  this.center = function(element){
    // zjisteni výšky a šířky okna
    if($.browser.msie) {
       // pro retardovane prohližeče :(
       this.wHeight = document.documentElement.clientHeight;
       this.wWidth  = document.documentElement.clientWidth;
    }  else  {
       // pro ostatní :)
       this.wWidth  = window.innerWidth;
       this.wHeight = window.innerHeight;
    }
    // výška šířka elementu
	  this.pHeight = $(element).height();
	  this.pWidth  = $(element).width();
    $(element).css({
		  "position": "fixed",
		  "top": this.wHeight/2-this.pHeight/2,
		  "left": this.wWidth/2-this.pWidth/2
	  });
  }
  
  // otevření okna
  this.open = function(element) {
    if(this.status == 0){
		  $("#ContentClona").fadeIn("slow");
		  $(element).fadeIn("slow");
		  this.status = 1;
	  }
  }
  
  // uzavření okna
  this.close = function(element) {
    if(this.status==1){
		  $("#ContentClona").fadeOut("slow");
		  $(element).fadeOut("slow");
		  $(element).empty();
		  this.status = 0;
	  }
  }
  
  // okno
  this.popupWindow = function() {
    $("#popupClose").click(function(){
      self.close(self.win);
      return false;
    });
    $("#ContentClona").click(function(){
		  self.close(self.win);
		  return false;
	  });
	  $(document).keydown(function(e){
	    this.key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
      if(this.key==27 && self.status == 1){
		     self.close(self.win);
		     return false;
		  }
	  }); 
  }
  
  // specifické funkce pro detail prstenu
  this.prsten = function() {
    this.win = "#popupPrsten";
    this.popupWindow();
    self.center("#popupPrsten");
    self.open("#popupPrsten");
    
	  $("#openEmail").click(function() {
	    $.ajax({url: this.getAttribute("href")+"&js=1",cache: false,
              success: function(html){
                   $("#popupEmail").append(html);
                   self.close("#popupPrsten");
                   self.email();
              }
      });
      return false;
    });
  }

  this.kontrola = function() {
     if($('input[name=kontakt]').val() == "") {
       alert('Prosím vyplnte kontaktní email !');
       return false;
     }
     this.emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     if (this.emailReg.test($('input[name=kontakt]').val()) == false){
       alert('Špatný email !');
       return false;
     }
     return true;
  }
  
  // specifická email funkce
  this.email = function() {
    this.win = "#popupEmail";
    this.popupWindow();
    self.center("#popupEmail");
    self.open("#popupEmail");
    
    $("#form").submit(function(){
      if(self.kontrola()) {
      $.ajax({
        url: this.getAttribute("action")+"&js=1",cache: false,
        type: "POST",data: "kontakt="+$('input[name=kontakt]').val()+"&msg="+$('textarea[name=msg]').val(),
        success: function(){
            alert("Email byl odeslán, děkujeme.");
            self.close("#popupEmail");
        }
      });
      }
      return false;
    });
    return false;
  }
  
  // init souboru
  this.init = function() {
    this.elmnts = $('#homepage > div > a');
    for (var i=0; i <= (this.elmnts.length-1); i++) {
	    $(this.elmnts[i]).click(function(){
	    $.ajax({
        url: this.getAttribute("href")+"&js=1",cache: false,
        success: function(html){
            $("#popupPrsten").append(html);
            self.prsten();
        }
      });
      return false;
      });
    }
  }
    
  // starting
  if($.browser.msie && $.browser.version.substr(0,1) > 6 || $.browser.msie == false) {
     this.init();
  }
});
