/************************************/
//  GLOBAL VARIABLES

// BOOLEANS
var isHome = false;
var isPortfolio = false;
var isContact = false;
var isLoggedIn = false;
var firstTime = true;

// BROWSER WINDOW
var windowWidth;
var windowHeight;
var lastSize;
var resizeTimer = null;


// HOME PAGE SLIDESHOW
var curNum = 1;
var totalImg = 3;
var intv = null;


// HOME PAGE SLIDER
var shownNum;
var position = 0;


// PORFOLIO PAGE
var ulWidth = 1200;
var oriWidth;
//var liWidth = 259;
//var liHeight = 160;
var liWidth = 290;
var liHeight = 170;


//var liRatio = 1.4;
//var imgRatio = 2.22222222;
//var pageUlRatio = 1.202;
var liRatio = 1.71;
var imgRatio = 2.23;
var pageUlRatio = 1.202;

var borderWidth = 257;
var borderHeight = 122;

var imgWidth = 290;
var imgHeight = 130;
var thumbSize;
var newSize = 1200;
//var displayNum = 5;
var displayNum = 4;
var oldWidth;

var showResizeTimer = null;

// PRELOADER 
var loaderTop;
var loaderLeft;
var loaded = false;



/*****************************/
/********  PRELOADER  ********/
/*****************************/
function preloader() {
	$('#preloader').hide();
	$('#everything').fadeIn();
	loaded = true;
}

$(function() {
	$('#preloader').click(function() {
		$(this).hide();
	});
});



/************************************/
/*********  DOCUMENT READY  *********/
/************************************/

$(document).ready(function() {
						   
	/***********************************************/
	/**********  INITIALIZE ON PAGE LOAD  **********/
	/***********************************************/
	oldWidth = $(window).width();
	
	// HIDES THE BLACK MASK
	$('.blackOver').hide();
	
	// GETS BROWSER DIMENSIONS
	checkSize();
	
	// POSITIONS PRELOADER
	loaderTop = (windowHeight - 32) / 2;
	loaderLeft = (windowWidth - 32) / 2;
	$('#preloader').css({
		'display' : 'block',
		'top' : loaderTop,
		'left': loaderLeft
	});
	
	// IF BROWSER IS SAFARI
	if ($.browser.safari) {
        $("body").addClass("webkit");
    };
	
	// CHECK TO SEE WHAT PAGE IT IS:
	if ( $('.home').length > 0) {isHome = true}
	if ( $('.portfolio').length > 0 ) {isPortfolio = true}
	if ( $('body').hasClass('loggedIn') ) { isLoggedIn = true }
	if ( $('.contactPage').length > 0 ) {isContact = true}
	
	// IF ON HOME PAGE:
	if (isHome) {
		moveNav(1);
		// SETS UP HOME PAGE SLIDESHOW
		$('.home #controls ul a').click(function() {
			var tmpNum = $(this).attr('id');
			slideSelect(tmpNum);
		});
		$('#controls .playPause').click(function() {
			var playing;
			( $(this).hasClass('play') ) ? playing = true : playing = false;
			if(playing) {
				stopInt();
				$(this).removeClass('play').addClass('pause');
			} else {
				startInt();
				$(this).removeClass('pause').addClass('play');
			}
		});
		window.clearInterval(intv);
		intv = '';
		startItUp();
		changeSlider();
	}
	
	function startItUp() {
		intv = window.setInterval('changeSlide()', 10000);
	}
	
	// IF ON WORK (PORTFOLIO) PAGE
	if ( isPortfolio ) {
		// SETS UP THUMB RESIZE ON PORTFOLIO PAGES
		//scaleThumbs();
	}
	
	// IF ON CONTACT PAGE
	if (isContact) {
		// PROVIDES AN ALTERNATE, FAKE PRELOADER THAT
		// WILL NOT INTERFERE WITH GOOGLE MAPS API
		$('#everything').show();
		$('.blackOver').show()
		var timeOut = setTimeout(function() {
			$('#preloader').hide();
			$('.blackOver').fadeOut();
		}, 1500);
		loaded = true;
	} else {
		// PROVIDE REGULAR PRELOADER
		window.onload = preloader;
	}
	
	// IF USER IS LOGGED IN
	if (isLoggedIn) {
		$('.logo a img').attr('src', 'images/p3-logoOnBlack.png');
	} else {
		$('.logo a img').attr('src', 'images/p3-logo.png');
	}
});




