/**
 * Copyright (c) 2010, Jakub Navratil
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @author        Jakub (TrubiT) Navratil
 * @copyright     Copyright (c) 2010 Jakub Navratil (navrc.kuba@seznam.cz) 
 * @license       New BSD License 
 * @date          23/6/2010
 * @version       1.0.6
 * 
 * @requires jQuery (tested with 1.3.2)
 *  
 * Usage: (can be used on multiple items in selector)
 * --------------------------------------------------
 * $('select').mySelectbox();
 * 
 * @param maxHeight: 250, //maximum list height(px) before vertical scrollbar appear
 * @param myClass: null, //you can specify your own class -> container will be: class="my_selectbox {myClass}" 
 */
(function($) {
  //function
  $.fn.mySelectbox = function(options) {
    
    //default settings
    var settings = {
				maxHeight: 250,
				myClass: null
		}
		
		//merge settings into var settings
		jQuery.extend(settings, options);
    
    //on each selected items
    $(this).each(function() {
      if(!$(this).is('select')) //check if item is select
        return;
    
      //init
      var selectbox = $('<div></div>');
      if(this.name=="vpoh"){
      selectbox.addClass('my_selectbox137');
      }
      else if(this.name=="vod" || this.name=="vpo"){
      selectbox.addClass('my_selectbox87');
      }
      else if(this.name=="vkra"){
      selectbox.addClass('my_selectbox197');
      }
      else if(this.name=="vkoh"){
      selectbox.addClass('my_selectbox137');
      }
      else if(this.name=="uztyp"){
      selectbox.addClass('my_selectbox197');
      }
      else if(this.name=="uzrad"){
      selectbox.addClass('my_selectbox197');
      }
      else{
      selectbox.addClass('my_selectbox');
      }
      if(settings.myClass) 
        selectbox.addClass(settings.myClass);
      
      //handle click
      selectbox.click(function(event) {       //something like "equals?"
        if($.fn.mySelectbox.notclosed && $(this)[0] != $.fn.mySelectbox.notclosed[0]) { //close previously opened
          $.fn.mySelectbox.notclosed.find('.my_selectbox_body').mySelectbox_toggle(true);
        }
        $.fn.mySelectbox.notclosed = $(this); //store last opened 
        $(document).click(function() { //close when clicked away
          $.fn.mySelectbox.notclosed.find('.my_selectbox_body').mySelectbox_toggle(true);
        });
        
        //togle show
        var body = $(this).find('.my_selectbox_body');
        body.mySelectbox_toggle();
        
        //resize etc.
        if(body.height() > settings.maxHeight) {
          body.css('overflow-y', 'scroll');
          body.css('overflow-x', 'hidden'); //appears in FF when item clicked
          body.height(settings.maxHeight);
        }
        if(body.innerWidth() < selectbox.innerWidth()) {
          var borders = parseInt(body.css('borderRightWidth'))+parseInt(body.css('borderLeftWidth'));
          body.width(selectbox.innerWidth()-borders);
        }
        
        //disable to call from document click
        event.stopPropagation();
      });
      
      var head = $('<span></span>');
      head.addClass('my_selectbox_head');
      
      var ul = $('<ul></ul>');
      ul.addClass('my_selectbox_body');
      
      //every option
      $(this).find('option').each(function(index) {
        var opt = $('<a href="#"></a>');
        opt.text($(this).text());
        opt.click(function(event){
          event.preventDefault();
          $(this).mySelectbox_select(index);  
        });
        
        var li = $('<li></li>');
        opt.appendTo(li);
        li.appendTo(ul);
        
        if($(this).is('option:selected'))
          head.text($(this).text()); //set head text
      });
      
      //append after select
      head.appendTo(selectbox);
      $('<span class="my_selectbox_arrow"></span>').appendTo(selectbox);
      ul.appendTo(selectbox);
      selectbox.insertAfter($(this));
      
      
      //hide select
      $(this).hide();
    });
    
    //function which handle onclick selection
    $.fn.mySelectbox_select = function(index) {
      var container = $(this).parent().parent().parent();
      
      //show selected item
      container.find('span.my_selectbox_head').text($(this).text());
      
      //select right option
      container.parent().find('select option').removeAttr('selected');
      container.parent().find('select option').eq(index).attr('selected', 'selected');  
    }
    
    //function which handle toggle and scroll informations
    $.fn.mySelectbox_toggle = function(hide) {
      $(this).parent().addClass('my_selectbox_topmost'); //force z-index
      
      //need to scroll top before hide - causes FF window blink
      if($(this).scrollTop() > 0)
        $(this).data('scroll', $(this).scrollTop()); //save scroll location
      $(this).scrollTop(0); //reset scroll
      
      //show or hide
      if(hide) {
        $(this).toggle(false); //hide
        $(this).parent().removeClass('my_selectbox_topmost');
      }
      else { 
        $(this).toggle(); //hide/show
      }
      
      $(this).scrollTop($(this).data('scroll')); //scroll to saved position
    }    
  };	
})(jQuery);







///////////////////////////




$(document).ready(function(){

    $('#vpoh').mySelectbox();  
    $('#vkoh').mySelectbox();  
    $('#vod').mySelectbox();  
    $('#vpo').mySelectbox();  
    $('#vkra').mySelectbox();  
    $('#uztyp').mySelectbox();  
    $('#uzrad').mySelectbox();  


    $('#clickme').click(function() {
  $('#topduelymuzi').show();
  $('#topduelyzeny').hide();
  $("#clickme").removeClass("muzi2").addClass("muzi1");
  $("#clickme2").removeClass("zeny2").addClass("zeny1");
});
   $('#clickme2').click(function() {
  $('#topduelymuzi').hide();
  $('#topduelyzeny').show();
  $("#clickme").removeClass("muzi1").addClass("muzi2");
  $("#clickme2").removeClass("zeny1").addClass("zeny2");

});


$(".blank").attr("target", "_blank");

});
