$(init)
/* vars */
var page=false;
var IE=false;
/* fonts */
Cufon.set('fontFamily', 'HeliosExtThinC').replace("#content .header");
Cufon.set('fontFamily', 'HeliosC').replace(".catalog-index .places-and-producers-list");
/* */
(function($){
	$.fn.outlinefix = function(){
		return this.live('mousedown', function(e) {
			e.target.blur();
			e.target.hideFocus = true;
			e.target.style.outline = 'none'
		}).live('mouseout', function(e) {
			try{
				e.target.blur();
				e.target.hideFocus = false;
				if (e.target.style && e.target.style.outline) {
					e.target.style.outline = null;
				}
			} catch(e) {
				
			}
		});
	}
})(jQuery);

function init(){
	$('.producer-carousel,.news-carousel,.special-carousel').jcarousel();
	if (IE==6 && $('.special-carousel').length>0){
		DD_belatedPNG.fix('.r img');
		DD_belatedPNG.fix('.special-carousel .image img');
	}
	if (IE && page=='index'){	
		$('#wrapper').css({'overflow':'visible'});
	}
	if ($('.producer-carousel').length>0){
		var els=$('.producer-carousel').find('li');
		for (var i=0,length=els.length;i<length;i++){
			els.eq(i).data('counter',i+1);
		}
		$('.producer-carousel').find('a').hover(
			function(){
				$(this).css({'zIndex':'50'});
				var el=$(this).parent().parent();
				var popup=el.find('.information');
				if ($('.producer-carousel').data('jcarousel').last==el.data('counter')){
					popup.addClass('ppp2');
				} else {
					if (IE) {
						el.next().css({'visibility':'hidden'});
					}
				}
				if (false){
					popup.fadeIn(400);
				} else {
					popup.show();
				}
			},
			function(){
				$(this).css({'zIndex':'1'});
				var el=$(this).parent().parent().find('.information');
				if (false){
					el.fadeOut(100)
				} else {
					el.hide();
				}
				el.removeClass('ppp2');
				if (IE) {
					$(this).parent().parent().next().css({'visibility':'visible'});
				}
			}
		);
	}
	if ($('.scroll-pane').length>0){
		$('.one-news .scroll-pane').css((function(){
			var newcss={};
			var parent=$('.one-news');
			newcss.height=272-parent.find('.title').outerHeight()-parent.find('.date').outerHeight();
			return newcss;
		})());
		$('.one-vine-description .scroll-pane').css((function(){
			var newcss={};
			var parent=$('.one-vine-description');
			newcss.height=257-parent.find('h2').outerHeight()-parent.find('.stars').outerHeight();
			return newcss;
		})());
		$('.one-news  .scroll-pane').jScrollPane({
			showArrows:true,
			scrollbarOnLeft:true,
			scrollbarWidth:5,
			scrollbarMargin:20
		});
		$('.one-vine-description .scroll-pane').jScrollPane({
			showArrows:true,
			scrollbarOnLeft:true,
			scrollbarWidth:5,
			scrollbarMargin:10
		});
		$('.one-vine-description .scroll-pane').css({paddingLeft:'15px'});
		$('.one-vine-description .scroll-pane').css({visibility:'visible'});
		$('.scroll-one-vino-properties').css({visibility:'visible'});
	}
	$('.about').jScrollPane({
		showArrows:true,
		scrollbarOnLeft:true,
		scrollbarWidth:5,
		scrollbarMargin:20
	}).css({visibility:'visible'});
	$('.producer-scroll').jScrollPane({
		showArrows:true,
		scrollbarOnLeft:true,
		scrollbarWidth:5,
		scrollbarMargin:10
	}).css({visibility:'visible'});
	if ($('.vino-carousel').length>0) {
		var vinoViewer=new jsViewer();
		vinoViewer.init({
			$elements:$('.vino-carousel').find('li'),
			$container:$('.one-vino')
		});
	}
	$('#navigation a').not('.active').css({
		'marginLeft':'0px'
	}).hover(
		function(){
			var el=$(this);
			el.addClass('hover');
			el.animate({
				'marginLeft':'20px'
			},200).animate({
				'marginLeft':'10px'
			},200);
		},function(){
			var el=$(this);
			el.removeClass('hover');
			el.animate({
				'marginLeft':'0px'
			},100);
		}
	);
	if (window.screen.width>1280){
		/* default
		$('.wrapper_2').css({
			'left':'20%',
			'marginLeft':'-300px'
		});
		*/
		$('.wrapper_2').css({
			'left':'0%',
			'marginLeft':'0px'
		});
	}
	$('a').outlinefix();
	if (IE && page=='index'){	
		setTimeout(function(){
			$('#wrapper').css({'overflow':'hidden'});
		},500);
	}
}
function activateCatalogScroll(){
	if ($('.one-vino .scroll-one-vino-properties').length>0){
		$('.one-vino .scroll-one-vino-properties').css((function(){
			var newcss={};
			var parent=$('.one-vino .scroll-one-vino-properties').parent();
			newcss.height=325-parent.find('.produer-logo').outerHeight()-parent.find('h2').outerHeight();
			return newcss;
		})()).jScrollPane({
			showArrows:true,
			scrollbarOnLeft:true,
			scrollbarWidth:5,
			scrollbarMargin:10
		}).css({visibility:'visible'});
	}
}
function jsViewer(){
	var self=this;
}
jsViewer.prototype={
	init:function(pars){
		var self=this;
		this.$container=pars.$container;
		if (pars.$elements){
			self.$elements=pars.$elements;
			for (var i=0,length=self.$elements.length;i<length;i++){
				self.$elements.eq(i).data('counter',i);
			}
			function pageload(hash) {
				// alert("pageload: " + hash);
				// hash doesn't contain the first # character.
				if(hash){
					// restore ajax loaded state
					if($.browser.msie) {
						hash = encodeURIComponent(hash);
					}
					self.showById(hash);
				} else {
					var hash=$('.vino-carousel').find('li:first').find('a').attr('href').split('_')[1];
					document.location.hash=hash;
				}
				if (first){
					var el=self.$elements.filter('#wine_'+hash)
					var start=1;
					if (el.length>0){
						start=el.data('counter');
						if (start<5){
							start=1;
						}
					}
					jQuery('.vino-carousel').jcarousel({
						start: start
					}).css({'display':'block'});
				}
				first=false;
			}
			// Initialize history plugin.
			// The callback is called at once by present location.hash.
			var first=true;
			if (document.location.hash.length>0){
				$.historyInit(pageload, document.location.hash);
			} else {
				$.historyInit(pageload, false);
				pageload(false);
			}
			// set onlick event for buttons
			self.$elements.find('.st01 a').click(function(){
				var hash = this.href;
				hash = hash.split('_')[1];
				$.historyLoad(hash);
				return false;
			});
		}
		if (IE==6){
			DD_belatedPNG.fix('.jcarousel-skin-intendant .st01 .image img');
		}
	},
	show:function(jTpl){
		var self=this;
		self.$container.empty().append(jTpl);
		if (IE==6){
			DD_belatedPNG.fix('.one-vino .image img');
			DD_belatedPNG.fix('.one-vino .vstar');
		}
		var $this=$(this);
		activateCatalogScroll();
	},
	getTplById:function(id){
		var self=this;
		var answer=false;
		if (id){
			var el=self.$elements.filter('#wine_'+id).eq(0);
			if (el.length>0){
				el.parent().find('.active').removeClass('active');
				el.addClass('active');
				answer=el.find('.st02').clone();
			}
		}
		return answer;
	},
	showById:function(id){
		var self=this;
		if (id) {
			self.show(self.getTplById(id));
		}
	}
}