/* 	Author: Alex Cho
	Company: Harlo Interactive
*/
$(window).load(function(){

	_theWindow = $(window);
	$bg = $('.imgbg');
	_aspectRatio = $bg.width()/$bg.height();

	$bheight = _theWindow.height();   // returns height of browser viewport
	$bwidth = _theWindow.width();
	$bgRatio = $bwidth / $bheight;

	$calcHeight = $bwidth * _aspectRatio;
	$calcWidth = $bheight / _aspectRatio;
	$calcLeft = Math.round(($calcWidth - $bwidth)/2);
	$calcTop = (Math.round(($calcHeight - $bheight)/2))/2;
	
	if($bgRatio < _aspectRatio) {
		$bg.css({'left':'-'+$calcLeft+'px', 'top':0}).removeClass().addClass('bgheight');
    } else {
		$bg.css({'left':0, 'top': '-'+$calcTop+'px'}).removeClass().addClass('bgwidth');
	}
	
	_theWindow.resize(function() {
		var aspectRatio = $bg.width()/$bg.height();

		$bheight = _theWindow.height();   // returns height of browser viewport
		$bwidth = _theWindow.width();
		$bgRatio = $bwidth / $bheight;

		$calcHeight = $bwidth * _aspectRatio;
		$calcWidth = $bheight / _aspectRatio;
		$calcLeft = Math.round(($calcWidth - $bwidth)/2);
		$calcTop = (Math.round(($calcHeight - $bheight)/2))/2;

		if($bgRatio < _aspectRatio) {
			$bg.css({'left':'-'+$calcLeft+'px', 'top':0}).removeClass().addClass('bgheight');
	    } else {
			$bg.css({'left':0, 'top': '-'+$calcTop+'px'}).removeClass().addClass('bgwidth');
		}
		
		return this;
	});	
	
	$top = ($(window).height() - $('nav').height()-50)/2;

	$('.bx-wrapper').css({height:$(window).height(), width:$(window).width()});
	$('.bx-window').css({height:$(window).height(), width:$(window).width()});
	$('ul.homeSlider').css({height:$(window).height(), width:$(window).width()});
	if($(window).height() < 590 && $(window).width() < 1100 && $top >= '15'){
		$('nav').css({position:'absolute'}).stop().css({top:$top}, 300);
		$('footer').css({position:'fixed'});
	}else if($(window).height() < 590 && $(window).width() < 1100 && $top <= '15'){
		$('nav').css({position:'absolute'}).stop().css({top:'15px'}, 300);
		$('footer').css({position:'absolute'});
	}else if($(window).height() < 590 && $(window).width() > 1100 && $top >= '15'){
		$('nav').css({position:'absolute'}).stop().css({top:$top}, 300);
		$('footer').css({position:'absolute'});
	}else if($(window).height() < 590 && $(window).width() > 1100 && $top <= '15'){
		$('nav').css({position:'absolute'}).stop().css({top:'15px'}, 300);
		$('footer').css({position:'absolute'});
	}else if($(window).height() > 590 && $(window).width() < 1100){
		$('nav').css({position:'absolute'}).stop().css({top:$top}, 300);
		$('footer').css({position:'fixed'});
	}else{
		$('nav').css({position:'fixed'}).stop().css({top:$top}, 300);
		$('footer').css({position:'fixed'});
	}
	
	// --------------------------------------------------------------------------------------------
	// Background
	// --------------------------------------------------------------------------------------------
	var mainImg = new Image();
	var bck = $('.bg_Image').attr('data-default');
	$(mainImg).load(function(){
		$('.preloader').fadeOut(350, function() { $(this).remove(); });
		$('.bg_Image').css({'background-image':"url('"+baseurl+bck+"')"});
		$('.bg_Image img').attr('src', baseurl+bck);
		$('nav').css({'display' : 'block'});
	}).attr('src', baseurl+bck);

	// $('.ie9 .bg_Image').css({
	// 		'background':"none",
	// 		'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+baseurl+$bg+"', sizingMethod='scale')",
	// 		'-ms-filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+baseurl+$bg+"', sizingMethod='scale')"
	// 	});
	
});

