$(document).ready(function() {
		
	window.setTimeout(function(){ $(document).delegate('body', 'click', clickOutsideEvent); }, 0); 
	
	jQuery.fn.exists = function () { return jQuery(this).length > 0; }
	
	if (window.location.hash == "#contact-us") {
		if ($(window).width() >= 659) {
			$('div.contact-us-modal-positioning').css('display', 'block');
			$('div.contact-us-modal-positioning').animate({bottom:300}, 500, 'swing',function() {
				$(this).removeClass('contact-us-closed');
				$(this).addClass('contact-us-open');
				$(this).css('z-index', '3000');
				$(this).animate({bottom:155}, 500, 'swing');
			});
		} else {
			window.location = "/contact";
		}
    	//var link = window.location.hash;
    	//if (link.length > 0 && link.hasClass('contact-us')) {
//        	link.click();
//        }
    }
	
	hideShowDescriptionList();
	commentInputs();
	contactUsShowHide();
	closeBtnContactUs();
	hideIE6Modal();
	getCurrentCols();
	updateClientList();
	
	//if you click outside of the filter dropdowns, the dropdown will close	
	var clickOutsideEvent = function() {
     	$('#filter_options ul').hide();
		$('#filter_options').removeClass('menu_open');
	};
	
	//$('.contact-us').colorbox({ inline:true, href:'.contact-us-modal', opacity:0.7});
	if ($.browser.msie && $.browser.version=="6.0" && getCookie('ie6ModalCookie') == null) {
		//$('.ie6-message').colorbox({ inline:true, href:'.ie6-modal', opacity:0.7, open:true});
		$('.ie6-modal-hide').show();
		setCookie('ie6ModalCookie', 'IE6 Modal', 1);
	}

	if ($(window).width() >= 1025) {
		setCookie('browserCookie', 'Desktop', 30);
	} else {
		setCookie('browserCookie', 'Mobile', 30);
	}

	$(window).scroll(function() {
	if ($(window).width() >= 1025) {
		if ($(window).scrollTop() >= (($(document).height() - $(window).height()) - 50)) {
			$('.footerOpenClose').animate({opacity:0}, 150, function() {
				$('.footerOpenClose').removeClass('visible');
				$('.footerOpenClose').addClass('not-visible');
				$('.footerOpenClose').removeAttr('style');
			});
			$('footer').addClass('bottom');
			$('footer').clearQueue();
			$('footer').animate({ bottom: '0px' }, 300, function() {
			$('footer').removeClass('closed');
			$('footer').addClass('opened');
			$('footer').removeAttr('style');
			$('.footerOpenClose').addClass('disabled');
			if ($('.footerOpenClose').hasClass('btnClose')) {
				$('.footerOpenClose').addClass('btnExpand');
				$('.footerOpenClose').removeClass('btnClose');
			}
		});
		}
		  
		if ($(window).scrollTop() < (($(document).height() - $(window).height()) - 50)) {
			if ($('footer').hasClass('bottom')) {
				$('.footerOpenClose').animate({opacity:1}, 150, function() {
					$('.footerOpenClose').removeClass('not-visible');
					$('.footerOpenClose').addClass('visible');
					$('.footerOpenClose').removeAttr('style');
				});
				$('footer').clearQueue();
				$('footer').animate({ bottom: '-182px' }, 300, function() {
					$('footer').removeClass('opened');
					$('footer').addClass('closed');
					$('footer').removeAttr('style');
					$('.footerOpenClose').removeClass('disabled');
					$('footer').removeClass('bottom');
				});
			} else {
				//do nothing
			}
		}
	} else {
		//do nothing
	}

	$(window).resize(function() {
		updateSliderSize();
		getCurrentCols();
		updateClientList();
		if ($(window).width() >= 1025) {
			setCookie('browserCookie', 'Desktop', 30);
		} else {
			setCookie('browserCookie', 'Mobile', 30);
		}
		if ($(window).width() <= 659) {
			if ($('div.contact-us-modal-positioning').hasClass('contact-us-open')) {
				$('div.contact-us-modal-positioning').removeClass('contact-us-open');
				$('div.contact-us-modal-positioning').addClass('contact-us-closed');
				$('div.contact-us-modal-positioning').animate({bottom:300}, 500, 'swing',function() {
					$(this).css('z-index', '1000');
					$('div.contact-us-modal-positioning').animate({bottom:-585}, 500, 'swing', function() {
						$(this).css('display', 'none');
					});
				});
			}
		}
	});
	
	});
	
	$('.footerOpenClose').click(function() {
		if ($(this).hasClass('disabled')) {
			//do nothing
		} else {
			if ($('footer').hasClass('closed')) {
				$('.footerOpenClose').removeClass('btnExpand');
				$('.footerOpenClose').addClass('btnClose');
				$('.footerOpenClose').css('display', 'block');
				$('footer').animate({ bottom: '0px' }, 300, function() {
					$('footer').removeClass('closed');
					$('footer').addClass('opened');
					$('footer').removeAttr('style');
					$('.footerOpenClose').removeAttr('style');
				});
				
			} else if ($('footer').hasClass('opened')) {
				$('.footerOpenClose').removeClass('btnClose');
				$('.footerOpenClose').addClass('btnExpand');
				$('footer').animate({ bottom: '-182px' }, 300, function() {
					$('footer').removeClass('opened');
					$('footer').addClass('closed');
					$('footer').removeAttr('style');
					$('.footerOpenClose').removeAttr('style');
				});
			}
		}
	});
	
	
	//filter dropdown menu animation
	$('#btn_home_filter').click(function() {
		if ($('#btn_home_filter').hasClass('open')) {
			if ($.browser.msie) {
				$('#filter_bar').animate({ marginBottom: '+=5', height: '-=90' }, 500, function() {
					$('#filter_bar').css('overflow', 'hidden');
					$('#btn_home_filter').removeClass('open');
				});
			} else {
				$('#filter_bar').animate({ height: '-=90' }, 500, function() {
					$('#filter_bar').css('overflow', 'hidden');
					$('#btn_home_filter').removeClass('open');
				});	
			}
		} else {
			$('#btn_home_filter').addClass('open');
			if ($.browser.msie) {
				$('#filter_bar').animate({ marginBottom: '+=5', height: '+=85' }, 500, function() {
					$('#filter_bar').css('overflow', 'visible');
				});
			} else {
				$('#filter_bar').animate({ height: '+=85' }, 500, function() {
					$('#filter_bar').css('overflow', 'visible');
				});	
			}
		}
	});
	
	$('#btn_home_filter').hover(function() {
		if ($("#btn_home_filter").hasClass('open')) {
			//do nothing
		} else {
			$('#filter_bar').clearQueue();
			$('#filter_bar').css('height', '40px');
			$('#filter_bar').animate({ height: '+=5' }, 200, function() {
				$('#filter_bar').css('overflow', 'hidden');	
			});
		}
	}, function() {
		if ($("#btn_home_filter").hasClass('open')) {
			//do nothing
		} else {
		$('#filter_bar').clearQueue();
		$('#filter_bar').css('height', '45px');
		$('#filter_bar').animate({ height: '-=5' }, 200, function() {
			$('#filter_bar').css('overflow', 'hidden');
		});
		}
	});
	
	$('.commentlist li:last').css("background-image", "none")
	
	//updates the filter with cufon'd text
	function update_filter() {
		var selectedfilter = $('#filter_options li.selected').text();
		$('#selected_filter').children().text(selectedfilter);
	}
	
	//click handler for showing/hiding filter dropdown
	$('#selected_filter').click(function(event) {
		event.stopPropagation();
		if ($("#filter_options ul").css("display") == "none") {
			$('#filter_options ul').show();
			$('#filter_options').addClass('menu_open');
		} else {
			$('#filter_options ul').hide();
			$('#filter_options').removeClass('menu_open');
		}
	});
	
	//click handler for filter selection
	$('#filter_options ul li').click(function() {
		$('#filter_options ul li').removeClass('selected');
		$(this).addClass('selected');
		update_filter();
		$('#filter_options ul').hide();
		$('#filter_options').removeClass('menu_open');
	});
	
	$('#filter_options ul li').each(function() {
		$(this).children('a').clearQueue();
		$(this).hover(function() {
			$(this).children('a').animate({marginLeft: '+=2'}, 100);
		}, function() {
			$(this).children('a').animate({marginLeft: '-=2'}, 100);
		});
	});
	
	update_filter();	
});

