function initPopups()
{
	$("ul.input-info input, ul.phone-number input, li.comment textarea, div.text input").each(function(i, el)
	{
		var holder = $(el).parent();
		$(el).focus(function()
		{
			$(holder).addClass("focus");
		});
		$(el).blur(function()
		{
			$(holder).removeClass("focus");
		});
	});
}

function initLightbox() {
	$('a.contact-popup').simpleLightbox({
		faderOpacity: 0.7,
		event:'click',
		positioning: false,
		closeOnFader: false,
		faderBackground: '#fff',
		closeLink:'.close'
	});
}

function initGallery() {
	var _gallery = $('.popup-gallery');
	var _openLinks = $('a.gallery-opener');
	_gallery.parent().show();
//	_gallery.galleryScroll({
//		holderList: 'div.slides-holder',
//		scrollElParent: '.gallery-slider',
//		scrollEl: '.slide',
//		disableClass: 'disable',
//		btPrev: 'a.btn-prev',
//		btNext: 'a.btn-next',
//		circleSlide: false,
//		duration : 800
//	});

	_openLinks.simpleLightbox({
		faderBackground: '#6b6b6b',
		addOverlayClass: true,
		faderOpacity: 0.7,
		closeLink:'.btn-close'
	});

	_openLinks.each(function(_ind){
		$(this).click(function(){
			_gallery.get(0).recalc = true;
			//_gallery.find('.gallery-slider').css({marginLeft: -parseInt(_gallery.find('.slides-holder').css('width'))*_ind });
			// Get the case study code to start the show with.
			var galleryParams = $(this).attr('rel');
			loadGalleryWithData(galleryParams); // Function in slide show control.
			return false;
		});
	});
	_gallery.parent().hide();
}

function initTrend() {
    var _trend = $('.popup-trend');
    var _openLinks = $('a.trend-opener');
    _trend.parent().show();

    _openLinks.simpleLightbox({
        faderBackground: '#6b6b6b',
        addOverlayClass: true,
        faderOpacity: 0.7,
        closeLink: '.btn-close'
    });

    _openLinks.each(function (_ind) {
        $(this).click(function () {
            _trend.get(0).recalc = true;
            var trendParams = $(this).attr('rel');
            loadTrendWithData(trendParams); // Function in slide show control.
            return false;
        });
    });
    _trend.parent().hide();
}





function initLinks()
{

	var _links = $('a');
	var _pressedClass = 'pressed';
	_links.mousedown(function(){
		$(this).addClass(_pressedClass);
	});
	_links.mouseup(function(){
		$(this).removeClass(_pressedClass);
	});
	_links.mouseout(function(){
		$(this).removeClass(_pressedClass);
	});
	
	// Unbind all behaviors from the logo link.
	$(".logoLink").unbind('mousedown');
	
	$(".nopress").unbind('mousedown');
	
	
	
	$('.greenHover').mousedown(function(){
		$('.greenTarget').removeClass('hoverframe');
		$(this).parent('div .frame').addClass('pressframe');
		$(this).parent('div .frameOff').addClass('pressframe');
	});
	$('.greenHover').mouseup(function(){
		$(this).parent('div .frame').removeClass('pressframe');
		$(this).parent('div .frameOff').removeClass('pressframe');
	});
	$('.greenHover').mouseout(function(){
		$(this).parent('div .frame').removeClass('pressframe');
		$(this).parent('div .frameOff').removeClass('pressframe');
	});
	
	$('.greenHover').mouseover(function(){
		//$('.greenTarget').addClass('hoverframe');
		$(this).parent('div .frame').addClass('hoverframe');
		$(this).parent('div .frameOff').addClass('hoverframe');
		
		//alert('enter');
	});

	$('.greenHover').mouseout(function(){
		$('.greenTarget').removeClass('hoverframe');
		$('.frameOff').removeClass('hoverframe');
		//alert('leave');
	});
	
	//alert('here3');
	
}

$(function () {
    initLinks();
    initPopups();
    initLightbox();
    initGallery();
    initTrend();
});

