jQuery(document).ready(function() {
    /*
    jQuery('#mycarousel1').jCarouselLite({
            scroll: 1,
            auto: 4,
            animation: 'slow',
            wrap: 'circular'
            });
    */
    
    
    $('#myselectbox').selectbox({debug: true});
    
    /* HP slider
    ----------------------------------------------------- */    
    /* prvni je aktivni */
    $(".slider-link:first").addClass("active");

    
    /* klika se na odkaz */
    $(".slider-link").click(function(e){
        
        e.preventDefault();
        
        var rel = $(this).attr("rel");
        $(".slider-link").removeClass("active");
        $(this).addClass("active");
        
    });
    
    $("#myselectbox_container").hover(
            function()
            {
                // nic
            },
            
            function()
            {
                if (this.style.display == 'none')
                {
                    var url = $(this).find('li[class=selected]').attr('id');
                    window.location.href = url.split('/')[1];
                }
            }

        
        
        );

    
});


/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
 jQuery.timer = function (interval, callback)
 {
 /**
  *
  * timer() provides a cleaner way to handle intervals  
  *
  *	@usage
  * $.timer(interval, callback);
  *
  *
  * @example
  * $.timer(1000, function (timer) {
  * 	alert("hello");
  * 	timer.stop();
  * });
  * @desc Show an alert box after 1 second and stop
  * 
  * @example
  * var second = false;
  *	$.timer(1000, function (timer) {
  *		if (!second) {
  *			alert('First time!');
  *			second = true;
  *			timer.reset(3000);
  *		}
  *		else {
  *			alert('Second time');
  *			timer.stop();
  *		}
  *	});
  * @desc Show an alert box after 1 second and show another after 3 seconds
  *
  * 
  */

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };


$.fn.iSlider = function(config){
    
    $(this).each(function(){
        
        $(this).iSliderSingle(config);
        
    });
    
}

$.fn.iSliderSingle = function(config){
    
    var defaultConfig = {
        arrows: false,
        effect: 'slide',
        nav: 'manual',
        steps: 1,
        displayedSlides: 1,
        arrowsPosition: 'auto',
        slideCallback: false,
        slideEndCallback: false
    }
    
    config = $.extend({}, defaultConfig, config)
    
    var slider = $(this);
    
    var slidy = slider.find('.slide');
    
    
    var pocetSlidu = slidy.length;

    var currentSlide = 0;

    var sliderWidth = slider.find('.slider-container').width();
    var sliderHeight = slider.find('.slider-container').height();
/*
    slider.find('.slider-container').width(sliderWidth);
    slider.find('.slider-container').height(sliderHeight);
*/
    slider.find('.slider-container').css('z-index', '105');
    slider.find('.slider-container').css('overflow', 'hidden');

    /*
    slidy.each(function(){
        if($(this).outerHeight() > sliderHeight){
            sliderHeight = $(this).outerHeight();
        }
    });
    */
    //slider.find('.slider-container').height(sliderHeight);
    
    slidy.css('position','absolute');
    
    
    slidy.hide();
    
    if(config.nav == 'generate'){
        var i = 0;
        var nav = '';
        for(i=0;i<pocetSlidu;i++){
            nav = nav + '<a href="javascript:void(0)" data-slide="' + i + '">' + (i+1) + '</a>';
        }
        slider.find('.slidernav').prepend(nav);
    }
    
    
    if(config.arrows){
        
        var arrowR = $('<span class="arrowr"></span>').appendTo(slider);
        var arrowL = $('<span class="arrowl"></span>').appendTo(slider);
        
        arrowR.css('position', 'absolute').css('cursor', 'pointer');
        arrowL.css('position', 'absolute').css('cursor', 'pointer');
        if(config.arrowsPosition == 'auto'){
            arrowR.css('right', '-' + arrowR.width() + 'px');
            arrowL.css('left', '-' + arrowL.width() + 'px');
            arrowR.css('top', (sliderHeight/2 - arrowR.height()/2) + 'px');
            arrowL.css('top', (sliderHeight/2 - arrowL.height()/2) + 'px');
        }

        arrowR.click(function(){
            var posun = config.steps;
            if(currentSlide + posun > pocetSlidu - 1){
                posun = pocetSlidu - currentSlide - 1;       
            }
            slide(currentSlide + posun); 
        });
        arrowL.click(function(){
            var posun = config.steps;
            if(currentSlide - posun < 0){
                posun = currentSlide;       
            }
           slide(currentSlide - posun); 
        });
       
    }


    slider.find('.slidernav a').click(function(){
        var page = $(this).attr('data-slide');
        slide(page);
    });
    
    function slide(page){
        
        if(typeof config.slideCallback == 'function'){
            config.slideCallback(slidy.eq(currentSlide), slidy.eq(page));
        }
        
        slider.find('.slidernav a').removeClass('active');
        slider.find('.slidernav a').eq(page).addClass('active');

        if(config.effect == 'reel'){
        
            var posunTotal = 0;
        
            for(i = 0; i < pocetSlidu; i++){
                
                slidy.eq(i).show();
                if(i == page){
                    var newPosun = posunTotal;
                }
                if(i == currentSlide){
                    var oldPosun = posunTotal;
                }
                posunTotal += slidy.eq(i).outerWidth(true);
                
            }
            
            posunTotal = 0;
        
            for(i = 0; i < pocetSlidu; i++){
        
                slidy.eq(i).animate({left: (posunTotal - newPosun) + 'px'}, 'slow', function(){
                    
                    slideEnd(currentSlide, page);
                    /*
                    if(parseInt($(this).css('left')) < 0 ||  parseInt($(this).css('left')) > sliderWidth){
                        //$(this).hide();
                    }*/
                });
                posunTotal += slidy.eq(i).outerWidth(true);
                
            }
        
        }else{
        
    
            if(currentSlide < page){
                slidy.stop(true, true);
                
                if(config.effect == 'slide'){
                    
                    slidy.eq(currentSlide).animate({left: '-' + sliderWidth + 'px'}, 'slow', function(){
                        $(this).hide();
                    });
                    slidy.eq(page).show();
                    slidy.eq(page).css('left', sliderWidth + 'px');
                    slidy.eq(page).animate({left: '0px'}, 'slow', function(){
                        slideEnd(currentSlide, page);
                    });
                    
                }else if(config.effect == 'fade'){
                    
                    slidy.eq(currentSlide).css('z-index', '101');
                    slidy.eq(page).css('z-index', '100');
                    slidy.eq(page).css('left', '0px');
                    slidy.eq(page).show();
                    slidy.eq(currentSlide).fadeOut('slow', function(){
                        slideEnd(currentSlide, page);
                    });
                    
                }
                
            }else if(currentSlide > page){
                slidy.stop(true, true);
                
                if(config.effect == 'slide'){
                    
                    slidy.eq(currentSlide).animate({left: sliderWidth + 'px'}, 'slow', function(){
                        $(this).hide();
                    });
                    slidy.eq(page).show();
                    slidy.eq(page).css('left', '-' + sliderWidth + 'px');
                    slidy.eq(page).animate({left: '0px'}, 'slow', function(){
                        slideEnd(currentSlide, page);
                    });
                    
                }else if(config.effect == 'fade'){
                    
                    slidy.eq(currentSlide).css('z-index', '101');
                    slidy.eq(page).css('z-index', '100');
                    slidy.eq(page).css('left', '0px');
                    slidy.eq(page).show();
                    slidy.eq(currentSlide).fadeOut('slow', function(){
                        slideEnd(currentSlide, page);
                    });
                    
                }
                
            }else{
    
                slidy.eq(page).show();
                slideEnd(currentSlide, page);
     
            }
            
        }
        
        if(config.arrows){
             
            if(page < pocetSlidu - config.displayedSlides){
                arrowR.addClass('active');
            }else{
                arrowR.removeClass('active');
            }
            if(page != 0){
                arrowL.addClass('active');
            }else{
                arrowL.removeClass('active');
            }
        }

        currentSlide = page;
    }
    
    function slideEnd(page, newPage){
        if(typeof config.slideEndCallback == 'function'){
            config.slideEndCallback(slidy.eq(page), slidy.eq(newPage));
        }
    }
    
    slide(currentSlide);
    
    //slider.find('.slidernav a').eq(currentSlide).click();
    
}


