/*

--------------------------------------------------------->8---

AUTHOR		g madison / ohsoso.com
CLIENT		crille forsberg
VERSION		1.6 new orient modes, scrolltop, clean rel
DATE		110420

--------------------------------------------------------------

*/

document.documentElement.className += " js";

$(document).ready(function() {

	// HIDE INITIALLY
	
	$('img, #prev span').hide();
	$('img').one('load', function() {
			$(this).fadeTo(300, 1);
	})
	.each(function(){
			if(this.complete) $(this).trigger('load');
	});
	
	// LOADING MESSAGE

//	$('#sharetop p').html('Loading');
	
	$(window).load(function(){ 
			$('#sharetop p').html('');
	});
	
	// SHOW STUFF
	
	$('#thumbs li').hover(function() {
			$('p', this).fadeIn(50);
	}, function(){
			$('p', this).fadeOut(300);
	});
	
	$('#thumbsimg li').hover(function() {
			$('img', this).fadeTo(50, 0.1);
	}, function(){
			$('img', this).fadeTo(300, 1);
	});
	
	$('#inctxt img, #thumbsimg img').hover(function() {
			$('#txttitle h1').html(this.title);
	}, function(){
			$('#txttitle h1').html('Title');
	});

	$('#logo').hover(function() {
			$('#sharetop p').html('Back to first page');
	}, function(){
			$('#sharetop p').html('');
	});

	// MENU
	
	$('#nav li').hover(function() {
            $('ul', this).fadeIn(50);
 	}, function() {
			$('ul', this).fadeOut(300);
	});
	
	// SHARE
	
	$('#sharetop span').hover(function () {
			$('#sharetop p').html(this.title);
	}, function(){
			$('#sharetop p').html('');
	});

	$('#share span').hover(function () {
			$(this).css('background-image', 'url(../img/facebook_.png');
	}, function(){
			$('#sharetop p').html('');
	});

	// LANDSCAPE MODE PLEASE
	
	window.onorientationchange = function() {
			switch(window.orientation) {
    		case 0:
			$('#txttitle h1').html('Rotate to landscape mode');
			break; 
			case 90:
			$('#txttitle h1').html('Title');
			break;
			case -90: 
			$('#txttitle h1').html('Title');
			break;
			}
	}

	// MOVIE VIEW
	
	$('#thumbs li.box').click(function(){
			$('#sharetop span').fadeTo(300, 0);
			var movpath = $('img', this).attr('alt');
			window.location.hash = movpath;
//			$('p', this).hide();
			$('#thumbs img').fadeTo(50, 1);
			$('img', this).fadeTo(300, 0.1);
			if(!(navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) { 
					document.title = '\u25ba CRILLE FORSBERG FSF, Director of Photography';
					$('#movie').slideDown(300);
					$('html,body').delay(100).animate({scrollTop: 0}, 300);
					setTimeout(function(){
							$('#movholder').load("/inc/mov.php", { 'movie': movpath } );
					}, 1200);
			return false;
			}
	});

	$('#close').click(function() {
			$('#thumbs p').hide();
			window.location.hash = '';
			$("#movholder").load("/inc/mov.php", { 'movie': '' }, function () {
					document.title = 'CRILLE FORSBERG FSF, Director of Photography';
					$('#movie').delay(600).slideUp(300);
					$('#thumbs img').fadeTo(330, 1);
			$('#sharetop span').fadeTo(300, 1);
			});
			return false;
	});

	// REPLAY
	
//	$.replay = function() {
//			window.alert('tadaa');
//			$('#close').click();
//	}

	// IMAGE GALLERY
	
	$('#thumbsimg img').click(function() {
				$('html,body').stop(true, false).animate({ scrollTop: 66 }, 300);
	});

	var thumbs = $('#thumbsimg li'),
		 next = $('#next span'),
		 prev = $('#prev span'),
		 imageIndex = 0;

	thumbs.click(function() {
	
			var imgpath = $('img', this).attr('src');
			var imgtitle = $('img', this).attr('title');
			next.show();
			prev.show();
			
			$('#txttitle h1').html(imgtitle);
			$('img', thumbs).fadeTo(300, 1);
			$('img', this).fadeTo(50, 0.2);
			$('#imglrg').fadeOut(0).attr({ src: imgpath }).load(function() { 
					$(this).fadeIn(300);
			});

			imageIndex = thumbs.index(this);
			return false;
	});
			
	next.click(function() {
			if (imageIndex !== thumbs.length) { 
					imageIndex++;
					thumbs.eq(imageIndex).click();
			} else {
					next.hide();
			}
			return false;
	});
			
	prev.click(function() {
			if (imageIndex !== 0) {
					imageIndex--;
					thumbs.eq(imageIndex).click();
			} else {
					prev.hide();
			}
			return false;
	});
	
	// KEYBOARD NAVIGATION
	
	$(document.documentElement).keyup(function (event) {
			var direction = null;
			if (event.keyCode == 37) {
					prev.click();
			} else if (event.keyCode == 39) {
					next.click();
			}
	});
	
	// ADMIN NEW CATEGORY

	$('#txtcat').change(function() {
			if ($(this).val() == 'new') {
					$('#newcat').show();
			}
			else {
					$('#newcat').hide();
			}
	});
		
	// DISABLE SKYPE
	
	window.setTimeout(function() {
			$('.skype_pnh_container').html('');
			$('.skype_pnh_print_container').removeClass('skype_pnh_print_container');
	}, 300);
		
	// IF DEEP LINKED
	
	if(window.location.hash.length){
			$('#sharetop').fadeTo(300, 0);
			var movpath = window.location.hash.replace('#','');
			if(!(navigator.userAgent.match(/iPhone/i))) { 
					document.title = '\u25ba CRILLE FORSBERG FSF, Director of Photography';
					$('#movie').delay(1200).slideDown(300);
					$('html,body').delay(1300).animate({scrollTop: 0}, 300);
					setTimeout(function(){
							$('#movholder').load("/inc/mov.php", { 'movie': movpath } );
					}, 1200);
			}
//			else {
//			window.location = 'http://www.crilleforsberg.com/new/mov/mobile/' + movpath + '.mov';
//			}
			return false;
	};


return false;

});
