(function($)
{
    $.outerDiv = "$.vari";
    $.fn.vari = "$.fn.vari";
    var ww = 0;
    

    
    
    // $.fn is the object we add our custom functions to
//    $.fn.sideScroller = function(options){
sideScroller = {
		/*
    	containerWidth: = $(this).width();
    	var container = this;
    	var w = 0; //width of 1 li
    	var c = 0; //number of li's
    	var maxWidth = 0;
    	var currentPosition = 0;
    	*/
    	defaults: { 
		    left:      false,
		    right: false,
		    scrollCount:     1,
		    scrollFullPage: false,
		    speed: 200
		  },
		containerWidth:0,
		w: 0,
		c: 0,
		maxWidth: 0,
		currentPosition: 0,
		options: {},
		container: false,
		  
		getScrollCount: function(){
			  if (this.options.scrollFullPage == true){
				  return 5;
			  }else{
				  return this.options.scrollCount;
			  }
		},
		goLeft: function(){
			var newPosition = this.currentPosition + (this.w * this.getScrollCount());
			newPosition = (newPosition < 0) ? newPosition : 0;
			var scrollNum = (newPosition - this.currentPosition) / this.w;
			this.currentPosition = newPosition;
			$('> ul',this.container).animate({'left':this.currentPosition+'px'},this.options.speed * scrollNum);
		},
		goRight: function(){
			var newPosition = this.currentPosition - (this.w * this.getScrollCount());
			newPosition = (Math.abs(newPosition) < (this.maxWidth - this.containerWidth)) ? newPosition : ((this.maxWidth - this.containerWidth) * -1);
			var scrollNum = (this.currentPosition - newPosition) / this.w;
			this.currentPosition = newPosition;
			$('> ul',this.container).animate({'left':this.currentPosition+'px'},this.options.speed * scrollNum);
		},
    	
		setPosition: function(itemId){
			this.currentPosition = this.w * itemId * -1;
			$('> ul',this.container).css({'left':this.currentPosition+'px'});
		},
		
    	_init: function(element, options){	
		 // Overwrite default options 
		  // with user provided ones 
		  // and merge them into "options". 
		  this.options = $.extend({}, this.defaults, options);	
		  this.container = element;
		  this.containerWidth = $(element).width();
		  
		  var instance = this;
		  
	    	return $(element).each(function(){
	        	
		    	if (options.left){
					$(options.left).click(function(e){
						e.preventDefault();
						instance.goLeft();
					});
				}
				if (options.right){
					$(options.right).click(function(e){
						e.preventDefault();
						instance.goRight();
					});
				}
				
			
			
				$(this).css('overflow', 'hidden');
	    		instance.w = $('> ul > li', this).width();
	    		//var c = $('ul li', this).length;
	    		instance.c = $('> ul > li',this).length;
	    		instance.maxWidth = instance.w * instance.c;
	    		
	    		$('> ul', this).css('position','absolute').css('left','0px');
	    		$('> ul > li', this).css('position','absolute').each(function(i){
	    			$(this).css('left',(i*instance.w)+'px');
	    		});
	    	    		
	    	});
		}
    };

$.sideScroller = function(element, options){
	this._init(element, options);
}
$.sideScroller.prototype = sideScroller;


$.fn.sideScroller = function(options){
	if ($(this).length == 0) return false;
	var name = 'sideScroller';
	var isMethodCall = (typeof options == 'string'),
		args = Array.prototype.slice.call(arguments, 1);
	
	// prevent calls to internal methods
	if (isMethodCall && options.substring(0, 1) == '_') {
		return this;
	}
	
	// handle initialization and non-getter methods
	return this.each(function() {
		var instance = $(this).data(name);

		
		
		// constructor
		(!instance && !isMethodCall &&
			$(this).data(name, new $.sideScroller(this, options)));

		// method call
		
		(instance && isMethodCall && $.isFunction(instance[options]) &&
			instance[options].apply(instance, args));
	});
	
};


})(jQuery);

