(function($){

function initPage () {
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: false,
		filterClass: "default"
	});
	textChanger.init();
	classTabs.init ();
}

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";
	}
}

// classTabs
var classTabs = {
	hasClass: function(obj,cname) {
		return (obj.className ? obj.className.match(new RegExp('(\\s|^)'+cname+'(\\s|$)')) : false);
	},
	addClass: function (obj,cname) {
		if (!this.hasClass(obj,cname)) {obj.className += " "+cname;this.accurateRecordClass(obj)}
	},
	removeClass: function(obj,cname) {
		if (this.hasClass(obj,cname)) {obj.className=obj.className.replace(new RegExp('(\\s|^)'+cname+'(\\s|$)'),' ');this.accurateRecordClass(obj)}
	},
	accurateRecordClass: function (obj) {
		if (obj.className) obj.className = obj.className.replace(/(\s+)/g, ' ').replace(/(^ | $)/g, '');
	},
	applyCss: function (obj, css){
		for(var key in css) {
			obj.style[key] = css[key];
		}
	},
	applyEvent: function(v,settings) {
		var c = document.getElementById(v.hr);
		if (c) {
			for (var i = 0; i < v.tab.length; i++) {
				this.applyCss(document.getElementById(v.tab[i].hr),settings.cssHide);
				this.removeClass(v.tab[i],settings.activeClass);
				if (settings.addParentClass) {
					this.removeClass(v.tab[i].parent,settings.parentActiveClass);
				}
			}
			this.addClass(v,settings.activeClass);
			if (settings.addParentClass) {
				this.addClass(v.parent,settings.parentActiveClass);
			}
			this.applyCss(c,settings.cssShow);
			return false;
		}
	},
	init: function (o) {
		var settings = {
			addParentClass: false,
			addFirstLast: false,
			addHoverClass: false,
			eventClick: true,
			clickDisabled:true,
			cssShow:{position: 'static',left: 0,top: 0},
			cssHide:{position: 'absolute',left: '-9999px',top: '-99999px'},
			activeClass: 'active',
			parentActiveClass: 'parentactive',
			firstClass: 'first',
			lastClass: 'last',
			hoverClass: 'hover',
			tagTabMenu: 'ul',
			classTabMenu: 'tabset',
			tagBtnTabMenu: 'a',
			classBtnTabMenu: 'tab'
		};
		if (typeof(o) == 'object') {
			for(var key in o) if (typeof(settings[key]) == typeof(o[key])) settings[key] = o[key];
		}
		this.sets = document.getElementsByTagName(settings.tagTabMenu);
		for (var i = 0; i < this.sets.length; i++) {
			if (this.hasClass(this.sets[i],settings.classTabMenu)) {
				var tab = [];
				var btn = this.sets[i].getElementsByTagName(settings.tagBtnTabMenu);
				if (btn.length>1 && settings.addFirstLast){
					this.addClass(btn[0].parentNode, settings.firstClass);
					this.addClass(btn[0], settings.firstClass);
					this.addClass(btn[btn.length -1].parentNode, settings.lastClass);
					this.addClass(btn[btn.length -1], settings.lastClass);
				}
				for (var j = 0; j < btn.length; j++) {
					if (settings.addHoverClass){
						btn[j].parentNode.onmouseover = function() {classTabs.addClass(this,settings.hoverClass)}
						btn[j].parentNode.onmouseout = function() {classTabs.removeClass(this,settings.hoverClass)}
					}
					if (this.hasClass(btn[j],settings.classBtnTabMenu)) {
						var _hr = btn[j].href;
						if(_hr.indexOf('#') != -1) {
							_hr = _hr.substr(_hr.indexOf("#") + 1);
							if (_hr.length >0) {
								btn[j].hr = _hr;
								btn[j].parent = btn[j].parentNode;
								tab.push(btn[j]);
								btn[j].tab = tab;
								var c = document.getElementById(_hr);
								if (c) {
									if (this.hasClass(btn[j],settings.activeClass)) {
										this.applyCss(c,settings.cssShow);
										if (settings.addParentClass && !this.hasClass(btn[j].parent,settings.parentActiveClass)) {
											this.addClass(btn[j].parent,settings.parentActiveClass);
										}
									} else {
										this.applyCss(c,settings.cssHide);
									}
								}
								if (settings.eventClick) {
									btn[j].onclick = function () {
										classTabs.applyEvent(this,settings);
										return false;
									}
								} else {
									btn[j].onmouseover = function() {
										classTabs.applyEvent(this,settings);
									}
									if (settings.clickDisabled) {
										btn[j].onclick = function () {return false;}
									}
								}
							}
						}
					}
				}
			}
		}
	}
};

