jQuery.noConflict();

jQuery(document).ready(function(){
	initDrop();
	initGallery();
	initTabs();
	jQuery(".tab-map").eq(0).ATMapPaging();
})



function initDrop(){
	var _activeClass = 'hover';
	var _body = jQuery('body');
	jQuery('.header-top ul:has(.drop)').each(function(){
		var _holder = jQuery(this);
		var _links = jQuery('>li:has(.drop)', _holder);

		_links.each(function(){
			var _link = jQuery(this);
			var _btn = jQuery('>a', _link);
			var _drop = jQuery('.drop', _link);
			var _btnClose = jQuery('.btn-close', _link);
			
			_btn.click(function(){
				if (_link.hasClass(_activeClass)){
					_link.removeClass(_activeClass);
				}
				else {
					_links.removeClass(_activeClass);
					_link.addClass(_activeClass);
				}
				return false;
			})
			_link.click(function(e){
				e.stopPropagation();
			})
			_body.bind('click', function(){
				_link.removeClass(_activeClass);
			})
			_btnClose.bind('click', function(){
				_link.removeClass(_activeClass);
			})
		})
	})
}

function initTabs(){
	var _activeClass = 'active';
	var _hiddenClass = 'hidden';
	jQuery('.tab-nav').each(function(){
		var _holder = jQuery(this);
		var _tabs = jQuery('.tabset:eq(1)>li>a', _holder);
		var _tabSwitch = jQuery('.tabset:eq(0)>li>a', _holder);
		var _target = jQuery('#content');
		var _active = _tabs.filter('.' + _activeClass).length ? _tabs.index(_tabs.filter('.' + _activeClass)) : 0;
		var _href;
		
		_tabs.eq(_active).addClass(_activeClass);

		if (_target.children().length) _target.children().eq(0).attr('rel', _active) 
			else {
				_href = _tabs.eq(_active).attr('href');
				jQuery.ajax({
					url: _href, 
					cache: false, 
					type: 'POST',
					data: 'ajax=1',
					success: function(msg){
						var _tab = jQuery(msg);
						_tab.appendTo(_target).attr('rel', _active);
						if (_tabSwitch.eq(0).hasClass(_activeClass)) {
							_target.find('.tab-map').addClass(_hiddenClass);
							_target.find('.tab-list').removeClass(_hiddenClass);
						}
						else if (_tabSwitch.eq(1).hasClass(_activeClass)) {
							_target.find('.tab-map').removeClass(_hiddenClass);
							_target.find('.tab-list').addClass(_hiddenClass);
						}
						else {
							_tabSwitch.eq(0).addClass(_activeClass);
							_target.find('.tab-map').addClass(_hiddenClass);
							_target.find('.tab-list').removeClass(_hiddenClass);
						}
						
						
						
					}, 
					error: function(){
						alert('AJAX error');
					}
				})
			}
		
		function tabLoad(_tabTo, _index){
			_href = _tabTo.attr('href');
			jQuery.ajax({
				url: _href, 
				cache: false, 
				type: 'POST',
				data: 'ajax=1',
				success: function(msg){
					var _tab = jQuery(msg);
					_target.find('.tab-content').hide();
					_tab.appendTo(_target).attr('rel', _index);
					if (_tabSwitch.eq(0).hasClass(_activeClass)) {
						_target.find('.tab-map').addClass(_hiddenClass);
						_target.find('.tab-list').removeClass(_hiddenClass);
					}
					else if (_tabSwitch.eq(1).hasClass(_activeClass)) {
						_target.find('.tab-map').removeClass(_hiddenClass);
						_target.find('.tab-list').addClass(_hiddenClass);
						
					}
					else {
						_tabSwitch.eq(0).addClass(_activeClass);
						_target.find('.tab-map').addClass(_hiddenClass);
						_target.find('.tab-list').removeClass(_hiddenClass);
					}
					
					_tab.find('.visual .gallery').gallery({
						effect: 'fade',
						IE: true
					});
					
					
					initGMap(_tab);
					
				}, 
				error: function(){
					alert('AJAX error');
				}
			})
		}
		
		_tabs.click(function(){
			var _link = jQuery(this);
			if (!_link.hasClass(_activeClass)){
				var _ind = _tabs.index(_link);
				if (_target.find('.tab-content[rel=' + _ind + ']').length) {
					_target.find('.tab-content[rel=' + _ind + ']').show();
					_target.find('.tab-content[rel=' + _active + ']').hide();
					_tabs.eq(_active).removeClass(_activeClass);
					_tabs.eq(_active).parent().removeClass(_activeClass);
					_tabs.eq(_ind).addClass(_activeClass);
					_active = _ind;
				}
				else {
					tabLoad(_link, _ind);
					_tabs.eq(_active).removeClass(_activeClass);
					_tabs.eq(_active).parent().removeClass(_activeClass);
					_tabs.eq(_ind).addClass(_activeClass);
					_active = _ind;
				}
			}
			return false;
		})
		
		_tabSwitch.click(function(){
			var _link = jQuery(this);
			var _ind = _tabSwitch.index(_link);
			if (!_link.hasClass(_activeClass)) {
				if (_ind == 0) {
					_target.find('.tab-map').addClass(_hiddenClass);
					_target.find('.tab-list').removeClass(_hiddenClass);
				}
				else if (_ind == 1) {
					_target.find('.tab-map').removeClass(_hiddenClass);
					_target.find('.tab-list').addClass(_hiddenClass);
					
					/* Init the google map */
					initializeGMap();
					
					jQuery(".map-place").each(function(){
					
						google.maps.event.trigger(jQuery(this)[0], 'resize');
					
					});
				}
				_tabSwitch.removeClass(_activeClass);
				_link.addClass(_activeClass);
			}
			return false;
		})
	})
}

