$(function(){ 
  $('#page').css('height', $(window).height());
  $('.copyright').css({ 'top': $(window).height()-25, 'left': '45%' });
  $('#page-index div.abutment').css('height', $(window).height());
  $('#i-frame').css('height', $(window).height());
  $('a.link-about').attr('href','#about');
  $('a.link-portfolio').attr('href','#portfolio');
  $('a.link-contact').attr('href','#contact');
  $('a.link-services').attr('href','#services');
  
  $(window).resize(function() {
    $('#page').css('height', $(window).height());
    $('.menu').css('width', '60%');
    $('.copyright').css({ 'top': $(window).height()-25, 'left': '40%' });
    $('#page-index div.abutment').css('height', $(window).height());
  });
  
  if ( $.url.attr('anchor') != null ){
    $('.page-open').removeClass('page-open');
    $('#page-'+$.url.attr('anchor')).addClass('page-open');
    //$('head title').text( $('#main-button').text()+' / '+$('#page-index .link-'+$.url.attr('anchor')).text() );
    effectPageServices($.url.attr('anchor'));
  }
  
  $('a.menu-link').click(function(){
    if ( $('.pages').hasClass('page-new-open') == false ){
      //$('head title').text( $('#main-button').text()+' / '+$(this).text() );
      changePage($(this).attr('class').replace('menu-link link-', ''), $('.page-open').attr('id'));
      effectPageServices($(this).attr('class').replace('menu-link link-', ''));
    } else {
      return false;
    }
  });
  
  /** Page Index
  =================================== **/
  
  $('#main-button').mousedown(function(){
    $(this).addClass('active');
    $('#main-tip-about').slideDown(function(){
      $('#main-tip-portfolio').slideDown(function(){
        $('#main-tip-services').slideDown(function(){
          $('#main-tip-contact').slideDown();
        });
      });
    });
    
    return false;
  }).mouseup(function(){
    $(this).removeClass('active');
    return false;
  });
  
  $('#main-button').click(function(){
    return false;
  });
  
  /** Page Portfolio
  =================================== **/
  
  $('#page-portfolio .content .works .work').hover(function(){
    $('.info', this).fadeIn();
  }, function(){
    $('.info', this).fadeOut();
  });
  
  $('#page-portfolio .content .img .light').click(function(){
    $('#page-portfolio .light-off, #page-portfolio .light-on').fadeIn();
    return false;
  });
  
  $('#page-portfolio .light-on').click(function(){
    $('#page-portfolio .light-off, #page-portfolio .light-on').fadeOut();
  });
  
  $('#page-portfolio .content .pager a').click(function(){
    if ( $(this).hasClass('active') != true ){
      var openPageGal = $(this).attr('rel');
      var closePageGal = $('#page-portfolio .content .pager a.active').attr('rel');
      
      $('#page-portfolio .content .works-'+closePageGal).fadeOut('fast', function(){
        $('#page-portfolio .content .works-'+openPageGal).fadeIn(1000);
      });
      
      $('#page-portfolio .content .pager a').removeClass('active');
      $(this).addClass('active');
    }
    
    return false;
  });
  
  /** Page Contact
  =================================== **/
  
  $('#feedback input, #feedback textarea').bind("focus", function(){
    $(this).addClass('focus');
  }).bind("blur", function(){
    $(this).removeClass('focus');
  });
  
  $('#feedback-name').val('Name').bind("focus", function(){				
    if ( this.value == 'Name' ) { this.value = ''; }
  }).bind("blur", function(){
    if ( this.value == '' ) { this.value = 'Name'; }
  });
  
  $('#feedback-email').val('E-mail').bind("focus", function(){				
    if ( this.value == 'E-mail' ) { this.value = ''; }
  }).bind("blur", function(){
    if ( this.value == '' ) { this.value = 'E-mail'; }
  });
  
  $('#feedback-button').hover(function(){
    $(this).addClass('hover');
  }, function(){
    $(this).removeClass('hover');
  });
  
  $('#feedback-button').click(function(){
    var formError = {};
    var countError = 0;
    var data = {};
    
    $('input, textarea', $('#feedback')).each(function(){
      if ( this.type != 'button' && this.type != 'reset' ) {
        var elementId = this.id;
        var rules = $(this).attr('class');
        rules && $.each(rules.split(' '), function(){
          if (this == 'required' ) formError = checkField('required', formError, elementId);
          if (this == 'mail' && formError[elementId] == null ) formError = checkField('mail', formError, elementId);
        });
        data[this.name] = this.value;
      }
    });
    
    for(var i in formError)
      countError++; 
    
    if (countError == 0){
      $.ajax({ 
        type: "POST", 
        async: false, 
        url: 'php/index.php?option=feedback', 
        data: data, 
        success: function(data){
          $('#feedback').html(data);
        } 
      });
    }
    
    return false;
  });
  
  /** Page Services
  =================================== **/
  
  var arrServicesTipText = new Array();
  
  $('#page-services .service').hover(function(){
    var position = parseInt($('.li',this).css('backgroundPosition').replace('0px ', ''));
    
    if ( position > -70 ) {
      position-=14;
    } else{
      position = 0;
    }
    
    $('.li',this).css('backgroundPosition', '0px ' + position + 'px');
    $('strong',this).addClass('hover');
  }, function(){
    $('strong',this).removeClass('hover');
  });
  
  $('#page-services .service').click(function(){
    var index = $(this).attr('class').replace('service service-', '');
    
    if ( arrServicesTipText[index] == undefined ){
      arrServicesTipText[index] = $('#page-services .i-'+index+' p').text();
    }
    
    $('#page-services .i-'+index+' p').text('');
    $('#page-services .i').removeAttr('style');
    $('#i-frame').show();
    
    $('#page-services .i-'+index).show('scale', {}, 800, function(){
      printText(index, arrServicesTipText, $('p', this), 20);
    });
  });
  
  $('#i-frame').click(function(){
      $(this).removeAttr('style');
      $('#page-services .i').fadeOut();
  });
});

