// accordion in sidebar *********************
function accordion(){
	var _holder = $('#navigation');
	var _d = 500;
	var _actIndex = -1;
	var _t =null;
	
	var _opener = _holder.find('li');
	var _links = _holder.find('a');
	_opener.find('ul').hide();

	var cookiesValue = getCookie("menuActive");
	if(cookiesValue != 'undefined' && cookiesValue != null && cookiesValue != -1) {
		_opener.eq(cookiesValue).addClass("active");
		if (_opener.eq(cookiesValue).parents('li:eq(0)').length) _opener.eq(cookiesValue).parents('li:eq(0)').addClass('active');
		else _opener.eq(cookiesValue).addClass('active');
	}

	_opener.filter('.active').find('>ul').show();
	_opener.filter('.picked').find('>ul').show();
	_links.click(function(){
		var _li = $(this).parents('li:eq(0)');
		if(_li.hasClass('active')){
			if (!_li.hasClass('picked')) {
				_li.removeClass('active');
				_li.find('> ul').slideUp(_d);
			}
		}else{
			_opener.each(function(){if(!$(this).hasClass('.picked')){
				$(this).removeClass('active');
				$(this).find('ul:eq(0)').slideUp(_d);
			}})
			_li.addClass('active');
			if (_li.find('> ul').is(':animated')) _li.find('> ul').stop(true,true);
			else _li.find('> ul').slideDown(_d);
		}
		if(_li.parent('#navigation').length) return false;
		else {
			var today = new Date();
			var exp = new Date(today.getTime() + (365*24*60*60*1000));
			_actIndex = _links.index($(this));
			//setCookie("menuActive", _actIndex, exp.toGMTString(), "/");
		};
	})
/* 	_opener.mouseenter(function(){
		var _li = $(this);
		if (_t) clearTimeout(_t);
		_t = setTimeout(function(){
			_li.addClass('active');
			if (_li.find('> ul').is(':animated')) _li.find('> ul').stop(true,true);
			else _li.find('> ul').slideDown(_d);
		},250)
	}).mouseleave(function(){
		var _li = $(this);
		if (!_li.hasClass('picked')) {
			_li.removeClass('active');
			_li.find('> ul').slideUp(_d);
		}
	}); */

	_links.click(function(){
		var today = new Date();
		var exp = new Date(today.getTime() + (365*24*60*60*1000));
		_actIndex = _links.index($(this));
		//setCookie("menuActive", _actIndex, exp.toGMTString(), "/");
	})
}
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
//* jquery tabs *********
jQuery.fn.jqueryTabs = function(_options){
	// defaults options
/*
		event: 'click','mouseover','mouseleave'
*/
	var _options = jQuery.extend({
		event: 'click',
		tabset: 'div.tabset'
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _tabset = jQuery(_options.tabset,_this);

		_tabset.each(function(){
			var _list = $(this);
			var _links = _list.find('a.tab', _list);

			_links.each(function() {
				var _link = $(this);
				var _href = _link.attr('href');
				var _tab = $(_href, _this);

				if(_link.hasClass('active')) _tab.show();
				else _tab.hide();

				_link.bind(_options.event, function(){
					_links.filter('.active').each(function(){
						$($(this).removeClass('active').attr('href')).hide();
					});
					_link.addClass('active');
					_tab.show();
					return false;
				});
			});
		});
	})
}

/*--- fade gall ---*/
function fadeGallery(){
	var _d = false;
	var _time = 1000;
	if ($.browser.msie && $.browser.version > 6) _time = 0;
	$('#slider').each(function(){
		var _this = $(this);
		var _list = $('.slider > li', _this);
		var _btnPrev = $('.back a',_this);
		var _btnNext = $('.next a', _this);
		var _a = _list.index(_list.filter('.active:eq(0)'));

		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		if ($.browser.msie) _list.eq(_a).css('opacity', 'auto');
		var _t;
		if (_d){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, _d);
		}
		if (_btnPrev){
			_btnPrev.click(function(){
				var _prevItem = 0;
				if (_a > 0) _prevItem = _a-1;
				else _prevItem = _list.length-1;
				changeEl(_prevItem);
				return false;
			})
		}
		if (_btnNext){
			_btnNext.click(function(){
				var _nextItem = 0;
				if (_a < _list.length - 1) _nextItem = _a+1;
				else _nextItem = 0;
				changeEl(_nextItem);
				return false;
			})
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').css({display:'block'}).animate({opacity: 0}, {queue:false, duration:_time});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:_time, complete: function(){
					$(this).css({opacity: 'auto'});
				}});
				_a = _ind;
			}
			if (_d){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
				}, _d);
			}
		}
	});
}

//*breadcrumbs zIndex**************************************************
function initNav(){
	$("#breadcrumbs li").each(function(_ind){
		$(this).css({zIndex: 999-_ind})
	});
}



//**** only for IE 6 *************************************************************************
function initIE6(){
	fixTrans();
	ieHover('.product-item li');
}
// input focus ********************
function initInputs(){
	if ($.browser.msie){
		$("input").each(function(){
			$(this).focus(function(){
				$(this).addClass('focus');
			}).blur(function(){
				$(this).removeClass('focus');
			})
		});
	}
}
//** IE6 hover on element ******************
function ieHover(h_list){
	if ($.browser.msie && $.browser.version < 7){
		$(h_list).live('mouseover', function(){
			$(this).addClass('hover');
		}).live('mouseout', function(){
			$(this).removeClass('hover');
		});
	}
}
// fix PNG **************************
var transparentImage = "images/transparent.gif";
function fixTrans()
{
	if (typeof document.body.style.maxHeight == 'undefined') {

	var imgs = document.getElementsByTagName("img");

	for (i = 0; i < imgs.length; i++)
	{
		if (imgs[i].src.indexOf(transparentImage) != -1)
		{
			return;
		}
		if (imgs[i].src.indexOf(".png") != -1)
			{
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "inline-block";
			}
		}
	}
}


// init all function ********************
$(function(){
	$('.tabset').jqueryTabs({
		tabset: 'ul#tabs'
	});
	initNav();
	fadeGallery();
	if ($.browser.msie && $.browser.version <7){
		initIE6();
	}
	initInputs();
	accordion();
})


