$(document).ready(function(){

	// Inputs with place holders
	$('.placeholder').each(function(index, el){

		var el = $(el);
		el.blur(placeholderInputBlur);
		el.focus(placeholderInputFocus);        
		el.blur();

		el.parents('form:not(.submit-keep-placeholders)').bind('submit', placeholderFormSubmit);
	});

	$('.toggle-porfolio-item').each(function(index, el){
		var el = $(el).toggle(portfolioItemExpand, portfolioItemCollapse);
		el.parent().attr('data-height', el.parent().height());
	});
	
	$('.place_button1').click
	( 
		function()
		{ 
			$(this).addClass('selected'); 
			$('.place_button2').removeClass('selected');
			$('#visual-contact-us').removeClass('dublin'); 
			$('#addr1').show();  
			$('#addr2').hide(); 
			$('#contacts_map_dublin').hide();
			$('#contacts_map_wat').show();
			$('#contacts_details_wat').show();
			$('#contacts_details_dublin').hide();
		} 
	);
    $('.place_button2').click
	( 
		function()
		{ 
			$(this).addClass('selected'); 
			$('.place_button1').removeClass('selected');
			$('#visual-contact-us').addClass('dublin'); 
			$('#addr2').show();  
			$('#addr1').hide();
			$('#contacts_map_dublin').show();
			$('#contacts_map_wat').hide();
			$('#contacts_details_wat').hide();
			$('#contacts_details_dublin').show();
		} 
	);
});

/* Placeholders */

function placeholderInputFocus(event){

	var el = $(event.target);

	if (el.hasClass('placeholder-active')){
		el.removeClass('placeholder-active');
		el.val('');
	}
}

function placeholderInputBlur(event){

	var el = $(event.target);

	if (!el.hasClass('placeholder-active') && el.val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == ''){
		el.addClass('placeholder-active');
		el.val(el.attr('placeholder'));
	}
}

function placeholderFormSubmit(event){

	var form = $(event.target);
    
	form.find('.placeholder').each(function(index, el){		
		placeholderInputFocus({'target': el});
	});

	return true;
}

function placeholderFormSubmitCancel(form){

   var form = $(form);

   form.find('.placeholder').each(function(index, el){
      placeholderInputBlur({'target': $(el)});
   });

   return true;
}

/* Slide show */

var eSlideshow;
var eSlideshowContainer;
var eSlideshowImage;
var eSlideshowTitle;
var eSlideshowDescription;
var eSlideshowLink;
var eSlideshowPages;

function slideshowInit(i) {
	 	
	eSlideshow = $('#center-slideshow');
	
	if (!eSlideshow) {
		return false;
	}
	
	eSlideshow.find('.pages .prev').click(slideshowPrev);
	eSlideshow.find('.pages .next').click(slideshowNext);
	
	eSlideshowPages = eSlideshow.find('.pages .items span');	
	eSlideshowPages.each(function(index, elPage){
		$(elPage).click(function(){
			slideshowShow(index);
		})
	});
	
	eSlideshowContainer = eSlideshow.find('.container .border-top');
	eSlideshowImage = eSlideshowContainer.children('.image');	
	eSlideshowTitle = eSlideshowImage.children('.title');
	eSlideshowDescription = eSlideshowImage.children('p');
	eSlideshowLink = eSlideshowImage.children('a');
      	
	i = slideshowGetIndex(i);

	iSlideCurrent = i;
	
	for (var j = 0; j < aSlides.length; j++) {
		$.preloadImage(aSlides[j].image);
	}
}

function slideshowGetIndex(i) {
	
	return (i + aSlides.length) % aSlides.length;
}

function slideshowNext(bAuto) {
	slideshowShow(iSlideCurrent+1);
}

function slideshowPrev() {
	slideshowShow(iSlideCurrent-1);
}

function slideshowShow(i) {
	
	i = slideshowGetIndex(i);
	
	if (iSlideCurrent == i) {
		return;
	}
	                         
	eSlideshowContainer.stop().fadeTo(300, 0.01, function() {
		slideshowSetContent(i);
		eSlideshowContainer.fadeTo(400, 1);		
	});
	
	eSlideshowPages.eq(iSlideCurrent).removeClass('current');
	eSlideshowPages.eq(i).addClass('current');
	
   	iSlideCurrent = i;
}

function slideshowSetContent(i) {
	i = slideshowGetIndex(i);
	eSlideshowImage.css('backgroundImage', 'url(' + aSlides[i].image + ')');
	eSlideshowTitle.html(aSlides[i].title);
	eSlideshowDescription.html(aSlides[i].description);		
	eSlideshowLink.attr('href', aSlides[i].url);
}

/* Image preload */

jQuery.preloadImage = function(src) {
	jQuery("<img>").attr("src", src);
}

jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}

/* Portfolio item */

function portfolioItemExpand(e) {
	e.preventDefault();
	var el = $(this);
	el.addClass('expanded').html('Hide Project');
	el.parent().stop().animate({height: el.siblings('img').height()});
}

function portfolioItemCollapse(e) {
	e.preventDefault();
	var el = $(this);
	el.removeClass('expanded').html('View Project');
	el.parent().stop().animate({height: el.parent().attr('data-height')});
}

/* Ask Us form */

function ajaxSubmitForm(elForm){
	
	elForm = $(elForm);
	elContainer = elForm.parent();
	
	placeholderFormSubmit(elForm);
	elContainer.addClass('loading');	
	
	$.post(elForm.attr('action') + '?ajax', 
		elForm.serialize(),
		function(data){
			
			elContainer.removeClass('loading');
			
			if (data.result) {
				elForm.replaceWith('<p class="message">' + data.message + '</p>');
			}
			else {
				placeholderFormSubmitCancel(elForm);
				
				elForm.find('input, textarea').each(function(i, elField){
					
					elField = $(elField);
					var elFieldParent = elField.parent();
					
					if (elField.attr('name') in data.errors) {
						if (elFieldParent.hasClass('required')) {
							ajaxSubmitFormBlinkField(elFieldParent);
						}
						else {
							elFieldParent.addClass('required');
						}
					}
					else {
						elFieldParent.removeClass('required');
					}
				});
			}
		}, 
		"json"
	);
   
	return false;
}

function ajaxSubmitFormBlinkField(elField, iCount) {
	
	if (iCount === undefined) {
		iCount = 4;
	}
	
	if (iCount > 0) {
		
		$(elField).toggleClass('required');
		
		setTimeout(function(){
				ajaxSubmitFormBlinkField(elField, iCount-1);
			},
			150
		);
	}
}
