jQuery(document).ready(function(){
	initCufon();
	refreshCufon();
	ieHover('#nav li, .menu li', 'hover');
	$('#carousel').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '.carousel-holder > ul > li',
		switcher: '.menu>li',
		effect:true
	});
	initValidation();
	initClearFields();
	linkNewWindow();
	fNewPDFWindows();
});
function initClearFields(){
	var form = jQuery('form');
	form.each(function(){
		fields = jQuery(this).find('input:text,input:password,textarea')
		fields.each(function(){
			var _this = jQuery(this);
			var _title = '';
			if (_this.val() != '') {
				_title = _this.val();
				_this.attr('alt',_title)
			}
			_this.mouseenter(function(){
				if (jQuery(this).val() == _title) jQuery(this).val('');
			}).mouseleave(function(){
				if (jQuery(this).val() == '' && _title != '') jQuery(this).val(_title);
			});
		});
	});
}
function initValidation(){
	var _errorClass = 'error';
	var _regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	$('form.contact-form').each(function(){
		var _form = $(this);
		var _submit = _form.find('.btn');
		_submit.click(function(){
			_form.trigger('submit');
			return false;
		});
		function checkFields() {
			
			var _flag = false;
			_form.find('.'+_errorClass).removeClass(_errorClass);

			// fields validation
			_form.find('input.required-email').each(function(){
				if(!_regEmail.test($(this).val())) addError($(this));
			});
			_form.find('input.required, textarea.required').each(function(){
				if(!$(this).val().length || $(this).val() == $(this).attr('alt')) addError($(this));
			});

			// error class adding
			function addError(_obj) {
				_obj.addClass(_errorClass);
				_flag=true;
			}
			return _flag;
		}

		// catch form submit event
		_form.submit(function(){
			if(checkFields()) {
				return false;
			}
		});
	});
}
// cufon function
function initCufon() {
	Cufon.replace('.top-block a span, .header-info h2 span, .header-info p span, .carousel-holder ul .text p span, .menu .title, .heading .link1 span, #twocolumns h3 span', { fontFamily: 'FS Albert Bold' });
	Cufon.replace('.header-info .btn span, #nav li a, .heading .link2 span, .footer-area ul span, #content .btn .btn-holder span', { fontFamily: 'Tw Cen MT Bold' });
	Cufon.replace('#nav a', { fontFamily: 'Tw Cen MT Bold' });
	Cufon.replace('.carousel-holder ul .text h3 span', { textShadow: '#666 1px 2px', fontFamily: 'FS Albert Bold' });
}

// IE6 hover
function ieHover(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	};
}
// refresh cufon
function refreshCufon(){
	jQuery('#nav a').each(function() {
		jQuery(this).hover(function() {
			Cufon.replace(this, { fontFamily: 'Tw Cen MT Bold', color: '#131f48' });
		},function() {
			Cufon.replace(this, { fontFamily: 'Tw Cen MT Bold', color: '#59627f' });
		});
	});
	jQuery('#nav .active a').each(function() {
		jQuery(this).hover(function() {
			Cufon.replace(this, { fontFamily: 'Tw Cen MT Bold', color: '#131f48' });
		},function() {
			Cufon.replace(this, { fontFamily: 'Tw Cen MT Bold', color: '#131f48'});
		});
	});
	jQuery('.menu .title').each(function() {
		jQuery(this).hover(function() {
			Cufon.replace(this, { fontFamily: 'FS Albert Bold', color: '#cc1111' });
		},function() {
			Cufon.replace(this, { fontFamily: 'FS Albert Bold', color: '#131f48' });
		});
	});
	jQuery('.menu .active .title').each(function() {
		jQuery(this).hover(function() {
			Cufon.replace(this, { fontFamily: 'FS Albert Bold', color: '#cc1111' });
		},function() {
			Cufon.replace(this, { fontFamily: 'FS Albert Bold', color: '#cc1111'});
		});
	});
}
(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 700,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				direction: false,
				event: 'click',
				IE: false
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			if (this.options.switcher) this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.list.css({marginLeft: -(this.woh * this.active)});
				else this.list.css({marginTop: -(this.woh * this.active)});
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			}
			
			if (this.options.disableBtn) {
				if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
				if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			}
			
			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			if (this.options.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.floor(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(){
			if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this));
				if ($this.active == 0) {
					$this.prevBtn.addClass($this.options.disableBtn);
					$this.nextBtn.removeClass($this.options.disableBtn);
				}
				else if ($this.active == $this.count) {
					$this.nextBtn.addClass($this.options.disableBtn);
					$this.prevBtn.removeClass($this.options.disableBtn);
				}
				else {
					$this.prevBtn.removeClass($this.options.disableBtn);
					$this.nextBtn.removeClass($this.options.disableBtn);
				}
				if($this._t) clearTimeout($this._t);
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function($this, side){
			if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
			if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
			for (var i = 0; i < $this.options.slideElement; i++){
				if (side) {
					if ($this.active + 1 > $this.rew) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
					}
					else $this.active++;
				}
				else{
					if ($this.active - 1 < 0) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
					}
					else $this.active--;
				}
			};
			if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
			if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
			if (!$this.options.effect) $this.scrollElement();
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));
function linkNewWindow(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = obj.className + " external";
	};	
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};		
};
function fNewPDFWindows ()
{
if (!document.getElementsByTagName) return false;
var links = document.getElementsByTagName("a");
for (var eleLink=0; eleLink < links.length; eleLink ++) {
if (links[eleLink].href.indexOf('.pdf') !== -1) {
links[eleLink].onclick =
function() {
window.open(this.href,'resizable,scrollbars');
return false;
}
var img = document.createElement("img");
img.setAttribute("src", "/images/pdf.gif");
img.setAttribute("align", "left");
links[eleLink].appendChild(img);
}
}
}