$(document).ready(function(){
	if($('#homepage_background span').length){
		
		$('#homepage_background span:first').addClass('active').show();
		//$('#homepage_background span').not('.active').css({opacity: 0.0});
		//$('#homepage_background span').show();
		
		var fmRotate = function (){
				//$el = $('.fm image.active').next();
				//$('.fm img.active').removeClass('active');
				//$el.addClass('active');
				//do transition
				var $active = $('#homepage_background span.active');
				
				var $next = $('#homepage_background span.active').next();
				if (!$next.length){
					$next = $('#homepage_background span:first');
				}
				$('img, a', $active).animate({
					opacity: 0.0
				}, 1200, function(){
					//$active.removeClass('active last-active');
					$active.hide().removeClass('active');
				});
					$next.show().addClass('active').find('img, a').css({
						opacity: 0.0
					}).animate({
						opacity: 1.0}, 4000);
					
				
			return false;
		};
		
		var ssInt = null;
		var ssTo = null;
		
		fmStopShow = function(){
			clearInterval(ssInt);
			clearTimeout(ssTo);
			ssTo = null;
			ssInt = null;
		}
		fmStartShow = function(l){
			fmStopShow();//clear timers
			if (l == 0) {
				fmSlideShow();
				ssInt = setInterval('fmSlideShow()', 3000);
			}else{
				ssTo = setTimeout('fmStartShow(0)',l);
			}
		}
		fmSlideShow = function(){
			fmRotate();
		}
		
		fmStartShow(3000);
		
	}

	
	$('.productslist, #pageproducts').after('<a href="#" id="goleft"><b>left</b></a><a href="#" id="goright"><b>right</b></a>')
	$('.productslist').sideScroller({left:'#goleft',right:'#goright',scrollFullPage: true});
	$('#pageproducts').sideScroller({left:'#goleft',right:'#goright',scrollFullPage: true});
	$('#pageproducts > ul > li:last-child > a').css('border-right','0');
	
	
	/*
	var productsPop = null;
	
	var showProductsPop = function(itemId){
		if (productsPop === null){
			$('#page_content').append('<div id="products_pop"><a href="#" id="popgoleft"><b>left</b></a><a href="#" id="popgoright"><b>right</b></a><a href="#" id="popexit"><b>exit</b></a><div id="products_popin"></div></div>');
			var url = window.location.href
			if (url.indexOf('#') > 0){
				url = url.substring(0, url.indexOf('#'));
			}
			$('#products_popin').load('/ajax.php?page='+url+'&action=display_page_products_full', function() {
				  productsPop = true;
				  $('#products_popin').sideScroller({left:'#popgoleft',right:'#popgoright'}).sideScroller('setPosition',itemId);
			});
			$('#popexit').click(function(e){
				e.preventDefault();
				//$('#products_pop').hide();
				history.go(-1);
			});
		}else{
			$('#products_popin').sideScroller('setPosition',itemId);
			$('#products_pop').show();
			
		}
		
	}
	
	
	$('#pageproducts ul li a, .productslist ul li a').click(function(e){
		//alert($('#pageproducts ul li, .productslist ul li').index($(this).parent()));
		e.preventDefault();
		var itemId = $(this).parent().prevAll().length
		showProductsPop(itemId);
		$.historyLoad('pop');
	});
	*/
	
	
	var productsPop = null;
	var nextItemId = false;
	var showProductsPop = function(itemId){
		nextItemId = itemId
		if (productsPop === null){
			productsPop = true;
			$('#page_content').append('<div id="products_pop"><a href="#" id="popgoleft"><b>left</b></a><a href="#" id="popgoright"><b>right</b></a><a href="#" id="popexit"><b>exit</b></a><div id="products_popin"></div></div>');
			$('#products_pop').css('visibility','hidden').css('zIndex','-10');
			var url = window.location.href
			if (url.indexOf('#') > 0){
				url = url.substring(0, url.indexOf('#'));
			}
			
			$('#products_popin').load('/ajax.php?page='+url+'&action=display_page_products_full', function() {
				  $('#products_popin').sideScroller({left:'#popgoleft',right:'#popgoright'}).sideScroller('setPosition',0);
				  if (nextItemId !== false){
					  $('#products_popin').sideScroller('setPosition',nextItemId);
					  $('#products_pop').css('visibility','visible').css('zIndex','100').show();
				  }else{
					 $('#products_pop').hide().css('visibility','visible').css('zIndex','100');
				  }
				  
			});
			$('#popexit').click(function(e){
				//alert("OK");
				e.preventDefault();
				$('#products_pop').hide();
				history.go(-1);
			});

		}else if (nextItemId !== false){
			$('#products_popin').sideScroller('setPosition',itemId);
			$('#products_pop').show();
			
		}
		
	}
	
	
	$('#pageproducts ul li a, .productslist ul li a').click(function(e){
		//alert($('#pageproducts ul li, .productslist ul li').index($(this).parent()));
		e.preventDefault();
		var itemId = $(this).parent().prevAll().length
		showProductsPop(itemId);
		$.historyLoad('pop');
	});
	
	
	
	
	var pageloaded = false;	
	var pageload = function (hash) {
		if(hash) {
			if (!pageloaded){
			}
		}else{
			$('#products_pop').hide();
		}
		pageloaded = true;
	}
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
	
	var wh = $(window).height();   // returns height of browser viewport
	//var dh = $(document).height(); // returns height of HTML document
	var dh = $('body > div').height();
	if (wh > dh){
		$('#bottom_wrap1').height($('#bottom_wrap1').height() + wh - dh - 10);
	}
	
	$('#top_nav ul ul').each(function(){
		var page_right = $('#page_wrap').offset().left + $('#page_wrap').width();
		var ul_right = $(this).offset().left + $(this).width();
		if (ul_right > page_right){
			$(this).css('margin-left', (15 - (ul_right - page_right)) + 'px');
		}
	});
	$('#top_nav ul > li:has(ul)').mouseenter(function(){
		var page_right = $('#page_wrap').offset().left + $('#page_wrap').width();
		var ul_right = $(this).find('ul').offset().left + $(this).find('ul').width();
		if (ul_right > page_right){
			$(this).find('ul').css('margin-left', (15 - (ul_right - page_right)) + 'px');
		}
		$(this).unbind('mouseenter');
	});
	if ($('#pageproducts ul li a, .productslist ul li a').length){
		showProductsPop(false);
	}
});