function effectPageServices(page){
  if ( page == 'services' ) {  
    var print_text = new Array();
    
    $('.service').each(function(iq){
      print_text[iq+1] = $('strong',this).text();
    });
    
    if ($.browser.msie) {
      $('#page-services .lines').show(1, function(){
        for( var i = 1; i <= 11; i++ ){
          $('.service-'+i).show();
          printText(i, print_text, $('.service-'+i+' strong'), 80);
        }
      });
    } else {
      $('#page-services .lines').effect("pulsate", { times:3 }, 600, function(){
        for( var i = 1; i <= 11; i++ ){
          $('.service-'+i).show("drop", { direction: "right" }, 1000);
          printText(i, print_text, $('.service-'+i+' strong'), 80);
        }
      });
    }
  }
}

function printText(i, print_text, obj, delay){
  var j = 0;
  var message = print_text[i];
  var wrmsg = function() {
    if ( j <= message.length ) {
      obj.text( message.slice(0, j)+'_' );
      j++;
      setTimeout(wrmsg,delay);
    } else {
      obj.text( message );
    }
  }
  
  setTimeout( wrmsg, delay );
}

function changePage(openPage, page){
  $('#page-'+openPage).addClass('page-new-open');
  
  if ( page == 'page-index' || page == 'page-portfolio' || page == 'page-about' ){
    var cssObj = {'top': ($(window).height()+31)+'px'};
    var speed = 6000;
    
    if ( page == 'page-index' ){
      cssObj = {'left': ($(window).width()+31)+'px'}
    } else if ( page == 'page-about' ){
      cssObj = {'top': -($(window).height()+31)+'px'};
    }
    
    $('div.page-open div.abutment').show();
    $('div.page-open').animate(cssObj, speed, function() {
      $('div.abutment', this).hide();
      $('#page-'+openPage).addClass('page-open').removeClass('page-new-open');
      $(this).removeAttr('style').removeClass('page-open');
    });
  } else if ( page == 'page-contact' ){
    $('div.page-open').hide('puff', 800, function(){
      $('#page-'+openPage).addClass('page-open').removeClass('page-new-open');
      $(this).removeAttr('style').removeClass('page-open');
    });
  } else if ( page == 'page-services' ){
    $('div.page-open').hide('scale', {}, 800, function(){
      $('#page-'+openPage).addClass('page-open').removeClass('page-new-open');
      $(this).removeAttr('style').removeClass('page-open');
      $('.lines, .service').removeAttr('style');
    });
  }
}

function checkField(checkType, formError, elementId){
  var value = $('#'+elementId).val();
  
  if ( checkType == 'required' ){
    if ( value == '' || value == 'Name' || value == 'E-mail' ){
      if ( formError[elementId] == null ){ formError[elementId] = 1; }
      addFieldError(elementId,'This required field.');
      return formError;
    }  
  } else if ( checkType == 'mail' ){
    if ( !(/^[a-zA-Z0-9-\._]*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(value)) ) {
      if ( formError[elementId] == null ){ formError[elementId] = 1; }
      addFieldError(elementId,'Email format error.');
      return formError;
    }
  }
  
  delete formError[elementId];
  deleteFieldError(elementId);
  return formError;
}

function addFieldError(id, text){
  $('#'+id).addClass('error-fild').attr('tip', text);
  $.mel.tipper.init();
}

function deleteFieldError(id){
  $('#'+id).removeClass('error-fild').removeAttr('tip');
}
