$(function(){
	$("html").addClass("JS");
	
	/* Nav */	
	$("#header .nav ul").each(function(){
		var list = $(this);
		var size = 3;
		var current_size = 0;
		list.children().each(function(){
			if (++current_size > size) {
				var new_list = $("<ul></ul>").insertAfter(list);
				list = new_list;
				current_size = 1;
			}
			list.append(this);
		});
	});
	
	$("#header .nav ul:nth-child(2)").addClass("left");
	$("#header .nav ul:nth-child(3)").addClass("right");
	
	/* Items */
	$("#home .items li:nth-child(2n)").addClass("nth-child-2n");
	$("#ourWork .items li:nth-child(4n)").addClass("nth-child-4n");
	$("#caseStudy .items li:nth-child(2n)").addClass("nth-child-2n");
	$("#ourCompany .items li:nth-child(2n)").addClass("nth-child-2n");
	
	$(".column:nth-child(3n)").addClass("nth-child-3n");
	$(".column:nth-child(3n+1)").addClass("nth-child-3nplus1");
	$(".column:last-child").addClass("last-child");
	
	$(".items").each(function() {
		var elem = $(this);
		if (elem.children("li").length % 2 !== 0) {
			elem.find("li:last-child").addClass("nth-last-of-type-1");
		} else {
			elem.find("li:last-child").addClass("nth-last-of-type-1");
			elem.find("li:last-child").prev("li").addClass("nth-last-of-type-2");
		}
	});
		
	/* Promo Action */
	$("#caseStudy #promo .action li:first-child").addClass("first-child");
	
	$("#caseStudy .subsection .content span").each(function(){
		if ($(this).css("font-style") === 'italic') {
			$(this).css("color","#ffffff");
		}
	});
	
	function promoBefore() {
  		$(".before").toggleClass('active');
  		$(".after").toggleClass('active');
  		$("#caseStudy .carousel li").toggleClass('dN');
		$(".after").click(promoAfter);
		$(".before").unbind('click');
	}
	
	function promoAfter() {
  		$(".before").toggleClass('active');
  		$(".after").toggleClass('active');
  		$("#caseStudy .carousel li").toggleClass('dN');
		$(".before").click(promoBefore);
		$(".after").unbind('click');
	}

	$(".before").click(promoBefore);
		
	/* Profiles */
	
	$(".profiles li:nth-child(3n)").addClass("nth-child-3n");
	$(".subnav li:first").addClass("first-of-type");
	$(".subsection .content:last-child").addClass("last-child");
	
	$(".subsection .content p:last-child").addClass("last-child");
	
	/* Inputs */
	
	$("#googleMaps").each(function() {
	    var defaultValue = $(this).attr("value");
	
	    $(this).bind('focus', { defaultValue: defaultValue }, function(e) {
	        if ($(this).attr("value") === defaultValue) { 
	        	$(this).attr("value", "");
	    	}
	    });
	
	    $(this).bind('blur', { defaultValue: defaultValue }, function(e) {
	        if ($(this).attr("value") === "") {
	        	$(this).attr("value", defaultValue);
	        }
	    });
	});
	
	/* Colorbox ;*/
	$("a[rel='image']").colorbox({rel:'nofollow'});
	$("a[rel='services']").colorbox({innerWidth:978, innerHeight:605, width:978, height:605, initialWidth:978, initialHeight:605,scrolling:false});
	$("a[rel='services-thumbs']").colorbox({innerWidth:978, innerHeight:705, width:978, height:705, initialWidth:978, initialHeight:705,scrolling:false});
	$("a[rel='timeline']").colorbox({photo:true,scrolling:false,scalePhotos:false,innerWidth:978, innerHeight:605, width:978, height:605, initialWidth:978, initialHeight:605, 
					onComplete:function() { 
						$("#cboxLoadedContent").css("overflow-x","scroll"); 
					}
	});
	
	$("a[rel='film']").colorbox({iframe:true, innerWidth:802, innerHeight:473});
	$("a[rel='casestudy-film']").colorbox( { 
					onOpen:function() { 
						$("body").addClass('videoOverlay'); 
					},
					onClosed:function() { 
						$("body").removeClass('videoOverlay'); 
					}
	});
	
	$('a.videobox').colorbox({iframe:true,innerWidth: '600px', innerHeight: '337px'});
	
	
	//Add tipsy tooltips
	$('.tipsyimg').tipsy();
	
	
	//Look for promo carousels
	$(".imgcarousel .wrapper").promoCarousel();
		
});

/* Carousel */
var slideSet = 0;

function pagerFactoryNumbers(idx, slide) {
    return '<li><a href="#">'+(idx+1)+'</a></li>';
}

function pagerFactoryThumbs(idx, slide, total) {	
	return '<li><a href="#"><img src="'+thumbs['thumb'+(idx)+'']+'" alt="'+thumbs['thumb'+(idx)+'name']+'" /></a></li>';
}

