var intervalId = 0;
var intervalId2 = 0;
var page = window.page || {};
var active_section = 'section#main';


function resizeView(){
	var viewHeight = $(window).height();      
    if (viewHeight > 768){ $('section').height(viewHeight-100); } 
    else { $('section').height(678); };
}

$(window).load(function () {
        resizeView();        
});

$(window).resize(function() {
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
        $(this).trigger('resizeEnd');
    }, 500);
});

$(window).bind('resizeEnd', function() {                               
        resizeView();        
        //goToArea(active_section, section_id, 'easeInOutQuint');   
});



$(document).ready(function(){
	
	
	
// If $_GET['f']


	
	// Fade in the main header gradient
	$('#on-main').fadeIn(3000);
	
	
	//when logo is clicked, simulate click on the home element
	$('#siteheader h1 a').click(function() {
		$("nav a[rel='main']").click();
	});
	
	
	//A menu item was clicked
	$('nav a').click(function() {
			
		//make all items inactive
		$('nav li').removeClass('active');
		$('section').removeClass('active_section');
		//make just the selected item active
		$(this).parent().addClass('active');
		
		//Remove gradient form all headers
		$('div.on').fadeOut('slow');

	
		//Get the identifier of the item clicked, which is stored in the tag's "rel" attribute
		var itemId = $(this).attr('rel');

		// Add the class 'active section' to the active section on Menu Click!
		$('section#'+itemId).addClass('active_section');

		//Show the gradient for this item
		$('#' + itemId + ' header div.on').fadeIn(3000);
		
		
		
		//stop the slideshow, and only restart it if I'm going to the home section
		clearInterval(intervalId);
		intervalId = false;
		if(itemId == 'main') {
			intervalId = setInterval ( "rotator()", rotator_interval );
		}
		
		//scroll to appropriate section
		$.scrollTo( '#'+itemId, 1500, { easing:'easeInOutExpo' });	
		return false;
		
	});
	
  // A main rotator text CTA
  $('.pw-cta').click(function(){
    $('nav li').removeClass('active');
    $('section').removeClass('active_section');
    $('a[rel=work]').parent().addClass('active');
    $('div.on').fadeOut('slow');
    $('section#work').addClass('active_section');
    //scroll to appropriate section
    $.scrollTo( 'section#work', 1500, { easing:'easeInOutExpo' });
    $('#work header div.on').fadeIn(3000);
    
    /* Click Add */
    $('ul#work-scroller li').removeClass('active');
		//$(this).parent().addClass('active');
		$('.work_holderhelper').hide();
    var toTurnOn = $(this).attr('rel');
    $('div.' + toTurnOn).parent().addClass('active');
		$('.work_holderhelper.'+toTurnOn).fadeIn('fast');
    
    
    return false;
  });
	
	
	//Activate Main rotator
	$("#rotator-dot-1").addClass("active");
	intervalId = setInterval ( "rotator()", rotator_interval );
	
	//Activate Secondary rotator
	$("#rotator2-dot-1").addClass("active");
	intervalId2 = setInterval ( "rotator2()", rotator2_interval );
	
	//A main rotator dot was clicked
	$('.rotator-dot').click(function() {
		
		//stop the rotator
		clearInterval(intervalId);	
		intervalId = false;
		
		//get the dot number (stored in the title attr)
		var dotNum = $(this).attr('title');
		
		//make just this dot active
		$(".rotator-dot").removeClass("active");
		$("#rotator-dot-"+dotNum).addClass("active");
		
		//scroll to this item
		rotator_scrollto(dotNum);
		
		//roll down any overlay that was up
		$(".rotator-overlay").animate({ top: "205px"}, "slow");
		$(".rotator-arrow").removeClass("down");
		$(".rotator-arrow").addClass("up");
		
	});
	
	//A Secondary rotator dot was clicked
	$('.rotator2-dot').click(function() {
		
		//stop the rotator
		clearInterval(intervalId2);
		intervalId2 = false;	
		
		//get the dot number (stored in the title attr)
		var dotNum = $(this).attr('title');
		
		//make just this dot active
		$(".rotator2-dot").removeClass("active");
		$("#rotator2-dot-"+dotNum).addClass("active");
		
		//scroll to this item
		rotator2_scrollto(dotNum);
		
	});
	
	//A main rotator item was clicked
	$('.rotator-item').click(function() {
		
		//stop the rotator
		clearInterval(intervalId);
		intervalId = false;	
		
		//get the dot number (stored in the title attr)
		var itemNum = $(this).attr('rel');
		
		//make just this dot active
		$(".rotator-dot").removeClass("active");
		$("#rotator-dot-"+itemNum).addClass("active");
		
		//scroll to this item (in case we were already out of it)
		rotator_scrollto(itemNum);
		
		//toggle the overlay (roll up if down, roll down if up)
		var overlayTop = $("#rotator-overlay-"+itemNum).css('top');
		if(overlayTop == '205px') {
			$("#rotator-overlay-"+itemNum).animate({ top: "180px"}, "slow");
			$("#rotator-arrow-"+itemNum).removeClass("up");
			$("#rotator-arrow-"+itemNum).addClass("down");
		} else {
			$("#rotator-overlay-"+itemNum).animate({ top: "205px"}, "slow");
			$("#rotator-arrow-"+itemNum).removeClass("down");
			$("#rotator-arrow-"+itemNum).addClass("up");
			//resume slideshow
			intervalId = setInterval ( "rotator()", rotator_interval );
		}
				
	});
	
	//A main rotator item was hovered over 
	$('.rotator-item').mouseenter(function() {
		
		//stop the rotator
		clearInterval(intervalId);
		intervalId = false;	
		
		//get the dot number (stored in the title attr)
		var itemNum = $(this).attr('rel');
		
		//make just this dot active
		$(".rotator-dot").removeClass("active");
		$("#rotator-dot-"+itemNum).addClass("active");
		
		//scroll to this item (in case we were already out of it)
		rotator_scrollto(itemNum);
		
		//roll overlay up if down
		var overlayTop = $("#rotator-overlay-"+itemNum).css('top');
		if(overlayTop == '205px') {
			$("#rotator-overlay-"+itemNum).animate({ top: "180px"}, "slow");
			$("#rotator-arrow-"+itemNum).removeClass("up");
			$("#rotator-arrow-"+itemNum).addClass("down");
		} 
				
	});
	
	//A main rotator item was hovered out of
	$('.rotator-item').mouseleave(function() {
		
		//stop the rotator
		//clearInterval(intervalId);
		//intervalId = false;	
		
		//get the dot number (stored in the title attr)
		var itemNum = $(this).attr('rel');
		
		//make just this dot active
		$(".rotator-dot").removeClass("active");
		$("#rotator-dot-"+itemNum).addClass("active");
		
		//scroll to this item (in case we were already out of it)
		//rotator_scrollto(itemNum);
		
		//roll overlay down if up
		var overlayTop = $("#rotator-overlay-"+itemNum).css('top');
		if(overlayTop != '205px') {
			$("#rotator-overlay-"+itemNum).animate({ top: "205px"}, "slow");
			$("#rotator-arrow-"+itemNum).removeClass("down");
			$("#rotator-arrow-"+itemNum).addClass("up");
			//resume slideshow
			intervalId = setInterval ( "rotator()", rotator_interval );
		}
				
	});
	
	
	//A Secondary rotator item was clicked
	$('.rotator2-item').click(function() {	

		//toggle the rotator on/off
		if(intervalId2){
			clearInterval(intervalId2);
			intervalId2 = false;
		} else {
			intervalId2 = setInterval ( "rotator2()", rotator2_interval );
		}
		
		//get the dot number (stored in the title attr)
		var itemNum = $(this).attr('rel');
		
		//make just this dot active
		$(".rotator2-dot").removeClass("active");
		$("#rotator2-dot-"+itemNum).addClass("active");
		
		//scroll to this item (in case we were already out of it)
		rotator2_scrollto(itemNum);
		
	});
	
	
	// v02 About us stuff! -------------------------------------
	
	$('#work-scroller').jcarousel();
	
	$('#photos-rotator').cycle({
		fx: 'scrollRight'
	});

	$('.pics').mouseenter(function() {
		var infoString = $(this).attr('alt');
		$('#about-info p').fadeIn().html(infoString);			
	});
	
	$('.pics').mouseleave(function() {
		$('#about-info p').html('');
	});

  
	$("a.p-cta").fancybox({
		'padding'			:	12,
		'overlayColor'		  :	'#000',
		'enableEscapeButton'  :	true,
		'width'				        : 	875,
		'autoDimensions'	:	true,
		'scrolling'			  :	'no'
	});
	
	
	
	$('.work_holderhelper.welcomejdch').fadeIn('fast');
	$('.job.welcomejdch').parent().addClass('active');
	
	// Click on a carousel work item
	$('.job').click(function() {
		
		$('ul#work-scroller li').removeClass('active');
		$(this).parent().addClass('active');
		
		// Fade Out any other Work Item that is on
		$('.work_holderhelper').hide();
		
		// get the name of the DIV that we are going to show
		var toTurnOn = $(this).attr('rel');
		// And fade it IN
		$('.work_holderhelper.'+toTurnOn).fadeIn('fast');
	});
	
	
});


