/* Global function called by jw player after instantiation */
function playerReady(obj) {
	$('ul.slidernav li').click(function() {
		$('embed').each(function() {
			$(this)[0].sendEvent("PLAY","false");
		});
	});
}

function prettydate(rawdate) {
	rawyear = rawdate.substring(0,4);
	rawmonth = rawdate.substring(5,6);
	rawday = rawdate.substring(7,8);
	
	montharray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	formatteddatetext = montharray[rawmonth] + " " + rawday + ", " + rawyear;
	return formatteddatetext;
} //prettydate


/* === What's Your Story Interaction ================================================= */

function whatsyourstoryinteraction() {

	if ($.browser.msie) {
		$('#storycontainer ul li a').hover(function () {
			$(this).parent('li').stop().animate({'paddingLeft': '10px'},300).siblings('li').children('a');
		}, function () {
			$(this).parent('li').stop().animate({'paddingLeft': '0px'},300).siblings('li').children('a');
		});
	} else {
		$('#storycontainer ul li a').hover(function () {
			$(this).parent('li').stop().animate({'paddingLeft': '10px'},300).siblings('li').children('a').stop().animate({'opacity': '0.4'}, 300);
		}, function () {
			$(this).parent('li').stop().animate({'paddingLeft': '0px'},300).siblings('li').children('a').stop().animate({'opacity': '1'}, 300);
		});
	} //if ie
	
	$('#storycontainer ul li a').click(function () {
		$('body').append('<div class="white-bg"></div>');
		
		var thehref = $(this).attr('href');
		
		$('#storycontainer').animate({'top': '-40px'}, 500).animate({'top': '3000px'}, 2000);
		$('#storycontainer').before('<h2 class="mystory">' + $(this).siblings('span').text() + '</h2>');
		Cufon.replace('h2.mystory', {hover: true, fontFamily: 'glyphabold'});   
		setTimeout(function () {
			$('h2.mystory').animate({'top': '280px'}, {duration: 1000, easing: 'backout'}).animate({'opacity': '1'}, 1000).animate({'opacity': '0'}, 1000);
			/* $('.white-bg').animate({'opacity': '1'}, 2000).fadeIn(500); */
		}, 1000 );
		
		setTimeout(function() {
			window.location.href = thehref;
		}, 4500);

		
		return false;
	}); //click

} //whatsyourstoryinteraction


/* === Grid Setup and Manipulation ================================================= */

function bottomofthewindow() {
	return $(window).height() + $(window).scrollTop() - 20;
}


function gridinitialize() {
	if ($('ul.thegrid').children('li').eq(howfarareweshowing+1).length) {
		if (bottomofthewindow() > ($('ul.thegrid').children('li').eq(howfarareweshowing).offset().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height())) {
			howfarareweshowing = howfarareweshowing + 4;
			$('ul.thegrid').css({'height': $('ul.thegrid').children('li').eq(howfarareweshowing).position().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height() + 2});
			gridinitialize();
		} //if
	} //if
} //gridinitialize


function gridinteraction() {
	
	/* The following was removed in order to fix problems with IE6 - JMD - 2010-05-20 */
	/*
$('ul.thegrid').children('li').each(function() {
		$(this).css({'top': $(this).position().top, 'left': $(this).position().left});
	}); //each

	$('ul.thegrid').children('li').each(function() {
		$(this).css({'position': 'absolute'});
	}); //each
*/
	
	$('ul.thegrid').children('li').children('a').hover(function() {
		$(this).children('span').stop().animate({'right': '257px'}, {duration: 500, easing: 'expoinout'});
	}, function() {
		if ($.browser.msie) {
			$(this).children('span').stop().animate({'right': '0'},10);
		} else {
			$(this).children('span').stop().animate({'opacity': '0'}, {duration: 500, easing: 'linear'}).animate({'right': '0'},10).animate({'opacity': '1'}, 400);
		} //if ie
	}).click(function() { // end hover - start click
		window.location.href = $(this).attr('href') + window.location.hash;
		return false;
	}); //end click
	
	if (window.location.hash) {
		sortVal = window.location.hash.replace('#', '').replace(/%20/g, ' ');
		// build an array of the medium values to see if the hash is a medium or a client
		mediumVals = [];
		$('li.medium select option').each(function() {
			mediumVals.push($(this).val());
		});
		if ( mediumVals.indexOf(sortVal) != -1 ) {  //if the value is in the array select from the medium options
			$('li.medium select option[value='+ sortVal +']').attr({'selected': 'selected'}).change();
		} else { //otherwise select from the client options
			$('li.client select option:contains('+ sortVal +')').attr({'selected': 'selected'}).change();		
		}
	}
		
} //gridinteraction


