/**
 *  Zajisteni sledovani pozice mysi
 */
var mouseX = 0;
var mouseY = 0;
//var messages = [{}];
var messages = new Array();

$().mousemove( function(e) {
	mouseX = e.pageX;
	mouseY = e.pageY;
});
/**
 *  ==========
 */


/**
 *  Funkce pro zobrazovani tooltipu
 */
var tooltip_element = false;

function show_tooltip(tooltip_text)
{
	show_tooltip_div(tooltip_text);
}

function hidde_tooltip()
{
    hidde_tooltip_div();
}

function show_tooltip_div(tooltip_text)
{
    var top_position 	= mouseY - 170;
	var left_position 	= mouseX + 10;

	if ( tooltip_element )
	{
	    tooltip_element.style.top  = top_position+'px';
	 	tooltip_element.style.left	= left_position+'px'
	}
	else
	{
		paramsDiv      		= new Array();
	    paramsDiv[0]      	= new Array();
	    paramsDiv[0][0]   	= 'id';
	    paramsDiv[0][1]   	= 'tooltip_id';

	 	tooltip_element   	= createNewElement( 'div',   paramsDiv, document.body, true );

	 	if ( tooltip_element )
	 	{
	 	    tooltip_element.innerHTML	= tooltip_text;
	 	    tooltip_element.style.top  = top_position+'px';
	 	    tooltip_element.style.left	= left_position+'px';
		}
	}

}

function hidde_tooltip_div()
{
	if ( tooltip_element ){
    	remove_element(tooltip_element);
    	tooltip_element = false;
	}
}
/**
 *  ==========
 */


/**
 * DOCUMENT READY
 */

$(document).ready(function()
{

    //lightbox
    $(function() {
        $('.lightboxclass').lightBox();
    });

    //schovani prvku kdyz jede JS
    $(".inputDis").hide();

    //odkaz na tisk do PDF
    $("a.out").click(function(){
      window.open($(this).attr("href"),'blank');
      return false;
    });


    //nastylovani tabulek

    $('.table01').each(
        function()
        {
            $(this).find('tr:first').addClass('first');
            $(this).find('tr:odd').addClass('odd');
            $(this).find('tr:even').addClass('even');
        }
        
    );

  
    /*
    $('.table01 tr:first').addClass('first');
    $('.table01 tr:odd').addClass('odd');
    $('.table01 tr:even').addClass('even');
    */

    $(".selectChange").change(
        function()
        {
     
            name  = $(this).attr('name');
            value = $(this).val();
            url = document.location.toString();

            if(gup(name)!="")
            {
               url = url.replace(name+"="+gup(name),name+"="+value);
            }
            else
            {
                sep =  (url.indexOf('?')>0) ? '&' : '?';
                url += sep+name+"="+value;
            }
            location.assign(url);
        }
    );




    //Podstranka Typ B
    if(document.getElementById('TypeBtopBox'))
    {


    }


    //Podstranka Typ B
    if(document.getElementById('TypeCtopBox'))
    {

    }


    var SearchStringClicked = false;
    $("#SearchString").click(function(){ if(!SearchStringClicked) {$("#SearchString").val('');SearchStringClicked=true;} });

    
    $('a.AddBookmark').click(function(){CreateBookmarkLink();return false;});
    $('a.PrintPage').click(function(){window.print();return false;});

    $("a.InformationAboutChanges").colorbox({href:WEB_URL+'ajax.php?modul=information_about_changes',width:'600px',height:'400px',onComplete:function(){validateFormInformationAboutChanges();}});
    $("a.SendToFriend").colorbox({href:WEB_URL+'ajax.php?modul=send_to_friend',width:'600px',height:'500px',onComplete:function(){validateFormSendToFriend();}});


    loadMessagesFromXML();

    validateFormInformationAboutChanges();
    validateFormSendToFriend();
    validateFormContactForm();

    //Infografiky...

    $(".infographic").each(function() {

        lang = $(this).attr('lang');

        tmp = $(this).attr('title').split(';');


        swf  = tmp[0];
        xml  = tmp[1];
        id   = tmp[2];
        width   = tmp[3];
        height   = tmp[4];


        if(swf!='' && xml!='')
        {

            var rand = Math.floor(Math.random()*11);

            var flashvars = {
                xmlPath: "flash/infographic/data/"+lang+"/"+xml+'?rnd='+rand
              };

              var flashparams = {
                quality: "high",
                bgcolor: "#ffffff",
                allowScriptAccess: "sameDomain",
                allowFullScreen: "false",
                allowNetworking: "all",
                scale: "showall",
                wmode:'window'
              };

              var flashattributes = {
                id: 'FlashInfographic_'+id,
                name: 'FlashInfographic_'+id,
                'class': 'infographic'
              };
              swfobject.embedSWF("flash/infographic/"+swf, 'Infographic_'+id, width, height, "10.0.0", "", flashvars, flashparams, flashattributes);

        }

    });




    if(document.getElementById('FormInformationAboutChangesManagement'))
    {
        var bool = true;
        $("#idSelectDeselectAll").click(function ()
          {
             $("#FormInformationAboutChangesManagement .chbox").attr('checked', bool);

             if(bool)
             {
               bool = false;
             }
             else
             {
                bool = true;
             }

             return false;
          });
    }

    
});




