auth = new Auth();
globalEvent = new GlobalEvent();
ui = new UI();

$(document).ready(function() {
	$("#list-item-add-field").autocomplete("/search/items", {
		width: 355,
		selectFirst: false
	});

	initFlashMessages();
	initCancelEmail();
	
    	
    $(".following").mouseenter(function (){
        $(this).addClass("unfollow");
        $(".following a").html("&#10006; Unfollow");
        
    });
    $(".following").mouseleave(function (){
        $(this).removeClass("unfollow");
        $(".following a").html("&#10004; Following");
    });



	modalWindow.init();
	globalEvent.trigger('ready');
	
	// MT stuff below
	$(".item-img").click(function(){
		var check = $(this).find('img').attr('src');
		var html = '';
		var showModal = false;
		if (check!=''){
			var url = $(this).parent().find('.item-url').attr('href');
			if (url.indexOf('youtube')>=0){
				showModal = true;
				var youtube_id = url.replace(/^[^v]+v.(.{11}).*/,"$1");
				html = 	'<iframe title="YouTube video player" class="youtube-player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/'+youtube_id+'" frameborder="0" allowFullScreen></iframe>';
			} else {
				var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;	
				var match = url.match(regExp);
				if (match){
					showModal = true;
					html = 	'<iframe src="http://player.vimeo.com/video/'+match[2]+'" width="640" height="375" frameborder="0"></iframe>';						    
				}	
			}
			if ($("#videoplayer").length>0){
				$("#videoplayer").remove();
			}
			$('body').append('<div id="videoplayer" class="hidden-object">'+html+'</div>');
			if (showModal) {
				modalWindow.showVideo("#videoplayer");	
			}
		}
	});
	
	

	$("#list-rank .item-img").each(function(){
    	if ($(this).find('img').hasClass('hidden-object')){
    		$(this).css('width','0px');
    	}
    });	
	
	$(".item-url").each(function(){
		var href = $(this).attr('href');
		$(this).text(href.split(/\/+/g)[1]+' - ');
	});
	
	
	$(window).scroll(function() {
	    
    	scroll_to_top = $(window).scrollTop();
    	side_bar_position = $("#main #side-bar").position();
    	scroll_diff = Math.round(side_bar_position.top - scroll_to_top);
    	
    	if (scroll_diff > 0) {
    	    $("#main #side-bar #static").addClass("static");
    	    $("#main #side-bar #static").removeClass("fixed");
    	    $("#main #side-bar").removeClass("padded");
    	    $("#main #side-bar").css({
    	        'padding-top':'0px'
    	        })
    	} else if (scroll_diff < 0) {
    	    //console.log("Scrolling");
    	    $("#main #side-bar #static").addClass("fixed");
    	    $("#main #side-bar #static").removeClass("static");
    	    $("#main #side-bar").addClass("padded");
    	    $("#main #side-bar").css({
    	        'padding-top':$("#main #side-bar #static").height()
    	        })
    	}
    	
    });
	
});

function initFlashMessages(){
	var $messages = $('#flash-messages');
	if($messages.size()){
		
		var height = $messages.height();
		$messages.css('top',-height+"px");
		$messages.animate({top:0},300,function(){
			setTimeout('checkMessagesHover()',3*1000);
		});
		
		$messages.hover(
			function(){
				this.hovered = true;
			},//over
			function(){
				this.hovered = false;
				setTimeout('checkMessagesHover()',2*1000);
			}//out
		);
	}
};

function showFlashMessage(message){
	var $messages = $('#flash-messages');
	
	if(0 == $messages.size()){
		if(typeof message == 'undefined'){
			message = '';
		}
		$('<div id="flash-messages"><p>'+ message +'</p></div>').appendTo($('body'));
		initFlashMessages();
		return;
	}
	
	if(typeof message != 'undefined'){
		$messages.html('<p>'+ message +'</p>');
	}
	
	var height = $messages.height();
	$messages.css('top',-height+"px");
	$messages.animate({top:0},300,function(){
		setTimeout('checkMessagesHover()',3*1000);
	});
}

function checkMessagesHover(){
	var messages = document.getElementById('flash-messages');
	if(!messages){
		return;
	}
	if(!messages.hovered){
		var height = $(messages).height();
		$(messages).animate({top:-height+"px"},300);
	}
};

function initCancelEmail(){
	$('a.cancel-email-addr').click(function(e){
		e.preventDefault();
		modalWindow.show('#cancel-email-popup');
	});
};
