﻿//------------------------------------
//	HOMEPAGE.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.3.2
//				global.js
//				cufon.js
//				gazz.js
//------------------------------------

//CUFON
Cufon.replace('#twitter p.loading');
Cufon.replace('#twitter_update_list li');
Cufon.replace('#news dd a', {
    hover: true
});






//BEGIN jQuery
$(function() {

  

    //TWITTER

    totalT = 0;

    $("#twitter").getTwitter({
        userName: "mDeals",
        numTweets: 5,
        loaded: function() {

            totalT = $('#twitter_update_list li').size();

            $('#twitter_update_list li:first').clone().appendTo('#twitter_update_list');

            //Apply cufon
            Cufon.refresh('#twitter_update_list li');

            //Do the animating!
            $('#twitter p.loading').animate({ opacity: 0 }, 400, 'easeInOutExpo', function() {
                $(this).remove();
                $('#twitter_update_list').animate({ top: 0 }, 400, 'easeInOutExpo', function() {
                    tweetCycle();
                });
            });
        }
    });

    currentY = 0;
    currentT = 1;

    var tweetTimer = {};

    $('#twitter').hover(function() {
        $.clearTimer(tweetTimer);
    }, function() {
        tweetCycle();
    });

    function tweetCycle() {
        tweetTimer = $.timer(3800, function() {
            $.clearTimer(tweetTimer);
            if (currentT == totalT + 1) {
                $('#twitter_update_list').css({ top: 0 });
                currentY = 0;
                currentT = 1;
            }
            currentY = currentY - 38;
            $('#twitter_update_list').animate({ top: currentY }, 400, 'easeInOutExpo');
            currentT = currentT + 1;
            tweetCycle();
        });
    }


    //PROMO SLIDER

    totalPromo = $('#promo_slider div').size();
    xPos = 0;
    currentPromo = 1;
    moose = true;
    autoMoose = true;
    promoDelay = 6000;
    easeIn = 'easeInExpo';
    easeOut = 'easeOutElastic';
    inSpeed = 400;
    outSpeed = 1000;

    $('#promo_nav .next a').click(function() {
        if (moose == true && totalPromo > currentPromo) {
            xPos = xPos - 838;
            currentPromo = currentPromo + 1;
            slideTo(xPos, 'right');
        } else if (moose == true) {
            currentPromo = 1;
            infiniteLoop('right');
        }
        return false;
    });

    $('#promo_nav .prev a').click(function() {
        if (moose == true && currentPromo > 1) {
            xPos = xPos + 838;
            currentPromo = currentPromo - 1;
            slideTo(xPos, 'left');
        } else if (moose == true) {
            currentPromo = totalPromo;
            infiniteLoop('left');
        }
        return false;
    });

    function infiniteLoop(dir) {

        moose = false;

        totalX = 838 - (838 * totalPromo);

        if (dir == 'left') {
            $('#promo_slider').prepend('<div class="promo_content">' + $('#promo_slider div.promo_content:last').html() + '</div>').css({ left: -838 }).animate({ left: 419 }, inSpeed, easeIn, function() {
                $(this).animate({ left: 0 }, outSpeed, easeOut, function() {
                    $('#promo_slider').css({ left: totalX });
                    $('#promo_slider div.promo_content:first').remove();
                    moose = true;
                })
            });
            xPos = totalX;
        } else if (dir == 'right') {
            $('#promo_slider').append('<div class="promo_content">' + $('#promo_slider div.promo_content:first').html() + '</div>').animate({ left: totalX - 419 }, inSpeed, easeIn, function() {
                $(this).animate({ left: totalX - 838 }, outSpeed, easeOut, function() {
                    $('#promo_slider').css({ left: 0 });
                    $('#promo_slider div.promo_content:last').remove();
                    moose = true;
                });
            });
            xPos = 0;
        }
    }

    $('#promo_panel').hover(function() {
        $.clearTimer(promoTimer);
    }, function() {
        autoMagical();
    });

    var promoTimer = {};

    function autoMagical() {
        promoTimer = $.timer(promoDelay, function() {
            $.clearTimer(promoTimer);
            $('#promo_nav .next a').click();
            autoMagical();
        });
    }

    autoMagical();

    function slideTo(newX, dir) {
        moose = false;
        if (dir == 'left') {
            halfX = newX - 419;
        } else {
            halfX = newX + 419;
        }
        $('#promo_slider').animate({ left: halfX }, inSpeed, easeIn, function() {
            $(this).animate({ left: newX }, outSpeed, easeOut, function() {
                moose = true;
            });
        })
    }


    //PRELOAD IMAGES

    /*$('img.preload').each(function(){
    imgLoad($(this));
    });
	
	function imgLoad(img){
    url = $(img).attr('href').substring(1);
    alert(url);
    }*/

    //END jQuery
});