//Nacteni jazykovych hlasek do pole

function loadMessagesFromXML()
{
    $.ajax({
        type: "GET",
	url: WEB_URL+"langs/"+LANG+".xml",
	dataType: "xml",
        async:false,
	success: function(xml) {
          $(xml).find("messages").each(function()
          {
            $(this).find("item").each(function()
              {
                messages[$(this).attr('key')] = $(this).text();
              });
          });

          
	}
    });
}

//Zobrazeni hlasky
function get_message(key)
{
    
    if(messages[key])
    {
        return messages[key];
    }
    else
    {
        return key;
    }
}



function validateFormInformationAboutChanges()
{


    

    if(document.getElementById('FormInformationAboutChanges'))
    {

        //Neco malo proti robotum..
        $(".inputDis").hide();
        
        $("#FormInformationAboutChanges").append('<input type="hidden" name="email2" id="idemail2" value="te'+'st" />');
        
        $.validator.setDefaults({
            submitHandler: function() { saveInformationAboutChanges(); }
        });

        $("#FormInformationAboutChanges").validate({
                    rules: {
                            email: {
                                    required: true,
                                    email: true
                            },
                            confirm: "required"

                    },
                    messages: {
                            email: get_message("Please, fill your e-mail address"),
                            confirm: get_message("Please, fill confirmation")

                    }
            });
    }
}


function validateFormSendToFriend()
{

    if(document.getElementById('FormSendToFriend'))
    {

        $(".inputDis").hide();
        $("#FormSendToFriend").append('<input type="hidden" name="email2" id="idemail2" value="te'+'st" />');
        $.validator.setDefaults({
            submitHandler: function() { sendToFriend(); }
        });

        $("#FormSendToFriend").validate({
                    rules: {
                            
                            name: {
                                    required: true
                            },

                            email: {
                                    required: true,
                                    email: true
                            },
                            author_email: {
                                    required: true,
                                    email: true
                            }
                    },
                    messages: {
                            name: get_message("Please, fill your name"),
                            email: get_message("Please, fill your friend e-mail address"),
                            author_email: get_message("Please, fill your e-mail address")


                    }
            });
    }
}

function validateFormContactForm()
{
    if(document.getElementById('FormContactForm'))
    {

        $(".inputDis").hide();
        $("#FormContactForm").append('<input type="hidden" name="email2" id="idemail2" value="te'+'st" />');
  
        $("#FormContactForm").validate({
                    rules: {

                            name: {
                                    required: true
                            },

                            email: {
                                    required: true,
                                    email: true
                            },
                            confirm: "required",
                            question: "required"
                    },
                    messages: {
                            name: get_message("Please, fill your name"),
                            email: get_message("Please, fill your e-mail address"),
                            confirm: get_message("Please, fill confirmation"),
                            question: get_message("Please, fill your question")
                    }
            });
    }
    
}

//AJAX - doporuceni stranky priteli