function overlay(state, callback){
    if(state){
        $('.overlay').show().stop().animate({'opacity':'0.8'}, function(){
            if(typeof callback == 'function'){
                callback();
            }
        });
    }else{
        $('.overlay').stop().animate({'opacity':'0'},function(){
            $(this).hide();
            if(typeof callback == 'function'){
                callback();
            }
        });
    }
}

function popup(selector, callback){
    
    var content;
    if(typeof selector == 'string'){
        content = selector;
    }else{
        content = selector.html();
    }
    
    overlay(true, function(){
        
        
        $('#popup').html(content);
        $('#popup').show();
        $('#popup').css('left', $(window).width()/2 - $('#popup').width()/2);
        $('#popup').css('top', $(window).height()/2 - $('#popup').height()/2);

        if(typeof callback == 'function'){
            callback($('#popup'));
        }

        $('.overlay').click(function(){
            overlay(false);
            $('#popup').hide();
        });
    });
    
}



$.fn.loadImages = function(callback){
    
    var n = $(this).find('img[data-src][src=""]').length;
    if(n){
        $(this).find('img[data-src][src=""]').each(function(){
            $(this).attr('src', $(this).attr('data-src'));
            $(this).load(function() {
                n = n - 1;
                if(n <= 0){
                    if(typeof callback == 'function'){
                        callback();
                    }
                }
            });
        });
    }else{
        if(typeof callback == 'function'){
            callback();
        }
    }

}

$.fn.ajaxSubmit = function(callback){
    
    var method = $(this).attr('method').toLowerCase();
    var action = $(this).attr('action');
    var data = $(this).serialize();
    
    if(method == 'post'){
        $.post(action, data, function(response){
            if(typeof callback == 'function'){
                callback(response);
            }
        });
    }else{
        $.get(action+'&'+data, function(response){
            if(typeof callback == 'function'){
                callback(response);
            }
        });
    } 

}

