// DO NOT REMOVE THE COMMENT BLOCK BELOW THIS LINE //
/*global addEventListener: false, blur: false, clearInterval: false, clearTimeout: false, close: false, closed: false, defaultStatus: false,  
document: false, event: false, focus: false, frames: false, getComputedStyle: false, history: false, Image: false, length: false, location: false,  
moveBy: false, moveTo: false, name: false, navigator: false, onblur: true, onerror: true, onfocus: true, onload: true, onresize: true,  
onunload: true, open: false, opener: false, Option: false, parent: false, print: false, resizeBy: false, resizeTo: false, screen: false,  
scroll: false, scrollBy: false, scrollTo: false, setInterval: false, setTimeout: false, status: false, top: false, XMLHttpRequest: false,  
hs: false, $: false, VideoJS: false, $f: false, window: false, iScroll: false, videoJSPlayers: false,  */
// DO NOT REMOVE THE COMMENT BLOCK ABOVE THIS LINE //

$(document).ready(function() {
	
	var deviceAgent = navigator.userAgent.toLowerCase();
	if (deviceAgent.match(/(ipad)/)) {
		if ($('#scroller').length > 0) {
			document.addEventListener('touchmove', function(e){ e.preventDefault(); });
			var myScroll = new iScroll('scroller');
		}
		
		// raising longer headlines
		var headerheight, headerlineheight, headeroffset;

		if ($('#content .headingtitle').length) {
			headerheight = $('#content .headingtitle').height();
			headerlineheight = $('#content .headingtitle').css("line-height").replace('px','');
			headeroffset = (parseInt(headerheight, 10) / parseInt(headerlineheight, 10));
			if(headeroffset > 1){
				headeroffset = ((((headeroffset * headerlineheight)) + 10) * (-1));
				$('#content .headingtitle').css('top',headeroffset);
			}
		}
		
	} else if (deviceAgent.match(/(iphone|ipod)/)) {
		//touch gallery
		$('.projectgallery a.mediaimage').touchGallery();
	} else {
		$('.projectdesc').jScrollPane({
			dragMaxHeight: 75, 
			dragMinHeight: 75, 
			scrollbarWidth: 25
		});
		$('.pagetext').jScrollPane({
			dragMaxHeight: 75, 
			dragMinHeight: 75, 
			scrollbarWidth: 25
		});
		
		// raising longer headlines
		var headerheight, headerlineheight, headeroffset;

		if ($('#content .headingtitle').length) {
			headerheight = $('#content .headingtitle').height();
			headerlineheight = $('#content .headingtitle').css("line-height").replace('px','');
			headeroffset = (parseInt(headerheight, 10) / parseInt(headerlineheight, 10));
			if(headeroffset > 1){
				headeroffset = ((((headeroffset * headerlineheight)) + 10) * (-1));
				$('#content .headingtitle').css('top',headeroffset);
			}
		}
	}
	
	if (deviceAgent.match(/(iphone|ipod)/)){
		$(".banner").fadeIn().cycle({
			timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
			speed:         1000,  // speed of the transition (any valid fx speed value) 
			next:          null,  // id of element to use as click trigger for next slide 
			prev:          null,  // id of element to use as click trigger for previous slide 
			before:        null,  // transition callback (scope set to element to be shown) 
			after:         null,  // transition callback (scope set to element that was shown) 
			height:       'auto', // container height 
			sync:          1,     // true if in/out transitions should occur simultaneously 
			fit:           0,     // force slides to fit container 
			pause:         1,     // true to enable "pause on hover" 
			delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
			slideExpr:     null   // expression for selecting slides (if something other than all children is required)
		});
	}
	
	//detecting internet explorer
	var msie = deviceAgent.match(/(msie)/);


	

	
	if ($('.jumpnav').length > 0) {
		headerlineheight = $('#content .headingtitle').css("top").replace('px','');
		headeroffset = (parseInt(headerlineheight, 10) + 10);
		$('#content .headingtitle').css('top',headeroffset);
	}

	
	// apply the carousel to image gallery block, except for those belonging to projects.
	if ($(".mediablock:not(.projectgallery .mediablock) li").size() > 4) {
		$("div.mediablock").carousel({
			dispItems: 4, 
			animSpeed: 1000, 
			pagination:true, 
			paginationPosition:'outside', 
			blockType:"li",
			addressIdentifier:'images'
		});
	}
	
	// Add captions to images in the 'creative team' section
	$(".people").children('.center-wrap').after('<p class=\"imgcaption\"></p>');
	$(".people a").each(function(){
		var imagetitle = $(this).attr('title');
		$(this).hover(
			function() {
				$('.imgcaption').html(imagetitle);
			}, 
			function() {
				$('.imgcaption').html("");
			}
		);
	});
	
	// hide images, video so that only synopsis is shown when a project page first loads
	$(".gallery").children('.center-wrap').after('<p class=\"imgcaption\"></p>');
	
	$('#project-detail').parent('html').css('overflow','hidden');
	
	// toggle image, video, synopsis blocks when the appropriate links are clicked
	$("a.texttoggle").addClass('active').addClass('activebox');
	$("p.jumpnav a:not('.website')").each(function(){
		var sectiontitle = $(this).attr('title');
		$(this).click(function() {
			$(".activebox").fadeOut('normal', function(){
				$("p.jumpnav a").removeClass('active');
				$(this).addClass('active');
				$("."+sectiontitle).fadeIn('normal', function() {
					if (sectiontitle == 'projectgallery') {
						$('.mediablock').removeAttr('style');
					}

					if (sectiontitle == 'projectdesc') {
						$('.jScrollPaneContainer').fadeIn();
					}

					if (sectiontitle == 'projectvideo') {
						$('#content').css("bottom", "-135px");
						var videoposition = Math.round(((parseInt($(window).height(), 10)/2) + 188) * -1);
						$('.projectvideo').css("top", videoposition);
						
						// setup the video player
						if ($('.vjs-controls').length === 0){
							var videoJSPlayers = VideoJS.setup("All");
							
							if (videoJSPlayers.length > 0) {
								videoJSPlayers[0].onPlayButtonClick();
							}
						}
						if ($f()){
							$f().onLoad(function(){
									$f().play();
								}
							);
						}
					} else {
						$('#content').css("bottom", "30px");
					}

					if (sectiontitle == 'projectgallery') {
						// Apply the 'carousel' paginator for blocks of images
						if ($('.mediablock.js').length === 0) {
							if ($(".mediablock li").size() > 4) {
								$("div.mediablock").carousel({
									dispItems: 4, 
									animSpeed: 1000, 
									pagination:true, 
									paginationPosition:'outside', 
									blockType:"li",
									addressIdentifier:'images'
								});
							}
						}
					}
					
				}).addClass('activebox');
				$('.jScrollPaneContainer').hide();
				
				return false;
			});
		});
	});
	


	if ($(".projectdesc .column").size() > 100) {
		$(".projectdesc").carousel({
				effect: "fade", 
				animSpeed: 1000, 
				pagination:true, 
				prevBtnInsert: "appendTo", 
				nextBtn:"<a role=\"button\">></a>", 
				prevBtn:"<a role=\"button\"><</a>", 
				blockType:"div.column", 
				addressIdentifier:'page'
		});
	}

	if ($(".pagetext .column").size() > 100) {
		$(".pagetext").carousel({
			effect: "fade", 
			animSpeed: 1000, 
			pagination:true, 
			prevBtnInsert: "appendTo", 
			nextBtn:"<a role=\"button\">></a>", 
			prevBtn:"<a role=\"button\"><</a>", 
			blockType:"div.column", 
			addressIdentifier:'page'
		});
	}
	
	// project image replacement
	var projectImages = [];
	if (!deviceAgent.match(/(iphone|ipod)/)) {
		$('a.mediaimage').each(function() {
			var imagetitle = $(this).attr('title');
			var imageFile = $(this).attr('href');
			projectImages.push(imageFile);
			$(this).click(function() {
				$(this).append("<div class=\"loading\"></div>");
				$.backstretch(imageFile, {speed: 550}, function() {
					$('.imgcaption').html(imagetitle);
					$('.loading').remove();
				});
				return false;
			});
		});
	}
	
	if (!(deviceAgent.match(/(iphone|ipod|ipad)/)) && !(msie)) {
		$('a.mediaimage').click(function() {
			$('.mediablock').addClass('opaqued').fadeTo("slow", 0.23);
			return false;
		});
		
		$('.mediablock.opaqued').live('mouseenter', function () {
			$(this).fadeTo('fast', 1, function() {
				$('.mediablock').removeAttr('style');
			});
			return false;
		}).live('mouseleave', function () {
			$(this).unbind('mousemove');
			$(this).fadeTo("fast", 0.23);
			return false;
		}).live('mousemove', function () {
			if ($(this).css('opacity') < 1) {
				$(this).fadeTo(25, 1, function(){
					$('.mediablock').removeAttr('style');
					$(this).unbind('mousemove');
				});
				return false;
			}
		});
		
		$('.mediablock.opaqued a.mediaimage').live('mouseleave', function () {
			$('.mediablock.opaqued').unbind('mousemove');
		});
	
		$('p.jumpnav a').click(
			function(){
			$('.mediablock').removeClass('opaqued').fadeTo("fast", 1);
		});
	}
	// open new window for ext links
	$("a").filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).attr('target', '_blank');
	
	// home page video: when it's done playing, fade out the intro video and fade in the carousel
	if ($('body').attr('id') == "home") {
		//repositioning the video/banner
		if(parseInt($(window).height(), 10) > 690){
			$('.introvideo').css("top", "106px");
		}
		
		// center home box
		var pagewidth, positionvalue;
		boxpercentage = ((640 / parseInt($(window).width(), 10))*100);
		positionvalue = ((100-boxpercentage)/2);
		positionvalue = Math.round(positionvalue)+"%";
		$('#content').css('left',positionvalue);
		
		if ($.cookie("pdft.introVideoViewed")){
			$(".video-js-box").remove();
			$(".banner").fadeIn().cycle({
				timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
				speed:         1000,  // speed of the transition (any valid fx speed value) 
				next:          null,  // id of element to use as click trigger for next slide 
				prev:          null,  // id of element to use as click trigger for previous slide 
				before:        null,  // transition callback (scope set to element to be shown) 
				after:         null,  // transition callback (scope set to element that was shown) 
				height:       'auto', // container height 
				sync:          1,     // true if in/out transitions should occur simultaneously 
				fit:           0,     // force slides to fit container 
				pause:         1,     // true to enable "pause on hover" 
				delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
				slideExpr:     null   // expression for selecting slides (if something other than all children is required)
			});
		}
		
		$("video").bind("ended", function(){
			$(".video-js-box").fadeOut('normal', function() {
				$(".banner").fadeIn().cycle({
					timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
					speed:         1000,  // speed of the transition (any valid fx speed value) 
					next:          null,  // id of element to use as click trigger for next slide 
					prev:          null,  // id of element to use as click trigger for previous slide 
					before:        null,  // transition callback (scope set to element to be shown) 
					after:         null,  // transition callback (scope set to element that was shown) 
					height:       'auto', // container height 
					sync:          1,     // true if in/out transitions should occur simultaneously 
					fit:           0,     // force slides to fit container 
					pause:         1,     // true to enable "pause on hover" 
					delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
					slideExpr:     null   // expression for selecting slides (if something other than all children is required)
				});
				$.cookie("pdft.introVideoViewed", true, { expires: 1 });
			});
		}, false);
		if ($f()) {
			$f().onFinish(function() {
				$(".video-js-box").fadeOut('normal', function() {
					$(".banner").fadeIn().cycle({
						timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
						speed:         1000,  // speed of the transition (any valid fx speed value) 
						next:          null,  // id of element to use as click trigger for next slide 
						prev:          null,  // id of element to use as click trigger for previous slide 
						before:        null,  // transition callback (scope set to element to be shown) 
						after:         null,  // transition callback (scope set to element that was shown) 
						height:       'auto', // container height 
						sync:          1,     // true if in/out transitions should occur simultaneously 
						fit:           0,     // force slides to fit container 
						pause:         1,     // true to enable "pause on hover" 
						delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
						slideExpr:     null   // expression for selecting slides (if something other than all children is required)
					});
				});
				$.cookie("pdft.introVideoViewed", true, { expires: 1 });
			});
		}
	}
	
	
});

window.onload = function() {
	if ($('body').attr('id') != 'project-detail') {
		VideoJS.setup();
	}
};
