function reseter() {
    $('.range').children('.icol').animate({top:0}, 500, 'easeOutSine');
    //$('.range').children('.icol').queue("fx", []);
    overRangeElem = false;
    clearTimeout(timer);
}
var overRangeElem = null;
var timer = false;


$(function() {

    $(".product-desc:odd").each(function(){
        $(this).addClass("product-desc-even");    
    });
    
    var animated = null;
    
    (document.all) ? $(".mask").css('height', document.documentElement.clientHeight+"px") : null;
     
    /** main menu */   
    $("#menu img").bind("mouseover", function() {
        //$(this).attr({origSrc: $(this).attr("src"), src: $(this).attr("src").replace(/_off/, "_on")});
        var elSrc = $(this).attr("src"), oldElSrc;
        if ($(this).attr("src").match(/blank.gif/)) {
            elSrc = $(this).css("filter");
        }
        
        if (!elSrc.match(/_on_/)) {
            oldElSrc = elSrc;
            elSrc = elSrc.replace(/_off/, "_on")
            if ($(this).attr("src").match(/blank.gif/)) {
                this.runtimeStyle.filter = elSrc;
            } else {
                $(this).attr("src", elSrc);
            }
        }
        $(this).attr("oldSrc", oldElSrc);
    });
    $("#menu img").bind("mouseout", function() {
        //$(this).attr("src", $(this).attr("origSrc"));

        if ($(this).attr("oldSrc")) 
			{
	            if ($(this).attr("src").match(/blank.gif/)) {
	                this.runtimeStyle.filter = $(this).attr("oldSrc");
	            } else {
	                $(this).attr("src", $(this).attr("oldSrc"));
	            }
			}
		});
	/** language dropdown */
    $('#lang').bind('click', function() {
        $(this).children('ul').toggle();
    });

    
    $('.range').mouseover(function() {
	
	    clearTimeout(timer);
        
    	if (! overRangeElem || $(this).attr('id') != overRangeElem.attr('id')) {
            $('.range').not(this).children('.icol').animate({top:0}, 500, 'easeOutSine');

            offsets = Array();
            offsets['c1'] = -228;
            offsets['c2'] = -230;
            offsets['c3'] = -227;
            offsets['c4'] = -212;

            $(this).children('.icol').animate({top:offsets[$(this).attr('id')]}, 500, 'easeOutSine');
        }

        overRangeElem = $(this);
        return false;
    });
    
    $('.range').mouseout(function() {
        $('.range').children('.icol').queue("fx", []);
        clearTimeout(timer);
        return false;
    });
    
    $("body").mouseover(function() {
        $('.range').children('.icol').each(function() {
            if (this.offsetTop < 0) {
                timer = setTimeout("reseter()", 200);
            }
        });
    });
    
    var overDrawerElem = null;

    $('.drawer-small').mouseover(function() {
        if (! overDrawerElem || $(this).attr('id') != overDrawerElem.attr('id')) {
            
            // var fixHeight = $(this).parent('.drawer-small').height();
            // $(this).parent('.drawer-small').css('height', fixHeight);
            
            $(this).animate( {height:68}, 250, 'easeOutSine', function() {
                /*$(this).children('.info').fadeIn(500);*/
                $(this).children('.info').show();
            });
            $('.drawer-small').not(this).animate( {height:20}, 400, 'easeOutSine', function() {
                $(this).children('.info').hide();
            });
        }
        overDrawerElem=$(this);
        return false;
    });
    
    $('.drawer-small').mouseout(function(){
        $('.drawer-small').queue("fx", []);
        return false;
    });
    
    $('.drawer-small:first').css('height', 68).children('.info').show();

    /* VV stuff */
    
    ///add groups click handler
    $(".vv-groups div").click(function() {
        //hide all
        $(".vv-group").hide();
        
        $(".vv-groups div").removeClass('active');
        $(this).addClass('active');
        
        //show clicked group
        //IE8.js adds a className, so we have to split
        $("#" + $(this).attr("class").split(" ")[0]).show();
        //show surrounding stuff
        $("#vv-selected").html($(this).html());
        $("#vv-desc").show();
        $(".vv-line").show();
        $("#vv-next").show();
    });

    var mediascope_settings = {
        size: {
            width: 235,
            height: 235
        },
        zoomedImages: {
		    source: function(thumbnail, index) {
		        var pictureFilename = $(thumbnail).attr('src').replace("235\/235", "780\/560");
		        var altText = $(thumbnail).attr('alt');
                return { src: pictureFilename, alt: altText };
		    },
			size: {
			    width: 780,
			    height: 560
			},
    		icons: {
    			previous: '/Content.Node/_images/img_switch_arrow_l.gif',
    			next: '/Content.Node/_images/img_switch_arrow_r.gif',
    			close: '/Content.Node/_images/closeicon.jpg',
    			'background-top': '/Content.Node/_images/topbg.jpg',
    			'background-bottom': '/Content.Node/_images/img_switch_bg_small.gif'
    		}
		},
		icons: {
			previous: '/Content.Node/_images/img_switch_arrow_l.gif',
			next: '/Content.Node/_images/img_switch_arrow_r.gif',
			close: '/Content.Node/_images/closeicon.jpg',
			'background-top': '/Content.Node/_images/topbg.jpg',
			'background-bottom': '/Content.Node/_images/img_switch_bg_small.gif'
		}
	}

    $('.mediascope').mediascope(mediascope_settings);

    $('.brand-menu-arrow').filter('.l').click(getNext);
    $('.brand-menu-arrow').filter('.r').click(getNext);
    startLeft = $('.brand-menu-visible').css('left');

    //brand menu mouseovers
    $(".brand-menu-visible a").hover(function() {
        $(this).attr("oldBG", $(this).css("background-image"));
        $(this).css("background-image", $(this).css("background-image").replace(/_off-/, "_on-"));
    }, function() {
        $(this).css("background-image", $(this).attr("oldBG"));
    });
    
    //dl box word breaks
    $(".dl-box a span.bold").each(function() {
        var word = $(this).html();
        if (word.length > 15 && word.indexOf("-") > -1) {
            //alert(":: " + $(this).html());
            $(this).html($(this).html().replace(/-/, "-<br />"));
        }
    });

});

