function log(e) {
	//return false;
	if(typeof(console)=='object') console.log(e);
	else alert(e);
};

var advClasses = {
	init: function() {
		advClasses.html();
		advClasses.tables();
		advClasses.external();
	},
	tables: function() {
		$('tr:odd').addClass('odd');
		$('tr:even').addClass('even');
	},
	html: function() {
		for (var bname in $.browser) {
			if (bname == 'version') {
				var ver = parseInt($.browser[bname]);
			} else {
				var bodyclass = bname;
				var nomsie = '';
				if (bodyclass!='msie') {
					nomsie = 'nomsie';
				}
			}
		}
		$('html').addClass(bodyclass);
		$('html').addClass(bodyclass+'-'+ver);
		$('html').addClass(nomsie);
	},
	external: function() {
		$('#content a[href^="http"]:not([href*='+window.location.host+'])').filter(function(){ return !$(this).find('img').length }).addClass('external');
	}
};

function heightsEqualizer(elements, rwdl) {
	var self = this;
	self.elements = elements;
	self.rwdl = typeof rwdl != 'undefined' ? rwdl : false;

	self.equalize = function() {
		var max = 0;
		$(self.elements).each(function() {
			if ($(this).css('min-height', '0').height('auto').height() > max) {
				max = $(this).height();
			}
		});
		if ($.browser.msie && parseInt($.browser.version) < 7) {
			$(self.elements).height(max + 'px');
		} else {
			$(self.elements).css('min-height', max + 'px');
		}
	}

	self.equalize();

	if (self.rwdl) {
		self.timer = window.setInterval(function(){
			try {
				self.equalize
			}
			catch (e) {
			}
		}, 50);
		$(window).load(function(){
			window.clearInterval(self.timer);
		});
	}
};


/* Scrolling logo */
function LogoScroll() {
	var self = this;

	self.scrollEnable = false; // use animated scroll
	self.logo = $('#logo'); // static element
	self.wrapper = $('#logoFloat'); // floating element
	self.corner =	self.wrapper.find('.corner'); // corner element
	self.dzone = 18; // dead zone on top
	self.offset = -18; // top offset
	self.duration = 600; // duration of scroll effect
	self.wait = 200; // wait between scroll events (if animated scroll enabled)
	self.minwidth = 1044; // minimal width to scroll logo

	/* Makes logo scrollable */
	self.makeScrollable = function() {
		if(self.scrollEnable) {
			// Change classes
			self.wrapper.removeClass('fixed').addClass('scrollable').css('top', 0);
			self.logo.removeClass('fixed');
		} else {
			// Change classes
			self.wrapper.removeClass('fixed').css('top', '');
			self.logo.removeClass('fixed');
			// Get wrapper top offset
			self.wtop = parseInt(self.wrapper.css('top'));
		}
		// Create scroll function
		var scrollHandler = function() {
			// Clear timer
			window.clearTimeout(self.timer);
			// Set new timer
			self.timer = window.setTimeout(function(){
				// Get scroll top
				self.scroll = $(document).scrollTop();
				// Regular zone
				if(self.scroll > self.dzone) {
					if(self.scrollEnable) {
						self.wrapper.animate({top: self.scroll + self.offset}, self.duration);
					} else {
						self.wrapper.css('top', 0);
					}
					self.corner.hide();
				} else {
					// Dead zone
					if(self.scrollEnable) {
						self.wrapper.animate({top: 0}, self.duration).queue(function(){
							self.corner.fadeIn('fast');
							$(this).dequeue();
						});
					} else {
						self.corner.fadeIn('fast');
						self.wrapper.css('top', self.wtop - self.scroll);
					}
				}
				// Animate to top offset
			}, self.wait);
		}
		// Trigger once
		scrollHandler();
		// Bind onscroll event
		$(window).scroll(scrollHandler);
	};
	
	/* Makes logo fixed */
	self.makeFixed = function() {
		// Change classes
		self.wrapper.removeClass('scrollable').addClass('fixed').css('top', 0);
		self.logo.addClass('fixed');
		// Show corner
		self.corner.fadeIn('fast');
		// Unbind onscroll
		$(window).unbind('scroll');
	};
	
	/* Initialization */
	self.init = function() {
		// Wait only if scroll enabled
		self.wait = self.scrollEnable ? self.wait : 0;

		// window width is big enough
		if( $(window).width() > self.minwidth ) {
			self.makeScrollable();
		} else {
			// window width is too small
			self.makeFixed();
		}
	};
	
	// Init self
	self.init();
	// Bind reinit
	$(window).resize(self.init);
	
	return self;
}

