var sliderWidth;
var sliderItems;

function resetSlider()
{
    sliderWidth = $('#content_slider_ul_container ul li').width()+30;
    sliderItems =  $('#content_slider_ul_container ul li').not('.folderHolder').size();
    resetSliderArrows();
}

function resetSliderArrows()
{
    if($('#content_slider_ul_container').position().left >= 0) {
        $('#content_slider_left').addClass('content_slider_inactive');
        $('#content_slider_right').removeClass('content_slider_inactive');
    } else {
        $('#content_slider_left').removeClass('content_slider_inactive');
    }

    if($('#content_slider_ul_container').position().left <= -(sliderWidth*Math.ceil(((sliderItems/4)-1)*4))) {
        $('#content_slider_right').addClass('content_slider_inactive');
		
		if($('#content_slider_ul_container').position().left != 0) {
			$('#content_slider_left').removeClass('content_slider_inactive');
		}
    } else {
        $('#content_slider_right').removeClass('content_slider_inactive');
    }
	
	$("#content_slider_ul_container a[rel~=external]").each(function(i){
		this.target="_blank";
	});	
}

function hideHumanValidationFields() {
	$('#hvalidation').prev().hide();
}

jQuery(window).load(function() {
	$('img[delayedsrc]').each(function(i){
		 $(this).attr('src', $(this).attr('delayedsrc'));
		 $(this).removeAttr('delayedsrc');
	});
});

jQuery(document).ready(function() {
	$('.buttonContent:hidden img').each(function(i){
		 $(this).attr('delayedsrc', $(this).attr('src'));
		 $(this).removeAttr('src');
	});
	
	$("a.ajax").fancybox({
		'scrolling' : 'no',
		'type': 'iframe',
		'width' : 260,
		'height' : 450,
		'onComplete': hideHumanValidationFields
	});	

	hideHumanValidationFields();

	$("label.inline").each(function(i){
		var fieldId = '#' + $(this).attr('for');

		if(!$(fieldId).val()) {
			$(fieldId).val($(this).text());
			$(fieldId).addClass('inline-label');
		}
		
		$(fieldId).addClass('inline-helper');
		$(this).hide();
	});

	$('.inline-label').live('focus click', function() {
        $(this).cursorPosition(0);
		$(this).removeClass('inline-label');
		$(this).addClass('inline-softlabel');
	});

	$('.inline-helper').live('keypress', function() {
		$(this).removeClass('inline-label');
		$(this).removeClass('inline-softlabel');

		var labelVal = $("label[for=" + $(this).attr('id') + "]").text();
		if($(this).val() == labelVal) {
			$(this).val(null);
		}
	});

	$('.inline-helper').live('focusout', function() {
		var labelVal = $("label[for=" + $(this).attr('id') + "]").text();
		$(this).removeClass('inline-softlabel');

		if(!$(this).val()) {
			$(this).val(labelVal);
			$(this).addClass('inline-label');
		}

		if($(this).val() == labelVal) {
			$(this).addClass('inline-label');
		}
	});

	if ($('#content_slider_ul_container').length) {
        if($('#content_slider_ul_container ul').length) {
            resetSlider();
        }
		$('#content_slider_left').click(function() {
			if (!$('#content_slider_ul_container').is(":animated") && !$('#content_slider_left').hasClass('content_slider_inactive')) {
				$('#content_slider_ul_container').animate({
					left: '+='+(sliderWidth)*4+'px'
				},400, function() {
					resetSliderArrows();
				});
			}
		});

		$('#content_slider_right').click(function() {
			if (!$('#content_slider_ul_container').is(":animated") && !$('#content_slider_right').hasClass('content_slider_inactive')) {
				$('#content_slider_ul_container').animate({
					left: '-='+(sliderWidth)*4+'px'
				},400, function() {
					resetSliderArrows();
				});
			}
		});
	}

	$(".content_h3tab").each(function(i){
		$(this).find('h3:first').addClass('roundLeft');
		$(this).find('h3:last').addClass('roundRight');
	});

	$('.content_h3tab h3.button').click(function(){
		$(this).parent().find('h3').removeClass('active');
		$(this).parent().parent().find('.buttonContent').hide();
		var parts = $(this).attr('id').split("_");

		$(this).parent().parent().find('#'+parts[0]+'_content').show();
		$(this).addClass('active');
		return false;
	});

	$('ul.folderBlock li:nth-child(5n)').addClass('last');
	$('ul.logoList li:nth-child(5n)').addClass('last');
	$('ul.productFeedArticleBlock li:nth-child(5n)').addClass('last');
	$('ul.discountBlockHorizontal li:nth-child(2n)').addClass('last');
	$('div.indexList.triple div:nth-child(3n)').after('<div style="clear: both;"></div>');

	$('#content_breadcrumbs_links a:last').wrap('<span>').contents().unwrap();

	$("a[rel~=external]").each(function(i){
		this.target="_blank";
	});

	if (!$.browser.mozilla && !$.browser.msie && !$.browser.webkit && !$.browser.opera)
	{
		$("a#bookmarkLink").parent().remove();
	}

    $("a#bookmarkLink").click(function(){
		var bookmarkUrl = this.href;
		var bookmarkTitle = this.title;

		if ($.browser.mozilla)
		{
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
		}
		else if($.browser.msie || $.browser.webkit)
		{
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
		}
		else if($.browser.opera )
		{
			$(this).attr("href",bookmarkUrl);
			$(this).attr("title",bookmarkTitle);
			$(this).attr("rel","sidebar");
			$(this).click();
		}
		return false;
    });
});
