var frameOn = 0;
var homexml='';
var themeurl='';
var imgpager, homepager;
var PHONESLIDEDURATION = 2500;
var continuecycle = true;

function stepTwo() {
	var bye = jQuery('#home .slide img.bye');
	bye.fadeOut(500,function() {
		if(jQuery(this).next().next().size() > 0) {
			jQuery(this).next().attr('class','bye');
			setTimeout(stepThree,(PHONESLIDEDURATION - 500));
		}
		jQuery(this).remove();
	});	
}

function stepThree() {
	var bye = jQuery('#home .slide img.bye');
	bye.fadeOut(500,function() {
		jQuery(this).next().addClass('on');
		jQuery(this).remove();
	});
}

function goFrame() {
	jQuery(this).addClass('on').siblings('.on').removeClass();
	frameOn = jQuery(this).attr('href');
	//error correct IE7
	var poundIndex = frameOn.indexOf('#');
	if(poundIndex!=0) frameOn = frameOn.substr(poundIndex);
	
	on = jQuery(frameOn);
	on.addClass('on').siblings('.on').removeClass('on')
	frameOn = frameOn.substr(6);
	clearTimeout(imgpager);
	clearTimeout(homepager);
	var imgs = on.children('.slides').children().clone();
	
	jQuery('#home .txt').fadeOut(500,function() {
		jQuery(this).empty();
		jQuery('#home').attr('class','f'+frameOn);
		on.children('.txt').children().clone().hide().appendTo(jQuery('#home .txt'));
        _typeface_js.renderDocument();
		jQuery('#home .txt').show().children().fadeIn(300);
		
		jQuery('#home .slide img').eq(0).addClass('on').siblings().remove();
		if(jQuery('#home .slide img:eq(0)').attr('src') != imgs.eq(0).attr('src')) {
			imgs.eq(0).addClass('slidee').css('left','290px');
			jQuery('#home .slide').append(imgs);
			jQuery('#home .slide').children('img.on').animate({left: '-=290px'},300,function() { jQuery(this).remove(); })
			.next().addClass('on').animate({left: '0'},300,function() {
				
				if(jQuery(this).next().size()>0) {
					jQuery(this).attr('class','bye').next().addClass('on');
					imgpager = setTimeout(stepTwo,(PHONESLIDEDURATION - 300));
				}
				
			});
		}
		
	});
	if(continuecycle) homepager = setTimeout(nextButton,(PHONESLIDEDURATION * 3));
	
	return false;
}

function nextButton() {
	var on = jQuery('#jumpto a.on');
	var nex = on.next();
	if(nex.size() == 0) {
		nex = on.siblings('a:first-child');
		clearTimeout(imgpager);
		clearTimeout(homepager);
		continuecycle = false;
		jQuery('#restartanim').fadeIn(300);
	}
	nex.trigger('click.neer');
}

function manualGo() {
	clearTimeout(imgpager);
	clearTimeout(homepager);
	continuecycle = false;
	return false;
}

function restartAnimation() {
	jQuery('#home .txt, #jumpto, #restartanim').fadeOut(300);
	frameOn = 0;
	continuecycle=true;
	startAnimation();
	return false;
}

function startAnimation() {
	if(frameOn==4) {
		jQuery('#path .chat').fadeOut(300);
		jQuery('#jumpto').fadeIn(300).children('a:eq(0)').trigger('click.neer');
	} else {
		jQuery('#path .chat:eq('+frameOn+')').hide().stop().fadeIn(500);
		imgpager = setTimeout(startAnimation,(PHONESLIDEDURATION-500));
		frameOn++;
	}
}

jQuery(function() {
	if(homexml!='') {
		jQuery.ajax({
			url: homexml,
			type: 'GET',
			dataType: 'xml',
			success: function(xml) {
				jQuery('frame',xml).each(function(i) {
					var fram = jQuery('<div class="frame" id="slide'+i+'" />');
					var txt = jQuery('<div class="txt" />');
					var slides = jQuery('<div class="slides" />');
					
					jQuery(this).children('text').children('h1').each(function() {
						txt.append('<h1>'+jQuery(this).text()+'</h1>');
					}).siblings('h2').each(function() {
						txt.append('<h2>'+jQuery(this).text()+'</h2>');
					});
					fram.append(txt);
					
					jQuery(this).children('slides').children('slide').each(function() {
						slides.append('<img src="'+themeurl+'images/slides/'+jQuery(this).text()+'" />');
					});
					fram.append(slides);
					
					jQuery('#frames').append(fram);
					jQuery('#jumpto').append('<a href="#slide'+i+'" />');
				});
				jQuery('#jumpto a').bind('click.neer',goFrame).click(manualGo);
				jQuery('#home').append('<a href="#" id="restartanim">play again</a>');
				jQuery('#restartanim').click(restartAnimation);
				jQuery('#restartanim').hide();
				imgpager = setTimeout(startAnimation,PHONESLIDEDURATION);
			}
		});
	}
	jQuery('#path').append('<a href="http://www.cracked.com/article_14990_what-monkeysphere.html" target="_blank"></a>');
});
