function set_fslider_height(slider) {
		// cl('set_fslider_height', this);
		/*if ($('body').hasClass('slider-view')) {
			// cl(slider, ' slider');
			if(slider === undefined) slider = $('.fullscreen-slider').first();
			var dh = $('.slides .flex-active-slide img', slider).height();
			// cl(dh);
			$(slider).css({
				top: 0
			})
			$wnd.scrollTop(0);
			$('#wrapper-overflow').css({
				height: dh
			})
		}*/
	}

function init_homepage() {
	var is_init_homepage = true;
	var sectionID = '';
	var slideID = '';
	var slider_placeholder = '/css/images/ajax-loader-b.gif';

	// parse_subpage();
	// cl(sectionID, slideID);

	function set_fslider_hash() {
		// cl('set_fslider_hash', this);
		if (slideID!=='') {
			document.location.hash = '#!/'+sectionID+'/'+slideID;
		} else if (sectionID!=='') {
			document.location.hash = '#!/'+sectionID;
		} else {
			document.location.hash = '';
		}
	}


	function load_slide_image(current_slide, fslider) {
		// cl('load_slide_image', this);
		var $img = $('img', current_slide);
		if ($img.length) {
			if ($img.data('src')) {
				$img.attr('src', $img.data('src')).on('load', function(event) {
					$img.closest('li').addClass('loaded');
					$img.fadeIn('normal');
					// cl('load_slide_image > set_fslider_height');
					set_fslider_height(fslider);
					$('#svfw').addClass('svfw-loaded');
				});
				// cl(this.complete, " this.complete");
				// if (this.complete) $(this).trigger('load');
				$img.data('src', '');
			}
		}
	}

	function hide_fslider() {
		// cl('hide_fslider', this);
		$('#wrapper-overflow').css('height', '');
		if(!$('body').hasClass('slider-view')){
			setTimeout(function(){
				$('body').removeClass('finish-animate');
			}, 500)
		}
		$('body').css('overflow','')
		setTimeout(function(){
			$('body').removeClass('slider-view');
		}, 200)
		slideID = '';
		set_fslider_hash();
		$('.flex-direction-nav a.flex-prev, .flex-direction-nav a.flex-next').css('position', 'absolute');
		// console.log(sectionID, $('#'+sectionID).height());
		/*if($('#'+sectionID).height() != 0){
			var $openlink = $('#'+sectionID+' .more-toggle');
			console.log($openlink);
			if ($openlink.length) $openlink.eq(0).trigger('click');
		};*/
		$('#svfw').remove();
		/*setTimeout(function () {
			var $section = $('#'+sectionID);
				if ($section.length){
					if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {
						window.scrollTo(0, parseInt($section.offset().top + $section.find('.title-wrap').outerHeight() - $('#header').outerHeight()));
					}else{
						$('html, body').animate({
					        scrollTop: $('.expanded-content', $section).offset().top-73
					    }, 2000, 'swing');
				    }
				}
		}, 500);*/
	}

	function canSee($block) {
		// cl('canSee', this);
		var c = {
			top: $wnd.scrollTop(),
			left: $wnd.scrollLeft()
		};
		c.right = c.left + $wnd.width();
		c.bottom = c.top + $wnd.height();
		var a = $block.offset();
		a.right = a.left + $block.outerWidth();
		a.bottom = a.top + $block.outerHeight();
		return (!(c.right < a.left || c.left > a.right || c.bottom < a.top || c.top > a.bottom));
	}

	function parse_subpage() {
		// cl('parse_subpage', this);
		var hash = document.location.hash;
		if (hash.length>1) {
			hash = hash.substring(1);
			var parts = hash.split('/');
			if (parts.length>2) {
				slideID = parts[2];
				sectionID = parts[1];
				var $section = $('#'+sectionID);
				if ($section.length) {
					// $('html, body').animate({
				 //        scrollTop: $section.offset().top - $section.outerHeight()
				 //    }, 2000, 'swing', function () {
				 //    	// cl('click!');
				 //    	// $('.column.image', $section).eq(slideID).trigger('click.startSlider');
					// });
					$('.column.image', $section).eq(slideID).trigger('click.startSlider');
				show_home_content();
				}
			} else if (parts.length>1) {
				sectionID = parts[1];
				var $section = $('#'+sectionID);
				if ($section.length){
					show_home_content();
					
					setTimeout(function(){
				
						$('html, body').animate(
							{scrollTop: $section.offset().top},
							1000,
							'swing',
							function () {
								// var $openlink = $('#'+sectionID+' .more-toggle');
								// if ($openlink.length) {
								// 	$openlink.eq(0).trigger('click');
								// }
							}
						);
					}, 1000);
					
				}
			}
		}
	}

	function prepare_section($section) {
		// cl('prepare_section', this);
		$('.column.image', $section).each( function() {
			var $col = $(this);
			var thumb = $col.data('thumb');
			$col.data('thumb', '');
			if (thumb) {
				var $img = $('<img src="'+thumb+'" style="position: absolute; left: -10000px; top: 0; visibility:hidden;" />');
				$img.appendTo('#wrapper-overflow').on('load', function() {
					$col.css({
						'background': 'url("'+thumb+'") no-repeat center '+($col.hasClass('centered') ? 'center' : 'top'),
						'background-size': 'cover',
						'cursor': 'pointer'
					})
					$(this).remove();
				}).each(function() {
					if (this.complete) $(this).trigger('load');
				});
			}
		});
	}

	function show_home_content() {
		// cl('show_home_content', this);
		setTimeout( function() {
			$(document).off('mousewheel.splash keydown.splash');
		}, 500);

		$(document).on('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', '#wrapper-overflow', function() {
			if (!is_touch) {
				$('.homepage-section .title-wrap').each(function () {
					var $sect = $(this);
					var speed = 2;
					$sect.css({
						backgroundAttachment: "fixed",
						backgroundPosition: "50%" + Math.floor(($sect.offset().top - $wnd.scrollTop()) / speed) + "px"
					});
					$wnd.on('scroll.parallax', function () {
						if (canSee($sect)) {
							var d = Math.floor(($sect.offset().top - $wnd.scrollTop()) / speed);
							var pos = "50% " + d + "px";
							$sect.css({
								backgroundPosition: pos
							});
						}
					});
				});
			} else {
				$wnd.on('scroll.active_section', function () {
					var top = $wnd.scrollTop();
					var wh = Math.max(320, $wnd.height());
					$('.homepage-section').each( function() {
						var $this = $(this);
						var this_top = $this.offset().top;
						var this_bottom = this_top + wh;
						if (this_top<top+wh/8 && this_bottom>top+wh/8) {
							$this.addClass('visible');
						} else {
							$this.removeClass('visible');
						}
					});
				}).trigger('scroll.active_section');
			}
		});

		$('body').addClass('content-show');
		setTimeout(function(){
			$('body').addClass('finish-animate');
		}, 600)
		if (!css_transitions) {
			$('#wrapper-overflow').trigger('transitionend');
		}
	}

	$('#splash-arrow, #main-logo').on('click.splash', function() {
		show_home_content();
		$('#splash-page').closest('.section').remove();
		return false;
	});
	$(document).on('keydown.splash', function(e) {
		if (e.keyCode==40 || e.keyCode==34) {
			show_home_content();
		}
	}).on('mousewheel.splash', function(e) {
		e.preventDefault();
		window.scrollTo(0, 0);
		show_home_content();
	});

	$wnd.on('resize', function() {
		var wh = Math.max(320, $wnd.height());
		var ww = $wnd.width();
		$('.homepage-section .title-wrap').css('height', wh);
		var squ = Math.floor(ww/4);
		if (ww<641) {
			squ = Math.floor(ww);
		} else if (ww<800) {
			squ = Math.floor(ww/3);
		}
		$('.homepage-section .column').css({
			'height': squ,
			'line-height': squ+'px'
		});
		$('.fullscreen-slider .slides li').css('line-height', wh+'px');
	}).trigger('resize');

	$('#header-logo').on('click tap', function() {
		if ($('body').hasClass('slider-view')) {
			hide_fslider();
			window.scrollTo(0, 0);
			return false;
		}
	});

	$('#header-logo, #header-nav nav li:first-child a').click(function(){
		$('#homepage').removeClass('header-nav-open');
		if($('body').hasClass('slider-view')){
			window.scrollTo(0, 0);
			$('body').removeClass('slider-view');
			$('#svfw').remove();
			$('.flex-direction-nav a.flex-prev, .flex-direction-nav a.flex-next').css('position', 'absolute');
			$('body').css('overflow','')

		}else if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)){
			window.scrollTo(0, 0);
		}else{
			$('html,body').animate({scrollTop: 0}, 650);
		}

		$('#header-menu-toggle').removeClass('opened');
		$('.expanded-content').css('height', 0);
		$('.homepage-section').removeClass('open');
		$('.more-link.more-toggle').html('READ MORE');
			
		return false
	})

	$('.homepage-section .more-toggle').unbind('click tap').on('click tap', function(e) {
		var $section = $(this).parents('.homepage-section');;
		// cl($section);
		var $ec = $('.expanded-content', $section);
		var $slides = $('.column', $ec);
		var $link = $('.more-link', $section);
		var height = $section.height();

		if (!$section.hasClass('open')) {
			// cl("section.not_open");
			var eh = $ec.css({
				'position': 'absolute',
				'left': 0,
				'top': 0,
				'width': '100%',
				'opacity': 0,
				'height': 'auto',
				'display': 'block'
			}).height();
			$ec.css({
				'position': '',
				'left': '',
				'top': '',
				'opacity': '',
				'height': eh,
				'display': (css_transitions) ? 'block' : 'none'
			});

			$slides.hide();
			
			if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {   
			            window.scrollTo(0, parseInt($ec.offset().top-73));
			}
			
			if (css_transitions) {
				$section.addClass('open');
			} else {
				$('.expanded-content', $section).slideDown(650, function() {
					$section.addClass('open');
					$wnd.trigger('scroll');
				});
			}

			$('html,body').animate({scrollTop: $ec.offset().top-73}, 650);

			$link.text('READ LESS');
			$wnd.trigger('scroll');
		} else {
			// cl("section.open");
			if (css_transitions) {
				$section.removeClass('open');
				$ec.css('height', 0);
			} else {
				$('.expanded-content', $section).slideUp(650, function() {
					$section.removeClass('open');
					$wnd.trigger('scroll');
				})
			}
			$('html,body').animate({scrollTop: $section.offset().top}, 650);
			$link.text('READ MORE');
		}

		setTimeout(function(){
			$slides.show();
			prepare_section($section);
		}, 700)

		return false;
	})