/************************************/
/********  RESIZE EVENTS  ***********/
/************************************/

// GETS BROWSER WINDOW WIDTH/HEIGHT
function checkSize() {
	windowWidth = $(window).width();
	windowHeight = $(window).height();
}

// RESIZE LISTENER
$(function() {
	$(window).resize(function() {
		clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizeComplete, 200);
	});
});

// RESIZE EVENT HANDLER
function resizeComplete() {
	if ( !loaded ) {
		loaderTop = (windowHeight - 32) / 2;
		loaderLeft = (windowWidth - 32) / 2;
		$('#preloader').css({
			'display' : 'block',
			'top' : loaderTop,
			'left': loaderLeft
		});
	}
	if ( isHome ) {
		changeSlider();
	} else {
		if ( isPortfolio ) {
			//scaleThumbs(oldWidth);
		}
	}
}




/**************************************************/
/***********  HOME PAGE FUNCTIONALITY  ************/
/**************************************************/

// USER MANUALLY SELECT A SLIDE
function slideSelect(inNUM) {
	if(inNUM == curNum) {
		// DO NOTHING
	} else {
		
		if(intv != '') {
			window.clearInterval(intv);
			intv = '';
			changeSlide(inNUM);
			intv = window.setInterval(function() {
				changeSlide();
			}, 10000);
		} else {
			changeSlide(inNUM);
		}
	}
}

// SLIDESHOW FUNCTIONALITY
function changeSlide(inNUM) {
	if (inNUM == 'undefined' || inNUM == null || inNUM == '') {
		if(curNum != totalImg) {
			$('#'+curNum).removeClass('active');
			$('#images').fadeOut('fast', function() {
				$("#slide_" + curNum).hide();
				moveNav(curNum+1);
				$("#slide_" + (curNum +1)).show();
			}).fadeIn('fast');
			curNum++;
		} else {
			$('#'+curNum).removeClass('active');
			$('#images').fadeOut('fast', function() {
				$("#slide_" + curNum).hide();
				moveNav(1);
				$("#slide_1").show();
			}).fadeIn('fast');
			curNum = 1;
		}
		$('#'+curNum).addClass('active');
		
	} else {
		$('#'+curNum).removeClass('active');
		$('#images').fadeOut('fast', function() {
			$("#slide_" + curNum).hide();
			curNum = parseInt(inNUM);
			moveNav(curNum);
			$("#slide_" + (curNum)).show();
		}).fadeIn('fast');
		$('#'+curNum).addClass('active');
	}
	
}

// STOP SLIDESHOW
function stopInt() {
	window.clearInterval(intv);
	intv = '';
}

// START SLIDESHOW
function startInt() {
	changeSlide();
	intv = window.setInterval('changeSlide()', 10000);
}

// CHANGES THE POSITION OF 'LAUNCH' AND 'MORE INFO' BASED ON WHAT SLIDE IS SHOWN
function moveNav(inNUM) {
	switch (inNUM)	{
		case 1:
			$('#images ul').css({'top': 265, 'left': 13});
			$('#images ul a.moreInfo').attr('href', 'careers.aspx');
			$('#images ul a.launch').hide();
			$('#images ul a.moreInfo').show();
			break;
		case 2:
			$('#images ul').css({'top': 245, 'left': 11});
			$('#images ul a.launch').attr('href', 'http://ten.propaganda3.com/');
			$('#images ul a.moreInfo').attr('href', 'work_p3anni.aspx');
			$('#images ul a.launch').show();
			$('#images ul a.moreInfo').show();
			break;
		case 3: 
			$('#images ul').css({'top': 240, 'left': 11});
			$('#images ul a.launch').attr('href', 'http://itunes.apple.com/us/app/road-trip-66/id415478066?mt=8');
			$('#images ul a.moreInfo').attr('href', 'work_roadTrip66.aspx');
			$('#images ul a.launch').show();
			$('#images ul a.moreInfo').show();
			break;	
		default: break;
	}
}

