if(!PB) var PB = {};

var NewImageGalleryForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	}

});

var EditImageInfoForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	}

});

var EditImageGalleryInfoForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	}

});

var NewVideoGalleryForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	}

});

var EditVideoInfoForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	}

});

var EditVideoGalleryInfoForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	}

});

var CommonMedia = Class.create({
	initialize:function()
	{
		this.initUploadModal();
	},
	openImageUploader:function(for_what)
	{
		this.uploadModal.open();
		window.initImgSWFU(for_what);
	},
	openFileUploader:function()
	{
		this.uploadModal.open();
		window.initFileSWFU();
	},
	openVideoUploader:function()
	{
		//content = this.uploadTemplate.getContent();
		
		//this.uploadModal.container.update(content);
		this.uploadModal.open();
		window.initVidSWFU();
	},
	openVideoStreamUploader:function(gallery_id)
	{
		//content = this.uploadTemplate.getContent();
		
		//this.uploadModal.container.update(content);
		this.streamUploadModal = new Control.Modal();
		
		content =' <div class="box_holder fiveTwenty" >'+
    '<h2 class="">Upload Video Stream</h2>'+
    '<div class="box-style_2" >'+
    		'<form action="" class="form-style_1" >'+
           '<input type="hidden" id="gallery_id" value="'+gallery_id+'"/>'+
           ' <fieldset>'+
                ' <ol>'+
                     '<li>'+
                        '<label for="">URL:</label>'+
                        '<input id="stream[url]" name="" type="text" class="" size="45" />'+
                     '</li>'+
                 '</ol>'+
            ' </fieldset>'+
             '<div class="center">'+
             	'<input type="button" value="upload stream" onclick="javascript:PB.Media.uploadVideoStream();"/>'+
             '</div>'+
         '</form>'+
        '<div class="tr"></div>'+
       '<div class="tl"></div>'+
        '<div class="br"></div>'+
       '<div class="bl"></div>'+
   '</div>'+
'</div>';
	this.streamUploadModal.container.update(content);
		this.streamUploadModal.open();
		//window.initVidSWFU();
	},
	uploadVideoStream:function()
	{
		stream_url = $('stream[url]').value;
		if(stream_url.length == 0)
		{
			window.growler.error('you must enter an url',{header:'Error'});
			return;
		}
		if(!stream_url.match('http://'))
			stream_url = 'http://'+stream_url;
		
		$ajax('/user',{action:'upload_stream',stream_url:stream_url,gallery_id:$('gallery_id').value},this.handleServerResponse.bind(this));
	},
	deleteImageGallery:function(gallery_id)
	{
		this.confirmDeleteModal.close();
		$ajax('/user',{action:'delete_image_gallery',gallery_id:gallery_id},this.handleServerResponse.bind(this));
	},
	confirmDeleteImageGallery:function(gallery_id)
	{
		var are_you_sure = 
		'<div class="box_holder fourSixty end" >'+
			'<div class="box-style_4 warning" >'+
				'<h2 class="pop_title">Warning</h2>'+
				'<p>Deleting this gallery will remove all contents from it.</p>'+
				'<p>Do you want to continue?</p>'+
				'<div>'+
					'<input type="button" value="cancel" onclick="PB.Media.confirmDeleteModal.close();"/>'+
					'<input type="button" value="remove gallery" onclick="PB.Media.deleteImageGallery('+gallery_id+');"/>'+
				'</div>'+
			'<div class="clear"></div>'+
			'<div class="tr"></div>'+
			'<div class="tl"></div>'+
			'<div class="br"></div>'+
			'<div class="bl"></div>'+
			'</div>'+
		'</div>';
		
		this.confirmDeleteModal = new Control.Modal();
		
		this.confirmDeleteModal.container.update(are_you_sure);
		this.confirmDeleteModal.open();
	},
	deleteVideoGallery:function(gallery_id)
	{
		this.confirmDeleteModal.close();
		$ajax('/user',{action:'delete_video_gallery',gallery_id:gallery_id},this.handleServerResponse.bind(this));
	},
	confirmDeleteVideoGallery:function(gallery_id)
	{
		var are_you_sure = 
		'<div><span>Warning</span></div>'+
		'<div><p>Deleting this gallery will remove all contents from it.</p></div>'+
		'<div><p>Do you want to continue?</p></div>'+
		'<div><input type="button" value="cancel" onclick="PB.Media.confirmDeleteModal.close();"/>'+
		'<input type="button" value="remove gallery" onclick="PB.Media.deleteVideoGallery('+gallery_id+');"/>'+
		'</div>';
		
		this.confirmDeleteModal = new Control.Modal();
		
		this.confirmDeleteModal.container.update(are_you_sure);
		this.confirmDeleteModal.open();
	},
	handleServerResponse:function()
	{
	
	},
	rotateImage:function(how,image_id)
	{
		$ajax('/user',{action:'rotate_image',image_id:image_id,how:how},this.handleServerResponse.bind(this));
	},
	deleteVideo:function(video_id)
	{
		$ajax('/user',{action:'delete_video',video_id:video_id},this.handleServerResponse.bind(this));
	},
	deleteImage:function(image_id)
	{
		$ajax('/user',{action:'delete_image',image_id:image_id},this.handleServerResponse.bind(this));
	},
	handleServerResponse:function(response)
	{	
		foo = eval(response);
		
		
		switch(foo.code)
		{
			case 200:
				if(response.location)
					document.location = response.location;
			break;
			case 210:
				if(foo.error)
				{
					window.growler.error(error,{header:'woops!'});
				}
				//alert(foo.errors);
			break;
		}
	},
	initUploadModal:function()
	{
		this.uploadModal = new Control.Modal();
		this.uploadTemplate = new PB.Template('modals_uploader');
		
		document.observe('template:modals_uploader',function(event)
		{
			this.uploadModal.container.update(event.memo);
		}.bind(this));
	}
	
});