function gridsorting() {

	$('li.viewall a').click(function() {
		window.location.hash = ''; //clear the sort options
	
		$('ul.thegrid').children('li').each(function(i) {
			$(this).animate({'opacity': '0'}, i*50);
		});//each
		
		setTimeout( function () {
			$('ul.thegrid').children('li').remove();
			$.ajax({
				type: "GET",
				url: "/_includes/workgrid.shtml",
				dataType: "html",
				success: function(pulleddata) {				
					$(pulleddata).find('ul.thegrid').children('li').css({'opacity': '0'}).appendTo('.thegrid');
					gridinteraction();
					$('ul.thegrid').children('li').each(function(i){
						if ($('.showcase').length) {
							$(this).animate({'opacity': '.5'}, i*50);
						} else {
							$(this).animate({'opacity': '1'}, i*50);
						} //if
					}); //each li
					
					Cufon.replace('ul.thegrid li a span', {hover: true, fontFamily: 'glyphabold'});
					
					$('li.client select').children('option').eq(0).attr({'selected': 'selected'});
					$('li.medium select').children('option').eq(0).attr({'selected': 'selected'});
				} //success
			}); //ajax
		
		}, 500 );
		
		return false;
	}); //click


   $('.datesort a').click(function() {

      var mylist = $('ul.thegrid');
      var listitems = mylist.children('li').find('span.date').get();
      listitems.sort(function(a, b) {
         var compA = $(a).text().toUpperCase();
         var compB = $(b).text().toUpperCase();
         return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
      });

      $('ul.thegrid').children('li').each(function(i) {
         $(this).animate({'opacity': '0'}, i*100);
      });

      setTimeout( function () {
         $.each(listitems, function(idx, itm) {
            $('ul.thegrid').append($(itm).parent('a').parent('li'));
            $(itm).parent('a').parent('li').css({'position': 'static'});
            $(itm).parent('a').parent('li').css({'top': $(itm).parent('a').parent('li').position().top, 'left': $(itm).parent('a').parent('li').position().left});
         }); //each
   
         $('ul.thegrid').children('li').each(function() {
            $(this).css({'position': 'absolute'});
         }); //each
   
         $('ul.thegrid').children('li').each(function(i) {
            $(this).animate({'opacity': '1'}, i*100);
         });
      }, 100*$('ul.thegrid li').length );
		return false;
   }); //click


	$('li.client select').change(function() {
		$('li.medium select').children('option').eq(0).attr({'selected': 'selected'});
		window.location.hash = theselected = $(this).children('option:selected').text();
		$('ul.thegrid').children('li').removeClass('special');
		var mylist = $('ul.thegrid');
		var listitems = mylist.children('li').find('span.client').get();
		
		$('ul.thegrid').children('li').each(function(i) {
			$(this).animate({'opacity': '0'}, 500);
			var str = $(this).find('span.client').text();
			isittheclient = str.indexOf(theselected);
			if (isittheclient == -1) {

			} else {
				$(this).addClass('special');
			} //if
		});//each
		
		setTimeout( function () {
			$('ul.thegrid').children('li.special').each(function(i) {
					$(this).prependTo('ul.thegrid');
				}); //each special
				$('ul.thegrid').children('li').each(function(i){
					$(this).css({'position': 'static'});
					$(this).css({'top': $(this).position().top, 'left': $(this).position().left});
				}); //each li
				$('ul.thegrid').children('li').not('.special').each(function(i){
					$(this).animate({'opacity': '.3'}, 500);
				}); //each li
				$('ul.thegrid').children('li.special').each(function(i){
					$(this).removeClass('special');
					$(this).animate({'opacity': '1'}, 500);
			}); //each li
		}, 500 );
		
		return false;
	}); //change


	$('li.medium select').change(function() {
		$('li.client select').children('option').eq(0).attr({'selected': 'selected'});
		window.location.hash = theselected = $(this).children('option:selected').val();
		$('ul.thegrid').children('li').removeClass('special');
		var mylist = $('ul.thegrid');
		var listitems = mylist.children('li').find('span.client').get();
		
		$('ul.thegrid').children('li').each(function(i) {
			$(this).animate({'opacity': '0'}, 500);
			var str = $(this).attr('class');
			isittheclient = str.indexOf(theselected);
			if (isittheclient == -1) {

			} else {
				$(this).addClass('special');
			} //if
		});//each
	
		setTimeout( function () {
			$('ul.thegrid').children('li.special').each(function(i) {
				$(this).prependTo('ul.thegrid');
			}); //each special
			$('ul.thegrid').children('li').each(function(i){
				$(this).css({'position': 'static'});
				$(this).css({'top': $(this).position().top, 'left': $(this).position().left});
			}); //each li
			$('ul.thegrid').children('li').not('.special').each(function(i){
				$(this).animate({'opacity': '.3'}, 500);
			}); //each li
			$('ul.thegrid').children('li.special').each(function(i){
				$(this).removeClass('special');
				$(this).animate({'opacity': '1'}, 500);
			}); //each li
		}, 500 );
	
		return false;

	}); //change

} //gridsorting