// PROJECT THUMB SLIDER (HOME)
$(function() {
	$('#projectNav').each(function() {
		
		var prev = $('#prevBtn', this);
		var next = $('#nextBtn', this);
		var slider = $('#projectSlider ul', this);
		var thumbs = $('#projectNav ul li').size();
		var thumbWidth = 325;
		var maxLength = thumbs * thumbWidth;
		$(prev).click(function() {
			if (position < 0) {
				position += thumbWidth;
				$(slider).animate({
					left: position
				}, 200);
			} else {
				// DO NOTHING
			}
		});
		
		$(next).click(function() {
			if ( position != (maxLength * -1) + ( thumbWidth * shownNum) ) {
				position -= thumbWidth;
				$(slider).animate({
					left: position
				}, 200);
			} else {
				// DO NOTHING
			}
		});
		
		// BORDER ANIMATTION:
		$('li', this).each(function() {
			var border = $('.border a', this);
			
			$(border).mouseover(function() {
				$(this).animate({
					'top'    : 0,
					'left'   : 0,
					'width'  : 299,
					'height' : 131
				}, 150);
			}).mouseout(function() {
				$(this).animate({
					'top'    : -4,
					'left'   : -4,
					'width'  : 307,
					'height' : 139
				}, 150);
			});
		});
		
		
	});
});

// ON RESIZE: SCALES HOME PROJECT SLIDER
function changeSlider() {
	checkSize();
	shownNum = Math.floor(windowWidth / 325);
	if (shownNum < 3) shownNum = 3;
	newWidth = shownNum * 325;
	var lis = $('#projectSlider ul li').size();
	var endMarker = Math.abs(position/325) + shownNum;
	
	if(newWidth != lastSize) {
		
		if(newWidth > lastSize && endMarker > lis) {
			$('.fadeWrap').fadeOut(300, function() {
				$('#projectSlider').css('width', newWidth);
				var diff = endMarker - lis;
				position = position + (325 * diff);
				$('#projectSlider ul').css('left', position);
			}).fadeIn(300);
		} else {
			$('.fadeWrap').fadeOut(300, function() {
				$('#projectSlider').css('width', newWidth);
			}).fadeIn(300);
		}
		
		lastSize = newWidth;
	}
}



/************************************/
/*********  PORTFOLIO PAGES  ********/
/************************************/

// WORK PAGE THUMBS:
$(function() {
	$('.workThumbs li').each(function() {
		
		var li_width = $(this).width();
		var li_height = $(this).height();
		
		// BORDER ANIMATION:
		var border = $('.border a', this);
		var color;
		
		(isLoggedIn) ? color = '#ccc' : color = '#ccc';
		
		$(border).stop(true, false).mouseover(function() {
			$(this).css('border-color','#ccc').animate({
				'top'    : 0,
				'left'   : 0,
				'width'  : borderWidth - 8,
				'height' : borderHeight - 8
			}, 150);
		}).mouseout(function() {
			$(this).stop(true, false).animate({
				'top'    : -4,
				'left'   : -4,
				'width'  : borderWidth,
				'height' : borderHeight
			}, 150, function() {
				$(this).css('border-color', color);
			});
		});
	});
});

