/*

minus99.com

*/

(function($){
	$.fn.extend({
		minusGallery : function(options){
			
			var defaults = {
				displayNumber: 1,
				speed: 333,
				white: "http://www.cemhakko.com/wp-content/themes/onepage/images/white.gif"
			};
			
			var options = $.extend(defaults, options);
			
			return this.each(function(){
				var o = options;
				var holder = $(this);
				var slide = $('ul.bigImages', this);
				
				var piece = slide.children('li').outerWidth(),
				stage = piece*o.displayNumber,
				speed = o.speed,
				white = o.white,
				thumbs = $('ul.gridContent', this),
				scrollerholder = $('#mcs_gallery', this),
				totalWidth = 0, itemsNum = 0, xpos = 0, canSlide = true, onDisplay = 0;
				
				$('li.big a', this).click(function(){
					var big = $(this).attr("rel"), wd, ht;
					
					var img = new Image();
						img.onload = function(){
						
						this.width < 1000 ? wd = this.width : wd = 1000;
						this.height < 500 ? ht = this.height : ht = 500;
						
						holder.children('.navigator, .backtothumbs').fadeOut(speed);
						holder.children('.zoomHolder').html('<img id="zoomImage" src="'+white+'" width="'+wd+'" height="'+ht+'" big="'+big+'" />').fadeIn(speed,
							function(){
								new Zoomer(holder.children('.zoomHolder').children('img#zoomImage'), options);
							} 
						).click(function(){
							holder.children('.zoomHolder').fadeOut(speed);
							holder.children('.navigator, .backtothumbs').fadeIn(speed);
						});;
					}
					img.src = big;
				});
				
				$(function(){
				    /* count items */
				    
				    slide.children('li.big').each(function(){
					  totalWidth += piece;
					  thumbs.append('<li><a href="javascript:void(0)"><div class="shade"></div><img src="'
					  					+$('img', this).attr("small")
										+'" /><span>'
										+$('img', this).attr("rel")
										+'</span></a></li>');
					  itemsNum++;
				    });
				    slide.css('width', totalWidth+'px');
				    
				    thumbs.children("li").mouseover(function(){ $('.shade', this).show() }).mouseout(function(){ $('.shade', this).hide(); });
				    setTimeout( function(){
					    scrollerholder.mCustomScrollbar("vertical",300,"easeOutCirc",1.05,"auto","yes","yes",15); 
				    }, 500);
				    
				    /* thumbs and stuff */
				    
				    thumbs.children('li').children('a').each(function(){
					  $(this).click(function(){
						$(this).animate({opacity: "0.5"}, 200);
						canSlide = false;
						onDisplay = parseInt($(this).parent().index());
						slide.animate({left: (0-(piece*parseInt($(this).parent().index())))+'px'}, speed, function(){ canSlide = true; });
						setTimeout(function(){
							scrollerholder.animate({top:"-500px"}, speed);
							holder.children('.navigator').fadeIn(speed);
							slide.animate({top:0}, speed, function(){
								holder.children('.backtothumbs').fadeIn(speed).click(function(){
									$(this).fadeOut(speed, function(){
										slide.animate({top:"500px"}, speed);
										scrollerholder.animate({top:0}, speed);
										holder.children('.navigator').fadeOut(speed);
									});
								});
							});
						}, speed);
					  });
				    });
				});
				
				$('#next', this).click(function(){ slideNext(); });
				$('#prev', this).click(function(){ slidePrev(); });
				
				
				/* Slider functions */
				
				function slideNext(){
				    xpos = slide.position().left;
				    
				    if(canSlide == true){ canSlide = false;
					  // IE8 turn around slide.children('li').length
					  if(xpos > stage-totalWidth1+slide.children('li').length)
						slide.animate({left: (xpos-piece)+'px'}, speed, function(){ canSlide = true; onDisplay++; });
					  else
						slide.animate({left: '0px'}, speed, function(){ canSlide = true; onDisplay=0; });
				    }
				}
				
				function slidePrev(){
				    xpos = slide.position().left;
				    
				    if(canSlide == true){ canSlide = false;
					  if(xpos < 0)
						slide.animate({left: (xpos+piece)+'px'}, speed, function(){ canSlide = true; onDisplay--; });
					  else
						slide.animate({left: (stage-totalWidth)+'px'}, speed, function(){ canSlide = true; onDisplay=itemsNum-1; });
				    }
				}
				
				var proxied1 = jQuery.fn.nexta;
				var proxied2 = jQuery.fn.preva;
				
				jQuery.fn.nexta = function(){
					slideNext();
				};
				
				jQuery.fn.preva = function(){
					slidePrev(); 
				};

				//---------------
			});
		}
	});
})(jQuery);


//-zoomer----------------------------------------//
/*
---
 
name: Zoomer
description: Class to show zoomed image inside original. MooTools Zoomer port
license: MIT-Style License (http://mifjs.net/license.txt)
copyright: Anton Samoylov (http://mifjs.net)
authors: Anton Samoylov (http://mifjs.net)
requires: jQuery1.4
provides: Zoomer
 
...
*/

