/*************************************************************************/
/*                                                                       */
/*    project:  GROUP OFFICE web 2008                                    */
/*    code:     group_office_script.js                                   */
/*    author:   Radovan Čapek                                            */
/*    date:     18.3.2009                                                */
/*                                                                       */
/*************************************************************************/
var activeEl = null;
/********************************************************************/
/*     HomePage scripts                                             */
/*     requirements : jQuery.js                                     */
/********************************************************************/
$(document).ready(
/********************************************************************/
/*     HomePage scripts                                             */
/*     requirements : jQuery.js                                     */
/********************************************************************/
    function () {
    /********************************************************************/
    /*     for the begining...                                          */
    /********************************************************************/
    $('.srt_top').bind('click', showDetail);
    $('.confirm').bind('click', confirm_file );
    /********************************************************************/
    /*     script name: serve login input                               */
    /*     describe: change style (ev.text) of input                    */
    /********************************************************************/
    var login_form_str = "vaš login";
    var login_err_form_str = "zadejte svůj login";
    
    $('.t_input')
			.bind(
				'focus',                    // on focus
				function()
				{
					if($(this).attr("value") == login_form_str || $(this).attr("value") == login_err_form_str) {
              $(this).attr("value", "");
              $(this).css("color", "#424242");
              //$(this).css("fontSize", "12px");
          }
				}
			)
			.bind(
				'blur',                     // on blur
				function()
				{
					if(!(/\S/.test($(this).attr("value")))) {
              $(this).attr("value", login_form_str);
              $(this).css("color", "#989898");
              //$(this).css("fontSize", "11px");
          }
				}
			)
    /********************************************************************/
    /*     script name: serve password input                            */
    /*     describe: change style (ev.text) of input                    */
    /********************************************************************/
    var pwd_form_str = "*****";
    
    $('.t_input_pwd')
			.bind(
				'focus',                    // on focus
				function()
				{
					if($(this).attr("value") == pwd_form_str) {
              $(this).attr("value", "");
              //$(this).attr("type", "password");
              $(this).css("color", "#424242");
              //$(this).css("fontSize", "12px");
          }
				}
			)
			.bind(
				'blur',                     // on blur
				function()
				{
					if(!(/\S/.test($(this).attr("value")))) {
              $(this).attr("value", pwd_form_str);
              //$(this).attr("type", "text");
              $(this).css("color", "#989898");
              //$(this).css("fontSize", "11px");
          }
				}
			)
		
		$('#login_form_submit')
			.bind(
				'click',                    // on click
				function(e)
				{
					if(!(/\S/.test($(".t_input").attr("value"))) || $(".t_input").attr("value") == login_form_str || $(".t_input").attr("value") == login_err_form_str) {
              e.preventDefault();
              $(".t_input").attr("value","zadejte svůj login");
              $(".t_input").css("color","#ff0000");
          }
          if(!(/\S/.test($(".t_input_pwd").attr("value"))) || $(".t_input_pwd").attr("value") == pwd_form_str) {
              e.preventDefault();
              alert("Prosím ,zadejte své heslo.");
          }
				}
			)
    /********************************************************************/
    /*     script name: serve password input                            */
    /*     describe: change style (ev.text) of input                    */
    /********************************************************************/
    $('#nav_menu li')
			.bind(
				'click',                    // on click
				function()
				{
					if($("ul", this).css("display") == "none") 
              $("ul", this).stop().slideDown();
          else
              $("ul", this).stop().slideUp();
				}
			)
		/********************************************************************/
    /*     script name: search input                                    */
    /*     describe: delete text "Search..." in search input            */
    /********************************************************************/
    var search_text = "hledání...";
    
    $('#search_text')
			.bind(
				'click',                    // on click
				function()
				{
  					if($(this).attr("value") == search_text) {
                $(this).attr("value","");
                $(this).css("color","#000000");
                $(this).css("font-style","normal");
            }
				}
			)
    /********************************************************************/
    /*     PHORUM STUFF                                                 */
    /********************************************************************/    	
			
		/********************************************************************/
    /*     script name: show topic detail                               */
    /*     describe: ajax calling body of text phorum                   */
    /********************************************************************/
    $('a.topic_name')
			.bind(
				'mouseover',                    // on mouse over
				function(e)
				{ 
          activeEl = this;
          var topic_id = $(this).attr("href");
          var offset = topic_id.indexOf("?id=");
          topic_id = topic_id.substr(offset+4);
          offset = topic_id.indexOf("&");
          topic_id = topic_id.substring(0,offset);
          $(this).delayAndShow(1000, this, "phorum", topic_id, e);
				}
			)
			$('a.topic_name')
			.bind(
				'mouseout',                    // on mouse out
				function(e)
				{ 
          activeEl = null;
          if($('#info_panel').css("display") != "none")
              $('#info_panel').fadeTo("normal",0,function(){$(this).hide();});
				}
			)	
		/********************************************************************/
    /*     script name: show author detail                              */
    /*     describe: ajax calling of info about author                  */
    /********************************************************************/
    $('a.author_name')
			.bind(
				'mouseover',                    // on mouse over
				function(e)
				{ 
          activeEl = this;
          $(this).delayAndShow(1000, this, "login", $(this).html(), e);
				}
			)
			$('a.author_name')
			.bind(
				'mouseout',                    // on mouse out
				function(e)
				{ 
          activeEl = null;
          if($('#info_panel').css("display") != "none")
              $('#info_panel').fadeTo("normal",0,function(){$(this).hide();});
				}
			)	
		/********************************************************************/
    /*     script name: show file detail                                */
    /*     describe: ajax calling of info about file                    */
    /********************************************************************/
    $('a.file_link')
			.bind(
				'mouseover',                    // on mouse over
				function(e)
				{ 
          activeEl = this;
          var file_id = $(this).attr("href");
          var offset = file_id.indexOf("file_id=");
          file_id = file_id.substr(offset+8);
          $(this).delayAndShow(1000, this, "file", file_id, e);
				}
			)
			$('a.file_link')
			.bind(
				'mouseout',                    // on mouse out
				function(e)
				{ 
          activeEl = null;
          if($('#info_panel').css("display") != "none")
              $('#info_panel').fadeTo("normal",0,function(){$(this).hide();});
				}
			)
		/********************************************************************/
    /*     script name: show file detail                                */
    /*     describe: ajax calling of info about file                    */
    /********************************************************************/
    $('a.file_topic_link')
			.bind(
				'mouseover',                    // on mouse over
				function(e)
				{ 
          activeEl = this;
          var file_id = $(this).attr("href");
          var offset = file_id.indexOf("file_id=");
          file_id = file_id.substr(offset+8);
          $(this).delayAndShow(1000, this, "topic_file", file_id, e);
				}
			)
			$('a.file_topic_link')
			.bind(
				'mouseout',                    // on mouse out
				function(e)
				{ 
          activeEl = null;
          if($('#info_panel').css("display") != "none")
              $('#info_panel').fadeTo("normal",0,function(){$(this).hide();});
				}
			)		
		/********************************************************************/
    /*     script name: show upload form                                */
    /*     describe: change html code of upload box to form             */
    /********************************************************************/
    $('#upload_box a')
			.bind(
				'click',                    // click
				function(e)
				{ 
          var html = '<input type="file" name="new_file" class="input" />';
              html+= '<textarea name="file_describe" cols="31" rows="20"></textarea>';
              html+= '<input type="submit" name="file_send" id="upload_box_button" value="Nahrát" />';
          $('#upload_form').html(html);
				}
			)
		/********************************************************************/
    /*     script name: serve password input                            */
    /*     describe: change style (ev.text) of input                    */
    /********************************************************************/
    $('#mail_answers')
			.bind(
				'click',                    // on click
				function()
				{ 
          if($(this).attr("checked"))
					    $('#client_mail').attr("disabled","");           
          else
              $('#client_mail').attr("disabled","disabled");
				}
			)
		/********************************************************************/
    /*     script name: enable attachment                               */
    /*     describe: show/hide, add/remove file input                   */
    /********************************************************************/
    $('#attach')
			.bind(
				'click',                    // on click
				function()
				{ 
          if($('.form_file_list').css("display") == "none") {
					    $('.form_file_list').slideDown();
					    $('#attach').html('<img src="../img/icon_topic_attach_cancel.gif" alt="sponka" />&nbsp;&nbsp;zrušit přílohy?');
					    }
          else {
              $('.form_file_list').slideUp();
              $('#attach').html('<img src="../img/icon_topic_attach.gif" alt="sponka" />&nbsp;&nbsp;připojit soubor?');
              $('#files_count').attr('value', 0);
              $('#attach_field').html('<span  class="form_file_list"><input type="file" name="file1" size="50" class="new_file_input" onchange="insertFileInput(this)"/></span>');
          }
				}
			)
		/********************************************************************/
    /*     script name: switch intro page                               */
    /*     describe: switch prologue and contact page                   */
    /********************************************************************/
    $('#intro1')
			.bind(
				'click',                    // on click
				function(e)
				{ 
          e.preventDefault();
          $('#intro_page1').css('display','block');
          $('#intro_page2').css('display','none');
          $('#intro_page3').css('display','none');
				}
			)
		$('.intro2')
			.bind(
				'click',                    // on click
				function(e)
				{ 
          e.preventDefault();
          $('#intro_page2').css('display','block');
          $('#intro_page1').css('display','none');
          $('#intro_page3').css('display','none');
				}
			)
		$('.intro3')
			.bind(
				'click',                    // on click
				function(e)
				{ 
				  e.preventDefault();
          $('#intro_page3').css('display','block');
          $('#intro_page2').css('display','none');
          $('#intro_page1').css('display','none');
				}
			)
		/********************************************************************/
    /*     script name: serve password input                            */
    /*     describe: change style (ev.text) of input                    */
    /********************************************************************/
    $('.file_input')
			.bind(
				'change',                    // on click
				function()
				{ 
          insertFileInput(this);
				}
			)
		/********************************************************************/
    /*     script name: serve password input                            */
    /*     describe: change style (ev.text) of input                    */
    /********************************************************************/
			$('#profil_info div input')
			.bind(
				'focus',                    // on focus
				function()
				{
					$(this).css("backgroundColor", "#ddddff");
				}
			)
			.bind(
				'blur',                     // on blur
				function()
				{
					$(this).css("backgroundColor", "transparent");
				}
			)
    /********************************************************************/
    /*     script name: get files names                                 */
    /*     describe: ajax connect to database and get file names        */
    /********************************************************************/
    $('#file_searcher')
			.keyup(function (e) {         // on keypress
					
          var param  = $('#file_searcher').attr('value');
                  
          if(param.length >= 3) {
          
              $('#file_suggest').css("display","block");
              var param2 = $('#member_select').attr('value');
              var param3 = $('#year_select').attr('value');
              var id     = $('#session_id').attr('value');
              var lvl    = $('#access_lvl').attr('value');
          
              $.ajax({ 
                          method: "get",url: "../include/ajax.php?",data: "action=search_file&param="+param+"&param2="+param2+"&param3="+param3+"&mid="+id+"&lvl="+lvl, 
                          success: function(html){
                              $('#file_suggest').html("");
                              address_array = html.split('|');
                              for(i=0;i<address_array.length;i++) {
                                  $('#file_suggest').append(address_array[i]);
                              }
                            
                           } 
                        }); //close $.ajax
            }
				}
			)
			$('#member_select')
			.bind('change',function (e) {         // on keypress
					
          var param  = $('#file_searcher').attr('value');
                  
          if(param.length >= 3) {
          
              $('#file_suggest').css("display","block");
              var param2 = $('#member_select').attr('value');
              var param3 = $('#year_select').attr('value');
              var id     = $('#session_id').attr('value');
              var lvl    = $('#access_lvl').attr('value');
          
              $.ajax({ 
                          method: "get",url: "../include/ajax.php?",data: "action=search_file&param="+param+"&param2="+param2+"&param3="+param3+"&mid="+id+"&lvl="+lvl, 
                          beforeSend: function() {
                                         $('#file_suggest').html("Načítám data");
                          },
                          success: function(html){
                              $('#file_suggest').html("");
                              address_array = html.split('|');
                              for(i=0;i<address_array.length;i++) {
                                  $('#file_suggest').append(address_array[i]);
                              }
                            
                           } 
                        }); //close $.ajax
            }
				}
			)
			$('#year_select')
			.bind('change',function (e) {         // on keypress
					
          var param  = $('#file_searcher').attr('value');
                  
          if(param.length >= 3) {
          
              $('#file_suggest').css("display","block");
              var param2 = $('#member_select').attr('value');
              var param3 = $('#year_select').attr('value');
              var id     = $('#session_id').attr('value');
              var lvl    = $('#access_lvl').attr('value');
          
              $.ajax({ 
                          method: "get",url: "../include/ajax.php?",data: "action=search_file&param="+param+"&param2="+param2+"&param3="+param3+"&mid="+id+"&lvl="+lvl, 
                          beforeSend: function() {
                                         $('#file_suggest').html("Načítám data");
                          },
                          success: function(html){
                              $('#file_suggest').html("");
                              address_array = html.split('|');
                              for(i=0;i<address_array.length;i++) {
                                  $('#file_suggest').append(address_array[i]);
                              }
                            
                           } 
                        }); //close $.ajax
            }
				}
			)
		/********************************************************************/
    /*     script name: zoom logo                                       */
    /*     describe: zoom in/out logo on map                            */
    /********************************************************************/
			$('#main_map a img')
			.bind(
				'mouseenter',                    // on mouse enter
				function()
				{
          //$(this.parentNode).animate( {left: parseInt($(this.parentNode).css("left"))-26+"px", top: parseInt($(this.parentNode).css("top"))-7+"px"}, 500);
          $(this).stop().animate( {width: "120px"}, 500);
				}
			)
			.bind(
				'mouseout',                     // on mouse out
				function()
				{
					//$(this.parentNode).animate( {left: parseInt($(this.parentNode).css("left"))+26+"px", top: parseInt($(this.parentNode).css("top"))+7+"px"}, 350);
          $(this).stop().animate( {width: "60px"}, 350);
				}
			)
		/********************************************************************/
    /*     script name: DIR refresh                                     */
    /*     describe: refresh list of files from Catalogue directory     */
    /********************************************************************/			
		$('#dir_refresh')
			.bind(
				'click',                    // on click
				function(e)
				{ 
            e.preventDefault();
            $.ajax({ 
                          method: "get",url: "../include/ajax.php?",data: "action=dir_refresh", 
                          beforeSend: function() {
                                  $('#dirContent').html("Prosím čekejte...");
                          },
                          success: function(html){
                              $('#dirContent').html(html);
                           } 
            }); //close $.ajax
				}
			)
    /********************************************************************/
    /*     script name: password input script                           */
    /*     describe: remove input text on focus                         */
    /********************************************************************/
    $('#phorum_edit_button')
			.bind(
				'click',                    // on focus
				function(e)
				{
				     var is_valid = true;
             
             if($('#ta_edit_text').attr("value") == "") {
                
                e.preventDefault();
                $(".report_bubble").remove();
                
                var position = $('#ta_edit_text').position();
                var top      = position.top -25;
                var left     = position.left + 200;
                var text     = "Nezadán&nbsp;text&nbsp;k&nbsp;přidání";
                
				        $('#ta_edit_text').after("<div class='report_bubble' style='top:"+top+"px;left:"+left+"px;'><div class='rb_content'>"+text+"</div><div class='rb_bottom'></div></div>");
             }
				}
		  )
/********************************************************************/
/*     end of JQUERY.document.ready                                 */
/********************************************************************/			
    }
);

