/*
 * JavaScript interface to the SB network.
 * class SB.photogallery - manipualtion with the photogallery
 *
 * Requirements: jQuery library 1.4.2 and above
 * Copyright (C) 2011 Tomas Hnilica.   All Rights Reserved.
 */
 
/**
Interface to the profile
*/ 

SB.photogallery = function(fid, init) {
	 if (init == null) init = true;
	 this.doActivity = true;
   this.fid = fid;
   this.id = $('#photogallery').attr('pgid');
   this.admin = false;
   if ($('#photogallery').attr('admin') == "1") this.admin = true;
   if (init) {
		   this.initUI();
		   $('.jq-button').button();
	}      
};

SB.photogallery.prototype.permsSelect = function(key) {
      var code = "&nbsp;<select id='perm_"+key+"'  name='perm_"+key+"'><option value='0'>Všichni</option><option value='5'>Přátelé</option><option value='10'>Nikdo</option></select>";
      return code;
}

SB.photogallery.prototype.initUI = function() {
   
   $('#photogallery-toolbar').html('');
   if (this.admin) {
     /*add controls to the photogallery */
     var code = "<a href='#' onclick='SBphotogallery.addPhotos(); return false;' class='buttonLike fr'><span>přidat fotky</span></a>";
     code += "<a href='#' onclick='SBphotogallery.editPhotoGallery(); return false;' class='buttonLike fr'><span>upravit</span></a>";
     code += "<a href='#' onclick='SBphotogallery.deletePhotoGallery(); return false;' class='buttonLike fr'><span>smazat</span></a>";
     $('#photogallery-toolbar').append(code);
    if ($('#photogallery').attr('new') == '1') this.addPhotos();
 
   }
   
   /*append back button*/
   code = "<a href='"+$('#photogallery').attr('backlink')+"' class='buttonLike fr'><span>"+$('#photogallery').attr('backtitle')+"</span></a>";
   $('#photogallery-toolbar').append(code);
	 
	 /*$('.jq-button').button();*/

}

SB.photogallery.prototype.addPhotos = function() {
			$('#photo-upload').show('slow');
			var uploadHTML = "<h3>Nahrát fotografie</h3>";
			uploadHTML += '<div id="dialog-image-browser-fileQueue"></div>';
			uploadHTML +='	  <input type="file" name="uploadify-image" id="uploadify-image" />';
			uploadHTML +='	  <div style="margin-top:0px; padding-top:0px; display:inline; position:relative; top:-12px;">';
			uploadHTML +='	  <a href="#" onclick="$(\'#uploadify-image\').uploadifyUpload(); return false;" class="jq-button">Nahrát fotky</a>  <a href="#" onclick="$(\'#uploadify-image\').uploadifyClearQueue(); return false;" class="jq-button">Vyčistit frontu</a>';
      uploadHTML +='    <a href="#" onclick="$(\'#photo-upload\').hide(\'slow\');  return false;" class="jq-button">Zavřít</a>';
			uploadHTML +='	  </div>';
			$('#photo-upload').html(uploadHTML);
			var context = this;
		
			
			/*get the session ID from cookies. The uploadify is flash based and the session is not correct  - Adobe bug*/
			var start = document.cookie.indexOf("PHPSESSID=");
			var end = document.cookie.indexOf(";", start); // First ; after start
			if (end == -1) end = document.cookie.length; // failed indexOf = -1
			var cookie = document.cookie.substring(start+10, end);
			
			/*this is a hack and possible security problem*/
			cookie = SESSION_COOKIE;
			
				$("#uploadify-image").uploadify({
					'buttonText'		 : 'Vybrat',
					'uploader'       : '/swf/uploadify.swf',
					'script'         : '',
					'cancelImg'      : '/css/uploadify/cancel.png',
					'folder'         : '/images',
					'queueID'        : 'dialog-image-browser-fileQueue',
					'fileDesc'			 : 'Fotografie (*.jpg)',
					'fileExt'				 : '*.jpg;',
					'auto'           : false,
					'multi'          : true,
					'scriptData'     : { 'PHPSESSID': cookie },
					'onAllComplete'	 : function (event, data) {
												$('#photo-upload').hide();
												/*post feed about this action*/
												var link = jQuery.url.attr("path"); 
												if ($('#photogallery').attr('new') == '1' ) {
											    if (context.doActivity) context.owner.addActivity('založil novou [[a href='+link+' ]]fotogalerii[[/a]]', 'photogallery', context.id, link);
											    //SBpage - distinguish according the owner!!!
											  } else {
											    if (context.doActivity) context.owner.addActivity('přidal nové fotky do galerie [[a href='+link+' ]]'+$('#photogallery-name').html()+'[[/a]]','photogallery', context.id, link);
											  }
												context.getPhotoGallery();
												
						},
					'onComplete'	 : function (evt, queueID, fileObj, response, data) {
									/*alert(response);*/
									
						}
				});
				
				$('.jq-button').button();
				   
};