/**
 * method stub for javascript links
 * todo: improve functionality
 */
function openUrl(url) {
    document.location.href = url;
}


function changeSrc(elem, url) {
	if (elem.src) {
		elem.src = url;
	} else {
		elem.style.backgroundImage = "url("+url+")";
	}
}

function mouseover(elem) {
    elem.origImg = $(elem).attr('src');

    // hover button
    if (elem.hoverImg) { 
        $(elem).attr('src', elem.hoverImg);
    }
    
    // change background
    if (elem.bgImg) {
        $('#' + elem.bgId).attr('src', elem.bgImg);
    }
}

function mouseout(elem) {
    $(elem).attr('src', elem.origImg);
}


var startLeft = 0;
var inAnimation = false;

function getNext() {
	if (! inAnimation) {
		var left = $('.brand-menu-visible').css('left');
		inAnimation = true;
		if ($(this).hasClass('r')) {
			$('.brand-menu-visible').animate({left:(parseInt(left) - 150) + 'px'}, function() {
				var brand = $('.brand-menu-visible a:first').clone();
				$('.brand-menu-visible a:first').remove();
				$('.brand-menu-visible').css('left', startLeft);
				$('.brand-menu-visible').append(brand);
				inAnimation = false;
			});
		} else {
			$('.brand-menu-visible').animate({left:(parseInt(left) + 150) + 'px'}, function() {
				var brand = $('.brand-menu-visible a:last').clone();
				$('.brand-menu-visible a:last').remove();
				$('.brand-menu-visible').css('left', startLeft);
				$('.brand-menu-visible').prepend(brand);
				inAnimation = false;
			});
		}
	}
	return false;
}


function backberater() {
    document.location.href = "http://backberater.backaldrin.com/search/variant?variant=" + sLangCode;
	//openProductAdvisor();
}


function openProductAdvisor() {
	if ($('#appadv-mask').length == 0) {
		var html = '<div id="appadv-mask"></div>';
		html += '<div id="appadv-cont"><div id="appadv-frame-cont"><div onclick="closeProductAdvisor();" id="appadv-closer"></div>';
		html += '<iframe frameborder="0" src="http://backberater.backaldrin.com/search#byCategory" id="appadv-frame"></iframe></div></div>'
	
		$("body").append(html);
	}

	$('#appadv-cont').fadeIn();
	$('#appadv-mask').fadeTo(500, .2).show();	
	$('body').addClass('overflow');
	$('html').css('overflow','hidden');
}

function closeProductAdvisor() {
	$('#appadv-cont').hide();
	$('#appadv-mask').hide();	
	$('body').removeClass('overflow');
	$('html').css('overflow','auto');
}
