/*
____________________________________________________________

• AUTHOR	gabriel madison
• WEB		ohsoso.com
• EMAIL		info@ohsoso.com
• CLIENT	fredrik bäckar
• VERSION	1.3
____________________________________________________________
*/
document.documentElement.className += " js";

$(document).ready(function() {

	// INITIALISE
	
	$('.hide').hide();
	
	$(window).load(function() { 
			$('#loading').delay(2000).fadeOut(400);
	});

	$('img').one('load', function() {
			$(this).fadeIn(1000);
	})
	.each(function(){
			if(this.complete) $(this).trigger('load');
	});
	
	// VARS, IE FIXED
	
	var thumbs;
	var prev;
	var next;
	init	= $('#thumbs li');
	init_a	= $('#awards li');
	i		= 0;
	thumbs 	= $('#thumbs li').not('.grey');
	next 	= $('.next');
	prev 	= $('.prev');
	imageIndex = 0;

//	$('.cptn', thumbs).css( { opacity: 0.85 } );
	$(init).hide();
	$('.home #logo').animate( { top: '29px' }, 100);

	(function() {
			$(init[i++]).slideToggle(100, arguments.callee);
	})();

/*	$(init_a).hide();
	(function() {
			$(init_a[i++]).slideToggle(100, arguments.callee);
	})();
*/
	// SHOW STUFF
	
	$(thumbs).hover(function() {
			$('.cptn', this).not('.act').stop(true, true).slideDown(100);
	}, function(){
			$('.cptn', this).not('.act').stop(true, true).delay(320).slideUp(200);
	});
		
	// MENU
	
	$('#nav .nav_top').click(function() {
			$('ul', this).stop(true, true).slideToggle(200);
			$(this).toggleClass('nav_up');
	});
	
	// MOVIE VIEW
	
	thumbs.click(function() {
			prev.show();
			next.show();
			movpath = $('img', this).attr('alt');
			window.location.hash = movpath;
			$('#loading').show();
			$('#movie').slideDown(300);
			$('#logo').animate( { top: '29px' }, 100);
			$('html,body').delay(300).animate({scrollTop: 0}, 300);
			$('#thumbs li .cptn').removeClass('act').slideUp(200);
			$('.cptn', this).addClass('act').slideDown(100);
			$.ajax({
					type: "GET",
					url: "/inc/mov.php",
					data: "movie=" + movpath,
					success: function(vimeo) {
							$("#movie").html(vimeo);
							$('#loading').delay(3000).fadeOut(400);
					}
			});
			imageIndex = thumbs.index(this);
			return false;
	});
			
	next.click(function() {
			if (imageIndex !== thumbs.length) { 
					imageIndex++;
					thumbs.eq(imageIndex).click();
			} else {
					imageIndex = 0;
					thumbs.eq(imageIndex).click();
			}
			return false;
	});
			
	prev.click(function() {
					imageIndex--;
					thumbs.eq(imageIndex).click();
			return false;
	});
	
// KEYBOARD NAVIGATION
	
	$(document.documentElement).keydown(function(event) {
			if (event.keyCode == 37) {
					prev.click();
			} else if (event.keyCode == 39) {
					next.click();
			} else if (event.keyCode == 27) {
					prev.hide();
					next.hide();
					$('#thumbs li .cptn').removeClass('act').slideUp(200);
					$('#movie').slideUp(300);
					$('#logo').delay(200).animate( { top: 0 }, 100);
					window.location.hash = '';
					$.ajax({
							type: "GET",
							url: "/inc/mov.php",
							data: "movie=''",
							success: function(vimeo) {
									$("#movie").html(vimeo);
							}
					});
			return false;
			}
	});
	
	// ADMIN NEW CATEGORY

	$('#txtcat').change(function() {
			if ($(this).val() == 'new') {
					$('#newcat').show();
			}
			else {
					$('#newcat').hide();
			}
	});
	
	// ADMIN CHANGE PAGE TO EDIT
	
	$(function() {
			$("#txt_editmenu").change(function() {
			window.location = '/admin' + this.value
			});
			$("#txt_edit").change(function(){
			window.location = '/admin' + this.value
			});
	});
	
	// ADMIN SELECT IMAGE

	$('.col_2.movs img').hover(function() {
			$('#sel_mov').text(this.title);
	}, function(){
			$('#sel_mov').text('Select video');
	});

	$('.col_2.awards img').hover(function() {
			$('#sel_award').text(this.title);
	}, function(){
			$('#sel_award').text('Select award');
	});

	// 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) {
			movpath = window.location.hash.replace('#','');
//			imageIndex = $(thumbs).index($('li:has(a[href="' + movpath + '"])'));
//		imageIndex = $(thumbs).index($('#thumbs li').hasClass(movpath));
			// imageIndex = $(thumbs).   index($('li:has(a[href="' + imgpath2 + '"])'));
	//		$(thumbs).hasClass(movpath).children('.cptn').slideDown(100);
			$('#movie').delay(1200).slideDown(300);
			$('#logo').animate( { top: '29px' }, 100);
			prev.show();
			next.show();
			$.ajax({
					type: "GET",
					url: "/inc/mov.php",
					data: "movie=" + movpath,
					success: function(vimeo) {
							$("#movie").html(vimeo);
							$('#loading').delay(3000).fadeOut(400);
					}
			});
	};


return false;

});
