//jQuery Pluging Here
/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
/**
 *  corner() takes a single string argument:  $('#myDiv').corner("effect corners width")
 *
 *  effect:  name of the effect to apply, such as round, bevel, notch, bite, etc (default is round). 
 *  corners: one or more of: top, bottom, tr, tl, br, or bl. 
 *           by default, all four corners are adorned. 
 *  width:   width of the effect; in the case of rounded corners this is the radius. 
 *           specify this value using the px suffix such as 10px (and yes, it must be pixels).
 *
 * @name corner
 * @type jQuery
 * @param String options Options which control the corner style
 * @cat Plugins/Corner
 * @return jQuery
 * @author Dave Methvin (http://methvin.com/jquery/jq-corner.html)
 * @author Mike Alsup   (http://jquery.malsup.com/corner/)
 * 
 * documentation -  http://www.malsup.com/jquery/corner/
 */
;(function($){var expr=(function(){if(!$.browser.msie)return false;var div=document.createElement('div');try{div.style.setExpression('width','0+0')}catch(e){return false}return true})();    
function sz(el,p){return parseInt($.css(el,p))||0};
function hex2(s){var s=parseInt(s).toString(16);return(s.length<2)?'0'+s:s};
function gpc(node){for(;node&&node.nodeName.toLowerCase()!='html';node=node.parentNode){var v=$.css(node,'backgroundColor');if(v=='rgba(0, 0, 0, 0)')continue;if(v.indexOf('rgb')>=0){var rgb=v.match(/\d+/g);return'#'+hex2(rgb[0])+hex2(rgb[1])+hex2(rgb[2])}if(v&&v!='transparent')return v}return'#ffffff'};
function getWidth(fx,i,width){switch(fx){case'round':return Math.round(width*(1-Math.cos(Math.asin(i/width))));case'cool':return Math.round(width*(1+Math.cos(Math.asin(i/width))));case'sharp':return Math.round(width*(1-Math.cos(Math.acos(i/width))));case'bite':return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));case'slide':return Math.round(width*(Math.atan2(i,width/i)));case'jut':return Math.round(width*(Math.atan2(width,(width-i-1))));case'curl':return Math.round(width*(Math.atan(i)));case'tear':return Math.round(width*(Math.cos(i)));case'wicked':return Math.round(width*(Math.tan(i)));case'long':return Math.round(width*(Math.sqrt(i)));case'sculpt':return Math.round(width*(Math.log((width-i-1),width)));case'dog':return(i&1)?(i+1):width;case'dog2':return(i&2)?(i+1):width;case'dog3':return(i&3)?(i+1):width;case'fray':return(i%2)*width;case'notch':return width;case'bevel':return i+1}};
$.fn.corner=function(o){if(this.length==0){if(!$.isReady&&this.selector){var s=this.selector,c=this.context;$(function(){$(s,c).corner(o)})}return this}o=(o||"").toLowerCase();var keep=/keep/.test(o);var cc=((o.match(/cc:(#[0-9a-f]+)/)||[])[1]);var sc=((o.match(/sc:(#[0-9a-f]+)/)||[])[1]);var width=parseInt((o.match(/(\d+)px/)||[])[1])||10;var re=/round|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dog/;var fx=((o.match(re)||['round'])[0]);var edges={T:0,B:1};var opts={TL:/top|tl/.test(o),TR:/top|tr/.test(o),BL:/bottom|bl/.test(o),BR:/bottom|br/.test(o)};if(!opts.TL&&!opts.TR&&!opts.BL&&!opts.BR)opts={TL:1,TR:1,BL:1,BR:1};var strip=document.createElement('div');strip.style.overflow='hidden';strip.style.height='1px';strip.style.backgroundColor=sc||'transparent';strip.style.borderStyle='solid';return this.each(function(index){var pad={T:parseInt($.css(this,'paddingTop'))||0,R:parseInt($.css(this,'paddingRight'))||0,B:parseInt($.css(this,'paddingBottom'))||0,L:parseInt($.css(this,'paddingLeft'))||0};if(typeof this.style.zoom!=undefined)this.style.zoom=1;if(!keep)this.style.border='none';strip.style.borderColor=cc||gpc(this.parentNode);var cssHeight=$.curCSS(this,'height');for(var j in edges){var bot=edges[j];if((bot&&(opts.BL||opts.BR))||(!bot&&(opts.TL||opts.TR))){strip.style.borderStyle='none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');var d=document.createElement('div');$(d).addClass('jquery-corner');var ds=d.style;bot?this.appendChild(d):this.insertBefore(d,this.firstChild);if(bot&&cssHeight!='auto'){if($.css(this,'position')=='static')this.style.position='relative';ds.position='absolute';ds.bottom=ds.left=ds.padding=ds.margin='0';if(expr)ds.setExpression('width','this.parentNode.offsetWidth');else ds.width='100%'}else if(!bot&&$.browser.msie){if($.css(this,'position')=='static')this.style.position='relative';ds.position='absolute';ds.top=ds.left=ds.right=ds.padding=ds.margin='0';if(expr){var bw=sz(this,'borderLeftWidth')+sz(this,'borderRightWidth');ds.setExpression('width','this.parentNode.offsetWidth - '+bw+'+ "px"')}else ds.width='100%'}else{ds.margin=!bot?'-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px':(pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px'}for(var i=0;i<width;i++){var w=Math.max(0,getWidth(fx,i,width));var e=strip.cloneNode(false);e.style.borderWidth='0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';bot?d.appendChild(e):d.insertBefore(e,d.firstChild)}}}})};
$.fn.uncorner=function(){$('div.jquery-corner',this).remove();return this};    
})(jQuery);
/**
 * jQuery - Set Sibling Hover
 * Copyright (c) 2008 - 2009 Mike Slater
 * @author Mike Slater
 *
 */
;(function($){
  $.fn.setSiblingHover = function(options) {
  	var settings = $.extend({
  		fadeLevel : 0.25,
  		overSpeed : 250,
  		sibSpeed : 500
  	},options||{});
	var item = $(this)
	item.hover(
		function(){
			$(this).stop()
			.fadeTo(settings.overSpeed, 1.0)
			.siblings()
			.stop()
			.fadeTo(settings.sibSpeed, settings.fadeLevel);
		}, function(){
			item.parent().children()
			.stop()
			.fadeTo(settings.sibSpeed, 1.0);
	});
    return this;
  };
})(jQuery);

//GLOBAL VARIABLES HERE
var $thumbnails;
var galLength = 0;
var rotationTime = 12000;
var lgImg = '';
var lgImgCaption = '';
var rotation = '';
var $photo;

//GLOBAL FUNCTIONS HERE
function getHeader(index) {
	switch(index)
	{
	case 0:
	  headerURL = 'url(../../images/headers/header_banjo.jpg)';
	  break;    
	case 1:
	  headerURL = 'url(../../images/headers/header_banjo2.jpg)';
	  break;
	case 2:
	  headerURL = 'url(../../images/headers/header_mando.jpg)';
	  break;
	case 3:
	  headerURL = 'url(../../images/headers/header_guitar.jpg)';
	  break;
	case 4:
	  headerURL = 'url(../../images/headers/header_bass.jpg)';
	  break;
	case 5:
	  headerURL = 'url(../../images/headers/header_elec.jpg)';
	  break;
	case 6:
	  headerURL = 'url(../../images/headers/header_elec.jpg)';
	  break;
	case 7:
	  headerURL = 'url(../../images/headers/header_mando.jpg)';
	  break;
	case 8:
	  headerURL = 'url(../../images/headers/header_elec.jpg)';
	  break;
	case 9:
	  headerURL = 'url(../../images/headers/header_mando.jpg)';
	  break;
	default:
	  headerURL = 'url(../../images/headers/header_banjo.jpg)';
	}
	
	return headerURL;
}

var swapPhoto = function()
{			
	var fadeInPhoto = function()
	{
		$photo.fadeTo(750, 1.0);
		$('#main_content .img_caption').text(lgImgCaption);
		$photo.unbind('load');
	};
	
	clearTimeout(rotation);
	rotation = setTimeout(function()
	{
		$('#nextButton').trigger('click');
	}, rotationTime);

	lgImg = $(this).addClass('current').siblings().removeClass('current')
										   .end().attr('rel');
	lgImgCaption = $(this).find('img').attr('alt');
				
	$photo.fadeTo(750, 0.0, function()
	{
		$photo.attr('src', lgImg);
		if($photo.complete)
		{
			fadeInPhoto();
		}
		else
		{
			$photo.bind('load', fadeInPhoto);
		}
	});
	return false;
};

var initSelectable = function()
{
	$("ul.selectable li").hover(function(e) {
			$(this).addClass("selectOver");									  
		}, function(e){
			$(this).removeClass("selectOver");	
		}
	);
};

var handleFocus = function (element)  
{  
	if (element.value == element.defaultValue)   
	{  
    	element.value = '';  
	}  
};

var handleBlur = function (element)  
{  
	if (element.value === '')  
	{  
    	element.value = element.defaultValue;  
	}  
};

$.fn.tip = function() 
{
	$(this).click(
		function(e)
		{	
			if ($('#theTip').length === 0)
			{
				var theText = $(this).attr('rel');
				
				var $theTip = $('#bioTip');
				$theTip.html('<blockquote><img class="bio_close_but floatleft" src="/images/buttons/close.gif" alt="Close" />' + theText + '</blockquote>');
				$theTip.css('top', 20);
				$theTip.fadeTo(250, 1.0);
				$('.bio_close_but').click(
									function()
									{
										$theTip.fadeTo(250, 0.0, function()
																 {
																	$(this).css('top', -9999);
																})
									});
			}
			
		});
	return this;
};

//RUN jQUERY AND JS ON DOM LOAD
$(function() {  	
	var randNum = Math.floor(Math.random() * 12);
	var headerImg = getHeader(randNum);
	$('#header').css('background-image', String(headerImg));
	
	//setting nav current state
	$(".home_home .home", $("#wrapper")).addClass("current");
	$(".home_tunes .tunes", $("#wrapper")).addClass("current");
	$(".home_shows .shows", $("#wrapper")).addClass("current");
	$(".home_photos .pics", $("#wrapper")).addClass("current");
	$(".home_contact .contact", $("#wrapper")).addClass("current");
	$(".home_news .news", $("#wrapper")).addClass("current");
	
	$("#navbar ul li").hoverIntent(
		function() {
			$(this).find("ul:hidden").show();
		},
		function() {
			$(this).find("ul:visible").hide();
		}
	);	
		
	$(".images_left_rail_heading", $("#rail")).corner("slide top 6px");
	$(".sectionheader", $("#main")).corner("slide top 8px");
	$("img", $("#footer_main")).draggable({axis:"x", containment:"parent"}).addClass('hand');
	
	$('#bioTip').fadeTo(10, 0.0).css('top', -9999);
	
	if (document.getElementById('news_list')) {  
		$theList = $('#news_list');
		$('.info:gt(0)', $theList).hide();
	    $('#news_list li').click(function(e){
			$(e.target).parent().siblings().children('div.info')
																.slideUp(900)
																.end()
										   .end()
								.children('div.info')
								.slideDown(900);
		});

		$('#news_list li .info .news_hide').click(function(e){
			$(e.target).parent().slideUp(900);
		});
	}
	
	initSelectable();
	$('a.tipMe').tip().click(function(){return false;});

	$("#date").datepicker(
		{
			dateFormat: 'yy-mm-dd',
			showOn: 'button',
			buttonImage: '/images/icons/calendar.gif',
			buttonImageOnly: true
		});
	
});


//RUN THIS CODE AFTER EVERYTHING ON PAGE IS LOADED
$(window).load(function() 
{
	
	$photo = $('#photo');
	
	//CHECK FOR A GALLERY ON THE PAGE BEFORE RUNNING CODE
	if (document.getElementById('thumbnails')) 
	{
		
		$thumbnails = $('#thumbnails');
			
		//RUN THIS AFTER CLICKING A GALLERY
		$('#gallery_list li a').bind('click', function()
		{			
			$thumbnails.addClass('invisible');
			
			$('#gallery_list li.active').removeClass('active');
			$(this).parent().addClass('active');
			
			var galleryID = $(this).attr('rel');

			$thumbnails.load('../../index.php/ajax/getImages', {theGal : galleryID}, function()
			{
				$thumbnails.find('a').setSiblingHover().bind('click', swapPhoto);
				$thumbnails.removeClass('invisible');
				$thumbnails.find('a:first').trigger('click');
			});
			
			return false;
		});
		
		//RUN THIS AFTER CLICKING A THUMBNAIL
		$thumbnails.find('a').setSiblingHover().bind('click', swapPhoto);
		
		//INITIALIZE FIRST PHOTO ON PAGELOAD
		$thumbnails.find('a:first').trigger('click');
		
		//SETUP PREV BUTTON
		$('#previousButton').bind('click', function()
		{
			if ($thumbnails.find('a:first-child').hasClass('current'))
			{
				$thumbnails.find('a:last-child').trigger('click');
			}
			else
			{
				$thumbnails.find('a.current').prev().trigger('click');
			}
		});
		
		//SETUP NEXT BUTTON
		$('#nextButton').add('#photo').bind('click', function()
		{
			if ($thumbnails.find('a:last-child').hasClass('current'))
			{
				$thumbnails.find('a:first-child').trigger('click');
			}
			else
			{
				$thumbnails.find('a.current').next().trigger('click');
			}
		});
		
	}
	
});