function gridlayout() {

	howfarareweshowing = 15;
	//$('ul.thegrid').css({'height': $('ul.thegrid').children('li').eq(howfarareweshowing).position().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height() + 2});
	
	//bottomofthewindow = $(window).height() + $(window).scrollTop() - 120;
	
	gridinitialize();
	gridsorting();
	
	$(window).scroll(function () {
		if ($('ul.thegrid').children('li').eq(howfarareweshowing + 1).length) {		
			/* bottomofthewindow = $(window).height() + $(window).scrollTop() - 120; */
			if (bottomofthewindow() > ($('ul.thegrid').children('li').eq(howfarareweshowing).offset().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height())) {
				//alert("GONE TOO FAR");
				howfarareweshowing = howfarareweshowing + 4;
				$('ul.thegrid').animate({'height': $('ul.thegrid').children('li').eq(howfarareweshowing).position().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height() + 2}, 500);
			} //if
		} //if
	}) //scroll
	.resize(function() {
		if ($('ul.thegrid').children('li').eq(howfarareweshowing + 1).length) {		
			/* bottomofthewindow = $(window).height() + $(window).scrollTop() - 120; */
			if (bottomofthewindow() > ($('ul.thegrid').children('li').eq(howfarareweshowing).offset().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height())) {
				//alert("GONE TOO FAR");
				howfarareweshowing = howfarareweshowing + 4;
				$('ul.thegrid').animate({'height': $('ul.thegrid').children('li').eq(howfarareweshowing).position().top + $('ul.thegrid').children('li').eq(howfarareweshowing).height() + 2}, 500);
			} //if
		} //if
	}); //resize
	
	$('span.date').each(function(){
		date = prettydate($(this).text());
		$(this).after('<span class="formatteddate">' + date + '</span>');
	}); //each date

} //gridlayout


/* === Showcase ================================================= */

function showcaseinteraction() {
	$('ul.slidernav').children('li').click(function() {
		whichdidiclick = $(this).parent('ul').children('li').index($(this));
		$(this).addClass('active').siblings('.active').removeClass('active');
		// $('ul.slider').animate({'left': -$('ul.slider').children('li').eq(whichdidiclick).position().left}, {duration: 750, easing: 'expoinout'}); /* Original code for absolute positioning */
		$('ul.slider').stop().animate({'marginLeft': -whichdidiclick*($('.slideshow').width())}, {duration: 750, easing: 'expoinout'});
		return false;
	}); //click
} //showcaseinteraction


