/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        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();
        }
        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 {
        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]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
function initGallery(){
	var _holder = $('.gallery ul');
	
	_holder.each(function(){
		var _els = $(this).find('li');
		var __hovered = false;
		var _fadeDur = 500; // animation speed
		var _autoSlide = 5000; // autoslide
		
		var _currEl = _els.index(_els.filter('.active'));
		var _nextEl = _currEl;
		_els.filter('.active').removeClass('active').css('z-index',2)
		
		function autoRotate(){
			_currEl = _nextEl;
			if(_nextEl<_els.length-1) _nextEl++
			else _nextEl = 0;
			
			_els.eq(_currEl).css('z-index',3);
			_els.eq(_nextEl).css('z-index',2);
			
			_els.eq(_currEl).animate({
				'opacity':0
			},_fadeDur,function(){
				_els.eq(_currEl).css({
					'z-index':1,
					'opacity':1
				});
			});
		}
		
		$(this).hover(function(){
			__hovered = true;
		},function(){
			__hovered = false;
		});
		
		setInterval(function(){
			if(!__hovered) autoRotate();
		},_autoSlide);
	});
}
function initFirstPopup(){
	var _alert = $('.nav .current');
	if($.cookie('first_load_popup')){
		_alert.hide();
	}else{
		$.cookie('first_load_popup',true);
	}
}
$(function(){
	initGallery();
	initFirstPopup();
});