$(document).ready(function(){
	
	jQuery.easing.def = "easeInExpo";
	
	$('#image').load(function() {
        $('#image_loader').hide();
	    $(this).animate({'opacity': 100}, 800,function() {
            $('#image_loader').hide(); // ie8 need this;
	    });
	});
	function doNothing(e) {
	    e.preventDefault();
	}
	function showAd(e){
    	e.preventDefault();
    	old = $('#image').attr('src');
        $("#image").attr("src", $(this).attr('href'));
        $(this).attr('href',old);
    }
	function loadPoster(e) {
		e.preventDefault();
		folder = $(e.currentTarget).attr('href');
		$('#image').animate({'opacity':0},800, function() {
    		$('#image').attr('src',folder+'image.jpg');
    		$('#image_loader').show();
		});
		$('#text_loader').show();
		$('#desc').hide();
		$('#desc').load(folder+'desc.html', function() {
			Cufon.replace('#desc .cufon',{hover: true});
        	$('#next,#prev').hover(function() {
        	    $(this).addClass('hover');
        	}, function() {
        	    $(this).removeClass('hover');
        	});
			$('#pagination a').click(loadPoster);
			$('#text_loader').hide();
			$('#desc').show();
			$("#viewAD").hover(showAd,showAd);
			$("#viewAD").click(doNothing);
		})
	}
	function showGallery() {
	    $('#showroom').animate({left: 30}, 400, function() {
			window.location.hash = '#gallery';
			$('#home').show();
		});
		$('#poster').animate({left: singleStart}, 800);

		if (!$('#thumbs')[0]) {
			$('#showroom .loader').show();
			$('#showroom .content').load('gallery.html', function() {
				$('#showroom .loader').fadeOut();
				Cufon.replace('#showroom .cufon');
				$('.thumb a').click(function(e) {
				    $('#image').css({'opacity':0});
				    $('#image').attr('src','');
					$('#single').click();
					loadPoster(e);
				});
			});
		}
	}
	$('#head .poster').click(function(e) {
		$('#image').attr('src','');
		$('#gallery').click();
		$('#single').click();
		loadPoster(e);
	});
	
    $('#see').click(function(e) {
		e.preventDefault();
		$('body').css({'overflow-y':'hidden'});
        calculateLefts();
		showGallery();
    });
	$('#gallery').click(function(e) {
		e.preventDefault();
		if (current_pane != PANES.SHOWROOM) {
    		$('body').css({'overflow-y':'hidden'});
 			current_pane = PANES.SHOWROOM;
            calculateLefts();
			showGallery();
		} else {
			current_pane = PANES.HOME;
            calculateLefts();
			$('#showroom').animate({left: galLeft}, 1000, function() {
    			$('#home').hide();
    			$('body').css({'overflow-y':'auto'});
    			$(window).resize();
    			window.location.hash = '#home';
			});
			$('#poster').animate({left: singleLeft}, 400);
		}
	});

	$('#single').click(function(e) {
		e.preventDefault();
		$('#home').show();
		if (current_pane != PANES.POSTER) {
			current_pane = PANES.POSTER;
            calculateLefts();
    		$('body').css({'overflow-y':'hidden'});
			$('#poster').animate({left: 60}, 400);
		} else {
			current_pane = PANES.SHOWROOM;
            calculateLefts();
			$('#poster').animate({left: singleLeft}, 400);
			window.location.hash = '#gallery';
		}
	});
	
	$("#home").click(function(e){
		e.preventDefault();
		$('body').css({'overflow-y':'auto'});
		current_pane = PANES.HOME;
        calculateLefts();
		$('#showroom').animate({left: galLeft}, 1000, function() {
			$('#home').hide();
			$(window).resize();
			window.location.hash = '#home';
		});
		$('#poster').animate({left: singleLeft}, 400);
	});
	
	$('#rules_btn').click(function(e) {
	    e.preventDefault();
	    $('#rules').show();
		$(window).resize();
		$('body').scrollTo('#rules', 1000,{axis:'y'});
	});
	
	if ($.browser.mozilla) {
	    $('body').addClass('mozilla');
	} else if ($.browser.safari) {
	    $('body').addClass('safari');
	} else if ($.browser.msie) {
	    v = $.browser.version.split('.');
	    if (parseInt(v[0]) < '7') {
	        window.location = 'noie6.html';
	    }
	    $('body').addClass('msie');
	}
	if (window.location.hash) {
		el = window.location.hash;
		window.location.hash = null;
		$(el).click();
	}
	
	$('#next,#prev').hover(function() {
	    $(this).addClass('hover');
	}, function() {
	    $(this).removeClass('hover');
	});
	$('#pagination a').click(loadPoster);
	$("#viewAD").hover(showAd,showAd);
	$("#viewAD").click(doNothing);
});

