/*
 * main.js
 *  April 2010
 *  
 *  Javascript enhancements for Vidya.tv
 * 
 * by kchevalier@suntouchdesign.com
 * 
 */

$(document).ready(function() {
	Scroller.init();
	ExploreLinks.init();
	Addresser.init();
	Styles.init();
	LotusNav.init();
	FixedLotusNav.init();
});

var ScreenPosition = 'screen1';

var Scroller = {
	
	siteView: null,
	scroll: null,
	page: null,
	viewWidth: 1200,
	viewHeight: 900,
	widthDif: 0,
	heightDif: 0,
	currHeight: 0,
	pad: 150,
	
	init: function() {
		Scroller.siteView = $('#siteView');
		Scroller.scroll = $('#scroll');
		Scroller.page = $('#page');
		Scroller.updateCSS();
		Scroller.page.draggable( { containment: 'parent', cancel: 'p,h1,h2,h3,h4,h5,h6,ul,ol,a,#mediaPlayer', stop: Scroller.stopDrag } );
		$(window).resize( function(evt) {
			if ($.browser.msie && parseInt($.browser.version) < 7) {
				return;		// ie6 won't resize well - too bad
			}
			window.location = location.pathname;  // bit of a hack for drag container, but oh well...
		} );
	},
	
	stopDrag: function( evt, ui ) {
		Scroller.updateScreen( evt, ui );
		FixedLotusNav.update();
	},
	
	updateScreen: function( evt, ui ) {
		// south
		if ( ui.offset.top < -Scroller.viewHeight + Scroller.pad ) {
			if ( ui.offset.left < -Scroller.viewWidth + Scroller.pad ) ScreenPosition = 'screen4'; // east
			else ScreenPosition = 'screen3'; // west
		}
		
		// north
		else {
			if ( ui.offset.left < -Scroller.viewWidth + Scroller.pad ) ScreenPosition = 'screen2'; // east
			else ScreenPosition = 'screen1'; // west
		}
	},
	
	updateCSS: function(){
		
		// reset window
		Scroller.siteView.css({ width: $(window).width(), height: $(window).height() });
		
		// math
		Scroller.widthDif = Scroller.viewWidth - $(window).width();
		Scroller.heightDif = Scroller.viewHeight - $(window).height();
		
		// reset X axis
		Scroller.scroll.width( Scroller.scroll.width() + Scroller.widthDif );
		Scroller.scroll.css('left',  parseInt( Scroller.scroll.css('left') ) - Scroller.widthDif );
		Scroller.page.css('left',  parseInt( Scroller.page.css('left') ) + Scroller.widthDif );
		
		// reset Y axis
		Scroller.scroll.height( Scroller.scroll.height() + Scroller.heightDif );
		Scroller.scroll.css('top',  parseInt( Scroller.scroll.css('top') ) - Scroller.heightDif );
		Scroller.page.css('top',  parseInt( Scroller.page.css('top') ) + Scroller.heightDif );

	}
};

