var maxClick;
var totalClick;
var allowLink = false;

var canClick = true;

var homeGallerySize;
var homeGalleryPointer = 0;

function initialiseGallery(){
	maxClick = $('.scrolling').children().length;
	totalClick = 0;
	$('.scrolling div.singleimage').eq(0).clone().appendTo('.scrolling');
	$('.nextImg').click(function(){
		if(totalClick == maxClick){
			shiftGallery(false);
		}
		if(canClick == true){
			canClick = false;
			totalClick++;
			$('.scrolling').animate({
				left: '-=1025'
			}, 800, checkGallery);
		} 
	});
	$('.prevImg').click(function(){
		if(totalClick == 0){
			shiftGallery(true);
		}
		if(canClick == true){
			canClick = false;
			totalClick--;
			$('.scrolling').animate({
				left: '+=1025'
			}, 800, checkGallery);
		} 
	});
	checkGallery();
}

function shiftGallery(goMinus){
	if(goMinus == true) {
		var totalLength = -1*(maxClick)*1025;
		$('.scrolling').css('left',totalLength);
		totalClick = maxClick;
	} else {
		$('.scrolling').css('left',0);
		totalClick = 0;
	}
}

function checkGallery(shiftFirstImage){
	if($('#thegallery div.scrolling img').length <= 1){
		$('.nextImg').fadeOut(150);
		$('.prevImg').fadeOut(150);
	} else {
		if(totalClick >= maxClick){
			totalClick = 0;
			$('.scrolling').css('left',0);
		}
		$('.nextImg').fadeIn(150);
		$('.prevImg').fadeIn(150);
		canClick = true;
	}
}

function homeGalleryTransition() {
	$('.infoCollection div').eq(homeGalleryPointer).fadeOut(200);
	$('.featuredproject div').eq(homeGalleryPointer).fadeOut(200);
	if(homeGalleryPointer < homeGallerySize-1) {
		homeGalleryPointer++;
	} else {
		homeGalleryPointer = 0;
	}
	$('.infoCollection div').eq(homeGalleryPointer).fadeIn(200);
	$('.featuredproject div').eq(homeGalleryPointer).fadeIn(200);
}

