$(document).ready(function(){	
	var oGId;
	var objId;
	var objType;
	var formURL;
	var iframeInit = false;
	
	$('#media-dialog').dialog({
		autoOpen: false,
		width: 750,
		height: 400,
		modal: true,
		zIndex : 12000,
		overlay: {
			opacity: 0.5,
			background: 'black'
		}
	});
		
	// Killing this with fire. FUCK
	/*
	$(".addMedia").live('click', function(event){
			event.preventDefault();
			event.stopPropagation();					
			var fullUrl = $(this).attr('href');
			if (fullUrl != null) {
				var url = fullUrl.split('/');
			}
			for (var i = 0; i < url.length; i++) {
				if (url[i] == 'gallery') {
					oGId = url[++i];
				}
				if(url[i]=='objId'){
					objId = url[++i];
				}
				if(url[i]=='objType'){
					objType = url[++i];
				}
			}
			formURL = baseUrl+'gallery/ajax/upload';
	
			function openDialog(){
				$('#media-dialog').html($('<div class="loading"><p>loading...<\/p><\/div>'));
				$('#media-dialog').dialog('open');
				$.ajax({
					url: baseUrl + 'gallery/ajax/manage/gallery/'+oGId+'/objType/'+objType+'/objId/'+objId,
					success: function(dialogText) {
						$('#media-dialog').html(dialogText);
						var swf_params = { 'PHPSESSID' : sessId, 'casUid' : casUid};
						
						var swfu_settings = {
							upload_url : baseUrl + "gallery/ajax/process-media",
							post_params : swf_params,
							flash_url : baseUrl + "js/plugins/swfupload/swfupload.swf",
							button_image_url : baseUrl + "js/plugins/swfupload/button.png",
							button_placeholder_id : "swfupload",
							button_width : 61,
							button_height : 22,
							debug: true
						};
						
						$( '#swfupload' ).swfupload(swfu_settings)
							.bind( 'fileQueued', function (event, file) {
								$(this).swfupload('startUpload');
							})
							.bind( 'uploadSuccess', function (file, data, response) {
								console.log("%s: %s", response, data);
							})
							.bind( 'uploadError', function (event, file, errorCode, message) {
								console.log("%s: %s", errorCode, message);
							});
					},
					error: function(dialogText) {
						$('#media-dialog').html(dialogText);
					}
				});
			};
			loginCheck(openDialog);		
	});
	*/

	/*
	$("#uploadMedia").live("click", function(event){
			event.preventDefault();
			event.stopPropagation();
			$('ul.vert-one a').eq(0).addClass('current');
			$('ul.vert-one a').eq(1).removeClass('current');
			$('ul.vert-one a').eq(2).removeClass('current');			
			$('div#mediaLoading').show();
			var fullUrl = $(this).attr('href');
			if (fullUrl != null) {
				var url = fullUrl.split('/');
			}
			for(var i=0; i < url.length; i++){
				if(url[i]=='gallery'){
					oGId = url[++i];
				}
				if(url[i]=='objId'){
					objId = url[++i];
				}
				if(url[i]=='objType'){
					objType = url[++i];
				}
			}
			formURL = baseUrl+'gallery/ajax/upload';
			$.ajax({
				type: "GET",
  				url: formURL,
				data: {
					gallery: oGId,
					objType: objType,
					objId: objId
				},
				success: function(data){
					$('div#managerRight').html(data);
				},
				complete: function(data){
					$('div#mediaLoading').hide();
				}
			});
	});
	*/

	$("#galleryAttach").live("click", function(event){
			event.preventDefault();
			event.stopPropagation();
			$('ul.vert-one a').eq(1).addClass('current');
			$('ul.vert-one a').eq(0).removeClass('current');
			$('ul.vert-one a').eq(2).removeClass('current');
			$('div#mediaLoading').show();
			var fullUrl = $(this).attr('href');
			if (fullUrl != null) {
				var url = fullUrl.split('/');
			}
			for(var i=0; i < url.length; i++){
				if(url[i]=='event') {					
					var type = 'EVENT';
					var id = url[++i];
				}
				if(url[i]=='venue') {
					var type = 'VENUE';					
					var id = url[++i];
				}
				if(url[i]=='people') {
					var type = 'PEOPLE';					
					var id = url[++i];
				}
				if(url[i]=='gallery'){
					oGId = url[++i];
				}
				if(url[i]=='objId'){
					objId = url[++i];
				}
				if(url[i]=='objType'){
					objType = url[++i];
				}				
			}
			formURL = baseUrl+'gallery/ajax/galleries';
			$.ajax({
				type: "GET",
  				url: formURL,
				data: {
					objType: objType,
					objId: objId,
					gallery: oGId
				},
				success: function(data){
					$('div#managerRight').html(data);					
				},
				complete: function(data){
					$('div#mediaLoading').hide();
					galleryListener(oGId);
					attachGalleryListener();
				}
			});
	});
	
	function galleryListener(objGalId) {
		$("div#managerRight .showGallery").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var fullUrl = $(this).attr('href');
			if (fullUrl != null) {
				var url = fullUrl.split('/');
			}
			oGId = url[url.length-1];
			$('div#mediaLoading').show();
			formURL = baseUrl+'gallery/ajax/gallery-view';
			$.ajax({
				type: "GET",
  				url: formURL,
				data: {
					gallery: oGId,
					objId: url[url.length-5],
					objType: url[url.length-3]
				},
				success: function(data){
					$('div#managerRight').html(data);					
				},
				complete: function(data){
					$('div#mediaLoading').hide();
					mediaListener(oGId);
				}
			});
				
		});
	}
	
	function attachGalleryListener() {
		$("div#managerRight .attachGallery").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var url = $(this).attr('href');
			if (url != null) {
				var vars = url.split('/');
			}
			$('div#mediaLoading').show();
			formURL = baseUrl+'gallery/ajax/attach-gallery';
			$.ajax({
				type: "POST",
  				url: formURL,
				data: {
					objId: vars[vars.length-5],
					objType: vars[vars.length-3],
					gallery: vars[vars.length-1]
				},
				success: function(data){
					
				},
				complete: function(data){
					$('div#mediaLoading').hide();
					alert('This gallery has been attached.');
				}
			});
		});
	}
	
	function mediaListener(oGId){
		$("div#managerRight .attachMedia").click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var url = $(this).attr('href');
			if (url != null) {
				var vars = url.split('/');
			}
			$('div#mediaLoading').show();
			formURL = baseUrl+'gallery/ajax/attach-media';
			$.ajax({
				type: "POST",
  				url: formURL,
				data: {
					objId: vars[vars.length-7],
					objType: vars[vars.length-5],
					mediaId: vars[vars.length-3],
					mediaType: vars[vars.length-1]
				},
				success: function(data){
					
				},
				complete: function(data){
					$('div#mediaLoading').hide();
					alert('This media has been attached.');
				}
			});
		});
	}		
	
	$("#urlUpload").live("click", function(event){
			event.preventDefault();
			event.stopPropagation();
			$('ul.vert-one a').eq(2).addClass('current');
			$('ul.vert-one a').eq(0).removeClass('current');
			$('ul.vert-one a').eq(1).removeClass('current');
			$('div#mediaLoading').show();
			var fullUrl = $(this).attr('href');
			if (fullUrl != null) {
				var url = fullUrl.split('/');
			}
			for(var i=0; i < url.length; i++){
				if(url[i]=='gallery') {					
					oGId = url[++i];
				}
				if(url[i]=='objId'){
					objId = url[++i];
				}
				if(url[i]=='objType'){
					objType = url[++i];
				}
			}
			formURL = baseUrl+'gallery/ajax/url';
			$.ajax({
				type: "GET",
				url: formURL,
				data: {
					gallery: oGId,
					objType: objType,
					objId: objId
				},
				success: function(data){
					$('div#managerRight').html(data);
				},
				complete: function(data){
					$('div#mediaLoading').hide();
				}
			});
	});
	
	$("#uploadSubmit").live("click", function(event){
		$("#mediaFormWrap").toggle();
		$('div#managerRight').prepend(
			$('<div id="uploadLoading" class="loading"><p>loading...<\/p><\/div>')	
		);
		$('#mediaUserMsg').text('');
	});
	

	$("#uploadTarget").load(function(event){
	    if(iframeInit) {
			$.ajax({
				type: "GET",
				url: formURL,
				data: {
					gallery: oGId,
					objType: objType,
					objId: objId
				},
				success: function(data){
					$('div#managerRight').html(data);
				},
				complete: function(data){
					$('div#mediaLoading').hide();
				}
			});
	    } else {
	    	iframeInit = true;
	    }
	});
});

function postUploadSubmit(status, title){
	$("#uploadLoading").remove();
	if (status){
		$('#mediaUserMsg').text(title + ' uploaded!');
	}
	else{
		$('#mediaUserMsg').text('error uploading'+ title +'!');
	}
	$("#uploadForm").toggle();
	$("#uploadFile").val('');
	$("#title").val('');
	$("#desc").val('');
}
