jQuery(function(){
	clearInputs();
	initNavFix();
	initGalleries();
	initCufon();
	hoverForIE6(".gallery-holder li", "gallery-hover");
});
function hoverForIE6(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		jQuery(h_list).hover(function (){
			jQuery(this).addClass(h_class);
		}, function (){
			jQuery(this).removeClass(h_class);
		});
	}
}
//init cufon
function initCufon(){
	Cufon.replace('.gallery-holder .text *', { textShadow: '#343636 1px 1px', fontFamily: 'Droid Serif', hover: true });
	Cufon.replace('.slogan', { fontFamily: 'Droid Serif Italic', hover: true });
}
// galleries init
function initGalleries() {
	// homepage gallery init
	// jQuery('div.gallery').fadeGallery({
		// slideElements:'div.gallery-holder > ul > li',
		// pagerLinks:'div.switcher li',
		// pauseOnHover:true,
		// autoRotation:true,
		// autoHeight:false,
		// switchTime:4000,
		// pagerHold: 'div.switcher',
		// duration:650
	// });
	jQuery('div.gallery').each(function(){
		var set = jQuery(this);
		set.find('.gallery-holder>ul>li').each(function(){
			jQuery(this).attr('url-image', jQuery(this).attr('title')).removeAttr('title');
		});
		var slideshow = new initSlidesNV(set,{
			list: '.gallery-holder>ul',
			swichTime: 4000,
			loadImageFlag: true,
			ajaxBox: '.image-wrapper',
			pagerHold: 'div.switcher',
			onStart: function(obj, list, slides, next){
				obj.autoHeight(0)
			}
		});
	});
};


