// JavaScript Document

(function( $ ){
$.jqhelper = function(text, options) {

var himage,xml,exp_date,cookies,jqhhidden;
var animation = new Array();
var jqhelper = new Array();

// cookie functions from http://www.elated.com/articles/javascript-and-cookies/
function setCookie(name, value)
{
  var cookie_string=name+"="+escape(value);
  cookie_string+="; expires="+exp_date;
  document.cookie=cookie_string;
} 

function getCookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results ) return ( unescape ( results[2] ) );
  else return null;
}

function jqhelperHide()
{
   if (cookies=="on")
   {
     setCookie("helperHidden",'true');
     setCookie("startAnimation",'true'); 
   }
   $("#jqhelper").remove();
   $(".jqhbubble").remove();
   $(".jqhbmenu").remove();
}

function jqhelperBubbleReplace(x,y)
{
  var pos = $("#jqhelper").offset();
  var height=$(".jqhbubble").height(); 
  var width=$(".jqhbubble").width();
  //show the menu directly over the placeholder
  $(".jqhbubble").css( { "left": (pos.left - width+parseInt(x)) + "px", "top": (pos.top-height+parseInt(y)) + "px" } ).css("height",height + "px");  
}
 
function writeCookies()  
{
  if (cookies=="on")
  { 
    setCookie("posX",$("#jqhelper").css("left"));
    setCookie("posY",$("#jqhelper").css("top"));  
    setCookie("helperID",$("#jqhelper").attr("name"));
    //setCookie("helperHidden",'true');
  }  
}

  var settings = {
      'forcedDisplay'    : 'false',
      'location'         : 'top',
      'background-color' : 'blue'
    };

  $.extend( settings, options );

  var right_button="kill";
  var bubble_width="30%"; 
  var bubble_border="20";
  var bubble_radius="15";
  var bubble_vanish="interval";
  var vanish_interval="5000"; 
  var bubble_close="click";
  var bubble_tail_width="50";
  var bubble_tail_upper_width="30";
  var bubble_tail_height="50";
  var bg_color,text_color; 
  var posX=getCookie("posX");
  var posY=getCookie("posY");
  var bubbleOffsetX=0;
  var bubbleOffsetY=0;
  var jqhhidden=getCookie("helperHidden");
  if (jqhhidden==null) jqhhidden='false';
  var helperID=getCookie("helperID");         //alert(helperID);
  cookies="off";
  $.ajax({
    type: "GET",
	  url: "jqhsettings.xml",
	  dataType: "xml",
	  success: function(xml) 
    { // nacteni parametru z xml
      right_button=$(xml).find("right_btn").text();
      bubble_width=$(xml).find("bubble_width").text()+$(xml).find("bubble_width_units").text();
      bubble_border=$(xml).find("bubble_border_width").text();
      bubble_radius=$(xml).find("corner_radius").text();  
      bubble_vanish=$(xml).find("bubble_vanish").text(); 
      bubble_close=$(xml).find("bubble_close").text(); 
      vanish_interval=$(xml).find("vanish_interval").text(); 
      bubble_tail_width=$(xml).find("bubble_tail_width").text(); 
      bubble_tail_upper_width=$(xml).find("bubble_tail_upper_width").text(); 
      bubble_tail_height=$(xml).find("bubble_tail_height").text();
      bg_color=$(xml).find("color-bg").text();
      text_color=$(xml).find("color-text").text(); 
      menu_color=$(xml).find("color-menu").text();
      menubg_color=$(xml).find("color-menubg").text(); 
      cookies=$(xml).find("cookies").text();
      if (helperID==null || helperID=="undefined")
        helperID=$(xml).find("helper").attr("id");
      //alert($(xml).find("helper[id='"+helperID+"']").find("name").text()); 
      bubbleOffsetX=$(xml).find("helper[id='"+helperID+"']").find("vertical_bubble_shift").text();
      bubbleOffsetY=$(xml).find("helper[id='"+helperID+"']").find("horizontal_bubble_shift").text();
      //alert(bubbleOffsetX+", "+bubbleOffsetY);
      if($(xml).find("helper[id='"+helperID+"']").next().length == 0) 
      {
        jqhelper["nextName"]=$(xml).find("helper").first().find("name").text();
        jqhelper["nextID"]=$(xml).find("helper").first().attr("id");
      }  
      else
      {
        jqhelper["nextName"]=$(xml).find("helper[id='"+helperID+"']").next().find("name").text();
        jqhelper["nextID"]=$(xml).find("helper[id='"+helperID+"']").next().attr("id");
      } 
      if ($(xml).find("helper").length == 0) //if helper not found in xml
        alert("Chybějící specifikace pomocníka v souboru jqhsettings.xml!");
      else         
      { 
        if (jqhhidden=='true' && $(xml).find("user-hide").text()=="on")//if hidden & user-hide 
        { 
          if (settings["forcedDisplay"]=='true')
          {
            jqhhidden='false';
            if (cookies=="on") setCookie("helperHidden",'false');
          }
        }                                         
        if (jqhhidden=='false' || $(xml).find("user-hide").text()!="on")//if hidden & user-hide 
        {
          if (cookies=="on") setCookie("helperHidden","false"); 
          var himagesrc=$(xml).find("helper[id='"+helperID+"']").find("animation[type='image']").text();
          var startAnimation=getCookie("startAnimation");
          if (startAnimation==null || startAnimation=="undefined") startAnimation=='true';
          if (cookies=="on" && startAnimation=='true') // if user-hide start animation TODO cookies, start only at first appearance, not every refresh
          {
            if ($(xml).find("helper[id='"+helperID+"']").find("animation[type='start']").text()!="")
              himagesrc=$(xml).find("helper[id='"+helperID+"']").find("animation[type='start']").text();
            setCookie("startAnimation",'false');              
          }  
          else if (text.length>0 && $(xml).find("helper[id='"+helperID+"']").find("animation[type='bubble']").text()!="")
            himagesrc=$(xml).find("helper[id='"+helperID+"']").find("animation[type='bubble']").text();        
         
          $("#jqhelper").remove();
            
          if (jqhhidden=='false' || $(xml).find("user-hide").text()!="on") // if not hidden by user (first apperanace) or not allowed user-hide then append helper
          {
            himage = $('<img />').attr("src",himagesrc).attr("id","jqhelper").attr("name",helperID);
            $("body").append(himage);
            himage.css("position","fixed");
            if (posX==null || posY==null) himage.css("right","2em").css("bottom","2em");
            else himage.css("top",posY).css("left", posX);  
            himage.css("z-index","200").draggable();
            himage.bind("contextmenu", function(e){ 
              if (right_button=="kill")
              {
                e.preventDefault(); 
                jqhelperHide();            
              }
              else if (right_button=="menu")
              {
                e.preventDefault(); 
                $(".jqhbmenu").remove();
                $("body").append('<div class="jqhbmenu">'+
                  '<div class="jqhbmchange">Změnit pomocníka: '+jqhelper["nextName"]+'</div>'+
                  '<div class="jqhbmanimate">Animovat!</div>'+
                   '<div class="jqhbmexit">Skrýt pomocníka</div>'+
                 '</div>');
                $(".jqhbmenu").css("z-index","300").css("border","1px solid #"+menu_color).css("width","20em");
                $(".jqhbmenu div").css("color","#"+menu_color).css("background-color","#"+menubg_color).css("padding","0.2em").css("cursor","pointer");
                $(".jqhbmenu div").mouseover(function(){$(this).css("background-color","#"+menu_color).css("color","#"+menubg_color)});
                $(".jqhbmenu div").mouseout(function(){$(this).css("color","#"+menu_color).css("background-color","#"+menubg_color)});
                $(".jqhbmenu").offset({ top: e.pageY-$(".jqhbmenu").height(), left: e.pageX-$(".jqhbmenu").width() });
                //$(".jqhbmenu").mouseout(function(e){if($.contains(this, e.relatedTarget) == false) {$(".jqhbmenu").remove();}});
                $(".jqhbmenu").mouseleave(function(e){$(".jqhbmenu").remove();});
                $(".jqhbmenu div").click(function(){$(".jqhbmenu").hide()});
                $(".jqhbmchange").click(function() {
                  helperID=jqhelper["nextID"];
                  setCookie("helperID",helperID);
                  $.jqhelper("Ahoj, já jsem "+$(xml).find("helper[id='"+helperID+"']").find("name").text());
                });
                $(".jqhbmanimate").click(function(){
                  //alert("hell"); 
                  if ($(xml).find("helper[id='"+helperID+"']").find("animation[type='user']").text()!="")
                    $("#jqhelper").attr("src",$(xml).find("helper[id='"+helperID+"']").find("animation[type='user']").text());
                }); 
                $(".jqhbmexit").click(function(){jqhelperHide();});             
              }
            });
            himage.bind("drag", function(event, ui) {
              jqhelperBubbleReplace(bubbleOffsetX,bubbleOffsetY);
              writeCookies();
            });
          }   
          $(".jqhbubble").remove();
          if (text.length>0)
          {
            if (bubble_close=="icon")
            {
              var close_icon=$(xml).find("close_icon").text(); 
              text="<img alt=\"X\" id=\"jqhb_close_icon\" src=\""+close_icon+"\">"+text;
            }
            var border=parseInt(bubble_radius)+parseInt(bubble_border); // výška a šířka horního a bočních elementů bubliny     
            var bborder=+parseInt(bubble_tail_height)+parseInt(bubble_radius); // výška spodních elementů bubliny      
            $("body").append('<div class="jqhbubble">'+
              '<div class="jqhburc">&nbsp;</div><div class="jqhbulc">&nbsp;</div><div class="jqhbtop">&nbsp;</div>'+
               '<div class="jqhbleft"><div class="jqhbright"><div class="jqhbcenter">'+text+'</div></div></div>'+
               '<div class="jqhbdrc">&nbsp;</div><div class="jqhbtail">&nbsp;</div><div class="jqhbdlc">&nbsp;</div><div class="jqhbbottom">&nbsp;</div>'+
             '</div>');
            $(".jqhbubble .jqhburc").css("background-image","url('images/jqhelper/jquery-helper-urcorner.gif')").css("float","right");
            $(".jqhbubble .jqhbulc").css("background-image","url('images/jqhelper/jquery-helper-ulcorner.gif')").css("float","left");
            $(".jqhbubble .jqhbdrc").css("background-image","url('images/jqhelper/jquery-helper-drcorner.gif')").css("float","right");
            $(".jqhbubble .jqhbdlc").css("background-image","url('images/jqhelper/jquery-helper-dlcorner.gif')").css("float","left");
            $(".jqhbubble .jqhbtail").css("background-image","url('images/jqhelper/jquery-helper-tail.gif')").css("width",bubble_tail_width+"px").css("height",bborder+"px").css("float","right");
            $(".jqhbubble .jqhburc,.jqhbubble .jqhbulc").each(function(){$(this).css("width",border+"px").css("height",border+"px");});
            $(".jqhbubble .jqhbdrc,.jqhbubble .jqhbdlc").each(function(){$(this).css("width",border+"px").css("height",bborder+"px").css("background-repeat","no-repeat");});
            $(".jqhbubble .jqhbtop").css("background","transparent url('images/jqhelper/jquery-helper-top.gif') top left repeat-x scroll").css("height",border+"px");
            $(".jqhbubble .jqhbbottom").css("background","transparent url('images/jqhelper/jquery-helper-bottom.gif') top left repeat-x scroll").css("height",bborder+"px");
            $(".jqhbubble .jqhbleft").css("background","#"+bg_color+" url('images/jqhelper/jquery-helper-left.gif') top left repeat-y scroll");
            $(".jqhbubble .jqhbright").css("background","transparent url('images/jqhelper/jquery-helper-right.gif') top right repeat-y scroll").css("color","#"+text_color);
            $(".jqhbubble .jqhbcenter").css("margin","0em "+(parseInt(bubble_border)+5)+"px"); // TODO nastaveni marginu textu v bubline 
            $(".jqhbubble").css("position", "fixed");
            $(".jqhbubble").css("width", bubble_width);
            $(".jqhbubble").css("z-index","200");
            $(".jqhbubble .jqhbtop").css("width",($(".jqhbubble").width()-2*border)).css("margin-left",border); 
            $(".jqhbubble .jqhbbottom").css("width",($(".jqhbubble").width()-2*border-parseInt(bubble_tail_width))).css("margin-left",border); 
            jqhelperBubbleReplace(bubbleOffsetX,bubbleOffsetY);
            $("#jqhelper").load(function(){jqhelperBubbleReplace(bubbleOffsetX,bubbleOffsetY); return false;});
            if (bubble_close=="click")
              $(".jqhbubble").click(function(event){$("#main_menu li").fadeOut("slow");}); // zavreni bubliny i fadeout viz xml settings)
            if (bubble_close=="icon")
            {
              $("#jqhb_close_icon").css("cursor","pointer").css("float","right");
              $("#jqhb_close_icon").click(function(){
                $(".jqhbubble").remove();
              });
            }
            if (bubble_vanish=="interval")  
            {
              $(".jqhbubble").delay(parseInt(vanish_interval)).fadeOut("slow");
            }
          }  
        }
      }      
    },
    error: function()
    { // nacteni defaultnich parametru?
      alert("Chybějící nebo poškozený soubor jqhsettings.xml!");             
    }
  });   
};  
})( jQuery );

$(document).ready(function(){
   //himage = $('<img />').attr("src","images/jqhelper/dice-start.gif").attr("id","#jqhelper"); 
  var current_date = new Date;
  var exp_y = current_date.getFullYear ( );
  var exp_m = current_date.getMonth ( )+1;
  var exp_d = current_date.getDate ( );
  var expires = new Date ( exp_y, exp_m, exp_d );
  exp_date=expires.toGMTString();

});