// lightbox plugin
jQuery.fn.simpleLightbox = function(_options){
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0.8,
		faderBackground: '#ffffff',
		closeLink:'a.close-btn, a.cancela',
		addOverlayClass:false,
		closeOnFader: true,
		event:false,
		positioning:true,
		href:true,
		onClick: null
	},_options);

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		if (!_options.href)
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		else _this.lightboxContentBlock = _this.attr('href');
		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = $(_this.lightboxContentBlock);
			if (!jQuery('div.lightbox-fader').length)
				_fader = $('body').append('<div class="lightbox-fader"></div>');

			_fader = jQuery('div.lightbox-fader');
			_lightbox.css({
				'zIndex':9999
			});
			_fader.css({
				opacity:_this.faderOpacity,
				backgroundColor:_this.faderBackground,
				display:'none',
				position:'absolute',
				top:0,
				left:0,
				zIndex:998,
				textIndent: -9999
			}).text('&nbsp;');
			_lightbox.shownFlag = false;
			_lightbox.positioning = _options.positioning;

			if(_options.event) {
				$(_this).bind(_options.event, function(){
					if(_fader.is(':visible')) return false;
					_lightbox.shownFlag = true;
					_lightbox.hide();
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					if(_options.addOverlayClass) $('body').addClass('overlay');
					_fader.css({backgroundColor:_this.faderBackground});

					_fader.fadeIn(300, function(){
						_lightbox.fadeIn(400);
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);replaceSelects();
					});
					
					return false;
				});
			} else {
				_this.click(function(){
					if (jQuery.isFunction(_options.onClick)) {
						_options.onClick.apply(_this);
					}
					_lightbox.shownFlag = true;
					_lightbox.hide();
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					if(_options.addOverlayClass) $('body').addClass('overlay');
					_fader.css({backgroundColor:_this.faderBackground});
					_fader.fadeIn(300, function(){
						_lightbox.fadeIn(400);
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					});
					return false;
				});
			}

			jQuery(_this.closeLink).click(function(){
				_lightbox.fadeOut(400, function(){
					$('body').removeClass('overlay');
					_fader.fadeOut(300);
					_scroll = false;
				});
				return false;
			});

			if(_options.closeOnFader) {
				_fader.click(function(){
					_lightbox.fadeOut(400, function(){
						$('body').removeClass('overlay');
						_fader.fadeOut(300);
					});
					return false;
				});
			}

			var _scroll = false;
			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if(!_lbox.shownFlag) return false;
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();
				var _page = $('body');
				if (_lbox.length) {
					if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
					if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());

					if(_lbox.positioning) {
						if (_height > _thisHeight) {
							if ($.browser.msie && $.browser.version < 7) {
								_lbox.css({
									position:'absolute',
									top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
								});
							} else {
								_lbox.css({
									position:'fixed',
									top: ((_height - _lbox.outerHeight()) / 2)+"px"
								});
							}
						}
						else {
							var _fh = parseInt(_fader.css('height'));
							if (!_scroll) {
							if (_fh - _thisHeight > parseInt($(document).scrollTop())) {
									_fh = parseInt($(document).scrollTop())
									_scroll = _fh;
								} else {
									_scroll = _fh - _thisHeight;
								}
							}
							_lbox.css({
								position:'absolute',
								top: _scroll
							});
						}
						if (_width > _lbox.outerWidth()) _lbox.css({left:((_width - _lbox.outerWidth()) / 2 + 10) + "px"});
						else _lbox.css({position:'absolute',left: 0});
					}
				}
			}

			jQuery(window).resize(function(){
				if (_lightbox.is(':visible'))
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			jQuery(window).scroll(function(){
				if (_lightbox.is(':visible'))
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});

			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				$(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.fadeOut(400, function(){
						$('body').removeClass('overlay');
						_fader.fadeOut(300);
					});
				}
			});
		}
	});
}

