window.tatamo.ids = {
    nav: '#navbar'
};
window.tatamo.classNames = {
    navActiveItem: 'hover'
};

(function($){

$.navBar = {};
$.navBar.o = [];
$.navBar.op = {};
$.navBar.defaults = {
    activeClassName: 'active'
};

$.fn.navBar = function( op ) {

    var s = this.serial = $.navBar.o.length;
    var o = $.extend( {}, $.navBar.defaults, op );
    $.navBar.o[ s ] = $.navBar.op = o;

    return this.each(function() {
        $( ' a', this).hover(
            function(){
                $(this).addClass( o.activeClassName );
            },
            function(){
                $(this).removeClass( o.activeClassName );
            }
        );
    });

};

})(jQuery);

$.autoWidth = {};
$.autoWidth.o = [];
$.autoWidth.op = {};
$.autoWidth.defaults = {
    minWidth: 160
};

$.fn.autoWidth = function( op ) {

    var s = this.serial = $.autoWidth.o.length;
    var o = $.extend( {}, $.autoWidth.defaults, op );
    $.autoWidth.o[ s ] = $.autoWidth.op = o;

	return this.each(function(){
		var $select = $(this);
		var currentwidth = $select.width();
		if ( currentwidth > o.minWidth ) {
			$select.data("origWidth", o.minWidth ).css("width", o.minWidth );
			var active = 0;
			$select.hover(
				function(){
					$(this).css("width", "auto");
				},
				function(){
					if ( !active ) {
						$(this).css("width", $(this).data("origWidth"));
					}
				}
			)
			.mousedown(function(){
					active = true;
			})
			.blur(function(){
				active = false;
				$(this).css("width", $(this).data("origWidth"));
			})
			.change(function(){
				active = false;
				$(this).css("width", $(this).data("origWidth"));
			});
		}
	});
};

$(document).ready(function(){
    $("#paypal-button select").autoWidth();
    if ( !$("#wrapper").hasClass("en") ) {
		var currTemplate = $.trim( $('body').attr('class') ),
			indexTemplates = [ 'template-homepage', 'template-homeknowledges', 'template-homeproduct' ];
		if ( $.inArray( currTemplate, indexTemplates ) == -1 ) {
			$.get( '/sidebar-latest-entries.html', function ( data ) {
				if ( currTemplate == 'template-blog' ) {
					data = data.replace( 'tatamo-blog\.png', 'tatamo-blog-last-posts\.png');
				}
				$('#sidebar div.widget-archives-lastentries').replaceWith( data );
				$('#sidebar div.widget-archives-lastentries').css('visibility','visible');
			});
		}
	}
});
$(window).load(function(){
    $( tatamo.ids.navbar ).navBar({ activeClassName: tatamo.classNames.navActiveItem });
    document.documentElement.className += ' js-loaded';
});