var textChanger = {
	defaultFS : 1.2,
	FS1: 1.5,
	FS2: 1.8,
	init: function() {
		var el = document.getElementsByTagName("body")[0];
		el.style.fontSize = textChanger.defaultFS + 'em';
		var incr1 = document.getElementById('increase-1');
		if(incr1)
			incr1.onclick = function(){textChanger.changeSize(textChanger.FS1); return false;};
		var incr2 = document.getElementById('increase-2');
		if(incr2)
			incr2.onclick = function(){textChanger.changeSize(textChanger.FS2); return false;};
		var reset = document.getElementById('reset');
		if(reset)
			reset.onclick = function(){textChanger.changeSize(textChanger.defaultFS); return false;};
	},
	changeSize: function(val) {
		var el = document.getElementsByTagName("body")[0];
		fSize = val;
		el.style.fontSize = val + 'em';
	}
}

// mobile browsers detect
browserPlatform = {
	platforms: [
		{
			// Blackberry <5
			uaString:['BlackBerry','midp'],
			cssFile:'blackberry.css'
		},
		{
			// Symbian phones
			uaString:['symbian','midp'],
			cssFile:'symbian.css'
		},
		{
			// Opera Mobile
			uaString:['opera','mobi'],
			cssFile:'opera.css'
		},
		{
			// IE Mobile <6
			uaString:['msie','ppc'],
			cssFile:'ieppc.css'
		},
		{
			// IE Mobile 6+
			uaString:'iemobile',
			cssFile:'iemobile.css'
		},
		{
			// Palm WebOS
			uaString:'webos',
			cssFile:'webos.css'
		},
		{
			// Android
			uaString:'Android',
			cssFile:'android.css'
		},
		{
			// Blackberry 6+
			uaString:['BlackBerry','6.0','mobi'],
			cssFile:'blackberry6.0.css'
		},
		{
			// iPad
			uaString:'ipad',
			cssFile:'ipad.css',
			miscHead:''
		},
		{
			// iPhone and other webkit browsers
			uaString:['safari','mobi'],
			cssFile:'safari.css',
			miscHead:''
		}
	],
	options: {
		cssPath:'css/',
		mobileCSS:'allmobile.css'
	},
	init:function(){
		this.checkMobile();
		this.parsePlatforms();
		return this;
	},
	checkMobile: function() {
		if(this.uaMatch('mobi') || this.uaMatch('midp') || this.uaMatch('ppc') || this.uaMatch('webos')) {
			this.attachStyles({cssFile:this.options.mobileCSS});
		}
	},
	parsePlatforms: function() {
		for(var i = 0; i < this.platforms.length; i++) {
			if(typeof this.platforms[i].uaString === 'string') {
				if(this.uaMatch(this.platforms[i].uaString)) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			} else {
				for(var j = 0, allMatch = true; j < this.platforms[i].uaString.length; j++) {
					if(!this.uaMatch(this.platforms[i].uaString[j])) {
						allMatch = false;
					}
				}
				if(allMatch) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			}
		}
	},
	attachStyles: function(platform) {
		if(platform.cssFile) {
			document.write('<link rel="stylesheet" href="' + this.options.cssPath + platform.cssFile + '" type="text/css"/>');
		}
		if(platform.miscHead) {
			document.write(platform.miscHead);
		}
	},
	uaMatch:function(str) {
		if(!this.ua) {
			this.ua = navigator.userAgent.toLowerCase();
		}
		return this.ua.indexOf(str.toLowerCase()) != -1;
	}
}.init();

if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent && !window.opera)
    window.attachEvent("onload", initPage);

/* anythingslider.js */
$(document).ready(function () {

    $(".anythingSlider").anythingSlider({
        easing: "swing",                // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        delay: 10000,                    // How long between slide transitions in AutoPlay mode
        animationTime: 2000,             // How long the slide transition takes
        hashTags: false,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
        pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
        startText: "Start",             // Start text
        stopText: "Stop",               // Stop text
        navigationFormatter: null       // Details at the top of the file on this use (advanced use)
    });
});

//Advanced search
$(document).ready(function () {
    $('.AdvancedSearch').each(function () {
        $(this).click(function () {
            $(this).prev().toggle('slow');
            return false;
        });
    });
//    clearInputs();
});


})(jQuery);
