// tabs function
function initTabs() {
	$('ul.tabset').each(function () {
		var _list = $(this);
		var _links = _list.find('a.tab');
		var _original = _list.find('a#tab3');
		var _orighref = _original.attr('id');
		var _origtab = $(_orighref);

		_links.each(function () {
			var _link = $(this);
			var _href = _link.attr('id');
			var _tab = $(_href);

			if (_link.hasClass('active')) { _tab.show(); }
			else { _tab.hide(); }

			_link.mouseover(function () {
				_links.filter('.active').each(function () {
					$($(this).removeClass('active').attr('id')).hide();
				});
				//console.log(_link);
				_link.addClass('active');
				$('#tab3').hide();
				_tab.show();
				return false;
			});
			
			_link.mouseout(function () {
				_links.filter('.active').each(function () {
					$($(this).removeClass('active').attr('id')).hide();
				});
				_original.addClass('active');
				$('#tab3').show();
				return false;
			});
			
		});
	});
}
/*function slideEfect() {
    var _parentSlide = 'div.slider'; 
    var _linkSlide = 'a.contact-us'; 
    var _slideBlock = 'div.cont-us'; 
    var _openClassS = 'active'; 
    var _durationSlide = 750;
	var _contactLink = 'a.contact';
	
	console.log(_parentSlide);
	console.log(_linkSlide);
	console.log(_contactLink);
	
    $(_parentSlide).each(function(){
	if (!$(this).is('.'+_openClassS)) {
	    $(this).find(_slideBlock).css({
			height:'0px',
			padding:'0px'
		});
	}
    });
    $(_linkSlide,_parentSlide,_contactLink).click(function(){
	if ($(_linkSlide).parents(_parentSlide).is('.'+_openClassS)) {
	    $(_linkSlide).parents(_parentSlide).removeClass(_openClassS);
	    $(_linkSlide).parents(_parentSlide).find(_slideBlock).css({height:'auto'}).slideUp(_durationSlide);
	    $(_linkSlide).text(_textOpenS);
	} else {
	    $(_linkSlide).parents(_parentSlide).addClass(_openClassS);
	    $(_linkSlide).parents(_parentSlide).find(_slideBlock).css({height:'auto', padding:'24px 0 34px 0'}).hide().slideDown(_durationSlide);
	    $(_linkSlide).text(_textCloseS);
	}
	return false;
    });

}*/
function initContactExpander() {
	$(".contact-us").click(function () {
		$(".cont-us").slideToggle('slow');		
	});
	$(".contact").click(function () {
		$(".cont-us").slideToggle('slow');
	});
	$(".close").click(function () {
		$(".cont-us").slideToggle('slow');
	});
}

// smooth page scroller function
function initPageScroller() {
	// replace sifr link
//	if (typeof sIFR === "function") {
//		sIFR.replaceElement(named({sSelector: "div.to-top span", sFlashSrc: "swf/avand_regular.swf", sColor: "#dee1e3", nPaddingTop: 0, sWmode: "transparent", nPaddingBottom: 0, sFlashVars: "textalign=left&offsetTop=0"}));
//	}

	// scroll options
	var _opener = $('#page-scroll').hide();//.css({opacity:0});
	var _main = $('#wrapper');
	var _easing = 'easeOutExpo';
	var _showScrollTop = 200;
	var _scrollSpeed = 10;
	var _fadeSpeed = 200;

	if($.browser.msie && $.browser.version < 7) {
		_opener.css({
			position:'absolute',
			top: $(window).height() - _opener.height(),
			right: 0
		});
	} else {
		_opener.css({
			position:'fixed',
			bottom: 0,
			right: 0
		});
	}

	_opener.click(function () {
		console.log('clicked')
		$.scrollTo(_main, _scrollSpeed, {easing:_easing});
		return false;
	});

	$(window).scroll(function () {
		// toggle visibility
		if($(window).scrollTop() > _showScrollTop) {
			//_opener.animate({opacity:1},{duration:_fadeSpeed,queue:false});
			_opener.fadeIn(_fadeSpeed);
		} else {
			//_opener.animate({opacity:0},{duration:_fadeSpeed,queue:false});
			_opener.fadeOut(_fadeSpeed);
		}

		// position element
		if($.browser.msie && $.browser.version < 7) {
			_opener.css({
				top: $(window).height() + $(window).scrollTop() - _opener.height(),
				bottom: 'auto'
			});
		} else {
			_opener.css({
				top: $(window).height() - _opener.height(),
				bottom: 'auto'
			});
		}
	});

	$(window).resize(function () {
		// position element
		if($.browser.msie && $.browser.version < 7) {
			_opener.css({
				top: $(window).height() + $(window).scrollTop() - _opener.height(),
				bottom: 'auto'
			});
		} else {
			_opener.css({
				top: $(window).height() - _opener.height(),
				bottom: 'auto'
			});
		}
	});
	
	


	
	
	
}

