$(document).ready(function(){
	// jQuery('abbr[class*=timeago]').timeago();
	// jQuery.timeago.settings.allowFuture = true;
	// Count how many release items there are.
	var releasesCount = $('.ul_releases li').size()
	// Calculate the width of the release items
	releaseItemWidth = Math.round(parseInt($('#releases_pane').css('width')) / releasesCount);
	// Set the width.
	$('.ul_releases li').css('width', releaseItemWidth);
	// Remove background shadow of first item.
	$('.ul_releases li:first-child a').css('background-image','none');
	// Intiate the kwicks.
	$('.ul_releases').kwicks({
		max : 360,
		spacing : 0
	});	
	
	// Menu Stuff
	
	// Append the html to contain the extra links.
	$('#ul_menu').append('<li id="li_gigs2" class="li_gig"><a href="gigs.php">Gigs</a></li><li id="li_photos2" class="li_photo"><a href="photos.php">Photos</a></li><li id="li_releases2" class="li_releases"><a href="releases.php">Releases</a></li><li id="li_releases3" class="li_releases"><a href="releases.php">Releases</a></li><li id="li_videos2" class="li_videos"><a href="videos.php">Videos</a></li><li id="li_videos3" class="li_videos"><a href="videos.php">Videos</a></li><li id="li_videos4" class="li_videos"><a href="videos.php">Videos</a></li><li id="li_members2" class="li_members"><a href="members.php">Members</a></li><li id="li_contact2" class="li_contact"><a href="contact.php">Contact</a></li><li id="li_signup2" class="li_signup"><a href="register.php">Signup</a></li><li id="li_shop2" class="li_shop" rel="external"><a href="http://www.arcticmonkeys-store.co.uk">Shop</a></li><li id="li_links2" class="li_links"><a href="links.php">Links</a></li>');
	
	// Settings for the hover states.
	$('.li_gig').hover(
		function(){
			$('#li_gigs').css('background', 'url("_graphics/ul_menu.gif") 0 -76px');
			$('#li_gigs2').css('background', 'url("_graphics/ul_menu.gif") -40px -68px');
		},
		function(){
			$('.li_gig').css('background', 'none');
		}	
	);
	
	$('.li_photo').hover(
		function(){
			$('#li_photos').css('background', 'url("_graphics/ul_menu.gif") 0 -158px');
			$('#li_photos2').css('background', 'url("_graphics/ul_menu.gif") -58px -153px');
		},
		function(){
			$('.li_photo').css('background', 'none');
		}	
	);
	
	$('.li_releases').hover(
		function(){
			$('#li_releases').css('background', 'url("_graphics/ul_menu.gif") 0 -221px');
			$('#li_releases2').css('background', 'url("_graphics/ul_menu.gif") -44px -209px');
			$('#li_releases3').css('background', 'url("_graphics/ul_menu.gif") -130px -209px');
		},
		function(){
			$('.li_releases').css('background', 'none');
		}	
	);
	
	$('.li_videos').hover(
		function(){
			$('#li_videos').css('background', 'url("_graphics/ul_menu.gif") 0 -279px');
			$('#li_videos2').css('background', 'url("_graphics/ul_menu.gif") -33px -283px');
			$('#li_videos3').css('background', 'url("_graphics/ul_menu.gif") -54px -289px');
			$('#li_videos4').css('background', 'url("_graphics/ul_menu.gif") -68px -293px');
		},
		function(){
			$('.li_videos').css('background', 'none');
		}	
	);	
	
	$('.li_members').hover(
		function(){
			$('#li_members').css('background', 'url("_graphics/ul_menu.gif") 0 -427px');
			$('#li_members2').css('background', 'url("_graphics/ul_menu.gif") -74px -417px');
		},
		function(){
			$('.li_members').css('background', 'none');
		}	
	);

	$('.li_contact').hover(
		function(){
			$('#li_contact').css('background', 'url("_graphics/ul_menu.gif") 0 -487px');
			$('#li_contact2').css('background', 'url("_graphics/ul_menu.gif") -92px -483px');
		},
		function(){
			$('.li_contact').css('background', 'none');
		}	
	);

	$('.li_signup').hover(
		function(){
			$('#li_signup').css('background', 'url("_graphics/ul_menu.gif") 0 -562px');
			$('#li_signup2').css('background', 'url("_graphics/ul_menu.gif") -43px -554px');
		},
		function(){
			$('.li_signup').css('background', 'none');
		}	
	);

	$('.li_shop').hover(
		function(){
			$('#li_shop').css('background', 'url("_graphics/ul_menu.gif") 0 -623px');
			$('#li_shop2').css('background', 'url("_graphics/ul_menu.gif") -50px -630px');
		},
		function(){
			$('.li_shop').css('background', 'none');
		}	
	);

	$('.li_links').hover(
		function(){
			$('#li_links').css('background', 'url("_graphics/ul_menu.gif") 0 -675px');
			$('#li_links2').css('background', 'url("_graphics/ul_menu.gif") -33px -682px');
		},
		function(){
			$('.li_links').css('background', 'none');
		}	
	);

	//External links.
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

	$('input.txt_input').hint();
});

// Hint function for forms.
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

/*
	Kwicks for jQuery (version 1.5.1)
	Copyright (c) 2008 Jeremy Martin
	http://www.jeremymartin.name/projects.php?project=kwicks
	Licensed under the MIT license:
		http://www.opensource.org/licenses/mit-license.php
	Any and all use of this script must be accompanied by this copyright/license notice in its present form.
*/