function showcaseinitialize() {
	$('.showcase').append('<ul class="slidernav"></ul>');
	$('ul.slider').children('li').each(function() {
		$(this).clone().appendTo('.slidernav');
	}); //each
	
	$('ul.slidernav').children('li').eq(0).addClass('active');
	
	$('ul.slidernav').find('.flashcontainer').each(function() {
		flashvars = $(this).find('embed').attr('flashvars');
		start = flashvars.indexOf('image=');
		end = flashvars.indexOf('&back');
		src = flashvars.substring(start+6, end);
		$(this).after('<img src="' + src + '" alt="Park&amp;Co. Portfolio Piece" />').remove();
	});
	
	if ($('ul.slider').find('li').length == 1) {
		$('ul.slidernav').hide();
		$('ul.showcasemetameta').next('h3').hide();
	} // if loner
	
	showcaseinteraction();
} //showcaseinitialize

/*
$('.slideshow').find('img').ready(function() {
   $('.slideshow').css({'height': $('.slideshow').find('img').height()});
});
*/


function dimthelights() {
	$('.gridcontainer').addClass('dimlights');
} //dimthelights


/* === Services ================================================= */
function loadServicesContent() {
	$('.services .stronglinks h4 a:not(.active)').each(function() {
		$(this).parent().after('<div class="service-content"></div>');
	}).hover(function() {
		$(this).stop().animate( {paddingLeft: '8px'}, 150 );
	}, function() {
		$(this).stop().animate( {paddingLeft: '0'}, 150 );
	});
	$('.services .stronglinks h4 a:not(.active)').live('click', function() {
		$('.active').removeClass('active').parent().next('div').slideUp(200);
		$(this).addClass('active').parent().next('div').slideDown(200);
		return false;
	});
	$('.services .stronglinks h4 a.active').live('click', function() {
		return false;
	});
	$('.service-content').each(function() {
		$(this).load($(this).prev('h4').find('a').attr('href') + ' .major', function() {
			Cufon.replace('a.stronglink', {hover: true, fontFamily: 'glyphabold'});
		});
	});
	
	//If loading with an anchor, open that section
	if (window.location.hash) {
		$('a#'+window.location.hash).click();
	}
}


/* === Subnavigation ================================================= */
function subnavigation(){
	$('body:not(.services) ul#navigation li.services').hover(function(){
		$('ul.subnavigation').show();
		$('body').css({'backgroundImage': 'url(/_images/60gradient.png)'});
	}, function() {
		$('ul.subnavigation').hide();
		$('body').css({'backgroundImage': 'url(/_images/30gradient.png)'});
	}); //hover
} //subnavigation


/* === Grey lines to right-most edges ================================================= */

function extender() {
	$('#worldcontainer').before('<div class="extender"></div>');
	$('#worldcontainer').after('<div class="extender2"></div>');
} //extender


/* === Calling necessary functions after DOM ready ==================================== */
$(document).ready(function() {

	if ($.browser.msie && $.browser.version < 8) {
		Cufon.replace('#entries h2, #header h1, #tagline h3, h2, #content h4, .emphasis, ul.thegrid li a span, ul.gridsort li span, ul.gridsort li a, a.stronglink, h3, ul.services-subnav a, .showcasemeta ul a', {hover: true, fontFamily: 'glyphabold'});
	} else {
		Cufon.replace('#entries h2, #header h1, #tagline h3, h2, #content h4, .emphasis, ul.thegrid li a span, ul.gridsort li span, ul.gridsort li a, #navigation li a, a.stronglink, h3, ul.services-subnav a, .showcasemeta ul a', {hover: true, fontFamily: 'glyphabold'});
	}; //ifie7
	
	$('body#landing h1, ul#navigation li.home, div.gridcontainer li').supersleight();
		
	whatsyourstoryinteraction();
	if ($('li.client select').length) {
		$('li.client select').children('option').eq(0).attr({'selected': 'selected'});
	} // if client exists
	if ($('li.medium select').length) {
		$('li.medium select').children('option').eq(0).attr({'selected': 'selected'});
	} // if medium exists
	if ($('.thegrid').length) {
		if (!($.browser.msie && $.browser.version.substr(0,1)<7)) {
			gridlayout();
			gridinteraction();
		}
	} // if imagecontainer exists
	if ($('.showcase').length) {
		showcaseinitialize();
		dimthelights();
	} // if imagecontainer exists
	//subnavigation();
	extender();
	loadServicesContent();
});
