$(document).ready(function(){

	/* 	Initial Configuration...   */

	/* 	Redirect IE6 elsewhere...   */
	var browser = navigator.userAgent;
	if (browser.match('MSIE 6')) {
		window.location = "/ie6.html";
	}
	if (browser.match('iPhone|Android')) {
		window.location = "http://facebook.com/bemisbalkind/";
	}
	

	/* 	Determine what kind of video to serve...   */
	var video_type = 'flash';
//	if (Modernizr.video == true) {
//	 	if (!browser.match('Firefox|MSIE')) {
//			video_type = 'html5';
//	 	}
//	}
// 	if (browser.match('Safari')) {
 	if (browser.match('iPad')) {
		video_type = 'html5';
 	}

	/* 	Setup the initial page state   */
	var loaded = 0;
	var clicked = 0;
	var current_port_category = 0;
	var current_pointer = 1;
	var current_feature = 1;
	var current_parent_ul = '';
	var current_port_page = '';
	var current_heritage_page = 'page1';
	var window_height = $(window).height();
	var window_width = $(window).width();

	$('.campaign_page').css({'top':window_height});	
	$('.content_main .bg_wrap').css({'height':window_height});	
	$('.content_main .bg_wrap .bg').css({ 'height':window_height, 'top': -(window_height) });

//	var adjectives = Array('ICONIC','INNOVATIVE','IMMERSIVE','DYNAMIC','STRATEGIC');
	var adjectives = Array('ICONIC','INNOVATIVE','IMMERSIVE','DIGITAL','STRATEGIC');
//	if ($.cookie('seen_counter') == 'yes') {
//		shuffle(adjectives);
//	}
	for (i = 0; i < 5; i++ ) {
		$('#title'+(i+1)+' .adjective').html(adjectives[i]);
	}

	/* 	Make sure the page doesn't get messed up on resize   */
	$(window).resize(function() {
		window_height = $(window).height();
		window_width = $(window).width();
		$('.content_main .bg_wrap').css({'height':window_height});	
		$('.campaign_page_wrap').css({ 'top':'40px', 'height': (window_height-40) });	
		$('.campaign_page').css({ 'top':'0', 'height': (window_height-40) });	
		for (var i = 1; i < 16; i++) {
			if (i == +current_feature) {
	 			$('.bg_wrap #bg'+i).css({ 'top':0, 'height':window_height });
			} else if (i < +current_feature) {
		 		$('.bg_wrap #bg'+i).css({ 'top':window_height, 'height':window_height });
			} else {
	 			$('.bg_wrap #bg'+i).css({ 'top':-(window_height), 'height':window_height });
			}
		}
	});


//	current_pane = 'select_campaigns';
//	$('.portfolio_section .portfolio_subtitle .overview').css({'opacity':'1'});
//	$('.portfolio_section>.overview').fadeIn('fast');

	var current_pane = 'overview';

	$('#menu').click(function(){
		clicked = 1;
	});
	

	if (Modernizr.touch == true){
		$('.box_thumb').click(function() {
			$(this).find('img').fadeIn('fast');
			feature_switch((this).id);
		});
	} else {
		$('.box_thumb').hover(
			function() {
				$(this).find('img').fadeIn('fast');
			},
			function() {
				if ((this).id !== 'box'+current_feature) {
					$(this).find('img').fadeOut('fast');
				}
			}
		);
		$('.box_thumb').click(function() {
			feature_switch((this).id);
		});
	}

	if (jQuery.url.segment(1) > 0) {
		clicked = 1;
	}

	if (jQuery.url.segment(2) > 0) {
		loaded = 16;
		open_campaign(jQuery.url.segment(2),1);
		open_portfolio(jQuery.url.segment(1));
		open_the_main_page(0);
	}

	if (browser.match('AppleWebKit')) {
		loaded = 16;
	}



	$('#empty_hidden').load(function(){
		if ($.cookie('seen_counter') == 'yes') {
			loaded++;
		} else {
			display_counter();
		}
	});

	$('.box_thumb img').load(function(){
		loaded++;
		get_started(loaded);
	});

	$('.bg_hidden').load(function(){
		loaded++;
		get_started(loaded);
	});


	/* All hovers should be hidden from touch interfaces */
	if (Modernizr.touch == false){
		$('.right_boxes .subtitle').hover(
			function(){ $(this).find('.whitebg').fadeIn(200);},
			function(){ $(this).find('.whitebg').fadeOut(200);}
		);

		$('#left_nav_bar .social_buttons').hover(
			function(){ $(this).find('img').fadeIn('fast'); },
			function(){ $(this).find('img').fadeOut('fast'); }
		);
		$('.subtitle div div').hover(
			function(){	$(this).animate({ 'opacity':.7 }, { duration:400});},
			function(){	$(this).animate({ 'opacity':1 }, { duration:400});}
		);

		$('.close_static').hover(
			function(){	$(this).animate({ 'opacity':.7 }, { duration:400});},
			function(){	$(this).animate({ 'opacity':1 }, { duration:400});}
		);

		$('.apply_now').hover(
			function(){	$(this).animate({ 'opacity':1 }, { duration:200});},
			function(){	$(this).animate({ 'opacity':.8 }, { duration:200});}
		);

		$('#left_nav_bar #menu li').hover(
			function(){
				var width = $(this).css('width');
				width = width.replace('px','');
				$(this).find('.bg:first').animate({ 'left': (width-250) },{ duration: 100, easing: 'easeInOutCirc'});	
			},
			function(){
				if (current_port_category !== (this).id) {
					$(this).find('.bg').animate({ 'left':-(380)},{ duration: 100, easing: 'easeInOutCirc'});	
				}
			}
		);
	}

	$('.subtitle>div>div').click(function(){
		var category_id = (this).id;
		category_id = category_id.substr(8);
		open_portfolio(category_id);
	});	

	$('#left_nav_bar .social_buttons').click(function(){
		if ((this).id == 'facebook') {
			if (browser.match('iPhone') || browser.match('android')) {
				window.open("http://m.facebook.com/bemisbalkind");
			} else {
				window.open("http://facebook.com/bemisbalkind");
			}
		} else if ((this).id == 'twitter') {
			window.open("http://www.twitter.com/bemisbalkind");
		} else {
			window.open("http://www.linkedin.com/company/bemis-balkind");
		}
	});	

	$('.close_portfolio').click(function(){
		$(this).find('.x').rotate({ angle:0,animateTo:180 });
		setTimeout(function(){ close_portfolio(0) }, 500);				
	});
	$('.close_static').click(function(){
		$(this).find('.x').rotate({ angle:0,animateTo:180 });
		setTimeout(function(){ close_static() }, 500);				
	});
	$('.close_campaign').click(function(){
		$(this).find('.x').rotate({ angle:0,animateTo:180 });
		setTimeout(function(){ close_campaign() }, 500);				
	});
	$('#logo').click(function(){
		close_campaign();
		close_static();
		close_portfolio(0);
	});
	$('.left_arrow').click(function(){
		$(this).attr('disabled','disabled');
		switch_campaign((this).id)
	});
	$('.right_arrow').click(function(){
		$(this).attr('disabled','disabled');
		switch_campaign((this).id)
	});
	$('#replay_counter').click(function(){
		$.cookie('seen_counter', 'no', { expires: 7, path: '/', domain: window.location.hostname, secure: false });
		 window.location.replace("http://www.bemisbalkind.com");
	});

	$('#left_nav_bar #menu li').click(function(){
		if ((this).id.length > 0) {
			var track_info = '';
			var new_parent_ul = $(this).parent().get(0).id;
			$('#menu>ul>ul>ul').each(function(){
				if ((this).id !== new_parent_ul) {
					if ($(this).css('display') == 'block') {
						$(this).slideUp('fast');
	//					this_li = (this).id;
	//					alert(this_li);
					}
				}
			});
	
			if ($(this).parent().find('#ul'+(this).id).length == 0) {

				if ($(this).parent().get(0).id == 'ul0') {
					$('#menu li').css( { 'list-style-image': 'url(/images/tinyright.png)' } );
				}

				track_info = (this).id;
				if (isNaN(current_port_category) && isNaN((this).id)){
					if (current_port_category !== (this).id) {
						display_static_page((this).id);
					}
					$('#menu li').css( { 'list-style-image': 'url(/images/tinyright.png)' } );
				} else if (isNaN((this).id)){
					close_portfolio((this).id);
					$('#menu li').css( { 'list-style-image': 'url(/images/tinyright.png)' } );
				} else if (isNaN(current_port_category)){
					track_info = 'Portfolio '+(this).id+', '+$(this).text();
					close_static();
					open_portfolio((this).id);
				} else if (current_port_category > 0){
					track_info = 'Portfolio '+(this).id+', '+$(this).text();
					close_portfolio((this).id);
				} else {
					track_info = 'Portfolio '+(this).id+', '+$(this).text();
					open_portfolio((this).id);
				}
			} else {
				var display = $('#ul'+(this).id).css('display');
				track_info = 'Portfolio '+(this).id+', '+$(this).text();
				if (!(browser.match('MSIE 7') || browser.match('MSIE 6'))) {
					if (display == 'none') {
						$('#menu #ul'+(this).id).slideDown(500);
						$('#menu #'+(this).id).css( { 'list-style-image': 'url(/images/tinydown.png)' } );
					} else {
						$('#menu #ul'+(this).id).slideUp(500);
						$('#menu #'+(this).id).css( { 'list-style-image': 'url(/images/tinyright.png)' } );
					}
				}
			}
			_gaq.push(['_trackEvent', 'Open Page', track_info]);
			current_parent_ul = new_parent_ul;
		}
	});

	// Careers Page
	var current_id = $('.careers_list .listing:first').attr('id');
	$('.careers_list #'+current_id).css({'background-color':'#1a1a1a'});
	$('.careers_list .listing').click(function(){
		var thisid = (this).id;
		if (thisid !== current_id) {
			$('.careers_list #'+thisid).css({'background-color':'#1a1a1a'});
			$('.careers_list #'+current_id).css({'background-color':'#000000'});
			$('.careers_content').scrollTo( $('#job_id_'+thisid), 800 )
			$('.careers_list #'+current_id).css({'background-color':'#000000'});
			current_id = thisid;
		}
	});

	// Leadership Page
	$('.leadership_list .listing:first').addClass('active');
	var leadership_id = $('.leadership_list .listing:first').attr('id');
	$('.leadership_list #'+current_id).css({'background-color':'#1a1a1a'});
	$('.leadership_list .listing').click(function(){
		var thisid = (this).id;
		if (thisid !== leadership_id) {
			$('.leadership_list>.listing.active').removeClass('active');
			$(this).addClass('active');
			$('.leadership_content').scrollTo( $('#bio_id_'+thisid), 800 )
			leadership_id = thisid;
		}
	});

	// Press Page
	$('.press_list .press_listing:first').addClass('active');
	var press_id = $('.press_list .press_listing:first').attr('id');
	$('.press_list #'+current_id).css({'background-color':'#1a1a1a'});
	$('.press_list .press_listing').click(function(){
		var this_press_id = (this).id;
		if (this_press_id !== press_id) {
			$('.press_list>.press_listing.active').removeClass('active');
			$(this).addClass('active');
			$('.press_content').scrollTo( $('#press_id_'+this_press_id), 800 )
			press_id = this_press_id;
		}
	});


	Cufon.replace('#left_nav_bar'); 
	Cufon.replace('.right_boxes p'); 
	Cufon.replace('.right_boxes .subtitle div div'); 
	Cufon.replace('.box_bottom'); 
	Cufon.replace('.box_bottom_black'); 
	Cufon.replace('.static_page .static_title'); 
	Cufon.replace('.static_page .static_subtitle'); 
	Cufon.replace('.static_page .static_text'); 
	Cufon.replace('.static_page .title'); 
	Cufon.replace('.static_page .content'); 
	Cufon.replace('.static_page .job_listing'); 
	Cufon.replace('.static_page .listing'); 
	Cufon.replace('.static_page .bio_description'); 
	Cufon.replace('.static_page .main_text'); 
	Cufon.replace('.static_page .press_listing'); 
	Cufon.replace('.static_page .disclaimer'); 
	Cufon.replace('.careers_list .listing'); 
	Cufon.replace('.close_campaign'); 
	Cufon.replace('.close_static'); 
	Cufon.replace('.close_portfolio'); 




	/* 	OK, now that everything's configured, let's get things rolling...   */

	function get_started(loaded) {
		if (loaded == 31) {
			open_the_main_page(10); // Digital
//			open_the_main_page(0);
		}
	}

	function display_counter() {
		$.cookie('seen_counter', 'yes', { expires: 1, path: '/', domain: window.location.hostname, secure: false });
		$('.content_main .bg_wrap').fadeIn('fast');
		for(i=0;i<4;i++) {
			$('#counter-holder').append('<div class="counter" ID="counter'+i+'"><div class="numerals"></div></div>');
			for(j=0;j<10;j++) {
				$('#counter-holder #counter'+i+' .numerals').append('<div><P>'+j+'</P></div>');
			}
		}
		Cufon.replace('#counter-holder .numerals div p');	
		$('#counter-holder #counter0 .numerals').css({top:-(1*45)});	
		$('#counter-holder #counter1 .numerals').css({top:-(9*45)});	
		$('#counter-holder #counter2 .numerals').css({top:-(6*45)});	
		$('#counter-holder #counter3 .numerals').css({top:-(8*45)});	
		$('#counter-holder .counter').slideDown('2000');
		var d = new Date();
		setTimeout(function(){scroll_numerals(1968,d.getFullYear());}, 3000);
	}

	function scroll_numerals(current_year,end_year) {
		if ((current_year++ < end_year)) {
			thestring = ''+current_year;
			var numerals = Array(thestring.charAt(0), thestring.charAt(1), thestring.charAt(2), thestring.charAt(3));
			for (i=0;i<4;i++) {
				do_the_numbers(i,thestring.charAt(i))
			}
			setTimeout(function(){scroll_numerals(current_year,end_year)}, 150);
		} else {
			setTimeout(function(){open_the_main_page(1)}, 2000);
		}
	}

	function do_the_numbers(location,digit) {
		var topPosition = -(digit * 45);
		$('#counter-holder #counter'+location+' .numerals').animate({ top: (topPosition) },{ duration: 20});	
	}

	function open_the_main_page(first_feature) {
		if (first_feature == 0) {
			first_feature = (Math.ceil(Math.random()*15));
		}
		var first_feature_section = (Math.ceil(first_feature/3));
		$('#counter-holder').fadeOut('1500', function(){
			$('#counter-holder').remove();
			$('#left_nav_bar').fadeIn(500);
			$('.content_main .bg_wrap').fadeIn(500);
			$('.content_main .bg_wrap #bg0').animate({ 'top': window_height}, { duration: 1000, easing: 'easeInOutCubic'});	
			$('.content_main .bg_wrap #bg'+first_feature).animate({ 'top': '0'}, { duration: 1000, easing: 'easeInOutCubic'});	
			$('.right_boxes').fadeIn(500,function(){
				$('.title>div>#title'+first_feature_section).animate({ 'top':0 },{ duration: 1000, easing: 'easeInOutCirc' });
				$('.subtitle>div>#subtitle'+first_feature_section).animate({ 'top':0 },{ duration: 1000, easing: 'easeInOutCirc' });
				$('.text>div>#text'+first_feature_section).animate({ 'top':0 },{ duration: 1000, easing: 'easeInOutCirc' });
				$('.bottom_boxes_wrapper').fadeIn(1000);
				feature_switch(first_feature);
				// Added on Sept 11, 2011: Load portfolios and apply all listeners only after page load.
				// We'll wait half a second before loading, just to be sure it doesn't interfere with the
				// feature switch.
				setTimeout(function(){load_portfolios()},500);
			});

			if (jQuery.url.segment(0) == 'portfolio') {
				open_portfolio(jQuery.url.segment(1))
			} else if (jQuery.url.segment(0) == 'contact' || jQuery.url.segment(0) == 'careers' || jQuery.url.segment(0) == 'about' || jQuery.url.segment(0) == 'leadership') {
				display_static_page(jQuery.url.segment(0));
			}
		});
	}
	
	function feature_switch(new_feature) {
		if (new_feature == 0) {
			new_feature = (Math.ceil(Math.random()*15));
		} else if (isNaN(new_feature)) {
			new_feature = new_feature.substr(3);
		}
		var section = $('#box'+new_feature).parent().get(0).id;
		var section_id = section.substr(7);

		_gaq.push(['_trackEvent', 'Homepage Features', 'Feature '+new_feature]);
		for (var i = 1; i < 16; i++) {
			if (i == +new_feature) {
	 			$('.bg_wrap #bg'+i).animate({ 'top':0},{ duration: 1000, easing: 'easeInOutCubic' });
			} else if (i == +current_feature) {
				if (+current_feature < +new_feature) {
		 			$('.bg_wrap #bg'+i).animate({ 'top':window_height},{ duration: 1000, easing: 'easeInOutCubic' });
				} else {
		 			$('.bg_wrap #bg'+i).animate({ 'top':-(window_height)},{ duration: 1000, easing: 'easeInOutCubic' });
				}
			} else {
				if (i < +new_feature) {
		 			$('.bg_wrap #bg'+i).css({ 'top':window_height });
				} else {
		 			$('.bg_wrap #bg'+i).css({ 'top':-(window_height) });
				}
			}
		}

		$('#box'+current_feature+' img').fadeOut('fast');
		current_feature = new_feature;
		$('#box'+current_feature+' img').fadeIn('fast');

		if (+current_pointer !== +section_id) {
			$('#section'+current_pointer).css({'background-color':'#000'});
			$('#section'+current_pointer+' .box_bottom_black').fadeOut('fast');
			$('#section'+current_pointer+' .box_bottom').fadeIn('fast');
			$('#section'+section_id).css({'background-color':'#fff'});
			$('#section'+section_id+' .box_bottom_black').fadeIn('fast');
			$('#section'+section_id+' .box_bottom').fadeOut('fast');
//			$('#title'+section_id+' .adjective').html(adjectives[i]);
			for (var i = 1; i < 6; i++) {
				if (i == +section_id) {
					$('.title>div>#title'+i).animate({ 'top':0 },{ duration: 1000, easing: 'easeInOutCirc' });
					$('.subtitle>div>#subtitle'+i).animate({ 'top':0 },{ duration: 1000, easing: 'easeInOutCirc' });
					$('.text>div>#text'+i).animate({ 'top':0 },{ duration: 1000, easing: 'easeInOutCirc' });
				} else if (i == +current_pointer) {
					if (+current_pointer < +section_id) {
						$('.title>div>#title'+i).animate({ 'top': 50 },{ duration: 1000, easing: 'easeInOutCirc' });
						$('.subtitle>div>#subtitle'+i).animate({ 'top': 28 },{ duration: 1000, easing: 'easeInOutCirc' });
						$('.text>div>#text'+i).animate({ 'top':240 },{ duration: 1000, easing: 'easeInOutCirc' });
					} else {
						$('.title>div>#title'+i).animate({ 'top': -50 },{ duration: 1000, easing: 'easeInOutCirc' });
						$('.subtitle>div>#subtitle'+i).animate({ 'top': -28 },{ duration: 1000, easing: 'easeInOutCirc' });
						$('.text>div>#text'+i).animate({ 'top': -240 },{ duration: 1000, easing: 'easeInOutCirc' });
					}
				} else {
					if (i < +section_id) {
						$('.title>div>#title'+i).css({ 'top': 50 });
						$('.subtitle>div>#subtitle'+i).css({ 'top': 28 });
						$('.text>div>#text'+i).css({ 'top':240 });
					} else {
						$('.title>div>#title'+i).css({ 'top': -50 });
						$('.subtitle>div>#subtitle'+i).css({ 'top': -28 });
						$('.text>div>#text'+i).css({ 'top': -240 });
					}
				}
			}
			current_pointer = section_id;
		}

		if (clicked == 0) {
			setTimeout(function(){
				if (clicked == 0) {
					feature_switch(0);
				}
			}, 8000);
		}
	}


	function load_portfolios() {
		$('.portfolio_content').load('/functions/portfolio.php', function() {
			Cufon.replace('.portfolio_page .portfolio_content #portfolio_title'); 
			Cufon.replace('.portfolio_page .portfolio_content .portfolio_subtitle'); 
			Cufon.replace('.overview_text '); 
			Cufon.replace('.portfolio_pane p'); 

			$('.portfolio_section .portfolio_subtitle .overview').css({'opacity':'1'});
			$('.portfolio_section>.overview').fadeIn('fast');
			$('.content_main').click(function(){
				if (clicked == 0) {
					clicked = 1;
					$('.portfolio_section>.overview').css({'display':'none'});
					$('.portfolio_section .portfolio_subtitle .overview').css({'opacity':'.7'});
					$('.portfolio_section>.select_campaigns').fadeIn('fast');
					$('.portfolio_section .portfolio_subtitle .select_campaigns').css({'opacity':'1'});
					current_pane = 'select_campaigns';
				}
			});		

			$('.portfolio_subtitle .overview, .portfolio_subtitle .select_campaigns').click(function(){
				if (current_pane !== $(this).attr('class')) {
					$('.portfolio_section>.'+current_pane).fadeOut('fast', function(){
						$(this).css({'display':'none'});
					});
					
					$('.portfolio_section>.'+$(this).attr('class')).fadeIn('fast');
					$('.portfolio_subtitle .'+current_pane).animate({ 'opacity':.7 }, { duration:200});
					$('.portfolio_subtitle .'+$(this).attr('class')).animate({ 'opacity':1 }, { duration:200});
					current_pane = $(this).attr('class');
				}
			});


			$('.pager_page .page').click(function(){
				portfolio_boxes_fadeout('#'+current_port_category+'_'+current_port_page,6,'#'+current_port_category+'_'+(this).id);
				$('#portfolio'+current_port_category+' .pager_page #'+current_port_page).removeClass('active');
				$('#portfolio'+current_port_category+' .pager_page #'+(this).id).addClass('active');
			});
			$('.portfolio_pager .tinyleft').click(function(){
				var last_page =  $(this).parent().parent().parent().find('.portfolio_box_wrapper .portfolio_pages_page').length;
				var this_page = current_port_page.replace('page','');
				if (this_page == 1) { this_page = last_page; } else { this_page--; }
				portfolio_boxes_fadeout('#'+current_port_category+'_'+current_port_page,6,'#'+current_port_category+'_page'+this_page);
				$('#portfolio'+current_port_category+' .pager_page #'+current_port_page).removeClass('active');
				$('#portfolio'+current_port_category+' .pager_page #page'+this_page).addClass('active');
			});
			$('.portfolio_pager .tinyright').click(function(){
				var last_page =  $(this).parent().parent().parent().find('.portfolio_box_wrapper .portfolio_pages_page').length;
				var this_page = current_port_page.replace('page','');
				if (this_page == last_page) { this_page = 1; } else {this_page++;}
				portfolio_boxes_fadeout('#'+current_port_category+'_'+current_port_page,6,'#'+current_port_category+'_page'+this_page);
				$('#portfolio'+current_port_category+' .pager_page #'+current_port_page).removeClass('active');
				$('#portfolio'+current_port_category+' .pager_page #page'+this_page).addClass('active');
			});
			$('.heritage_pager .tinyleft').click(function(){
				var last_page =  $(this).parent().parent().parent().find('.portfolio_heritage_wrapper .portfolio_pages_page').length;
				var this_page = current_heritage_page.replace('page','');
				if (this_page == 1) { this_page = last_page; } else { this_page--; }
				heritage_boxes_fadeout('#h'+current_port_category+'_'+current_port_page,2);
		//		setTimeout(function(){
					$('#h'+current_port_category+'_'+current_heritage_page).fadeOut('slow');
					$('#h'+current_port_category+'_page'+this_page).fadeIn('slow');
		//		}, 500);
				setTimeout(function(){heritage_boxes_fadein('#h'+current_port_category+'_page'+this_page,1,2);}, 1000);
				current_heritage_page = 'page'+this_page;
			});
			$('.heritage_pager .tinyright').click(function(){
				var last_page =  $(this).parent().parent().parent().find('.portfolio_heritage_wrapper .portfolio_pages_page').length;
				var this_page = current_heritage_page.replace('page','');
				if (this_page == last_page) { this_page = 1; } else {this_page++;}
				heritage_boxes_fadeout('#h'+current_port_category+'_'+current_port_page,2);
		//		setTimeout(function(){
					$('#h'+current_port_category+'_'+current_heritage_page).fadeOut('slow');
					$('#h'+current_port_category+'_page'+this_page).fadeIn('slow');
		//		}, 500);
				setTimeout(function(){heritage_boxes_fadein('#h'+current_port_category+'_page'+this_page,1,2);},1000);
				current_heritage_page = 'page'+this_page;
			});
		
			$('.portfolio_box').click(function(){
				open_campaign((this).id,1);
			});

			$('.overview .view_select_campaigns, .select_campaigns').click(function(){
				$('.portfolio_subtitle .overview').animate({ 'opacity':.7 }, { duration:200});
				$('.portfolio_subtitle .select_campaigns').animate({ 'opacity':1 }, { duration:200});
				$('.portfolio_section>.overview').fadeOut('fast', function(){
					$(this).css({'display':'none'});
				});
				$('.portfolio_section>.select_campaigns').fadeIn('fast');
				current_pane = 'select_campaigns';
			});

			/* All hovers should be hidden from touch interfaces */
			if (Modernizr.touch == false){
				$('.portfolio_subtitle .overview, .portfolio_subtitle .select_campaigns ').hover(
					function(){
						if (current_pane !== $(this).attr('class')) {
							$(this).animate({ 'opacity':1 }, { duration:200});
						}
					},
					function(){	
						if (current_pane !== $(this).attr('class')) {
							$(this).animate({ 'opacity':.6 }, { duration:200});
						}
					}
				);
				$('.full_portfolio').hover(
					function(){
						$(this).find('.subfull_portfolio').animate({ 'opacity':0 }, { duration:200});
						$(this).find('.subcoming_soon').animate({ 'opacity':1 }, { duration:200});
					},
					function(){ 
						$(this).find('.subcoming_soon').animate({ 'opacity':0 }, { duration:200});
						$(this).find('.subfull_portfolio').animate({ 'opacity':1 }, { duration:200});
					}
				);
		
				$('.view_select_campaigns').hover(
					function(){	$(this).animate({ 'opacity':.6 }, { duration:200});},
					function(){	$(this).animate({ 'opacity':1 }, { duration:200});}
				);
				$('.close_portfolio').hover(
					function(){	$(this).animate({ 'opacity':.7 }, { duration:400});},
					function(){	$(this).animate({ 'opacity':1 }, { duration:400});}
				);
				$('.close_campaign').hover(
					function(){	$(this).animate({ 'opacity':1 }, { duration:400});},
					function(){	$(this).animate({ 'opacity':.6 }, { duration:400});}
				);
				$('.tinyleft').hover(
					function(){	$(this).animate({ 'opacity':1 }, { duration:200});},
					function(){	$(this).animate({ 'opacity':.6 }, { duration:200});}
				);
				$('.tinyright').hover(
					function(){	$(this).animate({ 'opacity':1 }, { duration:200});},
					function(){	$(this).animate({ 'opacity':.6 }, { duration:200});}
				);
				$('.image_holder').hover(
					function(){$(this).find('.image_over').fadeIn(300) ;},
					function(){ $(this).find('.image_over').fadeOut(300) ;}
				);
				$('.left_arrow').hover(
					function(){	$(this).animate({ 'opacity':1 }, { duration:200});},
					function(){	$(this).animate({ 'opacity':.6 }, { duration:200});}
				);
				$('.right_arrow').hover(
					function(){	$(this).animate({ 'opacity':1 }, { duration:200});},
					function(){	$(this).animate({ 'opacity':.6 }, { duration:200});}
				);
			}
		});
	}
	
	
	function open_portfolio(category_id) {
		if (current_port_category !== category_id) {
			$('#ul'+category_id).slideToggle('fast');
			if (category_id == 4) { // 'Digital' is a wrapper
				category_id = 6;
//			} else if (category_id == 5) { // 'Branding', too
//				category_id = 10;
			}
			if (current_port_category !== 0) {
				$('#menu ul #'+current_port_category+' .bg').animate({ 'left':-(380)},{ duration: 500, easing: 'easeInOutCirc'});	
			}
			var width = $('#menu ul #'+category_id).css('width');
			width = width.replace('px','');
			$('#menu ul #'+category_id+' .bg:first').animate({ 'left': (width-250) },{ duration: 500, easing: 'easeInOutCirc'});
	
			$('#portfolio'+category_id).fadeIn(400);
			$('#portfolio'+category_id+' .pager_page #page1').addClass('active');
			$('.portfolio_bg').fadeIn(400);
			$('.portfolio_page').fadeIn(400);

			if ($('#portfolio'+category_id+' .select_campaigns .portfolio_box_wrapper .portfolio_pages_page div').length == 0) {
				$('.portfolio_section>.select_campaigns').fadeOut('fast');
				$('.portfolio_section>.overview').fadeIn('fast');
			} else if (current_pane == 'select_campaigns') {
				$('.portfolio_section>.select_campaigns').fadeIn('fast');
				$('.portfolio_section>.overview').fadeOut('fast');
			}

//			if (current_pane == 'select_campaigns') {
				$('#portfolio'+category_id+' #'+category_id+'_page1').fadeIn(400,function(){
					portfolio_boxes_fadein('#'+category_id+'_page1', 1,6);
				});
				$('#portfolio'+category_id+' #h'+category_id+'_page1').fadeIn(400,function(){
					portfolio_boxes_fadein('#h'+category_id+'_page1', 1,6);
				});
//			}

			current_port_page = 'page1';
			current_port_category = category_id;
		}
	}

	function close_portfolio(next_id) {
		if (current_port_category !== next_id) {
			if (next_id == 0) {
				$('#menu ul li .bg').animate({ 'left':-(380)},{ duration: 500, easing: 'easeInOutCirc'});	
			}
			portfolio_boxes_fadeout('#'+current_port_category+'_'+current_port_page,8,next_id);
			heritage_boxes_fadeout('#h'+current_port_category+'_'+current_port_page,2);
		}
	}
	
	function portfolio_boxes_fadein(wrapper_id,current,last) {
		if (current <= last) {
			$(wrapper_id+' .box'+current+' .image_holder img').animate({ 'opacity':1},{ duration: 150});	
			current++;
			setTimeout(function(){portfolio_boxes_fadein(wrapper_id,current,last)}, 150);
		}
	}

	function portfolio_boxes_fadeout(wrapper_id,current,next_id) {
		if (current > 0) {
			$(wrapper_id+' .box'+current+' .image_holder img').animate({ 'opacity':0},{ duration: 150});	
			current--;
			setTimeout(function(){portfolio_boxes_fadeout(wrapper_id,current,next_id)}, 150);
		} else {
			if (next_id !== 0 && next_id.match('page')) {
				$('#'+current_port_category+'_'+current_port_page).fadeOut(500, function(){
					$(next_id).fadeIn(500, function(){
						current_port_page = (this).id.substring(2);
						portfolio_boxes_fadein(next_id,1,6);
					});
				});
			} else {
				if (isNaN(next_id)) {
					$('#portfolio'+current_port_category).fadeOut(200);
					$('.portfolio_page').fadeOut(200);
					$('.portfolio_bg').fadeOut(1000);
					display_static_page(next_id);
				} else if (next_id > 0) {
					$('#'+current_port_category+'_'+current_port_page).fadeOut(200);
					$('#portfolio'+current_port_category).fadeOut(200);
					open_portfolio(next_id);
				} else {
					$('#portfolio'+current_port_category).fadeOut(200);
					$('.portfolio_page').fadeOut(200);
					$('.portfolio_bg').fadeOut(200);
					current_port_category = 0;
				}
			}
		}
	}

	function heritage_boxes_fadein(wrapper_id,current,last) {
		if (current <= last) {
			$(wrapper_id+' .box'+current+' .image_holder img').animate({ 'opacity':1},{ duration: 150});	
			current++;
			setTimeout(function(){heritage_boxes_fadein(wrapper_id,current,last)}, 450);
		}
	}

	function heritage_boxes_fadeout(wrapper_id,current) {
		if (current > 0) {
			$(wrapper_id+' .box'+current+' .image_holder img').animate({ 'opacity':0},{ duration: 150});	
			current--;
			setTimeout(function(){heritage_boxes_fadeout(wrapper_id,current)}, 150);
		}
	}


	function open_campaign(this_id,initial) {
		var campaign_id = this_id.replace("campaign","");
		var campaign_name = '';
		var campaign_description = '';
		var campaign_url = '';
		var category_name = '';
		var main_image_width = '';
		var main_image_height = '';
		var campaign_thumb_id = 0;
		var prev_id = 0;
		var next_id = 0;
	    $.ajax({
	        type: "GET",
	        url: '/functions/campaign_asset_info_xml.php?campaign_id='+campaign_id,
	        dataType: "xml",
		    error: function(){ alert('Error loading XML document'); },
	        success: function(xml) {
				$(xml).find('campaign_name').each(function(){ campaign_name = $(this).text(); });
				$(xml).find('campaign_subtitle').each(function(){ campaign_subtitle = $(this).text(); });
				$(xml).find('category_id').each(function(){ category_id = $(this).text(); });
				campaign_name = campaign_name.toUpperCase();
				$(xml).find('campaign_description').each(function(){ campaign_description = $(this).text(); });
				$(xml).find('campaign_url').each(function(){ campaign_url = $(this).text(); });
				$(xml).find('category_name').each(function(){ category_name = $(this).text(); });
				$(xml).find('campaign_asset_width').each(function(){ campaign_asset_width = $(this).text(); });
				$(xml).find('campaign_asset_height').each(function(){ campaign_asset_height = $(this).text(); });
				$(xml).find('number_of_assets').each(function(){ number_of_assets = $(this).text(); });
				$(xml).find('number_of_related').each(function(){ number_of_related = $(this).text(); });
				$(xml).find('prev_id').each(function(){ prev_id = $(this).text(); });
				$(xml).find('next_id').each(function(){ next_id = $(this).text(); });
				_gaq.push(['_trackEvent', 'Portfolios', 'Open Campaign '+ campaign_name]);

				var assets = '';
				var asset_thumbs = '';
				var asset_video_ids = Array();
				$(xml).find('asset').each(function(){
					var asset_id = $(this).find("asset_id").text();
					var mimetype = $(this).find("mimetype").text();
					if (mimetype == 'video/mp4') {
						if (video_type == 'html5') {
//							campaign_asset_width = 658;
//							campaign_asset_height = 468;
							assets += '<div class="asset_main" ID="asset'+asset_id+'" STYLE="width:'+campaign_asset_width+'px;height:'+campaign_asset_height+'px;"><video width="'+campaign_asset_width+'" height="'+campaign_asset_height+'" id="video'+asset_id+'" controls poster="/files/campaigns/assets/'+asset_id+'.jpg"><source id="mp4" src="/files/campaigns/assets/'+asset_id+'.mp4" type="video/mp4" codecs="avc1, mp4a"></video></div>';
						} else {
							campaign_asset_height = +campaign_asset_height -2;
							assets += '<div class="asset_main" ID="asset'+asset_id+'" STYLE="width:'+campaign_asset_width+'px;height:'+campaign_asset_height+'px;"><div id="video'+asset_id+'"></div></div>';
							asset_video_ids.push(asset_id);
						}
					} else {
						assets += '<div class="asset_main" ID="asset'+asset_id+'" STYLE="width:'+campaign_asset_width+'px;height:'+campaign_asset_height+'px;"><IMG SRC="/files/campaigns/assets/'+asset_id+'.jpg" STYLE="width:'+campaign_asset_width+'px;height:'+campaign_asset_height+'px;"></div>';
					}
					if (number_of_assets > 1) {
						asset_thumbs += '<div class="asset_thumb" ID="asset'+asset_id+'" style="background-image:url(/files/campaigns/assets/'+asset_id+'_thumb_bw.jpg)"><IMG SRC="/files/campaigns/assets/'+asset_id+'_thumb.jpg"></div>';
					}
				});

				var campaign_markup = '<div class="left_column" style="width:'+(+campaign_asset_width+70)+'px;">';				
				campaign_markup += '<div class="social_links"><div>SHARE:</div><div class="share" id="email"><img src="/images/social/email.png" alt="link" width="16" height="13" /> Email</div><div class="share" id="link"><img src="/images/social/link.png" alt="link" width="16" height="13" /> Link</div></div>';
				if ((category_id == 6 || category_id == 7 || category_id == 8 || category_id == 9) && campaign_url.length > 0) {
					campaign_markup += '	<div class="launch_button"><P><A HREF="'+campaign_url+'" TARGET="_BLANK">Launch this campaign</A></P></div>';
				}
				campaign_markup += '	<div class="asset_thumbs">'+asset_thumbs+'</div>';
				campaign_markup += '	<div class="assets" STYLE="width:'+campaign_asset_width+'px;height:'+campaign_asset_height+'px;">'+assets+'</div>';
				campaign_markup += '</div>';
				campaign_markup += '<div class="right_column">';
				campaign_markup += '	<div ID="campaign_title">'+campaign_name+'</div>';
				if (campaign_subtitle.length > 0) {
					campaign_markup += '	<div ID="campaign_subtitle">'+campaign_subtitle+'</div>';
				}
				campaign_markup += '	<div ID="campaign_text">'+campaign_description+'</div>';
				if (number_of_related > 0) {
					campaign_markup += '	<div ID="campaign_subtitle">RELATED</div>';
					campaign_markup += '	<div ID="related_links"></div>';
				}
				campaign_markup += '</div>';
				var campaign_content_width = (+campaign_asset_width+400);
				var campaign_margin_left = -(campaign_content_width/2);
				$('.campaign_content').css( { 'width' : campaign_content_width, 'margin-left' : campaign_margin_left+'px'} );

				if (($('.campaign_content div').length) == 0) {
					$('.campaign_content').append(campaign_markup);
					if (video_type == 'flash') {
						for(var i in asset_video_ids) {
						    $('#video'+asset_video_ids[i]).jwPlayer({
						        id: 'jwPlayer'+i,
						        swf: '/lib/player.swf',
						        file: '/files/campaigns/assets/'+asset_video_ids[i]+'.mp4',
						        image: '/files/campaigns/assets/'+asset_video_ids[i]+'.jpg',
						        height: campaign_asset_height,
						        width: campaign_asset_width,
						        backcolor:'#FFFFFF',
						        controlbar:'over',
						        autostart:true,
						        allowfullscreen:true
						    });
						}
					}
					if (jQuery.url.segment(3)) {
						campaign_thumb_id = jQuery.url.segment(3);
						$('.asset_thumbs #'+campaign_thumb_id+' img').fadeIn('slow');
						$('.assets #'+campaign_thumb_id).fadeIn('slow');
						$('.assets #'+campaign_thumb_id).addClass('current');
					} else {
						$('.asset_thumb:first img').fadeIn('slow');
						campaign_thumb_id = $('.asset_thumb:first').attr('id');
						$('.assets .asset_main:first').fadeIn('slow');
						$('.assets .asset_main:first').addClass('current');
					}
					if (Modernizr.touch == false){
						$('.asset_thumb').hover(
							function(){
								if ((this).id !== campaign_thumb_id) {
									$(this).find('img').fadeIn('fast')
								}
							},
							function(){
								if ((this).id !== campaign_thumb_id) {
									$(this).find('img').fadeOut('fast')
								}
							}
						);
					}
					$('.asset_thumb').click(function(){
						var this_id = (this).id;
						if (campaign_thumb_id !== this_id) {
							$('.asset_thumbs #'+this_id+' img').fadeIn('slow');
							$('.asset_thumbs #'+campaign_thumb_id+' img').fadeOut('slow');
							$('.assets .current').fadeOut(250, function(){
								$('.assets .asset_main').removeClass('current');
							});
							$('.assets #'+this_id).fadeIn(250, function(){
								$('.assets #'+this_id).addClass('current');
							});
							campaign_thumb_id = this_id;
						}
					});
	
	
					$('.share').click(function(){
						var the_link = 'http://www.bemisbalkind.com/portfolio/'+category_id+'/'+campaign_id;
						if (!(typeof campaign_thumb_id === 'undefined')) {
							the_link += '/'+campaign_thumb_id;
						}
						if ((this).id == 'link') {
							$('.social_links').append('<DIV CLASS="link_box"><INPUT TYPE="text" CLASS="link_text" VALUE="'+the_link+'"><IMG ID="x" SRC="/images/close_x_whitebg.png"></div>');
							$('.social_links .link_text').focus();
							$('.social_links .link_text').select();
							$('.social_links .link_box #x').click(function(){
								$('.social_links .link_box').fadeOut('fast',function(){
									$(this).remove();
								})
							});
						} else {
							var the_email = 'mailto:?subject=Bemis Balkind Portfolio > '+campaign_name+'&body=I want to share Bemis Balkind\'s '+campaign_name+' campaign with you.  Click here to check it out:%0A%0A'+the_link;
							window.location.href = the_email;
						}
					});
	
	
	
					$(xml).find('related').each(function(){
						var rcampaign_id = $(this).find("rcampaign_id").text();
						var rcampaign_name = $(this).find("rcampaign_name").text();
						var rcategory_id = $(this).find("rcategory_id").text();
						var rcategory_name = $(this).find("rcategory_name").text();
						var image = $(this).find("image").text();
						$('#related_links').append('<div class="related" ID="rcampaign'+rcampaign_id+'_'+rcategory_id+'"><IMG SRC="'+image+'"><div class="text"><div class="rcampaign_name">'+rcampaign_name+'</div><div class="rcategory_name">'+rcategory_name+'</div></div></div>');
					});
	
					$('.campaign_content .right_column #related_links .related').click(function(){
						var lump = (this).id;
						var stuff = lump.split('_');
						var campaign_id = stuff[0].replace('rcampaign','');
						if (stuff[1] == current_port_category) {
							switch_campaign(campaign_id);
						} else {
							close_campaign();
							close_portfolio(stuff[1]);
							setTimeout(function(){ open_campaign(campaign_id,1) }, 2500);
						}
					});
	
					$('.left_arrow').attr("id",prev_id);
					$('.right_arrow').attr("id",next_id);
					Cufon.replace('.campaign_page .campaign_content .left_column .social_links div'); 
					Cufon.replace('.campaign_page .campaign_content .right_column div'); 
				}
			}
	    });

		if (initial == 1) {
			$('.campaign_bg').fadeIn(500,function(){
				$('.campaign_page_wrap').css({ 'top':'40px', 'height': (+window_height-40)+'px' });	
				$('.campaign_page_wrap').fadeIn('fast');	
				$('.campaign_page').css({'height': (+window_height-40)+'px' });	
				$('.campaign_page').animate({ 'top':0 },{ duration: 500, easing: 'easeInOutCirc' });	
				$('.close_portfolio').fadeOut(100);
			});
		}
	}

	function close_campaign() {
		$('.campaign_page').animate( { 'top':window_height },{ duration: 500, easing: 'easeInOutCirc', complete: function() {
			$('.campaign_bg').fadeOut(200, function(){
				$('.campaign_page_wrap').fadeOut(100);	
				$('.close_portfolio').fadeIn(100);
				$('.campaign_content').html('');
			});
		}});	
	}
	function switch_campaign(new_id) {
		if (new_id > 0) {
			$('.campaign_content').html('');
			open_campaign(new_id,0);
		} else {
			close_campaign();
		}
	}


	function display_static_page(page_name) {
		if (isNaN(current_port_category)) {
			$('.static_content_wrap .static_content#s'+current_port_category).fadeOut(500);
			$('.static_content_wrap .static_content#s'+page_name).fadeIn(500);
		} else {
			$('.static_bg').fadeIn(500,function(){
				$('.static_page').fadeIn(500);
				$('.static_page .static_content_wrap .static_content#s'+page_name).fadeIn(500);
			});
		}
		$('#menu ul #'+current_port_category+' .bg').animate({ 'left':-(380)},{ duration: 500, easing: 'easeInOutCirc'});	
		var width = $('#menu ul #'+page_name).css('width');
		width = width.replace('px','');
		$('#menu ul #'+page_name+' .bg:first').animate({ 'left':(width-250)},{ duration: 500, easing: 'easeInOutCirc'});
		current_port_category = page_name;
	}

	function close_static() {
		$('.static_bg').fadeOut(500);
		$('.static_page .static_content_wrap .static_content#s'+current_port_category).fadeOut(500);
		$('.static_page').fadeOut(500);
		$('#menu ul #'+current_port_category+' .bg').animate({ 'left':-(380)},{ duration: 500, easing: 'easeInOutCirc'});
//		current_port_category = '';
	}

	function shuffle(list) {
		var i, j, t;
		for (i = 1; i < list.length; i++) {
			j = Math.floor(Math.random()*(1+i));  // choose j in [0..i]
			if (j != i) {
				t = list[i];                        // swap list[i] and list[j]
				list[i] = list[j];
				list[j] = t;
			}
		}
	}

});

