/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/10/16
 *
 * @author Blair Mitchelmore
 * @version 1.2
 * http://plugins.jquery.com/project/timers
 **/

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval < 0)
				return;

			if (typeof times != 'number' || isNaN(times) || times < 0) 
				times = 0;
			
			times = times || 0;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});
/**
 * Written by Rob Schmitt, The Web Developer's Blog
 * http://webdeveloper.beforeseven.com/
 */

/**
 * The following variables may be adjusted
 */

var active_color = '#000'; // Colour of user provided text
var inactive_color = '#afafaf'; // Colour of default text

/**
 * No need to modify anything below this line
 */

$(document).ready(function() {
  $(".form-question .textq").css("color", inactive_color);
  var default_values = new Array();
  $(".form-question .textq").focus(function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
	$(this).blur(function() {
	  if (this.value == '') {
		this.style.color = inactive_color;
		this.value = default_values[this.id];
	  }
	});
  });
});

$(document).ready(function(){
    var COOKIE_NAME = 'dontpanic_logo';
    var ANIM_COOKIE = 'anim';
    var options = { path: '/', expires: 30};
    if($.cookie(COOKIE_NAME)=="carrots")
        $(".box-logo-holder").toggleClass("div-parent-add");
        
	if(!$.cookie(ANIM_COOKIE))
	{	var img = new Image(); img.src="/animation.jpg";}
	var use_anim = true;

	//$.each(preload_images,function(){img = new Image();img.src=this;});

    $("#logo-switcher").click(function(){

		function replaceLogo()
		{
			use_anim = true;
			$("#header").attr('style',null)
			$(".logo").attr('style',null);
			$(".box-logo-holder").attr('style',null);
			$(".box-logo-holder").toggleClass("div-parent-add",true);
			$("#header").attr('style',null);
		}
		if((!$.cookie(ANIM_COOKIE)) && use_anim)
		{
			use_anim = false;
			var el = $("#header").eq(0);
			el.css({'background-position':'50% 30px'})
			$(".logo").css('background','none');
			$(".box-logo-holder").css("background","none");

			$.cookie(ANIM_COOKIE,1,options);
			$.cookie(COOKIE_NAME,'carrots',options);
			$(".box-logo-holder").toggleClass("div-parent-add");

			var offset=30;
			$(this).everyTime(50,function()
				{ if(offset <= -14050) {replaceLogo();return;} offset -= 440; el.css({'background-position':'50% '+offset+'px'});}
				,33);
		}
		else if(use_anim)
		{
			$("#header").attr('style',null)
			$(".logo").attr('style',null);
			$(".box-logo-holder").attr('style',null);
			$(".box-logo-holder").toggleClass("div-parent-add");
			($.cookie(COOKIE_NAME)=="carrots") ? $.cookie(COOKIE_NAME, "not_carrots", options) : $.cookie(COOKIE_NAME, 'carrots', options);
		}
		return false;
    });
    $(".link-question").click(function () {
        $(".popup").slideToggle("slow");
		return false;
        });
        
    $(".submit1").click(function () {
        $(".popup").slideUp("slow");
		return false;

    });
});

$(document).ready(function(){
$("#schema").click(function(){
$("#map").modal({overlayClose:true,onOpen: function (dialog) {
                dialog.overlay.fadeIn('slow', function () {
                    dialog.data.hide();
                    dialog.container.fadeIn('slow', function () {
                        dialog.data.slideDown('slow');
        });
    });
},
onClose: function (dialog) {
    dialog.data.fadeOut('fast', function () {
        dialog.container.hide('slow', function () {
            dialog.overlay.slideUp('fast', function () {
                $.modal.close();
            });
        });
    });
}});
return false;
});
});

//slider
$(document).ready(function(){

if(!$(".index-content").length) return;

$(".index-content").addClass("d-index");

var cur_pos = 0;
var total_count = $(".scroll .list li").size();
var list_w = total_count*((el = $(".scroll .list li").eq(1)).width()+parseInt(el.css('padding-left'))+parseInt(el.css('padding-right')));
var li_w = el.width();

var images_list_w = $(".images li").size()*((el = $(".images li").eq(1)).width()+parseInt(el.css('padding-left'))+parseInt(el.css('padding-right')));
var images_li_w = el.width();

var epsilon = 1.7*li_w;

var max_li_h = 0;

var description_to_show = 1;
$.each($(".scroll .list li"),function(){
	if($(this).height() > max_li_h)
		max_li_h = $(this).height();
});
var list_h = $(".list").height();
$.each($(".scroll .list li"),function(){
	$(this).css('top',Math.max(0,list_h-max_li_h));
});

$(".list ol").css('width',list_w);

$(".slider").draggable({axis:'x', containment:'parent', 
	drag:function(event, ui){ 
		cur_pos = ui.position.left/(ui.helper.parent().width()-ui.helper.width());
		ml = cur_pos*(list_w - $(".list").width());
		$(".list ol").css("margin-left","-"+ml+"px");

		images_ml = cur_pos*(images_list_w - $(".images").width());
		$(".images ol").css("margin-left","-"+images_ml+"px");
		recalc();
		}
}).click(function(){return false;});
$(".slider-holder").click(function(event){
	var to_x = event.clientX-$(event.target).offset().left;
	var sl_w = $(".slider").width();
	to_x -=sl_w/2;
	if(to_x > $(this).width()-sl_w)	to_x = $(this).width()-sl_w;
	else if(to_x < sl_w/2)	to_x = 0;
	$(".slider").animate({'left':to_x});
	cur_pos = to_x/($(this).width()-sl_w);
	ml = cur_pos*(list_w - $(".list").width());
	$(".list ol").animate({"marginLeft":("-"+ml+"px")});

	ml = cur_pos*(images_list_w - $(".images").width());
	$(".images ol").animate({"marginLeft":("-"+ml+"px")});

	recalc();
	return false;
	});

function recalc()
{
	var cpw = cur_pos*list_w;
	$.each($(".list li"),function(){
	var c = Math.cos(
					Math.min(Math.abs($(this).position().left + li_w/2-cpw), epsilon)/epsilon*Math.PI/2);
	var color = ''+parseInt((1-c)*10);
		$(this).css('top',(1-c)*(list_h-3-max_li_h)).css('color','#'+color+color+color);
	});

	prev_description_to_show = description_to_show;
	if(cur_pos ==0)
		description_to_show = 1;
	else 
		description_to_show = Math.ceil(cur_pos*total_count);
	if(prev_description_to_show != description_to_show)
	{
		$(".description > li:nth-child("+prev_description_to_show+")").removeClass("shown");
		$(".description > li:nth-child("+description_to_show+")").addClass("shown");
		//$(".description > li:nth-child("+description_to_show+") .box-text li").removeClass("shown");
	}
}
$(".description > li .box-text > ol > li:first-child").addClass("shown");

$(".pagination a").click(function(){
	to_show = this.hash.substring(1);
	$(".description li.shown .box-text > ol > li").removeClass("shown");
	$(".description li.shown .pagination li").removeClass("active");

	$(".description li.shown .box-text > ol > li:nth-child("+to_show+")").addClass("shown");
	$(".description li.shown .pagination li:nth-child("+to_show+")").addClass("active");
	return false;});
recalc();
});