// WORK PAGE (INDIVIDUAL) THUMBS:
$(function() {
	$('#thumbs li').each(function() {
		
		// BORDER ANIMATTION:
		var border = $('a', this);
		var color;
		
		(isLoggedIn) ? color = '#ccc' : color = '#ccc';
		
		$(border).stop(true, false).mouseover(function() {
			$(this).css('border-color','#ccc').animate({
				'top'    : 0,
				'left'   : 0,
				'width'  : 296,
				'height' : 76
			}, 150);
		}).mouseout(function() {
			if ( $(this).hasClass('.active') ) {
				// do nothing
			} else {
				$(this).stop(true, false).animate({
					'top'    : -4,
					'left'   : -4,
					'width'  : 304,
					'height' : 84
				}, 150, function() {
					$(this).css('border-color', color);
				});
			}
		}).click(function() {
			$('#thumbs li a').removeClass('active');
			$(this).addClass('active');
			
			$('#thumbs li a:not(.active)').animate({
				'top'    : -4,
				'left'   : -4,
				'width'  : 304,
				'height' : 84
			}, 150, function() {
				$(this).css('border-color', color);
			});
						
		});
	});
});


// SCALES PORTFOLIO THUMBS
/*
function scaleThumbs() {
	checkSize();
	
	liWidth = (Math.floor(windowWidth / 6) - 22);
	ulWidth = (liWidth + 22) * displayNum;
	var actWidth = $('.workThumbs').width();
	imgWidth = liWidth - 8;
	imgHeight = Math.floor(imgWidth / imgRatio);
	liHeight = Math.floor(imgHeight + 45);
	newSize = ulWidth;
	
	var diff = Math.abs(oldWidth - windowWidth);	
	
	if (diff > 50 && ulWidth > 1100 || firstTime) { 
		$('.workThumbs').fadeOut(300, function() {
			$('.workThumbs').css('width', ulWidth);
			$('.workThumbs li').css({
				'width': liWidth,
				'height': liHeight
			});
			$('.workThumbs li img').css({
				'width': imgWidth,
				'height': imgHeight
			});
			var h3size = Math.round(ulWidth * .01);
			if (h3size > 11) h3size = 11;
			$('.workThumbs li h3').css({
				'font-size': h3size,
				'margin': '0 0 ' +Math.round((ulWidth * .01)/2) + 'px'
			});
		}).fadeIn(300);
		thumbSize = newSize;
		firstTime = false;
		oldWidth = $(window).width();
	} else {
		if (diff > 50 && ulWidth < oldWidth && actWidth != 1100 || firstTime) { 
			$('.workThumbs').fadeOut(300, function() {
				$('.workThumbs').css('width', 1100);
				$('.workThumbs li').css({
					'width': 198,
					'height': 125
				});
				$('.workThumbs li img').css({
					'width': 190,
					'height': 85
				});
				var h3size = Math.round(ulWidth * .01);
				if (h3size > 11) h3size = 11;
				$('.workThumbs li h3').css({
					'font-size': h3size,
					'margin': '0 0 ' +(ulWidth * .01)/4 + 'px'
				});
			}).fadeIn(300);
			thumbSize = newSize;
			firstTime = false;
			oldWidth = $(window).width();
		}
	}
}
*/
function scaleThumbs() {
	checkSize();
	
	var marginWidth = 10;
	var actWidth = oldWidth;
	//alert(actWidth);
	var diff = Math.abs(oldWidth - windowWidth);	
	
	liWidth = (Math.floor(windowWidth / displayNum) - marginWidth);
	imgWidth = liWidth;
	imgHeight = Math.floor(imgWidth / imgRatio);
	ulWidth = (liWidth + marginWidth) * displayNum;
	liHeight = Math.floor(imgHeight + imgHeight * .35);
	
	var h3size = Math.round(ulWidth * .015);
	
	borderWidth = imgWidth - 8;
	borderHeight = imgHeight - 8;
	
	newSize = ulWidth;
	
	if (diff > 50 /*&& actWidth >= 1200*/) {
		$('.workThumbs').fadeOut(300, function() {
			$('.workThumbs').css('width', ulWidth);
			
			$('.workThumbs li').css({
				'width': liWidth,
				'height': liHeight
			});
			
			$('.workThumbs li .border').css({
				'width': imgWidth,
				'height': imgHeight
			});
			
			$('.workThumbs li .border a').css({
				'width': borderWidth,
				'height': borderHeight
			});
			
			$('.workThumbs li .border img').css({
				'width': imgWidth,
				'height': imgHeight
			});
			
			$('.workThumbs li h3').css({
				'font-size': h3size,
				'line-height' : h3size+6+'px'
			});
			
		}).fadeIn(300);
		
		thumbSize = newSize;
		firstTime = false;
		oldWidth = $(window).width();
	}
}