function loadImage(id){
	if ($('#'+id+'').find('img').attr('src') === undefined) {
		var img = new Image();   			
		$(img).load(function () {
			$(this).hide();
			$('#'+id+'').find('h3').after(img);	
			$(this).fadeIn();
		}).error(function () {	
		}).attr('src', slides[slideSet][id]);
	}
}

function loadNext(currSlideElement, nextSlideElement, opts, fwd){                
	var nextId = $(nextSlideElement).attr('id').match(/[0-9]+/);
	var loadSlideId = '';
	
	if ($(nextSlideElement).find('img').attr('src') === undefined) {
		loadImage('slide'+nextId);
	}
	if (fwd){
		loadSlideId = (nextId  + 1);
		
		if ((loadSlideId) > opts.slideCount){
			return;
		}
	}else {
		if (nextId === 1) {
			loadSlideId = opts.slideCount;
		}else {
			loadSlideId = (nextId  - 1);
		}
	}
	loadImage('slide'+loadSlideId);		
}

function loadNextCasestudy(currSlideElement, nextSlideElement, opts, fwd) {
	var nextId = $(nextSlideElement).attr('id').match(/[0-9]+/);
	$.cookies.set('caseStudy',''+nextId+'');
	loadNext(currSlideElement, nextSlideElement, opts, fwd);
}

function scrollUp() {
	$('html, body').animate({scrollTop:0}, 'slow'); 
}

/* sIFR  */
var paralucentLight = { src: '/wp-content/themes/jkr-theme-2011/swf/sIFR-paralucentLight.swf' };
	
sIFR.activate(paralucentLight);
	
sIFR.replace(paralucentLight, {  
  selector: '#designGazette h2'  
  ,css: [  
       'a { background-color: #ffffff; color: #000000; text-decoration: none;}'  
      ,'a:link { background-color: #ffffff; color: #000000; text-decoration: none; }'  
      ,'a:hover { background-color: #ffffff; color: #000000; text-decoration: none; }'
      ,'.sIFR-root { background-color: #ffffff; color: #000000; }'
 ]  
});
	
sIFR.replace(paralucentLight, {  
  selector: '#theLatest h2'  
  ,css: [
       'a { background-color: #ffffff; color: #000000; text-decoration: none;}'  
      ,'a:link { background-color: #ffffff; color: #000000; text-decoration: none; }'  
      ,'a:hover { background-color: #ffffff; color: #000000; text-decoration: none; }'
      ,'.sIFR-root { background-color: #ffffff; color: #000000; }' 
 ]  
});
	
sIFR.replace(paralucentLight, {  
  selector: '#designGazette .date'  
  ,css: [  
      '.sIFR-root { background-color: #ffffff; color: #000000; text-align: right; }'
 ]  
}); 
	
sIFR.replace(paralucentLight, {  
  selector: '#theLatest .date'  
  ,css: [ 
      '.sIFR-root { background-color: #ffffff; color: #000000; text-align: right; }'
 ]  
});

sIFR.replace(paralucentLight, {  
  selector: '#dynamicpromo h4'  
  ,css: [ 
      '.sIFR-root { background-color: #000000; color: #ffffff; text-align: left; font-size: 30px; }'
 ]  
});

sIFR.replace(paralucentLight, {  
  selector: '.homepromo h4'  
  ,css: [ 
      '.sIFR-root { background-color: #000000; color: #ffffff; text-align: left; font-size: 28px; cursor: pointer; }'
 ]  
});

sIFR.replace(paralucentLight, {  
  selector: '#gazettepromo h4'  
  ,css: [ 
      '.sIFR-root { background-color: #ffffff; color: #999999; text-align: left; font-size: 18px; }'
 ]  
});




function ourWorkViewVideo(video) 
{
	var hldr = $("#vidframehldr")[0];
	var parent = $("#promo .content")[0];
	var frame = $("#vidframe")[0];
	var nav = $(".subnav", parent)[0];
	
	$(hldr).height($(parent).height() - $(nav).height());
	
	$(hldr).fadeIn(400, function(){
		frame.src = video;
	});
	
	$(".subnav ul li").removeClass('active');
	$(".subnav ul li.film").addClass('active');
	
	return false;
}



$.fn.promoCarousel = function()
{
	var wrapper = this;
	var imgwidths = [];
	var totalw = 0;
	$("img", this).each(function()
	{
		imgwidths.push($(this).width());
		totalw += $(this).width();
	});
	
	var i = 0;
	var fwd = true;
	window.setInterval(function()
	{
		var w = imgwidths[i];
		if (!fwd) 
		{
			w = 0-imgwidths[i-1];;
		}
		$(wrapper).animate(
		{
			left: '-=' + w
		},1000);
		
		if (fwd) 
		{
			i++;
			if (i==imgwidths.length-1) 
			{
				fwd = false;
			}
		}
		else
		{
			i--;
			if (0==i)
			{
				fwd = true;
			}
		}
	},5000);
	
	$(this).width(totalw);
}

