$(document).ready(function() {
	$('input[type=text], textarea').focus(function(){
		var currentVal = $(this).val();										// Read initial field value
		$(this).val('');														// Clear initial value

		$(this).blur(function(){
			var getNewVal = $(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				$(this).val(currentVal);										// Switch to initial value
			}
		});
	});

	$('ul.prjnav>li:last').css('border-bottom','none');

	// fix png for IE
	$('img[src$=.png], div').ifixpng();

	// Safari CSS
	if ( $.browser.safari ) {
		$("head").append('<link href="css/safari.css" media="screen" rel="stylesheet" type="text/css" />');
	}

	// hover menu
	$("#navigator li").not("#on").hover(
		function(){
			$(this).css("background-position","left bottom");
			$(this).children("a").css("background-position","right bottom");
		},
		function(){
			$(this).css("background-position","left top");
			$(this).children("a").css("background-position","right top");
		}
	);

	$("#navigator li#on").each(function(){
		$(this).css("background-position","left bottom");
		$(this).children("a").css("background-position","right bottom");
	});

	// cycle screenshots
	if ( $("a.site-preview").length ){
		$('a.site-preview').each(function(){
			$(this).cycle({
				fx:     'blindY',
				speed:  300,
				next:   $(this),
				timeout: 0
			});
		});

		$("a.site-preview").click(function(){return false;});
	}

	// new cycle for screenshots
	if ( $('#gallery').length ){
		$('#biggest').cycle({
			fx: 'scrollHorz',
			speed:    500, 
    	timeout:  0
		});
		
		$('.pager a').click(function(){			

			/* // First version animation
			$('#biggest img').slideToggle('fast',function(){
				$('#biggest img').attr('src',newImage).slideToggle('fast');
			});*/
			
			/* // Second version animation
			var newImage = $(this).attr('href');
			$('#biggest img').animate(
				{
					left:"-=496"
				},
				{
					duration:'fast',
				    complete: function(){
						$('#biggest img').attr('src',newImage).animate(
							{
								left:"+=496"
							},
							{ duration:'slow' }
						);
					}
				}
			);

			$('.pager a').removeClass('current');
			$(this).addClass('current');
			*/
		  
			$('#biggest').cycle($('.pager a').index($(this)));
			
			$('.pager a').removeClass('current');
			$(this).addClass('current');

			return false;
		});
	}
	
	// image preloader
	var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
  
  // for project page
  if ( $('#gallery .pager').length ) {
  	for ( var idx= 0; idx < $('#gallery .pager li').length; idx++ ){
  		jQuery.preLoadImages($('#gallery .pager li a:eq('+idx+')').attr('href'));
  	}
  }

  // for contact page
  
  $('form#contactForm').submit(function(){
	  $error = 'Errors found:';
	  //name
	  if($('input#name').val() == 'Your Name') 
		  $error += '\n - Must fill your name';
	  //email
	  if($('input#email').val() == 'Your Email') 
		  $error += '\n - Must fill your email';
	  else {
		  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		  if (!filter.test($('input#email').val())) {
			  $error += '\n - Email is in wrong format';
		  }
	  }
	  
	  if($('textarea#message').val() == 'Your Message')
		  	$error += '\n - Fill your message';
	
	  if($error != 'Errors found:') {
		  alert($error);
		  return false;
		
	  }
		  
	  
  });
});

var detect = navigator.userAgent.toLowerCase();
var os_ = navigator.platform.toLowerCase();
var browser,thestring;

if (checkIt('safari')) browser = "Safari"
else browser = "not Safari"

if ( os_ == 'macppc' || os_ == 'macintel' ) os = "Mac"
else os = "not Mac"

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