// GALLERY FUNCTIONALITY
$(function() {
	$('#gallery #thumbs a').click(function() {
		var tmpNum = $(this).attr('id');
		imgSelect(tmpNum);
	});
});

function imgSelect(inNUM) {
	if(inNUM == curNum) {
		// DO NOTHING
	} else {
		curNum = parseInt(inNUM);
		
		$('#slider').animate({
			left: ((curNum - 1) * 760) * -1
		}, 500);
		
		$('#gallery #thumbs a').removeClass('active');
		$('#'+curNum).addClass('active');
	}
}





/***************************************************/
/*************  LOGIN FUNCTIONALITY  ***************/
/***************************************************/

// BRINGS UP LOGIN PAGE WHEN "LOG IN FOR FULL PORTFOLIO" IS CLICKED
$(function() {
	$('.lock a').click(function() {
		var loggedIn = $('body').hasClass('loggedIn')
		if ( !loggedIn ) {
			windowHeight = $(window).height();
			var loginH = windowHeight/2 - 100;
			
			$('.container').animate({
				top: loginH
			}, function() {
				$('.loginOverlay').fadeIn('200', function() {
					$('#password').focus();
				});
			});
		}
	});
	
	// FADES OUT LOGIN PANE WHEN "X" IS CLICKED
	$('.close a').click(function() {
		$('.loginOverlay').fadeOut(200);
	});
	
	// LETS LOGIN PASSWORD BE SUBMITTED BY THE ENTER KEY
	$('#password').keypress(function(event) {
		if (event.keyCode == 13 || event.keyCode == '13') {
			
			doLogin();
		}
	});
	
});

// AUTHENTICATES LOGIN
function doLogin() {
	var doEmail = false;
	$('#loginErrorDiv').innerHTML = "<img src=\"images/ajax-loaderSmall.gif\" />";
	$('#loginErrorDiv')[0].style.display = "block";
	
	var password = $('#password').val();
	
	(password != "") ? doEmail = true : doEmail = false;
	
	if (doEmail) 
	{
		$.getJSON("submit_login.ashx",{
			password : password
			}, function(j)
			{
			
			if( j.length > 0 )
			{
				if( j[0].result == "1" )
				{
					$("#loginErrorDiv")[0].innerHTML = "<span id=\"response\">Login Successful</span>";
					$("#password").val("");
					location.href = "default.aspx";
				}
				else
				{
					$("#loginErrorDiv")[0].innerHTML = "<span id=\"response\">Login Failed: " + j[0].message + "</span>";
				}
			}
		});
	
		//$.ajax({
		//	url: 'submit_login.ashx',
		//	type: 'POST',
		//	data: 'password=' + escape(password)
		//	, success:function(result)
		//	{
		//		if(result == "1")
		//		{
		//			$('#loginErrorDiv')[0].innerHTML = '<span id="response">Login Successful</span>';
		//			$('#password').val("");
		//			location.href = "work_addys.aspx";
		//		}
		//		else
		//		{
		//			$('#loginErrorDiv')[0].innerHTML = '<span id="response">Login Failed</span>';
		//		}
		//	},
		//	error:function(result) 
		//	{
		//		$('#loginErrorDiv')[0].innerHTML = '<span id="response">Login Failed</span>';
		//	}
		//});
		//return false;
	} 
	else 
	{
		alert('Oops!  You forgot something!');
		$('#loginErrorDiv')[0].innerHTML = "";
		$('#loginErrorDiv')[0].style.display = "none";
	}
}



