$(document).ready(function() {
    $('#slider').nivoSlider({
        effect: 'fade',
        directionNav: false,
        controlNav: false,
        pauseOnHover: false,
        pauseTime: 8000
    });
    
    $(".image_fancy").fancybox({
		'hideOnContentClick': false,
		'autoDimensions': true,
		'padding': '0',
		'margin': '10px',
                'overlayColor': '#000000',
                'overlayOpacity': '0.9',
                'titleShow': true
    });
    
    
    $(function(){
            $('#contentslider h1').addClass('dontend');
            $('#colsslider').columnize({
                    width : 300,
                    height : 360,
                    doneFunc: function(){
                        if($('.column').length <= 2){
                            $('#scrollbarrslider').hide();
                        }
                    }
            });
    }); 
    
    
    $(function() {
                        var newPos;
                        var newScrollVal;
                        var newScrollLeft;
			//scrollpane parts
			var scrollPane = $( "#contentslider" ),
				scrollContent = $( "#colsslider" );
			
			//build slider
			var scrollbar = $( ".scroll-bar" ).slider({
				slide: function( event, ui ) {
                                        var numcols = $('.column').length;
                                        var minDif = numcols * 300;
                                        var b;
                                        offsetContent = scrollPane.offset();
                                        for(i = 0; i<numcols; i++){
                                            offset = $('.column').eq(i).offset();
                                            dif = offsetContent.left - offset.left
                                            minDifB = Math.abs((offset.left - offsetContent.left));
                                            if(minDifB < minDif){
                                                minDif = minDifB;
                                                posDef = offset.left;
                                                b = i;
                                            }   
                                        }
                                        if(b == (numcols - 1)){
                                            c = b - 1;
                                        }else{
                                            c = b ;
                                        }
                                        newPos = (300 * c * -1);
                                        if ( scrollContent.width() > scrollPane.width() ) {
						scrollContent.css( "margin-left", Math.round(
							ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
						) + "px" );
					} else {
						scrollContent.css( "margin-left", 0 );
					}
                                        
                                        newScrollVal = Math.round(100 * ((scrollContent.css( "margin-left").replace('px', '') * -1) * (scrollPane.width() - scrollContent.width())));
                                        newScrollLeft = Math.round((c * 100)/ (numcols - 2));
				},
                                stop: function(event, ui) {
                                       
                                       
                                        
                                        scrollContent.animate({
                                            'margin-left': newPos+'px'
                                        }, 500);
                                        
                                        $('.ui-slider-handle').animate({
                                                "left": newScrollLeft+'%'
                                            }, 500, function(){
                                                newScrollVal = Math.round(100 * ((scrollContent.css( "margin-left").replace('px', '') * -1) * (scrollPane.width() - scrollContent.width())));
                                                ui.value = newScrollVal;
                                            });
                                }
			});
			
			//append icon to handle
			var handleHelper = scrollbar.find( ".ui-slider-handle" )
			.mousedown(function() {
				scrollbar.width( handleHelper.width() );
			})
			.mouseup(function() {
				scrollbar.width( "100%" );
			})
			.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
			.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
			
			//change overflow to hidden now that slider handles the scrolling
			scrollPane.css( "overflow", "hidden" );
			
			//size scrollbar and handle proportionally to scroll distance
			function sizeScrollbar() {
				var remainder = scrollContent.width() - scrollPane.width();
				var proportion = remainder / scrollContent.width();
				var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
				scrollbar.find( ".ui-slider-handle" ).css({
					width: handleSize,
					"margin-left": -handleSize / 2
				});
				handleHelper.width( "" ).width( scrollbar.width() - handleSize );
			}
			
			//reset slider value based on scroll content position
			function resetValue() {
				var remainder = scrollPane.width() - scrollContent.width();
				var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
					parseInt( scrollContent.css( "margin-left" ) );
				var percentage = Math.round( leftVal / remainder * 100 );
				scrollbar.slider( "value", percentage );
			}
			
			//if the slider is 100% and window gets larger, reveal content
			function reflowContent() {
					var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
					var gap = scrollPane.width() - showing;
					if ( gap > 0 ) {
						scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
					}
			}
			
			//change handle position on window resize
			$( window ).resize(function() {
				resetValue();
				sizeScrollbar();
				reflowContent();
			});
			//init scrollbar size
			setTimeout( sizeScrollbar, 10 );//safari wants a timeout
		});
    
    //$('#menu-rightmenu').append('<li><a href="#">kkk</a><ul class="sub-menu"><li><a href="">kkk</a></li></ul></li>');
    
    $('.sub-menu').hide();
    $('.current-menu-parent .sub-menu').show();
    
    $('#menu-rightmenu > li > a').click(function(){
        
       var obj = $(this);
       if(obj.parent().children('.sub-menu').css('display') != 'block'){
            $('.sub-menu').slideUp('normal');
            obj.parent().children('.sub-menu').slideDown('normal');
        }
        
    });
    
    $('#content').hide();
    $('#content').slideDown('slow');
    
    
});
 
