/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
/*--- 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);
		});
	}
}
jQuery(document).ready(function(){
	clearInputs();
	ieHover('#nav li, .tabs-gallery .tabs li, .heading-line .drop li');
});

(function($) {
	jQuery.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 = jQuery.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 = jQuery(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.switcher = jQuery(context).find(this.options.switcher);
			this.nextBtn = jQuery(context).find(this.options.nextBtn);
			this.prevBtn = jQuery(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');
				this.switcher.removeClass('active').eq(this.active).addClass('active');
				var _this = this;
				setTimeout(function(){
					_this.list.parent().css({height:_this.list.eq(_this.active).outerHeight(true)})				}, 100);
			}
			
			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.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		fadeElement: function(){
			if (jQuery.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(){
					jQuery(this).css('opacity','auto');
				}});
				this.list.parent().animate({height:this.list.eq(this.active).outerHeight(true)}, {queue:false, duration: this.options.duration});
			}
			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(jQuery(this));
				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($) {
	jQuery.fn.simplebox = function(options) { 
		return new Simplebox(this, options); 
	};
	
	function Simplebox(context, options) { this.init(context, options); };
	
	Simplebox.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				duration: 300,
				linkClose: 'a.close, a.btn-close',
				divFader: 'fader',
				faderColor: 'black',
				opacity: 0.7,
				wrapper: '#wrapper',
				linkPopap: '.link-submit'
			}, options || {});
			this.btn = jQuery(context);
			this.select = jQuery(this.options.wrapper).find('select');
			this.initFader();
			this.btnEvent(this, this.btn);
		},
		btnEvent: function($this, el){
			el.click(function(){
				if (jQuery(this).attr('href')) $this.toPrepare(jQuery(this).attr('href'));
				else $this.toPrepare(jQuery(this).attr('title'));
				return false;
			});
		},
		calcWinWidth: function(){
			this.winWidth = jQuery('body').width();
			if (jQuery(this.options.wrapper).width() > this.winWidth) this.winWidth = jQuery(this.options.wrapper).width();
		},
		toPrepare: function(obj){
			this.popup = jQuery(obj);
			this.btnClose = this.popup.find(this.options.linkClose);
			this.submitBtn = this.popup.find(this.options.linkPopap);
			
			if (jQuery.browser.msie) this.select.css({visibility: 'hidden'});
			this.calcWinWidth();
			this.winHeight = jQuery(window).height();
			this.winScroll = jQuery(window).scrollTop();
			
			this.popupTop = this.winScroll + (this.winHeight/2) - this.popup.outerHeight(true)/2;
			if (this.popupTop < 0) this.popupTop = 0;
			this.faderHeight = jQuery(this.options.wrapper).outerHeight();
			if (jQuery(window).height() > this.faderHeight) this.faderHeight = jQuery(window).height();
			
			this.popup.css({
				top: this.popupTop,
				left: this.winWidth/2 - this.popup.outerWidth(true)/2
			}).hide();
			this.fader.css({
				width: this.winWidth,
				height: this.faderHeight
			});
			this.initAnimate(this);
			this.initCloseEvent(this, this.btnClose, true);
			this.initCloseEvent(this, this.submitBtn, false);
			this.initCloseEvent(this, this.fader, true);
		},
		initCloseEvent: function($this, el, flag){
			el.click(function(){
				$this.popup.fadeOut($this.options.duration, function(){
					$this.popup.css({left: '-9999px'}).show();
					if (jQuery.browser.msie) $this.select.css({visibility: 'visible'});
					$this.submitBtn.unbind('click');
					$this.fader.unbind('click');
					$this.btnClose.unbind('click');
					jQuery(window).unbind('resize');
					if (flag) $this.fader.fadeOut($this.options.duration);
					else {
						if ($this.submitBtn.attr('href')) $this.toPrepare($this.submitBtn.attr('href'));
						else $this.toPrepare($this.submitBtn.attr('title'));
					}
				});
				return false;
			});
		},
		initAnimate:function ($this){
			$this.fader.fadeIn($this.options.duration, function(){
				$this.popup.fadeIn($this.options.duration);
			});
			jQuery(window).resize(function(){
				$this.calcWinWidth();
				$this.popup.animate({
					left: $this.winWidth/2 - $this.popup.outerWidth(true)/2
				}, {queue:false, duration: $this.options.duration});
				$this.fader.css({width: $this.winWidth});
			});
		},
		initFader: function(){
			if (jQuery(this.options.divFader).length > 0) this.fader = jQuery(this.options.divFader);
			else{
				this.fader = jQuery('<div class="'+this.options.divFader+'"></div>');
				jQuery('body').append(this.fader);
				this.fader.css({
					position: 'absolute',
					zIndex: 999,
					left:0,
					top:0,
					background: this.options.faderColor,
					opacity: this.options.opacity
				}).hide();
			}
		}
	}
}(jQuery));

function initMoreInfo(){
	jQuery('div.news-box > div.item, div.our-people-box > div.item > div.col2').each(function(){		var hold = jQuery(this);		var link = hold.find('a.open-close');    // div-open		var hClose = hold.height();
		hold.css({height: 'auto'});
		var hOpen = hold.height();
		hold.css({height: hClose});
		
		link.click(function(){
			if (!hold.hasClass('div-open')){
				hold.addClass('div-open');
				hold.animate({height: hOpen}, {queue:false, duration: 300});
			}
			else{
				hold.removeClass('div-open');
				hold.animate({height: hClose}, {queue:false, duration: 300});
			}
			return false;
		});	});
}

jQuery(function() {
	jQuery('div.gallery-holder').each(function(){
		var _hold = jQuery(this);
		var _list = _hold.find('ul.gallery-content > li');
		var _nav = jQuery('<ul class="gallery-tools"></ul>');
		for(var i = 0; i < _list.length; i++){
			if(_list.eq(i).hasClass('active')) _nav.append('<li class="active"><a href="#">'+(i+1)+'</a></li>');
			else _nav.append('<li><a href="#">'+(i+1)+'</a></li>');
		}
		_hold.append(_nav);
	});
	jQuery('div.gallery-holder').gallery({
		duration: 500,
		autoRotation: 4000,
		listOfSlides: 'ul.gallery-content > li',
		switcher: 'ul.gallery-tools > li',
		effect: 'fade',
		IE:true
	});
	jQuery('div.tabs-gallery').gallery({
		duration: 500,
		autoRotation: 6000,
		listOfSlides: 'div.tabs-content > ul > li',
		switcher: 'ul.tabs > li',
		effect: 'fade'
	});
	jQuery('ul.news-list').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: ' > li',
		effect: 'fade'
	});
	jQuery('div.tab-gal').gallery({
		duration: 500,
		listOfSlides: 'div.tab-content-holder > div.tab-content',
		switcher: 'ul.tabset > li',
		effect: 'fade'
	});
	initMoreInfo();
	jQuery('.link-popup').each(function(){
		var form = jQuery(this).parents('form');
		var name = form.find('input#name');
		var email = form.find('input#email');
		var nameC = jQuery('input#name-copy');
		var emailC = jQuery('input#email-copy');
		var tN = name.val();
		var tE = email.val();
		jQuery(this).click(function(){
			if (name.val() != tN) nameC.val(name.val());
			if (email.val() != tE) emailC.val(email.val());
			return false;
		});
	});
	jQuery('.link-popup').simplebox();
	
	/*WDP amends*/
	jQuery("div.product").each(function(){			
			
		
		//does a gallery exist? If so, get first anchor and set that as href so when user clicks on initial thumb it goes to first item, not itself
		if(jQuery("div.imagegallery", this).size()>0)
		{//we have a gallery			
			var strRel = jQuery('a.gallerylink', this).attr('rel');	
			var strCorrectHref = jQuery("div.imagegallery span.gallery-icon a:first", this).attr('href');
			var strCorrectTitle = jQuery("div.imagegallery span.gallery-icon a:first", this).attr('title');	
			jQuery('a.gallerylink', this).attr('href',strCorrectHref).attr('title',strCorrectTitle);			
			//now remove that first item to prevent duplication - assuming it's not just one item
			if (jQuery("div.imagegallery span.gallery-icon", this).siblings("span.gallery-icon").size()!=-1)
			{
				jQuery("div.imagegallery span.gallery-icon a:first", this).remove();				
			}
			jQuery('a[rel="'+strRel+'"]').colorbox();
			strRel="";
			
		}
			
		
	});	
	
	
	//jQuery('.gallerylink').simplebox();
	inForm();
	//
});
function inForm(){
	var _popup = jQuery('#popup');
	if(_popup.length){
		var _box = _popup.find('div.deff');
		var succes_box = _popup.find('div.success');
		_popup.find('input:submit').click(function(){
			jQuery.ajax({
				type: 'POST',
				data: _popup.find('form').serialize(),
				url: _popup.find('form').attr('action'),
				success: function(){
					_box.hide();
					succes_box.show();
				}
			});
			return false;
		});
	}
}
