// jQuery functions
// select a given range in an elemt - useful for moving a cursor with an input
$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if(this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if(this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

// javascript to run when DOM has fully loaded (i.e. html structure ready and the earliest you can safely run js)
$(function () {
	// this will toggle the month and year items in our archive menu
	if ($('#archive')) {			
		if ($('#archive').children().size() > 1) {
			//collapse the menu			
			$('#archive').addClass('js');					
		}
		else if ($('#archive ul').find('li:not(#archive ul ul li)').size() > 3) {
			//collapse the menu			
			$('#archive').addClass('js');				
		}
		else {
			$('#archive').find('li:not(#archive ul ul li)').addClass('open');
		}
	}
	
	$('#archive li').click(function (event) {
	    event.stopPropagation();
	    if ($(event.target).is('li')) {
			$('#archive').addClass('js');
	        $(this).toggleClass('open');
	    }
	});	
	
	if ($('#query')) {
		$('#query').focus(function () {
			// set the cursor to the start of the input
			if($(this).val() == 'Search') {
				$(this).selectRange(0,0);
				$(this).addClass('fade');
			}
			else {
				$(this).selectRange(0,$(this).val().length);
			}
		}).keydown(function () {
			// remove the 'Search' text (if necessary)
			if($(this).val() == 'Search') {
				$(this).val('');
				$(this).removeClass('fade');
			}			
		}).blur(function () {
			// if empty replace 'Search' text
			if($(this).val() == '') {
				$(this).val('Search');
			}	
			$(this).removeClass('fade');		
		});
	}
	
	if($('#search-go')) {
		$('#search-go').hover(function () {
			$(this).toggleClass('over');
		});
	}
	
	// sIFR
	if (typeof sIFR == "function") {
		sIFR.replaceElement("h1 span.sifr-title, h2 span.sifr-title, h3 span.sifr-title, h4 span.sifr-title", named({sFlashSrc: "/libraries/sifr/technic.swf", sColor: "#e8393a", sWmode: "transparent"}));
	}
	
	if ($('#sign-up')) {
		$('#sign-up').submit(function () {
			if ($('#email-input').val() == '' || $('#email-input').val() == 'Enter your email') {
				alert('Please enter your email.');
				return false;
			}
			else {
				return true;
			}
		});
	}
	
	if ($('#email-input')) {
		$('#email-input').focus(function () {
			// set the cursor to the start of the input			
			if($(this).val() == 'Enter your email') {
				$(this).selectRange(0,0);
				$(this).addClass('fade');
			}
			else {
				$(this).selectRange(0,$(this).val().length);
			}
		}).keydown(function () {
			// remove the 'Search' text (if necessary)
			if($(this).val() == 'Enter your email') {
				$(this).val('');
				$(this).removeClass('fade');
			}			
		}).blur(function () {
			// if empty replace 'Search' text
			if($(this).val() == '') {
				$(this).val('Enter your email');
			
			}
			$(this).removeClass('fade');					
		});	
	}
	
	// run gallery code
	if ($('#gallery').length > 0) {
	 	$('#gallery').adGallery({
  			loader_image: 'loader.gif',
  			width: 659,
  			slideshow: {  			
				enable: true,
				autostart: true,
				speed: 4000,
				start_label: 'Start',
				stop_label: 'Stop',
				stop_on_scroll: true, // Should the slideshow stop if the user scrolls the thumb list?
				countdown_prefix: '(', // Wrap around the countdown
				countdown_sufix: ')',
				onStart: function() {
				  // Do something wild when the slideshow starts
				},
				onStop: function() {
				  // Do something wild when the slideshow stops
				}
			}			
		});
	}

	
	// sphider suggestions
	initializeSuggestFramework();
});

// javascript to run when window has fully loaded (i.e. all html and images loaded)
/*
$(window).load(function () {
	
});
*/