/**
 * @author amit ashckenazi obviocity@gmail.com
 * http://dean.edwards.name/packer/
 */
var slideShowTimer, intervalTime = 15000;

$(document).ready(function(){
	$('div#galleryWrapper').hover(
		function () {
			if ($.browser.msie) {
				$('div.caption').fadeTo(500,0.6);
 			} else {
				$('div.caption').fadeIn();
			}
			$('div.caption h3').css({marginLeft: '-50px'}).animate({marginLeft:0},{queue:false, duration:150});
			$('div.caption a.frontPageMoreLink').css({right: '72px', opacity:.1}).animate({right:'36px',opacity:1},{queue:false, duration:250});
			if( $('div.caption p').css('opacity') <= 0.1  ){
				$('div.caption p').css({marginLeft: '-50px', opacity:.1}).animate({marginLeft:0, opacity:1},{queue:false, duration:300});
			}
			if($('ul#galleryControls').length > 0 && $('ul#galleryControls').is(':hidden') ){
				$('ul#galleryControls').hide().delay(50).fadeIn(500);
			}	
		}, 
		function () {
			if ($.browser.msie) {
				$('div.caption').fadeOut();
 			} else {
				$('div.caption').fadeOut();
			}
			$('div.caption h3').animate({marginLeft:'-50px'},{queue:false, duration:300});
			$('div.caption p').animate({marginLeft:'-50px', opacity:.1},{queue:false, duration:300});
			$('div.caption a.frontPageMoreLink').animate({right:'72px',opacity:0},{queue:false, duration:300});
			$('ul#galleryControls').fadeOut(300);
		}
	);
	/*hover effect on thumb in all projects page*/
	$('a.postThumb').hover(
		function () {
			$('img', this).animate({opacity: 0.1}, {queue:false, duration:150});
		}, 
		function () {
			$('img', this).animate({opacity: 1.0}, {queue:false, duration:500});
		}
	);	
	
	
	/*GALLERIA FOR HOME PAGE*/
    if ($('body').is('.home')) {
        $('#gallery').galleria({
            history: false,
            clickNext: true,
            insert: '#img',
            onImage: function(image, caption, thumb){
                image.hide().fadeIn(1000);
                var _li = thumb.parents('li');
                _li.siblings().children('img.selected').fadeTo(1000, 0.3);
                thumb.fadeTo('fast', 1).addClass('selected');
                image.attr('title', 'Next Screenshot &arr;');
            },
            onThumb: function(thumb){
                var _li = thumb.parents('li');
                var _fadeTo = _li.is('.active') ? '1' : '0.6';
                thumb.css({
                    display: 'none',
                    opacity: _fadeTo
                }).fadeIn(1500);
                thumb.hover(function(){
                    thumb.fadeTo('fast', 1);
                }, function(){
                    _li.not('.active').children('img').fadeTo('fast', 0.3);
                });
            }
        });
		
        slideShowTimer = window.setInterval(function(){
            $.galleria.next();
        }, intervalTime);
		
    };
	
	$("a#zoom").fancybox({
		'titleShow' : false,
		'overlayShow' : true,
		'hideOnOverlayClick' : true,
		'hideOnContentClick':true,
		'overlayOpacity':	1,
		'overlayColor': '#000000',
		'padding':0
	});
	
	/*check for gallery elemnt in DOM, if true run the gallery script*/
	if( $('#gallery').length > 0  && $('body').is('.home') == false){
		var imageNum = 1;
		var currentImageNum = 1;
		makeGallery();
	}
	
	/*send links in bookmarks list to a new tab/window*/
	$('ul.links a').attr('target', '_blank');
});

function showProject(which){
	if (which == 'http://contextive.com/works-and-projects/edible-material' ||
	    which == 'http://contextive.com/works-and-projects/karin-aviaz' || 
	    which == 'http://contextive.com/works-and-projects/martin-casares-libros-arte' ||
	    which == 'http://contextive.com/works-and-projects/fricciones-universales-site-shopping-cart-flex' ){
	    clearInterval( slideShowTimer );
		window.location = which.toString();
	} else if (which !== 'Projects\' Fast Access / Acceso Rapido / גישה מהירה'){
		clearInterval( slideShowTimer );
		$('#gallery li img').each( function(){
			if ( $(this).hasClass(which) ){
				$.galleria.activate( $(this).attr('src') );
				slideShowTimer = window.setInterval(function(){
            		$.galleria.next();
        		}, intervalTime);
			}
		});
	} 
}

/*creates the gallery*/
function makeGallery(){
		$('#galleryNext').click(function(){
        	galleriaPrev();
    	});
    	$('#galleryPrev').click(function(){
        	galleriaNext();
    	});	
    $('#gallery').galleria({
        insert: '#img',
        history: false,
        onImage: function(image, caption, thumb){
            image.hide().fadeIn(500);
            thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.6);
            $('#img').trigger('image-loaded');
            updateIndex();
			$('a#zoom').attr('href',  image.attr('src'));
        },
        onThumb: function(thumb){
            var $li = thumb.parent(), opacity = $li.is('.active') ? 1 : 0.6;
            $li.hover(function(){
                $li.fadeTo(200, 1);
            }, function(){
                $li.not('.active').fadeTo(200, opacity);
            })
        }
    }).delay(500).find('li:first').addClass('active');
   currentImageNum = 1;
   imageNum = $('#gallery li').length;
   $('#galleryCounter').replaceWith('<div id="galleryCounter">&nbsp;&nbsp;' + currentImageNum + '/' + imageNum + '&nbsp;</div>');
   $('#galleryControls').fadeIn();  
};

function galleriaNext(){
	$.galleria.next();
	updateIndex();
};

function galleriaPrev(){
	$.galleria.prev();
	updateIndex();
};

function updateIndex(){
	$('.caption').hide().fadeIn();
	var theIndex = $('li.active').index()+1;
	if (theIndex <= 9){
		preImageNum = '&nbsp;&nbsp;';
	} else {
		preImageNum = '&nbsp;';
	}
	$('#galleryCounter').replaceWith('<div id="galleryCounter">' + preImageNum + theIndex + '/' + imageNum + '&nbsp;</div>');
};