$(document).ready(function(){

/* -- Styles for Internet Explorer 6 and 7 -- */

if ($.browser.msie && $.browser.version <= 6 ) {
 $('div#header').css('background-image', 'url(images/layout/backHeader.gif)');
 $('body.sectionAbout div#header').css('background-image', 'url(images/layout/backHeaderAbout.gif)');
}

/* -- Columns with the same height -- */

function columnHeight() {
	var windowHeight = $(window).height();
	var columnLeft = $('div#wrapper div#columnLeft');
	var columnLeftHeight = columnLeft.height();
	var columnRight = $('div#wrapper div#columnRight');
	var columnRightHeight = columnRight.height();
	var articleHeight = $('div#article').height();	
	if ((columnRightHeight + articleHeight) < windowHeight) {
	 columnRight.height(windowHeight);
		columnLeft.height(windowHeight);
	} else if (columnLeftHeight < (columnRightHeight + articleHeight)) {
		columnLeft.height(columnRightHeight + articleHeight);
	}
}

columnHeight()

$(window).resize(function() {
 columnHeight()
});

/* -- Boxs with the same height in the section about and section item -- */

var totalHeight = 0;
var boxItems = $('body.sectionAbout div.box, body.sectionItem div.box');
var boxItemHeight1 = $('body.sectionAbout div.box:eq(0), body.sectionItem div.box:eq(0)').height();
var boxItemHeight2 = $('body.sectionAbout div.box:eq(1), body.sectionItem div.box:eq(1)').height();
var boxItemHeight3 = $('body.sectionAbout div.box:eq(2), body.sectionItem div.box:eq(2)').height();
var boxItemHeight4 = $('body.sectionAbout div.box:eq(3), body.sectionItem div.box:eq(3)').height();
var boxAboutLink = $('a.boxLink');

if (boxItemHeight1 > totalHeight) { totalHeight = boxItemHeight1; }
if (boxItemHeight2 > totalHeight) { totalHeight = boxItemHeight2; }
if (boxItemHeight3 > totalHeight) { totalHeight = boxItemHeight3; }
if (boxItemHeight4 > totalHeight) { totalHeight = boxItemHeight4; }

boxItems.height(totalHeight);
boxAboutLink.height(totalHeight + 18);

/* -- Show or hide column right -- */

var wrapper = $('div#wrapper');
var columnRight = $('div#wrapper div#columnRight');
var bodyElement = $('body');
var linkAbout = $('div#nav a.sectionAbout, div#header h1 a');
var linkProjects = $('div#nav a.sectionProjects');
var linkContact = $('div#nav span.sectionContact');
var linkContactSpeed = 600;
var arrowContact = $('strong.sectionContact');

linkContact.toggle(
	function(event){
		if (bodyElement.hasClass('sectionAbout')) { linkAbout.css({'color': '#969696'}); }
		else if (bodyElement.hasClass('sectionProjects')) { linkProjects.css({'color': '#969696'}); }
		else if (bodyElement.hasClass('sectionItem')) { linkProjects.css({'color': '#969696'}); }
		bodyElement.addClass('sectionContact');
		arrowContact.fadeIn('normal');
		wrapper.animate({'margin-left': '0px'}, linkContactSpeed);
		columnRight.animate({'left': '284px'}, linkContactSpeed);
		event.preventDefault();
 },
	function(event){
		if (bodyElement.hasClass('sectionAbout')) { linkAbout.css({'color': '#333'}); }
		else if (bodyElement.hasClass('sectionProjects')) { linkProjects.css({'color': '#333'}); }
		else if (bodyElement.hasClass('sectionItem')) { linkProjects.css({'color': '#333'}); }
		bodyElement.removeClass('sectionContact');
		arrowContact.fadeOut('normal');
		wrapper.animate({'margin-left': '-300px'}, linkContactSpeed);
		columnRight.animate({'left': '300px'}, linkContactSpeed);
		event.preventDefault();
	}
);

/* - Auto hide column right, redirect and prevent default - */

// Variables.
var linkArray = $('a.readMore, body.sectionItem a#selectProjectsForestry, body.sectionItem a#selectProjectsRealEstate, body.sectionItem a#selectProjectsConstruction, body.sectionItem a#selectProjectsVineyards, body.sectionItem a#selectProjectsAll');

// Function.
function autoHideContact(){
	bodyElement.removeClass('sectionContact');
	arrowContact.fadeOut('normal');
	wrapper.animate({'margin-left': '-300px'}, linkContactSpeed);
}

// About link.
linkAbout.click(function(event){
	var pageValue = $(this).attr('href');
 if (bodyElement.hasClass('sectionContact') && bodyElement.hasClass('sectionProjects') || bodyElement.hasClass('sectionContact') && bodyElement.hasClass('sectionItem')) {
	 autoHideContact()		
  columnRight.animate({'left': '300px'}, linkContactSpeed, function() {	location.href = pageValue;	});
	} else if (bodyElement.hasClass('sectionContact') && bodyElement.hasClass('sectionAbout')) {
	 linkContact.trigger('click');
	}	else if (bodyElement.hasClass('sectionAbout')) {
	}	else {
		location.href = pageValue;
	}
 event.preventDefault();
});

// Projects link.
linkProjects.click(function(event){
 var pageValue = $(this).attr('href');
	if (bodyElement.hasClass('sectionContact') && bodyElement.hasClass('sectionAbout') || bodyElement.hasClass('sectionContact') && bodyElement.hasClass('sectionItem')) {
	 autoHideContact()
		columnRight.animate({'left': '300px'}, linkContactSpeed, function() {	location.href = pageValue;	});
	} else if (bodyElement.hasClass('sectionContact') && bodyElement.hasClass('sectionProjects')) {
	 linkContact.trigger('click');
	}	else if (bodyElement.hasClass('sectionProjects')) {
	}	else {
		location.href = pageValue;
	}
 event.preventDefault();
});

// Other links.
linkArray.click(function(event){
 var pageValue = $(this).attr('href');
	autoHideContact()
	columnRight.animate({'left': '300px'}, linkContactSpeed, function() {	location.href = pageValue;	});
 event.preventDefault();
});

/* -- Hide email -- */

$('div#sideBar a').each(function(){
	$('div#sideBar a').attr('href', function(index, value){ return (value).replace('(at)','@').replace('(dot)','.') });
	$('div#sideBar a').text(function(index, text) { return (text).replace('(at)','@').replace('(dot)','.') });
});

/* -- Carousel -- */

// Variables.
var carouselItem = $('div.carouselItem');
var carouselNext = $('span#carouselNext');
var carouselPrev = $('span#carouselPrev');
var carouselSize = carouselItem.length - 1;
var carouselSelected = 0; 
var carouselItemsWrapper = $('div#carouselItemsWrapper');
var carouselItemsWrapperWidth = (carouselSize + 1) * 362;

// Action.
carouselItemsWrapper.width(carouselItemsWrapperWidth);
carouselPrev.css({'cursor': 'default'});

// Next button.
carouselNext.click(function(event){
	if(carouselSelected < carouselSize){
	 carouselItem.eq(carouselSelected).animate({'margin-left': '-362px'}, 'slow');
  carouselSelected++;
	}
	if(carouselSelected == carouselSize){
		$(this).css({'cursor': 'default'});
	}
	carouselPrev.css({'cursor': 'pointer'});
	event.preventDefault();
});

// Prev button.
carouselPrev.click(function(event){
	if(carouselSelected != 0){
	 carouselSelected--;
		carouselItem.eq(carouselSelected).animate({'margin-left': '0px'}, 'slow');
	}
	if(carouselSelected == 0){
		$(this).css({'cursor': 'default'});
	}
	carouselNext.css({'cursor': 'pointer'});
	event.preventDefault();
});

/* -- Categories, in the section projects -- */

// Variables.
var projectsAll = $('body.sectionProjects a#selectProjectsAll');
var projectsVineyards = $('body.sectionProjects a#selectProjectsVineyards');
var projectsConstruction = $('body.sectionProjects a#selectProjectsConstruction');
var projectsRealEstate = $('body.sectionProjects a#selectProjectsRealEstate');
var projectsForestry = $('body.sectionProjects a#selectProjectsForestry');
var projectsItemsAll = $('body.sectionProjects div.itemVineyards, body.sectionProjects div.itemConstruction, body.sectionProjects div.itemRealEstate, body.sectionProjects div.itemForestry');

// Function.
function columnsAlpha() {
	if (bodyElement.hasClass('sectionProjects')) {
		projectsItemsAll.fadeTo(0, 1);
	} 
	if (bodyElement.hasClass('sectionVineyards')){
		projectsItemsAll.fadeTo(0, 0.5);
		$('div.itemVineyards').fadeTo(0, 1);
	} else if (bodyElement.hasClass('sectionConstruction')){
		projectsItemsAll.fadeTo(0, 0.5);
		$('div.itemConstruction').fadeTo(0, 1);
	} else if (bodyElement.hasClass('sectionRealEstate')){
		projectsItemsAll.fadeTo(0, 0.5);
		$('div.itemRealEstate').fadeTo(0, 1);
	} else	if (bodyElement.hasClass('sectionForestry')){
		projectsItemsAll.fadeTo(0, 0.5);
		$('div.itemForestry').fadeTo(0, 1);
	}
}

columnsAlpha();

// Categories.
projectsAll.click(function(event){ if(bodyElement.hasClass('sectionContact')){ bodyElement.removeClass().addClass('sectionProjects').addClass('sectionContact'); } else { bodyElement.removeClass().addClass('sectionProjects'); } event.preventDefault(); columnsAlpha(); });
projectsVineyards.click(function(event){ if(bodyElement.hasClass('sectionContact')){ bodyElement.removeClass().addClass('sectionProjects').addClass('sectionContact').addClass('sectionVineyards'); } else { bodyElement.removeClass().addClass('sectionProjects').addClass('sectionVineyards'); } event.preventDefault(); columnsAlpha(); });
projectsConstruction.click(function(event){ if(bodyElement.hasClass('sectionContact')){ bodyElement.removeClass().addClass('sectionProjects').addClass('sectionContact').addClass('sectionConstruction'); } else { bodyElement.removeClass().addClass('sectionProjects').addClass('sectionConstruction'); } event.preventDefault(); columnsAlpha(); });
projectsRealEstate.click(function(event){ if(bodyElement.hasClass('sectionContact')){ bodyElement.removeClass().addClass('sectionProjects').addClass('sectionContact').addClass('sectionRealEstate'); } else { bodyElement.removeClass().addClass('sectionProjects').addClass('sectionRealEstate'); } event.preventDefault(); columnsAlpha(); });
projectsForestry.click(function(event){ if(bodyElement.hasClass('sectionContact')){ bodyElement.removeClass().addClass('sectionProjects').addClass('sectionContact').addClass('sectionForestry'); } else { bodyElement.removeClass().addClass('sectionProjects').addClass('sectionForestry'); } event.preventDefault(); columnsAlpha(); });

/* -- Back to top -- */

$('a.backTop').click(function (event) {
	if ($.browser.opera) {
  $('html').animate({ scrollTop: 0 }, 500);
 } else {
	 $('body, html').animate({ scrollTop: 0 }, 500);  
	}
 event.preventDefault();
});

/* -- Pictures gallery -- */

// Variables.
var galleryItem = $('div#boxPictures img');
var galleryNext = $('span#buttonNext');
var galleryPrev = $('span#buttonPrev');
var gallerySize = galleryItem.length - 1;
var gallerySelected = 0; 
var galleryItemsWrapper = $('div#picturesWrapper');
var galleryItemsWrapperWidth = (gallerySize + 1) * 595;

// Action.
galleryItemsWrapper.width(galleryItemsWrapperWidth);
galleryPrev.css({'cursor': 'default'});

// Next button.
galleryNext.click(function(event){
	if(gallerySelected < gallerySize){
	 galleryItem.eq(gallerySelected).animate({'margin-left': '-595px'}, 'slow');
  gallerySelected++;
	}
	if(gallerySelected == gallerySize){
		$(this).css({'cursor': 'default'});
	}
	galleryPrev.css({'cursor': 'pointer'});
	event.preventDefault();
});

// Prev button.
galleryPrev.click(function(event){
	if(gallerySelected != 0){
	 gallerySelected--;
		galleryItem.eq(gallerySelected).animate({'margin-left': '0px'}, 'slow');
	}
	if(gallerySelected == 0){
		$(this).css({'cursor': 'default'});
	}
	galleryNext.css({'cursor': 'pointer'});
	event.preventDefault();
});

/* -- Show or hide location -- */

// Variables.
var locationButtonShow = $('span#buttonLocation');
var locationButtonHide = $('span#buttonPictures');
var boxLocation = $('div#boxLocation');
var boxPictures = $('div#boxPictures');

// Show location.
locationButtonShow.click(function(event){
	boxPictures.animate({'top': '-372px'}, 'slow');
	boxLocation.animate({'top': '0px'}, 'slow');
	locationButtonShow.fadeOut('normal');
	locationButtonHide.fadeIn('normal').delay('500').css({'display': 'block'});
	galleryNext.fadeOut('normal');
	galleryPrev.fadeOut('normal');
 event.preventDefault();
});

// Hide location.
locationButtonHide.click(function(event){
	boxPictures.animate({'top': '0px'}, 'slow');
	boxLocation.animate({'top': '372px'}, 'slow');
	locationButtonShow.fadeIn('normal');
	locationButtonHide.fadeOut('normal');
	galleryNext.fadeIn('normal');
	galleryPrev.fadeIn('normal');
 event.preventDefault();
});

/* -- Share -- */

// Variables.
var buttonShare = $('span#buttonShare');
var boxShare = $('div#projectShare dd');
var boxShareContent = $('div#projectShare dd ul, div#projectShare dd strong');

// Action.
buttonShare.toggle(
	function(event){
	 boxShareContent.css({'visibility': 'hidden'});
		boxShare.slideDown('fast', function(){
		boxShareContent.hide();
		boxShareContent.css({'visibility': 'visible'});
		boxShareContent.fadeIn('fast');
		});
		event.preventDefault();
	},
	function(event){
	 boxShareContent.fadeOut('fast', function(){
		 boxShare.slideUp('normal', function(){
			boxShareContent.show();
			});
		});
		event.preventDefault();
	}
);

// Add target blank.
$('div#projectShare a').attr('target', '_blank');

/* -- Form -- */

/* - Styles - */

if ($.browser.msie && $.browser.version <= 6 ) {
	$('input[type="text"]').css({'width': '258px', 'margin': '0 0 5px 0', 'padding': '4px', 'display': 'block', 'border': 'none', 'background-color': '#fff', 'font-family': 'Arial, Helvetica, sans-serif', 'font-size': '12px', 'color': '#777'});
	$('textarea').css({'width': '258px', 'margin': '0 0 5px 0', 'padding': '4px', 'display': 'block', 'border': 'none', 'background-color': '#fff', 'font-family': 'Arial, Helvetica, sans-serif', 'font-size': '12px', 'color': '#777'});
	$('input[type="radio"]').css({'position': 'absolute', 'left': '-9999px'});
	$('input[type="checkbox"]').css({'position': 'absolute', 'left': '-9999px'});
}

if ($.browser.msie && $.browser.version <= 7 ) {
	$('label.contactYes, label.contactNo').css({'top': '63px'});
	$('label.contactVinyard').css({'top': '270px'});
	$('label.contactForestry').css({'top': '290px'});
	$('label.contactConstruction').css({'top': '310px'});
	$('label.contactReakEstate').css({'top': '330px'});
}

/* - Default value - */

(function(jQuery){
jQuery.fn.extend({
defaultValue: function(str, callback) {	
return this.each(function() {
var $input	=	$(this),
defaultValue	=	str || $input.attr('rel'),
callbackArguments =	{'input':$input};
if( $input.attr('type') == 'password' ) {
handlePasswordInput();
} else {
handleTextInputs();
}
function handlePasswordInput(){
var $clone = createClone();
callbackArguments.clone = $clone;
$clone.insertAfter($input);
$input.hide();
$input.blur(function(){
if( $input.val().length <= 0 ){
$clone.show();
$input.hide();
}
});
}
function handleTextInputs(){
setState();
$input.keypress( function () {
if( $input.val().length > 0 ) {
setState();
}
}).blur(setState).focus( function () {
$input.val() == defaultValue && $input.val('');
});
$input.closest("form").submit(function() {
$input.val() == defaultValue && $input.val('');
});
}
function setState(){
val = jQuery.trim($input.val());
if( val.length <= 0 || val == defaultValue ) {
$input.val(defaultValue);
$input.addClass('empty');
} else {
$input.removeClass('empty');
}
}
function createClone(){
var $el = jQuery("<input />").attr({
'type': 'text',
'value': defaultValue,
'class': $input.attr('class')+'empty',
'style': $input.attr('style'),
'tabindex' : $input.attr('tabindex')
});
$el.focus(function(){
$el.hide();
$input.show();
setTimeout(function () {
$input.focus();
}, 1);
});				
return $el;
}
if(callback){
callback(callbackArguments);
}	
});
}
});
})(jQuery);

$('input#contactName').defaultValue('Name');
$('input#contactEmail').defaultValue('Email');
$('input#contactPhone').defaultValue('Phone');
$('input#contactAdress').defaultValue('Adress');
$('input#contactCity').defaultValue('City / Town');
$('input#contactZip').defaultValue('ZIP');
$('input#contactCountry').defaultValue('Country');
$('input#contactWebsite').defaultValue('Website');
$('textarea#contactComment').defaultValue('Comment / Enquires');

/* - Elastic textarea - */

(function(jQuery){ 
jQuery.fn.extend({  
elastic: function() {
var mimics = [
'paddingTop',
'paddingRight',
'paddingBottom',
'paddingLeft',
'fontSize',
'lineHeight',
'fontFamily',
'width',
'fontWeight'];
return this.each( function() {
if ( this.type != 'textarea' ) {
return false;
}
var $textarea	=	jQuery(this),
$twin		=	jQuery('<div class="textarea"><div />').css({'position': 'absolute','display':'none','word-wrap':'break-word'}),
lineHeight	=	parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),'10'),
minheight	=	parseInt($textarea.css('height'),10) || lineHeight*3,
maxheight	=	parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
goalheight	=	0,
i 			=	0;
if (maxheight < 0) { maxheight = Number.MAX_VALUE; }
$twin.appendTo($textarea.parent());
var i = mimics.length;
while(i--){
$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));
}
function setHeightAndOverflow(height, overflow){
curratedHeight = Math.floor(parseInt(height,10));
if($textarea.height() != curratedHeight){
$textarea.css({'height': curratedHeight + 'px','overflow':overflow});
}}
function update() {
var textareaContent = $textarea.val().replace(/&/g,'&amp;').replace(/  /g, '&nbsp;').replace(/<|>/g, '&gt;').replace(/\n/g, '<br />');
var twinContent = $twin.html();
if(textareaContent+'&nbsp;' != twinContent){
$twin.html(textareaContent+'&nbsp;');
if(Math.abs($twin.height()+lineHeight - $textarea.height()) > 3){
var goalheight = $twin.height()+lineHeight;
if(goalheight >= maxheight) {
setHeightAndOverflow(maxheight,'auto');
} else if(goalheight <= minheight) {
setHeightAndOverflow(minheight,'hidden');
} else {
setHeightAndOverflow(goalheight,'hidden');
}}}}
$textarea.keyup(function(){ update(); });
$textarea.live('input paste',function(e){ setTimeout( update, 250); });				
update();
});
} 
}); 
})(jQuery);

$('textarea').elastic();

/* - Labels - */

$('label.contactYes, label.contactNo').click(function(){
	$('label.contactYes, label.contactNo').removeClass('radioOn');
	$(this).addClass('radioOn');
});

$('label.contactVinyard, label.contactForestry, label.contactConstruction, label.contactReakEstate').click(function(){
 $(this).toggleClass('checkOn');
});

/* - Ajax send - */

$('form#contactForm').submit(function() {
	$.ajax({
		type: 'POST',
		url: $(this).attr('action'),
		data: $(this).serialize(),
		success: function(data) {
			$('div#boxContact').html(data);
		}
	});
return false;
});

});
