window.addEvent("domready", function(){

var el = $('chlink');
if (el) {
	$('chlink').addEvent('click', function(e){
		 $('changelink').toggleClass('nocss');
		 //$('changebutt').toggleClass('nocss');
		 $('f_username').erase('disabled');
		 $('f_username').focus();
	});
}

          /* xbox */

var x_box = new XBox();

          /* form-validation */

var form_validation = new FormValidation({
    default_border : "1px solid #CEC7B7"
});


var ext	= $$('a.flash');
	ext.each(function(e){
		e.addEvent('click', function(ev){
			ev	= new Event(ev).stop();
			window.open(e.getProperty('href'),'_blank',"status=0,toolbar=0,location=0,scrollbars=0,width=950,height=700");
		});
	});

          /* targetblanks */

$$("a.targetblank").addEvent("click", function(event){
  if(typeof window.open(this.href)=="object"){
    event.stop();
  }
});


          /* dealer gallery */

if($("photos")){
  var photos = new Photos({
    id : "photos",
    wheeler_id : "photo_wheeler",
    top_photo_wheeler_id : "top_photo_wheeler",
    top_photo_id : "top_photo_a",
    prev_id : "photo_next",
    next_id : "photo_prev",
    wheel_width : 300,
    crop_width : 263,
    item_width : 100,
    item_width_addend : 4,
    visible_items : 3,
    transition : "quad:out",
    duration : "normal"
  });
}


         /* usser & pass text */

if($$("html").get("lang") == "en"){
  var user_value = "Name";
  var pass_value = "Password";
} else {
  var user_value = "Váš e-mail";
  var pass_value = "Heslo";
}

if($("f_user")){
  if($("f_user").value == ""){
    $("f_user").value = user_value;
  }
  $("f_user")
    .addEvent("click", function(){
      if($("f_user").value == user_value){
        $("f_user").value = "";
      }
    })
    .addEvent("blur", function(){
      if($("f_user").value == ""){
        $("f_user").value = user_value;
      }
  });
}

if($("f_pass")){
  if($("f_pass").value == ""){
    $("f_pass").value = pass_value;
  }
  $("f_pass")
    .addEvent("click", function(){
      if($("f_pass").value == pass_value){
        $("f_pass").value = "";
      }
    })
    .addEvent("blur", function(){
      if($("f_pass").value == ""){
        $("f_pass").value = pass_value;
      }
    })
    .addEvent("focus", function(){
      if($("f_pass").value == pass_value){
        $("f_pass").value = "";
      }
    });
}

          /* print */

$$(".print").addEvent("click", function(){
  window.print();
  return false;
});

          /* top products */

if($("wheeler")){
  var top_products = new TopProducts({
      id : "wheeler",
      item_width : 288,
      visible_items : 2,
      prev_id : "collage_prev",
      next_id : "collage_next",
      transition : "back:in",
      duration : "750",
      delay : 3000
  });
}


          /* dealer map */

if($("dealer_map")){
  $$("#dealer_map map area").addEvent("mouseenter", function(){
      $("js_hover").className = "map_hover";
      $("js_hover").addClass(this.id);
      $("js_hover").title = this.title;
      $("js_hover").href = this.href;
    });
  $("js_prague")
    .addEvent("mouseenter", function(){
      this.removeClass("disabled");
      $("js_hover").className = "map_hover map_def";
    })
    .addEvent("mouseleave", function(){
      this.addClass("disabled");

    })
}

if($("f_clubonly_search_value")){
  $("f_clubonly_search_value").addEvent("click", function(){
  	var el;
  	el = $('f_search_info_url');
  	if (el) {
  		$(this.form).setProperty('action', el.getProperty('value'));
		}
    this.form.submit();
  });
}


if($("account_form") || $("teta_form")){
  if($("f_favorite_market_region")){
    // ajax pre nacitanie predajni
    var getStores = new Request.JSON({
      url: "/ajax.php",
      onSuccess: function(stores){
        var stores = stores[0];
        var stores_html = '<option value="0">-- vyberte --</option>';

        if(stores.length > 0){
          for(var x=0; x < stores.length; x++){
            stores_html += '<option value="' + stores[x].id + '">' + stores[x].nazev + "</option>";
          }
        } else {
          stores_html = '<option value="0">-- vyberte --</option><option value="-1">bez prodejny</option>';
        }
        $("f_favorite_market").set("html", stores_html);

      }
    });

    // vyber okresu pre letaky
    $("f_favorite_market_region").addEvent("change", function(){
      //Ajax
      getStores.get({
        "action": "get_stores",
        "region_id": this.get('value')
      });
    });

     $("f_favorite_market").addEvent("change", function(){
        if(this.get('value')!=''){
          $("f_action_offers_more").checked=true;
        }
     });
  }
}




        /* my account */

if($("account_form")){
  if($("f_new_pass") && $("f_new_pass2")){
    $("account_form").addEvent("submit", function(){
      if($("f_new_pass").value != $("f_new_pass2").value){
        alert("Nezadali jste dvakrát stejné heslo!!");
        return false;
      }
    });
  }

  // AJAX

  // Změna okresu
  var getCities = new Request.JSON({
    url: "/ajax.php",
    onSuccess: function(cities){
      var cities_html = '<option value="0">-- vyberte --</option>';
      if(cities.length > 0){
        for(var x=0; x < cities.length; x++){
          cities_html += '<option value="' + cities[x].obec_kod + '">' + cities[x].nazev + "</option>";
        }
      } else {
        cities_html = '<option value="0">-- vyberte --</option><option value="-1">bez záznamu</option>';
      }
      $("f_city").set("html", cities_html);
      $("f_city").options.selectedIndex = 0;
    }
  });

  $("f_street").addEvent("change",function(){
    if(this.value==-2){
      $("custom_street").style.display='block';
    }else{
      $("custom_street").style.display='none';
    }
  });
  $("f_region").addEvent("change", function(){
    if(this.value == 0){
      $("f_city").set("disabled", "disabled");
      $("f_city_part").set("disabled", "disabled");
      $("f_street").set("disabled", "disabled");
      $("f_city").options.selectedIndex = 0;
      $("f_city_part").options.selectedIndex = 0;
      $("f_street").options.selectedIndex = 0;
      $("f_zip").value = "";
    } else {
      $("f_city_part").set("disabled", "disabled");
      $("f_street").set("disabled", "disabled");
      $("f_city_part").options.selectedIndex = 0;
      $("f_street").options.selectedIndex = 0;
      $("f_zip").value = "";
      $("f_city").set("disabled", "");
      //Ajax
      getCities.get({
        "action": "get_cities",
        "region_id": this.value
      });
    }
  });
  // Změna města
  var getCityParts = new Request.JSON({
    url: "/ajax.php",
    onSuccess: function(cityparts){
      var cityparts_html = '<option value="0">-- vyberte --</option>';
      if(cityparts.length > 0){
        for(var x=0; x < cityparts.length; x++){
          cityparts_html += '<option value="' + cityparts[x].cobce_kod + '">' + cityparts[x].nazev + "</option>";
        }
      } else {
        cityparts_html = '<option value="0">-- vyberte --</option><option value="-1">bez záznamu</option>';
      }
      $("f_city_part").set("html", cityparts_html);
      $("f_city_part").options.selectedIndex = 0;
    }
  });

  $("f_city").addEvent("change", function(){
    if(this.value == 0){
      $("f_city_part").set("disabled", "disabled");
      $("f_street").set("disabled", "disabled");
      $("f_city_part").options.selectedIndex = 0;
      $("f_street").options.selectedIndex = 0;
    } else {
      $("f_street").set("disabled", "disabled");
      $("f_street").options.selectedIndex = 0;
      $("f_city_part").set("disabled", "");
      //Ajax
      getCityParts.get({
        "action": "get_cityparts",
        "city_id": this.value
      });
    }
  });

  // Změna části města
  var getCityStreets = new Request.JSON({
    url: "/ajax.php",
    onSuccess: function(citystreets){
      var citystreetss = citystreets[1];
      var citystreets_html = '<option value="0">-- vyberte --</option><option value="-1">bez ulice</option><option value="-2">zadat ručně</option>';
      if(citystreetss.length > 0){
        for(var x=0; x < citystreetss.length; x++){
          citystreets_html += '<option value="' + citystreetss[x].ulice_kod + '">' + citystreetss[x].nazev + "</option>";
        }
      } else {
        citystreets_html = '<option value="0">-- vyberte --</option><option value="-1">bez ulice</option><option value="-2">zadat ručně</option>';
      }
      $("f_street").set("html", citystreets_html);
      $("f_street").options.selectedIndex = 0;
      if($("f_zip").value == ""){
        $("f_zip").value = citystreets[0].psc;
      }
    }
  });

  $("f_city_part").addEvent("change", function(){
    if(this.value == 0){
      $("f_street").set("disabled", "disabled");
      $("f_street").options.selectedIndex = 0;
    } else {
      $("f_street").set("disabled", "");
      //Ajax
      getCityStreets.get({
        "action": "get_citystreets",
        "city_id": $("f_city").value,
        "citypart_id": this.value
      });
    }
  });




  // Změna PSČ

  var setZipcode = new Request.JSON({
    url: "/ajax.php",
    onSuccess: function(request){
      if(request[0].psc == "true"){
        $("f_region").value = request[1].region_id;
        $("f_city_part").set("disabled", "disabled");
        $("f_street").set("disabled", "disabled");
        $("f_city_part").options.selectedIndex = 0;
        $("f_street").options.selectedIndex = 0;
        $("f_city").set("disabled", "");
        var citiess = request[2];
        var citiess_html = '<option value="0">-- vyberte --</option>';
        if(citiess.length > 0){
          for(var x=0; x < citiess.length; x++){
            citiess_html += '<option value="' + citiess[x].obec_kod + '">' + citiess[x].nazev + "</option>";
          }
        } else {
          citiess_html = '<option value="0">-- vyberte --</option><option value="-1">bez záznamu</option>';
        }
        $("f_city").set("html", citiess_html);
      } else {
        alert("PSČ nebylo nalezeno v databázi.");
      }
    }
  });

  var setZipCode = function(){
    var zipcode = this.value.clean().replace(" ", "").substr(0,5);
    this.value = zipcode;
    if(zipcode.length > 4){
      if(/^[0-9]{5}$/.test(zipcode)){
        setZipcode.get({
          "action": "set_zipcode",
          "zipcode": zipcode
        });
      } else {
        alert("PSČ je zadáno ve špatném tvaru!");
      }
    }

  }

  $("f_zip").addEvent("keyup", setZipCode);

  if(!($("account_form").hasClass("edit"))){
    $("f_city").set("disabled", "disabled");
    $("f_city_part").set("disabled", "disabled");
    $("f_street").set("disabled", "disabled");


    if($("f_action_offers") && $("f_action_offers_more") && $("f_favorite_market_region") && $("f_favorite_market")){

      // init
      $("f_action_offers").checked = true;
      $("f_action_offers_more").checked = false;
      $("f_favorite_market_region").options.selectedIndex = 0;
      $("f_favorite_market").options.selectedIndex = 0
      $("f_favorite_market_region").disabled = true;
      $("f_favorite_market").disabled = true;


      $("f_action_offers").addEvent("click", function(){
        if(this.checked == true){
          $("f_action_offers_more").disabled = false;
        } else {
          $("f_action_offers_more").checked = false;
          $("f_action_offers_more").disabled = true;
          $("f_favorite_market_region").options.selectedIndex = 0;
          $("f_favorite_market").options.selectedIndex = 0;
          $("f_favorite_market_region").disabled = true;
          $("f_favorite_market").disabled = true;
          $("f_favorite_market_region").removeClass("check-is-selected-first");
          $("f_favorite_market").removeClass("check-is-selected-first");
          $("f_favorite_market_region").setStyle("background", "#ffffff");
          $("f_favorite_market").setStyle("background", "#ffffff");
        }
      });

      $("f_action_offers_more").addEvent("click", function(){
        if(this.checked == true){
          $("f_favorite_market_region").disabled = false;
          $("f_favorite_market").disabled = false;
          $("f_favorite_market_region").addClass("check-is-selected-first");
          $("f_favorite_market").addClass("check-is-selected-first");
        } else {
          $("f_favorite_market_region").options.selectedIndex = 0;
          $("f_favorite_market").options.selectedIndex = 0;
          $("f_favorite_market_region").disabled = true;
          $("f_favorite_market").disabled = true;
          $("f_favorite_market_region").removeClass("check-is-selected-first");
          $("f_favorite_market").removeClass("check-is-selected-first");
          $("f_favorite_market_region").setStyle("background", "#ffffff");
          $("f_favorite_market").setStyle("background", "#ffffff");
        }
      });



    }

  }
}


if($("teta_form")){
  if($("f_action_offers") && $("f_action_offers_more") && $("f_favorite_market_region") && $("f_favorite_market")){

    if($("f_action_offers").checked == false){
        $("f_action_offers_more").checked = false;
        $("f_action_offers_more").disabled = true;
        $("f_favorite_market_region").options.selectedIndex = 0;
        $("f_favorite_market").options.selectedIndex = 0;
        $("f_favorite_market_region").disabled = true;
        $("f_favorite_market").disabled = true;
        $("f_favorite_market_region").removeClass("check-is-selected-first");
        $("f_favorite_market").removeClass("check-is-selected-first");
        $("f_favorite_market_region").setStyle("background", "#ffffff");
        $("f_favorite_market").setStyle("background", "#ffffff");
    }


    $("f_action_offers").addEvent("click", function(){
      if(this.checked == true){
        $("f_action_offers_more").disabled = false;
      } else {
        $("f_action_offers_more").checked = false;
        $("f_action_offers_more").disabled = true;
        $("f_favorite_market_region").options.selectedIndex = 0;
        $("f_favorite_market").options.selectedIndex = 0;
        $("f_favorite_market_region").disabled = true;
        $("f_favorite_market").disabled = true;
        $("f_favorite_market_region").removeClass("check-is-selected-first");
        $("f_favorite_market").removeClass("check-is-selected-first");
        $("f_favorite_market_region").setStyle("background", "#ffffff");
        $("f_favorite_market").setStyle("background", "#ffffff");
      }
    });

    $("f_action_offers_more").addEvent("click", function(){
      if(this.checked == true){
        $("f_favorite_market_region").disabled = false;
        $("f_favorite_market").disabled = false;
        $("f_favorite_market_region").addClass("check-is-selected-first");
        $("f_favorite_market").addClass("check-is-selected-first");
      } else {
        $("f_favorite_market_region").options.selectedIndex = 0;
        $("f_favorite_market").options.selectedIndex = 0;
        $("f_favorite_market_region").disabled = true;
        $("f_favorite_market").disabled = true;
        $("f_favorite_market_region").removeClass("check-is-selected-first");
        $("f_favorite_market").removeClass("check-is-selected-first");
        $("f_favorite_market_region").setStyle("background", "#ffffff");
        $("f_favorite_market").setStyle("background", "#ffffff");
      }
    });

  }
}

if($("account_form")){
  if(!($("account_form").hasClass("edit"))){
    if($("f_city" ) && $("f_city_part")){
      if($("f_city").options.selectedIndex != 0 && $("f_city_part").options.selectedIndex == 0){
        //Ajax
        getCityParts.get({
          "action": "get_cityparts",
          "city_id": $("f_city").value
        });
        $("f_city").set("disabled", "");
        $("f_city_part").set("disabled", "");
      }
    }
  }
}


if($$(".calendar_ico").length > 0){
    var calendar_info = new Element("div", {
      "id" : "calendar_info"
    });
    calendar_info.inject(document.body);
    var calendar_info_html = '<p>Kliknutím na datum přepnete na výběr měsíce.</p><p class="tac"><img src="/common/images/layout/calendar/calendar_info_image.png" alt="" /></p><p>Dalším kliknutím přepnete na možnost vybrat rok.</p>';
    $("calendar_info").set("html", calendar_info_html);
    $("calendar_info").setStyle("opacity", 0);
}

});

