var state = '';
var play = true;
var animating = false;
var isOver = false;
var timer = false;
var timer2 = false;
var preTimer = false;
var posting = false;

function scrollFolio() {
	if (isOver) {
		clearTimeout(timer);
		timer = setTimeout(function(){scrollFolio()},500);
		return 3;
	}
	if (animating) {
		clearTimeout(timer);
		timer = setTimeout(function(){scrollFolio()},500);
		return 4;
	}
	
	var $item = $('div .tile:first-child');
	itemID = $item.attr('id');
	
	if ($item.css('width') != '320px') {
		//$('#displayTxt').html($('#'+itemID+'Info').css('opacity'));
		/*
		$('#'+itemID+'Info').animate({'opacity':0},1000,function(){
			//$('#'+itemID+'Info').animate({'width':'0px'},500,function(){
				$('#'+itemID+'Info').css({'display':'none'});
				$item.animate({'width':'320px'},500,function(){
					$item.animate({'opacity':0},1000,function(){
						$item.animate({'width':'0px','margin':'0px'},1000,function(){
							//$item.remove();
							$item.appendTo('#folioHolder');
							$item.animate({'width':'320px','opacity':1,'margin':'10px'},1000,function(){
								animating = false;
								if (play) {
									scrollFolio();
								}
							});
						});
					});
				});
			//});
		});
		*/
		closeFolio($item);
		clearTimeout(timer);
		timer = setTimeout(function(){scrollFolio()},1000);
		return false;
	} else {
		animating = true;
		$item.animate({'opacity':0},1000,function(){
			$item.animate({'width':'0px','margin':'0px'},1000,function(){
				//$item.remove();
				$item.appendTo('#folioHolder');
				$item.animate({'width':'320px','opacity':1,'margin':'10px'},1000,function(){
					animating = false;
					if (play) {
						scrollFolio();
					}
				});
			});
		});
	}
	// just in case
	$('#'+itemID+'Info').css({'opacity':0,'display':'none'});
}

function folioPre() {
	if (play) {
		folioPlay();
	}
	var $item = $('div .tile:first-child');
	if (animating || $item.css('width') != '320px') {
		clearTimeout(preTimer);
		closeFolio($item);
		preTimer = setTimeout(function(){folioPre()},500);
		return false;
	}
	animating = true;
	var $item = $('div .tile:last-child');
	$item.css({'opacity':0,'width':'0px'});
	$('#folioHolder').prepend($item);
	$item.animate({'width':'320px'},1000,function(){
		$item.animate({'opacity':1},1000,function(){
			animating = false;
		});
	});
}

function folioPlay() {
	if (play) {
		play = false;
		$('#cntPlay').html('play');
	} else {
		play = true;
		$('#cntPlay').html('pause');
		scrollFolio();
	}	
}

function folioNext() {
	if (play) {
		folioPlay();
	}
	if (animating) {
		setTimeout('folioNext()',500);
		return false;
	}
	scrollFolio();
}

function folioOver(item) {
	var thisItem = item;
	
	if(thisItem.css('opacity') != 1) {
		return false;
	}
	isOver = true;
	if (thisItem.css('width') == '320px') {
		thisItem.animate({'backgroundColor':'#E05900'}, 500,function(){});
	}
}

function folioOut(item) {
	var thisItem = item;
	
	thisItem.animate({'backgroundColor':'#333'}, 500,function(){});
	if (thisItem.css('width') != '320px') {
		closeFolio(thisItem);	
	}
	isOver = false;
}

function expandFolio(item) {
	var thisItem = item;
	
	if(thisItem.css('opacity') != 1 || thisItem.css('width') != '320px') {
		return false;
	} else if (animating) {
		//clearTimeout(timer2);
		//timer2 = setTimeout(function(){expandFolio(item)},300);
		//return false;
	}
	animating = true;
	thisItem.animate({'backgroundColor':'#333','width':'550px'},500,function(){
		itemID = item.attr('id');
		$('#'+itemID+'Info').css({'opacity':0,'display':'block'});
		$('#'+itemID+'Info').animate({'opacity':1},500,function(){
			animating = false;
		});
	});
}

function closeFolio(item) {
	var thisItem = item;
	
	if (thisItem.css('width') != '550px' || animating) {
		return false;
	}
	animating = true;
	itemID = thisItem.attr('id');
	$('#'+itemID+'Info').animate({'opacity':0},500,function(){
		$('#'+itemID+'Info').css({'display':'none'});
		thisItem.animate({'width':'320px'},500,function(){
			animating = false;
		});
	});
}

