$(document).ready(function(){ 
	
	// Load Cufon
	Cufon.replace('h1.page, .content ul.navigation li.questions a, .content ul.navigation li.features a', {
		hover: true
	});
	
	// Phone Slider
	jQuery('#slider-phone').jcarousel({
        auto: 4,
		scroll: 1,
		wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: phone_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: phone_itemVisibleOutCallback},
		initCallback: phone_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	// Ego Slide
	jQuery('#slider-ego').jcarousel({
        auto: 5,
		scroll: 3,
		wrap: 'circular',
		itemVisibleInCallback: {onBeforeAnimation: ego_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: ego_itemVisibleOutCallback},
		initCallback: ego_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	// Preload images
	var cache = [];
	$.each(phone_itemList, function(key, value) { 
		var cacheImage = document.createElement('img');
		cacheImage.src = value;
		cache.push(cacheImage);
	});
	
});

function phone_itemVisibleInCallback(carousel, item, i, state, evt) {
	var idx = carousel.index(i, phone_itemList.length);
	carousel.add(i, phone_getItemHTML(phone_itemList[idx - 1]));
	$('.phone-controls a').removeClass('current');
	$('.phone-controls a.item'+idx).addClass('current');
};

function phone_itemVisibleOutCallback(carousel, item, i, state, evt) {
	carousel.remove(i);
};

function phone_getItemHTML(item) {
	return '<img src="' + item + '" width="154" height="228" />';
};

function phone_initCallback(carousel) {
    $('.phone-controls a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });
};

function ego_initCallback(carousel) {
	$('#ego-next').bind('click', function() {
        carousel.next();
		carousel.startAuto(0);
		$('#ego-pause').addClass('active');
        return false;
    });

    $('#ego-prev').bind('click', function() {
        carousel.prev();
		carousel.startAuto(0);
		$('#ego-pause').addClass('active');
        return false;
    });
	
	$('#ego-pause').bind('click', function() {
        
		if($(this).hasClass('active')) {
			$(this).removeClass('active');
			carousel.startAuto(4);
		} else {
			$(this).addClass('active');
			carousel.startAuto(0);
		}
		
        return false;
    });
};

function ego_itemVisibleInCallback(carousel, item, i, state, evt) {
	var idx = carousel.index(i, ego_itemList.length);
	carousel.add(i, ego_itemList[idx - 1].content);
};

function ego_itemVisibleOutCallback(carousel, item, i, state, evt) {
	carousel.remove(i);
};

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);