$(document).ready(function(){

	// Disable Links
	// --------------------------------------------------------------------------------------------
	$('[href="#"], .disabled').click(function() { return false; });

	// Login Form Elements
	// --------------------------------------------------------------------------------------------
	$('.input, textarea').bind('focusin', function() { if( $(this).val() == $(this).attr('data-default') ) { $(this).val(''); } });
	$('.input, textarea').bind('focusout', function() { if( $(this).val() == '' ) { $(this).val( $(this).attr('data-default') ); } });
	
	// --------------------------------------------------------------------------------
	// Contact Form Validation
	// --------------------------------------------------------------------------------

	$('form.contactFrm .json').val('1');
	$('form.contactFrm').submit(function() {
		var $self = $(this),
			$allFields = $self.find('input, textarea').not(':submit, :checkbox, :radio, [type="hidden"]'),
			$reqFields = $self.find('.req'),
			$message = $self.find('.message'),
			_errorMsg = 'You did not complete all of the fields. Please try agian.',
			$emailField = $self.find('input.email'),
			_emailRegEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		e=0;
		$reqFields.css({'border':'1px solid #fff'});
		// $phoneField.css({'border-color':'#ccc'});

		// Check the Required Fields
		$reqFields.each(function() {
			if($(this).val() == '' || $(this).val() == $(this).attr('data-default')) { e=1; $(this).css({'border':'1px solid #3d3c3c'}); }
		});
	
		// Hide any previous messages
		$message.hide().removeClass('error success');
	
		// Check for errors
		if(e==1) { $message.addClass('error').html(_errorMsg).show(); }
		// Check for a valid Email Address
		else if( _emailRegEx.test($emailField.val()) == false ) {
			$emailField.css({'border':'1px solid #3d3c3c'});
			$message.addClass('error').html('Please provide a valid email address.').show();
		}
		// No Errors - Proceed with Form Processing
		else
		{
			// Get the form data and add the submit button
			var formData = $self.serialize();
			formData = formData + '&' + $self.find('[type="submit"]').attr("name") + '=1';
		
			// Submit the Form with AJAX
			$.ajax({
				url: $self.attr('action'),
				data: formData,
				type: 'post',
				dataType: 'json',
				success: function(data)
				{
					if(data.error != undefined && data.error == '1') { $message.addClass('error').html(data.msg).show(); }
					else { $allFields.val('').focus(); $message.addClass('success').html(data.msg).show(); }
				}
			});
		}
	
		return false;
	});
	
	
	// --------------------------------------------------------------------------------------------
	// Sidebar Scroll
	// --------------------------------------------------------------------------------------------
	$('.scroll').jScrollPane({showArrows: false,verticalDragMaxHeight: 32,verticalDragMinHeight: 32,verticalGutter: 0});

	// --------------------------------------------------------------------------------------------
	// Nav Animation
	// --------------------------------------------------------------------------------------------
	$('li.main_nav').hover(function(){
		var $slideout = $(this).find('a'), _paddingRight = '50px', _speed = 250;
		$slideout.stop().animate({ 'padding-right': _paddingRight}, _speed, function(){
			$(this).parent().find('.subnavContain.inactive').css({'display':'block'});
			$(this).parent().find('.subnavContain.inactive ul.subnav').css({'display':'block'}).stop().animate({height:'30px', opacity:1}, _speed);
		});
	}, function(){
		var $slideout = $(this).find('a'),  _paddingRight = '50px', _speed = 250;
		$slideout.stop().animate({ 'padding-right': 0}, _speed);
		$(this).find('.subnavContain.inactive ul.subnav').css({'display':'none'}).stop().animate({height:0, opacity:0}, _speed, function(){
			$(this).parent().find('.subnavContain.inactive').css({'display':'none'});
		});
	});

	$(window).resize(function(){
		$('ul.homeSlider').css({height:$(window).height(), width:$(window).width()});
		$('.bx-wrapper').css({height:$(window).height(), width:$(window).width()});
		$('.bx-window').css({height:$(window).height(), width:$(window).width()});
		$top = ($(window).height() - $('nav').height()-50)/2;
		if($(window).height() < 590 && $(window).width() < 1100 && $top >= '15'){
			$('nav').css({position:'absolute'}).stop().animate({top:$top}, 300);
			$('footer').css({position:'fixed'});
		}else if($(window).height() < 590 && $(window).width() < 1100 && $top <= '15'){
			$('nav').css({position:'absolute'}).stop().animate({top:'15px'}, 300);
			$('footer').css({position:'absolute'});
		}else if($(window).height() < 590 && $(window).width() > 1100 && $top >= '15'){
			$('nav').css({position:'absolute'}).stop().animate({top:$top}, 300);
			$('footer').css({position:'absolute'});
		}else if($(window).height() < 590 && $(window).width() > 1100 && $top <= '15'){
			$('nav').css({position:'absolute'}).stop().animate({top:'15px'}, 300);
			$('footer').css({position:'absolute'});
		}else if($(window).height() > 590 && $(window).width() < 1100){
			$('nav').css({position:'absolute'}).stop().animate({top:$top}, 300);
			$('footer').css({position:'fixed'});
		}else{
			$('nav').css({position:'fixed'}).stop().animate({top:$top}, 300);
			$('footer').css({position:'fixed'});
		}
	});
	
	// --------------------------------------------------------------------------------------------
	// Sliders
	// --------------------------------------------------------------------------------------------
	var mainSlider = $('.homeSlider').bxSlider({
		auto:true, startingSlide:0, displaySlideQty:1, moveSlideQty:1, mode:'fade',
		infiniteLoop: true, controls: false, speed: 650, pager:false, pause:6000,
		onBeforeSlide:function(a,b,c){
			var img = new Image();
			var slide_bck = c.attr('data-background');
			$(img).load(function(){
				$('.bg_Image').css({'background-image':"url('"+baseurl+slide_bck+"')"});
				$('.bg_Image img').attr('src', baseurl+slide_bck);
			}).attr('src', baseurl+slide_bck);
		
		}
	});
	
	
	// --------------------------------------------------------------------------------------------
	// Background
	// --------------------------------------------------------------------------------------------
	// $bg = $('.bg_Image').attr('data-default');
	// $('.bg_Image').css({'background-image':"url('"+baseurl+$bg+"')"});

});



