// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){
  //$('#sections ul li').css('height', $(ducument).height()+'px');
  
  if ( $.browser.msie ) {
  	if ( $.browser.version == '6.0' ){
  	  $('#header-line').css('width', ($(document).width()-16)+'px');
      $('#sections, #sections ul li').css('width', ($(document).width()-16)+'px');
  	}else{
      $('#header-line').css('width', ($(document).width()-21)+'px');
      $('#sections, #sections ul li').css('width', ($(document).width()-21)+'px');
    }
  } else {
    $('#header-line').css('width', $(document).width()+'px');
    $('#sections, #sections ul li').css('width', $(document).width()+'px');
  }
  $('a.prev').css('left', ((($(document).width() - $('#sections .page').width())/2)-172)+'px');
  $('a.next').css('right', ((($(document).width() - $('#sections .page').width())/2)-172)+'px');
	$('#page').serialScroll({
		target:'#sections',
		items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'a.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'a.next',// Selector to the 'next' button (absolute too)
		axis:'xy',// The default is 'y' scroll on both ways
		navigation:'#menu li a',
		duration:700,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		
		//queue:false,// We scroll on both axes, scroll both at the same time.
		//event:'click',// On which event to react (click is the default, you probably won't need to specify it)
		//stop:false,// Each click will stop any previous animations of the target. (false by default)
		//lock:true, // Ignore events if already animating (true by default)		
		start: 1, // On which element (index) to begin ( 0 is the default, redundant in this case )		
		//cycle:true,// Cycle endlessly ( constant velocity, true is the default )
		//step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
		//jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
		//lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
		//interval:1000, // It's the number of milliseconds to automatically go to the next
		//constant:true, // constant speed
		
		onBefore:function( e, elem, $pane, $items, pos ){
			e.preventDefault();
			if ( this.blur ) { this.blur(); }
			$('#page .next, #page .prev').hide();
			if($.browser.opera){ $('#sections li div.page').show('fast'); }
		},
		onAfter:function( elem ){
			$('#sections').css('height', ($('#'+elem.id).height()+118)+'px');
			$('#page .next, #page .prev').show();
			$('#menu ul li').removeClass( 'active' );
			if ( elem.id == 'page-about' ) {
        $('#menu ul li').eq(0).addClass( 'active' );
        if($.browser.opera){ $('#page-portfolio div.page, #page-contacts div.page').hide(); }
			} else if ( elem.id == 'page-portfolio' ) {
        $('#menu ul li').eq(1).addClass( 'active' );
        if($.browser.opera){ $('#page-about div.page, #page-contacts div.page').hide(); }
			} else if ( elem.id == 'page-contacts' ) {
        $('#menu ul li').eq(2).addClass( 'active' );
        if($.browser.opera){ $('#page-about div.page, #page-portfolio div.page').hide(); }
			}
		}
	});
	
  $("#menu ul li").click( function(){
    $("#menu ul li").removeClass( 'active' );
    $(this).addClass( 'active' );
  });
  
  $('.services-tip').attr('title','');
  $('.button-submit').bt({ width: 50, padding: 1, cornerRadius: 0, strokeStyle: '#b6bfca', fill: '#b6bfca', spikeGirth: 0, spikeLength: 0, cssStyles: {color: '#fff', textAlign: 'center', fontSize: '10px', textTransform: 'uppercase', letterSpacing: '2px'}, positions: ['right', 'left', 'bottom'] });
  /*
  $('.services-tip').bt({ width: 300, padding: 10, cornerRadius: 0, strokeStyle: '#7e8790', fill: '#7e8790', spikeGirth: 32, spikeLength: 9, cssStyles: {color: '#000', textAlign: 'justify'} });
  */
});

