$(document).ready(function(){
	$.fx.speeds._default = 500;
	
	//flash messages
	var klasa = $('#flashMessage').attr('class');
	$('#flashMessage').dialog({
		hide: 'puff',
		draggable: false,
		modal: true,
		resizable: false,
		dialogClass: klasa
	});
	
	var time = $('#flashMessage').attr('rel') !== undefined ? $('#flashMessage').attr('rel') : 1500;	
	setTimeout(function(){$('#flashMessage').dialog("close")}, time);
	
	//banners
	$('#banner-layer').dialog({
		hide: 'puff',
		draggable: false,
		modal: true,
		resizable: false,
		width: 600,
		height: 400,
                zIndex: 50000
	});
        
        
        //przewijanie submenu
        var scrollable = $('div.scrollable');
        var items = $('div.scrollable .items');
        var lastItem = items.find('.element:last-child');

        scrollable.css({overflow: 'hidden'});

        scrollable.mousemove(function(e){
                var divWidth = scrollable.width();
                var itemsWidth = lastItem[0].offsetLeft + lastItem.outerWidth();

               var left = (e.pageX - scrollable.offset().left) * (itemsWidth-divWidth) / divWidth;
               scrollable.scrollLeft(left);
        });


	
	//menu
                  $('#submenuUl li:first').addClass('first');
                  $('#submenuUl li').hover(function(){
                      $(this).addClass('hover');
                  },function(){
                      $(this).removeClass('hover');
                  });
                   
                   
                    $('#menu ul>li').hover(function(){
                            var height = $(this).find('ul:first').height();   
                            var $container = $(this).find('.submenuContainer:first').stop().css('height','auto');
                            
                            
                            $(this).find('.submenuContainer:first').removeClass('hide');
                            $(this).addClass('hover').find('ul:first').stop(true).css('opacity','1').animate({'top':'0px'},300);
                           
                        
                    },function(){
                            var $this = $(this);
                            var $li = $(this).removeClass('hover').find('ul:first');
                            var $container = $this.find('.submenuContainer:first');
                            var slideOffset = $li.height() * -1;
                            $(this).removeClass('hover').find('ul:first').stop(true)
                            .animate({'top':slideOffset+'px','opacity':'0'},300,
                                function(){
                                    $container.stop().animate({'height':'0px'},300,
                                        function(){$container.addClass('hide')})
                                    }
                            );
                    }).find('.submenuContainer:first').addClass('hide');
        
	
	//dropdown
	$(".dropdown dt a").click(function(e) {
        $(this).parent().parent().find("dd ul").toggle();
		e.preventDefault();
	});
	
	$(".dropdown dd ul li a").click(function(e) {
		var text = $(this).html();
		var value = $(this).find('.value').html();
		var mainParent = $(this).parent().parent().parent().parent()
		
		mainParent.find("dt a span").html(text);
		$(".dropdown dd ul").hide();
		
		var id = mainParent.attr('id').replace('To', '');
		$('#'+id).val(value);
		
		e.preventDefault();
	});
        
        var localizationId = $('#InwLocalLocalizationId').val();
        if (localizationId > 0){
	$('#ToInwLocalLocalizationId dd a').each(function(){
            var $text = $(this).find('span').text();
            if ($text == localizationId){
                $(this).trigger('click');
            }
        });
        }
	
	//left menu
	$('#leftMenu a.category').click(function(e){
		$(this).parent().find('ul').slideToggle();
		
		e.preventDefault();
	});
	
	$('#leftMenu li.active').parent().show();
	
	$('.form-input').click(function(){
		$(this).val('');
	});
});

$(document).bind('click', function(e) {
    var $clicked = $(e.target);
    if (! $clicked.parents().hasClass("dropdown")){
        $(".dropdown dd ul").hide();
	}
});