var Addresser = {
	
	home: true,
	reIEhack: new RegExp( "^/?" + $.address.baseURL() ),
	fadeSpeed: 700,
	
	init: function() {
		
		var landingClass = $('body').hasClass('home');
		
		if ( landingClass ) {
			
			$.address.strict( true );
			
			$.address.change(function(evt) {
				var href = '/home' + evt.value.replace( Addresser.reIEhack, '' );
				$('body').append('<div id="loading"></div>');
				$('#loading').css({ top: parseInt( ( $(window).height() / 2 ) - 16 ) + 'px', left: parseInt( ( $(window).width() / 2 ) -16 ) + 'px' });
				$.ajax({
					type: 'GET',
					url: href,
					dataType: 'html',
					success: function( page, textStatus ){
						var newContent = $(page).find('#content');
						var contentClass = newContent.attr('class');
						$.address.title( $(page).find('#content').attr('pageTitle') );
						if ( contentClass ) {
							switch( contentClass ) {
								case 'category':
									if ( href != '/home/' ) ExploreLinks.goToCategory();
									$('#screen3').fadeOut( Addresser.fadeSpeed, function() {
										$('#screen3').html( newContent.html() );
										$('#screen3').fadeIn( Addresser.fadeSpeed, function() {
											if ( navigator.appName == 'Microsoft Internet Explorer' ) this.style.removeAttribute('filter'); 
										} );
									} );
									break;
								case 'post':
									ExploreLinks.goToArticle();
									$('#screen4').fadeOut( Addresser.fadeSpeed, function() {
										$('#screen4').html( newContent.html() );
										$('#screen4').fadeIn( Addresser.fadeSpeed, function(){
											if ( navigator.appName == 'Microsoft Internet Explorer' ) this.style.removeAttribute('filter'); 
										} );
									} );
									break;
								case 'page':
									ExploreLinks.goToArticle();
									$('#screen4').fadeOut( Addresser.fadeSpeed, function() {
										$('#screen4').html( newContent.html() );
										$('#screen4').fadeIn( Addresser.fadeSpeed, function(){
											if ( navigator.appName == 'Microsoft Internet Explorer' ) this.style.removeAttribute('filter'); 
										} );
									} );
									break;
								default:
									break;
							}
						}
					},
					complete: function( XMLHttpRequest, textStatus ) {
						$('#loading').remove();
					}
				});
				return href;
			});
			
			$("a[class!=outside]").live( 'click', function() {
				if ( $(this).parents('#lotusNav').length == 0 ) {
					$.address.value( $(this).attr('href').replace( Addresser.reIEhack, '' ) );
				}
				return false;
			});
			
		}
		
	},
	
	isHome: function() {
		if ( $.address.path() != '/home/' ) {
			Addresser.home = false;
		}
		return Addresser.home;
	}
	
};

var ExploreLinks = {
	speed: 1600,
	init: function() {
		$('body.home a.explore').live( 'click', ExploreLinks.goToExplore );
		$('#categoryList li a').live( 'click', ExploreLinks.goToCategory );
		$('body.home a.articleLink').live( 'click', ExploreLinks.goToArticle );
		$('a.videoLink').live( 'click', ExploreLinks.goToVideo );
	},
	goToExplore: function(evt){
		var newLeft = 0; 
		if ( Scroller.widthDif > 0  ) {
			newLeft = Scroller.widthDif;
		}
		$('#page').stop().animate( { left: newLeft + 'px', top: parseInt( Scroller.viewHeight + Scroller.heightDif ) + 'px' }, ExploreLinks.speed, 'swing', function(){
			FixedLotusNav.update();
		} );
		ScreenPosition = 'screen2';
		return false;
	},
	goToCategory: function(evt){
		var newLeft = 1200;
		if ( Scroller.widthDif < 0  ) {
			newLeft = newLeft + Scroller.widthDif;
		}
		$('#page').stop().animate( { left: newLeft + 'px', top: parseInt( Scroller.viewHeight + Scroller.heightDif - 900 ) + 'px' }, ExploreLinks.speed, 'swing', function(){
			FixedLotusNav.update();
		} );
		ScreenPosition = 'screen3';
	},
	goToArticle: function(evt){
		var newLeft = 0; 
		if ( Scroller.widthDif > 0  ) {
			newLeft = Scroller.widthDif;
		}
		$('#page').stop().animate( { left: newLeft + 'px', top: parseInt( Scroller.viewHeight + Scroller.heightDif - 900 ) + 'px' }, ExploreLinks.speed, 'swing', function(){
			FixedLotusNav.update();
		} );
		ScreenPosition = 'screen4';
	},
	goToVideo: function(evt){
		var newLeft = 1200;
		newLeft = newLeft + Scroller.widthDif;
		$('#page').stop().animate( { left: newLeft + 'px', top: parseInt( Scroller.viewHeight + Scroller.heightDif ) + 'px' }, ExploreLinks.speed, 'swing', function(){
			FixedLotusNav.update();
		} );
		ScreenPosition = 'screen1';
		return false; 
	}
};

