/**
 * Namespace "Common"
 * @class Common
 * @desc Common of Base
 */
Base.Common = function($){
	/**
	 * Adds styles that can't be set with CSS due to limited support
	 */
	function ieAddStyles()
	{
		$('#stufftabs .view1 .item:nth-child(5)').css('background', 'none'); // StuffBrowser listview 2 dots
	}
	
	/**
	 * Recommend box is on personal home
	 */
	function initRecommendbox()
	{
		if( $('#recommendbox').length > 0 ){
			$('#recommendbox .carousel-holder').jCarouselLite({
				btnPrev: $('#recommendbox .pager .prev'),
				btnNext: $('#recommendbox .pager .next'),
	      visible: 1,
				auto: 3000,
				speed: 400
	    });
			$('#recommendbox .carousel-holder').css('visibility', 'visible');
		}
	}
	
	/**
	 * Add stuff is the blue tab on the right of the screen
	 */
	function initAddStuff()
	{
		$('#add-stuff-toggler').bind('click.addstuff', function(){
			var add = $('#add-stuff');
			var fadeSpeed = 100;
			
			if( !this.addActive ){
				if( typeof this.addActive == 'undefined' ) // first time, set baseWidth
					this.baseWidth = $(this).width();
				
				this.addActive = true;
				$.infoWindow._overlay('show', fadeSpeed, false); // show overlay, don't close on click
				setTimeout(function(){
					add.animate({width: add.children().width()}, 500);
				}, fadeSpeed);
			}
			else {
				this.addActive = false;
				add.animate({width: this.baseWidth}, 250, 'swing', function(){
					$.infoWindow._overlay('hide', fadeSpeed);
				});
			}
			$(this).blur(); // ie7
			return false;
		});
		
		$('#add-stuff-close').bind('click', function(){
			$('#add-stuff-toggler').triggerHandler('click');
			$(this).blur(); // ie7
			return false;
		});
	}
	
	/**
	 * Binds functions to slide buttons (ie. on the product page / display)
	 * Call this function separate for each group of slide buttons. Also if you just want to add one.
	 * 
	 * @param {String} sel
	 * @param {Object} opts
	 * 
	 * DEPRECATED (for productpage)
	 */
	function initSlideSelectors(sel, opts)
	{
		var defaults = {
			toggleClass : true,
			offsetLeft: 0,
			selOffsetLeft: 0 // = selected offset left
		};
		var s = $.extend(defaults, opts);
		var leftpos = [];
		
		$(sel).each(function(i, elem) {
			elem = $(elem);
			var startleft = parseInt(elem.css('left').replace(/px/, '')) || 0;
			leftpos[i] = [startleft - (elem.width() - s.offsetLeft), startleft - (elem.width() - s.selOffsetLeft)]; // store left position for selected and non selected items
			
			elem.bind('click.StuffBrowser.View', function(){
				//$('#stuff-browser > .select-view.selected')
				$(sel +':not(:eq('+ i +'))').removeClass('selected').triggerHandler('mouseleave');
				
				if(s.toggleClass){
					elem.toggleClass('selected');
				}
				else
					elem.addClass('selected');
				
				return false;
			})
			
			.bind('mouseenter.slideSelector', function(){
				elem.stop().animate({left: startleft}, 150);
			})
			
			.bind('mouseleave.slideSelector', function(){
				elem.stop().animate({left: elem.hasClass('selected')? leftpos[i][0]: leftpos[i][1]}, 300);
			});
			
			// on startup
			setTimeout(function(){
				elem.animate({left: elem.hasClass('selected')? leftpos[i][0]: leftpos[i][1]}, 350);
			}, 200 + i*100);
		});
	}
	
	/**
	 * The product image carousel appears on several pages
	 * It's the wide carousel with small images
	 */
	function initProductImgCarousel()
	{
		var carousels = $('.prod-img-carousel .carousel');
		carousels.each(function(){
			elem = $(this);
			
			// less items available then shown?
			if( elem.children('ul').children('li').length <= parseInt(elem.attr('items')) )
				return false;
			
			elem.parent().children('a').show();
			var id = '#'+ elem.parent().attr('id');
			var c = ( elem.attr('circular') === 'false' || elem.attr('circular') === 'null' )? false: true;
			
			elem.jCarouselLite({
				btnPrev: id +' .prev',
				btnNext: id +' .next',
	      visible: parseInt(elem.attr('items')),
				circular: c,
				scroll: parseInt(elem.attr('scroll')) || 1
			});
		});
		
		
		// Determine what kind of carousel this is to continue
		//
		if( carousels.find('a:first').attr('href') == '#' ){ // lightbox carousel
			// carousel on product page has "set default" link on it
			var product_page = location.href.indexOf('view-product.jspa') == -1? false: true;
		
			carousels.find('a').bind('click.carousel', function(){
				var img_data = $(this).attr('data').split('-');
				var html = $('<div/>');
				
				html.append('<div class="close"></div>');
				html.append('<img src="'+ Base.paths.product_image +'?id='+ img_data[0] +'-'+ img_data[1] +'&w=274&h=274" alt="" />');
				
				if( Base.admin_user && product_page )
					html.append('<p class="set-default"><a href="#">set as default</a></p>');
				
				infoWindow(html.html(), {contentClass: 'product-image-lightbox'}, function(){
					if( Base.admin_user && product_page ){
						$(this).children('.set-default').children('a').bind('click', function(){
							Base.Ajax.updateDefaultProductAttachment(img_data[1]);
							return false;
						});
					}
				});
				return false;
			});
		}
	}
	
	/**
	 * Init JS for the the product page
	 * Maybe put this in it's own namespace later (Base.Product), depends on how more JS we need
	 */
	function initProductPage()
	{
		// Selector buttons
		$('#product-ownership .ownership').bind('click.ownership', function(){
			var elem = $(this);
			Base.Ajax.toggleOwnership(elem);
			elem.blur();
			return false;
		});
		
		// Upload attachment
		$('#add-picture').bind('click', function(){
			infoWindow($('#create-attachment-form').html(), {});
			return false;
		});
	}
	
	return {
		/**
		 * Initialize this Class
		 */
		init: function() {
			// General
			initAddStuff();
			initProductImgCarousel();
			
			// Personal home
			initRecommendbox();
			
			// Product
			initProductPage();
			
			// General
			//initSlideSelectors('#product-display > .ownership');
			
			if( Base.ieVersion ) ieAddStyles();
		}
	};
}(jQuery);

/* Initialize this class */
Base.register(Base.Common.init);
