var ie;

var agent = navigator.userAgent;
if( agent.search(/iPhone/) != -1 ){
	mobile = "iPhone";
}else if( agent.search(/iPad/) != -1 ){
	mobile = "iPad";
}else if( agent.search(/Android/) != -1 ){
	mobile = "Android";
} else {
	mobile = "";
}

if (!("console" in window)) {
     window.console = {};
     window.console.log = function(str){
          return str;
     };
}

$(document).ready(function(){

	//init - literal
	$sls = {
		header : $('#header'),
		header_nav : $('#header-nav'),
		header_h1 : $('#header h1'),
		side : $('#side'),
		wrapper : $('.wrapper:first'),
		main : $('#main'),
		footer : $('#footer'),
		footer_bottom_a : $('#footer-bottom a'),
		home_header : $('#home-header'),
		home_banner : $('#home-banner')
	};

	//init - header 
	if ( mobile !="" ) {
		$('#header h2').html('<a href="/"><img src="/assets/images/header-logo.png" alt="SWEET LOVE SHOWER 2011" width="106" height="97" /></a>');
	}

	//init - quart
	jQuery.easing.quart = function (x, t, b, c, d) { return -c * ((t=t/d-1)*t*t*t - 1) + b; };

	//browser check -------------------------------------------- */
	if (navigator.platform.indexOf("Win") != -1) {
		$('body').addClass("win");
		if(!jQuery.support.opacity){
			if(!jQuery.support.style){ 
				$('body').addClass("win-ie").addClass("win-ieL");
				ie = "ie7"
			} else {
				$('body').addClass("win-ie");
			}
		}
	} else {
		$('body').addClass("mac");
	}

	//scroll -------------------------------------------- */
	$('.a-pagetop').click( function() { 
		$('html,body').animate({ scrollTop: 0 }, 800, 'quart');
		return false;
	});

	//scroll - anchor  -------------------------------------------- */
	$('.a-anchor').click( function() { 
		var target = $(this).attr('href');
		var scroll = $(target).offset();
		$('html,body').animate({ scrollTop: scroll.top }, 800, 'quart');
		return false;
	});

	// artist -------------------------------------------- */
	$('.lineup a').hover(function() {
		$(this).addClass('click-hover');
		$(this).find('dt img').stop(true, false).animate({ opacity: .0 }, { duration: 200});
	}, function() {
		$(this).removeClass('click-hover');
		$(this).find('dt img').stop(true, false).animate({ opacity: 1 }, { duration: 200});
	});

	// comment -------------------------------------------- */
	$('.comment li:nth-child(3n)').each( function() {
		$(this).addClass('last').after('<div style="clear:both;"></div>');
	});

	/* .post-content -------------------------------------------- */
	$('.post-content img').each(function() {
		if ( $(this).width() > 405 ) {
			$(this).css({
				'width':'404px',
				'height':'auto'
			});
		}
	});

	/* animation -------------------------------------------- */
	if ( animation == true && ie != "ie7") {
		$sls.side.css('width','0px');
		$sls.header_nav.css('padding-top','60px');
		$sls.header_h1.css('margin-top','-120px');
		$sls.footer_bottom_a.css('margin-top','0px');
		$sls.home_header.css('margin-left','780px');
		$sls.home_banner.find('li').css('margin-left','780px');
	} else {
		$sls.header.show();
		$sls.footer.show();
		$sls.wrapper.show();
/*
		if ( $sls.side.find('ul ul').length > 0 ) {
			$sls.side.find('ul ul').hide();
			$sls.side.css('width','134px');
		}
*/
	}
	
	/* animation - hover -------------------------------------------- */
	$sls.header.find('ul a').hover(function() {
		$(this).animate({paddingTop:5},100,'quart');
	}, function() {
		$(this).animate({paddingTop:0},100,'quart');
	});

	$sls.home_header.children().each(function() {
		$(this).hover(function() {
			$(this).animate({marginLeft:-5},100,'quart');
		}, function() {
			$(this).animate({marginLeft:0},100,'quart');
		});
	});

	$sls.home_banner.find('a').each(function() {
		$(this).hover(function() {
			$(this).animate({marginLeft:5},100,'quart');
		}, function() {
			$(this).animate({marginLeft:0},100,'quart');
		});
	});

	$sls.footer_bottom_a.hover(function() {
		$(this).animate({marginTop:0},200,'quart');
	}, function() {
		$(this).animate({marginTop:85},200,'quart');
	});
	
	
});