$(function(){ 
  $('form').find('input[type=text], textarea').focus(function(){ $(this).attr('style','border: #9ca6b2 solid 1px;'); });
  $('form').find('input[type=text], textarea').blur(function(){ $(this).removeAttr('style'); });
  $('input[name=project_platform]').click(function(){ 
    if ( this.value == 5 ) {
      $('#project-platform-specify').removeAttr('disabled').removeClass('input-disabled'); 
    } else {
      $('#project-platform-specify').attr('disabled','disabled').addClass('input-disabled');
    }
  });
  
  /*
  $('#project-platform-specify').attr( 'disabled', 'disabled' ); 
  */
  $('input[type=submit]', '#feedback').click(function(){
    var formError = 0;
    var data = {};
    $('input, select, textarea', $('#feedback')).each(function(){
      if ( this.type != 'submit' && this.type != 'button' && this.type != 'reset' ) {
        var elementId = this.id;
        var rules = $(this).attr('class');
        data[this.name] = this.value;
        rules && $.each(rules.split(' '), function() {
          if (this == 'required' && formError != 1 ){ if (checkRequiredField(elementId)){ formError = 1; } else { formError = 0; } }
          if (this == 'mail' && formError != 1){ if (checkMailField(elementId)){ formError = 1; } else { formError = 0; } }
        });
      }
    });
    if ( formError != 1 ) {
      $('.bt-wrapper').remove();
      
      $.ajax({ 
        type: "POST", 
        async: false, 
        url: 'php/index.php?option=feedback', 
        data: data, 
        beforeSend: function(XMLHttpRequest){
          $('.contact_form .info').removeAttr('colspan');
          $('.contact_form .c-2, .contact_form .c-3').remove();
          $('.contact_form .c-1').empty().append('<div class="loader-form"></div>');
        },
        success: function(data){
          $('.contact_form .c-1 .loader-form').remove();
          $('.contact_form .c-1').html(data);
          $('#sections').css('height', $('#page-contacts').height()+'px');
          
        } 
      });
    }
    return false;
  });
  
  $('input[type=submit]', '#order').click(function(){
    var formError = 0;
    var data = {};
    $('input, select, textarea', $('#order')).each(function(){
      if ( this.type != 'submit' && this.type != 'button' && this.type != 'reset' ) {
        var elementId = this.id;
        var rules = $(this).attr('class');
        
        if ( this.type == 'radio' || this.type == 'checkbox' ) {
          if ( this.checked == true ){
            data[this.name] = this.value;
          }
        } else {
          data[this.name] = this.value;
        }
        
        rules && $.each(rules.split(' '), function() {
          if (this == 'required' && formError != 1 ){ if (checkRequiredField(elementId)){ formError = 1; } else { formError = 0; } }
          if (this == 'mail' && formError != 1){ if (checkMailField(elementId)){ formError = 1; } else { formError = 0; } }
        });
      }
    });
    
    if ( formError != 1 ) {
      $('.bt-wrapper').remove();
      
      $.ajax({ 
        type: "POST", 
        async: false, 
        url: 'php/index.php?option=order', 
        data: data, 
        beforeSend: function(XMLHttpRequest){
          $('.order_form .c-2, .order_form .c-3, .order_form .c-4').remove();
          $('.order_form .c-1').empty().append('<div class="loader-form"></div>');
        },
        success: function(data){
          $('.order_form .c-1 .loader-form').remove();
          $('.order_form .c-1').html(data);
          $('#sections').css('height', $('#page-contacts').height()+'px');
        } 
      });
    }
    
    return false;
  });
  
  $.ajax({ 
    type: "GET", 
    url: 'php/index.php?option=portfolio', 
    success: function(data){ 
      $('#page-portfolio .page').html(data); 
      $('#sections').css('height', ($('#page-portfolio').height()+118)+'px');
    } 
  });
});

function checkRequiredField(elementId){
  var fieldValue = $('#'+elementId).val();
  var fieldLabel = $('label[for=' + elementId + ']');
  
  if ( fieldValue == '' ) {
    addFieldError(fieldLabel,elementId,'This required field.');
    return true;
  }
  
  deleteFieldError(fieldLabel,elementId);
  return false;
}

function checkMailField(elementId){
  var fieldValue = $('#'+elementId).val();
  var fieldLabel = $('label[for=' + elementId + ']');
  
  if ( !(/^[a-zA-Z0-9-\._]*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(fieldValue)) ) {
    addFieldError(fieldLabel,elementId,'Email error.');
    return true;
  }
  
  deleteFieldError(fieldLabel,elementId);
  return false;
}

function addFieldError(label, id, text){
  if ( $('.error-msg',label).size() <= 0 ) {
    label.html( label.html() + " <span class='error-msg'>"+text+"</span>" );
    $('#'+id).addClass('error-fild');
  }
}

function deleteFieldError(label, id){
  $('.error-msg', label).remove();
  $('#'+id).removeClass('error-fild');
}
