/*
 * jQuery Tools 1.2.5 - The missing UI library for the Web
 * 
 * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
 * 
 * http://flowplayer.org/tools/
 * 
 */

(function($){
	// placeholder test
	$(function() {
		jQuery.support.placeholder = false;
		test = document.createElement('input');
		if('placeholder' in test) jQuery.support.placeholder = true;
	});
	$.fn.extend({
		prompt: function(){
			// test if supports placeholder attribute
			if ($.support.placeholder) { return $(this); }
			return this.each(function(){
				var el = $(this);
				var ph = el.attr('placeholder');
				if (!el.val()) {
					el.val(ph).addClass('placeholder');
				}
				el.focus(function(e){
					if (this.value == ph) {
						this.value = '';
						el.removeClass('placeholder');
					}
				})
				.blur(function(e){
					if (this.value == ph || this.value === '') {
						this.value = ph;
						el.addClass('placeholder');
					}
				});
			});
		},
		dropdown: function(opts){
			opts = $.extend({
				hd: 'dt',
				bd: 'dd',
				openClass: 'open'
			}, opts);
			return $(this).each(function(){
				var cnt = $(this),
					hd = $(opts.hd, cnt),
					bd = $(opts.bd, cnt);
				var open;
				hd.each(function(){
					var h = $(this),
						b = h.next(opts.bd);
					if (!h.is('.'+opts.openClass)) {
						b.hide();
					}
					h.bind('click', function(){
						if (h.is('.'+opts.openClass)) {
							b.slideUp(function(){
								h.removeClass(opts.openClass);
								open = null;
							});
						}
						else {
							h.addClass(opts.openClass);
							if (open && open.jquery) {
								open.prev().triggerHandler('click');
								open = null;
							}
							b.slideDown(function(){
								open = b;
							});
							
						}
					});
				});
				open = bd.filter(":visible");
			});
		},
		slider: function(opts){
			opts = $.extend({
				next: '.next',
				prev: '.prev',
				items: 'li',
				activeClass: 'active',
				pageItems: '.pages li a',
				activePageClass: 'active',
				interval: 2000
			}, opts);
			return $(this).each(function(){
				var cnt = $(this),
					next = $(opts.next, cnt),
					prev = $(opts.prev, cnt),
					items = $(opts.items, cnt),
					pageItems = $(opts.pageItems, cnt),
					activeIndex = 0,
					last = items.length - 1,
					locked = false,
					timer = null;
				items.first().nextAll().hide();
				
				var start = function(){
					timer = setInterval(function(){
						next.triggerHandler('click.slider');
					}, opts.interval)
				}
				var stop = function(){
					clearInterval(timer);
				}
				
				next.bind('click.slider', function(e){
					e.preventDefault();
					if (locked) { return; }
					locked = true;
					if (activeIndex == last) {
						n = 0;
					}
					else {
						n = activeIndex + 1;
					}
					items.eq(activeIndex).fadeOut(function(){
						items.eq(n).fadeIn();
						activeIndex = n;
						locked = false;
						pageItems.removeClass(opts.activePageClass).eq(n).addClass(opts.activePageClass);
					});
				});
				
				prev.bind('click.slider', function(e){
					e.preventDefault();
					if (locked) { return; }
					locked = true;
					if (activeIndex == 0) {
						n = last;
					}
					else {
						n = activeIndex - 1;
					}
					items.eq(activeIndex).fadeOut(function(){
						items.eq(n).fadeIn();
						activeIndex = n;
						locked = false;
						pageItems.removeClass(opts.activePageClass).eq(n).addClass(opts.activePageClass);
					});
				});
				
				pageItems.each(function(i){
					var a = $(this).bind('click.slider', function(e){
						e.preventDefault();
						items.eq(activeIndex).fadeOut(function(){
							items.eq(i).fadeIn();
							activeIndex = i;
							locked = false;
							pageItems.removeClass(opts.activePageClass).eq(i).addClass(opts.activePageClass);
						});
					});
				});
				if (opts.interval) {
					items.bind('mouseenter', function(){ 
						stop(); 
					}).bind('mouseleave', function(){ 
						start(); 
					})
					start();
				}
			});
		},
	});
	/**
 * 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
 *
 */

$.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;
    }
};
	
$(function(){
	var sizes = {
		small: '12px',
		medium: '14px',
		large: '16px'
	};
	$(".set_size").bind('click', function(e){
		e.preventDefault();
		var s = $(this).attr('rel');
		$('body').css({'font-size': sizes[s]});
		$.cookie('size', s, { expires: 999, path: '/'});
	});
	if ($.cookie('size') && $.cookie('size') != 'small') {
		$('body').css({'font-size': sizes[$.cookie('size')]});
	}
	
	$(".dropdown").dropdown();
	$(".slider").slider();
	$(".prompt").prompt();
	$(".picture_slider").slider({
		items: '.items li',
		interval: 5000
	});
	
});
	
})(window.jQuery);

