// copyright 2010 Refresh Mobile Ltd


// General site Feature Boxes
function featureBoxJs() {
	
	if($('.featureItem').size() > 1) {
	
		// Feature Box setup
		scrollSpeed = 1000;
		
		$('.featureBox').wrap('<div class="featureBoxWrap"></div>').after('<div><a href="#" class="next">Next</a><a href="#" class="previous">Previous</a></div><div class="featureBoxLinks"></div>');
		
		$('.featureBox').each( function() {
			$(this).find('.featureItems').width(($(this).width()) * $(this).find('.featureItem').size());
			for(i=1;i<=$(this).find('.featureItem').size();i++){
				$(this).parent().find('.featureBoxLinks').append('<a href="#" class="featureBoxLink">'+i+'</a> ');
			}
		});
	
		$(".featureBoxLink:contains('1')").addClass('active');
		
		
		// Run through once
		// featureBoxScrollOnce(scrollSpeed, 2000);
		
	
		// Feature Box actions
		$('.featureBoxWrap a.next, .featureBoxWrap a.previous, .featureBoxWrap .featureBoxLink').click( function() {
			if($(this).hasClass('next')) {
				scrollDestination = $(this).parent().parent().find('.featureBoxLink.active').text();
				scrollDestination++;
				if(scrollDestination > $(this).parent().parent().find('.featureBoxLink:last').text()){
					scrollDestination = 1;
				}
			} else if($(this).hasClass('previous')) {
				scrollDestination = $(this).parent().parent().find('.featureBoxLink.active').text();
				scrollDestination--;
				if(scrollDestination <= 0){
					scrollDestination = $(this).parent().parent().find('.featureBoxLink:last').text();
				}
			} else {
				scrollDestination = $(this).text();
			}
			
			scrollDestinationPx = (scrollDestination-1) * $(this).parent().parent('.featureBoxWrap').find('.featureBox').width() +'px';
			$(this).parent().parent('.featureBoxWrap').find('.featureBox').scrollTo(scrollDestinationPx, scrollSpeed, {axis:'x'});
	
			$(this).parent().parent().find('.featureBoxLink').removeClass('active');
			$(this).parent().parent().find(".featureBoxLink:contains('"+scrollDestination+"')").addClass('active');
			$(this).blur();
			return false;
		});
	
	} else {
		$('.featureBox').wrap('<div class="featureBoxWrap"></div>');
	}
}

// Cycle the first .featureBox on the page all the way through once 
function featureBoxScrollOnce(scrollSpeed , pauseTime) {
		
		sequenceTime = (pauseTime + scrollSpeed) *  $('.featureBoxWrap:first').find('.featureItem').size();
		
		for(i=1;i<=($('.featureBoxWrap:first').find('.featureItem').size());i++){
			if(i == $('.featureBoxWrap:first').find('.featureItem').size()) {
				$('.featureBox:first').delay(pauseTime).scrollTo('0px', scrollSpeed, {axis:'x'});
			} else {
				$('.featureBox:first').delay(pauseTime).scrollTo(($('.featureBox:first').width())*i +'px', scrollSpeed, {axis:'x'});
			}
		}
		$('.featureBoxWrap:first').find('.next, .previous, .featureBoxLink').hide().delay(sequenceTime+500).fadeIn('slow');
		
}


function backgroundMove() {
	$('#container').css('position', 'relative');
	$(document).bind('mousemove',function(e){ 
        percentX = (e.pageX / $(window).width()) * 100;
        percentY = (e.pageY / $(window).height()) * 100;
        
        backgroundPercentX = ((percentX) / 1) * -1;
        backgroundPercentY = ((percentY) / 1) * -1;
        
        containerPercentX = (percentX-50)/5;
        containerPercentY = (percentY-50)/5;
        
        $('#background').css('background-position', backgroundPercentX + 'px ' + backgroundPercentY + 'px');
        
        $('#container').css('top', containerPercentY + 'px');
        $('#container').css('left', containerPercentX + 'px');
        
        $('#emulator-inner').css('top', containerPercentY*3 + 'px');
        $('#emulator-inner').css('left', containerPercentX*3 + 'px');

	});	
}