function sendToFriend()
{


    email = $("#idemail").val();
    name  = $("#idname").val();
    email2  = $("#idemail2").val();
    author_email  = $("#idauthoremail").val();
    personal_message  = $("#idpersonalmessage").val();

    title = document.title;
    url   = document.location;


    $.ajax({
        global: false,
        cache: false,
        type: "POST",
	url: WEB_URL+'ajax.php',
        data:'modul=send_to_friend_exe&email='+email+'&email2='+email2+'&name='+name+'&title='+title+'&url='+url+'&personal_message='+personal_message+'&author_email='+author_email,
	dataType: "json",
	success: function(msg2) {


          status2 = msg2['status'];

          if(status2 == 2)
          {
                alert(get_message('Please, fill obligatory items'));
          }
          else
          {
                if(status2 == 1) {
                    alerttxt = get_message('Form was sent');
                }else{
                    alerttxt = get_message('Sending error, please try it later');
                }

                $("#FormSendToFriend").html('<div><label class="error">'+alerttxt+'</label></div>');

          }

	}
    });
}


//AJAX - doporuceni stranky priteli

function saveInformationAboutChanges()
{


    email = $("#idemail").val();
    url   = document.location;
    email2  = $("#idemail2").val();



    $.ajax({
        global: false,
        cache: false,
        type: "POST",
	url: WEB_URL+'ajax.php',
        data:'modul=information_about_changes_exe&email='+email+'&email2='+email2+'&url='+url,
	dataType: "json",
	success: function(msg2) {

          status2 = msg2['status'];
 
          if(status2 == 2)
          {
              alert(get_message('Please, fill obligatory items'));
          }
          else
          {
            alert2 = (status2 == 1) ? get_message('Form was sent') : get_message('Sending error, please try it later');

            $("#FormInformationAboutChanges").html('<div><label class="error">'+alert2+'</label></div>');

          }

	}
    });
}


function CreateBookmarkLink() {

        url = document.location;
        title = document.title;

        if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
  	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
        }
        else
        {
                alert('Unfortunately, this browser does not support the requested action');
        }

        return false;

 }


 function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


/**
 * Specialni pro JQUERY
 *--------------------------------------
 *
 */

jQuery.fn.toggleLeftHPBoxInfo = function ($pixelsToView) {

  $reserve = -1*(this.outerWidth() - $pixelsToView);

  this.animate({
  left: parseInt(this.css('left'),10) == 0 ?
    $reserve :
    0
  },
  {complete:function(){
                if(parseInt($(this).css('left'),10)<0){
                    $("#HPBoxInfoButton").addClass('showInfoBox');
                }
                else
                {
                    $("#HPBoxInfoButton").removeClass('showInfoBox');
                }
            }
  }
  );
  
 }

jQuery.fn.toggleDown = function () {
  elTop = parseInt(this.css('top'),10);
  parentHeight = this.parent().outerHeight();
  this.animate({top: elTop == parentHeight ? (parentHeight-this.outerHeight()) : parentHeight });
  
 }

jQuery.fn.toggleDownLayoutTypeA = function () {
  elTop = parseInt(this.css('margin-top'),10);
  
  this.animate({marginTop: elTop > 0 ? '0px' : '443px' });

 }

jQuery.fn.toggleDownLayoutTypeB = function () {
  elTop = parseInt(this.css('margin-top'),10);

  this.animate({marginTop: elTop > 0 ? '0px' : '543px' });

 }

jQuery.fn.toggleTop = function ($pixelsToView,$bgVisual) {
  var elTop = parseInt(this.css('top'),10);
  var el = this.attr('id');
  var bg = 'url(./images/layout/'+$bgVisual+')';

  
  if(elTop < 0 && $bgVisual!=''){
      this.css('background-image', bg);
  }


  //$reserve = $pixelsToView;
  $reserve = -1*(this.outerHeight() - $pixelsToView);
  
  this.animate({top: elTop == $reserve ? 0 : $reserve },{complete:function(){
          if(elTop == 0 && $bgVisual!=''){$("#"+el).css('background-image', 'none')}
       }
  });

  
  

 }



/**
 * Validace telefonu pomoci JQuery validatoru
 */
  $.validator.addMethod("phone", function(value) {

    var reg_mobil1 = "^[1-9]{1}[0-9]{8}$";
    var reg_mobil2 = "^00[0-9]{12}$";
    var reg_mobil3 = "^[\+][0-9]{12}$";

    value = value.replace(/ /g,"");
    if(value.length){
    return ( value.match(reg_mobil1) || value.match(reg_mobil2) || value.match(reg_mobil3));
    }
    return true;
  }, "Telefon");

    

