jQuery(document).ready(function($) {
	
	var initial = true;
	
	$(window).load(function() {

		//$(".gallery-link").colorbox({rel:'gallery-link', transition:"fade", maxWidth:$(window).width(), maxHeight:$(window).height()});
		
		
		
		if($('.banner-wrapper').length > 0) {
			$('.banner.cycle-slide-active .banner-content').removeAttr('style')
			if($(window).width() > 667)
				$('.banner.cycle-slide-active .banner-content').css({'margin-top': -($('.banner.cycle-slide-active .banner-content').outerHeight(true)/2), top: '80%'}).animate({opacity: 1, top: '50%'}, 500);
			$('.cycle-slideshow').on('cycle-before', function(event, optionHash, outgoingSlideEl, incomingSlideEl) {
				if($(window).width() > 667) {
					$(incomingSlideEl).show();
					$(incomingSlideEl).find('.banner-content').removeAttr('style').css({'margin-top': -($(incomingSlideEl).find('.banner-content').outerHeight(true)/2), top: '80%'});
					$(incomingSlideEl).hide();
					$(incomingSlideEl).find('.banner-content').animate({opacity: 1, top: '50%'}, 500);
				}
			});
		}
		
		
		$('.close-btn').click(function(e) {
			e.preventDefault();
			if($('#flyout-menu #flyout-menu-sub').is(':visible')) {
				$('#flyout-menu #flyout-menu-sub').animate({opacity: 0}, 400, function() {
					$('#flyout-menu #flyout-menu-sub').html('').hide();
				});
			}
			else {
				$('#flyout-menu').animate({top: '-100%', opacity: 0}, 400, function() {
					$('#flyout-menu').hide();
				});
			}
		});
		
		$('.flyout .icon-menu').click(function(e) {
			e.preventDefault();
			$('#flyout-menu').show().css({top: '-100%', opacity: 0}).animate({top: 0, opacity: 1}, 400);
		});
		
		$('#flyout-menu a').click(function(e) {
			if($(this).parent().hasClass('menu-item-has-children')) {
				e.preventDefault();
				$('#flyout-menu #flyout-menu-sub').html($(this).parent().html()).show().css({top: '-100%', opacity: 0}).animate({top: 0, opacity: 1}, 400, function() {
					$('#flyout-menu #flyout-menu-sub ul').delay(100).show().css({top: '-25px', opacity: 0}).animate({top: 0, opacity: 1}, 300);
				});
			}
		});
		
		
		
		if($('.client-list').length > 0) {
			$('.client-list a').click(function(e) {
				e.preventDefault();
				if(!$(this).hasClass('active')) {
					$('.client-list a').removeClass('active');
					$(this).addClass('active');
					if(typeof this.xhr !== 'undefined')
						this.xhr.abort();
					this.xhr = $.getJSON(path+'_/client.php', {id: $(this).attr('data-id')}, function(data) {
						if(data.msg == 'success') {
							if($('.client .icon-cancel').length > 0) {
								$('.client-wrapper').animate({height: 0}, 500, function() {
									$(this).html('').removeAttr('style');
									openClient(data.content);
									$('html,body').animate({ scrollTop: $('.client').offset().top}, 400);
								});
							}
							else
								openClient(data.content);
						}
            $('html,body').animate({ scrollTop: $('.client').offset().top}, 400);
					});
				}
				else {
					$('html,body').animate({ scrollTop: $('.client').offset().top}, 400);
				}
			});
			
			if($('.client .icon-cancel').length > 0) {
				$('.client .icon-cancel').click(function(e) {
					e.preventDefault();
					$('.client-wrapper').animate({height: 0}, 500, function() {
						$(this).html('').removeAttr('style');
						$('.client-list a').removeClass('active');
					});
				});
			}
		}
		
		function openClient(content) {
			$('.client-wrapper').removeAttr('style').html(content);
			var _boxHeight = $('.client-wrapper').height();
			$('.client-wrapper').css({height: 0}).animate({height: _boxHeight}, 600, function() {
				$(this).removeAttr('style');
			});
			$('.client .icon-cancel').click(function(e) {
				e.preventDefault();
				$('.client-wrapper').animate({height: 0}, 500, function() {
					$(this).html('').removeAttr('style');
					$('.client-list a').removeClass('active');
				});
			});
			$('.client-wrapper img').load(function() {
				$(this).fadeIn(400, function() { 
					$(this).parent().css({height: $(this).parent().height(), 'min-height': 0}).removeClass('loading'); 
					$(this).parent().animate({height: $(this).height()}, 400, function() { $(this).css('height', 'auto'); });
				}); 
			});
      setTimeout(function() {
        $('.client-images.loading').removeClass('loading');
      }, 1000);
		}
		
			
		var windowWidth = $(window).width();
		resizing(true);
		$(window).resize(function () {
			//$('.loading').stop(false, true).fadeIn(400);
			clearTimeout($.data(this, 'resizeTimer'));
			$.data(this, 'resizeTimer', setTimeout(function() {
				if($(window).width() != windowWidth) {
					windowWidth = $(window).width();
					resizing(false);
				}
			}, 150));
		});
		
		function resizing(_first) {
			var _width = $(window).width();
			var _height = $(window).height();
			
			if($('.banner-wrapper').length > 0) {
				$('.banner-wrapper').removeAttr('style');
				if(_width > 667)
					$('.banner-wrapper').css({height: $('.banner-img').height()});
			}
			
			if($('.banner').length > 0 && $('.home').length == 0) {
				$('.banner-img').removeAttr('style');
				$('.banner-img img').removeAttr('style');
				if(!_first)
					$('.banner-img img').css({'opacity': 1});
				var marginLeft = ($('.banner-img img').width() - $('.banner-img').width())/2;
				$('.banner-img img').animate({opacity: 1});
				if(marginLeft > 0)
					$('.banner-img img').css({'margin-left': -marginLeft});
				else
					$('.banner-img img').css({width: '100%', height: 'auto'});
				
				$('.banner-content').removeAttr('style');
				if(_width > 667) {
					 if($('.banner-content').outerHeight(true) < $('.banner-img').height()) {
						 $('.banner-content').css({'padding-top': (($('.banner-img').height() - $('.banner-content').outerHeight(true))/2)-parseInt($('.banner-content').css('top').replace('px', ''))});
					 }
				}
			}
			
//			if($('.banner-wrapper').length > 0) {
//				$('.banner.cycle-slide-active .banner-content').css({'margin-top': -($('.banner.cycle-slide-active .banner-content').outerHeight(true)/2)});
//			}
			
			if($('.blog-posts').length > 0) {
				var _postTitleHeight = 0;
				var _postBodyHeight = 0;
				$('.blog-posts .recent-post').each(function() {
					$(this).find('h4').removeAttr('style');
					$(this).find('p.desc').removeAttr('style');
					if(_width > 667) {
						_postTitleHeight = ($(this).find('h4').height() > _postTitleHeight) ? $(this).find('h4').height() : _postTitleHeight;
						_postBodyHeight = ($(this).find('p.desc').height() > _postBodyHeight) ? $(this).find('p.desc').height() : _postBodyHeight;
					}
				});
				if(_postTitleHeight > 0 && _postBodyHeight > 0) {
					$('.blog-posts .recent-post h4').css('height', _postTitleHeight);
					$('.blog-posts .recent-post p.desc').css('height', _postBodyHeight);
				}
			}
			
			if($('.client-wrap').length > 0) {
				$('.client-wrap').removeAttr('style');
				if(_width > 480) {
					var _row = new Array();
					var _rowHeight = 0;
					var _previousTop = 0;
					$('.client-wrap').each(function() {
						if(_previousTop != $(this).position.top) {
							for(var i=0; i < _row.length; i++) {
								_row[i].css('height', _rowHeight);
							}
							_previousTop = $(this).position.top;
							_row = new Array();
						}
						_row.push($(this));
						_rowHeight = ($(this).height() > _rowHeight) ? $(this).height() : _rowHeight;
					});
					for(var i=0; i < _row.length; i++) {
						_row[i].css('height', _rowHeight);
					}
					
				}
				
			}
			
			
			if($('.highlight-box').length > 0) {
				$('.highlight-box .content-row-image img').removeAttr('style');
				if(_width > 600) {
					$('.highlight-box').each(function() {
						if($(this).find('.content-row-image img').height() > $(this).outerHeight(true)) {
							$(this).find('.content-row-image img').css({'margin-top': -(($(this).find('.content-row-image img').height() - $(this).outerHeight(true))/2)});
						}
						else {
							$(this).find('.content-row-image img').css({height: $(this).outerHeight(true), width: 'auto', 'max-width': 'none'});
							$(this).find('.content-row-image img').css({'margin-left': -(($(this).find('.content-row-image img').width() - $(this).find('.content-row-image').width())/2)});
						}
					});
				}
			}
			
						
			$('.popup-link').click(function(e) {
				e.preventDefault();
				$('body').addClass('no-scroll');
				$('.popup-wrapper .popup-inner .popup-content').html('<iframe src="'+$(this).attr('href')+'" width="640" height="360" frameborder="0"></iframe>');
				$('.popup-wrapper').fadeIn(400);
			});
			
			
			$('.popup-wrapper').click(function(e) {
				e.preventDefault();
				$('body').removeClass('no-scroll');
				$(this).fadeOut(400);
			});
			$('.popup-wrapper .popup-inner').click(function(e) {
				e.stopPropagation();
			});
			
			
			
			
//			if($('.banner img').length > 0) {
//				var _headerImg = $('.banner img');
//				_headerImg.removeAttr('style').css('height', 'auto');
//				var _headerHeight = $('.banner').outerHeight(true);
//				if(_headerHeight > _headerImg.height()) {
//					_headerImg.css({height: _headerHeight, width: 'auto'});
//					if(_headerImg.width() > _width)
//						_headerImg.css({'margin-left': -(_headerImg.width()-$('#header').width())/2});		
//				}
//			}
			
			
			/*
			if($('.banner').length > 0) {
				var _height = $(window).height()-($('.section-split:eq(0) h3').position().top+$('.section-split:eq(0) h3').height());
				var _width = $(window).width();
				var _top = parseInt($('.banner-wrapper .banners .banner .banner-item-content-wrapper').css('top').replace('px', ''));
				var _minHeight = 0;
				
				$('.banner-wrapper').css('min-height', 0);
				$('.banner:not(.cycle-sentinel)').each(function(c) {
					$(this).css('position', 'absolute').show();
					var _bannerHeight = $(this).find('.banner-item-content-wrapper').height() + _top + (_top - $('#header').height())+$('#banner-nav').height()+parseInt($('#banner-nav').css('bottom').replace('px', ''));
					_minHeight = (_bannerHeight > _minHeight) ? _bannerHeight : _minHeight;
				});
				
				_height = (_height > _minHeight) ? _height : _minHeight;
				_height = (_height > 900) ? 900 : _height;
				$('.banner').css({'height': _height});
				
				$('.banner:not(.cycle-sentinel)').each(function(c) {
					$(this).find('img').removeAttr('style');
					if($(this).find('img').height() < _height) {
						$(this).find('img').css({height: _height, width: 'auto'});
						//if($(this).find('img').width() < _width)
						//	$(this).find('img').css({height: _height, width: '100%'});
					}
					
					if($(this).find('img').width() > _width)
						$(this).find('img').css({'margin-left': -(Math.round(($(this).find('img').width()-_width)/2))});
					if($(this).find('img').height() > _height)
						$(this).find('img').css({'margin-top': -(Math.round(($(this).find('img').height()-_height)/2))});
						
					if($(this).hasClass('cycle-slide-active')) {
						$(this).find('.banner-item-content-wrapper').removeAttr('style');
						//$(this).find('.banner-item-content-wrapper').css({top: '50%', 'margin-top': -($(this).find('.banner-item-content').height()/2)});
					}
					else
						$(this).hide();
				});
			}
			*/
			


			initial = false;

			$('.loading').fadeOut(400);
		}
		
		document.addEventListener( 'wpcf7submit', function( event ) {
			submitMC();
		}, false );

		submitMC = function() {
			$.ajax({
				data: $(".wpcf7-form").serialize(),
				type: "POST",
				url: path+"mc/form.php",
				cache : false,
				dataType: 'json',
				success: function(response, textStatus, jqXHR) {
				},
				error: function (xhr, textStatus, errorThrown) {
					console.log(xhr.responseText);
				}
			});
		}
		
	});		
});