function scroll_to(n)
{
	if(jQuery.browser.opera)
		$('html, body').scrollTo($("#"+n).offset().top);
	else
		$('html, body').animate({scrollTop: $("#"+n).offset().top}, 1000);
}

$(window).scroll(function(){ 
	scroll_complete();
});

var currentbg= '';
var newbg= '';
var currentpage= '';

function scroll_complete()
{
	var pos= $(window).scrollTop();
	var current= ''

	if( ($('#contact').length!= 0) && (pos>= $('#contact').offset().top-100) )
	{
		newbg= 'url("'+$('#bg-pf').attr('src')+'")';
		current= '#contact';
	}
	else if( ($('#pf').length!= 0) && pos>= ($('#pf').offset().top-100) )
	{
		newbg= 'url("'+$('#bg-pf').attr('src')+'")';
		current= '#pf';
	}
	else if( ($('#services').length!= 0) && (pos>= $('#services').offset().top-100) )
	{
		newbg= 'url("'+$('#bg-services').attr('src')+'")';
		current= '#services';
	}
	else if( ($('#gear').length!= 0) && (pos>= $('#gear').offset().top-100) )
	{
		newbg= 'url("'+$('#bg-gear').attr('src')+'")';
		current= '#gear';
	}
	else
	{
		newbg= 'url("'+$('#bg-studio').attr('src')+'")';
		current= '#studio';
	}
	
	if(currentpage!= current)
	{
		currentpage= current;
		
		$('#menu ul a').removeClass('select');
		$('#menu ul a').each(function()
		{
			if($(this).attr('href')== current)
				$(this).addClass('select');
		});
	}
}

function refresh()
{
	if(currentbg== newbg)
		return;
	currentbg= newbg;
	if($('#background-image-1').css('z-index') == -1)
	{
		$('#background-image-2').css({'z-index': '-1'});
		$('#background-image-1').css({
			'display':'none', 
			'background-image':newbg,
			'z-index':'0'
		}).fadeIn('fast');
	}
	else
	{
		$('#background-image-1').css({'z-index': '-1'});
		$('#background-image-2').css({
			'display':'none', 
			'background-image':newbg,
			'z-index':'0'
		}).fadeIn('fast');
	}
}

setInterval(refresh, 1500);

$(document).ready(function()
{
	currentbg= 'url("'+$('#bg-index').attr('src')+'")';
	scroll_complete();
	refresh();
});

/*
$(document).ready(function()
{
	$().piroBox({
		my_speed: 300, //animation speed
		bg_alpha: 0.5, //background opacity
		slideShow : 'false', // true == slideshow on, false == slideshow off
		slideSpeed : 3, //slideshow
		close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
	});
});
*/
function isiPhone()
{
	return (
		(navigator.platform.indexOf("iPhone") != -1) ||
		(navigator.platform.indexOf("iPod") != -1) ||
		(navigator.platform.indexOf("iPad") != -1)
	);
}

$(document).ready(function() 
{
	if(isiPhone())
	{
		// Background fixing
		$('#background-image-1').before('<div id="background-studio" class="background" style="height: 1000px;"></div>');
		$('#background-image-1').before('<div id="background-gear" class="background" style="height: 1000px;"></div>');
		$('#background-image-1').before('<div id="background-services" class="background" style="height: 1000px;"></div>');
		$('#background-image-1').before('<div id="background-pf" class="background" style="height: 1000px;"></div>');
		$('#background-image-1').remove();
		$('#background-image-2').remove();
		$('.background').css(
		{
			'height': '1000px',
			'position': 'absolute'
		});
		
		var url='';
		var offset='';
		url= $('#bg-studio').attr('src');
		offset= $('#studio').offset().top;
		$('#background-studio').css(
		{
			'background-image': 'url('+url+')',
			'top': offset+'px'
		});
		url= $('#bg-gear').attr('src');
		offset= $('#gear').offset().top;
		$('#background-gear').css(
		{
			'background-image': 'url('+url+')',
			'top': offset+'px'
		});
		url= $('#bg-services').attr('src');
		offset= $('#services').offset().top;
		$('#background-services').css(
		{
			'background-image': 'url('+url+')',
			'top': offset+'px'
		});
		url= $('#bg-pf').attr('src');
		offset= $('#pf').offset().top;
		$('#background-pf').css(
		{
			'background-image': 'url('+url+')',
			'top': offset+'px'
		});

		// menu fixing
		var menu= $("#menu").html();
		$("#menu").before('<div id="menu-studio" class="menu" style="position: absolute; top:'+$('#studio').offset().top+'px">'+menu+'</div>');
		$("#menu").before('<div id="menu-gear" class="menu" style="position: absolute; top:'+$('#gear').offset().top+'px">'+menu+'</div>');
		$("#menu").before('<div id="menu-services" class="menu" style="position: absolute; top:'+$('#services').offset().top+'px">'+menu+'</div>');
		$("#menu").before('<div id="menu-pf" class="menu" style="position: absolute; top:'+$('#pf').offset().top+'px">'+menu+'</div>');
							
		$('#menu-studio ul a').removeClass('select');
		$('#menu-studio ul a').each(function()
		{
			if($(this).attr('href')== '#studio')
				$(this).addClass('select');
		});
		$('#menu-gear ul a').removeClass('select');
		$('#menu-gear ul a').each(function()
		{
			if($(this).attr('href')== '#gear')
				$(this).addClass('select');
		});
		$('#menu-gear .gallery a').removeClass('first');
		$('#menu-services ul a').removeClass('select');
		$('#menu-services ul a').each(function()
		{
			if($(this).attr('href')== '#services')
				$(this).addClass('select');
		});
		$('#menu-services .gallery a').removeClass('first');
		$('#menu-pf ul a').removeClass('select');
		$('#menu-pf ul a').each(function()
		{
			if($(this).attr('href')== '#pf')
				$(this).addClass('select');
		});
		$('#menu-pf .gallery a').removeClass('first');
		$("#menu").remove();
	}
});
