(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();
			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');
				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.ceil(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._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 initGallery() {
	$('div.gallery-box').each(function(){
		var _gallery = $(this);
		var _navClass = 'switcher';
		var _listClass = 'big-img'
		var _xmlSource = _gallery.attr('title');
		_gallery.attr('title','');

		if(_xmlSource && _xmlSource.length>0) {
			$.ajax({
				url: _xmlSource,
				dataType: (jQuery.browser.msie) ? 'text' : 'xml',
				success: function(xmlData){
					var _galleryData;
					if ( typeof xmlData == 'string') {
						_galleryData = new ActiveXObject( 'Microsoft.XMLDOM');
						_galleryData.async = false;
						_galleryData.loadXML(xmlData);
					} else {
						_galleryData = xmlData;
					}

					// gallery structure
					var _navList = $('<ul class="'+_navClass+'"></ul>');
					var _list = $('<ul class="'+_listClass+'"></ul>');
					_gallery.empty().append(_list);
					_gallery.append(_navList);
					var _currentIndex = 0;

					// gallery create structure
					var _xmlItems = $('item',_galleryData);
					_xmlItems.each(function(_ind){
						var _menuItem = $('<li><a href="#"></a><div class="info-box"><p></p><a href="#"></a></div></li>');
						var _curItem = $(this);
						_menuItem.find('> a').attr('href', _curItem.find('link-menu').text()).html(_curItem.find('text-menu').text());
						_menuItem.find('> div > a').attr('href',_curItem.find('link-a').text()).html(_curItem.find('text-a').text());
						_menuItem.find('p').html(_curItem.find('text').text());
						loadSection(_ind);
						_navList.append(_menuItem);
						
					});

					// load section function
					function loadSection(_num) {
						var _curItem = _xmlItems.eq(_num);
						var _slideItem = $('<li><img /></li>');
						_slideItem.find('img').attr('src',_curItem.find('image-source').text()).attr('alt','');
						
						_list.append(_slideItem);
					}
					// gallery code
					_gallery.gallery({
						duration: 500,
						autoRotation: 5000,
						effect: 'fade',
						listOfSlides: '> ul.big-img > li',
						switcher: '> ul.switcher > li',
						event: 'mouseover'
					});
				},
				error: function() {
					alert('AJAX Error!');
				}
			});
		}
	});
}

function initCufon() {
	Cufon.replace('#content h2 span', { fontFamily: 'DTLNobelT-Bold'});
	Cufon.replace('.post-box h3 span', { fontFamily: 'DTLNobelT'});
	Cufon.replace('.box-orange h4 span', { fontFamily: 'DTLNobelT-Bold'});
	Cufon.replace('.box-form h3 span', { fontFamily: 'DTLNobelT'});
	Cufon.replace('.box-comments h3 span', { fontFamily: 'DTLNobelT'});
	Cufon.replace('#content-container h3 span', { fontFamily: 'DTLNobelT'});
	Cufon.replace('#content-container h4 span', { fontFamily: 'DTLNobelT'});
	Cufon.replace('#sidebar h2 span', { fontFamily: 'DTLNobelT-Bold'});
	Cufon.replace('.main-title h1 span', { fontFamily: 'DTLNobelT-Bold'});
}

function initStars(){
	var _activeClass = 'active';
	var _settedClass = 'setted';

	$('ul.rate').each(function(){
		var _holder = $(this);
		var _rates = _holder.children();
		var _activeIndex = _rates.index(_rates.filter('.'+_activeClass));
		var _settedIndex = -1;
		var _input = _holder.find('input:hidden.val');
		if(_activeIndex<0) _activeIndex = 0;
		_holder.removeAttr('title');

		_rates.each(function(ind){
			// hover behaviour
			var _item = $(this);
			_item.hover(function(){
				_rates.removeClass(_activeClass).removeClass(_settedClass);
			},function(){
				if(_settedIndex < 0) _rates.eq(_activeIndex).addClass(_activeClass);
				else _rates.eq(_settedIndex).addClass(_settedClass);
			});

			// click behaviour
			_item.click(function(){
				$.ajax({
					url:_url,
					type:'POST',
					data:'ajax=1&n='+_settedIndex,
					success:function(){
						//success code
					},
					error:function(){
						//ajax error
					}
				})
				_input.val((ind+1)/2);
				_settedIndex = ind;
				_rates.removeClass(_activeClass).eq(_settedIndex).addClass(_settedClass)
				//return false;
			})
		});
	});
}

$(document).ready(function(){
	initCufon();
	initGallery();
	initStars();
});