//homepage paragraph collapser/expander
function p_expander () {
	$("div#hide-we-offer p:gt(0)").css("display","none");
	$("div#hide-we-are p:gt(0)").css("display","none");
	$("div#hide-we-know p:gt(0)").css("display","none");	
	
	$("div#hide-we-offer div.btn a span").click(function () {
		$("div#hide-we-offer p:gt(0)").toggle();
		if($(this).text() == "LESS") {
			$(this).text("MORE");
		} else {
			$(this).text("LESS");
		}	
	});
	$("div#hide-we-are div.btn a span").click(function () {
		$("div#hide-we-are p:gt(0)").toggle();
		if($(this).text() == "LESS") {
			$(this).text("MORE");
		} else {
			$(this).text("LESS");
		}
	});
	$("div#hide-we-know div.btn a span").click(function () {
		$("div#hide-we-know p:gt(0)").toggle();
		if($(this).text() == "LESS") {
			$(this).text("MORE");
		} else {
			$(this).text("LESS");
		}
	});
}

var SLIDETIMER = 3;
var SLIDESPEED = 3;
var SCROLLTIMER = 3;
var SCROLLSPEED = 3;
var STARTINGOPACITY = 40;

// handles section to section scrolling of the content //
function slideContent(id,prefix,timer) {
  var div = document.getElementById(id);
  var slider = div.parentNode;
  clearInterval(slider.timer);
  slider.section = parseInt(id.replace(/\D/g,''));
  slider.target = div.offsetTop;
  slider.style.top = slider.style.top || '0px';
  slider.current = slider.style.top.replace('px','');
  slider.direction = (Math.abs(slider.current) > slider.target) ? 1 : -1;
  slider.style.opacity = STARTINGOPACITY * .01;
  slider.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  slider.timer = setInterval( function() { slideAnimate(slider,prefix,timer) }, SLIDETIMER);
}

function slideAnimate(slider,prefix,timer) {
  var curr = Math.abs(slider.current);
  var tar = Math.abs(slider.target);
  var dir = slider.direction;
  if((tar - curr <= SLIDESPEED && dir == -1) || (curr - tar <= SLIDESPEED && dir == 1)) {
    slider.style.top = (slider.target * -1) + 'px';
	slider.style.opacity = 1;
	slider.style.filter = 'alpha(opacity=100)';
    clearInterval(slider.timer);
	if(slider.autoscroll) {
	  setTimeout( function() { autoScroll(slider.id,prefix,timer) }, timer * 1000);
	}
  } else {
	var pos = (dir == 1) ? parseInt(slider.current) + SLIDESPEED : slider.current - SLIDESPEED;
    slider.current = pos;
    slider.style.top = pos + 'px';
  }
}

// handles manual scrolling of the content //
function scrollContent(id,dir) {
  var div = document.getElementById(id);
  clearInterval(div.timer);
  var sections = div.getElementsByTagName('div');
  var length = sections.length;
  var limit;
  if(dir == -1) {
    limit = 0;
  } else {
    if(length > 1) {
      limit = sections[length-1].offsetTop;
    } else {
      limit = sections[length-1].offsetHeight - div.parentNode.offsetHeight + 20;
    }
  }
  div.style.opacity = STARTINGOPACITY * .01;
  div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  div.timer = setInterval( function() { scrollAnimate(div,dir,limit) }, SCROLLTIMER);
}

function scrollAnimate(div,dir,limit) {
  div.style.top = div.style.top || '0px';
  var top = div.style.top.replace('px','');
  if(dir == 1) {
	if(limit - Math.abs(top) <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = '-' + limit + 'px';
	} else {
	  div.style.top = top - SCROLLSPEED + 'px';
	}
  } else {
	if(Math.abs(top) - limit <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = limit + 'px';
	} else {
	  div.style.top = parseInt(top) + SCROLLSPEED + 'px';
	}
  }
}

// cancel the scrolling on mouseout //
function cancelScroll(id) {
  var div = document.getElementById(id);
  div.style.opacity = 1;
  div.style.filter = 'alpha(opacity=100)';
  clearTimeout(div.timer);
}

// initiate auto scrolling //
function autoScroll(id,prefix,timer,restart) {
  var div = document.getElementById(id);
  div.autoscroll = (!div.autoscroll && !restart) ? false : true;
  if(div.autoscroll) {
    var sections = div.getElementsByTagName('div');
    var length = sections.length;
    div.section = (div.section && div.section < length) ? div.section + 1 : 1;
    slideContent(prefix + '-' + div.section,prefix,timer);
  }
}

