/* Author: 

*/


$(document).ready(function() {
  
		// Directors Popup Handler

		$('#directors-button, #close-directors, #lightbox').click(function(e) {
			e.preventDefault();
		  	$('#directors-popup').fadeToggle(600);
			$('#lightbox').fadeToggle('600', function() {

				var bodyHeight = $('html').height();
				var lightboxHeight = bodyHeight - 150;

				$(this).css("height",lightboxHeight);

          });

		});
		
		$(".load-posts-button").bind('click', function(e) {
			e.preventDefault();
			var skip = $(this).attr('id').substr(11);
			$.get('/news-archive-ajax.html?skip='+skip, function(ret) {
				$("#news-archive-list").append(ret);
				(function () {
			        var s = document.createElement('script'); s.async = true;
			        s.type = 'text/javascript';
			        s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
			        (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
			    }());
				
				var skip = $('.load-posts-button').attr('id').substr(11);
				skip = parseInt(skip) + 8;
				$('.load-posts-button').attr('id', 'load-posts-'+skip.toString());
			});
		});


		function initializeSlideshow() {
			//slideshow.width(slideshow.find('li').length * slideWidth);
			//slideshow.width(4000);
			resizeSlideshow();
		}
	
		function resizeSlideshow() {
			var winWidth = win.width();
			if (winWidth < 1000) {
			 winWidth = 1000;
			}
			var currentMid = (slideWidth + .5*slideWidth) - winWidth/2;
			slider.width(winWidth);
			slider.css('margin-left', -1 * (winWidth - cntr.width())/2);
			slideshow.css('left', -1 * (3*slideWidth - winWidth)/2);
			leftPanel.css('left', -1 * (3*slideWidth - winWidth)/2 + 20);
			rightPanel.css('right', -1 * (3*slideWidth - winWidth)/2 + 20);
			leftControl.css('left', slideWidth - currentMid - 5);
			rightControl.css('right', slideWidth - currentMid - 5);
		}
		
		function removeSlide(dir) {
			if(dir == 'prev') {
				slideshow.find('li').eq(3).remove();
			}
			else {
				slideshow.css('left','+='+slideWidth);
				slideshow.find('li').eq(0).remove();
			}
			inMotion = false;
			
			setSliderTimer();
		}
	
		function processSlide(moveTo, dir, ret) {
			var oldSlide = 1,
				newSlide = 2;
			if(dir == 'prev') {
				oldSlide = 2;
				newSlide = 1;
			}
			if(slideshow.children('li').eq(oldSlide).find('.slide-meta').length != 0) {
				if(slideshow.children('li').eq(oldSlide).find('.preview-image').length != 0) {
					slideshow.children('li').eq(oldSlide).find('.preview-image, .play-button').fadeIn('fast', function() {
						slideshow.children('li').eq(oldSlide).find('.vimeo-cntr').html('');
					});
				}
				slideshow.children('li').eq(oldSlide).find('.slide-meta').fadeOut('slow', function() {
					slideshow.animate({
						left: moveTo
					}, 'slow', 'swing', function() {
						if(slideshow.children('li').eq(newSlide).find('.slide-meta').length != 0) {
							slideshow.children('li').eq(newSlide).find('.slide-meta').fadeIn('fast');
						}
						removeSlide(dir);
					});
				});
			}
			else {
				slideshow.animate({
					left: moveTo
				}, 'slow', 'swing', function() {
					if(slideshow.children('li').eq(newSlide).find('.slide-meta').length != 0) {
						slideshow.children('li').eq(newSlide).find('.slide-meta').fadeIn('fast');
					}
					removeSlide(dir);
				});
			}
			
		};
	
		function animateSlide(dir) {
		  clearSliderTimer();
		
			var moveTo;
			var oldSlide = currentSlide;
			var postSlide;
			inMotion = true;
			if(dir == 'prev') {
				moveTo = '+='+slideWidth;
				currentSlide--;
				if(currentSlide < 1) {
					currentSlide = numBillboard;
				}
				postSlide = currentSlide - 2;
				if(postSlide < 0) {
					postSlide = numBillboard-1;
				}
			
			} 
			else {
				moveTo = '-='+slideWidth;
				currentSlide++;
				if(currentSlide > numBillboard) currentSlide = 1;
				postSlide = currentSlide;
				if(postSlide == numBillboard) {
					postSlide = 0;
				}
			}
		
			$.post('/ajax/slide.php', {num: postSlide}, function(ret) {
				if(dir == 'prev') {
					//slideshow.css('left', "-="+slideWidth).prepend(ret);
					slideshow.find('li:eq(0)').remove();
					slideshow.prepend(ret);
				} else {
					slideshow.append(ret);
				}
				$(".play-button").bind('click', function() {
					playVideo($(this));
				});
			
			});
		
			if(dir=="prev") {
				slideshow.css('left', "-="+slideWidth).prepend("<li><div class='slide'></div></li>");
			}
		
			processSlide(moveTo, dir);
		
		}
		
		function setSliderTimer() {
		  $(document).oneTime(10000, 't', function() {
		    animateSlide('next');
		  });
		}
		function clearSliderTimer() {
		  $(document).stopTime('t');
		}

		if($("#slider").length != 0) {
			
			// Slider handler
			var slider = $("#slider"),
				win = $(window),
				cntr = $("#container"),
				slideshow = $("#slideshow"),
				slideWidth = $("#slideshow li").outerWidth(true),
				leftPanel = $("#left-panel"),
				rightPanel = $("#right-panel"),
			 	leftControl = $("#previous-slide"),
				rightControl = $("#next-slide"),
				inMotion = false;
	
		
			win.resize(function() {
				resizeSlideshow();
			});
		
			initializeSlideshow();

			$('.prev').bind('click', function() {
				if(!inMotion) animateSlide('prev');
			});

			$('.next').bind('click', function() {
				if(!inMotion) animateSlide('next');
			});
			
			slider.find('img').bind('load', function() {
				$("#blanket").fadeOut(600, function() {
				    setSliderTimer();
				});
				
				
			});
			if($.browser.msie) {
				$("#blanket").delay(1000).fadeOut(function() {
				    setSliderTimer();
				});
		  }
		  
		  $(document).oneTime(4000, function() {
		    $("#blanket:visible").fadeOut();
		  })
			
		}

		
		// Credits Popup Handler

		$('.agency-credits, .close-credits').bind('click', function() {

		  	$(this).parents('.video-cntr').find('.video-credits-panel').fadeToggle(600);
			return false;

		});
		
		/* Contact Map */
		function initialize_map() {
		    var latlng = new google.maps.LatLng(33.997799,-118.47784);
			var myStyles = [
		    {
		      stylers: [
		        { saturation: -100 },
		      	{ gamma: 0.7 }
		      ]
		    }
		  ];
		    var myOptions = {
		      zoom: 16,
		      center: latlng,
			  disableDefaultUI: true,
		      mapTypeId: google.maps.MapTypeId.ROADMAP
		    };

		    var map = new google.maps.Map(document.getElementById("map_canvas"),
		        myOptions);

			map.setOptions({styles: myStyles});

			var image = 'images/contact/map-marker.png';
		    var myLatLng = new google.maps.LatLng(33.996799,-118.47784);
		    var beachMarker = new google.maps.Marker({
		        position: myLatLng,
		        map: map,
		        icon: image
		    });

		  }
		
		if($("#map_canvas").length != 0) {
			initialize_map();
		}
		
		$(".reps-div").bind('mouseenter', function() {
			var id = $(this).attr('id').substr(5);
			$("#map-"+id).fadeIn();
		});
		
		$(".reps-div").bind('mouseleave', function() {
			var id = $(this).attr('id').substr(5);
			$("#map-"+id).fadeOut();
		});

		/* News */
		
		var news_hovers = $(".news-item a")
			
		news_hovers.bind('mouseenter mouseleave', function() {
			$(this).parents('.news-item').children('.news-item-image-cntr').toggleClass('hover');
		});
		
		
		$(".play-button").bind('click', function() {
			playVideo($(this));
		});
		
		function playVideo(el) {
		  $(document).stopTime('t');
			var parent = el.parent('.video-slide');
			parent.find('.vimeo-cntr').html('<iframe class="vimeo-player" src="http://player.vimeo.com/video/'+el.attr('id')+'?title=0&amp;byline=0&amp;portrait=0&color=e18223&autoplay=1" width="940" height="523" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
			parent.find('.play-button, .preview-image').fadeOut();
			parent.siblings('.slide-meta').hide();
			parent.find('.vimeo-cntr').fadeIn();
		}
		
		var videoNav,
			currentNavPage,
			numNavPages;
		
		if($("#video-nav-cntr").length != 0) {
			
			videoNav = $("#video-nav-cntr");
			currentNavPage = 0;
			numNavPages = Math.floor(videoNav.find('li').length/8);
		
			$(".video-scroller").bind('click', function() {
				var dir = $(this).attr('id').substr(10);
				if(dir == 'next' && currentNavPage < numNavPages) {
					if(currentNavPage == 0) {
						$("#video-nav-previous").removeClass('no-go');
					}
					currentNavPage++;
					videoNav.animate({
						left: -1 * videoNav.find('li').eq(currentNavPage * 8).position().left - 15
					});
					if(currentNavPage == numNavPages) {
						$("#video-nav-next").addClass('no-go');
					}
				}
				else if(dir == 'previous' && currentNavPage > 0) {
					if(currentNavPage == numNavPages) {
						$("#video-nav-next").removeClass('no-go');
					}
					
					currentNavPage--;
					var position = 0;
					if(currentNavPage > 0) {
						position = -1 * videoNav.find('li').eq(currentNavPage * 8).position().left - 15;
					}
					
					videoNav.animate({
						left:position
					});
					
					if(currentNavPage == 0) {
						$("#video-nav-previous").addClass('no-go');
					}
				}
				
			});
			
			/* Directors Video */
			$("#video-slider-cntr").cycle({
				fx: 'scrollHorz',
				next: '#video-next',
				prev: '#video-previous',
				speed: 700,
				timeout: 0,
				after: onAfter,
				before: onBefore
			});

			$(".video-nav-row li").bind('click', function() {
				var index = $(this).index();
				$("#video-slider-cntr").cycle(index);
				$('.video-nav-row li.selected').removeClass('selected');
				$(this).addClass('selected');
			});
			
			$(".now-playing").css('top', (videoNav.find('li').height()-18)/2);
		}
		
		function adjustControlClass() {
			if(currentNavPage ==  0) {
				$("#video-nav-previous").addClass('no-go');
			}
			if(currentNavPage > 0 && $("#video-nav-previous").hasClass('no-go')) {
				$("#video-nav-previous").removeClass('no-go');
			}
			if(currentNavPage == numNavPages) {
				$("#video-nav-next").addClass('no-go');
			}
			if(currentNavPage < numNavPages && $("#video-nav-next").hasClass('no-go')) {
				$("#video-nav-next").removeClass('no-go');
			}
			
		}
		
		function onAfter(currSlideElement, nextSlideElement, opts) {
			var currEl = $(currSlideElement);
			currEl.find('.vimeo-cntr').html('');
			currEl.find('.play-button, .preview-image').show();
			$("#video-nav-cntr li").removeClass('selected').eq(opts.currSlide).addClass('selected');
			var lo = currentNavPage * 8,
				hi = currentNavPage * 8 + 7;
				
			if(opts.currSlide < lo || opts.currSlide > hi) {
				currentNavPage = Math.floor(opts.currSlide/8);
				var position = 0;
				if(currentNavPage > 0) {
					position = -1 * videoNav.find('li').eq(currentNavPage * 8).position().left - 15;
				}
				videoNav.animate({
					left: position
				});
				adjustControlClass();
			}
			$(nextSlideElement).find('.current-video-info').fadeIn();
		}
		
		function onBefore(currSlideElement, nextSlideElement) {
			$(currSlideElement).find('.current-video-info').fadeOut();
		}
		
		/* Permalink */
		var app = $.sammy(function() {
			
			this.get('#/video/:id', function() {
		    	
				var el = $("#thumb_"+this.params['id']);

		    	index = el.index();

		    	$("#video-slider-cntr").cycle(index);
		    });
		});

		$(function() {
			app.run()
		});
// External Links
region.rel_external = function () {
    
    var $links = $('a:[rel*=external]');
    
    $links.live('click', function () {
        
        window.open(this.href);
        
        return false;
    });
};
		
});
    