function initCommonMedia()
{
	PB.Media = new CommonMedia();
}

/*--------------
	IMAGE FORMS
*/

function initEditImageInfoForm()
{
	if($('edit_image_info_form'))
	PB.Forms.edit_image_info_form = new EditImageInfoForm('edit_image_info_form');
}
PB.addToDomLoad('initEditImageInfoForm','');

function initEditImageGalleryInfoForm()
{
	if($('edit_image_gallery_info_form'))
	PB.Forms.edit_image_gallery_info_form = new EditImageGalleryInfoForm('edit_image_gallery_info_form');
}
PB.addToDomLoad('initEditImageGalleryInfoForm','');


function initNewImageGalleryForm()
{
	if($('new_image_gallery_form'))
	PB.Forms.new_image_gallery_form = new NewImageGalleryForm('new_image_gallery_form');
}
PB.addToDomLoad('initNewImageGalleryForm','');

/*==============================
	VIDEO FORMS
*///edit_video_info_form
function initEditVideoInfoForm()
{
	if($('edit_video_info_form'))
	PB.Forms.edit_video_info_form = new EditVideoInfoForm('edit_video_info_form');
}
PB.addToDomLoad('initEditVideoInfoForm','');

function initEditVideoGalleryInfoForm()
{
	if($('edit_video_gallery_info_form'))
	PB.Forms.edit_video_gallery_info_form = new EditVideoGalleryInfoForm('edit_video_gallery_info_form');
}
PB.addToDomLoad('initEditVideoGalleryInfoForm','');


function initNewVideoGalleryForm()
{
	if($('new_video_gallery_form'))
	PB.Forms.new_video_gallery_form = new NewVideoGalleryForm('new_video_gallery_form');
}
PB.addToDomLoad('initNewVideoGalleryForm','');

PB.addToDomLoad('initCommonMedia','');