$(document).ready(
	function(){
		
		$('form.ajax-submit').submit(function(){			
			if($(this).hasClass('submitting')){
				return false;
			}
			
			var formId;
			var submitBtn;
			var SubmitTxt;
			
			$(this).addClass('submitting');
			
			formId = $(this).attr('id');
			url = $(this).attr('action');
		
			$('#'+formId+' input,'+' #'+formId+' textarea,'+' #'+formId+' select').attr('disabled','disabled');	
			
			submitBtn=$('#'+$(this).attr('id')+' input.Submit');	
			submitTxt=submitBtn.val();
			submitBtn.val('Saving form..');
	
			submitData=new Object();
			submitData['_SubmitTxt'] = submitTxt;
			submitData['_FormId'] = formId;
			submitData['_Ajax'] = true;
			
			$('#'+formId+' input,'+' #'+formId+' textarea,'+' #'+formId+' select').each(function(){
				type=$(this).attr('type');
				if(type=='radio' || type=='checkbox'){
					if($(this).is(":checked")){
						submitData[$(this).attr('name')]=$(this).val();
					}
				}
				else {
					submitData[$(this).attr('name')]=$(this).val();
				}
			});
	
			$.ajax({
				type: "POST",
				url: url,
				data:submitData,
				dataType:"json",
				error: function(){
					alert('An unexpected error occured. Please try again');
					$('form#'+formId).removeClass('submitting');
					submitBtn.val(submitTxt);
					$('#'+formId+' input,'+' #'+formId+' textarea,'+' #'+formId+' select').removeAttr('disabled');
				},
				success: function(response){
					errMsg='';
					for(i in response.errors){
						if(response.errors[i]){
							errMsg+=' - '+response.errors[i]+'\n';
						}
					}
					$('form#'+response.formId).removeClass('submitting');
					if(errMsg){
						$('form#'+response.formId+' input.Submit').val(response.SubmitTxt);
						$('#'+response.formId+' input,'+' #'+response.formId+' textarea,'+' #'+response.formId+' select').removeAttr('disabled');
						alert(errMsg);
					}
					else {
						eval(response.evaluate);
					}
				}
			});			
			return false;
		});
		
		//$('.dropdown a.on:last').parent('li').addClass('li-on');
		
		/*
		if($('.dropdown a.on').length > 0){
			$('.dropdown a.on').each(function(){
				$('.selected').html($(this).html());
			});
		}
		else {
			$('.selected').html($('.dropdown a:first').html());
		}
		*/
		
		$('#slideshow').cycle({ 
		  fx: 'custom', 
		  cssBefore: { left: 1025 }, 
		  animIn:  { left: 0 }, 
		  animOut: { left: -1025 },
		  timeout: 3000		  
		}); 
		$('.selected').click(function(e){
			  e.stopPropagation(); 
			$('.dropdown').slideDown('fast');
		});
		
		$('.dropdown li').hover(function(){
			$(this).children('ul.subnav').slideDown(100);
		},function(){
			$(this).children('ul.subnav').slideUp(100);
		});
		$('.dropdown a').click(function(){
			if($(this).hasClass('allow') && $(this).attr('href')){		
				allowLink = true;
				return true;
			}
			return false;
		});
		$('.cms-nav-13 li a').click(function(){
			$('.subsubnav').slideUp(100);
			$(this).siblings('.subsubnav').slideDown(100);
		});
		/*
		$('.dropdown a').click(function(){
			if($(this).siblings('ul').length > 0) {
				if($(this).siblings('ul').hasClass('subsubnav')){
					$('.subsubnav').slideUp('fast');
					if($(this).siblings('ul').is(":visible")){
						$(this).siblings('ul').slideUp('fast');	
					} else {
						$(this).siblings('ul').slideDown('fast');
					}
				} if($(this).siblings('ul').hasClass('subnav')){
					$('.subnav').slideUp('fast');
					if($(this).siblings('ul').is(":visible")){
						$(this).siblings('ul').slideUp('fast');	
					} else {
						$(this).siblings('ul').slideDown('fast');
					}
				}
			} else {
				$('.dropdown').slideUp('fast');
				$('.selected').html($(this).html());
			}
			
			if($(this).hasClass('allow') && $(this).attr('href')){		
				allowLink = true;
				return true;
			}
			return false;
		});
		*/
		$('#nav').click(function(){
			if(!allowLink){
				return false;
			}
		});
		/*
		$('body,html').click(function(){
			if($('.dropdown').is(":visible")){
				$('.dropdown').slideUp('fast',function(){
				   $('.subnav').hide();
				   $('.subsubnav').hide();
				});
			}
		});
		*/
		$('.loadAboutInfo').click(function(){
			$(this).hide();
			$('.aboutInfoLeft').animate({
				top: 0
			}, 400, function() {
				if($('#thegallery').find('div.scrolling img').length > 0){
					$('.closeAboutInfo').show();
				}
			});
			$('.aboutInfoRight').animate({
				bottom: 0
			}, 400);
		});
		$('.closeAboutInfo').click(function(){
			$(this).hide();
			$('.aboutInfoLeft').animate({
				top: -700
			}, 400, function() {
				$('.loadAboutInfo').show();
			});
			$('.aboutInfoRight').animate({
				bottom: -700
			}, 400);
		});
		if($('#thegallery').length > 0) {
			if($('#thegallery div.scrolling img').length > 0){
				initialiseGallery();
			}
			else {
				$('.loadAboutInfo').trigger('click');
			}
			$('.galleryBtn').hover(function(){
				$(this).children('img').fadeIn(100);
			}, function(){
				$(this).children('img').fadeOut(100);
			});
		}
		if($('.homegallery').length > 0) {
			homeGallerySize = $('.infoCollection div').length;
			$('.infoCollection div').eq(0).show();
			$('.featuredproject div').eq(0).show();
			window.setInterval('homeGalleryTransition()', 8000);
		}
	}
);