// cancel automatic scrolling //
function cancelAutoScroll(id) {
  var div = document.getElementById(id);
  div.autoscroll = false;
}

// page init
$(window).bind("load", function () {
	initPageScroller();
	p_expander();
});
$(function () {
	initTabs();
	initContactExpander();
});



/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e);};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable();};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);





/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
*/
jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,f,a,h,g){return jQuery.easing[jQuery.easing.def](e,f,a,h,g)},easeInQuad:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutQuad:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutQuad:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a},easeInCubic:function(e,f,a,h,g){return h*(f/=g)*f*f+a},easeOutCubic:function(e,f,a,h,g){return h*((f=f/g-1)*f*f+1)+a},easeInOutCubic:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f+a}return h/2*((f-=2)*f*f+2)+a},easeInQuart:function(e,f,a,h,g){return h*(f/=g)*f*f*f+a},easeOutQuart:function(e,f,a,h,g){return -h*((f=f/g-1)*f*f*f-1)+a},easeInOutQuart:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f+a}return -h/2*((f-=2)*f*f*f-2)+a},easeInQuint:function(e,f,a,h,g){return h*(f/=g)*f*f*f*f+a},easeOutQuint:function(e,f,a,h,g){return h*((f=f/g-1)*f*f*f*f+1)+a},easeInOutQuint:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f*f+a}return h/2*((f-=2)*f*f*f*f+2)+a},easeInSine:function(e,f,a,h,g){return -h*Math.cos(f/g*(Math.PI/2))+h+a},easeOutSine:function(e,f,a,h,g){return h*Math.sin(f/g*(Math.PI/2))+a},easeInOutSine:function(e,f,a,h,g){return -h/2*(Math.cos(Math.PI*f/g)-1)+a},easeInExpo:function(e,f,a,h,g){return(f==0)?a:h*Math.pow(2,10*(f/g-1))+a},easeOutExpo:function(e,f,a,h,g){return(f==g)?a+h:h*(-Math.pow(2,-10*f/g)+1)+a},easeInOutExpo:function(e,f,a,h,g){if(f==0){return a}if(f==g){return a+h}if((f/=g/2)<1){return h/2*Math.pow(2,10*(f-1))+a}return h/2*(-Math.pow(2,-10*--f)+2)+a},easeInCirc:function(e,f,a,h,g){return -h*(Math.sqrt(1-(f/=g)*f)-1)+a},easeOutCirc:function(e,f,a,h,g){return h*Math.sqrt(1-(f=f/g-1)*f)+a},easeInOutCirc:function(e,f,a,h,g){if((f/=g/2)<1){return -h/2*(Math.sqrt(1-f*f)-1)+a}return h/2*(Math.sqrt(1-(f-=2)*f)+1)+a},easeInElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k)==1){return e+l}if(!j){j=k*0.3}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}return -(g*Math.pow(2,10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j))+e},easeOutElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k)==1){return e+l}if(!j){j=k*0.3}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}return g*Math.pow(2,-10*h)*Math.sin((h*k-i)*(2*Math.PI)/j)+l+e},easeInOutElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k/2)==2){return e+l}if(!j){j=k*(0.3*1.5)}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}if(h<1){return -0.5*(g*Math.pow(2,10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j))+e}return g*Math.pow(2,-10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j)*0.5+l+e},easeInBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*(f/=h)*f*((g+1)*f-g)+a},easeOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*((f=f/h-1)*f*((g+1)*f+g)+1)+a},easeInOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}if((f/=h/2)<1){return i/2*(f*f*(((g*=(1.525))+1)*f-g))+a}return i/2*((f-=2)*f*(((g*=(1.525))+1)*f+g)+2)+a},easeInBounce:function(e,f,a,h,g){return h-jQuery.easing.easeOutBounce(e,g-f,0,h,g)+a},easeOutBounce:function(e,f,a,h,g){if((f/=g)<(1/2.75)){return h*(7.5625*f*f)+a}else{if(f<(2/2.75)){return h*(7.5625*(f-=(1.5/2.75))*f+0.75)+a}else{if(f<(2.5/2.75)){return h*(7.5625*(f-=(2.25/2.75))*f+0.9375)+a}else{return h*(7.5625*(f-=(2.625/2.75))*f+0.984375)+a}}}},easeInOutBounce:function(e,f,a,h,g){if(f<g/2){return jQuery.easing.easeInBounce(e,f*2,0,h,g)*0.5+a}return jQuery.easing.easeOutBounce(e,f*2-g,0,h,g)*0.5+h*0.5+a}});