/**************************************/
/********  FOOTER EMAIL FORM  *********/
/**************************************/

// VALIDATES AND SENDS FOOTER FORM
function doEmail() {
	var doEmail = false;
	$('#errorDiv').innerHTML = "<img src=\"images/ajax-loaderSmall.gif\" />";
	$('#errorDiv')[0].style.display = "block";
	
	var firstname = $('#firstname').val();
	var lastname = $('#lastname').val();
	var email = $('#email').val();
	var message = $('#message').val();
	var captcha = $('#captcha').val();
	
	(firstname != "" && lastname != "" && email != "" && message != "" && captcha != "") ? doEmail = true : doEmail = false;
	
	if (doEmail) 
	{
		$.getJSON("submit_email.ashx",{
			firstname : firstname
			, lastname : lastname
			, email : email
			, message : message
			, captcha : captcha
			}, function(j)
			{
			
			if( j.length > 0 )
			{
				if( j[0].result == "1" )
				{
					//$('#errorDiv')[0].innerHTML = "<span id=\"response\">Message Sent</span>";
					alert('Message Sent');
					$('#firstname').val("");
					$('#lastname').val("");
					$('#email').val("");
					$('#message').val("");
					$('#captcha').val("");
					reloadCaptcha();
				}
				else
				{
					//$('#errorDiv')[0].innerHTML = "<span id=\"response\">Message Failed: " + j[0].message + "</span>";
					alert('Message Failed:\n' + j[0].message);
				}
			}
		});
	
//	    $.ajax({
//			url: 'submit_email.ashx',
//			type: 'POST',
//			data: 'firstname=' + escape(firstname)
//				+ '&lastname=' + escape(lastname) 
//				+ '&email=' + escape(email) 
//				+ '&message=' + escape(message) 
//				+ '&captcha=' + escape(captcha)
//			, success:function(result)
//			{
//				if(result == "1")
//				{
//					$('#errorDiv')[0].innerHTML = '<span id="response">Message Sent</span>';
//					$('#firstname').val("");
//	                $('#lastname').val("");
//                    $('#email').val("");
//                    $('#message').val("");
//				}
//				else
//				{
//					$('#errorDiv')[0].innerHTML = '<span id="response">Message Failed</span>';
//				}
//			},
//			error:function(result) {
//				$('#errorDiv')[0].innerHTML = '<span id="response">Message Failed</span>';
//			}
//    	});
//	    return false;
	} 
	else 
	{
		alert('Oops!  You forgot something!');
		$('#errorDiv')[0].innerHTML = "";
		$('#errorDiv')[0].style.display = "none";
	}
}



/************************************************/
/********  PAGE FADE-OUT FUNCTIONALITY  *********/
/************************************************/

$(function() {
	$('.fader').live('click', function(e) {
		e.preventDefault();
		var link = this;
		
		$('.blackOver').fadeIn(500, function() {
			document.location = link.href;
		});
	});
});



/*************************************/
/********  INPUT TEXT CLEAR  *********/
/*************************************/

function clearText(input) {
	if(input.defaultValue==input.value){input.value=''};
}

function restoreText(input) {
	if(input.value==''){input.value=input.defaultValue;}
}



/***************************************/
/********  FLASH VIDEO PLAYER  *********/
/***************************************/

var flashVideoPlayer;
		
function initialize() {
	/* Check if the browser is IE. If so, flashVideoPlayer is window.videoPlayer. 
	Otherwise, it's window.document.videoPlayer. The videoPlayer is the id assigned to <object> and <embed> tags. */
	var ie = navigator.appName.indexOf("Microsoft") != -1;
	flashVideoPlayer = (ie) ? window['videoPlayer'] : document['videoPlayer'];
}

function callFlashPlayVideo() {
	initialize();
	flashVideoPlayer.resumeVideo();
}

// Call the pauseResume() function within the SWF.
function callFlashPauseVideo() {
	initialize();
	flashVideoPlayer.pauseVideo();
}