if ( $wnd.width() > 640 ) {

	$('.homepage-section .column.image').on('click.startSlider tap.startSlider', function(event) {
		var wh = Math.max(320, $wnd.height());
		var $slides = $(this).closest('.homepage-section').find('.column.image');
		if ($('.fullscreen-slider').length) {
			$('.fullscreen-slider').flexslider('destroy');
			$('.fullscreen-slider').remove();
		}
		var slider = '<div class="fullscreen-slider"><ul class="slides">';
		$slides.each( function() {
			slider += '<li style="line-height:'+wh+'px;"><div style="height:'+wh+'px;" class="image-wrraper"><img src="'+slider_placeholder+'" data-src="'+$(this).data('image')+'" alt="" /></div></li>'
		});
		slider += '</ul></div>';
		
		sectionID = $(this).closest('.homepage-section').attr('id');
		$('#wrapper-overflow').append(slider);
		var start = Math.max(0, $slides.index($(this)));
		$('.fullscreen-slider').css({
			'top': $wnd.scrollTop()
		}).flexslider({
			directionNav: true,
			controlNav: false,
			startAt: start,
			slideshow: false,
			start: function(fslider) {
				slideID = fslider.animatingTo;
				var current_slide = fslider.slides[fslider.animatingTo];
				load_slide_image(current_slide, fslider[0])
				set_fslider_hash();
			},
			before: function(fslider) {
				// cl('fullscreen-slider');
				slideID = fslider.animatingTo;
				var current_slide = fslider.slides[fslider.animatingTo];
				load_slide_image(current_slide, fslider[0])
				set_fslider_hash();
			},
			after: function(fslider) {
				set_fslider_height(fslider[0]);
			}
		});
		$('body').addClass('slider-view');
		setTimeout(function(){
			$('body').css('overflow','hidden')
		}, 500);		
		if ($('#svfw').size()>0) return;
		// cl('svfw');
		$('<div id = "svfw" >').appendTo('body')
		  .append('<div>'+
			 '<a href="" class="social-icon facebook" target="_blank"></a>'+
		     '<a href="" class="social-icon twitter" target="_blank"></a>'+
		     '<a href="" class="social-icon google" target="_blank"></a>'+
		     '<a href="" class="social-icon pinterest" target="_blank"></a>'+
		     '</div>')
		  .append('<div class="svfw-back-link" style="text-align: center">'+
			 '<a href="" class="text-link" target="_blank"> Back to '+
			 $('#'+ sectionID).find('h2>a').text()+ 
			 '</a></div>')
		  .find('div.svfw-back-link a.text-link')
		  .on('click tap', function(){
		 	// cl(is_init_homepage);
		  	if ($('body').hasClass('slider-view'))
		  		hide_fslider(); 
		  	// $('#header-logo').trigger('click');
		  	return false; 
		  });
	  	$('#svfw .social-icon').on('click', function(){
	  		var url = '';
			var href = document.location.href.replace(/#/, '%23');
	  		if($(this).hasClass('facebook')){
				url = 'https://www.facebook.com/sharer/sharer.php?u=' + href;
			}else if($(this).hasClass('twitter')){
				url = 'https://twitter.com/home?status=' + href;
			}else if($(this).hasClass('google')){
				url = 'https://plus.google.com/share?url=' + href;
			}else if($(this).hasClass('pinterest')){
				url = 'https://pinterest.com/pin/create/button/?url=https://twitter.com/home?status=' + href + '&media=' + $('.flex-active-slide.loaded img').attr('src') + '&description=';
			}

			if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {
				document.location.href = url;
			}else{
				window.open(url)
			}

			return false
	  	});
	});

};
	
	$('.homepage-section .column .showSlider').on('click.startSlider tap.startSlider', function(e) {
		e.preventDefault();
	})

	// $(document).on('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', '.fullscreen-slider', function() {
	// 	set_fslider_height(this);
	// });
	if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1){
		$('.homepage-section .column.image').each(function (i, el) {
			$(el).data('p', $(el).outerWidth()/25);
			// console.log($(el).css('width'), " | ", $(el).outerHeight(),  " | ", $(el).outerHeight()*$(el).data('p'));
			$(el).width($(el).outerHeight()*$(el).outerWidth()/25);
			$(el).data('h', $(el).outerHeight());
		})
		$(window).on('resize', function() {
			$('.homepage-section .column.image').each(function (i, el) {
				// console.log($(el).data('h'), $(el).outerHeight(), $(el).data('h') != $(el).outerHeight());
				if ($(el).data('h') != $(el).outerHeight()) {
					// console.log($(el).css('width'), " | ", $(el).outerHeight(),  " | ", $(el).outerHeight()*$(el).data('p'));
					$(el).width($(el).outerHeight()*$(el).data('p'));
					$(el).data('h', $(el).outerHeight());
				};
			})
		});
	};

	$('#splash-page .flex-control-nav a').on('click tap', function () {
		var $section = $('#'+$(this).attr('href').slice(4));
		var href = $(this).attr('href');
		show_home_content();
		// console.log($section);
		setTimeout(function(){
			document.location.href = href;
			parse_subpage();
		}, 0)
		
			// if ($section.length){
			// 	$('html, body').animate({
			//         scrollTop: $section.offset().top
			//     }, 2000, 'swing', function () {
			//     	//if ($openlink.length) $openlink.eq(0).trigger('click');
			//     });
			// }
		return false
	});

	// cl(sectionID, slideID);
	parse_subpage();
} // end init homepage function

