/**
 * jQuery thumbnail rotator
 *
 */
jQuery.rotator = {
	attach: function() {
		$("img.rotator").hover(function() {
			var img = $(this);

			img.urls = img.attr("urls").split(" ");
			img.count = img.urls.length;
			img.attr("default_url", img.attr("src"));

			jQuery.rotator.show(img, 0);
			jQuery.rotator.timer = setInterval(function() {
				jQuery.rotator.cycle(img);
			}, 500);
		}, function() {
			var img = $(this);

			clearInterval(jQuery.rotator.timer);
			jQuery.rotator.show(img, null);
		});
	},
	cycle: function(img) {
		var index = (img.index + 1) % img.count;

		jQuery.rotator.show(img, index);
	},
	show: function(img, index) {
		if(index != null) {
			img.index = index;
			img.attr("src",  img.urls[img.index]);
		}
		else {
			img.attr("src",  img.attr("default_url"));
		}
	},
	timer: null
};

/**
 * jQuery body rotator
 *
 */
jQuery.bodies = {
	attach: function() {
		$("img.body").hover(function() {
			var img = $(this);

			img.attr("front", img.attr("src"));
			img.attr("src", img.attr("back"));
		}, function() {
			var img = $(this);

			img.attr("src", img.attr("front"));
		});

		$("a.body").hover(function() {
			var id = $(this).attr("turns");
			var img = $("#" + id);

			img.attr("front", img.attr("src"));
			img.attr("src", img.attr("back"));
		}, function() {
			var id = $(this).attr("turns");
			var img = $("#" + id);

			img.attr("src", img.attr("front"));
		});
	}
};

/**
 * jQuery URL plugin
 *
 */
jQuery.url = {
	append: function(url, param, value) {
		var val = param + "=" + value;

		if(url.indexOf("?") >= 0) {
			url += "&" + val;
		}
		else {
			url += "?" + val;
		}

		return url;
	},
	getvars: function() {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}

		return vars;
	},
	getvar: function(name) {
		return jQuery.url.getvars()[name];
	}
};

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 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
 * https://github.com/carhartl/jquery-cookie
 *
 */
jQuery.cookie = function(key, value, options) {

    // key and at least value given, set cookie...
    if(arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if(value === null || value === undefined) {
            options.expires = -1;
        }

        if(typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;

    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