/********************************************************************/
/*     script name: confirm file for OG or designer                 */
/*     describe: move catalogue file to folder for OG/designer      */
/********************************************************************/			
var confirm_file = function(e)
{
  			//e.preventDefault();
        var file = $(this).attr("file");
        $.ajax({ 
                      method: "get",url: "../include/ajax.php?",data: "action=confirm&file="+file, 
                      success: function(html){
                              alert(html)
                      } 
        }); //close $.ajax
        return false;
}

var confirm_file2 = function(elem)
{
  			//e.preventDefault();
        var file = $(elem).attr("file");
        $.ajax({ 
                      method: "get",url: "../include/ajax.php?",data: "action=confirm&file="+file, 
                      success: function(html){
                              alert(html)
                      } 
        }); //close $.ajax
        return false;
}
/********************************************************************/
/*     script name: delayAndShow                                    */
/*     describe: wait and call AJAX                                 */
/********************************************************************/
jQuery.fn.delayAndShow = function(time, elem, act, param, e){
	return this.each(function(){    
    setTimeout(function(){if(elem == activeEl) {
            
            //var content_show = $("a", elem).attr("DOCID"); //retrieve title of link so we can compare with php file 
            $.ajax({ 
                      method: "get",url: "../include/ajax.php?",data: "action="+act+"&param="+param, 
                      beforeSend: function() {
                                      
                                      var left_pos = e.pageX + 30;
                                      $("#info_panel").css("top",e.pageY);
                                      $("#info_panel").css("left",left_pos);
                                      $("#info_panel").stop().show().fadeTo("slow",1);
                                      $("#ip_content").html('<img src="../img/loading.gif" alt="Načítám" class="ip_loading" />');                                  
                      },
                      //complete: function(){ $(".loading").hide("fast");}, //stop showing loading when the process is complete 
                      success: function(html){ //so, if data is retrieved, store it in html 
                          //$("div.article_detail",elem).html(html); //show the html inside .content div
                          //$("div.article_detail",elem).stop().show().fadeTo(600,1);
                          $('#ip_content').html(html);
                          setBubblePosition(e, 0);
                       } 
                    }); //close $.ajax( 
                  
    }}, time);
	});
};
/********************************************************************/
/*     script name: getClientHeight                                 */
/*     describe: return client height                               */
/********************************************************************/
function getClientHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
/********************************************************************/
/*     script name: getScrollY                                      */
/*     describe: return pixel count page scroll                     */
/********************************************************************/
function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}
/********************************************************************/
/*     script name: Show search detail                              */
/*     describe: show/hide body of search result                    */
/********************************************************************/
var showDetail = function(e)
{
      var targetContent = $('div.srt_body', this.parentNode);
     	if (targetContent.css('display') == 'none') {
       		targetContent.slideDown(300);
       		$("img",this.parentNode).attr('src','../img/arrow_down.gif');
       		$("img",this.parentNode).attr('alt','[-]');
     	} else {
       		targetContent.slideUp(300);
       		$("img",this.parentNode).attr('src','../img/arrow_right.gif');
          $("img",this.parentNode).attr('alt','[+]');
     	}
     	return false;
};
/********************************************************************/
/*     script name: Set Bubble Position                             */
/*     describe: check/modified position of detail bubble           */
/********************************************************************/
var setBubblePosition = function(e, ad_y)
{ 
            
            var ad_height = $("#info_panel").height();
            ad_height = parseInt(ad_height);            
            var ad_start = e.pageY;
            
            var ch = getClientHeight();
            var sy = getScrollY();
            
            
            var ip_right = $("#ip_content").height();
            ip_right = parseInt(ip_right);
            ip_right +=31;
            $("#ip_right").css("height", ip_right+"px");
       		  
            if (ad_start + ad_height > ch + sy ) {
                var diference = (ad_start + ad_height) - (ch + sy);
           	   $("#info_panel").css("top", ad_start-diference-10+"px" );
           	}
           	else {
               $("#info_panel").css("top", ad_start+"px" );
            }
}
/********************************************************************/
/*     script name: insertFileInput                                 */
/*     describe: add new file input, if needed                      */
/********************************************************************/
insertFileInput = function(elem){
    
    if(elem.value != "") {
      
      if($('.new_file_input:last').attr('value') != "") {
          
          var count = $('#files_count').attr('value');
          if(count<4) {
          
              count++; 
              $('#files_count').attr('value',count);
              count++;
              $(elem).after('<br /><input type="file" name="file'+count+'" size="50" class="new_file_input" onchange="insertFileInput(this)" />');
          }
          else
              $('#files_count').attr('value', 5);
      }
    }
}