var Styles = {
	init: function() {
		
		// make first word in post title bold
		$('#screen2 h2 a, #screen3 h3 a').each(function(){
			var me = $(this);
			me.html( me.text().replace(/(^\w+)/,'<strong>$1</strong>') );
		});
		
		// position categories on tree
		$('#screen2 #categoryList li:nth-child(1)').css({ position: 'absolute', top: '170px', left: '30px' });
		$('#screen2 #categoryList li:nth-child(2)').css({ position: 'absolute', top: '88px', left: '180px' });
		$('#screen2 #categoryList li:nth-child(3)').css({ position: 'absolute', top: '227px', left: '359px' });
		$('#screen2 #categoryList li:nth-child(4)').css({ position: 'absolute', top: '76px', left: '357px' });
		$('#screen2 #categoryList li:nth-child(5)').css({ position: 'absolute', top: '20px', left: '56px' });
	}
};

var LotusNav = {
	init: function(){
		$('#blogNav li').bind( 'mouseenter', function(evt){
			$(this).children().stop( true, true ).fadeIn(500);
		} );
		$('#blogNav li').bind( 'mouseleave', function(evt){
			$(this).children().stop( true, true ).fadeOut(2000);
		} );
	}
};

var FixedLotusNav = {
		
	nav: null,
	navNorth: null,
	navEast: null,
	navSouth: null,
	navWest: null,
	navNorthWest: null,
	navSouthWest: null,
	navSouthEast: null,
	navNorthEast: null,
	
	moving: false,
	increment: 20,
	timerMS: 20,
	scrollTimer1: null,
	scrollTimer2: null,
	clickTimerMS: 180,
	clickTimer: null,
	
	init: function() {
		
		FixedLotusNav.nav = $('#lotusNav');
		FixedLotusNav.navNorth = $('#navNorth a');
		FixedLotusNav.navEast = $('#navEast a');
		FixedLotusNav.navSouth = $('#navSouth a');
		FixedLotusNav.navWest = $('#navWest a');
		FixedLotusNav.navNorthWest = $('#navNorthWest a');
		FixedLotusNav.navSouthWest = $('#navSouthWest a');
		FixedLotusNav.navSouthEast = $('#navSouthEast a');
		FixedLotusNav.navNorthEast = $('#navNorthEast a');
		
		FixedLotusNav.nav.find('li a').bind( 'mouseout', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '0px 0px' });
		});
		
		$('#navFB').bind( 'mouseover', function(evt){
			$('#fbtwNav').css({ backgroundPosition: '0px -40px' });
		});
		$('#navTW').bind( 'mouseover', function(evt){
			$('#fbtwNav').css({ backgroundPosition: '0px -80px' });
		});
		$('#fbtwNav li a').bind( 'mouseout', function(evt){
			$('#fbtwNav').css({ backgroundPosition: '0px 0px' });
		});
		
		FixedLotusNav.nav.find('li a').bind( 'mouseup', function(evt){
			evt.preventDefault();
			if ( FixedLotusNav.clickTimer ) {
				clearTimeout( FixedLotusNav.clickTimer );
			}
			clearTimeout( FixedLotusNav.scrollTimer1 );
			clearTimeout( FixedLotusNav.scrollTimer2 );
			Scroller.stopDrag(
				{}, 
				{ offset: { 
					top: parseInt( Scroller.scroll.css('top') ) + parseInt( Scroller.page.css('top') ), 
					left: parseInt( Scroller.scroll.css('left') ) + parseInt( Scroller.page.css('left') ),
					} 
				}
			);
		});
		
		FixedLotusNav.navNorth.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollNorth(); 
			}, FixedLotusNav.clickTimerMS );
		});
		FixedLotusNav.navEast.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollEast(); 
			}, FixedLotusNav.clickTimerMS );
		});
		FixedLotusNav.navSouth.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollSouth(); 
			}, FixedLotusNav.clickTimerMS );
		});
		FixedLotusNav.navWest.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollWest(); 
			}, FixedLotusNav.clickTimerMS );
		});
		
		FixedLotusNav.navNorthEast.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollNorthEast(); 
			}, FixedLotusNav.clickTimerMS );
		});
		FixedLotusNav.navSouthEast.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollSouthEast(); 
			}, FixedLotusNav.clickTimerMS );
		});
		FixedLotusNav.navSouthWest.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollSouthWest(); 
			}, FixedLotusNav.clickTimerMS );
		});
		FixedLotusNav.navNorthWest.bind( 'mousedown', function() {
			FixedLotusNav.clickTimer = setTimeout( function() {
				FixedLotusNav.moving = true;
				FixedLotusNav.scrollNorthWest(); 
			}, FixedLotusNav.clickTimerMS );
		});
		
		FixedLotusNav.update();
	},
	
	scrollNorth: function(){
		var maxScrollTop = Scroller.viewHeight + Scroller.heightDif;
		if ( FixedLotusNav.moving ) {
			var newTop = parseInt( Scroller.page.css('top') ) + FixedLotusNav.increment;
			if ( newTop > maxScrollTop ) newTop = maxScrollTop;
			if ( newTop <= maxScrollTop ) {
				Scroller.page.stop().css( 'top', newTop + 'px' );
				if ( newTop != maxScrollTop ) FixedLotusNav.scrollTimer1 = setTimeout( FixedLotusNav.scrollNorth, FixedLotusNav.timerMS );
			}
		}
	},
	
	scrollNorthEast: function(){
		if ( FixedLotusNav.moving ){
			FixedLotusNav.scrollNorth();
			FixedLotusNav.scrollEast();
		}
	},
	
	scrollEast: function(){
		var maxScrollLeft = 0;
		if ( FixedLotusNav.moving ) {
			var newLeft = parseInt( Scroller.page.css('left') ) - FixedLotusNav.increment;
			if ( newLeft < maxScrollLeft ) newLeft = maxScrollLeft;
			if ( newLeft >= maxScrollLeft ) {
				Scroller.page.stop().css( 'left', newLeft + 'px' );
				if ( newLeft != maxScrollLeft ) FixedLotusNav.scrollTimer2 = setTimeout( FixedLotusNav.scrollEast, FixedLotusNav.timerMS );
			}
		}
	},
	
	scrollSouthEast: function(){
		if ( FixedLotusNav.moving ){
			FixedLotusNav.scrollSouth();
			FixedLotusNav.scrollEast();
		}
	},
	
	scrollSouth: function(){
		var maxScrollBottom = 0;
		if ( FixedLotusNav.moving ) {
			var newTop = parseInt( Scroller.page.css('top') ) - FixedLotusNav.increment;
			if ( newTop < maxScrollBottom ) newTop = maxScrollBottom;
			if ( newTop >= maxScrollBottom ) {
				Scroller.page.stop().css( 'top', newTop + 'px' );
				if ( newTop != maxScrollBottom ) FixedLotusNav.scrollTimer1 = setTimeout( FixedLotusNav.scrollSouth, FixedLotusNav.timerMS );
			}
		}
	},
	
	scrollSouthWest: function(){
		if ( FixedLotusNav.moving ){
			FixedLotusNav.scrollSouth();
			FixedLotusNav.scrollWest();
		}
	},
	
	scrollWest: function(){
		var maxScrollLeft = Scroller.viewWidth + Scroller.widthDif;
		if ( FixedLotusNav.moving ) {
			var newLeft = parseInt( Scroller.page.css('left') ) + FixedLotusNav.increment;
			if ( newLeft > maxScrollLeft ) newLeft = maxScrollLeft;
			if ( newLeft <= maxScrollLeft ) {
				Scroller.page.stop().css( 'left', newLeft + 'px' );
				if ( newLeft != maxScrollLeft ) FixedLotusNav.scrollTimer2 = setTimeout( FixedLotusNav.scrollWest, FixedLotusNav.timerMS );
			}
		}
	},
	
	scrollNorthWest: function(){
		if ( FixedLotusNav.moving ){
			FixedLotusNav.scrollNorth();
			FixedLotusNav.scrollWest();
		}
	},
	
	bindCorners: function() {
		FixedLotusNav.navSouthEast.bind( 'click', function(evt){
			if ( !FixedLotusNav.moving ) {
				ExploreLinks.goToArticle();
			}
			FixedLotusNav.moving = false;
			return false;
		});
		FixedLotusNav.navNorthEast.bind( 'click', function(evt) {
			if ( !FixedLotusNav.moving ) {
				ExploreLinks.goToExplore();
			}
			FixedLotusNav.moving = false;
		});
		FixedLotusNav.navSouthWest.bind( 'click', function(evt){
			if ( !FixedLotusNav.moving ) {
				ExploreLinks.goToCategory();
			}
			FixedLotusNav.moving = false;
			return false;
		});
		FixedLotusNav.navNorthWest.bind( 'click', function(evt){
			if ( !FixedLotusNav.moving ) {
				ExploreLinks.goToVideo();
			}
			FixedLotusNav.moving = false;
		});
	},
	
	bindMouseOvers: function(){
		
		FixedLotusNav.navNorth.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-155px 0px' });
		});
		FixedLotusNav.navNorthEast.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-310px 0px' });
		});
		FixedLotusNav.navEast.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-465px 0px' });
		});
		FixedLotusNav.navSouthEast.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-620px 0px' });
		});
		FixedLotusNav.navSouth.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-775px 0px' });
		});
		FixedLotusNav.navSouthWest.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-930px 0px' });
		});
		FixedLotusNav.navWest.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-1085px 0px' });
		});
		FixedLotusNav.navNorthWest.bind( 'mouseover', function(evt){
			FixedLotusNav.nav.css({ backgroundPosition: '-1240px 0px' });
		});
		
		var maxScrollTop = Scroller.viewHeight + Scroller.heightDif;
		var maxScrollRight = 0;
		var maxScrollBottom = 0;
		var maxScrollLeft = Scroller.viewWidth + Scroller.widthDif;
		
		var onNorthEdge = parseInt( Scroller.page.css('top') ) >= maxScrollTop;
		var onEastEdge =  parseInt( Scroller.page.css('left') ) <= maxScrollRight;
		var onSouthEdge = parseInt( Scroller.page.css('top') ) <= maxScrollBottom;
		var onWestEdge = parseInt( Scroller.page.css('left') ) >= maxScrollLeft;
		
		if ( onNorthEdge ) FixedLotusNav.navNorth.unbind( 'mouseover' );
		if ( onEastEdge ) FixedLotusNav.navEast.unbind( 'mouseover' );
		if ( onSouthEdge ) FixedLotusNav.navSouth.unbind( 'mouseover' );
		if ( onWestEdge ) FixedLotusNav.navWest.unbind( 'mouseover' );
		if ( onNorthEdge && onWestEdge ) FixedLotusNav.navNorthWest.unbind( 'mouseover' );
		if ( onNorthEdge && onEastEdge ) FixedLotusNav.navNorthEast.unbind( 'mouseover' );
		if ( onSouthEdge && onEastEdge ) FixedLotusNav.navSouthEast.unbind( 'mouseover' );
		if ( onSouthEdge && onWestEdge ) FixedLotusNav.navSouthWest.unbind( 'mouseover' );
	},
	
	update: function(){
		
		FixedLotusNav.nav.find('li a').unbind( 'mouseover' );
		FixedLotusNav.nav.find('li a').unbind( 'click' );
		FixedLotusNav.bindMouseOvers();
		
		switch( ScreenPosition ) {
			case 'screen1':
				FixedLotusNav.navNorth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToVideo();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navWest.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToVideo();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navEast.bind( 'click', function(evt) {
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToExplore();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navSouth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToCategory();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.bindCorners();
				break;
			case 'screen2':
				FixedLotusNav.navEast.bind( 'click', function(evt) {
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToExplore();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navNorth.bind( 'click', function(evt) {
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToExplore();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navWest.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToVideo();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navSouth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToArticle();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.bindCorners();
				break;
			case 'screen3':
				FixedLotusNav.navWest.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToCategory();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.navSouth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToCategory();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.navNorth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToVideo();
					}
					FixedLotusNav.moving = false;
				});
				FixedLotusNav.navEast.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToArticle();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.bindCorners();
				break;
			case 'screen4':
				FixedLotusNav.navEast.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToArticle();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.navSouth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToArticle();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.navNorth.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToExplore();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.navWest.bind( 'click', function(evt){
					if ( !FixedLotusNav.moving ) {
						ExploreLinks.goToCategory();
					}
					FixedLotusNav.moving = false;
					return false;
				});
				FixedLotusNav.bindCorners();
				break;
			default:
				
				break;
		}
	}
	
};