/* slideshow module */
function initSlidesNV(context, options){this.init(context, options)}
(function( $ ){
	initSlidesNV.prototype = {
		list: '>ul',
		slides: '>li',
		animationSpeed: 500,
		btnPrev: '.btn-prev',
		btnNext: '.btn-next',
		eventType: 'click',
		eventTypeForPager: 'click',
		pagerLinks: false,
		pagerHold: false,
		swichTime: false, // 1000 = 1 sec
		circle: true,
		direction: 'rtl', // 'rtl', 'ltr', 'ttb', 'btt'
		autoHeightList: true,
		startSlide: 0,
		stopOnHover: false,
		onStart: function(){},
		afterAnimation: function(){},
		loadImageFlag: false,
		imageUrl: 'url-image',
		ajaxBox: '.image-box',
		init: function(context, options){
			for ( var i in options ) this[i] = options[i]; 
			this.set = jQuery(context);
			if(this.set == undefined) return;
			this.waitAnimation = true;
			this.prevSlide = this.nextSlide = this.startSlide;
			this.btnNext = this.set.find(this.btnNext);
			this.btnPrev = this.set.find(this.btnPrev);
			this.list = this.set.find(this.list);
			this.slides = this.list.find(this.slides);
			
			if(this.set.find(this.pagerHold).length){
				var paging = '';
				for(var i=0; i< this.slides.length; i++){
					paging += '<li><a href="#">'+(i+1)+'</a></li>';
				}
				this.set.find(this.pagerHold).html('<ul>'+paging+'</ul>');
				this.pagerLinks = this.set.find(this.pagerHold).find('a');
			};
			if(!this.set.find(this.pagerHold).length){
				this.pagerLinks = this.set.find(this.pagerLinks);
			};
			if(this.pagerLinks){
				this.pagerLinks.each(this.binder(function(i, links){
					jQuery(links).bind(this.eventTypeForPager, this.binder(function(){
						this.prevSlide = this.nextSlide;
						this.nextSlide = i;
						if(this.loadImageFlag && !this.slides.eq(this.nextSlide).data('load')){
							this.loadImage(this.binder(function(){
								if(this.nextSlide != this.prevSlide) this.swichSlide(this.prevSlide, this.nextSlide);
							}))
						}
						else {
							if(this.nextSlide != this.prevSlide) this.swichSlide(this.prevSlide, this.nextSlide);
						};
						return false;
					}))
				}));
				this.pagerLinks.parent().removeClass('active');
				this.pagerLinks.eq(this.nextSlide).parent().addClass('active');
			};
			this.btnNext.bind(this.eventType, this.binder(function(){
				if(this.waitAnimation){
					this.waitAnimation = false;
					this.next();
				};
				return false;
			}));
			this.btnPrev.bind(this.eventType, this.binder(function(){
				if(this.waitAnimation){
					this.waitAnimation = false;
					this.prev();
				};
				return false;
			}));
			this.autoRotation();
			if(this.stopOnHover){
				this.set.bind('mouseenter', this.binder(function(){
					clearTimeout(this.timer);
				})).bind('mouseleave', this.binder(function(){
					this.autoRotation();
				}))
			};
			if(this.loadImageFlag && !this.slides.eq(this.nextSlide).data('load')){
				this.loadImage(this.binder(function(){
					this.slides.hide().eq(this.nextSlide).show();
				}))
			}
			else {
				this.slides.hide().eq(this.nextSlide).show();
			};
			this.onStart(this, this.list, this.slides, this.nextSlide);
		},
		prev: function(){
			this.prevSlide = this.nextSlide;
			if(this.nextSlide > 0){
				this.nextSlide--
			}
			else if(this.circle) {
				this.nextSlide = this.slides.length-1
			};
			if(this.loadImageFlag && !this.slides.eq(this.nextSlide).data('load')){
				this.loadImage(this.binder(function(){
					if(this.nextSlide != this.prevSlide) this.swichSlide(this.prevSlide, this.nextSlide);
				}))
			}
			else {
				if(this.nextSlide != this.prevSlide) this.swichSlide(this.prevSlide, this.nextSlide);
			};
		},
		next: function(){
			this.prevSlide = this.nextSlide;
			if(this.nextSlide < this.slides.length-1){
				this.nextSlide++
			}
			else if(this.circle) {
				this.nextSlide = 0
			};
			if(this.loadImageFlag && !this.slides.eq(this.nextSlide).data('load')){
				this.loadImage(this.binder(function(){
					if(this.nextSlide != this.prevSlide) this.swichSlide(this.prevSlide, this.nextSlide);
				}))
			}
			else {
				if(this.nextSlide != this.prevSlide) this.swichSlide(this.prevSlide, this.nextSlide);
			};
		},
		swichSlide: function(prev, next){
			if(!this.slides.eq(next).length) return;
			this.slides.eq(prev).animate({
				opacity: 0
			}, { queue: false, duration: this.animationSpeed, complete: this.binder(function(){
				this.slides.eq(prev).css({ display: 'none' });
			})})
			this.slides.eq(next).css({ 
				opacity: 0,
				display: 'block'
			}).animate({
				opacity: 1
			}, { queue: false, duration: this.animationSpeed, complete: this.binder(function(){
				this.afterAnimation(this, this.list, this.slides, next);
				this.waitAnimation = true;
				if(this.autoHeightList) this.autoHeight(this.nextSlide);
				clearTimeout(this.timer);
				this.autoRotation();
			})});
			if(this.pagerLinks){
				this.pagerLinks.parent().removeClass('active');
				this.pagerLinks.eq(this.nextSlide).parent().addClass('active');
			}
		},
		loadImage: function(callback){
			var newImage = new Image();
			newImage.onload = this.binder(function(){
				this.slides.eq(this.nextSlide).find(this.ajaxBox).append(newImage);
				this.slides.eq(this.nextSlide).data('load', 'loaded');
				if(this.autoHeightList) this.autoHeight(this.nextSlide);
				callback();
			});
			newImage.src = this.slides.eq(this.nextSlide).attr(this.imageUrl);
		},
		autoHeight: function(next){
			if(this.autoHeightList){
				this.list.animate({ height: this.slides.eq(next).outerHeight() }, { queue: false, duration: 150 });
			}
		},
		autoRotation: function(){
			if(this.swichTime && this.swichTime > 0){
				clearTimeout(this.timer);
				this.timer = setTimeout(this.binder(function(){
					if(this.direction == 'ltr'){
						this.prev();
					}else if(this.direction == 'rtl') {
						this.next();
					};
				}), this.swichTime)
			}
		},
		binder: function(fn, scope, args){
			var newScope = scope || this;
			return function() {
				return fn.apply(newScope, args || arguments);
			}
		}
	}
})( jQuery );

