// ============================== Menu Animation
(function(jQuery){
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	var colors = {
		aqua:[0,255,255],
		azure:[240,255,255],
		beige:[245,245,220],
		black:[0,0,0],
		blue:[0,0,255],
		brown:[165,42,42],
		cyan:[0,255,255],
		darkblue:[0,0,139],
		darkcyan:[0,139,139],
		darkgrey:[169,169,169],
		darkgreen:[0,100,0],
		darkkhaki:[189,183,107],
		darkmagenta:[139,0,139],
		darkolivegreen:[85,107,47],
		darkorange:[255,140,0],
		darkorchid:[153,50,204],
		darkred:[139,0,0],
		darksalmon:[233,150,122],
		darkviolet:[148,0,211],
		fuchsia:[255,0,255],
		gold:[255,215,0],
		green:[0,128,0],
		indigo:[75,0,130],
		khaki:[240,230,140],
		lightblue:[173,216,230],
		lightcyan:[224,255,255],
		lightgreen:[144,238,144],
		lightgrey:[211,211,211],
		lightpink:[255,182,193],
		lightyellow:[255,255,224],
		lime:[0,255,0],
		magenta:[255,0,255],
		maroon:[128,0,0],
		navy:[0,0,128],
		olive:[128,128,0],
		orange:[255,165,0],
		pink:[255,192,203],
		purple:[128,0,128],
		violet:[128,0,128],
		red:[255,0,0],
		silver:[192,192,192],
		white:[255,255,255],
		yellow:[255,255,0]
	};
	
})(jQuery);

$(function() {
    var $el, leftPos, newWidth;
        $mainNav2 = $("#example-two");

		$mainNav2.append("<li id='magic-line-two'></li>");
    
    var $magicLineTwo = $("#magic-line-two");
    
    $magicLineTwo
        .width($(".current_page_item_two").width())
        .height($mainNav2.height())
        .css("left", $(".current_page_item_two a").position().left)
        .data("origLeft", $(".current_page_item_two a").position().left)
        .data("origWidth", $magicLineTwo.width())
        .data("origColor", $(".current_page_item_two a").attr("rel"));
		
    $("#example-two li.page_item a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
		
		if ( $magicLineTwo.css("left") != $magicLineTwo.attr("origLeft") + "px" )
		{
			$(".current_page_item_two a").animate({ color: "#616161" },150);
		}
		
		$(this).animate({ color: "#FFFFFF" },300);
        $magicLineTwo.stop().animate({
            left: leftPos,
            width: newWidth,
            backgroundColor: $el.attr("rel")
        })
    }, function() {
        $magicLineTwo.stop().animate( {left: $magicLineTwo.data("origLeft"), width: $magicLineTwo.data("origWidth"), backgroundColor: $magicLineTwo.data("origColor")}, function(){ $(".current_page_item_two a").animate({ color: "#FFFFFF" },300); /*draw($(".current_page_item_two a").attr('class'));*/ });
    });
	
	$("#example-two li.page_item a").mouseleave(function() { $(this).animate({ color: "#616161" },150); } );
	
    $(".current_page_item_two a").mouseenter();
});

// =========================================== Scroll to top of the page

is_scroll_button_visible = false;

// show or hide scroll top button
$(window).bind('scroll', function ()
{
	if ( $(window).scrollTop() > 0 )
	{ if (!is_scroll_button_visible) { is_scroll_button_visible = true; $('#scroll_button').fadeIn(400); } }
	else
	{ if (is_scroll_button_visible) { is_scroll_button_visible = false; $('#scroll_button').fadeOut(400); } }
});

// scrolling page to top
function scroll_to_top() { $('html, body').animate({ scrollTop: 0 }, 400); }
$(window).ready( function() { $('#scroll_button img').bind('click', function() { $('html, body').animate({ scrollTop: 0 }, 400); } ); } );


var default_name = "Your name";
var default_email = "Your email";
var default_message = "Your message";
var form_visible = false;

// email validation
function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
}

