var currentSection = "about-pane";
var tabTag = "-tab";
var paneTag = "-pane";
var nextImage = 1;
var totalImages = 5;
var page = '';
var lastSection = ''; 
var currentSection  = '';
var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
var moveanim = {time:0, beginX:0, changeX:0.0, beginY:0, changeY:0, duration:0.0, element:null, timer:null};

$().ready(function(){
	loadImages();
	$.historyInit(loadPage);
	$("a").click(function(){
		var hash = this.href;
		page = hash.replace(/^.*#/, '');
		$.historyLoad(page);
	});
	$('#bathroom_gallery a').lightBox();
	$('#beforeafter_gallery a').lightBox();
	$('#kitchen_gallery a').lightBox();
	$('#tile_gallery a').lightBox();
	$('#countertop_gallery a').lightBox();
	$('#more_gallery a').lightBox();
	if(window.attachEvent) window.attachEvent("onload", sFocus);
	if(document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule) document.styleSheets[0].addRule('*', 'behavior: url(js/iepngfix.htc)');
});

sFocus = function(){
	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function(){ this.className+=" sfocus"; }
		sfEls[i].onblur=function(){ this.className=this.className.replace(new RegExp(" sfocus\\b"), ""); }
	}
}

function decode(s){
	var c = '';
	for(i=0;i<s.length;i++){
		c += String.fromCharCode(s.charCodeAt(i)-1);
	}
	document.write(c);
}

function loadPage(){
	setTimeout("goToPage()", 100);
}
function goToPage(){
	page = (location.hash != '') ? location.hash.replace('#',''):'about';
	document.getElementById(page+'-tab').className = "active";
	ScrollSection(page+'-pane', 'scroller', 'about-pane');	
}

function randomImage(){
	$("#header").css("background-image","url(images/header" + nextImage  + ".jpg)");
	nextImage += 1;
	if(nextImage > totalImages) nextImage = 1;
}
function loadImages(){
	var preLoad = new Array();
	for (i = 0; i < totalImages; i++){
		preLoad[i] = new Image();
		preLoad[i].src = 'images/header'+(i+1)+'.jpg';
	}
}
function ScrollSection(link, scrollArea, offset){
	if (link == 'about-pane') randomImage();
	else if (link == 'bathroom-pane') $("#header").css("background-image","url(images/header3.jpg)");
	else if (link == 'kitchens-pane') $("#header").css("background-image","url(images/header2.jpg)");
	else if (link == 'tile-pane') $("#header").css("background-image","url(images/header5.jpg)");
	else if (link == 'countertops-pane') $("#header").css("background-image","url(images/header2.jpg)");
	else if (link == 'more-pane') $("#header").css("background-image","url(images/header4.jpg)");
	else if (link == 'contact-pane') randomImage();
	
	lastSection = currentSection;
	
	if(link != currentSection) {
		currentSection = link;
    	sectionTab = currentSection.split("-")[0] + tabTag;
    	document.getElementById(sectionTab).className = "active";
	}
	if(lastSection != currentSection) {
		if(page != 'about' && lastSection == '') lastSection = 'about-pane';
		if(lastSection.length > 0) {
			lastTab = lastSection.split("-")[0] + tabTag;
			document.getElementById(lastTab).className = "inactive";
		}
	}
    
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));

	if(offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
}
function scrollStart(elem, start, end, direction){

	if(scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz") scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	else scrollanim.timer = setInterval("scrollVertAnim();", 15);
}
function scrollVertAnim(){
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}
function scrollHorizAnim(){
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		if(currentSection == 'contact-pane') $('#name').focus();
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

function validate(){
	$('input').removeClass('inputerror');
	$('#sectionerror').hide();

	// CHECK FOR EMPTY VALUES
	if($('#name').val() == ''){
		$('#sectionerror').html('Please Enter your Full Name.');
		$('#sectionerror').show();
		$('#name').addClass('inputerror');
		$('#name').focus();
		return false;
	}
	else if($('#email').val() == '' && $('#phone').val() == ''){
		$('#sectionerror').html('Please Enter an Email Address or a Phone Number.');
		$('#sectionerror').show();
		$('#email').addClass('inputerror');
		$('#email').focus();
		return false;
	}
	else if($('#email').val() != '' && $('#email').val().search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
		$('#sectionerror').html('Please Enter a Valid Email Address.');
		$('#sectionerror').show();
		$('#email').addClass('inputerror');
		$('#email').focus();
		return false;
	}
	else if($('#project').val() == '' && $('#message').val() == ''){
		$('#sectionerror').html('Please Describe your Project or Enter a Message.');
		$('#sectionerror').show();
		$('#project').addClass('inputerror');
		$('#project').focus();
		return false;
	}
}

function NewWindow(mypage, myname, w, h, scroll) {
	winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar=no,status=no,resizable=no,menubar=no,location=no';
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}