function setNav(content) {
	if (state == content) {
		return false;
	}
	
	$('#btnportfolio').attr({'src':'buzz-lounge/portfolio.png'});
	$('#btncontact').attr({'src':'buzz-lounge/contact.png'});
	$('#btnpayment').attr({'src':'buzz-lounge/payment.png'});
	$('#btn'+content).attr({'src':'buzz-lounge/'+content+'_2.png'});
	
	if (state !== '') {
		$('#'+state+'Content').animate({'opacity':0},600,function(){
			$('#'+state+'Content').css({'display':'none'});
			setContent(content);
		});
	} else {
		setContent(content);
	}

	state = content;
}

function setContent(content) {
	switch (content) {
		case 'portfolio':
			$('#payment').animate({'left':40+'px'},600,function(){});	
			$('#portfolio').animate({'left':80+'px'},600,function(){});	
			break;
		case 'contact':
			$('#payment').animate({'left':($('#contentContainer').innerWidth()-80)+'px'},600,function(){});	
			$('#portfolio').animate({'left':($('#contentContainer').innerWidth()-40)+'px'},600,function(){});	
			break;
		case 'payment':
			$('#portfolio').animate({'left':($('#contentContainer').innerWidth()-40)+'px'},600,function(){});	
			$('#payment').animate({'left':40+'px'},600,function(){});	
			break;
	}
	
	$('#'+content+'Content').css({'display':'block','opacity':0});	
	$('#'+content+'Content').animate({'opacity':1},600,function(){});
}
		
function setContentWidth() {
	
	if (state === '') {
		return false;
	}
	
	switch (state) {
		case 'portfolio':
			$('#payment').css({'left':40+'px'},function(){});	
			$('#portfolio').css({'left':80+'px'},function(){});	
			break;
		case 'contact':
			$('#payment').css({'left':($('#contentContainer').innerWidth()-80)+'px'},function(){});	
			$('#portfolio').css({'left':($('#contentContainer').innerWidth()-40)+'px'},function(){});	
			break;
		case 'payment':
			$('#payment').css({'left':40+'px'},function(){});	
			$('#portfolio').css({'left':($('#contentContainer').innerWidth()-40)+'px'},function(){});	
			break;
	}
	$('#payment').css({'right':'0px'},function(){});
	$('#portfolio').css({'right':'0px'},function(){});
	$('#contact').css({'right':'0px'},function(){});
}

function btnover(btn) {
	if (state != btn) {
		$('#btn'+btn).attr({'src':'buzz-lounge/'+btn+'_2.png'});
	}
}

function btnout(btn) {
	if (state != btn) {
		$('#btn'+btn).attr({'src':'buzz-lounge/'+btn+'.png'});
	}
}

function showAlert(title, msg) {
	$(document.body).append('<div id="alert"></div>');
	$('#alert').css({'opacity':0,'background-color':'#000'});
	$('#alert').animate({'opacity':0.8},400,function(){
		$(document.body).append('<div id="alertBox"><div class="titleText">'+title+'</div><br />'+msg+'<div id="closeBtn"><a onclick="closeAlert()">[close]</a></div></div>');
		$('#alertBox').css({'opacity':0,'display':'inline'});
		$('#alertBox').animate({'opacity':1},400,function(){});
	});
	
}

function closeAlert() {
	$('#alertBox').animate({'opacity':0},400,function(){
		$('#alert').animate({'opacity':0},400,function(){
			$('#alertBox').remove();
			$('#alert').remove();
		});
	});
}

function sendMessage() {
	//alert(1);
	if (posting) {
		return false;
	}
	
	posting = true;
	
	$.post("processEmail.php",
		{	
			name: $('#name').val(),
			email: $('#email').val(),
			phone: $('#phone').val(),
			company: $('#company').val(),
			website: $('#website').val(),
			notes: $('#notes').val(),
			enquiry: $('#enquiry').val(), 
			ajax: 'ajax'
		},
		function(data){
			showAlert('Message Sent',data);
			$('#contactForm').clearForm();
			posting = false;
		}
	);
}

function doSendMessage(theForm) {
	if (validateForm(['email','name','notes'], theForm, false)) {
		sendMessage();
	}
	return false;
}


$.fn.clearForm = function() {
  // iterate each matching form
  return this.each(function() {
    // iterate the elements within the form
    $(':input', this).each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = 0;
    });
  });
};


function validateForm(arr, target, exec) {

	var fail = false;
		
	jQuery.each(arr, function() {
		if ($("#" + this).val() != "" && $("#" + this).val() != $("#" + this).attr("defaultText")) {
			$("#" + this).css({border:"1px solid #000", "background-color":"#FFF", "color": "black"});
		} else {
			$("#" + this).css({border:"1px solid #990000", "background-color":"#FFCCCC", "color": "black"});
			fail = true;
		}
	});
	
	if (fail) {
		showAlert("Validation Error","Please Complete all marked fields");
		return false;
	} else if(exec) {
		target.submit();	
		return true;
	}
	return true;
}