function init_form()
{
	$('#input_name').addClass('input_default').val( default_name );
	$('#input_email').addClass('input_default').val( default_email );
	$('#input_message').addClass('input_default').val( default_message );

	if ( $('#input_name_alert').hasClass('input_alert') ) { $('#input_name_alert').removeClass('input_alert'); }	
	if ( $('#input_email_alert').hasClass('input_alert') ) { $('#input_email_alert').removeClass('input_alert'); }
	if ( $('#input_message_alert').hasClass('input_alert') ) { $('#input_message_alert').removeClass('input_alert'); }	
}

function check_and_submit_form()
{
	errors = false;
	
	// check name
	if (( !$.trim($('#input_name').val()).length ) || ( $('#input_name').val() == default_name ))
	{
		$('#input_name_alert').addClass('input_alert');	
		errors = true;
	}
	
	// check email
	if ( ( !$.trim($('#input_email').val()).length ) || ( $('#input_email').val() == default_email ) || ( !validateEmail($('#input_email').val()) ) )
	{
		$('#input_email_alert').addClass('input_alert');	
		errors = true;		
	}
	
	// check message
	if (( !$.trim($('#input_message').val()).length ) || ( $('#input_message').val() == default_message ))
	{
		$('#input_message_alert').addClass('input_alert');	
		errors = true;
	}
	
	if ( !errors ) { $('#contact_form').submit(); }
}


$(window).ready( function() {

	init_form();
	
	$('#input_name').focus( function() { if ( $('#input_name_alert').hasClass('input_alert') ) { $('#input_name_alert').removeClass('input_alert'); } if ( $(this).val() == default_name ) $(this).removeClass('input_default').val('');  } );
	$('#input_name').blur( function() { if (!$.trim(this.value).length) $(this).addClass('input_default').val( default_name ); } );

	$('#input_email').focus( function() { if ( $('#input_email_alert').hasClass('input_alert') ) { $('#input_email_alert').removeClass('input_alert'); } if ( $(this).val() == default_email ) $(this).removeClass('input_default').val(''); } );
	$('#input_email').blur( function() { if (!$.trim(this.value).length) $(this).addClass('input_default').val( default_email ); } );
	
	$('#input_message').focus( function() { if ( $('#input_message_alert').hasClass('input_alert') ) { $('#input_message_alert').removeClass('input_alert'); } if ( $(this).val() == default_message ) $(this).removeClass('input_default').val(''); } );
	$('#input_message').blur( function() { if (!$.trim(this.value).length) $(this).addClass('input_default').val( default_message ); } );	

	$('#input_clear').click( function() { init_form(); } );
	$('#input_send').click( function() { check_and_submit_form(); } );

	$('.contact_switcher').click(
	function () {
	
	if ( !form_visible )
	{
		$('#form_container').slideDown("slow");
		$('html, body').animate({ scrollTop: $(window).height() }, 400, function() { $('#contact_form').slideDown("slow"); } );
		form_visible = true;
	}
	else
	{
		$('#contact_form').slideUp("slow", function() {	$('#form_container').slideUp("slow"); } );
		form_visible = false;
	}
				} );
				
				
	google_analytics();
	
} );


// --------------------------------- Google Analytics -----------------------------------------
function gaSSDSLoad (acct) {
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."), pageTracker, s;
  s = document.createElement('script');
  s.src = gaJsHost + 'google-analytics.com/ga.js';
  s.type = 'text/javascript';
  s.onloadDone = false;
  function init () {
    pageTracker = _gat._getTracker(acct);
    pageTracker._trackPageview();
  }
  s.onload = function () {
    s.onloadDone = true;
    init();
  };
  s.onreadystatechange = function() {
    if (('loaded' === s.readyState || 'complete' === s.readyState) && !s.onloadDone) {
      s.onloadDone = true;
      init();
    }
  };
  document.getElementsByTagName('head')[0].appendChild(s);
}

window.onload = function () { gaSSDSLoad("UA-28646702-1"); };
