// FUNCTIONS THAT RUN ON PAGE LOAD //
$(document).ready(function(){
	// Set scrollTo links
	$('a.link').click(function () {
		$('#wrapper').scrollTo($(this).attr('rel'), 1000);
		setPosition($(this).attr('rel'), '#back1', '0px', '400px', '800px', '1200px');
		$("#back1 img").attr("src", bg_url);
		$("#back1 img").show();
		$("#back2 img").hide();
		$('a.link').removeClass('selected');  
		$(this).addClass('selected');
		$('.teamInfo').hide();
		$('.teamWelcome').show();
		$('.teamMsg').show();
		return false;
	});

	$('a.box_link').click(function () {
		$('.mask_box').scrollTo($(this).attr('rel'), 1000);
	});

	// Colorbox links
	$(".colorbox").colorbox({width:"80%", height:"80%", iframe:true, transition:"fade", speed:500});

	//form validation
	$("#mc-embedded-subscribe").attr("disabled", "true");
	$("#mce-EMAIL").keyup(function() {
		var valid_email = $("#mce-EMAIL").val()
		if( valid_email != 0 )
		{
			if( isValidEmailAddress( valid_email ) )
			{
				$("#mce-EMAIL").css({"background": "url('/images/validyes.png') #FFFFFF no-repeat 180px 4px"});
				$("#mc-embedded-subscribe").removeAttr("disabled");
			} 
			else 
			{
				$("#mce-EMAIL").css({"background": "url('/images/validno.png') #FFFFFF no-repeat 180px 4px"});
				$("#mc-embedded-subscribe").attr("disabled", "true");
			}
		} 
		else 
		{
			$("#mce-EMAIL").css({"background-image": "none"});			
		}
	});

	function isValidEmailAddress(valid_email)
	{
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(valid_email);
	}

	$("#mc-embedded-subscribe").click( function() {
		//$('#mc-embedded-subscribe-form').fadeOut('fast');
		var method = 'method=listSubscribe&id=d7ab041990';
		var apikey = 'apikey=91ed602c67d72e1cf8f925565db23350-us1';
		var id = 'id=d7ab041990'
		var email_address = 'email_address='+$('input[name=EMAIL]').val();
		var merge_vars = 'merge_vars[null]';
		var output = 'output=php';
		$.ajax({
			type: "GET",
			url: "http://api.mailchimp.com/1.2/",
			data: method+"&"+apikey+"&"+id+"&"+email_address+"&"+merge_vars+"&"+output,
			success: function (html) {
				$('#search').fadeOut('fast');
				$('#search').html('<form><label>Thank You For Signing Up</label></form>');
				$('#search').fadeIn('fast');
			}
		});
		//cancel the submit button default behaviours  
	    return false;
	});
	//end form validation
});



// ADDITIONAL CUSTOM FUNCTIONS //
function processURL( segment_1 )
{
	if ( segment_1 == 'web' )
	{
		$.post('/team/container/', function(data) {
			$('#team').html(data);
			$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
		});
		$.post('/work/container/', function(data) {
			$('#work').html(data);
			$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
		});
		$.post('/contact/container/', function(data) {
			$('#contact').html(data);
			$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
		});
		$.post('/work/web/', function(data) {
			$('.prev').show();
			$('.next').show();
			$('.horScroll').html(data);
			$('#wrapper').scrollTo($('#work'), 1000 );
		});
	}

	if ( segment_1 == 'print' )
	{
		$.post('/team/container/', function(data) {
			$('#team').html(data);
			$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
		});
		$.post('/work/container/', function(data) {
			$('#work').html(data);
			$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
		});
		$.post('/contact/container/', function(data) {
			$('#contact').html(data);
			$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
		});
		$.post('/work/print/', function(data) {
			$('.prev').show();
			$('.next').show();
			$('.horScroll').html(data);
			$('#wrapper').scrollTo($('#work'), 1000 );
		});
	}

	if ( segment_1 != '' )
	{
		$('#wrapper').scrollTo($('#'+segment_1), 0 );
		setPosition($('#'+segment_1), '#back1', '0px', '400px', '800px', '1200px');
		myLocation = 'http://www.fragmentlabs.com/#' + segment_1; 
		window.location.href = myLocation; //this rewrites the URL with JS if the user hits a link like /work/ for some reason.
	}
	else
	{
		if ( segment_1 != 'team' )
		{
			$.post('/team/container/', function(data) {
				$('#team').html(data);
				$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
			});		
		}

		if ( segment_1 != 'work' )
		{
			$.post('/work/container/', function(data) {
				$('#work').html(data);
				$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
			});		
		}

		if ( segment_1 != 'contact' )
		{
			$.post('/contact/container/', function(data) {
				$('#contact').html(data);
				$('#subNavReplace').columnizeList({cols:3,constrainWidth:0});
			});		
		}	
	}
}

function loadTeamDetails( teamMember )
{
	$('#back2 img').fadeIn("fast");
	$.post('/team/details/'+teamMember+'/', function(data) {
		$('.teamInfo').html(data);
		$('.teamWelcome').hide();
		$('.teamMsg').hide();
		$('.teamInfo').show();
	});
}

function loadCategory( category )
{
	$.post('/work/'+category+'/', function(data) {
		$('.prev').show();
		$('.next').show();
		$('.horScroll').html(data);
	});
	return false;
}

// PLUGIN FUNCTIONS //
function setPosition(check, div, p1, p2, p3, p4) {
	if(check==='#home')
	{
		$(div).scrollTo(p1, 1000, { onAfter:function(){window.location.hash = '#home' } } );
	}
	else if(check==='#team')
	{
		$(div).scrollTo(p2, 800, { onAfter:function(){window.location.hash = '#team' } } );
	}
	else if(check==='#work')
	{
		$(div).scrollTo(p3, 800, { onAfter:function(){window.location.hash = '#work' } } );
	}
	else if (check==='#contact')
	{
		$(div).scrollTo(p4, 800, { onAfter:function(){window.location.hash = '#contact' } } );
	}
};

function contact() {
	$.ajax({
		type: "POST",
		url: '/process_contact/',
		data: $("#contactForm").serialize(),
		success: function(data){
			$("#contactForm").toggle("fast");
			$("#successMessage").toggle("fast").html(data);
		}
	});
	return false;
}

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
}

function randomizeBG() {
	var backgrounds = new Array();
	backgrounds[0] = '/images/bg_01.jpg';
	backgrounds[1] = '/images/bg_02.jpg';
	backgrounds[2] = '/images/bg_03.jpg';
	backgrounds[3] = '/images/bg_04.jpg';
	backgrounds[4] = '/images/bg_05.jpg';
	backgrounds[5] = '/images/bg_06.jpg';
	backgrounds[6] = '/images/bg_07.jpg';
	backgrounds[7] = '/images/bg_08.jpg';
	backgrounds[8] = '/images/bg_09.jpg';
	backgrounds[9] = '/images/bg_10.jpg';
	var x = Math.floor(Math.random() * backgrounds.length);
	if ( x == 0 )
	{
		var backgroundImg = backgrounds[x];
	}
	else
	{
		var backgroundImg = backgrounds[x - 1];
	}
	return backgroundImg;
}