function initGallery(){
	jQuery('.intro .gallery').gallery({
		effect: 'fade',
		IE: false,
		duration: 2000,
		autoRotation: 4000,
		circle: true,
		nextBtn: 'a.link-next, a.btn-next, a.next, a.next-link',
		prevBtn: 'a.link-prev, a.btn-prev, a.prev, a.prev-link',
	});
	
	
	
	jQuery('.visual .gallery').gallery({
		effect: 'fade',
		IE: true
	});
}

(function(jQuery) {
	jQuery.fn.gallery = function(options) { 
	
				jQuery(this).each(function(){
					new Gallery(jQuery(this), options);
				});							
							 
			};
	function Gallery(context, options) { this.init(context, options); };
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				infinite: false,								//true = infinite gallery
				duration: 700,									//duration of effect it 1000 = 1sec
				slideElement: 1,								//number of elements for a slide
				autoRotation: false,							//false = option is disabled; 1000 = 1sec
				effect: false,									//false = slide; true = fade
				listOfSlides: 'ul > li',						//elements galleries
				switcher: false,								//false = option is disabled; 'ul > li' = elements switcher
				disableBtn: false,								//false = option is disabled; 'hidden' = class adds an buttons "prev" and "next"
				nextBtn: 'a.link-next, a.btn-next, a.next, a.next-link',		//button "next"
				prevBtn: 'a.link-prev, a.btn-prev, a.prev, a.prev-link',		//button "prev"
				circle: true,									//true = cyclic gallery; false = not cyclic gallery
				direction: false,								//false = horizontal; true = vertical
				event: 'click',									//event for the buttons and switcher
				IE: false,										//forced off effect it "fade" in IE
				autoHeight: false,								//auto height on fade
				counterClass: 'span.counter-current'
			}, options || {});
			var _el = jQuery(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			if(this.options.switcher) 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'));
			this.counterActive = jQuery(context).closest("div.visual").find(this.options.counterClass);
			
			/* Only show arrows if more than one picture */
			if(this.count < 1){
				this.nextBtn.css('display', 'none');
				this.prevBtn.css('display', 'none');
			}
			
			
			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.anim = '{marginLeft: -(this.woh * this.active)}';
				else this.anim = '{marginTop: -(this.woh * this.active)}';
				eval('this.list.css('+this.anim+')');
			}
			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.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});
			}
			this.flag = true;
			if (this.options.infinite){
				this.count++;
				this.active += this.count;
				this.list.append(_el.clone());
				this.list.append(_el.clone());
				eval('this.list.css('+this.anim+')');
			}
			
			this.initEvent(this, this.nextBtn, true);
			this.initEvent(this, this.prevBtn, false);
			if (this.options.disableBtn) this.initDisableBtn();
			if (this.options.autoRotation) this.runTimer(this);
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		initDisableBtn: function(){
			this.prevBtn.removeClass('prev-'+this.options.disableBtn);
			this.nextBtn.removeClass('next-'+this.options.disableBtn);
			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
			if (this.active == 0 && this.count == 1 || this.count+1 <= this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
		},
		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');
				}});
			}
			
			
			if(this.options.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
			
			/* Update counter */
			if(this.counterActive.length > 0)
			{
				this.counterActive.html(this.active*1 + 1);
			}
		},
		scrollElement: function(jQuerythis){
			if (!jQuerythis.options.infinite) eval('jQuerythis.list.animate('+jQuerythis.anim+', {queue:false, duration: jQuerythis.options.duration});');
			else eval('jQuerythis.list.animate('+jQuerythis.anim+', jQuerythis.options.duration, function(){ jQuerythis.flag = true });');
			if (jQuerythis.options.switcher) jQuerythis.switcher.removeClass('active').eq(jQuerythis.active / jQuerythis.options.slideElement).addClass('active');
		},
		runTimer: function(jQuerythis){
			if(jQuerythis._t) clearTimeout(jQuerythis._t);
			jQuerythis._t = setInterval(function(){
				if (jQuerythis.options.infinite) jQuerythis.flag = false;
				jQuerythis.toPrepare(jQuerythis, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function(jQuerythis, el){
			el.bind(jQuerythis.options.event, function(){
				jQuerythis.active = jQuerythis.switcher.index(jQuery(this)) * jQuerythis.options.slideElement;
				if(jQuerythis._t) clearTimeout(jQuerythis._t);
				if (jQuerythis.options.disableBtn) jQuerythis.initDisableBtn();
				if (!jQuerythis.options.effect) jQuerythis.scrollElement(jQuerythis);
				else jQuerythis.fadeElement();
				if (jQuerythis.options.autoRotation) jQuerythis.runTimer(jQuerythis);
				return false;
			});
		},
		initEvent: function(jQuerythis, addEventEl, dir){
			addEventEl.bind(jQuerythis.options.event, function(){
				if (jQuerythis.flag){
					if (jQuerythis.options.infinite) jQuerythis.flag = false;
					if(jQuerythis._t) clearTimeout(jQuerythis._t);
					jQuerythis.toPrepare(jQuerythis, dir);
					if (jQuerythis.options.autoRotation) jQuerythis.runTimer(jQuerythis);
				}
				return false;
			});
		},
		toPrepare: function(jQuerythis, side){
			if (!jQuerythis.options.infinite){
				if ((jQuerythis.active == jQuerythis.rew) && jQuerythis.options.circle && side) jQuerythis.active = -jQuerythis.options.slideElement;
				if ((jQuerythis.active == 0) && jQuerythis.options.circle && !side) jQuerythis.active = jQuerythis.rew + jQuerythis.options.slideElement;
				for (var i = 0; i < jQuerythis.options.slideElement; i++){
					if (side) { if (jQuerythis.active + 1 <= jQuerythis.rew) jQuerythis.active++; }
					else { if (jQuerythis.active - 1 >= 0) jQuerythis.active--; }
				};
			}
			else{
				if (jQuerythis.active >= jQuerythis.count + jQuerythis.count && side) jQuerythis.active -= jQuerythis.count;
				if (jQuerythis.active <= jQuerythis.count-1 && !side) jQuerythis.active += jQuerythis.count;
				eval('jQuerythis.list.css('+jQuerythis.anim+')');
				if (side) jQuerythis.active += jQuerythis.options.slideElement;
				else jQuerythis.active -= jQuerythis.options.slideElement;
			}
			if (this.options.disableBtn) this.initDisableBtn();
			if (!jQuerythis.options.effect) jQuerythis.scrollElement(jQuerythis);
			else jQuerythis.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 initGMap(tab){

			var jCanvas = tab.find(".map-place");
			var canvas = jCanvas[0];
			
			
 			var stylez = [
					  	 	{
						    	featureType: "all",
								elementType: "all",
								stylers: [
							  		{ lightness: 30 },
							  		{ saturation: -100 }
								]
					  	 	}
						  ];
					  	
					  	
					  	
		    var latlng = new google.maps.LatLng(23.502646731893982, -38.65992307662964);
		    var myOptions = {
		      zoom: 2,
		      center: latlng,
		      mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'bw'],
		      mapTypeControl: false,
		      panControl: false
		      
		    };
			
		    var map = new google.maps.Map(canvas, myOptions);
		    
		    
		    
		    var styledMapOptions = {
			      name: "Black-white"
			  }
		    
		    
		    var adamMapType = new google.maps.StyledMapType(
			      stylez, styledMapOptions);
			
			  map.mapTypes.set('bw', adamMapType);
			  map.setMapTypeId('bw');
			 
			var markers = jQuery.data(canvas, "markers");
			
			var markerImage = new google.maps.MarkerImage(
			    'wp-content/themes/adamtensta/images/at_map_mrk.png'
			  );
			
			
			
			for(var i in markers)
			{
			  	
				// Creating a marker and positioning it on the map  
				
				var marker = new google.maps.Marker({  
				  position: new google.maps.LatLng(markers[i].lat, markers[i].long),  
				  map: map,
				  icon: markerImage,
				  title: markers[i].title  
				});
				
				google.maps.event.addListener(marker, 'click', function() {
					window.location.href = markers[i].link;   
				});
				
			};
}




/*
 * jQuery plugin for handling of paging under the map
 * 
 */
(function(jQuery){
	jQuery.fn.ATMapPaging = function(options){
    
        var defaults = {
			postsPerPage	: 10,
			currentOffset	: 0
        };
        
        var options = jQuery.extend(defaults, options);
        
		
        return this.each(function(){
			
			var obj = jQuery(this);
			
			var prevButton	= obj.find(".navigation .prev span");
			var nextButton	= obj.find(".navigation .next span");
			
			var thisOffset = options.currentOffset;
			jQuery.data(obj[0],"thisOffset", thisOffset);
			
			/* Listen to older entries click */
			prevButton.click(function(){
				showOlder(obj);
				showHidePrevButton(obj);
				showHideNextButton(obj);
			});
			
			
			
			/* Listen to newer entries click */
			nextButton.click(function(){
				showNewer(obj);
				showHideNextButton(obj);
				showHidePrevButton(obj);
			});
			
			showOlder = function(obj){
				var offset = jQuery.data(obj[0],"thisOffset");
				var allPosts	= obj.find(".post.collapsed");
	    		allPosts.slice(offset, offset+options.postsPerPage-1 ).css("display", "none");
	    		offset += options.postsPerPage;
	    		allPosts.slice(offset, offset+options.postsPerPage-1 ).css("display", "block");
	    		
	    		jQuery.data(obj[0],"thisOffset", offset);
	    	}
	    	
	    	showNewer = function(obj){
	    		var offset = jQuery.data(obj[0],"thisOffset");
	    		var allPosts	= obj.find(".post.collapsed");
	    		allPosts.slice(offset, offset+options.postsPerPage-1 ).css("display", "none");
	    		offset -= options.postsPerPage;
	    		thisOffset = offset;
	    		allPosts.slice(offset, offset+options.postsPerPage-1 ).css("display", "block");
	    		
	    		jQuery.data(obj[0],"thisOffset", offset);
	    	}
	    	
	    	
	    	showHidePrevButton = function(obj){
	    		var offset = jQuery.data(obj[0],"thisOffset");
	    		var endPoint = offset + options.postsPerPage;
	    		var allPosts = obj.find(".post.collapsed");
	    		if( endPoint >= allPosts.length){
	    			prevButton.css("display", "none");
	    		}
	    		else
	    		{
	    			prevButton.css("display", "block");
	    		}
	    	}
	    	
	    	
	    	showHideNextButton = function(obj){
	    		var offset = jQuery.data(obj[0],"thisOffset");
	    		var allPosts	= obj.find(".post.collapsed");
	    		if(offset == 0){
	    			nextButton.css("display", "none");
	    		}
	    		else
	    		{
	    			nextButton.css("display", "block");
	    		}
	    	}
	    	
	    	
	    	/* Show inital 10 posts */
	    	var allPosts = obj.find(".post.collapsed");
			allPosts.slice(0, options.postsPerPage-1 ).css("display", "block");
			showHideNextButton(obj);
			showHidePrevButton(obj);
	
        });
        /* End of each */
        
        
        
    };
    /* End of wrapper function */

})(jQuery);



/*
 * Paging under a category on the front page 
 */
jQuery(".ajax-navigation a").live('click', function(){
	var _activeClass = 'active';
	var _hiddenClass = 'hidden';
	var _holder = jQuery('.tab-nav');
	var _tabs = jQuery('.tabset:eq(1)>li>a', _holder);
	var _tabSwitch = jQuery('.tabset:eq(0)>li>a', _holder);
	var _target = jQuery('#content');
	var _active = _tabs.filter('.' + _activeClass).length ? _tabs.index(_tabs.filter('.' + _activeClass)) : 0;
	
	_href = jQuery(this).attr('href');
	
	jQuery.ajax({
		url: _href, 
		cache: false, 
		type: 'POST',
		data: 'ajax=1',
		success: function(msg){
			var _tab = jQuery(msg);
			_target.find(".tab-content[rel='"+ _active +"']").remove();
			_tab.appendTo(_target).attr('rel', _active);
			initGallery();
		}, 
		error: function(){
			alert('AJAX error');
		}
	});
	
	return false;
});

