var App = {
	user_id:0,
	client_id:0,
	here:null,
	init:function(params){
		for(var prop in params)
		{
			this[prop] = params[prop];
		} 
	},
	msg:function(msg,type){
		time = new Date().getTime();
		//alert(msg);
		$('<div class="message" id="message'+time+'">'+msg+'<br /><a style="float:right" onclick="$(\'#message'+time+'\').hide(1000);return false;">close</a></div>').insertAfter('#breadcrumbs');
	}
};

$(document).ready(function(){
	$("#SearchTerm:text[value='']").autofill();
	
	$('a.loadMediaDialog').live('mouseup',function(e){
		e.preventDefault();
		if ($('body').has('#dialog').length <= 0) {
			$('body').append('<div id="dialog"><div id="dialog-content"></div></div>');
		}
		
		$id = $(this).attr('id');
		$id=$id.split('-');
		item_type = $id[0];
		media_id = $id[1];
		switch(item_type){
		case 'Video':
			item_type = item_type + 's';
			url = '/' + item_type + '/preview/' + media_id;
			break;
		case 'Image':
			item_type = item_type + 's';
			url = '/' + item_type + '/preview/' + media_id;
			break;
		default: // Audio
			url = '/' + item_type + '/preview/' + media_id;
			break;
		}
		
		// Limit title length to 50 characters
		var len = 50;
		var mediaTitle = $(this).attr('title');
		if(mediaTitle.length > len){
			mediaTitle = mediaTitle.substring(0, len)+'...';
		}
		
		$('#dialog').dialog({ title: mediaTitle,  position: 'top', width:'auto', height:'auto' });
		$("#dialog-content").load(url, function(){
			if (item_type == 'Images'){
				$("#dialog-content img").load(function(){
					//alert($(this).width());
					var $docWidth = $(window).width();
					var $dialogImgWidth = $('.ui-dialog').width();
					var $docCenter = ($docWidth/2) - ($dialogImgWidth/2);
					$('.ui-dialog').css({'left': $docCenter+'px'});
				});
			}else{
				var $docWidth = $(window).width();
				var $dialogImgWidth = $('.ui-dialog').width();
				var $docCenter = ($docWidth/2) - ($dialogImgWidth/2);
				$('.ui-dialog').css({'left': $docCenter+'px'});
			}
		});
		
		return false;
	});
});