/* Hiding labels in form */
function Labels() {
	var self = this;
	
	self.fadeDuration = 150; // Fade effect duration
	
	/* Find inputs */
	self.inputs = $('.labelin').find('input.text,textarea');

	/* Get label */
	self.getLabel = function(el) {
		return $(el).parents('.entries').find('label');
	};
	
	/* Is input filled in */
	self.isFilled = function(el) {
		return !!( $(el).attr('value') );
	};

	/* Onfocus */
	self.focus = function(el) {
		//$('.labelin').find('span.rounded').removeClass("error");
		el.parent().removeClass("error");
		$('#err1').hide();
		$('#err2').hide();
		
		if($.browser.msie) {
			self.getLabel(el).hide();
		} else {
			self.getLabel(el).fadeOut(self.fadeDuration);
		}
	};

	/* Onblur */
	self.blur = function(el) {
		if(!self.isFilled(el)) {
			if($.browser.msie) {
				self.getLabel(el).show();
			} else {
				self.getLabel(el).fadeIn(self.fadeDuration);
			}
		}
	};

	/* Bind events */
  	self.inputs.bind('focus click', function() {
		self.focus($(this));
	}).bind('blur', function() {
		self.blur($(this));
	});

	/* Init */
	self.inputs.each(function() {
		if(self.isFilled($(this))) {
			self.getLabel($(this)).hide();
		}
	});
	
	if ($("#clientLogin").length > 0) {
		$("#clientLogin form fieldset").append('<a href="#">&nbsp;</a>')
		$("#clientLogin form a").bind('click', function() {
			$(this).parent().children('p.entries').toggle(150);
			$(this).parent().children('div.submit').toggle(600);
			return false;
		});
	}
};


//--------------------------------------------------------------------------

$(document).ready(function() {     
	// Add advanced classes
	advClasses.init();
	
	// Make logo scrolling
	ls = new LogoScroll();

	/* CUFON */
	Cufon.replace('.cufon,.wsw h2', {
		hover: true,
		onAfterReplace: function() { // Works only with Cufon 1.10 engine
			// Curvy corners delayed init
			/** /
			if(typeof curvyCorners != 'undefined') {
				curvyCorners.init();
			}
			/**/
		}
	});

	/* CURVY CORNERS delayed init */
	if(typeof curvyCorners != 'undefined') {
		window.setTimeout(function(){
			$('html').addClass('cc-ready');
			curvyCorners.init();
		}, 100);
	}
	
	/* HOMEPAGE SLIDER */
	var nivoFadeDuration = 300;
	var nivoVarsApi = $('#slider .inner').nivoSlider({
		effect: "sliceUpDown",
		slices: 8,
		boxCols: 8,
        	boxRows: 4,
        	animSpeed: 1000,
		pauseTime: 7000,
		directionNav: false,
		captionOpacity: 1,
        	pauseOnHover: true, // Stop animation while hovering
		afterChange: function() {
			// get current image, find its caption and show
			$( nivoVarsApi.currentImage.parent().attr('rel') ).fadeIn(nivoFadeDuration);
		}, 
		afterLoad: function() {
			// toggle css visibility, but make hidden (cufon will sucessfully replace when visibility:hidden is set)
			$('#slider .caption').css('visibility', 'visible').hide();
			// show first caption
			$('#slider .caption:first').fadeIn(nivoFadeDuration);
		},
		beforeChange: function(a) {
			// hide all visible captions
			$('#slider .caption:visible').fadeOut(nivoFadeDuration);
		}
	}).data('nivo:vars'); // get nivo:vars object
	
	// Make labels in form hiding
	lb = new Labels();
	
	/* REFERENCES SLIDER */
	$('#refOverview .gallery .inner').nivoSlider({
		effect: 'fade',
		animSpeed: 500,
		pauseTime: 4000,
		directionNav: false,
		slices: 6
	});
});

