
/********************************************************************************************/
/*                               Oliver Seidel Portfolio                                    */
/********************************************************************************************/

//var site='www.olivermuc.com';
var startPic = -1;
var site=window.location.hostname;

// Viewed on the iPhone
var isMobileWebKit = false; //RegExp(" Mobile/").test(navigator.userAgent);
var mobileWidth = 320;
var curPhotoSet = 1;
var curPhotoPos = 0;
var maxYmove = 0;
var thumbStage = 150;
var thumbHeight = 152;
var MY_moving = false;
var MY_startY;
var MY_movedY;
var urlvars;

/**/
/**/
/**/

jQuery().ready( function () {

	var G;
	Galleria.loadTheme('/js/themes/lightbox/galleria.lightbox.js');

   	$('#contactBtn').click( function(e){
	    $('#middle div').hide();
	    $('#bottom').fadeOut(300);
	    $('#contact').fadeIn(500);

		// record root location
		$.bbq.pushState('');

		// Back Button
        $('#top .back').css({display:'block', opacity:'0'}).animate({'opacity':'1'},500).one('click', function(){

				// record root location
				$.bbq.pushState('');

				$( this ).hide();
			    $('#menu').fadeIn(500);
			    $('#contact').hide();
			    $('#bottom').fadeIn(500);
			});

		return false;
    });

   	$('#menu').click( function(e){

		if( e.target.nodeName.toUpperCase()=='IMG' ){
			switch( e.target.id ){
				case 'mood': getImageSet('mood'); break;
				case 'misc': getImageSet('misc'); break;
				case 'peop': getImageSet('people'); break;
				case 'food': getImageSet('food'); break;
				case 'paws': getImageSet('paws'); break;
				default: break;
			}
		}

		return false;
    });

   	$('#news').click( function(e){
		getImageSet('news');
		return false;
    });

	// iPhone Stuff
/*	if( isMobileWebKit ){
		jQuery('body').bind('orientationchange', updateOrientation).trigger('orientationchange');
        activateDetectFlicks();
	}
	else
*/

	hoverOverSet();

	urlvars = getUrlVars();

	if( urlvars['i'] ){
		startPic = urlvars['i'];
	}
	console.log("> img="+startPic+', '+window.location.href);

	if( window.location.href.match(/#new/) )
		setTimeout( "getImageSet('news',startPic)", 500 );
	else if( window.location.href.match(/#mood/) )
		setTimeout( "getImageSet('mood',startPic)", 500 );
	else if( window.location.href.match(/#food/) )
		setTimeout( "getImageSet('food',startPic)", 500 );
	else if( window.location.href.match(/#misc/) )
		setTimeout( "getImageSet('misc',startPic)", 500 );
	else if( window.location.href.match(/#paws/) )
		setTimeout( "getImageSet('paws',startPic)", 500 );
	else if( window.location.href.match(/#people/) )
		setTimeout( "getImageSet('people',startPic)", 500 );
	
	$(window).bind( 'hashchange', function( event ) {
	  var hash_str = event.fragment;
	  /*,
		param_obj = event.getState(),
		param_val = event.getState( 'param_name' ),
		param_val_coerced = event.getState( 'param_name', true );
	  ...*/
	  if (hash_str=='')
		$('#top .back').trigger('click');
	});	
});

/**/
/**/
/**/

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}

/*
// Capture Flicks on Sets
function activateDetectFlicks() {

	$('#menu a').detectFlicks({ axis: 'x', threshold: 30, flickEvent: function(d) { swipeSets(d); } });
	$('#menu a img').detectFlicks({ axis: 'x', threshold: 30, flickEvent: function(d) { swipeSets(d); } });

}

// Swipe Photo Sets
function swipeSets(d) {
			    if( d.direction=='left2right' && curPhotoSet>1 ){
                    curPhotoSet--;
					jQuery('#menu').animate({left: -(mobileWidth*(curPhotoSet-1)) },500,'easeOutBack');
				}else if( d.direction=='right2left' && curPhotoSet<5 ){
                    curPhotoSet++;
					jQuery('#menu').animate({left: -(mobileWidth*(curPhotoSet-1)) },500,'easeOutBack');
				}
}

// Capture Flicks on Thumbs
function activateDetectFlicksThumbs() {

		$('body').bind('touchstart', MY_touchStart);

}


// Capture touch on thumbnails
function MY_touchMoveY(e) {

    event.preventDefault(); //no scrolling
    MY_movedY = event.targetTouches[0].clientY;

	// must be dynamic based on container size-last page height
	absDelta = Math.abs(Math.abs(MY_movedY) - Math.abs(MY_startY));
    if ( !MY_moving && absDelta > 20 )  {

		direction = (MY_startY - MY_movedY < 0) ? 1:-1;

		newPos = curPhotoPos + (thumbHeight*direction);
		if( newPos <= curPhotoPos )
	    	curPhotoPos = ( newPos < -maxYmove )? -maxYmove:newPos;
	    else
	    	curPhotoPos = ( newPos > 0 )? 0:newPos;

		jQuery('.galleria-thumbnails').animate( { marginTop:curPhotoPos },80 );
	    MY_moving = true;

    }

}

// Capture touch on thumbnails, Start
function MY_touchStart(e) {
    var $el = $(e.target);
    MY_startY = event.targetTouches[0].clientY;
    $el.bind('touchmove', MY_touchMoveY);
    $el.bind('touchend', MY_touchEnd);

	maxYmove = jQuery(".galleria-thumbnails").height()-thumbStage;
//    alert('start '+MY_moving)
};
// Capture touch on thumbnails, End
function MY_touchEnd(e) {
    var $el = $(e.target);
	$el.unbind('touchmove touchend');
    MY_moving = false;
//    alert("end "+MY_moving)
};



// Capture Orientation
function updateOrientation() {
	var orientation='';
    orientation = (window.innerWidth < window.innerHeight) ? 'profile' : 'landscape';
    mobileWidth = window.innerWidth;
    jQuery('body').removeClass('profile landscape').addClass( orientation );
}
*/


// Get Photo Set
var picIndex=-1;
var pic;
function getImageSet(set,img){

	pic = typeof(img) != 'undefined' ? img : '';
	console.log('pic'+pic);

	$.ajaxSetup({ cache:false });
	$.getJSON('http://'+site+'/'+set+'.json', function(data) {

			// record location
			$.bbq.pushState('#'+set);
	
			$.each(data, function(i,item){
			    img = item.image;
				data[i]["image"] = 'images/portfolio/'+set+'/'+img;
				data[i]["thumb"] = 'images/portfolio/'+set+'/thumbnails/'+img;

				if( img == (pic+'.jpg') ){
					//console.log('index (img)='+i);
					picIndex = i;
				}
        	});

			
			// Show stage
		    $('#menu').fadeOut(200);
		    $('#photo').html('').show();
	    	if( ! isMobileWebKit ){
				$('#bottom').animate({'paddingTop':'30px'},200);
				$('#copyright').animate({'marginRight':'31px'},200);
				curPhotoPos=0;
   			}
			
			if( picIndex >= 0 ){
				console.log('start='+picIndex);
				G = $('#photo').galleria({ data_type: 'json', data_source: data, autoplay:false, transition:'lightbox',show:picIndex });
			}else
				G = $('#photo').galleria({ data_type: 'json', data_source: data, autoplay:false, transition:'lightbox'});
	
		    G.bind(Galleria.THUMBNAIL, function(e) {
		        hiLightIMG(e.thumbTarget);
		    });

   			// Back Button
	        $('#top .back').css({display:'block', opacity:'0'}).animate({'opacity':'1'},500).one('click', function(){

				// record root location
				$.bbq.pushState('');

				$( this ).hide();
				    $('#photo').hide().html('');
				    $('#menu').fadeIn(500);
			    	if( ! isMobileWebKit ){
//						$('body').unbind('touchstart');
						$('#bottom').animate({'paddingTop':'10px'},200);
					    $('#copyright').animate({'marginRight':'0px'},200);
					}
				});

/*
			if( pic!='' ){
				G.show( pic );
				console.log("> pic="+pic);
			}
*/
				
			// iPhone Stuff
			/*if( isMobileWebKit ){
		        activateDetectFlicksThumbs();
		    }*/
		});
}

function hiLightIMG(img){
	clone = $(img).clone();
	src = $(img).attr('src').replace( /thumbnails/,'thumbnails/color' );
	clone.css({'display':'none','opacity':'1'});
	clone.attr('src',src);
    clone.prependTo( $(img).parent() );

	$(img).css('opacity','0').animate({'opacity':'1'},700);


    $(img).mouseenter(
		function() {
		    $(this).prev().show();
			$(this).stop().animate({'opacity':'0'},180);
		}
	).mouseleave(
		function() {
			$(this).delay(500).stop().animate({'opacity':'1'},1600);
		}
	);
}

function hoverOverSet(){
    $('#menu a').mouseenter(
		function() {
			$('.info',this).stop().css({display:'block', opacity:'0'}).animate({'opacity':'1'},100);
		}
	).mouseleave(
		function() {
			$('.info',this).stop().animate({'opacity':'0'},300);
		}
	);
}










































/*
    jQuery plugin to detect flicks on elements
    Steve Gough 4/12/2010
*/
/*(function($) {
    $.fn.detectFlicks = function(options) {

        //for reference only
        var LeftToRight = 'left2right',
            RightToLeft = 'right2left',
            UpToDown = 'up2down',
            DownToUp = 'down2up';

        var flickController = {
            direction: '',
            isFlick: false
        };

        var defaults = {
            threshold: 15,
            axis: 'x',
            flickEvent: function() { return true; }
        };

        var options = $.extend(defaults, options);

        flickController.touchStart = function(e) {
            var $el = $(e.target);
            // this is where the touch was first detected
            this.isFlick = false;
            this.startX = event.targetTouches[0].clientX;
            this.startY = event.targetTouches[0].clientY;
            if (options.axis == 'y') {
                $el.bind('touchmove', flickController.touchMoveY);
            }
            else {
                $el.bind('touchmove', flickController.touchMoveX);
            }

            $el.bind('touchend', flickController.touchEnd);
        };

        flickController.touchMoveX = function(e) {

            event.preventDefault(); //no scrolling
            this.movedX = event.targetTouches[0].clientX;
            if (Math.abs(Math.abs(this.movedX) - Math.abs(this.startX)) > options.threshold) {
                this.isFlick = true;
                if (this.movedX > this.startX) {
                    flickController.direction = LeftToRight;
                }
                else {
                    flickController.direction = RightToLeft;
                }
            }
        };

        flickController.touchMoveY = function(e) {

            event.preventDefault(); //no scrolling
            this.movedY = event.targetTouches[0].clientY;
            if (Math.abs(Math.abs(this.movedY) - Math.abs(this.startY)) > options.threshold) {
                this.isFlick = true;
                if (this.movedY > this.startY) {
                    flickController.direction = UpToDown;
                }
                else {
                    flickController.direction = DownToUp;
                }
            }
        };

        // Evaluate the custom code if a flick was detected
        flickController.touchEnd = function(e) {
            var $el = $(e.target);
            if (this.isFlick) {
                options.flickEvent({ direction: flickController.direction });
            }
            $el.unbind('touchmove touchend');
        };

        obj = $(this);
        obj.bind('touchstart', flickController.touchStart);

        return flickController;
    };
})(jQuery);
*/



































/******************************************************************************/
/*********************************** EASING ***********************************/
/******************************************************************************/

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 *
 * Open source under the BSD License.
 *
 * Copyright 2008 George McGinley Smith
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * Neither the name of the author nor the names of contributors may be used to endorse
 * or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
/*
$.easing.jswing = $.easing.swing;

$.extend($.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert($.easing.default);
		return $.easing[$.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - $.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return $.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return $.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
*/
