if (typeof(ArthurBrett) == 'undefined') {
	ArthurBrett	= {};
	ArthurBrett.UI	= {};
}

ArthurBrett.UI.Global	= function()
{
	var init	= function() {
		$('body').addClass('js');
		
		init_products();
		search_form();
		
	};
	
	var init_products = function() {
		
		// Product detail
		if ($('div.product-detail').length) {
			$('ul.product-images a').click(function(e){
				e.preventDefault();
				var self = $(this);
				var src = self.attr('href');
				$('div.product-image img').removeAttr('height').attr('src', src);
			});
		}
		
		// Product listing
		if ($('ul.products').length) {
			$('ul.products li').click(function(e){
				e.preventDefault();
				var self = $(this);
				var link = self.find('a').attr('href')+ '/simple:true';
				
				$.get(link, function(result){
					if (result) {
						open_info_window(result);
					}
				});
			});
			
		}
	};
	
	
	var open_info_window = function(contents) {
		close_info_window();
		$('body').append('<div id="info-window"><div class="inner"></div></div>');
		var iwin = $('#info-window');
		iwin.find('.inner').append(contents).center().end().hide().fadeIn('fast');
		iwin.click(function(e){
			
			if ($(e.target).is('#info-window')) {
				e.preventDefault();
				close_info_window();
			}
		});
	};
	
	var close_info_window = function() {
		$('#info-window').fadeOut('fast', function(){
			$(this).remove();
		});
	};
	
	var search_form = function() {
		$('#q').focus(function(){
			if (this.value == this.defaultValue) this.value = '';
		}).blur(function(){
			if (this.value == '') this.value = this.defaultValue;
		});
	};
	
	return {
		init: init
	};
	
}();


jQuery(function($) { ArthurBrett.UI.Global.init(); });

jQuery.fn.center = function (absolute) {
    return this.each(function () {
        var t = jQuery(this);

        t.css({
            position:    absolute ? 'absolute' : 'fixed', 
            left:        '50%', 
            top:        '50%', 
            zIndex:        '101'
        }).css({
            marginLeft:    '-' + (t.outerWidth() / 2) + 'px', 
            marginTop:    '-' + (t.outerHeight() / 2) + 'px'
        });

        if (absolute) {
            t.css({
                marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
                marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }

		if (t.offset().top < 0) {
			t.css({
				top: 0,
				marginTop: '10px'
			});
		}
    });
};

