/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var GreyGroup = {
	
	init : function() {
		jQuery(function() {
			GreyGroup.prepareAll(document);
		});
	},
	
	prepareAll : function(context) {
		GreyGroup.preparePlaceholders(context);
		GreyGroup.prepareAjaxed(context);
		GreyGroup.prepareGallery(context);
		
		jQuery('a[href*=#]').click(function(e) {
			var name = jQuery(this).attr('href').match(/#(.+)$/);
			
			if (name && (name = name[1]) && jQuery('a[name='+name+']').length > 0) {
				jQuery(window).scrollTo({top : jQuery('a[name='+name+']').offset().top - 50}, 500, {axis:'y'});
				e.preventDefault();
				return false;
			} else {
				return; 
			} 
		});
	}, 
	
	preparePlaceholders : function(context) {
		jQuery('a.placeholder').click(function(e) {
			var link = jQuery(this);
			var iframe = link.attr('iframe');
			if (iframe) {
				jQuery('<iframe width="'+link.innerWidth()+'" height="'+link.innerHeight()+'" src="'+iframe+'" frameborder="0" allowfullscreen></iframe>').replaceAll(jQuery(this));
			}
			e.preventDefault();
			return false;
		});
		
		jQuery('a.replacement').click(function(e) {
			var link = jQuery(this);
			var replace = link.attr('replace');
			var target = jQuery('#' + replace);
			var iframe = link.attr('iframe');
			if (iframe) {
				target.html('<iframe width="'+target.innerWidth()+'" height="'+target.innerHeight()+'" src="'+iframe+'" frameborder="0" allowfullscreen></iframe>');
			}
			e.preventDefault();
			return false;
		});
	},
	
	
	prepareAjaxed : function(context) {
		
		jQuery('.ajaxed').each(function() {
			var ajaxed = this;
			jQuery('.ajaxed_list a', ajaxed).click(function (e) {
				GreyGroup.ajaxedShow(this);
				e.preventDefault();
				return false;
			});

			//GreyGroup.ajaxedAutoSizeInterval(ajaxed, enable);
			//ajaxed.autosizeInterval = window.setInterval(function() {GreyGroup.ajaxedAutoSize(ajaxed);}, 500);
			
		});
		
	},
	
	ajaxedAutoSizeInterval : function(ajaxed, enable) {
		var ajaxed = this.getAjaxed(ajaxed);
		if (ajaxed.autosizeInterval) {
			window.clearInterval(ajaxed.autosizeInterval);
			ajaxed.autosizeInterval = 0;
		}
		if (enable) {
			ajaxed.autosizeInterval = window.setInterval(function() {GreyGroup.ajaxedAutoSize(ajaxed);}, 500);
		}
	},
	
	getAjaxed : function(obj) {
		obj = jQuery(obj);
		if (obj.hasClass('ajaxed')) return obj.get(0);
		return obj.parents('.ajaxed').get(0);
	},	
	
	
	ajaxedShow : function(link) {
		link = jQuery(link);
		var ajaxed = this.getAjaxed(link);
		
		var url = link.attr('href');
		url += ((url.indexOf('?') > 0) ? '&' : '?') + 'ajax=1';
		url = url.replace(/^http:\/\/[^\/]+/, '');

		// ukrywam info...
		var info = jQuery('.ajaxed_info', ajaxed);
		if (!info.hasClass('hidden')) {
			info.addClass('hidden');
			info.slideUp();
		}
	
		var content = jQuery('.ajaxed_view .content', ajaxed);
	
		// chowam aktualny...
		var active = jQuery('.result.active');
		if (active.length > 0) {
			active.removeClass('active');
			//var activeBlind = jQuery('<div class="blind"></div>').appendTo(active);
			/*
			activeBlind.animate({height : active.outerHeight() + 'px'}, {duration: 750, complete : function() {
				active.remove();
			}});
			*/
		   active.css('clip', 'rect(0px, 920px, '+height + 'px, 0px)');
			var height = active.outerHeight();
			active.animate({clip : 'rect(' + height + 'px, 920px, '+height + 'px, 0px)'}, {duration: 750, complete : function() {
				active.remove();
			}});
		}
		
		jQuery(window).scrollTo({top : jQuery(ajaxed).offset().top - 60}, 500, {axis:'y'});
		
		var result = jQuery('<div class="result loading"></div>')
			.addClass('active')
			.appendTo(content)
		;
		
		jQuery.ajax({
			url : url,
			dataType : 'html',
			type : 'get',
			success : function(data) {
				result.html(data);
				GreyGroup.ajaxedPrepareContent(result);
				//result.slideUp(1);
				var height = result.outerHeight();
				result.css('height', '0px');
				result.animate({height : height + 'px'}, {duration: 1000, complete : function() {
					result.css('height', '');  
				}});
				//result.slideDown(5000);
				GreyGroup.ajaxedAutoSizeInterval(ajaxed, true);
			}
		});
		
	},	
	
	ajaxedHide : function(link) {
		link = jQuery(link);
		var ajaxed = GreyGroup.getAjaxed(link);
		
		// pokazuje info...
		var info = jQuery('.ajaxed_info', ajaxed);
		if (!info.hasClass('hidden')) return;
		
	
		// chowam aktualny...
		var active = jQuery('.result.active');
		active.removeClass('active');
		active.slideUp(500, function() {
			active.remove();
		});
		
		jQuery(window).scrollTo({top : jQuery(ajaxed).offset().top - 60}, 500, {axis:'y'});
		
		info.removeClass('hidden');
		info.slideDown();
		
		console.log(ajaxed);
		GreyGroup.ajaxedAutoSizeInterval(ajaxed, false);
		GreyGroup.ajaxedAutoSize(ajaxed);
		
	},
	
	
	ajaxedPrepareContent : function(content) {
		content = jQuery(content);
		content.find('a[rel=prev],a[rel=next]').click(function(e) {
			e.preventDefault();
			GreyGroup.ajaxedShow(this);
		});
		content.find('a[rel=back]').click(function(e) {
			e.preventDefault();
			GreyGroup.ajaxedHide(this);
		});
		
		GreyGroup.preparePlaceholders(content);

	},
	
	ajaxedAutoSize : function(ajaxed) {
		ajaxed = GreyGroup.getAjaxed(ajaxed);
		
		var active = jQuery('.result.active>*');
		var height = 0;
		if (active.length > 0) height = active.outerHeight();
		
		if (height == 0 && ajaxed.autosizeInterval != 0) {
			return;
		}
		
		if (ajaxed.viewerHeight == height) return;
		
		console.log(height);
		
		ajaxed.viewerHeight = height;
		
		var content = jQuery('.ajaxed_view>.content', ajaxed);
		
		content.animate({height : (height) + 'px'}, {duration:500});
		
		//content.css('height', (height) + 'px');
	},
	
	
	
	prepareGallery : function(context) {
		jQuery('.gallery').each(function() {
			var gallery = this;
			jQuery('.gallery_view .next').click(function() {
				GreyGroup.galleryNext(this);
			});
			jQuery('.gallery_view .prev').click(function() {
				GreyGroup.galleryPrev(this);
			});
			jQuery('.gallery_list a', gallery).click(function (e) {
				GreyGroup.galleryShow(this);
				e.preventDefault();
				
				return false;
			});
			jQuery('.gallery_list a:first', gallery).click();
			
			gallery.nextInterval = window.setInterval(function() {GreyGroup.galleryNext(gallery, true);}, 6000);
		});
	},
	
	getGallery : function(obj) {
		obj = jQuery(obj);
		if (obj.hasClass('gallery')) return obj.get(0);
		return obj.parents('.gallery').get(0);
	},
	
	galleryShow : function(link, leftRight) {
		link = jQuery(link);
		var gallery = this.getGallery(link);
		
		var nextLink = link.next('.item');
		if (nextLink.length) {
			jQuery('img.preload', gallery).attr('src', nextLink.attr('href'));
		}
		
		var activePhoto = jQuery('.active.photo', gallery);
		if (activePhoto.length) {
			activePhoto.removeClass('active');
			activePhoto.animate({left : (leftRight ? 920 : -920) + 'px' }, {duration: 500, complete : function() {
				activePhoto.remove();
			}})
			
			
		}
		  
		jQuery('a.active').removeClass('active');
		
		var photo = jQuery('.template', gallery).clone()
			.removeClass('template')
			.addClass('active')
			.attr('id', link.attr('id'))
			.appendTo('.gallery_view .content', gallery)
		;
		
		var title = link.attr('title').split(/\s*\/\s*/);
		photo.find('h2').text(title[0]); 
		if (title.length > 1) photo.find('h3').text(title[1]);
		
		link.addClass('active');
		
		photo.find('img').attr('src', link.attr('href')).attr('width', link.attr('width')).attr('height', link.attr('height'));
		
		photo.css('left' , (leftRight ? -920 : 920) + 'px');
		photo.animate({left : '0px' }, {duration: 500, complete : function() {
		}})
		  
	}, 
	
	galleryNext : function(link, leaveInterval) {
		var gallery = this.getGallery(link);
		if (!leaveInterval) {
			if (gallery.nextInterval) window.clearInterval(gallery.nextInterval);
		}
		var photo = jQuery('.gallery_list .active', gallery).next('.item');
		if (photo.length == 0) photo = jQuery('.gallery_list .item:first', gallery);
		GreyGroup.galleryShow(photo);
	},
	
	galleryPrev : function(link) {
		var gallery = this.getGallery(link);
		
		if (gallery.nextInterval) window.clearInterval(gallery.nextInterval);
		var photo = jQuery('.gallery_list .active', gallery).prev('.item');
		if (photo.length == 0) photo = jQuery('.gallery_list .item:last', gallery);
		GreyGroup.galleryShow(photo, true);
	}
	
	
};

GreyGroup.init();