var listWidth = 0;
var listCellWidth = 0;
var currentCols = 0;
var lastCols = -1;

function contactUsShowHide() {
	$('a.contact-modal').click(function(e) {
		if ($(window).width() >= 659) {
			e.preventDefault();
			if ($('div.contact-us-modal-positioning').hasClass('contact-us-closed')) {
				$('div.contact-us-modal-positioning').css('display', 'block');
				$('div.contact-us-modal-positioning').animate({bottom:300}, 500, 'swing',function() {
					$(this).removeClass('contact-us-closed');
					$(this).addClass('contact-us-open');
					$(this).css('z-index', '3000');
					$(this).animate({bottom:100}, 500, 'swing', function() {
						window.location.hash = "#contact-us";
					});
				});
			} else if ($('div.contact-us-modal-positioning').hasClass('contact-us-open')) {
				$('div.contact-us-modal-positioning').removeClass('contact-us-open');
				$('div.contact-us-modal-positioning').addClass('contact-us-closed');
				$('div.contact-us-modal-positioning').animate({bottom:300}, 500, 'swing',function() {
					$(this).css('z-index', '1000');
					$(this).css('z-index', '1000');
					window.location.hash = "";
					$('div.contact-us-modal-positioning').animate({bottom:-585}, 500, 'swing', function() {
						$(this).css('display', 'none');
					});
				});
			}
		} else {
			//do nothing
		}
		
	});
}