// MAIN ROTATOR ------------------------------------
	
var rotator_current_item = 1;
var rotator_items = 7;
var rotator_width = 870;
var rotator_interval = 8000;

function rotator() {
	
	$(".rotator-dot").removeClass("active");
	rotator_current_item++;
	$("#rotator-dot-"+rotator_current_item).addClass("active");	
	rotator_scrollto(rotator_current_item);
	if (rotator_current_item==rotator_items) rotator_current_item=0;
	
}

function rotator_scrollto(itemNum) {
	$("#rotator-wrapper").animate({ left: "-" + (rotator_width * (itemNum-1)) +"px"}, "slow");
}


// SECONDARY ROTATOR ------------------------------------
	
var rotator2_current_item = 1;
var rotator2_items = 3;
var rotator2_width = 870;
var rotator2_interval = 16000;

function rotator2() {
	
	$(".rotator2-dot").removeClass("active");
	rotator2_current_item++;
	$("#rotator2-dot-"+rotator2_current_item).addClass("active");	
	rotator2_scrollto(rotator2_current_item);
	if (rotator2_current_item==rotator2_items) rotator2_current_item=0;
	
}

function rotator2_scrollto(itemNum) {
	$("#rotator2-wrapper").animate({ left: "-" + (rotator2_width * (itemNum-1)) +"px"}, "slow");
}



// DATE FORMATTING ----------------------------

/*
 * Date Format 1.2.3
 * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
 * MIT license
 *
 * Includes enhancements by Scott Trenda <scott.trenda.net>
 * and Kris Kowal <cixar.com/~kris.kowal/>
 *
 * Accepts a date, a mask, or a date and a mask.
 * Returns a formatted version of the given date.
 * The date defaults to the current date/time.
 * The mask defaults to dateFormat.masks.default.
 */

var dateFormat = function () {
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date;
		if (isNaN(date)) throw SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};

