$(document).ready(function() {

    // home slideshow
    var autoslide;

    function getW($obj) {
        return $obj.outerWidth() +  parseInt($obj.css("margin-left"), 10);
    }

	$("#image-slides li").each(function(i) {
		//$(this).css({left: i * 100 + "%"});
        $(this).css({position: 'absolute', left: 0});
        if (i > 0)
            $(this).hide();
	});

    function startSlide() {
        var index = $("#name-slides li.active").index("#name-slides li");
        var next = index + 1;
        if (next == 5) next = 0;
		$("#name-slides li a").eq(next).trigger('go');
    }

	autoslide = setTimeout(function() {
        startSlide();
	}, 2500);
    
    /*
    // Go to the slide on click
    $("#name-slides li a").click(function() {
        $(this).trigger('go');
        return false;
    });
    */

	$("#name-slides li a").bind('go', function() {
        var $parent = $(this).parent();
		var index = $(this).index("#name-slides li a");
		var prevIndex = $("#name-slides li.active").index("#name-slides li");
		var differenceW = 0;

		if ($parent.hasClass("active"))
			return false;

		var module = index > prevIndex ? 1 : -1;

		differenceW = module * getW($("#name-slides li").eq(prevIndex)) / 2 + module * getW($("#name-slides li").eq(index)) / 2;

		for (var i = prevIndex + module; module > 0 ? i < index : i > index; module > 0 ? i++ : i--) {
			differenceW += module * getW($("#name-slides li").eq(i));
		}

		$("#arrow").animate({left: "+=" + differenceW});
        
        // Fade
        $("#image-slides ul li").each(function(i) {
            $(this).removeClass('active');
            if (i == index)
                $(this).css({'z-index': 1}).addClass('active').fadeIn(300, function() {
                    $("#image-slides ul li").each(function(j) {
                        if (!$(this).hasClass('active')) {
                            $(this).hide();
                            console.log('hide');
                        }
                    });
                });
            else 
                $(this).css({'z-index': 0});
        });
		$("#name-slides li").removeClass("active");
		$parent.addClass("active");

        clearTimeout(autoslide);
        autoslide = setTimeout(function() {
            startSlide();
        }, 5000);

        //return false;
	});
    

    // animate Main menu
    $(".menu li").hover(function() {
		if ($(this).find("ul").length)
			$(this).addClass("active");
		if ($(this).parents("ul").length == 1)
			$(this).find(">ul").hide().show();
		if ($(this).parents("ul").length > 1)
			$(this).find(">ul").hide().show();
	}, function() {
		$(this).removeClass("active").find(">ul").hide();
	});
	$(".menu>li").hover(function() {
	}, function() {
		$("#menu li ul").hide();
	});

    // animate Transaction card
    var speedTransaction = 200;
    $(".list-transactions .wrap-transaction").live({
        mouseenter: function() {
            $(this).stop().css({position: 'absolute', 'z-index': 100}).animate({scale: 1.6}, speedTransaction);
        },
        mouseleave: function() {
            $(this).stop().animate({scale: 1}, speedTransaction, function() {
                $(this).css({position: 'static', 'z-index': 0});
            });
        }
    });

    // animate inner slide
	var fadeTime = 500;
	var oneIntervalTime = 1000;
	var intervalSlideTime = 6000;

    function startInnerSlide() {
    	var index = $("#head-right-bg li.active").index("#head-right-bg li");
    	var length = $("#head-right-bg li").size();
    	var next = index + 1;

    	if (next == length) next = 0;

    	$slides = $("#head-right-bg li").eq(next).show();
    	for ( var i = 0; i < length; i++) {
    		(function(e) {
        		setTimeout(function() {
            		$("#head-right-bg li").eq(index).find(".slide").eq(e).animate({opacity : 0}, fadeTime);
        		}, oneIntervalTime * (e + 1));
    		}(i));
		};

    	setTimeout(function() {
    		$("#head-right-bg li.active").removeClass("active");
    		$("#head-right-bg li .slide").removeAttr("style");
    		$("#head-right-bg li").eq(next).addClass("active").removeAttr("style");
		}, (length + 1) * oneIntervalTime + fadeTime * 2);
    }

    if ($("#head-right-bg").length) {
    	startInnerSlide();

        setInterval(function() {
    		startInnerSlide();
    	}, intervalSlideTime);
    }
    
    // Filter transactions
    var $transactions = $('.list-transactions').clone();
    
    function filter_transactions(filter, noquicksand) {
        var $new_trans = $('<ul>');
        $('li', $transactions).each(function(i, li) {
            if (filter(li))
            $new_trans.append($(li).clone());
        });

        if (noquicksand) {
            $('.list-transactions li').remove();
            $('.list-transactions').append($('li', $new_trans)); 
        } else {
            $('.list-transactions').quicksand($('li', $new_trans));   
        }
    }
    
    $('.transaction-type a').click(function() {
        var a = this;
        if (this.innerHTML == 'All') {
            $('.list-transactions li').replaceWith($transactions.clone());
            return;
        }
        
        filter_transactions(function(trans) {
            return $(trans).attr('data-type') == a.innerHTML;
        });
    });
    
});
