var d = new Date();

$(function(){
	jQuery('#moving-banners').jcarousel({
        auto: 3,
        wrap: 'circular',
        visible: 1,
        animation: 'slow',
        scroll: 1,
        initCallback: mycarousel_initCallback
    });
	
	/*
$('textarea.editor').htmlarea({
		// Override/Specify the Toolbar buttons to show
		toolbar: [
		    ["html", "|", "bold", "italic", "|", "increasefontsize", "decreasefontsize", "|", "forecolor", "|", "orderedlist", "unorderedlist", "|", "indent", "outdent", "|", "link", "unlink", "image"],
		],
		
		css: ["/lib/css/text.css"]
    });
*/
	
	$("a.fancybox").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'hideOnContentClick': true
	});
	
	//$(".timestamp").prettyDate();
	
	$('.datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		changeMonth: true,
		changeYear: true,
		yearRange: '1901:'+d.getFullYear(),
		monthNamesShort: ['Jaan','Veeb','Märts','Aprill','Mai','Juuni','Juuli','Aug','Sept','Okt','Nov','Dets']
	});
	
	$('.long_datepicker').datepicker({
		dateFormat: 'yy-mm-dd H:i',
		changeMonth: true,
		changeYear: true,
		yearRange: '1901:'+(d.getFullYear()+5),
		monthNamesShort: ['Jaan','Veeb','Märts','Aprill','Mai','Juuni','Juuli','Aug','Sept','Okt','Nov','Dets']
	});
    
	$("a.fancybox").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'hideOnContentClick': true
	});
	
	$('.confirm').click(function(){
		return confirm('Oled sa kindel?');
	});
	
	$('.toggle').click(function(){
		if($(this).attr('rel') != 'undefined'){
			var rel = $(this).attr('rel');
			$('#'+rel).toggle();
		}
		return false;
	});
	
	$('.shopping_cart_table .amountinput').live('keyup', function (){
		var item_val = parseInt($(this).attr('value'));
		if(is_numeric(item_val) == false){
			$(this).attr('value', 0);
		}
	});
	
	setTimeout(function (){ play_right_banners(); }, 5000);
	var play_right_banners = function(){
		var visible_banner = $('.banner_300x100.show p:visible');
		visible_banner.fadeOut('slow');
		//visible_banner.hide('slide', {direction: 'left'});
		if(visible_banner.next('p').exists()){
			visible_banner.next('p').fadeIn('slow');
			//visible_banner.next('p').show('slide', {direction: 'left'});
		}
		else{
			$('.banner_300x100.show p:first').fadeIn('slow');
			//$('.banner_300x100 p:first').show('slide', {direction: 'left'})
		}
		
		setTimeout(function (){ play_right_banners(); }, 5000);
	}
	
	$('#slideshow a img:first').addClass('active');
	setInterval("slideSwitch('#slideshow', 1000)", 4000);
	
	$('#slideshow2 a img:first').addClass('active');
	setInterval("slideSwitch('#slideshow2', 1000)", 2000);
});

function is_numeric (mixed_var) {
    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
}

function slideSwitch(el, speed) {
    var $active = $(el+' a img.active');
	
    if ( $active.length == 0 ) $active = $(el+' a img:first');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next('img').length ? $active.next('img') : $(el+' a img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, speed, function() {
            $active.removeClass('active last-active');
        });
}

function mycarousel_initCallback(carousel){
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

jQuery.fn.exists = function(){return jQuery(this).length>0;}
