function initBannerCarousel() {
	$('#slider1').tinycarousel({interval: 10, pager: true});
}

function initProductCarousel() {
	$('#product_slider').tinycarousel({ display: 5 }, true);
	$('#detailed_product_slider').tinycarousel({ display: 5 }, true);
}

function showArrow(element) {
	$('.down-arrow').each(function(i, e){
			$(e).hide();
		});
	element.parent().prev('.down-arrow').show();
}

function make_visit_link(rootcat, listtype, element, extra_params) {
	var cat_assoc = {
		'men': '2',
		'women': '1',
		'boys': '5',
		'girls': '4'
	}
	, gender_assoc = {
		'f': 'women',
		'm': 'men'
	};
      
	if (listtype == 'new') {
		var search_url = "/shop/search/?"
			, cat = cat_assoc[rootcat]
                        , parameters = $.param($.extend({'category': cat, 'sort_by': '-date_added'}, extra_params))
			, link = "<a href=\"" + search_url + parameters + "\"&nbsp;</a>";
		element.html(link);
	}
	if (listtype == 'group') {
		var base_url = "/groups/?"
			, cat = gender_assoc[rootcat]
	                , parameters = $.param($.extend({'slug': cat}, extra_params))
			, link = "<a href=\"" + base_url + parameters + "\"&nbsp;</a>";
		element.html(link);
	}
	if (listtype == 'popular') {
		var search_url = "/like/lists?"
			, cat = cat_assoc[rootcat]
	                , parameters = $.param($.extend({'category': rootcat}, extra_params))
			, link = "<a href=\"" + search_url + parameters + "\"&nbsp;</a>";
		element.html(link);
	}
}

function updatePopularProducts(rootcat, shopwindow) {
	visit_link = make_visit_link(rootcat, 'popular', $('#visit-section'));
	data = $.get('/ajax/quicklist/popular/', {
			rootcat: rootcat
		}, function(data, textstatus, xmlreq) {
			shopwindow.html(data);
			initProductCarousel();
			FB.XFBML.parse(document.getElementById('detailed_product_slider'));
		})
}

function updateProducts(rootcat, listtype, detailed, shopwindow) {
	visit_link = make_visit_link(rootcat, listtype, $('#visit-section'));
	data = $.get('/ajax/quicklist/products/', {
			rootcat: rootcat,
			listtype: listtype,
			detailed: detailed
		}, function(data, textstatus, xmlreq) {
			shopwindow.html(data);
			initProductCarousel();
			FB.XFBML.parse(document.getElementById('detailed_product_slider'));
		})
}

function updateProductsWithVideos(rootcat, listtype, detailed, shopwindow) {
        $('#visit-section').html('<a href="shop/category/' + rootcat + '/?videos=1"/>');
	data = $.get('/ajax/quicklist/products/', {
			rootcat: rootcat,
			listtype: listtype,
                        detailed: detailed, 
                        only_videos: 'true'
		}, function(data, textstatus, xmlreq) {
			shopwindow.html(data);
			initProductCarousel();
			FB.XFBML.parse(document.getElementById('detailed_product_slider'));
		})
}


function updateLikes(rootcat, listtype, shopwindow) {
	data = $.get('/ajax/quicklist/likes/', {
			rootcat: rootcat,
			listtype: listtype
		}, function(data, textstatus, xmlreq) {
			shopwindow.html(data);
			initProductCarousel();
			FB.XFBML.parse(document.getElementById('detailed_product_slider'));
		})
}

function updateGroups(gender, shopwindow) {
	visit_link = make_visit_link(gender, 'group', $('#visit-section'));
	data = $.get('/ajax/quicklist/groups/', {
			gender: gender
		}, function(data, textstatus, xmlreq) {
			shopwindow.html(data);
			initProductCarousel();
		})
}

function showSubSelect(subselect) {
	$(subselect).show();
}

function selectSubSelect(subselect) {
	$(subselect).hide();
	
}

function updateBar(element) {
	
	$('#shinybar').children('ul').children('li').each(function(i, e){
			if (e != element) {
				$(e).removeClass('selected')
					}
		});

	$('.sub-select').each(function(i, e){
			if (!$(e).parent().hasClass('selected')) {
				$(e).hide();
			}
		});

	if ( !$(element).hasClass('selected')) {
		$(element).addClass('selected');
		showSubSelect($(element).children('.sub-select'));
	}
	else {
		if ($(element).children('.sub-select').css('display') == 'none') {
			$(element).children('.sub-select').show();
		} else {
			$(element).children('.sub-select').hide();
		}
	}
}