function init_splash() {
	// cl('init_splash');
	var t = (!!document.location.hash) /*? 5000 : 0*/;
	if (!t) $wnd.on('resize', function() {
		$('#title-page').css('height', $wnd.height());
	}).trigger('resize');

	$.waitForImages.hasImgProperties = ['backgroundImage'];
	var splash = /*document.location.hash>1 ? "" :*/ "#splash-page .slides li";
	var t = (!!document.location.hash) /*? 5000 : 0*/;
	// setTimeout(function() {
	if (t) init_homepage();
	else $(/*'.homepage-section' + ', ' +*/ splash).waitForImages({
			waitForAll: true,
			finished: function() {
				$('#splash-page').flexslider({
					directionNav: false,
					slideshowSpeed: 4000,
					animationSpeed: 1000,
					useCSS: false,
					video: true,
					manualControls: '#splash-page .flex-control-nav li a',
					start: function(slider) {
						$('#splash-page .flex-control-nav a').on('mouseover', function(){
							var slideNum = $(this).index('#splash-page .flex-control-nav a');
							slider.flexAnimate(slideNum);
						});

						$('#splash-page .flex-control-nav a').on('click', function(){
							$('#splash-arrow').trigger('click');
							document.location.href = $(this).attr('href');
						});
					},
					after: function(slider) {
						if (!slider.playing) {
							slider.play();
						}
					}
				});
				// cl('before init_homepage from init_splash', this);
				/*if (!t)*/ init_homepage();
			}
		});
	// }, t);
}

jQuery(document).ready(function($) {
	if ($('body').is('#homepage')) {
		// if (document.location.hash) init_homepage();
		load_page(function() {
			init_splash();
		});
	}
});