// scroll gallery plugin
jQuery.fn.galleryScroll = function(_options){
	var _options = jQuery.extend({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		slideNum: false,
		duration : 1000,
		step: false,
		circleSlide: true,
		disableClass: 'disable',
		funcOnclick: null,
		autoSlide:false,
		innerMargin:0,
		stepWidth:false
	},_options);

	return this.each(function(){
		var _this = jQuery(this);

		var _holderBlock = jQuery(_options.holderList,_this);
		var _gWidth = _holderBlock.width();
		var _animatedBlock = jQuery(_options.scrollElParent,_holderBlock);
		var _liWidth = jQuery(_options.scrollEl,_animatedBlock).outerWidth(true);
		var _liSum = jQuery(_options.scrollEl,_animatedBlock).length * _liWidth;
		var _margin = -_options.innerMargin;
		var f = 0;
		var _step = 0;
		var _autoSlide = _options.autoSlide;
		var _timerSlide = null;
		if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;
		if (_options.stepWidth) _step = _options.stepWidth;

		if (!_options.circleSlide) {
			if (_options.innerMargin == _margin)
				jQuery(_options.btPrev,_this).addClass('prev-'+_options.disableClass);
		}
		if (_options.slideNum && !_options.step) {
			var _lastSection = 0;
			var _sectionWidth = 0;
			while(_sectionWidth < _liSum)
			{
				_sectionWidth = _sectionWidth + _gWidth;
				if(_sectionWidth > _liSum) {
					_lastSection = _sectionWidth - _liSum;
				}
			}
		}
		if (_autoSlide) {
				_timerSlide = setTimeout(function(){
					autoSlide(_autoSlide);
				}, _autoSlide);
			_animatedBlock.hover(function(){
				clearTimeout(_timerSlide);
			}, function(){
				_timerSlide = setTimeout(function(){
					autoSlide(_autoSlide)
				}, _autoSlide);
			});
		}

		function reCalc() {
			if(_this.get(0).recalc) {
				_margin = Math.abs(parseInt(_animatedBlock.css('marginLeft')));
				_this.get(0).recalc = false;
			}
		}

		// click button 'Next'
		jQuery(_options.btNext,_this).bind('click',function(){
			jQuery(_options.btPrev,_this).removeClass('prev-'+_options.disableClass);
			reCalc();
			if (!_options.circleSlide) {
				if (_margin + _step  > _liSum - _gWidth - _options.innerMargin) {
					if (_margin != _liSum - _gWidth - _options.innerMargin) {
						_margin = _liSum - _gWidth  + _options.innerMargin;
						jQuery(_options.btNext,_this).addClass('next-'+_options.disableClass);
						_f2 = 0;
					}
				} else {
					_margin = _margin + _step;
					if (_margin == _liSum - _gWidth - _options.innerMargin) {
						jQuery(_options.btNext,_this).addClass('next-'+_options.disableClass);_f2 = 0;
					}
				}
			} else {
				if (_margin + _step  > _liSum - _gWidth + _options.innerMargin) {
					if (_margin != _liSum - _gWidth + _options.innerMargin) {
						_margin = _liSum - _gWidth  + _options.innerMargin;
					} else {
						_f2 = 1;
						_margin = -_options.innerMargin;
					}
				} else {
					_margin = _margin + _step;
					_f2 = 0;
				}
			}

			_animatedBlock.animate({marginLeft: -_margin+"px"}, {queue:false,duration: _options.duration });

			if (_timerSlide) {
				clearTimeout(_timerSlide);
				_timerSlide = setTimeout(function(){
					autoSlide(_options.autoSlide)
				}, _options.autoSlide);
			}

			if (_options.slideNum && !_options.step) jQuery.fn.galleryScroll.numListActive(_margin,jQuery(_options.slideNum, _this),_gWidth,_lastSection);
			if (jQuery.isFunction(_options.funcOnclick)) {
				_options.funcOnclick.apply(_this);
			}
			return false;
		});
		// click button 'Prev'
		var _f2 = 1;
		jQuery(_options.btPrev, _this).bind('click',function(){
			jQuery(_options.btNext,_this).removeClass('next-'+_options.disableClass);
			reCalc();
			if (_margin - _step >= -_step - _options.innerMargin && _margin - _step <= -_options.innerMargin) {
				if (_f2 != 1) {
					_margin = -_options.innerMargin;
					_f2 = 1;
				} else {
					if (_options.circleSlide) {
						_margin = _liSum - _gWidth  + _options.innerMargin;
						f=1;_f2=0;
					} else {
						_margin = -_options.innerMargin
					}
				}
			} else if (_margin - _step < -_step + _options.innerMargin) {
				_margin = _margin - _step;
				f=0;
			}
			else {_margin = _margin - _step;f=0;};

			if (!_options.circleSlide && _margin == _options.innerMargin) {
				jQuery(this).addClass('prev-'+_options.disableClass);
				_f2=0;
			}

			if (!_options.circleSlide && _margin == -_options.innerMargin) jQuery(this).addClass('prev-'+_options.disableClass);
			_animatedBlock.animate({marginLeft: -_margin + "px"}, {queue:false, duration: _options.duration});

			if (_options.slideNum && !_options.step) jQuery.fn.galleryScroll.numListActive(_margin,jQuery(_options.slideNum, _this),_gWidth,_lastSection);

			if (_timerSlide) {
				clearTimeout(_timerSlide);
				_timerSlide = setTimeout(function(){
					autoSlide(_options.autoSlide)
				}, _options.autoSlide);
			}

			if (jQuery.isFunction(_options.funcOnclick)) {
				_options.funcOnclick.apply(_this);
			}
			return false;
		});

		if (_liSum <= _gWidth) {
			jQuery(_options.btPrev,_this).addClass('prev-'+_options.disableClass).unbind('click');
			jQuery(_options.btNext,_this).addClass('next-'+_options.disableClass).unbind('click');
		}
		// auto slide
		function autoSlide(autoSlideDuration){
			//if (_options.circleSlide) {
				jQuery(_options.btNext,_this).trigger('click');
			//}
		};
		// Number list
		jQuery.fn.galleryScroll.numListCreate = function(_elNumList, _liSumWidth, _width, _section){
			var _numListElC = '';
			var _num = 1;
			var _difference = _liSumWidth + _section;
			while(_difference > 0)
			{
				_numListElC += '<li><a href="">'+_num+'</a></li>';
				_num++;
				_difference = _difference - _width;
			}
			jQuery(_elNumList).html('<ul>'+_numListElC+'</ul>');
		};
		jQuery.fn.galleryScroll.numListActive = function(_marginEl, _slideNum, _width, _section){
			if (_slideNum) {
				jQuery('a',_slideNum).removeClass('active');
				var _activeRange = _width - _section-1;
				var _n = 0;
				if (_marginEl != 0) {
					while (_marginEl > _activeRange) {
						_activeRange = (_n * _width) -_section-1 + _options.innerMargin;
						_n++;
					}
				}
				var _a  = (_activeRange+_section+1 + _options.innerMargin)/_width - 1;
				jQuery('a',_slideNum).eq(_a).addClass('active');
			}
		};
		if (_options.slideNum && !_options.step) {
			jQuery.fn.galleryScroll.numListCreate(jQuery(_options.slideNum, _this), _liSum, _gWidth,_lastSection);
			jQuery.fn.galleryScroll.numListActive(_margin, jQuery(_options.slideNum, _this),_gWidth,_lastSection);
			numClick();
		};
		function numClick() {
			jQuery(_options.slideNum, _this).find('a').click(function(){
				jQuery(_options.btPrev,_this).removeClass('prev-'+_options.disableClass);
				jQuery(_options.btNext,_this).removeClass('next-'+_options.disableClass);

				var _indexNum = jQuery(_options.slideNum, _this).find('a').index(jQuery(this));
				_margin = (_step*_indexNum) - _options.innerMargin;
				f=0; _f2=0;
				if (_indexNum == 0) _f2=1;
				if (_margin + _step > _liSum) {
					_margin = _margin - (_margin - _liSum) - _step + _options.innerMargin;
					if (!_options.circleSlide) jQuery(_options.btNext, _this).addClass('next-'+_options.disableClass);
				}
				_animatedBlock.animate({marginLeft: -_margin + "px"}, {queue:false, duration: _options.duration});

				if (!_options.circleSlide && _margin==0) jQuery(_options.btPrev,_this).addClass('prev-'+_options.disableClass);
				jQuery.fn.galleryScroll.numListActive(_margin, jQuery(_options.slideNum, _this),_gWidth,_lastSection);

				if (_timerSlide) {
					clearTimeout(_timerSlide);
					_timerSlide = setTimeout(function(){
						autoSlide(_options.autoSlide)
					}, _options.autoSlide);
				}
				return false;
			});
		};
		jQuery(window).resize(function(){
			_gWidth = _holderBlock.width();
			_liWidth = jQuery(_options.scrollEl,_animatedBlock).outerWidth(true);
			_liSum = jQuery(_options.scrollEl,_animatedBlock).length * _liWidth;
			if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;
			if (_options.slideNum && !_options.step) {
				var _lastSection = 0;
				var _sectionWidth = 0;
				while(_sectionWidth < _liSum)
				{
					_sectionWidth = _sectionWidth + _gWidth;
					if(_sectionWidth > _liSum) {
						_lastSection = _sectionWidth - _liSum;
					}
				};
				jQuery.fn.galleryScroll.numListCreate(jQuery(_options.slideNum, _this), _liSum, _gWidth,_lastSection);
				jQuery.fn.galleryScroll.numListActive(_margin, jQuery(_options.slideNum, _this),_gWidth,_lastSection);
				numClick();
			};
			//if (_margin == _options.innerMargin) jQuery(this).addClass(_options.disableClass);
			if (_liSum - _gWidth  < _margin - _options.innerMargin) {
				if (!_options.circleSlide) jQuery(_options.btNext, _this).addClass('next-'+_options.disableClass);
				_animatedBlock.animate({marginLeft: -(_liSum - _gWidth + _options.innerMargin)}, {queue:false, duration: _options.duration});
			};
		});
	});
}
