/* 
   # ###
   # #  
   # ###
   #   #  "Javascripts: martinkrakowski.com"
 ### ###  

*/

/*#
  # check if div exists */
jQuery.fn.exists = function(){return jQuery(this).length>0;}


/*#
  # Nav item animation */

$(function() {
	$('a.navitem').hover(function() { navHover(this, true); }, 
	function() { navHover(this, false); });
	$('#logo-link').hover(function() { navHover($('li#home a'), true); }, 
	function() { navHover($('li#home a'), false); });
});

function navHover(e, mouseOverStatus) {
  if (mouseOverStatus != false) {
    $(e).find('span').animate({ height: '4px' }, 100);
  	return false;
  } else { $(e).find('span').animate({ height: '0px' }, 100); }
}



/*#
  # Work snapshot rollovers */

$(function() {
  $('.snapshot').hover(function() {
    $(this).find('div.view').fadeIn(300);
  }, function() {
    $(this).stop();
    $(this).find('div.view').fadeOut(100);
  });
});



/*#
  # About process rollovers */

$(function() {
  $('.process-steps').hover(function() {

    var steps = $(this).attr('id');
    var process = $('#display-process');
    
    switch(steps) {
      case 'step01' : 
            process.css({backgroundPosition: '0px -246px'})
            break;
      case 'step02' : 
            process.css({backgroundPosition: '0px -492px'})
            break;
      case 'step03' : 
            process.css({backgroundPosition: '0px -738px'})
            break;
    } 
  }, function() {
            $('#display-process').css({backgroundPosition: '0px 0px'})
  });
});



/*#
  # homepage `expertise` panels (slider) */

$(function() {
  var container =  $('#expertise #panels');
  var navigation = $('#expertise ul#expertise-panel-navigation li a');

  $(navigation).click(function() {
    $('.active-panel').removeClass('active-panel');
    $(this).addClass('active-panel');
    var getPanel = $(this).attr('rel');
    $(container).scrollTo(getPanel, 800);
    return false;
  });
});



/*#
  # project listing page `highlighing` */

$(function() {

  $('.snapshot').hover(function() {
    $(this).addClass('snapshot-hover');
    $(this).click(function() { window.location = $(this).find('a:first-child').attr('href'); });
  }, function() {
    $(this).removeClass('snapshot-hover');
  });

});



/*#
  # Scroll to top of page */

$("a[href='#top']").click(function() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
  return false;
});



/*#
  # Simple validation: Martin Krakowski */
  
$(function() {
  
  if ($('form').attr('rel') != 'nofocus') { //Determine if we need to focus the first field
    $("input[type='text']:first", document.forms[0]).focus().addClass('active', 300);  //Focus in first text field
  }
	
	$('input:text, textarea').focus(function() {
	  $(this).addClass('active', 300);
    $(this).removeClass('form-error active');	  
	});
  
  $('input:text, textarea').bind('change, blur', function(){  // Validate on focus/blur
    if ($(this).attr('rel') != 'optional') {

      var elval = $(this).val(); //Value of current element
    
      if ( elval != '' ) { $(this).removeClass('form-error active'); }
      else { $(this).addClass("form-error"); }
    }
  });


  $('#submitform').click(function() { //Validate on form submission
  var errors = 0;
  
    $('input:text, textarea').each(function(i) {
      if ($(this).attr('rel') != 'optional') {
        var elval = $(this).val();

        if ($(this).attr('name') == 'email') { // Email validation
          var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
          if(reg.test(elval) == false) {       
            var message = 'You entered an Invalid Email Address';
            errors++;
          }
        } else {
        
          if ( elval != '' ) { $(this).removeClass("form-error"); } 
          else { 
            $(this).addClass("form-error"); 
            errors++;
          }
        }
      }
    });
    
    if ($('#state').val() == '') { errors++; }
    if ( errors > '0') { 
        alert("There are "+errors+" errors in your submission. Please modify the highlighted field(s)."); 
        return false;
        }
    else { 

        $(document.forms[0]).submit(); 
        //return false;

    } 
    
  });

});

  
/*#
  # Twitter */

$(function(){
  if ($('#twitter-feed').exists()) {

    $("#twitter-feed").tweet({
      join_text: "auto",
      username: "martinkrakowski",
      count: 1,
      auto_join_text_default: "we said,",
      auto_join_text_ed: "we",
      auto_join_text_ing: "we were",
      auto_join_text_reply: "we replied",
      auto_join_text_url: "we were checking out",
      loading_text: "loading tweets..."
    });
  
    //get number of followers
    $.ajax({
      url: 'http://api.twitter.com/1/users/show.json',
      data: {screen_name: 'martinkrakowski'},
      dataType: 'jsonp',
      success: function(data) {
        $('#twitter-followers span').html(data.followers_count);
      }
    });
  }

});



/*#
  # Flickr */


if ($('#flickr-container').exists()) {

  $('#flickr-container').jflickrfeed({
  	limit: 5,
  	qstrings: {
      set: '72157627576343351',
  		nsid: '67677142@N08'
  	},
  	useTemplate: false,
  	itemCallback: function(item){
  		$(this).append('<div class="photo"><a class="lightbox" href="' + item['image_b'] + '"><img width="120" height="120" src="' + item.image_m + '" alt="" /></a></div>');
  
  	}
  });

  /* Lightbox for flickr feed */
  $(function() { setTimeout("$('.lightbox').lightBox()", 500); });
}


/*#
  # google analytics */
  
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20166001-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();




