// movingBlock v1.0.0
jQuery.fn.movingBlock = function(_options){
	// defaults options
	var _options = jQuery.extend({
		holder:'div.column',
		topDif:0,
		bottomDif:0,
		toEl:true,
		cssTop:'top',
		duration:350
	} , _options);

	return this.each(function(){
		var _obj = $(this);
		var _holder = $(_options.holder);
		var _offset = _holder.offset();
		var _thisHeight = _obj.outerHeight();
		var _prop = _options.cssTop.toString();
		var _duration = _options.duration;
		var _defTop = parseInt(_obj.css(_prop));
		var _move = true, _height;
		if (window.innerHeight) _height = window.innerHeight;
		else _height = document.documentElement.clientHeight;
		var _brDiff = _height - _obj.height();
		_offset.top = _offset.top - _options.topDif + _defTop;
		_offset.bottom = _holder.innerHeight() - _options.bottomDif - _thisHeight - _defTop;
		if($('#main').hasClass('collection-page')){
				if (_options.toEl) {
				_offset.bottom = $('body').height()-_obj.height() - _brDiff + 200;
			}
		}
		else{
				if (_options.toEl) {
				_offset.bottom = $('body').height()-_obj.height() - _brDiff;
			}
		}
		var _win = $(window);
		_win.scroll(function(){
			animateBlock()
		});
		_win.resize(function(){
			winResize();
			animateBlock()
		});
		winResize();
		function winResize(){
			if (window.innerHeight) _height = window.innerHeight;
			else _height = document.documentElement.clientHeight;
			if (_thisHeight >= _height) _move = true;
			else _move = true;
		}
		function animateBlock(){
			if (_move) {
				var _top = _win.scrollTop();
				var _dif = _top - _offset.top;
		//		alert(_offset.top);
				
				
				
				if ($('#main').hasClass('collection-page')) {
					if (_dif > 0) {
						if (_dif >= _offset.bottom - _defTop) _dif = _offset.bottom - _defTop;
						_obj.animate({'top':_dif+_defTop}, {duration:_duration, queue:false});
					} else {
						_obj.animate({'top':_defTop}, {duration:_duration, queue:false});
					}
				}
				else{
					if (_dif > 0) {
						if (_dif >= _offset.bottom - _defTop) _dif = _offset.bottom - _defTop;
						_obj.animate({'top':_dif+_defTop}, {duration:_duration, queue:false});
					} else {
						_obj.animate({'top':_defTop}, {duration:_duration, queue:false});
					}
				}
			}
		}
	});
}

$(document).ready(function(){
	imgChange();
	$('div.cont-pict').movingBlock({
		holder:'#visual-holder',
		topDif:0,
		toEl:".pre-list",
		bottomDif:0,
		cssTop:'top',
		duration:300
	});
	$('div.gallery-slider').movingBlock({
		holder:'li.kwgal',
		topDif:0,
		toEl:".photo-title",
		bottomDif:0,
		cssTop:'top',
		duration:300
	});
	var _H = $('body').height();
	$('div.column-last').css('height', _H);
	var _imgHold = $('div.cont-pict').find('img');
	if(_imgHold.length != 0){
		var _H = $('body').height();		
		$('#visual-holder').height(_H);
		var _imgH = _imgHold.height();
		var _img = $('div.cont-pict');
		_img.css('height', _imgH);
	}
	
	var _imgHold2 = $('li.kwgal');
	if(_imgHold2.length != 0){
		var _H = $('body').height();
		$('li.kwgal').height(_H);
		var _imgH = _imgHold2.height();
		var _img = $('li.kwgal');
		_img.css('height', _imgH);
	}
	
	// newsletter
	var _btn_reg = $('a.btn-register');
	var _content = $('div.register-holder');
	var _form = $('form.register-form');
	_btn_reg.click(function(){
		_content.hide();
		_form.show();
		return false;
	});
	
	
		// init: hide the prev arrow since we start on the first image
		$('a.link-prev').hide();



		$('a.link-prev').click(function(e){
			// get the current visible image
			var curr = $(".kwgal:visible");
			// if there is no previous image do nothing
			if (curr.prev().length) {  
			 	 // if there is an earlier image
				// hide the current
				curr.hide();
				// show the previous
				curr.prev().show();
				// change the current to the new current
				curr = curr.prev();
			} 
			// if the new current = 0 then hide the prev arrow
			if (!curr.prev().length) {  
				$('a.link-prev').hide();
			}
			// if the new current = 0 then hide the prev arrow
			if (curr.next().length) {  
				$('a.link-next').show();
			}
		}
	)


		$('a.link-next').click(function(e){
			// get the current visible image
			var curr = $(".kwgal:visible");		
			// if there is no next image do nothing
			if (curr.next().length) {  
			 	 // if there is a later image
				// hide the current
				curr.hide();
				// show the previous
				curr.next().show();
				// change the current to the new current
				curr = curr.next();
			} 
			// if the new current is last then hide the prev arrow
			if (!curr.next().length) {  
				$('a.link-next').hide();
			}
			// if the new current = 0 then hide the prev arrow
			if (curr.prev().length) {  
				$('a.link-prev').show();
			}
		}
	)


		$('.collection li a').click(function(e){
			e.preventDefault();
			var clickedId = $(this).attr('id');
			$(".kwgal:visible").fadeOut(500);	
			var foo = $("#k" + clickedId);
			foo.fadeIn(500);
		}
	)

		$('.collection li a').mouseenter(function(e){
			var clickedId = $(this).attr('id');
			if ($(".kwgal:visible").attr('id') != ("k" + clickedId)) {
				$(".kwgal:visible").fadeOut(500);	
				var foo = $("#k" + clickedId);
				foo.fadeIn(500);
			};
		}
	)
	
	
		
});



function imgChange(){
	var _img = $('div.cont-pict');
	var _links = $('div.cont-cols').find('a');
	_links.mouseover(function(){
		var _src = $(this).attr("title");
		_img.empty();
		_img.append('<img src="'+_src+'" alt="image" />');
	});
}