var minus = true;

var Zoomer = function(element, options){
	this.initialize.apply(this, arguments);
	return this;
};

Zoomer.prototype = {
	
	version: '1.8.2',

	initialize: function(element, options){
		var dflt = {
			smooth: 6
		};
		this.options = $.extend(dflt, options || {});
		this.small = $(element);
		var self = this;
		if(!this.small[0].complete){
			this.small.bind('load', function(){
				self.prepareSmall();
			});
		}else{
			this.prepareSmall();
		}
		var src = this.options.big || this.small.attr('big');
		var img = new Image();
		img.src = src;
		this.big = $(img).css({
			position: 'absolute',
			top: 0,
			left: 0,
			cursor: 'crosshair'
		}).hide();
		if(!this.big[0].complete){
			this.big.bind('load', function(){
				self.prepareBig();
			});
		}else{
			this.prepareBig();
		}
	},
	
	prepareSmall: function(){
		this.small.wrap('<div class="zoomer-wrapper"></div>');
		this.wrapper = this.small.parent();
		var self = this;
		function getComputedStyle(element, property){
			if (element.currentStyle) return element.currentStyle[property];
			var computed = document.defaultView.getComputedStyle(element, null);
			return (computed) ? computed.getPropertyValue(property) : null;
		};
		$.each(['margin', 'left', 'top', 'bottom', 'right', 'float', 'clear', 'border', 'padding'], function(i, p){
			var style;
			if($.inArray(p, ['left', 'top', 'bottom', 'right']) != -1){
				style = getComputedStyle(self.small[0], p);
			}else{
				style = self.small.css(p);
			}
			if(p == 'margin' && style == 'auto') return;
			var dflt = 'auto';
			if($.inArray(p, ['float', 'clear', 'border']) != -1) dflt = 'none';
			if(p == 'padding') dflt = '0';
			try{
				self.small.css(p, dflt);
				self.wrapper.css(p, style);
			}catch(e){};
		});
		this.wrapper.css({
			width: this.small[0].offsetWidth,
			height: this.small[0].offsetHeight,
			position: 'relative',
			overflow: 'hidden'
		});
		this.smallSize = {
			width: this.small[0].width,
			height: this.small[0].height
		};
		if(this.bigPrepared){
			this.ready();
		}else{
			this.smallPrepared = true;
		}
	},
	
	prepareBig: function(){
		this.bigSize = {
			width: this.big[0].width,
			height: this.big[0].height
		};
		if(this.smallPrepared){
			this.ready();
		}else{
			this.bigPrepared = true;
		}
	},
	
	ready: function(){
		this.big.appendTo(this.wrapper);
		this.big.wrap('<div class="zoomer-wrapper-big"></div>');
		this.bigWrapper = this.big.parent();
		this.bigWrapper.css({
			position: 'absolute',
			overflow: 'hidden',
			top: this.small.offset().top - this.wrapper.offset().top - parseInt(this.wrapper.css('border-top-width'), 10) || 0,
			left: this.small.offset().left - this.wrapper.offset().left - parseInt(this.wrapper.css('border-left-width'), 10) || 0,
			width: this.small[0].offsetWidth,
			height: this.small[0].offsetHeight,
			background: 'url("' + this.small.attr('src') + '")'
		}).mouseenter($.proxy(this.startZoom, this))
		.mouseleave($.proxy(this.stopZoom, this))
		.mousemove($.proxy(this.move, this));
        //minus
        if(minus)
            this.big.fadeIn();
	},
	
	move: function(event){
		this.dstPos = {x: event.pageX, y: event.pageY};
	},
	
	startZoom: function(){
		this.position = this.small.offset();
		
		/** precalculations **/
		this.ratio = {
			x: 1 - this.bigSize.width / this.smallSize.width,
			y: 1 - this.bigSize.height / this.smallSize.height
		};
		this.current = {
			left: parseInt(this.big.css('left'), 10),
			top: parseInt(this.big.css('top'), 10)
		};
		
		this.timer = setInterval($.proxy(this.zoom, this), 10);
        //minus
        if(!minus)
            this.big.fadeIn();
	},
	
	stopZoom: function(){
		clearInterval(this.timer);
        //minus
        if(!minus)
            this.big.fadeOut();
	},
	
	zoom: function(){
		if(!this.dstPos) return;
		var steps = this.options.smooth;
		var dst = {
			left: parseInt((this.dstPos.x - this.position.left) * this.ratio.x, 10),
			top: parseInt((this.dstPos.y - this.position.top) * this.ratio.y, 10)
		};
		this.current.left -= (this.current.left - dst.left) / steps;
		this.current.top -= (this.current.top - dst.top) / steps;
		
		this.big.css(this.current);
	}
	
};
