jQuery.noConflict();

jQuery(document).ready(function(){
	jQuery('div.navigation').each(function(){
		if(jQuery(this).find('a').length == 0) jQuery(this).remove();
	});
	homeGallery();
	innerGallery();
	clearInputs();
});
/*---- clear inputs ---*/
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
/*--- home gallery ---*/
function homeGallery(){
	var change_speed = 400; //in ms
	var stay_time = 5000; //in ms
	jQuery('#home-gallery').each(function(){
		var _list = jQuery(this).find('li');
		var _a = _list.index(_list.filter('.active:eq(0)')) != -1 ? _list.index(_list.filter('.active:eq(0)')) : 0;
		var _t, _f = true;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		
		if(stay_time && _f){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0},{queue:false, duration: change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1},{queue:false, duration: change_speed});
				_a = _ind;
			}
			if(stay_time && _f){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- inner gallery ---*/
function innerGallery(){

	var full_images = new Array();
	// find all full images
    jQuery('div.original img').each(function(){
		var target_src = jQuery(this).attr('rel');
		if (target_src)
		{
			var orig_img_tag = jQuery(this);
			full_images.push([orig_img_tag, target_src]);
		}
    });
	
	var zoom_images = new Array();
	// find all zoom images
    jQuery('div.zoom img').each(function(){
		var target_src = jQuery(this).attr('rel');
		if (target_src)
		{
			var orig_img_tag = jQuery(this);
			zoom_images.push([orig_img_tag, target_src]);
		}
    });
	
	// function to swap the source of an image on demand
	function swapImageSource(orig_img_tag, target_src){
		if (orig_img_tag[0].src != target_src)
		{
			orig_img_tag[0].src = target_src;
		}
	}
	
	// function to stream an image on demand
	function streamImageSource(array, index, recursive){
		if (array[index][0][0].src != array[index][1])
		{
			var temp_img =  jQuery('<img/>');
			temp_img[0].src = array[index][1];
			
			temp_img.load(function () {
				array[index][0][0].src = array[index][1];
				if (recursive)
				{
					swapImageSource(zoom_images[image_index][0], array[image_index][1]);
					image_index = image_index + 1;
					streamImageSource(array, image_index, true);
				}
			});
		}
	}
	
	// load the full images one at a time
	var image_index = 0;
	streamImageSource(full_images, image_index, true);
	
	// pre load the first zoom image (the rest wil be streame din when the user changes slide)
	streamImageSource(zoom_images, 0);
	
	// prepare the gallery
	var move_speed = 600; //in ms
	var change_speed = 500; //in ms
	jQuery('div.gallery-page').each(function(){
		var _hold = jQuery(this);
		var btn_hold = _hold.find('div.thumbs ul');
		var _btn = btn_hold.find('a');
		var img_hold = _hold.find('div.img-holder div.cont');
		var _img = img_hold.find('div.el');
		var text_hold = _hold.find('div.title-hold');
		var _text = text_hold.find('div.title');
		/*--- variables ---*/
		var btn_hold_w = btn_hold.children().outerWidth(true) * btn_hold.children().length;
		var _step = btn_hold.parent().width();
		var a_step = 0;
		var _a = _btn.index(_btn.filter('.active:eq(0)')) != -1 ? _btn.index(_btn.filter('.active:eq(0)')):0;
		/*--- default ---*/
		_btn.removeClass('active').eq(_a).addClass('active');
		_img.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_text.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		/*--- add hover scroll ---*/
		_img.each(function()
		{
			var full_img = jQuery(this).find('div.original img').first();
			var zoom_img = jQuery(this).find('div.zoom img').first();
			var full_div = jQuery(this).find('div.original').first();
			
			var orig_pos = {};
			orig_pos.l = full_img.offset().left;
			orig_pos.t = full_img.offset().top;
			orig_pos.fw = full_img.width();
			orig_pos.fh = full_img.height();
			orig_pos.zw = zoom_img.width();
			orig_pos.zh = zoom_img.height();
			orig_pos.gw = 942 - orig_pos.fw;
			orig_pos.gh = 466;
			orig_pos.ratio = (orig_pos.fw / orig_pos.zw);
			orig_pos.lens_hw = (orig_pos.fw * orig_pos.ratio)/ (2 * (orig_pos.fw / orig_pos.gw));
			orig_pos.lens_hh = (orig_pos.fh * orig_pos.ratio)/ 2;
			orig_pos.small_lens_w = orig_pos.lens_hw * 2;
			orig_pos.small_lens_h = orig_pos.lens_hh * 2;
			
			
			var lens_box = jQuery('<div class="lens" style="width: '+orig_pos.small_lens_w+'px; height: '+orig_pos.small_lens_h+'px;"></div>');
			full_div.mouseenter(function() {
				lens_box.appendTo(full_div);
			})
			full_div.mouseleave(function() {
				full_div.find('div.lens').remove();
			});
			
			full_div.bind( 'mousemove', function(e)
			{
				var left_frac = ((e.pageX - orig_pos.l)/orig_pos.fw);
				var top_frac  = -((orig_pos.t - e.pageY)/orig_pos.fh);
				
				var left_target = -(left_frac * orig_pos.zw) + (orig_pos.lens_hw*(1/orig_pos.ratio));
				var top_target = -(top_frac * orig_pos.zh) + (orig_pos.lens_hh*(1/orig_pos.ratio));
				
				if (overleft()) { left_target = 0 }
				if (overright()) { left_target = -(orig_pos.zw - orig_pos.gw) }
				if (overtop()) { top_target = - 0 }
				if (overbottom()) { top_target = -(orig_pos.zh - orig_pos.gh) }
				
				zoom_img.css('position', 'relative');
				zoom_img.css('left', Math.round(left_target));
				zoom_img.css('top', Math.round(top_target));
				
				lens_box.css('left', Math.round(-left_target*orig_pos.ratio));
				lens_box.css('top', Math.round(-top_target*orig_pos.ratio));
				
				// functions
				function overleft() {
					return ((e.pageX - orig_pos.l) < orig_pos.lens_hw);
				}

				function overright() {
					return ((e.pageX - orig_pos.l) > (orig_pos.fw - orig_pos.lens_hw));
				}

				function overtop() {
					return ((e.pageY - orig_pos.t) < orig_pos.lens_hh);
				}

				function overbottom() {
					return ((e.pageY - orig_pos.t) > (orig_pos.fh - orig_pos.lens_hh));
				}
				
			});
			
		});
		/*--- add paginator ---*/
		if(btn_hold_w > _step){
			var _nav = jQuery('<ul class="pager"></ul>');
			var max_step = Math.ceil(btn_hold_w/_step);
			var a_step = Math.floor(_btn.eq(_a).position().left/_step);
			btn_hold.css('left', -a_step*_step);
			for(var i = 0; i < max_step; i++){
				_nav.append('<li><a href="#">'+(i+1)+'</a></li>');
			}
			_nav = _nav.appendTo(_hold.find('div.carusel')).find('a');
			_nav.eq(a_step).addClass('active');
			_nav.click(function(){
				moveList(_nav.index(this));
				return false;
			});
		}
		/*--- actions ---*/
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		_hold.find('div.thumbs a.prev').click(function(){
			if(_a > 0) changeEl(_a - 1);
			else changeEl(_btn.length - 1);
			return false;
		});
		_hold.find('div.thumbs a.next').click(function(){
			if(_a < _btn.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		/*--- changeEl function ---*/
		function changeEl(_ind){
			if(_ind != _a){
				moveList(Math.floor(_btn.eq(_ind).position().left/_step));
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				
				_img.eq(_a).removeClass('active').animate({opacity: 0},{ queue:false, duration: change_speed});
				_img.eq(_ind).addClass('active').animate({opacity: 1},{ queue:false, duration: change_speed});
				
				streamImageSource(full_images, _ind);
				streamImageSource(zoom_images, _ind);
				
				_text.eq(_a).removeClass('active').animate({opacity: 0},{ queue:false, duration: change_speed});
				_text.eq(_ind).addClass('active').animate({opacity: 1},{ queue:false, duration: change_speed});
				
				_a = _ind;
			}
		}
		/*--- moveList function ---*/
		function moveList(_ind){
			if(_ind != a_step){
				_nav.eq(a_step).removeClass('active');
				_nav.eq(_ind).addClass('active');
				btn_hold.animate({left: -_ind*_step},{queue:false, duration: move_speed});
				a_step = _ind;
			}
		}
	});
}
