$(document).ready(function() {
	
	/**
	 * FLASH PLAYER
	 */
		swfobject.embedSWF("player.swf", "mp3player", "200", "17", "9.0.0", "expressInstall.swf");
	
	/**
	 * BROWSER FIXES (IE5/6)
	 * --------------------------------------------------------- */
		var badBrowser = (/MSIE ((5\.5)|6|7|8)/.test(navigator.userAgent) && navigator.platform == "Win32");

	/** 
	 * DOCUMENT RESIZE - resize specific content elements for different resolutions
	 */
		$(window).resize(function(){
		  	docResize();
		});
		
		var docW = $(window).width();
		var docH = $(window).height();
		function docResize()
		{
			docW = $(window).width();
			docH = $(window).height();
			
			//footer
			if (docW < 1070)
			{
				$('.spacer').hide();
				$('input[name="email"]').css('width', '70px');
			}
			else 
			{
				$('input[name="email"]').css('width', '140px');
				$('.spacer').show();
			}
			
			//content pages
			if (docH < 810)	{
				$('#news-container').css('height', '70%');
			} else {
				$('#news-container').css('height', '82%');
			} 
			
			if (docH < 765)	{ 
				//$('#tour-dates-bg').css('height', '39%'); 
				$('#listen-content').css('height', '25%');
				$('#tour-dates').css('height', '20%'); 
				$('#look-media').css('height', '25%'); 
				$('#contact-content').css( { 'margin-top' : '80px', 'font-size' : '70%' });
				$('#contact-content h3').css( { 'margin-top' : '10px' });
				
				//resize image gallery if to small
				if (docH < 600)
				{
					if ($('#media-image').height() > 320)
					{
						$('#media-image').height(320);
					}
				} else {
					$('#media-image').height('auto');
				}
			}
			else { 
				//$('#tour-dates-bg').css('height', '49%'); 
				$('#listen-content').css('height', '45%');
				$('#tour-dates').css('height', '39%'); 
				$('#look-media').css('height', '45%'); 
			}
			
			if (docH < 1250) { 
				//$('#tour-dates-bg').css('width', '45%'); 
				$('#tour-dates').css('width', '42%'); 				
			} 
			else { 
				//$('#tour-dates-bg').css('width', '35%'); 
				$('#tour-dates').css('width', '45%'); 
			} 
						
			//scrolling divs to be reinitiated			
			$('.news-scroll').jScrollPaneRemove();
			$('.news-scroll').jScrollPane();
			$('.tour-scroll').jScrollPaneRemove();
			$('.tour-scroll').jScrollPane();
			$('.look-scroll').jScrollPaneRemove();
			$('.look-scroll').jScrollPane({animateTo:true, animateInterval:10, animateStep:5});
			$('.listen-scroll').jScrollPaneRemove();
			$('.listen-scroll').jScrollPane();
			
			if (!badBrowser)
			{
				$('.listen-learn-scroll').jScrollPaneRemove();
				$('.listen-learn-scroll').jScrollPane();
			}
			
			//fullscreen images adjusted
			resizeCenterFullscreenImage();
		}		
	
	/**
	 * PRELOAD IMAGES
	 */
		var urls = [ 'contact', 'home', 'listen', 'news', 'photos', 'tour' ];
		$.preload( urls, {
			base: 'images/',
			ext:'.jpg',
			onComplete:function( data ){				
			},
			onFinish:function(){
				loadSite();
			}
		});
		
	/**
	 * INITIAL SITE LOAD
	 */
		/* temp code *
		$('#loader').remove();
		$('#content').html('');
		$('#footer').css('opacity', .8)
		$('#bg').fadeTo(10, 0, function() {
			$('#bg').attr('src', 'images/look.jpg');
			$('#bg').fadeTo(10, 1, function(){
				loadContent('look'); //load Content after fade in
				$('body').css('background-color', '#ffffff');
				initMailingList();
			});
		});
		* end temp */
		
		function loadSite() 
		{
			$('#loader').fadeOut('slow', function() {
				$('#loader').remove(); //delete loader
				$('#bg').fadeTo(500, 0, function() {
					$('#bg').attr('src', 'images/home.jpg');
					$('#bg').fadeTo(1000, 1, function() {
						$('#footer')
							.css('margin-bottom', '-40px')
							.animate({'marginBottom' : '0px', 'opacity' : '.8'}, 1000, 'easeOut', function() {
								loadContent('home');
								initMailingList();
							});
						$('body').css('background-color', '#ffffff');
					});
				});
			});
		}
		
	/**
	 * CONTENT
	 */		
		//var emailVal = $('.email').val();
		//var zipVal = $('.zip').val();
		function initMailingList()
		{
			$('.footer-submit')
				.click(function() {
					emailVal = $('.email').val();
					zipVal = $('.zip').val();
					$('.mailing-list-container').html('<img src="images/loading.gif" style="padding: 0 210px" />');
					$.post(
						'mailinglist.php',
						{
							email : emailVal,
							zip	  : zipVal
						},
						function(data) {
							$('.mailing-list-container').html(data);
							initMailingList();													
						}
					);
				});
			$('a.optin')
				.click(function() {
					html  = '<span class="mailing-list">Mailing List:</span>';
					html += '<label>email</label><input type="text" name="email" id="email" />';
					html += '<label>zip</label><input type="text" class="zip" name="zip" id="zip" />';
					html += '<a href="javascript:;" class="footer-submit">submit</a>';
					$('.mailing-list-container').html(html);
					initMailingList();
				});
		}
		
		function loadContent(page)
		{
			$.get(
				'pages/'+ page +'.php',
				{ 
				},
				function(html) {
					$('#content').html(html);	
					if (page == "news") {
						$('.content-right-bg').fadeTo(1000, .5)
						$('.news-scroll').jScrollPane();	
					}	
					if (page == "tour") {
						$('#tour-dates-bg').fadeTo(1000, .2);
						$('#tour-dates').fadeTo(1000, 1, function() { docResize(); });												
						$('.tour-scroll').jScrollPane();
						initTour();
					}	
					if (page == "look") {
						
						$('.fullscreen-image-bg, .fullscreen-image').hide();
						//$('.look-scroll').jScrollPane({animateTo:true, animateInterval:10, animateStep:5});
						
						$('.look-jumpto a').bind(
							'click',
							function()
							{
								var destinationSelector = $(this).attr('rel');
								$('.look-scroll').each(function() {
									this.scrollTo(destinationSelector);
								});
								return false;								
							}
						); 
						initLook();
					}
					if (page == "listen") {
						$('#listen-content').fadeTo(2000, 1, function() {
							docResize();
							$('#listen-learn').fadeTo(1000, 1);	
						});
						$('.listen-scroll').jScrollPane();
						if (!badBrowser)
							$('.listen-learn-scroll').jScrollPane();
					}
					if (page == "contact") {
					}
					initNav();		
					docResize();					
				}
			);
		}
		
		function initTour()
		{
			$('.tour-table tr')
			.mouseover(function() {
				$('td', this)
				//.css({ 'cursor' : 'pointer', 'border-bottom' : '1px dotted black', 'padding' : '3px 0 3px 0' });
				.css({ 'cursor' : 'pointer', 'background' : 'white', 'color' : 'black' });
				//.animate({ 'paddingLeft' : '10px', 'backgroundColor' : '#ffffff', 'color' : '#000000' }, 300);
			})
			.mouseout(function() {
				$('td', this)
				.css({ 'background' : 'transparent', 'padding-left' : '0', 'color' : 'white' });
				//.animate({ 'paddingLeft' : '0', 'backgroundColor' : 'red', 'color' : '#000000' }, 300);
			})
			.click(function() {
				var id = $(this).attr('class').replace('tour-','');
				$('#tour-info table').hide();
				$('.tour-info-'+ id).show();
				$('#tour-info-bg').fadeTo(1000, .6);
				$('#tour-info').fadeTo(1000, .8);
			});
			
			$('#tour-close a').click(function() {
				$('#tour-info').fadeTo(1000, 0);
			})
		}
		
		var shown = 0;
		var imageCount = "";
		var currentImage = "";
		var gallery = "";
		function initLook()
		{
			var objGalleries = $('#gallery-promo, #gallery-tour, #gallery-life, #gallery-fans');
			objGalleries.hide();
			
			//show the galleries
			$('.gallery-link').click(function() {
				//objGalleries.hide();
				$(this).next('div').toggle();	
				gallery = $(this).next('div').attr('id').replace('gallery-', '');
				imageCount = $('.photos', $(this).next('div')).length;	
				docResize();
			});
			
			$('#video-container').hide();
			
			$('.media-thumb').click(function() {
				$('#video-container').hide();				
				var objImg = $('img', this);
				var image_id  = $('a', this).attr('class').replace('photo-', '');
				var image_src = 'galleries/'+ objImg.attr('rel') +'/images/'+ objImg.attr('id'); //attr('src').replace('thumbs', 'images');
				var image_dsc = $('span.media-desc-'+ image_id).html();
				currentImage = objImg.attr('class').replace('img-', '');	
				
				if (shown == 0)
				{
					shown = 1;
					$('#media-image').attr('src', image_src);
					var fullImg = image_src.replace('images', 'full');
					$('#fullImg img').attr('src', fullImg);
					
					var imgW = $('#full-'+ image_id).width();					
					//var imgW = $('#media-image').css('width').replace('px', '');
					if (imgW != "auto") {
						$('#media-full').css('width', imgW +'px');
					}
					$('#media-desc-text').html(image_dsc);
					$('#media-full-container').css( {'display' : 'block' }).fadeTo(1000,1);
					fullscreenImage();
				}
				else 
				{
					loadImage(image_src, image_dsc);
				}	
				
				//resize if to small
				if (docH < 600)
				{
					if ($('#media-image').height() > 320)
					{
						$('#media-image').height(320);
					}
				} else {
					$('#media-image').height('auto');
				}
			});
			
			$("[class^='video-']").click(function() {
				$('#media-full').hide();
				$('#video-container').show();
								
				var videoId = $(this).attr('class').replace('video-', '');
				if (shown == 0)
				{
					shown = 1;			
					$('#media-full-container').css( {'display' : 'block' }).fadeTo(1000,1, function(){
						loadVideo(videoId);	
					});
				}
				else 
				{
					loadVideo(videoId);					
				}
												
			});
			
			initPrevNext();
			
			//close
			$('#media-close').click(function() {
				$('#media-full-container').fadeTo(1000,0);
				shown = 0;
			});
		}
		
		function strstr (haystack, needle, bool) {
		    var pos = 0;

		    haystack += '';
		    pos = haystack.indexOf( needle );
		    if (pos == -1) {
		        return false;
		    } else{
		        if (bool){
		            return haystack.substr( 0, pos );
		        } else{
		            return haystack.slice( pos );
		        }
		    }
		}
		
		
		function loadImage(image_src, image_dsc)
		{
			$('#media-desc-text').stop().fadeTo(500,0);
			$('#media-image').fadeTo(500,0,function()
			{
				//load source and imensions
				$('#media-image').attr('src', image_src);
				
				var imgW = $('#media-image').width();//css('width').replace('px', '');
				var imgH = $('#media-image').height(); //css('height').replace('px', '');
				
				if (imgW == "NaN") imgW = 'auto';
				if (imgH == "NaN") imgH = 'auto';
				if (imgW == "0") {
					imgW = $("img[src^='"+ image_src +"']").width();
					imgH = $("img[src^='"+ image_src +"']").height();
				}
				
				
				//get fullscreen image ready
				var fullImg = $('#media-image').attr('src').replace('images', 'full');
				$('#fullImg img').attr('src', fullImg);
				
				//animate media image
				$('#media-full').animate({'width' : imgW, 'height' : imgH }, 500, function() {							
					$('#media-desc-text').html(image_dsc);	
					$('#media-desc-text').stop().fadeTo(300,1);						
					$('#media-image').fadeTo(300, 1);
					fullscreenImage();
				});			
				
			});
		}
		
		function initPrevNext()
		{			
			//rel=gallery_name, class=img-2, id=filename
			$('.media-next').click(function(){				
				currentImage++;
				if (currentImage > imageCount)	{ currentImage = 1;	}	
				var objImg = $('.img-'+ currentImage, $('#gallery-'+gallery));
				//var gallery = objImg.attr('rel');
				var filename = objImg.attr('id');
				image_src = 'galleries/'+ gallery  +'/images/'+ filename;
				parentId  = objImg.parent('a').attr('class').replace('photo-', '');
				image_dsc = $('span.media-desc-'+ parentId).html();
				loadImage(image_src, image_dsc);
			});
			$('.media-prev').click(function() {
				currentImage--;
				if (currentImage < 1) { currentImage = imageCount; }
				var objImg = $('.img-'+ currentImage, $('#gallery-'+gallery));					
				image_src = 'galleries/'+ gallery +'/images/'+ objImg.attr('id');
				parentId  = objImg.parent('a').attr('class').replace('photo-', '');
				image_dsc = $('span.media-desc-'+ parentId).html();
				loadImage(image_src, image_dsc);
			})
		}
		
		function fullscreenImage()
		{
			$('#media-image').click(function() {
				$('.fullscreen-image').show();
				$('.fullscreen-image-bg').show()					
					.fadeTo(500, .8, function() {
					$('.look').hide();
						resizeCenterFullscreenImage(); 
						$('.fullscreen-image').fadeTo(500, 1);
						$('.fullscreen-close').click(function() {
							$('.fullscreen-image-bg, .fullscreen-image')
							.stop()
							.fadeTo(100, 0, function() { $(this).hide(); $('.look').show(); });
						});
					});
			});
		}
		
		//downscale based on browerser size
		function resizeCenterFullscreenImage()
		{			
			var fullimgW = $('#fullImg img').width();
			var fullimgH = $('#fullImg img').height();
			var imgRatio = fullimgW / fullimgH;
			
			//check to see if the fits on the screen
			if ((fullimgW+50) > docW) 
			{
				fullimgW = docW-60;
				fullimgH = fullimgW / imgRatio;
				$('#fullImg img').width(fullimgW); //css('width',fullimgW);
				//$('#fullImg img').height(fullimgH); //css('height',fullimgH);
			}
			if ((fullimgH+50) > docH) 
			{
				fullimgH = docH-100;
				fullimgW = fullimgH * imgRatio;	
				//$('#fullImg img').width(fullimgW); css('width',fullimgW);
				$('#fullImg img').height(fullimgH); //css('height',fullimgH);
			}
			
			//get margins (after resize)
			var marginL =  fullimgW / 2;
			var marginT =  fullimgH / 2;
			
			//vertically and horizontally center image
			marginL = '-' + marginL + 'px';
			marginT = Number(marginT) +25;
			marginT = '-' + marginT + 'px';
			$('.fullscreen-image').css({'margin-left' : marginL, 'margin-top' : marginT });			

		}
		
		function loadVideo(videoId)
		{
			$('#media-image').attr('src', '');
			var width = 425;
			var height = 353;
			embed = '<object width="425" height="353">';
			embed += '<param value="http://www.youtube.com/v/'+ videoId +'" name="movie"/>';
			embed += '<param value="transparent" name="wmode"/>';
			embed += '<embed width="425" height="353" wmode="transparent" type="application/x-shockwave-flash" src="http://www.youtube.com/v/'+ videoId +'"/>';
			embed += '</object>';
			$('#video-container').html(embed);			
		}
		
		
		function fadeContentRight()
		{
			$('.content-right').fadeTo(500, 1);
		}
		
		function loadFooter()
		{
			$('#footer')
				.css('margin-bottom', '-40px')
				.animate({'marginBottom' : '0px', 'opacity' : '.8'}, { easing: 'easeOut', duration : 1000 });
		}
		
	/**
	 * NAVIGATION
	 */
		var iNav = 0;
		var iNavCount;
		function fadeInNav() //recursive function to chain load nav
		{
			var arNav = $('.nav a');
			
			//It's IE... skip the fade in
			if ( badBrowser ) 
			{
				iNav = iNavCount;
			}			
			
			//if ($(this).attr('')) { } //check to see if store or blog was clicked
			if (iNav < iNavCount) {		
					$(arNav[iNav]).fadeTo(300, 1, function() { fadeInNav(iNav) });
			}
			else {
				iNav = -1; //reset count
				initNavEvents();
				fadeContentRight();
			}
			iNav++;
		}
		
		function initNav()
		{
			iNavCount = $('.nav a').length; //num of nav links for fade in animation
			fadeInNav();
		}
		
		function initNavEvents()
		{
			$('.nav a, #logo a, .contact, .home-news-content a')
				.mouseover(function() {
					$(this).animate({'marginLeft' : '8px' }, 100);
				})
				.mouseout(function() {
					$(this).stop().animate({'marginLeft' : '0px' }, 400);
				})
				.click(function() {
					var className = $(this).attr('class').split(' ', 1); //className = name of page to load
					if (className == 'viewmore') className[0] = 'news';
					if (className[0] == "blog" || className[0] == "shop")
					{
					} else {
						$('#content').html('');
						$('#bg').fadeTo(500, 0, function() {
							$('#bg').attr('src', 'images/'+ className[0] +'.jpg');
							$('#bg').fadeTo(1000, 1, function(){
								loadContent(className[0]); //load Content after fade in
							});
						});	
					}
				});
		}
		
	/**
	 * RESIZABLE BG (SUPERSIZED)
	 */
		$(function(){		
			$.fn.supersized.options = {  
				startwidth: 1400,  
				startheight: 1021,
				vertical_center: 1,
				slideshow: 1,
				navigation: 0,
				transition: 3, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
				pause_hover: 0,
				slide_counter: 1,
				slide_captions: 1,
				slide_interval: 1000 
			};
	        $('#supersize').supersized(); 
	    });
	
});