$(document).ready(function(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
	initGallery();
	initScrollableHeight();
	_h = $('.scrollable').outerHeight(true);

//When page loads tabs settings

	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});



});
var _h;
function initGallery(){
	$('.block').Gallery({
		speed: 800,
		duration: 8000,
		holder: '.gallery'
	});
};

function initScrollableHeight(){

	var _sidebar = $('#sidebar');
	var _footer = $('#footer');
	var _header = $('#header');
	var _contentheader = $('#contentheader');
	var _content = $('content');
	var _scrollable = $('scrollable');
	var _scrollable2 = $('scrollable2');



	if (window.innerHeight){var _wy = window.innerHeight;}
	else{_wy = document.documentElement.clientHeight;};

	var _pageHeight = _sidebar.outerHeight(true) + _footer.outerHeight(true) + _header.outerHeight(true);
	//alert('header hoogte: ' +_header.outerHeight(true));
	//alert('sidebar hoogte: ' +_sidebar.outerHeight(true));
	//alert('footer hoogte: ' +_footer.outerHeight(true));
	//alert('contentheader hoogte: ' +_contentheader.outerHeight(true));
	//alert('content hoogte: ' +_content.outerHeight(true));
	//alert('scrollable hoogte: ' +_scrollable.outerHeight(true));

	if (_pageHeight < _wy){
//alert('pageheight: '+ _pageHeight + ' _wy : '+ _wy + ' _h:  ' + _h);
		var _scroll = $('.scrollable');
		_scroll.height(_sidebar.outerHeight(true)-_contentheader.outerHeight(true));

		var _scroll2 = $('.scrollable2');
		_scroll2.height(_sidebar.outerHeight(true)-_contentheader.outerHeight(true)-60);

		var _scroll3 = $('.scrollable3');
		_scroll3.height(_sidebar.outerHeight(true)-_contentheader.outerHeight(true)-120);


	}
};

$(window).resize(function(){	initScrollableHeight();	});
jQuery.fn.Gallery = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		duration: 4000,
		holder: '.holder',
		slider: '>ul',
		list: '>li',
		prev: 'a.prev',
		next: 'a.next',
		pager: 'ul.switcher',
		dinamicPagination: true,
		pause: '.pause'
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _duration = _options.duration;
		var _holder = _hold.find(_options.holder);
		var _slider = _holder.find(_options.slider);
		var _list = _slider.find(_options.list);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _f = true;
		var _p = _options.dinamicPagination;
	
		var _d = _list.eq(0).outerWidth(true);
/*--------ADDING SLIDES----------*/
		var _vis = Math.ceil(_holder.width()/_d);
		for	(var i=0; i < _vis; i++){
			_list.eq(i).clone().appendTo(_slider);
		};
/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager);
		if(_p){	_num.empty();
			_list.each(function(i){
				$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
			});	};
		var _thumb = _num.find('li');
/*-------------------------------------*/
		
		var _a = _list.index(_list.filter('.active'));
		if(_a == -1) {_a = 0;
			_thumb.eq(_a).addClass('active');
			_list.eq(_a).addClass('active');
		}
		var _x=0, _new, _t;

		Run(_a);
		function Run(_a){
			_t = setTimeout(function(){
				if (_a<_list.length){_a++
				}else {_a = 1;};
				Slide(_a);
			}, _duration);
		};
		function Slide(_new){
			_x = _new * _d;
			if (_new == _list.length){_new=0;}	_a = _new;
			_list.removeClass('active').eq(_new).addClass('active');
			_thumb.removeClass('active').eq(_new).addClass('active');
			_slider.animate({left: -_x}, {queue:false, duration:_speed, complete:function(){
				if (_new == 0){_slider.css({left:0});}
			}});
			if(_t) clearTimeout(_t);	Run(_new);
		};

		_thumb.click(function(){
			_a = _thumb.index($(this));
			Slide(_a);
			return false;
		});

		_next.click(function(){
			if (_a < _list.length){_a++
			}else {	_a = 1;	};
			Slide(_a);
			return false;
		});
		
		_prev.click(function(){
			if (_a>0){_a--
			}else {
				_slider.css({left:-_list.length*_d});
				_a = _list.length - 1;
			};
			Slide(_a);
			return false;
		});
	});
};
	

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";
	}
}


