$(function(){
	// init default settings
	$('#edit-search-block-form-1').val('Search our Site');
	$('div.view-latest-news').parents('.box-btm').find('.heading').addClass('heading4');
	
	$('ul.menu li:has(ul) > a').addClass('has-drop');
	$('ul.menu a').wrapInner('<span/>');
	$('#search-form').next('.box').removeAttr('class');
	
	// init function
	initCufon();
	if (!$('body').hasClass('admin') && !$('#node-form').length) {
		clearInputs();
		initGallery();
		initFancyBox();
	}
	
	$('img').each(function(){
		this.oncontextmenu = function () { return false; }
	})
	
	initTextVersion();
});

function initTextVersion(){
	var _body = $('body');
	var _links = $(document).find('link[rel="stylesheet"]');
	var _bodyClass = 'body-text';
	$('#footer a[href*="text.ver"]').click(function(){
		if (_body.hasClass(_bodyClass)) {
			_body.removeClass(_bodyClass);
			_links.attr('rel','stylesheet');
			$(this).text('Text Version');
		} else {
			_body.addClass(_bodyClass);
			_links.attr('rel','');
			$(this).text('HTML Version');
		}
		return false;
	});
}

function initFancyBox(){
	$('a[rel=fancybox]').fancybox();
}

function initGallery(){
	$('div.gallery').slideshow({
		onChange:function(){
			Cufon.replace('.swicher li a',{ fontFamily: 'helvetica', hover: true });
		}
	});
}

//create jQuery plugin
$.fn.slideshow = function(options){return new slideshow(this, options);}

//constructor
function slideshow(obj, options){this.init(obj,options)}