function clearInputs(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
// page init
function initNavFix() {
	new touchNav({
		navBlock: 'nav'
	});
}
// navigation accesibility module
function touchNav(options) {
	this.options = {
		mobileReg: /(ipad|iphone|ipod|android|blackberry|iemobile)/gi,
		hoverClass: 'hover',
		followLink: false,
		menuItems: 'li',
		menuOpener: 'a',
		menuDrop: 'div',
		navBlock: null
	}
	for(var p in options) {
		this.options[p] = options[p];
	}
	this.init();
}
touchNav.prototype = {
	init: function() {
		this.isMobile = (this.options.mobileReg).test(navigator.userAgent);
		if(typeof this.options.navBlock === 'string') {
			this.menu = document.getElementById(this.options.navBlock);
		} else if(typeof this.options.navBlock === 'object') {
			this.menu = this.options.navBlock;
		}
		if(this.menu) {
			this.getElements();
			this.addEvents();
		}
	},
	getElements: function() {
		this.menuItems = this.menu.getElementsByTagName(this.options.menuItems);
	},
	hideActiveDropdown: function() {
		if(this.activeParent) {
			for(var i = 0; i < this.menuItems.length; i++) {
				this.removeClass(this.menuItems[i], this.options.hoverClass);
			}
			this.activeParent = null;
		}
	},
	getOpener: function(obj) {
		for(var i = 0; i < obj.childNodes.length; i++) {
			if(obj.childNodes[i].tagName && obj.childNodes[i].tagName.toLowerCase() == this.options.menuOpener.toLowerCase()) {
				return obj.childNodes[i];
			}
		}
		return false;
	},
	getDrop: function(obj) {
		for(var i = 0; i < obj.childNodes.length; i++) {
			if(obj.childNodes[i].tagName && obj.childNodes[i].tagName.toLowerCase() == this.options.menuDrop.toLowerCase()) {
				return obj.childNodes[i];
			}
		}
		return false;
	},
	addEvents: function() {
		// mobile event handlers
		if(this.isMobile) {
			for(var i = 0; i < this.menuItems.length; i++) {
				this.menuItems[i].touchNav = this;
				if(this.getDrop(this.menuItems[i])) {
					this.addHandler(this.getOpener(this.menuItems[i]), 'click', this.bind(this.clickHandler,this.menuItems[i]));
				}
			}
			this.addHandler(document.body, 'click', this.bind(this.outsideHandler, this));
			this.addHandler(document.body, 'touchstart', this.bind(this.outsideHandler, this));
		}
		// desktop event handlers
		else {
			for(var i = 0; i < this.menuItems.length; i++) {
				this.menuItems[i].touchNav = this;
				this.addHandler(this.menuItems[i], 'mouseover', this.mouseoverHandler);
				this.addHandler(this.menuItems[i], 'mouseout', this.mouseoutHandler);
			}
		}
	},
	outsideHandler: function(e) {
		var childFlag = false;
		if(this.activeParent) {
			this.outsideTarget = e.target || e.currentTarget || e.srcElement;
			while (this.outsideTarget.parentNode) {
				if(this.activeParent == this.outsideTarget) {
					childFlag = true;
					break;
				}
				this.outsideTarget = this.outsideTarget.parentNode;
			}
			if(!childFlag) {
				this.hideActiveDropdown();
			}
		}
	},
	mouseoverHandler: function() {
		this.touchNav.addClass(this, this.touchNav.options.hoverClass);
	},
	mouseoutHandler: function() {
		this.touchNav.removeClass(this, this.touchNav.options.hoverClass);
	},
	clickHandler: function(e) {
		// get current dropdown
		var tNav = this.touchNav;
		tNav.currentElement = e.currentTarget || e.srcElement;
		tNav.currentParent = tNav.currentElement.parentNode;

		// hide previous drop (if exists)
		if(tNav.activeParent && !tNav.isParent(tNav.activeParent, tNav.currentParent) && tNav.currentParent != tNav.activeParent) {
			tNav.hideActiveDropdown();
		}

		// handle current drop
		if(tNav.hasClass(tNav.currentParent, tNav.options.hoverClass)) {
			tNav.removeClass(tNav.currentParent, tNav.options.hoverClass);
			if(tNav.options.followLink) {
				window.location.href = tNav.currentElement.href;
			}
		} else {
			tNav.addClass(tNav.currentParent, tNav.options.hoverClass);
			tNav.activeParent = tNav.currentParent;
			return tNav.preventEvent(e);
		}
	},
	preventEvent: function(e) {
		if(!e) e = window.event;
		if(e.preventDefault) e.preventDefault();
		if(e.stopPropagation) e.stopPropagation();
		e.cancelBubble = true;
		return false;
	},
	isParent: function(parent, child) {
		while(child.parentNode) {
			if(child.parentNode == parent) {
				return true;
			}
			child = child.parentNode;
		}
		return false;
	},
	addHandler: function(object, event, handler) {
		if (typeof object.addEventListener != 'undefined') object.addEventListener(event, this.bind(handler,object), false);
		else if (typeof object.attachEvent != 'undefined') object.attachEvent('on' + event, this.bind(handler,object));
	},
	removeHandler: function(object, event, handler) {
		if (typeof object.removeEventListener != 'undefined') object.removeEventListener(event, handler, false);
		else if (typeof object.detachEvent != 'undefined') object.detachEvent('on' + event, handler);
	},
	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;
	},
	removeClass: function(obj,cname) {
		if (this.hasClass(obj,cname)) obj.className=obj.className.replace(new RegExp('(\\s|^)'+cname+'(\\s|$)'),' ');
	},
	bind: function(func, scope){
		return function() {
			return func.apply(scope, arguments);
		}
	}
}
// mobile browsers detect
browserPlatform = {
	platforms: [
		{ uaString:['symbian','midp'], cssFile:'symbian.css' }, // Symbian phones
		{ uaString:['opera','mobi'], cssFile:'opera.css' }, // Opera Mobile
		{ uaString:['msie','ppc'], cssFile:'ieppc.css' }, // IE Mobile <6
		{ uaString:'iemobile', cssFile:'iemobile.css' }, // IE Mobile 6+
		{ uaString:'webos', cssFile:'webos.css' }, // Palm WebOS
		{ uaString:'Android', cssFile:'android.css' }, // Android
		{ uaString:['BlackBerry','/6.0','mobi'], cssFile:'blackberry6.css' },	// Blackberry 6
		{ uaString:['BlackBerry','/7.0','mobi'], cssFile:'blackberry7.css' }
	],
	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) {
		var head = document.getElementsByTagName('head')[0], fragment;
		var cssText = '<link rel="stylesheet" href="' + this.options.cssPath + platform.cssFile + '" type="text/css"/>';
		var miscText = platform.miscHead;
		if(platform.cssFile) {
			if(document.body) {
				fragment = document.createElement('div');
				fragment.innerHTML = cssText;
				head.appendChild(fragment.childNodes[0]);
			} else {
				document.write(cssText);
			}
		}
		if(platform.miscHead) {
			if(document.body) {
				fragment = document.createElement('div');
				fragment.innerHTML = miscText;
				head.appendChild(fragment.childNodes[0]);
			} else {
				document.write(miscText);
			}
		}
	},
	uaMatch:function(str) {
		if(!this.ua) {
			this.ua = navigator.userAgent.toLowerCase();
		}
		return this.ua.indexOf(str.toLowerCase()) != -1;
	}
}.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";
	}
}
