/* AJAX ERROR */
$(document).ajaxError(function (request,settings,e) {
    alert('Error requesting URL: '+e.url);
});
/* URL ROUTER */
var Router = function (route,params) {
    //parametre
    if (typeof(params) == 'object') {
        var p = '';
        $.each(params,function (name,value) {
            if (p != '') {
                p += '&';
            }
            p += escape(name)+'='+escape(value);
        });
        return Router(route)+'?'+p;
    }
    else {
        return '/' + route;
    }
};
Router.route = function (route,params) {
    var url = Router(route,params);
    location.href = url;
}

/* Plugin na input hint */
jQuery.fn.inputHint = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('input[type=text]')) {
            jQuery.inputHintShow(self);
            self.focus(function () {
                jQuery.inputHintHide(this);
            }).blur(function () {
                jQuery.inputHintShow(this);
            }).closest('form').submit(function () {
                jQuery.inputHintHide(self);
                return true;
            });
        }
    });
    return this;
};
jQuery.inputHintShow = function (inpt) {
    inpt = jQuery(inpt);
    if (inpt.val() == inpt.attr('title') || inpt.val() == '') {
        inpt.addClass('hint').val(inpt.attr('title'));
    }
};
jQuery.inputHintHide = function (inpt) {
    inpt = jQuery(inpt);
    inpt.removeClass('hint');
    if (inpt.val() == inpt.attr('title')) {
        inpt.val('');
    }
};

//plugin na zebra tabulky a highlight tabulky
jQuery.fn.zebraTable = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('table')) {
            $('tbody tr:even',self).removeClass('even').addClass('odd');
            $('tbody tr:odd',self).removeClass('odd').addClass('even');
        }
    });
    return this;
};
jQuery.fn.highlightTable = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('table')) {
            $('tbody tr',self).unbind('mouseover.highlight').bind('mouseover.highlight',function () {
                $('>td',this).addClass('highlight');
            });
            $('tbody tr',self).unbind('mouseout.highlight').bind('mouseout.highlight',function () {
                $('>td',this).removeClass('highlight');
            });
        }
    });
    return this;
};

$(function(){
	//hinty vo formularoch
	$('input.hint').inputHint();
	//scroller znaciek
	$('#brandsAnimation').innerfade({
        animationtype: 'slide',
        timeout: 6000,
        containerheight: $('#brandsAnimation').height() + 'px'
	});
	$('#brandsAnimation a').hover(function () {
        $(this).find('img.default').hide();
        $(this).find('img.hover').show();
    },function () {
        $(this).find('img.hover').hide();
        $(this).find('img.default').show();
    });
    
    $('table.zebra').zebraTable();
    $('table.highlight').highlightTable();
});




/* Scroller */
var active;
$(function() {
        
    active = true;
    if($("#action-products div.items div.item").length > 2){
        $("#action-products").everyTime(4000,function(){
            if(active){
                ProductScroll($(this).attr('id'));
            }
        }).mouseover(function(){
            $(this).stopTime();
        }).mouseout(function(){
            $(this).everyTime(4000,function(){
                if(active){
                    ProductScroll($(this).attr('id'));
                }
            });
        });
        
        $("#toNext").click(function(){
            if(active){
                ProductScrollMove("action-products",$("#action-products div.items div.item:eq(1)"));
            }
            return false;
        });
        
        $("#toPrev").click(function(){
            if(active){
                ProductScrollMove("action-products",$("#action-products div.items div.item:eq(0)"),"left");
            }
            return false;
        });
    } else {
        $("#toNext").css({ opacity : 0.3 });
        $("#toPrev").css({ opacity : 0.3 });
    }
});

function ProductScroll(id) {
    var item = $("#" + id + " div.items div.item:eq(0)")
    if(item){
        active = false;
        item.clone().appendTo($("#" + id + " div.items"));
        var moveTo = 0 - parseInt(item.width());
        $("#" + id + " div.items").animate({ left: moveTo + 'px' },1000,function(){
            item.remove();
            active = true;
            $(this).css('left',0);
        });
    }
}

function ProductScrollMove(id,item,direction){
    if(item){
        active = false;
        if(direction == "left"){
            var moveTo = 0;
            var lastItem = $("#" + id + " div.items div.item:last-child");
            lastItem.clone().prependTo($("#" + id + " div.items"));
            $("#" + id + " div.items").css({ left: (0 - item.width()) + 'px'});
            $("#" + id + " div.items").animate({ left: moveTo + 'px' },1000,function(){
                lastItem.remove();
                active = true;
            });
        } else {
            
            var moveTo = 0 - item.position().left;
            if(moveTo != 0){
                var firstItem = $("#" + id + " div.items div.item:first-child");
                firstItem.clone().appendTo($("#" + id + " div.items"));
                $("#" + id + " div.items").animate({ left: moveTo + 'px' },1000,function(){
                    firstItem.remove();
                    active = true;
                    $(this).css('left',0);
                });
            }
        }
    }
}
