$(function(){
    $('a.zoom').fancybox();
    placeHolders('#searchQuery');
    reviewsChange('.review-logos', '.reviews');
    $('.slideshow .list').carousel('.slideshow .prev', '.slideshow .next');
    $('a.details-js').click(function(){
        $(this).parent().hide().next('div.details-js').slideDown();
        return false;
    });
    
    setM2();
});


function setM2(){
	var l = $('.m1 .a').offset().left;
	var w = $('.m1 .a').width();
	var left = $('#headerTop').offset().left;
	var margin = l+w-left-85;
	
	$('.m2 .pos').css('margin-left', margin).show();
}

function placeHolders(selector){
    $(selector).each(function(){
        var input = $(this);
        var holder = input.attr('alt');
        input.val(holder).focus(function(){
            if (input.val() == holder) input.val('');
        }).blur(function(){
            if (input.val() == '') input.val(holder);
        });
    });
}


function reviewsChange(logos, reviews){
    var reviews = $('.i', reviews);
    $('a', logos).each(function(i){
        var link = $(this);
        var index = i;
        var review = reviews.eq(i);
        link.click(function(){
            reviews.not(review).fadeOut('normal', function(){
                $(this).removeClass('visible');
            });
            review.addClass('ibg').fadeIn('normal', function(){
                $(this).addClass('visible');
            });
            return false;
        });
    });
}


// Infinite Carousel by Stephane Roucheray
$.fn.carousel = function(previous, next) {
    var sliderList = $(this).children()[0];

    if (sliderList) {
        var increment = $(sliderList).children().outerWidth('true'),
        elmnts = $(sliderList).children(),
        numElmts = elmnts.length,
        sizeFirstElmnt = increment,
        shownInViewport = Math.ceil($(this).width() / sizeFirstElmnt),
        firstElementOnViewPort = 1,
        isAnimating = false;

        for (i = 0; i <= shownInViewport; i++) {
            $(sliderList).css('width', (numElmts + shownInViewport) * increment + increment + 'px');
            $(sliderList).append($(elmnts[i]).clone());
        }

        var autoAnimate = setInterval(function(){
            if (!isAnimating) {
                if (firstElementOnViewPort > numElmts) {
                    firstElementOnViewPort = 2;
                    $(sliderList).css('left', '0');
                } else {
                    firstElementOnViewPort++;
                }
                $(sliderList).animate({
                    left: '-=' + increment,
                    y: 0,
                    queue: true
                }, 1500, 'easeOutQuint', function(){
                    isAnimating = false;
                });
                isAnimating = true;
            }
        }, 3000);

        $(previous).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort == 1) {
                    $(sliderList).css('left', '-' + numElmts * sizeFirstElmnt + 'px');
                    firstElementOnViewPort = numElmts;
                } else {
                    firstElementOnViewPort--;
                }
                $(sliderList).animate({
                    left: '+=' + increment,
                    y: 0,
                    queue: true
                }, 1200, 'easeOutQuint', function(){
                    isAnimating = false;
                });
                isAnimating = true;
            }
            return false;
        });

        $(next).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort > numElmts) {
                    firstElementOnViewPort = 2;
                    $(sliderList).css('left', '0');
                } else {
                    firstElementOnViewPort++;
                }
                $(sliderList).animate({
                    left: '-=' + increment,
                    y: 0,
                    queue: true
                }, 1200, 'easeOutQuint', function(){
                    isAnimating = false;
                });
                isAnimating = true;
            }
            return false;
        });
    }
};
