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;
    }
};

jQuery(document).ready(function(){
	var options = { path: '/', expires: 365 };
	$(".accordion").each(function (i) {
		var cookie_name = this.id+'_count';
		if($.cookie(cookie_name)){
			var count = $.cookie(cookie_name) * 1;
		} else {
			var count = $(this).find('h3').length - 1;
			$.cookie(cookie_name,count,options);
		}
    $(this).accordion({
			header: "h3",
			fillSpace: true,
			active: count,
			change: function(event, ui) { $.cookie(cookie_name,ui.newHeader.prevAll('h3').length,options); },
			icons: {
	  			header: "ui-icon-circle-arrow-e",
	 				headerSelected: "ui-icon-circle-arrow-s"
			}
		});
	});
	
	$('#blink_slides').cycle({ 
    fx:      	'fade',
		next:   	'#cycle_next',
		prev: 		'#cycle_prev',
    speed:  	1000,
    timeout: 	5000
	});
	
	if($.cookie('hp_slideshow_is_paused')){
    $('#blink_slides').cycle('pause');
		$('#cycle_pause').attr("src","http://blink.ucsd.edu/_images/homepage/cycle_pause_orange.gif");
	  $('#cycle_pause').toggle(function () {resume_show();},function () {pause_show();});
	} else {
	  $('#cycle_pause').toggle(function () {pause_show();},function () {resume_show();});
	}

	function pause_show() {
    $('#blink_slides').cycle('pause');
		$('#cycle_pause').attr("src","http://blink.ucsd.edu/_images/homepage/cycle_pause_orange.gif");
		$.cookie('hp_slideshow_is_paused','true',options);
	}
	
	function resume_show() {
    $('#blink_slides').cycle('resume');
		$('#cycle_pause').attr("src","http://blink.ucsd.edu/_images/homepage/cycle_pause.gif");
		$.cookie('hp_slideshow_is_paused',null);
	}


		$('#blink_headlines').cycle({ 
	    fx:      	'scrollBothWays',
			next:   	'#cycle_next_blue',
			prev: 		'#cycle_prev_blue',
	    speed:  	1000,
	    timeout: 	0,
			pause:   	1,
			easeIn:   'easeOutBack',
			easeOut:  'easeOutBack'
		});
});


$.fn.cycle.transitions.scrollBothWays = function($cont, $slides, opts) {
	$cont.css('overflow','hidden');
	opts.before.push($.fn.cycle.commonReset);

	// custom transition fn (trying to get it to scroll forward and backward)
	opts.fxFn = function(curr, next, opts, cb, fwd) {
		
		var w = $cont.width();
		opts.cssFirst = { left: 0 };
		opts.animIn	  = { left: 0 };
		
		if(fwd){
			opts.cssBefore= { left: w, top: 0 };
			opts.animOut  = { left: 0-w };
		}else{
			opts.cssBefore= { left: -w, top: 0 };
			opts.animOut  = { left: w };
		};
		
		var $l = $(curr), $n = $(next);
		var speedIn = opts.speedIn, speedOut = opts.speedOut, easeIn = opts.easeIn, easeOut = opts.easeOut, animOut = opts.animOut, animIn = opts.animIn;
		$n.css(opts.cssBefore);
		var fn = function() {$n.show();$n.animate(animIn, speedIn, easeIn, cb);};
		$l.animate(animOut, speedOut, easeOut, function() {
			if (opts.cssAfter) $l.css(opts.cssAfter);
			if (!opts.sync) fn();
		});
		if (opts.sync) fn();
	};
};