/* 
* jQuery slideShow v1.3.0
*/

jQuery.fn.slideShow = function(_options){    
	// defaults options	    
	var _options = jQuery.extend({
	    slideEl:'div.slide',		
		linkNext:'a.next',		
		linkPrev:'a.prev',		
		linkPause:'a.pause',		
		numElement:'div.slideNav li a',		
		duration:800,		
		autoSlideShow:false,		
		switchTime:4000,
		noCicle:false,
		disableClass:'no-active',
		event:'click',		
		currentEl:'span.cur',		
		allEl:'span.all',
		hideBeforeShow: false,
		hoverStopGallery:true
	},_options);
    return this.each(function(){
	    var _THIS = jQuery(this),
			_slideEl = jQuery(_options.slideEl, _THIS),
			_linkNext = jQuery(_options.linkNext, _THIS),
			_linkPrev = jQuery(_options.linkPrev, _THIS),
			_linkPause = jQuery(_options.linkPause, _THIS),
			_numElement = jQuery(_options.numElement, _THIS),
			_currentEl = jQuery(_options.currentEl, _THIS),
			_allEl = jQuery(_options.allEl, _THIS),
			_duration = _options.duration,
			_switchTime = _options.switchTime,
			_numElActive, _timer = false, _hover = false, _current = 0, _next = 0, _pause = true;
		
		if (!_slideEl.filter('.active').length) {
		    var rndStart = Math.floor(Math.random() * _slideEl.length);
		    _slideEl.eq(rndStart).addClass('active');
		    _current = rndStart;
		} else {
			_current = _slideEl.index(_slideEl.filter('.active'));
		}
		_slideEl.not(".active").hide();
		
		if (jQuery(_numElement).length && _options.numElement) activeNumEl();
		if (_options.autoSlideShow) {
			_pause = false;
			_timer = setTimeout(function(){nextEl()},_switchTime);
			if (_linkPause.length && _options.linkPause) _linkPause.addClass('play')
			if (_options.hoverStopGallery) contentHover();
		}
		if (_options.currentEl && _currentEl.length) {
			_allEl.html(_slideEl.length);
			currentNum();
		}
		if (_linkNext.length && _options.linkNext) {
			_linkNext.click(function(){
				nextEl();
				return false;
			});
		}
		if (_options.noCicle) {
			_linkPrev.addClass('prev-'+_options.disableClass);
		}
		if (_linkPrev.length && _options.linkPrev) {	
			_linkPrev.click(function(){
				_linkNext.removeClass('next-'+_options.disableClass);
				if (!_slideEl.is(':animated') && !jQuery(this).hasClass('prev-'+_options.disableClass)) {
					if (_timer) clearTimeout(_timer);
					_next = _current-1;
					if (_next < 0) _next = _slideEl.length-1;
					if (_options.noCicle && _next-1 < 0) {
						jQuery(this).addClass('prev-'+_options.disableClass);
					}
					fadeElement();
					activeNumEl();
					pauseCode();
				}
				return false;
			});
		}
		if (_numElement.length && _options.numElement) {
			_numElement.bind(_options.event, function(){
				if (!_slideEl.is(':animated')) {
					_next = _numElement.index(jQuery(this));
					_linkNext.removeClass('next-'+_options.disableClass);
					_linkPrev.removeClass('prev-'+_options.disableClass);
					if (_options.noCicle && _next+1 >= _slideEl.length) _linkNext.addClass('next-'+_options.disableClass);
					if (_options.noCicle && _next-1 < 0) _linkPrev.addClass('prev-'+_options.disableClass);
					if (_timer) clearTimeout(_timer);
					if (!_slideEl.eq(_next).hasClass("active")){
						fadeElement();
						activeNumEl();
						pauseCode();
					}
				}
				return false;
			});
		}
		if (_linkPause.length && _options.linkPause) {
			_linkPause.click(function(){
				if (!_pause) {
					jQuery(this).removeClass('play');
					clearTimeout(_timer);
					_pause = true;
				} else {
					jQuery(this).addClass('play');
					_timer = setTimeout(function(){nextEl()},_switchTime);
					_pause = false;
				}
				return false;
			});
		}
		function fadeElement(){
			if (_current != _next) {
				_slideEl.removeClass('active');
				_slideEl.eq(_current).addClass('hide');
				
				if (!_options.hideBeforeShow) {
					_slideEl.eq(_next).fadeIn(_duration, function(){
						_slideEl.filter('.hide').hide().removeClass('hide');
					}).addClass('active');
				} else {
					_slideEl.filter('.hide').fadeOut(_duration, function(){
						_slideEl.eq(_next).fadeIn(_duration);
					}).removeClass('hide');
				}
				_current = _next;
				if (_options.currentEl && _currentEl.length) currentNum();
			}
		};
		function currentNum() {
			_currentEl.html(_current+1);
		}
		function activeNumEl() {
			_numElement.parent().removeClass("active");
			_numElement.eq(_current).parent().addClass("active");
		};
		function nextEl(){
			_linkPrev.removeClass('prev-'+_options.disableClass);
			if (!_slideEl.is(':animated') && !_linkNext.hasClass('next-'+_options.disableClass)) {
			    if (_timer) clearTimeout(_timer);
			    var rndNum = Math.floor(Math.random() * _slideEl.length);
				_next = rndNum;
				if (_next == _slideEl.length) _next = 0;
				if (_options.noCicle && _next+1 >= _slideEl.length) {
					_linkNext.addClass('next-'+_options.disableClass);
				}
				fadeElement();
				activeNumEl();
				pauseCode();
			}
		};
		function pauseCode(){
			if (!_pause) {
				if (_linkPause.length && _options.linkPause) {
					if (_linkPause.hasClass('play')) {
						_timer = setTimeout(function(){nextEl()},_switchTime);
					}					
				} else {
					_timer = setTimeout(function(){nextEl()},_switchTime);
				}
			}
		}
		function contentHover() {
			_hover = true;
			_slideEl.mouseenter(function() {
				if (_timer) clearTimeout(_timer);
			}).mouseleave(function(){
				pauseCode();
			});			
		}
    });
}

$(document).ready(function(){
	$('ul.product-list div.window').slideShow({		
		slideEl:'div.slide',		
		linkNext:'a.next',		
		linkPrev:'a.prev',		
		linkPause:'a.pause',		
		numElement:'div.slideNav li a',		
		duration:350,		
		autoSlideShow:true,		
		switchTime:5000,
		noCicle:false,
		disableClass:'no-active',
		event:'click',		
		currentEl:'span.cur',		
		allEl:'span.all',
		hideBeforeShow: true,
		hoverStopGallery:true
	});
	$('div.window-frame').slideShow({		
		slideEl:'p',		
		linkNext:'a.link',		
		linkPrev:'a.prev',		
		linkPause:'a.pause',		
		numElement:'div.slideNav li a',		
		duration:350,		
		autoSlideShow:true,		
		switchTime:5000,
		noCicle:false,
		disableClass:'no-active',
		event:'click',		
		currentEl:'span.cur',		
		allEl:'span.all',
		hideBeforeShow: true,
		hoverStopGallery:true
	});
	
	// page reload
	var _cite = $('div.preview blockquote');
	_cite.hide();
	var _cookie = $.cookie('blockquote');
	if (_cookie) {
		var _num = parseInt(_cookie);
		_num++;
		if (_num > _cite.length-1) _num = 0;
		_cite.eq(_num).show();
		$.cookie('blockquote', _num, {expires: 30});
	} else {
		_cite.eq(0).show();
		$.cookie('blockquote', 0, {expires: 30});
	}
	
});

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