// Home page feature box
function homeFeatureJs() {
	$("#featureItems").css("overflow","hidden").css('height','479px');
	$('.feature').css('height','600px');
	
	$('#body').before('<div id="imageCache" style="position: absolute; top: -9999px; left: -9999px; width: 10px; height: 10px; overflow: hidden;"><span class="imageCache1" style="background-image: url(v2/images/bg-apps.jpg);"></span><span class="imageCache2" style="background-image: url(v2/images/bg-portal2.jpg);"></span><span class="imageCache3" style="background-image: url(v2/images/bg-whitelabel.jpg);"></span></div>');

	$("#featureList .item1 a, #appsAndSites .previous a").click(function(){
		$('#featureItems').scrollTo('0px', 1000, {axis:'x'});
		$("#featureList a").removeClass('active');
		$("#featureList .item1 a").addClass('active');
		$('#background').fadeOut('slow', function() {
			$('#background').css('background-image', 'url(v2/images/bg-appFactory3.jpg)');
			$('#background').css('background-color', '#D2C8C9');
			$('#background').css('background-repeat', 'repeat');
		}).fadeIn('slow');
		$(this).blur();
		return false;
	});
	$("#featureList .item2 a, #mippinPortal .previous a, #appFactory .next a").click(function(){
		$('#featureItems').scrollTo('1000px', 1000, {axis:'x'});
		$("#featureList a").removeClass('active');
		$("#featureList .item2 a").addClass('active');
		$('#background').fadeOut('slow', function() {
			$('#background').css('background-image', 'url(v2/images/bg-apps.jpg)');
			$('#background').css('background-color', 'transparent');
			$('#background').css('background-repeat', 'no-repeat');
		}).fadeIn('slow');
		$(this).blur();
		return false;
	});
	$("#featureList .item3 a, #whiteLabel .previous a, #appsAndSites .next a").click(function(){
		$('#featureItems').scrollTo('2000px', 1000, {axis:'x'});
		$("#featureList a").removeClass('active');
		$("#featureList .item3 a").addClass('active');
		$('#background').fadeOut('slow', function() {
			$('#background').css('background-image', 'url(v2/images/bg-portal2.jpg)');
			$('#background').css('background-color', 'transparent');
			$('#background').css('background-repeat', 'no-repeat');
		}).fadeIn('slow');
		$(this).blur();
		return false;
	});
	$("#featureList .item4 a, #mippinPortal .next a").click(function(){
		$('#featureItems').scrollTo('3000px', 1000, {axis:'x'});
		$("#featureList a").removeClass('active');
		$("#featureList .item4 a").addClass('active');
		$('#background').fadeOut('slow', function() {
			$('#background').css('background-image', 'url(v2/images/bg-whitelabel.jpg)');
			$('#background').css('background-color', '#1E3138');
			$('#background').css('background-repeat', 'no-repeat');
		}).fadeIn('slow');
		$(this).blur();
		return false;
	});

}



function emulatorSetup() {
	$('#body').before('<div id="imageCache"><span class="imageCache1"></span><span class="imageCache2"></span><span class="imageCache3"></span><span class="imageCache4"></span><span class="imageCache5"></span></div>');
	
	$('.launchEmulator').click( function() {
		$('#portalEmulator iframe').attr('src',$(this).attr('href'));
		$('#portalEmulator a.home').attr('href',$(this).attr('href'));
		$('#portalEmulator').removeClass().addClass($(this).attr('class')).show().draggable();
		$(this).blur;
		return false;
	});
	$('#portalEmulator .close').click( function() {
		$('#portalEmulator').hide();
		$(this).blur;
		return false;
	});
	$('#portalEmulator .back').click( function() {
		window.history.back();
		$(this).blur;
		return false;
	});
	
}



function messageSetup() {
	for(i=0;i<$('.message').size();i++) {
		$('#body').after(
			"\n<div class=\"popup\" id=\""+$('.message:eq('+i+')').attr('id')+"\">\n" +
			"  <div class=\"popup-inner\">\n" + $('.message:eq('+i+')').html() + 
			"<div><a href=\"#\" class=\"linkButton close\">Close</a></div>" +
			"\n  </div>\n</div>\n\n"
		);
	}
	
	$('.message').remove();
	
	$('.popup').hide();
	
	$('a.messageLink').click( function() {
		$('.popup').hide();
		$('.popup'+$(this).attr('href')).show().draggable();
		$('.popup').css('cursor','move');
		$(this).blur();
		return false;
	});
	
	$('.popup .close').click( function() {
		$('.popup').hide();
		$(this).blur();
		return false;
	});
}



$(document).ready(function(){

	featureBoxJs();
	
	//backgroundMove();
	
	emulatorSetup();
	
	messageSetup();

	//Home page feature
	if($('body.page0').size() > 0){
		homeFeatureJs();
	}
	
});