window.addEvent('domready', function() {
    var movieLinks = $$('a.movie');
	var blogLinks = $$('a.blog');
    var movieOpen = 0;
    
    movieLinks.addEvent('click', function() {
        movieOpen = 1;
		var href = this.getAttribute('href');
        var movieID = this.getAttribute('rel');
        
        var overlay = new Element('div', {
            'styles': {
                'width': '100%',
                'height': '100%',
                'background': '#000',
                'filter': 'alpha(opacity=0)',
                '-moz-opacity': '0',
                '-khtml-opacity': '0',
                'opacity': '0',
                'position': 'fixed',
                'z-index': '10'
            }
		})
        $$('body')[0].grab(overlay, 'top');
        var overlayTween = new Fx.Tween(overlay, {
            onComplete: function() {
                windowSize = window.getSize();
                var containerRequest = new Request.HTML({
                    onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
                        var spaceLeft = (windowSize.x - (movieWidth + 20)) / 2;
                        if (spaceLeft < 0) spaceLeft = 20;
                        var movieDIV = new Element('div', {
                            'id': 'movieDIV',
                            'styles': {
                                'width': movieWidth,
                                'filter': 'alpha(opacity=100)',
                                '-moz-opacity': '1.0',
                                '-khtml-opacity': '1.0',
                                'opacity': '1.0',
                                'left': spaceLeft,
                                'top': '50px'
                            },
                            'html': responseHTML
                        })
                        $$('body')[0].grab(movieDIV, 'top');
                        var containerWidth = movieDIV.getDimensions().width
                        var containerHeight = movieDIV.getDimensions().height;
						resizeDIV(containerWidth, containerHeight);
						movieDIV.setStyles({
						    'filter': 'alpha(opacity=0)',
						    '-moz-opacity': '0',
						    '-khtml-opacity': '0',
						    'opacity': '0',
						    'display': 'block',
						    'overflow': 'hidden'
						})
						var containerMorph = new Fx.Morph(movieDIV, {duration: 'long', transition: Fx.Transitions.Sine.easeOut,
					    onComplete: function() {
							var movieRequest = new Request.HTML({
							    onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
								var objectDIV = new Element('div', {
								    'html': responseHTML
								})
								$('movie_space').grab(objectDIV);
							    }
							}).get('get_movie.php?id='+movieID);
			    		}
					});
					containerMorph.start({
					    'filter': 'alpha(opacity=100)',
					    '-moz-opacity': '1.0',
					    '-khtml-opacity': '1.0',
					    'opacity': '1.0'
					});
			
                    window.addEvent('resize', function() {
                        if (movieOpen == 1) {
                            resizeDIV(containerWidth, containerHeight);
                        }
                    })
                    $('close_button').addEvent('click', function() {
                        movieDIV.destroy();
                        overlay.destroy();
                        movieOpen = 0;
                    })
                }
            }).get(href);
            }
        });
        overlayTween.start('opacity', '0.9');
        
        overlay.addEvent('click', function() {
            var movieDIV = $('movieDIV');
            movieDIV.destroy();
            overlay.destroy();
            movieOpen = 0;
        })
    })
	
	blogLinks.addEvent('click', function() {
		var href = this.getAttribute('href');
		
        var overlay = new Element('div', {
            'styles': {
                'width': '100%',
                'height': '100%',
                'background': '#000',
                'filter': 'alpha(opacity=0)',
                '-moz-opacity': '0',
                '-khtml-opacity': '0',
                'opacity': '0',
                'position': 'fixed',
                'z-index': '10'
            }
		})
        $$('body')[0].grab(overlay, 'top');
		
		var overlayTween = new Fx.Tween(overlay, {
            onComplete: function() {
				var img = new Element('img', {
					'src': href,
					'styles': {
						'display': 'none'
					}
				});
				$$('body')[0].grab(img, 'top');
				var displayLayer = function() {
					var imgSize = img.getDimensions();
					windowSize = window.getSize();
	                var spaceLeft = (windowSize.x - (imgSize.width + 20)) / 2;
	                if (spaceLeft < 0) spaceLeft = 20;
	                var imgDIV = new Element('div', {
	                    'id': 'movieDIV',
	                    'styles': {
	                        'width': imgSize.width,
							'height': (imgSize.height < (windowSize.y - 150))? 'auto' : windowSize.y - 100,
							'overflow': (imgSize.height > windowSize.y - 100)? 'scroll' : 'hidden',
	                        'filter': 'alpha(opacity=100)',
	                        '-moz-opacity': '1.0',
	                        '-khtml-opacity': '1.0',
	                        'opacity': '1.0',
	                        'left': spaceLeft,
	                        'top': '50px'
	                    },
						'html': '<span id="close_button"></span>'
	                })
					img.setStyle('display', 'inline');
					imgDIV.grab(img, 'top');
	                $$('body')[0].grab(imgDIV, 'top');
				};
				
				var loader = new Asset.images(href, {
					onComplete: function() {
						displayLayer();
			            (function() {
							$('close_button').addEvent('click', function() {
			                	movieDIV.destroy();
			                	overlay.destroy();
			            	})
						}).delay(300);
					}
				})
			}
		})
		overlayTween.start('opacity', '0.9');
		
        overlay.addEvent('click', function() {
            var movieDIV = $('movieDIV');
            movieDIV.destroy();
            overlay.destroy();
        })
	})
    
	
	
    var resizeDIV = function(containerWidth, containerHeight) {
        var movieDIV = $('movieDIV');
        var windowSize = window.getSize();
        
        if (windowSize.x - 60 < containerWidth) {
            movieDIV.setStyles({
                'width': windowSize.x - 60,
                'overflow': 'scroll'
            });
        } else {
            var enoughWidth = 1;
            movieDIV.setStyles({
                'width': movieWidth
            })
        }
        if (windowSize.y - 50 < containerHeight) {
            movieDIV.setStyles({
                'height': windowSize.y - 70,
                'overflow': 'scroll'
            });
        } else {
            var enoughHeight = 1
            movieDIV.setStyles({
                'height': 'auto'
            });
        }
        if (enoughWidth == 1 && enoughHeight == 1) {
            movieDIV.setStyle('overflow', 'visible');
        }
	
        var spaceLeft = (windowSize.x - containerWidth) / 2;
        if (spaceLeft < 0) spaceLeft = 10;
        var spaceTop = (windowSize.y - containerHeight) / 2;
        if (spaceTop < 0) spaceTop = 10;
        movieDIV.setStyles({'left': spaceLeft, 'top': spaceTop});
    }
	
	
	/*=====================SHOW COMMENTS================================*/
	if ($$('a.showComments') != null) {
		var commentButtons = $$('a.showComments, span.form_close');
		commentButtons.addEvent('click', function() {
			if (this.hasClass('form_close'))
				var comments = this.getParent().getParent().getParent();
			else
				var comments = this.getParent().getParent().getNext('div.comments');
			if (comments.getStyle('display') == 'none') {
				var commentsHeight = comments.getDimensions().height;
				comments.setStyles({
					'height': 0,
					'display': 'block'
				});
				var commentsTween = new Fx.Tween(comments);
				commentsTween.start('height', commentsHeight);
				
				items.each(function(item, index) {
					if (item.getStyle('top').toInt() > comments.getParent().getStyle('top').toInt()) {
						var itemTween = new Fx.Tween(item);
						itemTween.start('top', item.getStyle('top').toInt() + commentsHeight);
					}
				})
				var itemTween = new Fx.Tween($('ditto_pages'));
				itemTween.start('top', $('ditto_pages').getStyle('top').toInt() + commentsHeight);
				$('blog').setStyle('height', $('blog').getStyle('height').toInt() + commentsHeight);
			} else {
				var commentsHeight = comments.getDimensions().height;
				var commentsTween = new Fx.Tween(comments, {
					onComplete: function() {
						comments.setStyles({
							'height': commentsHeight,
							'display': 'none'
						})
					}
				});
				commentsTween.start('height', 0);
				
				items.each(function(item, index) {
					if (item.getStyle('top').toInt() > comments.getParent().getStyle('top').toInt()) {
						var itemTween = new Fx.Tween(item);
						itemTween.start('top', item.getStyle('top').toInt() - commentsHeight);
					}
				})
				var itemTween = new Fx.Tween($('ditto_pages'));
				itemTween.start('top', $('ditto_pages').getStyle('top').toInt() - commentsHeight);
				$('blog').setStyle('height', $('blog').getStyle('height').toInt() - commentsHeight);
			}
			/*comments.setStyles({
				'height': comments.getDimensions().height,
				'display': (comments.getStyle('display') == 'none')? 'block' : 'none'
			})*/
		})
	}
	
	
	/*=====================FADE BLOG ITEMS=========================================*/
	if ($$('div.blog_item') != null) {
		var items = $$('div.blog_item');
		var i = 0;
		function fadeItem() {
			var fade = new Fx.Tween(items[i], {
				duration: 600,
				onComplete: function() {
					if (i+1 < items.length) {
						i++;
						fadeItem();
					}
				}
			})
			fade.start('opacity', 0, 1);
		}
		
		fadeItem();
	}
  
  
  /*ANTI SPAM*/
  if ($$('input.spamCheck') != null) {
    $$('input.spamCheck').setStyle('display', 'none');
    $$('input.spamCheck').set('value', 'Barack');
    $$('label.spamCheckLabel').setStyle('display', 'none');
    
  } else console.log('fail');
})