SB.photogallery.prototype.editPhotoGallery = function() {
   /*close possibly open upload */
   $('#photo-upload').hide('slow');
   
   /*change the data divs to editable*/
   var context = this;
   var name = $('#photogallery-name').html();
   var desc = $('#photogallery-desc').html();  
   
   var namecode = "<tr><td>Název: </td><td><input type='text' name='photogallery-edit-name' value='"+name+"' id='photogallery-edit-name' class='photogallery-edit-name'></td></tr>"; 

   //$('#photogallery-name').html(namecode);

   var desccode = "<tr><td>Popis: </td><td><textarea name='photogallery-edit-desc' id='photogallery-edit-desc' class='photogallery-edit-desc'>" + desc + "</textarea></td></tr>"; 
   //$('#photogallery-desc').html(desccode);
   
   var permcode = "<tr><td>Oprávnění: </td><td>" + this.permsSelect('gperm') + "</td></tr>";
   //$('#photogallery').prepend(permcode);
   
   var ctrlcode = "<table>"+namecode+desccode+permcode+"</table>";
   
   $('#photogallery').prepend(ctrlcode);
   $('#perm_gperm').val($('#photogallery').attr('perm'));
   $('#photogallery-name').remove();
   $('#photogallery-desc').remove();

 
   /*edit single photos*/
    $('.photogallery-photo-desc').each(function() {
      var val = $(this).html();
      var code = "<textarea name='photogallery-photo-desc-edit' class='photogallery-photo-desc-edit'>"+val+"</textarea>";
      $(this).html(code);
      $(this).show();
   });
   var editcode = "<input type='radio' name='photo-default'>Titulní foto<br><input type='checkbox' name='photo-delete'>Smazat";
    $('.photogallery-photo').append(editcode);
    $('.photogallery-photo').addClass('photogallery-photo-edit');
    var titlephoto = $('#photogallery').attr('titlephoto');
    if (titlephoto!='') {
    	var titlediv = $("div[photo="+titlephoto+"]");
    	if (titlediv) {
    	   titlediv.find("input[name=photo-default]").each(function() {
    	      $(this).attr('checked',true);
    	   });
    	}
    }

   
   /*change toolbar*/
   var cancelbtn = "<a href='#' class='buttonLike fr' onclick='SBphotogallery.getPhotoGallery();' id='profile-edit'><span>zrušit</span></a>";
   var savebtn = "<a href='#' class='buttonLike fr' onclick='SBphotogallery.savePhotoGallery();'><span>uložit</span></a>";
   $('#photogallery-toolbar').html(savebtn + cancelbtn);
   $('.jq-button').button();

   return false;
   
};

SB.photogallery.prototype.savePhotoGallery = function() {
    var params = {}
    params['name'] = $('#photogallery-edit-name').val();
    params['desc'] = $('#photogallery-edit-desc').val();
    params['perm'] = $('#perm_gperm').val();
    params['id'] = this.id;
    var context = this;

    /*comments of photos*/
    $('textarea[name=photogallery-photo-desc-edit]').each(function() {
      var val = $(this).val();
      var id = $(this).parent().parent().attr('photo');
      params['comment' + id] = val;
   });
   
   /*title photo*/
   $('#photogallery').find("input[name=photo-default]").each(function() {
      if ($(this).attr('checked')) {
      	params['photo'] = $(this).parent().attr('photo');
      }
   });
   
   /*files to delete*/
   $('#photogallery').find("input[name=photo-delete]").each(function() {
   	if ($(this).attr('checked')) {
        params['delete' + $(this).parent().attr('photo')] = 'yes';
    }
   });
   
		$.post("/ws/?ws=savePhotoGallery", params, function(data){
					context.getPhotoGallery();
	  	});
	  	return false;
};


SB.photogallery.prototype.deletePhotoGallery = function() {
		var context = this;
		var params = {}
		params["ws"] = "deletePhotoGallery";
		params["id"] = this.id;
		
		thConfirm("Skutečně chcete smazat tuto fotogalerii?","Smazat fotogalerii?", function(ret) {
		   if (ret) {
				$.get("/ws/", params, function(data){
						window.location = $('#photogallery').attr('backlink');
				});
		   }
		});
		return false;	
};

SB.photogallery.prototype.getPhotoGallery = function() {
		var params = {}
		params["ws"] = "getPhotoGallery";
		params["id"] = this.id;
		var context = this;
		$.get("/ws/", params, function(data){
					$('#photogallery-box').html(data);
					/*update controls*/
					context.initUI();
					$('a[rel*=lightbox]').lightBoxClone(); 
	  	});
};