$(window).load(function() {

	hHeight = $sls.header.height();
	fHeight = $sls.footer.height();
	wHeight = $sls.wrapper.height();
	mHeight = $sls.main.height();

	/* animation -------------------------------------------- */
	if ( ie != "ie7" ) {
		if ( animation == true ) {
			if ( $sls.side.find('ul ul').length > 0 ) {
				var sWidth = 305;
			} else {
				var sWidth = 134;
			}
			$sls.side.delay(1500).animate({ 
				width: sWidth
			}, 1500, 'quart');
		}
	}
	if ( animation == true ) {
		if ( ie != "ie7" ) {
			
			$sls.wrapper.css('height','0px').show().delay(1500).animate({ 
				height: wHeight
			 }, 800, 'quart',function() {
					mHeight = $sls.main.height();
					if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.header.css('height','0px').show().delay(400).animate({ 
				height: hHeight
			}, 800, 'quart', function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.footer.css('height','0px').show().delay(400).animate({ 
				height: fHeight
			}, 800, 'quart', function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.header_nav.delay(1500).animate({ 
				paddingTop: 0
			}, 1500, 'quart' , function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.header_h1.delay(1500).animate({ 
				marginTop: 0
			}, 1500, 'quart', function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.footer_bottom_a.delay(1500).animate({
				marginTop:85
			}, 1500, 'quart', function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.home_header.delay(1500).animate({
				marginLeft:0
			}, 1500, 'quart', function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
			
			$sls.home_banner.find('li').delay(1500).animate({
				marginLeft:0
			}, 1500, 'quart', function() {
				if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
			});
		} else {
			$sls.header.show();
			$sls.wrapper.show();
		}
		
	}

});

$(window).resize(function(){
	scroll_side();
});

$(window).scroll(function () {
	scroll_side();
});

function scroll_side() {
	windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || $(window).height();
	mHeight = $sls.main.height();
	sHeight = $sls.side.height();
	sTop = $sls.side.offset().top;
	var bHeight = $('body').height();
	var scr = $(window).scrollTop() || $(document).scrollTop();
	
	//console.log("bH:" + bHeight + " / wH:" + windowHeight + " / sH:" + sHeight + " / scr:" + scr + " / fH:" + fHeight + " / sTop:" + sTop);
	
	if ( mobile == "") { //pc 
		if ( windowHeight > sHeight) {
			if ( scr < 174 ) {
				$sls.side.css({
					'position':'relative',
					'top':'0px',
					'margin-top':'0px'
				});
			} else if (scr > 173 ) {
				if ( bHeight > fHeight + sHeight + scr + 40 ) {
					$sls.side.css({
						'position':'fixed',
						'top':'20px',
						'margin-top':'0px'
					});
				} else {
					$sls.side.css({
						'position':'relative',
						'top':'40px',
						'margin-top':mHeight - sHeight +  'px'
					});
				}
			}
		} else { // windowHeight < sHeight
			$sls.side.css('position','relative');
		}
	} else { // mobile
		var pad = scr  - 193;
		if ( windowHeight > sHeight) {
			if ( scr < 174 ) {
				$sls.side.css('margin-top','0px');
			} else if (scr > 173 ) {
				$sls.side.css('margin-top', pad + 'px');
			}
		} else {
			$sls.side.css('margin-top','0px');
		}
	}
}