//prototype
slideshow.prototype = {
	init:function(obj, options) {
		this.options = $.extend({
			slides:'ul.slideset > li',
			nextBtn:'a.next',
			prevBtn:'a.prev',
			pagingHolder:'ul.swicher',
			pagingTag:'a',
			createPaging:false,
			autoPlay:true,
			dynamicLoad:false,
			imgAttr:'alt',
			effect:'slideY',//fade, slideX, slideY,
			startSlide:false,
			switchTime:5000,
			animSpeed:700,
			onChange:function(){}
		},options);
		
		this.mainHolder = $(obj);
		this.slides = $(this.options.slides,this.mainHolder);		
		this.nextBtn = $(this.options.nextBtn,this.mainHolder);
		this.prevBtn = $(this.options.prevBtn,this.mainHolder);
		this.dynamicLoad = this.options.dynamicLoad;
		this.imgAttr = this.options.imgAttr;
		this.animSpeed = this.options.animSpeed;
		this.switchTime = this.options.switchTime;
		this.effect = this.options.effect;
		this.autoPlay = this.options.autoPlay;
		this.previous = -1;
		this.loadingFrame = 1;
		this.busy = false;
		this.direction = 0;
		this.timer;
		this.pagingArray = new Array;
		this.loadArray = new Array;
		this.preloader = new Array;
		this.slidesParent = this.slides.eq(0).parent();
		this.slideW = this.slidesParent.width();
		this.slideH = this.slidesParent.height();
		
		(function(){
			if (this.options.startSlide) this.current = this.options.startSlide
			else {
				var active = -1;
				for(var i = 0; i< this.slides.length-1; i++) {
					if (this.slides.eq(i).hasClass('active')) {
						active = i;
						break;						
					}
				}
				if (active != -1) this.current = active;
				else this.current = 0;
			}
		}).apply(this);
		
		this.initPaging();
		this.setStyles();
		this.bindEvents();
		this.showSlide();
	},
	
	initPaging:function(){
		var obj = this;
		this.pagingHolder = $(this.options.pagingHolder);
		if (this.options.createPaging) {
			
			this.pagingHolder.each(function(i){
				var _this = $(this);
				_this.empty();
				var list = $('<ul>');
				for (var i = 0; i < obj.slides.length; i++) $('<li><a href="#">' + (i + 1) + '</a></li>').appendTo(list);
				_this.append(list);
			});
		}
		this.paging = $(this.options.pagingTag, this.pagingHolder);
		this.pagingArray.push(this.paging);
	},
	
	setStyles:function(){
		//loader
		if (this.dynamicLoad) {
			this.loader = $('<div class="loader">');
			this.loaderDiv = $('<div>').appendTo(this.loader)
			this.loader.append(this.loaderDiv).appendTo(this.slidesParent);
		}
		
		//slides
		if (this.effect == 'fade') {
			this.slides.css({display:'none'});
			this.slides.eq(this.current).css({display:'block'});
		} else if (this.effect == 'slideX'){
			this.slides.css({display: 'none',left:-this.slideW});
			this.slides.eq(this.current).css({display:'block',left:0});
		} else if (this.effect == 'slideY'){
			this.slides.css({display:'none',top:-this.slideH});
			this.slides.eq(this.current).css({display:'block',top:0});
		}
	},
	
	bindEvents:function(){
		var obj = this;
		this.nextBtn.bind('click',function(){
			if (!obj.busy) obj.nextSlide();
			return false;
		});
		
		this.prevBtn.bind('click',function(){
			if (!obj.busy) obj.prevSlide();
			return false;
		});
		
		for (var i = 0; i < this.pagingArray.length; i++) {
			this.pagingArray[i].each(function(i){
				$(this).bind('click',function(){
					if (i != obj.current && !obj.busy) {
						obj.previous = obj.current;
						obj.current = i;
						obj.direction = 1
						obj.showSlide();
					}
					return false;
				});
			});
		}
		
		if (this.dynamicLoad) this.loader.bind('click',function(){
			obj.abortLoading();
		});
	},
	
	nextSlide:function(){
		this.previous = this.current;
		if (this.current < this.slides.length-1) this.current++
		else this.current = 0;
		this.direction = -1;
		this.showSlide();
	},
	
	prevSlide:function(){
		this.previous = this.current;
		if (this.current > 0) this.current--
		else this.current = this.slides.length-1;
		this.direction = 1;
		this.showSlide();
	},
	
	showSlide:function(){
		var obj = this;
		
		if (this.previous == this.current) return; 
		
		var _current = this.current;
		this.busy = true;
		clearTimeout(this.timer);
		
		if (typeof this.loadArray[_current] != 'undefined' || !this.dynamicLoad) {
			//slide already loaded
			this.switchSlide();
		
		} else {
			//slide not loaded
			this.showLoading();
			var images = $(this.dynamicLoad,this.slides.eq(this.current));
			if (images.length) {
				var counter = 0;
				images.each(function(){
					var preloader = new Image;
					obj.preloader.push(preloader);
					var img = $(this);
					preloader.onload = function(){
						counter++;
						checkImages();
					}
					preloader.onerror = function(){
						//ignore errors
						counter++;
						checkImages();
					}
					preloader.src = img.attr(obj.imgAttr);
				});
				
				function checkImages(){
					if (counter == images.length) {
						images.each(function(){
							var img = $(this);
							img.attr('src',img.attr(obj.imgAttr));
						});
						successLoad();
					}
				}
				
			} else successLoad();
		}
		
		function successLoad(){
			obj.loadArray[_current] = 1;
			obj.hideLoading();
			obj.switchSlide();
		}
	},
	
	switchSlide:function(){
		var obj = this;
		if (this.previous != -1) {
			var nextSlide = this.slides.eq(this.current);
			var prevSlide = this.slides.eq(this.previous);
			
			if (this.effect == 'fade') {
				nextSlide.css({display:'block',opacity:0}).animate({opacity:1},this.animSpeed,function(){
					$(this).css({opacity:'auto'});
				});
				prevSlide.animate({opacity:0},this.animSpeed,callback);
			} else if (this.effect == 'slideX'){
				this.slideW = this.slidesParent.width();
				nextSlide.css({display:'block',left:this.slideW*this.direction}).animate({left:0},this.animSpeed);
				prevSlide.animate({left:-this.slideW*this.direction},this.animSpeed+10,callback);
			} else if (this.effect == 'slideY'){
				nextSlide.css({display:'block',top:this.slideH*this.direction}).animate({top:0},this.animSpeed);
				prevSlide.animate({top:-this.slideH*this.direction},this.animSpeed+10,callback);
			}
		} else {
			if (this.autoPlay) this.startAutoPlay();
			this.busy = false;
		}
		
		this.refreshStatus();
		
		function callback(){
			prevSlide.css({display:'none'});
			if (obj.autoPlay) obj.startAutoPlay();
			obj.busy = false;
		}
	},
	
	refreshStatus:function(){
		for (var i = 0; i < this.pagingArray.length;i++) {
			this.pagingArray[i].eq(this.previous).removeClass('active');
			this.pagingArray[i].eq(this.current).addClass('active');
		}
		this.slides.eq(this.previous).removeClass('active');
		this.slides.eq(this.current).addClass('active');
		this.slideW = this.slidesParent.width();
		if (typeof this.options.onChange === 'function') this.options.onChange();
	},
	
	showLoading:function(){
		var obj = this;
		this.loader.show();
		clearInterval(this.loadingTimer);
		obj.loadingTimer = setInterval(animateLoading, 66);
		
		function animateLoading(){
			obj.loaderDiv.css('top', obj.loadingFrame * -40);
			obj.loadingFrame = (obj.loadingFrame + 1) % 12;
		}
	},
	
	hideLoading:function(){
		this.loader.hide();
		clearInterval(this.loadingTimer);
	},
	
	abortLoading:function(){
		this.busy = false;
		this.hideLoading();
		this.current = this.previous;
		for (var i = 0; i < this.preloader.length; i++) {
			this.preloader[i].onload = null;
			this.preloader[i].onerror = null;
		}
		if (this.autoPlay) this.startAutoPlay();
	},
	
	startAutoPlay:function(){
		var obj = this;
		clearTimeout(obj.timer);
		obj.timer = setTimeout(function(){
			obj.nextSlide();
		},obj.switchTime);
	}
}

//clear inputs
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
}
	
function initCufon() {
	Cufon.replace('h1, #header .box p,.swicher li a, #main h2, #main h3, #content .help strong, .big-box2 strong, #footer strong, .nav-column h2, #content .btn, #main .column .newsletter, #main .column2 .newsletter, #content legend', { fontFamily: 'helvetica', hover: true });
}

