$(document).ready(function(){
	$("#sw-spec-projects").click(function(){
		$("div.menu-sections").animate({
			opacity: 0
		}, 500).hide();
		$("div.menu-specprojects").show().animate({
			opacity: 1
		}, 500);
		return false;
	});
	$("#sw-sect-list").click(function(){
		$("div.menu-specprojects").animate({
			opacity: 0
		}, 500).hide();
		$("div.menu-sections").show().animate({
			opacity: 1
		}, 500);
		return false;
	});
	
	// Hints in inputs
	$('.hint-label').hints();
	
	// Tabs
	$('.tabs').tabs({
		linkSelector: '.tabs-controls .link',
		tabSelector: '.tab',
		saveState: true
	});
});


(function($){
	$(function(){
		var e = $(".scrollTopButton");
		var	speed = 500;

		e.click(function(){
			$("html:not(:animated)" +( !$.browser.opera ? ",body:not(:animated)" : "")).animate({ scrollTop: 0}, 500 );
			return false;
		});
		//появление
		function show_scrollTop(){
			( $(window).scrollTop()>300 ) ? e.fadeIn(600) : e.hide();
		}
		$(window).scroll( function(){show_scrollTop()} ); show_scrollTop();
	});

})(jQuery);


/*
 * Simple tooltip function
 * Usage: simple_tooltip('SPAN.section-select-link');
 */

function simple_tooltip(target_items, fixed_pos){
	$(target_items).each(function(i){
		var my_tooltip = $('#'+$(this).attr('id')+'-tooltip');

		$(this).removeAttr("title").click(function(){
			my_tooltip.fadeIn(400);
			return false;
		});
		if (fixed_pos) {
			my_tooltip.css({left:$(this).offset().left, top:$(this).offset().top+$(this).height()});
		} else {
			$(this).mouseover(function(kmouse){
				my_tooltip.css({left:kmouse.pageX+5, top:kmouse.pageY+5});
			//}).mousemove(function(kmouse){
			//	my_tooltip.css({left:kmouse.pageX+5, top:kmouse.pageY+5});
			});
			//my_tooltip.mouseleave(function(){
			//	$(this).fadeOut(400);
			//});
		}
		my_tooltip.find('.close-link').click(function(){
			my_tooltip.fadeOut(50);
			return false;
		});
	});
};


function isValidEmailAddress(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};


function subscribeBox(linkElementId) 
{
	simple_tooltip('#'+linkElementId,true);
	$('#'+linkElementId+'-tooltip input[type="submit"]').click(function(){
		var email = $('#'+linkElementId+'-tooltip input[name="email"]');
		if (email.val()=='' || !isValidEmailAddress(email.val())) {
			email.css('border','2px solid #ff0000');
			return false;
		}
		var sform = $('#'+linkElementId+'-tooltip form');
		sform.attr('action',sform.attr('action')+$('input[name="subscribe-list"]:checked').val());
		return true;
	});
};


String.prototype.supplant = function (o) {
	return this.replace(/{([^{}]*)}/g,
		function (a, b) {
			var r = o[b];
			return typeof r === 'string' || typeof r === 'number' ? r : a;
		}
	);
};


/*
 * Input hints
 */

(function($){
	$.fn.hints = function( userSettings ){
		var SETTINGS = {
			hintClass: 'hint-input'
		};
		
		return this.each(function(){
			if( userSettings ){
				$.extend( SETTINGS, userSettings );
			}
			
			var label = $(this),
				defaultValue = label.text(),
				input = $('input[id=' + label.attr('for') + ']');
			
			initLabels();
			assignEvents();
			
			function initLabels(){
				label.hide();
			}
			
			function assignEvents(){
				input.focus(function(){
					var input = $(this),
						value = input.val();
					
					if( value == defaultValue ){
						input.val('').removeClass(SETTINGS.hintClass);
					}
				});
				
				input.blur(function(){
					var input = $(this),
						value = input.val();
					
					if( $.trim(value) === '' || $.trim(value) == defaultValue ){
						input.val(defaultValue).addClass(SETTINGS.hintClass);
					}
				}).blur();
			}
		});
	};
})( jQuery );


/*
 * Tabs
 */

(function($){
	$.fn.tabs = function( userSettings ){
		var SETTINGS = {
			linkSelector: '',
			tabSelector: '',
			selectedClass: 'selected',
			saveState: false,
			onSwitchTab: null
		};
		
		return this.each(function(){
			if( userSettings ){
				$.extend( SETTINGS, userSettings );
			}
			
			var tabsContainer = $(this),
				links = $(SETTINGS.linkSelector, tabsContainer),
				tabs = $(SETTINGS.tabSelector, tabsContainer),
				cookieName = 'tab-' + $(this).attr('id'),
				cookieValue = '';
			
			initTabs();
			assignEvents();
			
			function initTabs(){
				if( SETTINGS.saveState ){
					cookieValue = $.cookie(cookieName);
					
					if(cookieValue){
						showTabFromCookie();
					}
					else{
						showFirstTab();
					}
				}
				else {
					showFirstTab();
				}
			}
			
			function showFirstTab(){
				tabs.filter(':not(:first)').hide();
				links.first().addClass(SETTINGS.selectedClass);
			}
			
			function showTabFromCookie(){
				tabs.filter(function(){
					return $(this).attr('id') != cookieValue;
				}).hide();
				
				links.filter(function(){
					return $(this).attr('href').substr(1) == cookieValue;
				}).addClass(SETTINGS.selectedClass);
			}
			
			function assignEvents(){
				links.click(
					clickHandler
				);
			}
			
			function clickHandler( event ){
				event.preventDefault();
				
				switchTab( $(this) );
				
				if( $.isFunction(SETTINGS.onSwitchTab) ){
					SETTINGS.onSwitchTab.call(this, event);
				}
			}
			
			function switchTab( link ) {
				var linkHrefTarget = link.attr('href').substr(1),
					targetTab = tabs.filter(function(){ return $(this).attr('id') == linkHrefTarget; });
				
				tabs.hide();
				targetTab.show();
				
				links.removeClass(SETTINGS.selectedClass);
				link.addClass(SETTINGS.selectedClass);
				
				if( SETTINGS.saveState ){
					$.cookie(cookieName, linkHrefTarget);
				}
			}
		});
	};
})( jQuery );


/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
//$.cookie('is_current')
//$.cookie('is_current', 'true', {path: '/', expires: 365});
//$.cookie('is_current', null, {path: '/'});

jQuery.cookie = function (key, value, options) {

 // key and at least value given, set cookie...
 if (arguments.length > 1 && String(value) !== "[object Object]") {
  options = jQuery.extend({}, options);
 
  if (value === null || value === undefined) {
   options.expires = -1;
  }
  
  if (typeof options.expires === 'number') {
   var days = options.expires, t = options.expires = new Date();
   t.setDate(t.getDate() + days);
  }
  
  value = String(value);
  
  return (document.cookie = [
   encodeURIComponent(key), '=',
   options.raw ? value : encodeURIComponent(value),
   options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
   options.path ? '; path=' + options.path : '',
   options.domain ? '; domain=' + options.domain : '',
   options.secure ? '; secure' : ''
  ].join(''));
 }
 
 // key and possibly options given, get cookie...
 options = value || {};
 var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
 return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
