
(function($) {

	$(document).ready(function() {
	
		$('.auth-link').live('click', function(e) {
			e.stopPropagation();
		});
		
		// Language settings
		var lang = {
			se: {
				author: 'Foto av ',
				openInSlideShow: 'Visa bild i bildspel »'
			},
			en: {
				author: 'Photographer ',
				openInSlideShow: 'Open image in sideshow »'
			}
		};
		
		// Set language
		lang = (jQuery('body').hasClass('i18n-sv')) ? lang.se : lang.en;
		
		var $imgWrapper = $('<div />').addClass('cb-image-wrapper').css({position: 'relative'});
		var $authInfo = $('<div class="auth-info"><div class="auth-info-content"><h2></h2><p><a class="auth-link" target="_blank" href="#"></a></p><p class="cb-link"><a class="auth-photo" href="#">'+lang.openInSlideShow+'</a></p></div></div>').css({
			position: 'absolute',
			padding: '7px'
		}).mouseleave(function() {
			var _this = jQuery(this);
			setTimeout(function() {
				_this.hide();
			}, 400);
		}).hide();
		
		var $iconTemplate = $('<img />').attr('src', '/sites/default/themes/ru/images/icons/image-info.png').css({
			position: 'absolute',
			bottom: 12,
			left: -12,
			cursor: 'pointer'
		}).hover(function() {
			var img = $(this).closest('div').find('img:eq(0)');
			$(this).prev('.auth-info').show();
		});

        $('.node .content .colorbox img').each(function(index) { //loops all the img tags in .node .content
        	
        	var _this = $(this);
        	// Clones
        	var it = $iconTemplate.clone(true);
        	var iw = $imgWrapper.clone();
        	var ai = $authInfo.clone(true);
        	
        	_this.wrap(iw); // Wrap image in template
        	
        	it.insertAfter(_this); // insert info icon 
        	ai.insertAfter(_this); // insert author information
        	
        	var title = _this.attr('title') || null;
        	var longdesc = _this.attr('alt') || null;
        	
        	// remove the 
        	/*var linkUrl = _this.attr('src').replace('_large', '').replace('_medium', '').replace('_small', '');
        	var $link = $('<a />').addClass('custom-box').attr('rel', 'gallery-all').attr('href', linkUrl).click(function(e) {
        		e.preventDefault();
        	});*/
        	
        	if(title !== null) {
        		ai.find('h2').text(lang.author + title);
        	}
        	if(longdesc !== null) {
        		ai.find('a:eq(0)').html(longdesc).attr('href', longdesc);
        	}
        	ai.find('a:eq(1)').click(function() {
        		$(this).closest('.colorbox-insert-image').trigger('click'); // Opens the colorbox
        	});
        	
        	//_this.wrap($link);
        	if($(this).css('float') == 'right') {
        		
        		$(this).parent('div').css('float', 'right');
        	}
		});
		
		// Colorbox setup
		/*
		if(!$('body').hasClass('page-node-edit')) {
		$('.custom-box').colorbox({
			width: 900,
			maxHeight: '100%',
			opacity: 0.7,
			onComplete: function() {
				var _this = $(this);
				var img = _this.find('img:eq(0)');
				var longdesc = img.attr('title') || null;
				var title = img.attr('alt') || null;
				
					var imageInfo = $('#image-info');
					imageInfo.empty(); 
					if(imageInfo.length) { // Div already exists, just update
						if(title !== null) {
							imageInfo.append(lang.author + img.attr('alt'))
						}
						if(longdesc !== null) {
							imageInfo.append($('<a />').attr('href', longdesc).html(longdesc));
						}
					}
					else { // ... insert new div
						var imageInfo = $('<div />').attr('id', 'image-info');
						
						if(title !== null) {
							imageInfo.append(lang.author + title);
						}
						if(longdesc !== null) {
							imageInfo.append($('<a />').attr('href', longdesc).html(longdesc)).css({
								color: '#fff'
							});
						}
						imageInfo.appendTo('#cboxContent').css({opacity: 0.6});;
					}
				}
			});
		}*/
		
		
	});	
	
})(jQuery);	
;