function hideIE6Modal() {
	$('.ie6-modal a.btnCloseModal').click(function() {
		$('.ie6-modal-hide').hide();
	});
}

function closeBtnContactUs() {
	$('.contact-us-modal a.btnCloseModal').click(function() {
		$('div.contact-us-modal-positioning').removeClass('contact-us-open');
		$('div.contact-us-modal-positioning').addClass('contact-us-closed');
		$('div.contact-us-modal-positioning').animate({bottom:300}, 500, 'swing',function() {
			$(this).css('z-index', '1000');
			window.location.hash = "";
			$('div.contact-us-modal-positioning').animate({bottom:-585}, 500, 'swing', function() {
				$(this).css('display', 'none');
			});
		});
	})
}

//functions for storing/updating/deleting cookies
function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

function updateSliderSize() {
	var imageHeight = $('.sliderkit-panel-active img').height();
	$('.photosgallery-captions').height(imageHeight);
}

function adjust_slidernav_width() {
	var navWidth = $('.sliderkit-nav-clip').width();
	$('.sliderkit-nav-item').css('width', navWidth+56);
};

function hideShowDescriptionList() {
	$('.viewmore').each(function() {
		
		$(this).click(function() {
			if ($(this).hasClass('expanded')) {
				$(this).removeClass('expanded');
				$(this).siblings('.list_info').slideUp(600);
				$(this).siblings('h2').css('color', '#B68D2E');
			} else {
				$(this).addClass('expanded');
				$(this).siblings('.list_info').slideDown(600);
				$(this).siblings('h2').css('color', '#816649');
			}
		});
	});
};

function commentInputs() {
	if ($('#commentform').exists()) {
		$('#commentform input, #commentform textarea').each(function() {
			var originalValue = this.value;
			$(this).focus(function() {
				if (this.value == originalValue) {
					$(this).val('');
				} else {
					//do nothing
				}
			});
			$(this).blur(function() {
				if (this.value == "") {
					$(this).val(originalValue);
				} else {
					//do nothing
				}
			});
		});
	}
}

function getCurrentCols() {
	listWidth = $("#client-list").width();
	listCellWidth = $("span.cell").width();
	currentCols = Math.round(((listWidth-40) / listCellWidth));
}

function updateClientList() {
	if ($('#client-list').length != 0) {	
		if(lastCols != currentCols) {
			var numElements;
			
			if (currentCols <= 2) {
				numElements = $("#client-list span:not('.object')").length;
			} else {
				numElements = $("#client-list span").length;
			}	
			
			var numLastRowItems = numElements % currentCols;
			
			if (numLastRowItems == 0) {
				numLastRowItems = currentCols;
			}
			
			var lastCellSlice;
			
			if (currentCols <= 2) {
				lastCellSlice = $("#client-list span:not('.object')").slice(-numLastRowItems); // isolate bottom row items
			} else {
				lastCellSlice = $('#client-list span').slice(-numLastRowItems); // isolate bottom row items
			}
			
			$("#client-list span:nth-child(n)").removeClass("last").removeClass("bot"); // remove bot and last class from all items
			
			if (currentCols <= 2) {
				var nonObj = $("#client-list span:not('.object')");
				
				for (var i=(currentCols-1); i<numElements; i=(i+currentCols)) {
					$(nonObj).eq(i).addClass('last');
				}
			} else {
				$("#client-list span:nth-child(" + currentCols + "n)").addClass("last"); // add last class to last column items
			}
			
			lastCellSlice.addClass("bot"); // take bottom bar off of last row items
			
			lastCols = currentCols;
		}
	}
}


function setDeviceMode() {
		if ($(window).width() >= 1025) {
			$ribbon = "ribbon_on";
		} else {
			$ribbon = "ribbon_off";
		}
}