(function($){
	$.fn.kwicks = function(options) {
		var defaults = {
			isVertical: false,
			sticky: false,
			defaultKwick: 0,
			event: 'mouseover',
			spacing: 0,
			duration: 500
		};
		var o = $.extend(defaults, options);
		var WoH = (o.isVertical ? 'height' : 'width'); // WoH = Width or Height
		var LoT = (o.isVertical ? 'top' : 'left'); // LoT = Left or Top
		
		return this.each(function() {
			container = $(this);
			var kwicks = container.children('li');
			var normWoH = kwicks.eq(0).css(WoH).replace(/px/,''); // normWoH = Normal Width or Height
			if(!o.max) {
				o.max = (normWoH * kwicks.size()) - (o.min * (kwicks.size() - 1));
			} else {
				o.min = ((normWoH * kwicks.size()) - o.max) / (kwicks.size() - 1);
			}
			// set width of container ul
			if(o.isVertical) {
				container.css({
					width : kwicks.eq(0).css('width'),
					height : (normWoH * kwicks.size()) + (o.spacing * (kwicks.size() - 1)) + 'px'
				});				
			} else {
				container.css({
					width : (normWoH * kwicks.size()) + (o.spacing * (kwicks.size() - 1)) + 'px',
					height : kwicks.eq(0).css('height')
				});				
			}

			// pre calculate left or top values for all kwicks but the first and last
			// i = index of currently hovered kwick, j = index of kwick we're calculating
			var preCalcLoTs = []; // preCalcLoTs = pre-calculated Left or Top's
			for(i = 0; i < kwicks.size(); i++) {
				preCalcLoTs[i] = [];
				// don't need to calculate values for first or last kwick
				for(j = 1; j < kwicks.size() - 1; j++) {
					if(i == j) {
						preCalcLoTs[i][j] = o.isVertical ? j * o.min + (j * o.spacing) : j * o.min + (j * o.spacing);
					} else {
						preCalcLoTs[i][j] = (j <= i ? (j * o.min) : (j-1) * o.min + o.max) + (j * o.spacing);
					}
				}
			}
			
			// loop through all kwick elements
			kwicks.each(function(i) {
				var kwick = $(this);
				// set initial width or height and left or top values
				// set first kwick
				if(i === 0) {
					kwick.css(LoT, '0px');
				} 
				// set last kwick
				else if(i == kwicks.size() - 1) {
					kwick.css(o.isVertical ? 'bottom' : 'right', '0px');
				}
				// set all other kwicks
				else {
					if(o.sticky) {
						kwick.css(LoT, preCalcLoTs[o.defaultKwick][i]);
					} else {
						kwick.css(LoT, (i * normWoH) + (i * o.spacing));
					}
				}
				// correct size in sticky mode
				if(o.sticky) {
					if(o.defaultKwick == i) {
						kwick.css(WoH, o.max + 'px');
						kwick.addClass('active');
					} else {
						kwick.css(WoH, o.min + 'px');
					}
				}
				kwick.css({
					margin: 0,
					position: 'absolute'
				});
				
				kwick.bind(o.event, function() {
					// calculate previous width or heights and left or top values
					var prevWoHs = []; // prevWoHs = previous Widths or Heights
					var prevLoTs = []; // prevLoTs = previous Left or Tops
					kwicks.stop().removeClass('active');
					for(j = 0; j < kwicks.size(); j++) {
						prevWoHs[j] = kwicks.eq(j).css(WoH).replace(/px/, '');
						prevLoTs[j] = kwicks.eq(j).css(LoT).replace(/px/, '');
					}
					var aniObj = {};
					aniObj[WoH] = o.max;
					var maxDif = o.max - prevWoHs[i];
					var prevWoHsMaxDifRatio = prevWoHs[i]/maxDif;
					kwick.addClass('active').animate(aniObj, {
						step: function(now) {
							// calculate animation completeness as percentage
							var percentage = maxDif != 0 ? now/maxDif - prevWoHsMaxDifRatio : 1;
							// adjsut other elements based on percentage
							kwicks.each(function(j) {
								if(j != i) {
									kwicks.eq(j).css(WoH, prevWoHs[j] - ((prevWoHs[j] - o.min) * percentage) + 'px');
								}
								if(j > 0 && j < kwicks.size() - 1) { // if not the first or last kwick
									kwicks.eq(j).css(LoT, prevLoTs[j] - ((prevLoTs[j] - preCalcLoTs[i][j]) * percentage) + 'px');
								}
							});
						},
						duration: o.duration,
						easing: o.easing
					});
				});
			});
			if(!o.sticky) {
				container.bind("mouseleave", function() {
					var prevWoHs = [];
					var prevLoTs = [];
					kwicks.removeClass('active').stop();
					for(i = 0; i < kwicks.size(); i++) {
						prevWoHs[i] = kwicks.eq(i).css(WoH).replace(/px/, '');
						prevLoTs[i] = kwicks.eq(i).css(LoT).replace(/px/, '');
					}
					var aniObj = {};
					aniObj[WoH] = normWoH;
					var normDif = normWoH - prevWoHs[0];
					kwicks.eq(0).animate(aniObj, {
						step: function(now) {
							var percentage = normDif != 0 ? (now - prevWoHs[0])/normDif : 1;
							for(i = 1; i < kwicks.size(); i++) {
								kwicks.eq(i).css(WoH, prevWoHs[i] - ((prevWoHs[i] - normWoH) * percentage) + 'px');
								if(i < kwicks.size() - 1) {
									kwicks.eq(i).css(LoT, prevLoTs[i] - ((prevLoTs[i] - ((i * normWoH) + (i * o.spacing))) * percentage) + 'px');
								}
							}
						},
						duration: o.duration,
						easing: o.easing
					});
				});
			}
		});
	};
})(jQuery);