//init page
jQuery(function(){
	initPosFixed();
	initClearInputs();
	isotopeInit();
	serviceListInit();
	emailButtons();
	navHover();
	homeSlideshow();
});

	//offsetX = "";


function homeSlideshow() {
	jQuery('.slideshow .images').cycle(
		{
			fx: 'uncover',
			timeout: 3000,
			speed: 2000,
			cleartypeNoBg:true		}
	);
	
	var timer = setInterval("redslash()",5000);
	redslash();
	
	
	if (jQuery('.slideshow').size()) {
	
		var offsetX = jQuery('.slideshow').offset().left;
		 
		jQuery(window).resize(function() {
			var offsetX = jQuery('.slideshow').offset().left;
		});
		
	
		jQuery(document).mousemove(function(e){
			clearInterval(timer);
			var x = e.pageX - offsetX - 58;
			//console.log(x);
			var x = (x > 481) ? 481 : x;
			var x = (x < 0) ? 0 : x;
			//console.log(x);
				//jQuery('#redslash').css({left:x});
				jQuery('#redslash').stop().animate({left:x});
			
		});
	
	}

}

function redslash() {
	jQuery('#redslash').animate(
		{left: '180'},250
	).delay(4750).animate(
		{left: '530'},250
	).delay(4750);
}

function navHover() {
	jQuery('#nav a').hover(function(){

		jQuery(this).parent().siblings().find('a').addClass('grey');
		

	},function(){
		jQuery('#nav a').removeClass('grey');
		//jQuery('#nav a').attr('style','');
		//jQuery(this).parent().siblings().find('a').removeClass('grey');

	});
}

function emailButtons() {
	jQuery('.form-holder .col2 a').click(function(){
		jQuery('#subject').val(jQuery(this).html());
		jQuery('#emailaddress').val(jQuery(this).attr('title'));
		jQuery('.form-holder .col2 a').parent().removeClass('active');
		jQuery(this).parent().addClass('active');
		jQuery('#resume,#filetype').addClass('hidden');
		return false;
		
	}).filter(':first').click();
	
	jQuery('#menu-item-96 a').click(function(){
		jQuery('#resume,#filetype').removeClass('hidden');	
	});
}

function serviceListInit() {
	jQuery('.toggle a').click(function(){
		jQuery('.toggle li').removeClass('active');
		var _this = jQuery(this);
		var desc = _this.attr('rel');
		_this.parent().addClass('active');
		_this.parents().find('.menu').next().html(desc);
		return false;
	}).filter(':first').click();
	

}

function isotopeInit() {
      var $container = jQuery('.items');

      $container.isotope({
        itemSelector : 'li'
      });


		jQuery('.nav-block a').click(function(){
		  jQuery('.nav-block a').parent().removeClass('active');
		  jQuery(this).parent().addClass('active');
		  var selector = jQuery(this).attr('data-filter');
		  $container.isotope({ filter: selector });
			jQuery('html,body').animate({scrollTop: jQuery(".items").offset().top-80});
		  return false;
		});
}

//init position fixed
function initPosFixed(){
	var nav = jQuery('.nav-block');
	var content = jQuery('.items');
	if(!nav.length) return;
	var leftPos = nav.offset().left;
	var topPos = nav.offset().top;
	var win = jQuery(window);
	var ie6 = jQuery.browser.msie && jQuery.browser.version < 7;
	
	win.resize(function(){
		nav.css({position: 'absolute', left: '', top: '-6px', right: '-6px'});
		leftPos = nav.offset().left;	
		reposition();
	});
	
	function reposition() {
	
		var minTopPos = topPos + content.outerHeight() - nav.outerHeight();

		var scrollNow = win.scrollTop()+100;
		if(scrollNow < topPos){
			nav.css({position: 'absolute', left: '', top: '-6px', right: '-6px'});
		} else if (scrollNow > topPos && scrollNow < minTopPos){
			if(ie6){
				nav.css({top: scrollNow - topPos});
			} else nav.css({position: 'fixed',left:leftPos, top:'100px', right: 'auto'});
		} else if (scrollNow > minTopPos){
			nav.css({position: 'absolute', left:'', top:minTopPos - topPos, right: '-6px'});
		}		
	}
	
	win.scroll(reposition);
	
}

//init clear inputs
function initClearInputs(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}

//clear form fields module
function clearFormFields(o){
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
