/**
 * .disableTextSelect - Disable Text Select Plugin
 * http://www.jdempster.com/category/jquery/disabletextselect/
 **/
(function($){if($.browser.mozilla){$.fn.disableTextSelect=function(){return this.each(function(){$(this).css({"MozUserSelect":"none"})})};$.fn.enableTextSelect=function(){return this.each(function(){$(this).css({"MozUserSelect":""})})}}else{if($.browser.msie){$.fn.disableTextSelect=function(){return this.each(function(){$(this).bind("selectstart.disableTextSelect",function(){return false})})};$.fn.enableTextSelect=function(){return this.each(function(){$(this).unbind("selectstart.disableTextSelect")})}}else{$.fn.disableTextSelect=function(){return this.each(function(){$(this).bind("mousedown.disableTextSelect",function(){return false})})};$.fn.enableTextSelect=function(){return this.each(function(){$(this).unbind("mousedown.disableTextSelect")})}}}})(jQuery)

var catalog_photo_is_loading = false;


jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}

$(function(){
	//document.oncontextmenu = function() {return false;}
	$("body").bind('copy paste cut select', function(e){
	    e.preventDefault();
    });
	//$('p, img, h2, h1, h3').disableTextSelect();
	//$('div.container').enableTextSelect();
	$('#scroller a').hide();
    //$('.header h1 a').pngfix();
    


	$('body:not(.main) div.slides .slide').removeClass('hide').css('opacity','0').first().animate({opacity:1}).appendTo('.slides');
	$('div.scrollpane').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 11,  autoReinitialise: true});
	$('a.toggler').click(function(e){
        e.preventDefault();
        //$(this.hash).toggleClass('hidden');
        //$('div.scrollpane').jScrollPane();
    });
	
	$('#scroller').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 11,  autoReinitialise: true});

    // Main page
    if($('body').hasClass('main')){
    $('body.main div.slides .slide').css('display','none').css('opacity','0');
    $('body.main div.slides .slides-bg').addClass('loading');
    var mainpage_photos = [
        '/i/slides/1.jpg',
        '/i/slides/2.jpg',
        '/i/slides/4.jpg',
        '/i/slides/6.jpg',
        '/i/slides/7.jpg'
    ];
    var mainpage_photos_loaded = 0;
    
    $.each (mainpage_photos, function (index, el) {
        var img = new Image();
        
        $(img).load(function () {
			mainpage_photos_loaded++;
            if(mainpage_photos_loaded == mainpage_photos.length) {
                $('body.main div.slides .slides-bg').removeClass('loading');
                $('body.main div.slides .slide').css('display','block').first().animate({opacity:1}, 2000);
                setInterval(nextSlide, 5000);
            }
		}).error(function () {
			// notify the user that the image could not be loaded
		}).attr('src', el);
	});
    }
    //setInterval(nextSlide, 5000);


    // Collection
    
	$('a.gallery_thumb').click(function(e){
		e.preventDefault();
		if(catalog_photo_is_loading) {
		    return;
		}
		catalog_photo_is_loading = true;
		var thumb = $(this);
		var holder = $('#bigphoto');
		holder.empty().addClass('slides-bg-loading');
		$('a.gallery_thumb.active').removeClass('active');
		thumb.addClass('active');
		

        var img = new Image();
		$(img).load(function () {
			holder.empty();
            $(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $('#bigphoto').removeClass('slides-bg-loading').append(this);
            if( ! $.browser.msie) {
			    $(this).fadeIn();
		    } else {
		        $(this).show();
		    }
		    catalog_photo_is_loading = false;
			    
			//$("#bigphoto img").pngfix(); //Using PNG-8, not needed now
			if(window.DD_belatedPNG) {
				DD_belatedPNG.fix(this);
			}
//			DD_belatedPNG.fix(this);
		}).error(function () {
			// notify the user that the image could not be loaded
		}).attr('src', thumb.attr('rel'));
	});
	galleryToggler(window.location.hash);
	$('.gallery_togglers a').click(function(e){
		e.preventDefault();
		galleryToggler($(this).attr('hash'));
		$('.gallery_togglers li.active').removeClass('active');
		$(this).parent().addClass('active');
	});
});

function galleryToggler(hash) {
	$('li.active').removeClass('active');
	switch (hash)
		{
		case '#girl':
			$('#scroller a.girl').fadeIn();
			$('#scroller a.boy').hide();
			$('a.girl').first().click();
			$(hash+'-link').addClass('active');
			break;
		case '#boy':
			$('#scroller a.girl').hide();
			$('#scroller a.boy').fadeIn();
			$('a.boy').first().click();
			$(hash+'-link').addClass('active');
			break;
		default:
			$('#scroller a.girl').fadeIn();
			$('#scroller a.boy').fadeIn();
			$('a.gallery_thumb').first().click();
			$('#all-link').addClass('active')
			break;
	}
	$('#scroller').jScrollPane({showArrows:true, scrollbarWidth: 11, arrowSize: 11, autoReinitialise: true});
	/*$("#scroller img").lazyload({         
         placeholder : "js/grey.gif",
         container: $("div.jScrollPaneContainer"),
         threshold : 100,
    });*/
}

function nextSlide() {
	$('.slides div.slide').first().animate({opacity:1}).appendTo('.slides').siblings('.slide').animate({opacity:0});
}
