/* 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;
};
      

window.fbAsyncInit = function() {
    FB.init({appId: '127402360637215', status: true, cookie: true,
             xfbml: true});
    };

$(function(){
    $.getScript(document.location.protocol + '//connect.facebook.net/sk_SK/all.js');   
	//hinty vo formularoch
	$('input.hint').inputHint();;
	//zebra tabulky + highlight
    $('table.zebra').zebraTable();
    $('table.highlight').highlightTable();
    //selecty
	$('select.sselect').sSelect({
        ddMaxHeight: 150,
        minListWidth: 100
	});
	$('#searchPanelForm select').sSelect({
        width:162,
        ddMaxHeight: 150,
        minListWidth: 100
	});
	//animacia hlavneho banneru
	var anim = $('#mainimage ul');
	if (anim.length) {
	    anim.innerfade({
            speed: 1000,
            timeout: 5000
	    });
	    $('#mainimage').addClass('animation');
	}
	//animacia hlavneho banneru 2
	var anim2 = $('#banner-homepage-2 ul');
	if (anim2.length) {
	    anim2.innerfade({
            speed: 1000,
            timeout: 5000
	    });
	    $('#banner-homepage-2').addClass('animation');
	}
	$('#infopanel1anim ul').innerfade({
        speed: 1000,
        timeout: 7000,
        animationtype: 'slide'
    });
	//akcie-novinky-vypredaj
	$('#anvSale').hover(function () {
        $('#anv').removeClass('a n v').addClass('a');
    },function () {
        $('#anv').removeClass('a n v').addClass($('#anv a.active').attr('rel'));
    });
    
    $('#anvNew').hover(function () {
        $('#anv').removeClass('a n v').addClass('n');
    },function () {
        $('#anv').removeClass('a n v').addClass($('#anv a.active').attr('rel'));
    });
    
    $('#anvSaleOut').hover(function () {
        $('#anv').removeClass('a n v').addClass('v');
    },function () {
        var c = 
        $('#anv').removeClass('a n v').addClass($('#anv a.active').attr('rel'));
    });
    
    $('#anv a.active').each(function () {
        $('#anv').addClass($(this).attr('rel'));
    });
    
    //posli znamemu
    $('#send2friendBtn').click(function () {
        send2friend();
        return false;
    });
    
    //tlac stranky
    if ($('body').is('.printOnLoad')) {
        window.print();
    }
    
    //slider na cenove vyhladavanie
    var pricerangeMax = 500;
    $('#searchPriceRange').slider({
        range: true,
        min: 0,
        max: pricerangeMax,
        step: 10,
        values: [$('#searchPriceMin').val() || 0, $('#searchPriceMax').val() || pricerangeMax],
        slide: function(event, ui) {
            searchPriceRangeDisplay(ui.values[0], ui.values[1], pricerangeMax);
            $('#searchPriceMin').val(ui.values[0]);
            $('#searchPriceMax').val(ui.values[1] >= pricerangeMax ? '' : ui.values[1]);
        }
    }).find('.ui-slider-handle').each(function (idx, el) {
        $(this).addClass('handle-' + idx);
    });
    searchPriceRangeDisplay($("#searchPriceRange").slider("values", 0), $("#searchPriceRange").slider("values", 1), pricerangeMax);
});

function searchPriceRangeDisplay(v1, v2, m) {
    var t = '';
    if (v1 == 0 && v2 < m) {
        t = 'do ' + v2 + ' EUR';
    } else if (v1 == 0 && v2 == m) {
        t = 'výber ceny pohybom šípok';
    } else if (v1 > 0 && v2 < m) {
        t = v1 + ' - ' + v2 + ' EUR';
    } else if (v1 > 0 && v2 == m) {
        t = 'od ' + v1 + ' EUR';
    }
    $("#searchPriceRangeAmount").text(t);
}

function send2friend() {
    if (!$('#send2friend').length) {
        var stf = $('<div>').attr('id','send2friend');
        $('#pageFuncs').before(stf);
        stf.empty().addClass('loadingContainer');
        stf.load(LANGROOT + '/send-page/',function () {
            stf.removeClass('loadingContainer');
            $('#sendToFriendForm').submit(function () {
                //zmena value buttonu
                var v1 = $('#sendToFriendFormSubmit').val();
                var v2 = $('#sendToFriendFormSubmit').attr('rel');
                $('#sendToFriendFormSubmit').attr('disabled','disabled').val(v2).attr('rel',v1);
                $('#sendToFriendFormUrl').val(window.location.href);
                $.post(this.action,$(this).serialize(),function (response) {
                    //response ako JSON
                    eval('var r = '+response+';');
                    //odstranenie reportov
                    $('div.report',stf).remove();
                    if (r.status == 'OK') {
                        stf.prepend('<div class="report report-INFO"><span class="ico ico-INFO">&nbsp;</span>'+r.message+'</div>');
                        $('#sendToFriendFormSubmit').val(v1).attr('rel',v2);
                        window.setTimeout(function () {
                            stf.fadeOut(function () {
                                $(this).remove();
                            });
                        },1500);
                    } else {
                        stf.prepend('<div class="report report-ERROR"><span class="ico ico-ERROR">&nbsp;</span>'+r.message+'</div>');
                        $('#sendToFriendFormSubmit').removeAttr('disabled').val(v1).attr('rel',v2);
                    }
                });
                return false;
            });
        });
    } else {
        $('#send2friend').fadeOut(function () {
            $(this).remove();
        });
    }
}
