var MaxLengthLock = false;
var loginFlowBoxClickPriority = false;
var optionalPopupClickPriority = false;
var calendarClickPriority = false;
var detailStatsBoxPriority = false;
var messageWindowPriority = false;
var canPublish = false;
var addEventStarted = false;
/// <name>showConfirm</name>
/// <summary>nastavi confirm hlasku pri odchodu z neulozeneho denicku</summary>
var showConfirm = function() { 
    var r=confirm(hlaska);
    if (r==true) {
      window.location = '' + this.rel;
      return false;
    } else {
      return false;
    }
}
/// <name>searchConfirm</name>
/// <summary>nastavi confirm hlasku pri kliknuti na search v neulozenem denicku</summary>
var searchConfirm = function() { 
    var r=confirm(hlaska);
    if (r==true) {
      document.searchForm.submit();
      return false;
    } else {
      return false;
    }
}
/// <name>showConfirmInvite</name>
/// <summary>nastavi confirm hlasku pri kliknuti na invite v neulozenem denicku</summary>
var showConfirmInvite = function() { 
    var r=confirm(hlaska);
    if (r==true) {
      checkFBLogin();
      return false;
    } else {
      return false;
    }
}
/// <name>showConfirmStatusReport</name>
/// <summary>nastavi confirm hlasku pri odchodu z neulozeneho status report</summary>
var showConfirmStatusReport = function() {
    var r=confirm(hlaska)
    if (r==true) {
      if (form == 'tweet' || document.getElementById(button).id == 'submitTweet') {
        if (navigator.userAgent.search("Firefox") != -1) {
          facebookPublish();
        } else {
          if (window.event.srcElement.id == 'submitDay') {
            facebookPublish(); 
          } else {
            facebookTweetPublish();
          }
        }
      } else {
        facebookPublish();  
      }
      return false;
    } else {
      return false;
    }
}
/// <name>loading</name>
/// <summary>ukaze AJAX loading pri ukladani dne v denicku</summary>
function loading() {
	
	if (document.getElementById('submitDay1') != null) {
		var objSubmit = document.getElementById('submitDay1');
	}
	
	if (document.getElementById('submitDay') != null) {
		var objSubmit = document.getElementById('submitDay');
	}
	   
	if (document.getElementById('submitOldDay1') != null) {
		var objSubmit = document.getElementById('submitOldDay1');
	}
	
	if (document.getElementById('submitOldDay') != null) {
		var objSubmit = document.getElementById('submitOldDay');
	}
	
	if (objSubmit) {
		objSubmit.className = 'loading';
		objSubmit.rel = objSubmit.value;
		objSubmit.value = '';
	}
}
/// <name>unloading</name>
/// <summary>smaze AJAX loading pri ukladani dne v denicku</summary>
function unloading() {
	
	if (document.getElementById('submitDay') != null) {
		var objSubmit = document.getElementById('submitDay');
	}
	   
	if (document.getElementById('submitOldDay') != null) {
		var objSubmit = document.getElementById('submitOldDay');
	}
	
	if (objSubmit) {
		objSubmit.className = '';
		objSubmit.value = objSubmit.rel;
		objSubmit.rel = '';
	}
}


function MaxLengthCount(fieldObj,fieldMaxLength,mess) 
{ 
  if (!MaxLengthLock) 
  { 
    MaxLengthLock = true; 
    if (fieldObj.value.length > fieldMaxLength) 
    { 
      alert(mess + " (" + fieldMaxLength +")"); 
      fieldObj.value = fieldObj.value.substring(0,fieldMaxLength); 
    } 
    var percentage = parseInt(100 - (( fieldMaxLength - fieldObj.value.length) * 100)/fieldMaxLength); 
    document.getElementById(fieldObj.id + "_PBar").style.width = parseInt((parseInt(fieldObj.offsetWidth)*percentage)/100)+"px"; 
    MaxLengthLock = false; 
  } 
} 

function itemCheck(inputCheck,imgCheck) {

   img_check_value = document.getElementById(inputCheck);
   img_check_img = document.getElementById(imgCheck);

   if (img_check_value.value == 0) {
    img_check_value.value = 1;
    img_check_img.src = img_check_img.src.split('-')[0] + "-" + "1.gif";
   } else {
    img_check_value.value = 0;
    img_check_img.src = img_check_img.src.split('-')[0] + "-" + "0.png";
   }
   
   if (document.getElementById('submitDay') != null) {
	checkIfSaved('');
    document.getElementById('submitDay').id = 'submitDay1';
   }
   
   if (document.getElementById('submitOldDay') != null) {
	checkIfSaved('');
    document.getElementById('submitOldDay').id = 'submitOldDay1';
   }
}

/// <name>isFbSendCheck</name>
/// <summary>nastavuje JS udalost s funkci pro odesilaci tlacitko v denicku</summary>
function isFbSendCheck() {
  
  id = 'submitDay';
  if (document.getElementById('submitOldDay') != null) id = 'submitOldDay';
  if (document.getElementById('submitOldDay1') != null) id = 'submitOldDay1';
  if (document.getElementById('submitDay') != null) id = 'submitDay';  
  if (document.getElementById('submitDay1') != null) id = 'submitDay1';
  
  post = document.getElementById(id);
  post.onclick = '';
  
  var newInput = document.createElement('input');
  newInput.type = 'button'; // that should work even with IE
  newInput.name = post.name;
  newInput.id = post.id;
  newInput.value = newInput.defaultValue = post.value;
  post.parentNode.replaceChild(newInput, post);
  
  if (document.getElementById("send_summary_facebook").checked || 
      document.getElementById("send_weight_facebook").checked || 
      document.getElementById("send_facebook").checked) {
    
    addEvent( document.getElementById(id), 'click', function(){facebookPublish();});  
  
  } else {
    
    addEvent( document.getElementById(id), 'click', function(){xajax_setDayAjax(xajax.getFormValues('form')); loading();});
  }
}
/// <name>removeIfNotSaved</name>
/// <summary>rusi vyvolani hlasky o neulozenem denicku na vsech odkazech a tlacitku search</summary>
function removeIfNotSaved() {
	
	addEventStarted = false;
	
    for ( var i in document.links )
    {
      var href = document.links[i].rel;
      document.links[i].href = href;
      Spry.Utils.removeEventListener(document.links[i], "click", showConfirm, false);
      	
      if (document.links[i].className == "fbInvite") {
		Spry.Utils.removeEventListener(document.links[i], "click", showConfirmInvite, false);
		Spry.Utils.addEventListener(document.links[i], "click", checkFBLogin, false);
	  } else {
		Spry.Utils.removeEventListener(document.links[i], "click", showConfirm, false);
	  }
    } 
    // prida udalost na vyhledavaci tlacitko
    Spry.Utils.removeEventListener("submit_diary_search", "click", searchConfirm, false);
    // prida udalost na status report tlacitko
    Spry.Utils.removeEventListener("submitTweet", "click", showConfirmStatusReport, false);
    Spry.Utils.addEventListener("submitTweet", "click", facebookTweetPublish, false);
    //facebookTweetPublish
}

/// <name>initCalendar</name>
/// <summary>vytvori znovu kalenar v denicku</summary>
function initCalendar() {
    /*
    /* resim kalendar
    */
    // nacitam mesic a rok
    var monthYear = (document.getElementById("display").firstChild.firstChild.firstChild.firstChild.firstChild.innerHTML);
    // resim rok
    var pos = monthYear.indexOf(", ");
    var year = monthYear.substr(pos+2,4);
    // resim mesic
    var monthName = monthYear.substr(0, pos);
    var months=new Array(12);
    months[1]="January";
    months[2]="February";
    months[3]="March";
    months[4]="April";
    months[5]="May";
    months[6]="June";
    months[7]="July";
    months[8]="August";
    months[9]="September";
    months[10]="October";
    months[11]="November";
    months[12]="December";
    
    var month;
    for ( var i in months )
    {
      if (months[i] == monthName) {
        month = i;  
      }
    };
    
    if (month < 10) month = '0' + month;
    
    // toto je link ktery se provede pokud user bude chtit pokracovat, prestoze nema ulozeny denicek
    var pathLink = 'y' + year + 'm' + month + 'd'; 
    
    // hledam vybrany den kalendare a kterym dnem zacina
    var day;
    var fdow = 2;
    for ( var i in document.getElementsByTagName("td"))
    {
      var obj = document.getElementsByTagName("td")[i];
      var cls = '' + obj.className;
      // zjistuju od ktereho dne je zobrazeny kalendar
      if (cls.search('name') != -1 && fdow == 2) {
          var dayName = document.getElementsByTagName("td")[i].innerHTML;
          fdow = dayName == 'Sun' ? 0 : 1;
      }
      // vybrany den
      if (cls.search('selected') != -1) {
          day = document.getElementsByTagName("td")[i].innerHTML;
          if (day < 10) day = '0' + day;
          break;
      }
      
    }
    if (fdow == 2) fdow = 0;
    
    // vyprazdnuju kalendar
    document.getElementById("display").innerHTML = '';
    // rusim mooznost brouzdani pomoci kalendare
    if (noEvent) noEvent = false;
    
    // formatuju dnesni datum a aktualni datum v kalendari
    var d = new Date();
    var tYear = d.getFullYear();
    var tMonth = d.getMonth() + 1; 
    if (tMonth < 10) tMonth = '0' + tMonth;
    var tDay = d.getDate();
    if (tDay < 10) tDay = '0' + tDay;
    var today = '' + tYear + tMonth + tDay;
    var iDate = '' + year + month + day;
    
    // stavim novy kalendar kde nefunguji kliknuti
    showFlatCalendar(iDate, fdow, today);
    
    return pathLink + day;
  
}

/// <name>addCalendarEvent</name>
/// <summary>prida kotrolu o ulozeni denicku na kazde tlacitko v kalendari</summary>
function addCalendarEvent() {
  
    var pathLink = initCalendar();
    var today = parseFloat(pathLink.substr(9,2));
    if (today < 10) {
    	today = '0' + today;
    }
    pathLink = pathLink.substr(0,9);
    
    // prochazim calendar a pridavam udalost
    for ( var i in document.getElementsByTagName("td"))
    {
        
        addEvent( document.getElementsByTagName("td")[i], 'click', function()
                { 
                    var myDay = this.innerHTML;
                    
                    // vyjimky pro jine tlacitko nez skok na den 
                    if (myDay.search('unselectable="on"') != -1) {
                        
                        // jaky je rok
                        thisYear = parseFloat(pathLink.substr(1,4));
                        // jaky je mesic
                        thisMonth = parseFloat(pathLink.substr(6,2));
                        
                        if (myDay.search('">«</') != -1) {
                          
                          // rok zpet
                          window.location = '../y' + (thisYear - 1) + 'm' + thisMonth + 'd' + today;
                        } else if (myDay.search('">‹</') != -1) {
                          // mesic zpet
                          thisMonth = thisMonth - 1;
                          if (thisMonth < 1) {
                            thisMonth = 12;
                            thisYear = thisYear - 1;
                          } else if (thisMonth < 10) {
                            thisMonth = '0' + thisMonth;
                          }
                          window.location = '../y' + thisYear + 'm' + thisMonth + 'd' + today;
                        } else if (myDay.search('">›</') != -1) {
                          // mesic dopredu
                          thisMonth = thisMonth + 1;
                          if (thisMonth > 12) {
                            thisMonth = 1;
                            thisYear = thisYear + 1;
                          }
                          if (thisMonth < 10) {
                            thisMonth = '0' + thisMonth;
                          }
                          window.location = '../y' + thisYear + 'm' + thisMonth + 'd' + today;
                        } else if (myDay.search('">»</') != -1) {
                          // rok dopredu
                          window.location = '../y' + (thisYear + 1) + 'm' + thisMonth + 'd' + today;
                        } else {
                          // today
                          window.location = '../';
                        } 
                        return false;

                    } else {
    
                      if (myDay < 10) myDay = '0' + myDay;
                      window.location = '../' + pathLink + myDay;
                      return false;
                    }
                });
    }
    
    
   removeEvent( document.getElementById('submitTweet'), 'click', showConfirmStatusReport);
  
    
}

/// <name>checkIfSaved</name>
/// <summary>prida udalost na vsechny odchozi linky pri editaci denicku</summary>
/// <param name="form">jmeno formulare</param>
function checkIfSaved(form) {
	
	if (addEventStarted == false) {
	
		addEventStarted = true;
		
		hlaska = document.getElementById("unsaved").value;
		
		/*
		/*
		/* resim okazy mimo
		*/
		// prochazim linky a pridavam udalost
		for ( var i in document.links )
		{
	
			var href = document.links[i].href;
			document.links[i].rel = href;
			
			// pokud se jedna o IE7 tak zapisuju do href #
			if (navigator.appName == 'Microsoft Internet Explorer' && 
				navigator.appVersion.search('MSIE 7') != -1) {
				document.links[i].href = '#';
			} else {
				document.links[i].href = 'javascript:void(0);';
			}
			if (document.links[i].className == "fbInvite") {
				document.links[i].onclick = "";
				Spry.Utils.removeEventListener(document.links[i], "click", checkFBLogin, false);
				Spry.Utils.addEventListener(document.links[i], "click", showConfirmInvite, false);
			} else {
				//addEvent( document.links[i], 'click', showConfirm);
				Spry.Utils.addEventListener(document.links[i], "click", showConfirm, false);
			}
		}
		
		/*
		/*
		/* resim kalendar
		*/
		var pathLink = initCalendar();
		var today = parseFloat(pathLink.substr(9,2));
		if (today < 10) {
	    	today = '0' + today;
	    }
		pathLink = pathLink.substr(0,9);
		
		// prochazim calendar a pridavam udalost
		for ( var i in document.getElementsByTagName("td"))
		{       
		        // dnum nepridavam zadnou udalost
		        var weekDay = document.getElementsByTagName("td")[i].innerHTML;
		        if (weekDay == 'Mon' || weekDay == 'Tue' || weekDay == 'Wed' || weekDay == 'Thu' || 
                weekDay == 'Fri' || weekDay == 'Sat' || weekDay == 'Sun') {
                  
            } else {
		  
        				addEvent( document.getElementsByTagName("td")[i], 'click', function()
                { 
                  var r=confirm(hlaska)
                  if (r==true) {
                    
                    var myDay = this.innerHTML;
                    
                    // vyjimky pro jine tlacitko nez skok na den 
                    if (myDay.search('unselectable="on"') != -1) {

                    	// jaky je rok
                        thisYear = parseFloat(pathLink.substr(1,4));
                        // jaky je mesic
                        thisMonth = parseFloat(pathLink.substr(6,2));
                        
                        if (myDay.search('title="Previous year"') != -1) {
                          // rok zpet
                          window.location = '../y' + (thisYear - 1) + 'm' + thisMonth + 'd' + today;
                        } else if (myDay.search('title="Previous month"') != -1) {
                          // mesic zpet
                          thisMonth = thisMonth - 1;
                          if (thisMonth < 1) {
                            thisMonth = 12;
                            thisYear = thisYear - 1;
                          } else if (thisMonth < 10) {
                            thisMonth = '0' + thisMonth;
                          }
                          window.location = '../y' + thisYear + 'm' + thisMonth + 'd' + today;
                        } else if (myDay.search('title="Next month"') != -1) {
                          // mesic dopredu
                          thisMonth = thisMonth + 1;
                          if (thisMonth > 12) {
                            thisMonth = 1;
                            thisYear = thisYear + 1;
                          }
                          if (thisMonth < 10) {
                            thisMonth = '0' + thisMonth;
                          }
                          window.location = '../y' + thisYear + 'm' + thisMonth + 'd' + today;
                        } else if (myDay.search('title="Next year"') != -1) {
                          // rok dopredu
                          window.location = '../y' + (thisYear + 1) + 'm' + thisMonth + 'd' + today;
                        } else {
                          // today
                          window.location = '../';
                        } 
                        return false;

                    } else {
    
                      if (myDay < 10) myDay = '0' + myDay;
                      window.location = '../' + pathLink + myDay;
                      return false;
                    }
                    
                  } else {
                    return false;
                  }
                });
            }
		}
	}
		/*
		/*
		/* resim odesilaci button
		*/
		var button = 'submitDay';
		if (form != 'tweet') {
				
				// pokud voli, tak povoluju klik na tlacitko save changes
				button = 'submitTweet';
        
				if (document.getElementById('submitDay')) {
					
					post = document.getElementById('submitDay');
					post.onclick = '';
					
					var newInput = document.createElement('input');
					newInput.type = 'button'; // that should work even with IE
					newInput.name = post.name;
					newInput.id = post.id;
					newInput.value = newInput.defaultValue = post.value;
					post.parentNode.replaceChild(newInput, post);
            
            // pokud je zaskrtnuto odesilani na FB tak neresim ukladani ajaxem
					if (document.getElementById("send_summary_facebook").checked || 
		                document.getElementById("send_weight_facebook").checked || 
		                document.getElementById("send_facebook").checked) {
              
		              addEvent( document.getElementById('submitDay'), 'click', function(){facebookPublish();});  
		            
		            } else {
		              
		              addEvent( document.getElementById('submitDay'), 'click', function(){xajax_setDayAjax(xajax.getFormValues('form')); loading();});
					  
					 // addEvent( document.getElementById('submitDay'), 'click', function(){document.form.submit();});
					  
					  
		            }
					
				}
				
				
		} 
			if (document.getElementById(button)) {
				// odstranuju udalost s post tlacitka status reports
				post = document.getElementById(button);
				post.onclick = '';
				
				var newInput = document.createElement('input');
				newInput.type = 'button'; // that should work even with IE
				newInput.name = post.name;
				newInput.id = post.id;
				newInput.value = newInput.defaultValue = post.value;
				post.parentNode.replaceChild(newInput, post);
				
				
				// pridavam udalost na post tlacitko pro status reports
				// addEvent( document.getElementById(button), 'click', showConfirmStatusReport);
				Spry.Utils.addEventListener(document.getElementById(button), "click", showConfirmStatusReport, false);
			}
				
			Spry.Utils.addEventListener("submit_diary_search", "click", searchConfirm, false);
		 
	
}

function paramCheck(oldvalue, newvalue, inputCheck, paramCheck1, paramCheck2, paramCheck3, paramCheck4, paramCheck5) {

   param_check_value = document.getElementById(inputCheck);
   param_check_one = document.getElementById(paramCheck1);
   param_check_two = document.getElementById(paramCheck2);
   param_check_three = document.getElementById(paramCheck3);
   param_check_four = document.getElementById(paramCheck4);
   param_check_five = document.getElementById(paramCheck5);
   
   if (oldvalue == 1) {
     param_check_one.src = param_check_one.src.split('-')[0] + "-" + "0.png";
   }
   if (oldvalue == 2) {
     param_check_two.src = param_check_two.src.split('-')[0] + "-" + "0.png";
   }
   if (oldvalue == 3) {
     param_check_three.src = param_check_three.src.split('-')[0] + "-" + "0.png";
   }
   if (oldvalue == 4) {
     param_check_four.src = param_check_four.src.split('-')[0] + "-" + "0.png";
   }
   if (oldvalue == 5) {
     param_check_five.src = param_check_five.src.split('-')[0] + "-" + "0.png";
   }
   
   if (newvalue == 1) {
     param_check_one.src = param_check_one.src.split('-')[0] + "-" + "1.png";
   }
   if (newvalue == 2) {
     param_check_two.src = param_check_two.src.split('-')[0] + "-" + "1.png";
   }
   if (newvalue == 3) {
     param_check_three.src = param_check_three.src.split('-')[0] + "-" + "1.png";
   }
   if (newvalue == 4) {
     param_check_four.src = param_check_four.src.split('-')[0] + "-" + "1.png";
   }
   if (newvalue == 5) {
     param_check_five.src = param_check_five.src.split('-')[0] + "-" + "1.png";
   }
   
   param_check_value.value = newvalue;
   
   if (oldvalue != newvalue) {
       if (document.getElementById('submitDay') != null) {
		checkIfSaved('');
        document.getElementById('submitDay').id = 'submitDay1';
       }
       
       if (document.getElementById('submitOldDay') != null) {
		checkIfSaved('');
        document.getElementById('submitOldDay').id = 'submitOldDay1';
       }
   }
}

function textAreaCounter(field,cntfield,maxlimit) {
if (field != '') {
  if (field.value.length > maxlimit) {
    field.value = field.value.substring(0, maxlimit);
  } else {
   // if (field.value != document.getElementById("textSummary").value) {
      cntfield.value = maxlimit - field.value.length;
    /*} else {
      cntfield.value = maxlimit;
    }*/
  }
} else {
  cntfield.value = maxlimit;  
} 
   
   if(cntfield.id != 'counter') { //tyka se tweet textu
     if (document.getElementById('submitDay') != null) {
	  checkIfSaved('');
      document.getElementById('submitDay').id = 'submitDay1';
     } 
     if (document.getElementById('submitOldDay') != null) {
   	  checkIfSaved('');
         document.getElementById('submitOldDay').id = 'submitOldDay1';
     } 
   }
   if(field.name == 'tweet') {
	   if (document.getElementById('submitTweet') != null) {
	   	if (document.getElementById('submitTweet').className == '') {
	   		if (document.getElementById('submitDay') != null) {
				checkIfSaved('tweet');
				
       			document.getElementById('submitTweet').className = 'tweetSend';
			}
		}
     }  
   }
}

function weightChange(form) {
	
  if (document.getElementById('submitDay') != null) {
	checkIfSaved('');
    document.getElementById('submitDay').id = 'submitDay1';
  }
   
  if (document.getElementById('submitOldDay') != null) {
	checkIfSaved('');
    document.getElementById('submitOldDay').id = 'submitOldDay1';
  }
}

function removeFriend(whose,who) {
	
	var hlaska = document.getElementById("hlaska").innerHTML;
	hlaska = hlaska.replace("__USER_NAME__", who);
	
	var r=confirm(hlaska)
	if (r==true)
	  {
	  window.location = "/removeFriend.php?whose=" + whose + "&who=" + who;
	  return false;
	  }
	else
	  {
	  return false;
	  }
	
  
}

function addFriendToTop(whose,who) {

  window.location = "/addFriendToTop.php?whose=" + whose + "&who=" + who;
}

function removeFriendFromTop(whose,who) {

  window.location = "/removeFriendFromTop.php?whose=" + whose + "&who=" + who;
}

function setUnit(unitType) {

	  window.location = "/set-unit.php?unitType=" + unitType;
	}

function topFriendsFullAlert(hlaska) {
  alert(hlaska);
}

function getNowDateStatement(alt) {
  
  var date=new Date();
  var daystring = '';
  var month=date.getMonth();
  month++;
  
	if(month==1) {month = "January";}
	else if(month==2) {month = "February";}
	else if(month==3) {month = "March";}
	else if(month==4) {month = "April";}
	else if(month==5) {month = "May";}
	else if(month==6) {month = "June";}
	else if(month==7) {month = "July";}
	else if(month==8) {month = "August";}
	else if(month==9) {month = "September";}
	else if(month==10) {month = "October";}
	else if(month==11) {month = "November";}
	else if(month==12) {month = "December";}
  
  if(date.getDay()==0) {daystring = "Sunday";}
	else if(date.getDay()==1) {daystring = "Monday";}
	else if(date.getDay()==2) {daystring = "Tuesday";}
	else if(date.getDay()==3) {daystring = "Wednesday";}
	else if(date.getDay()==4) {daystring = "Thursday";}
	else if(date.getDay()==5) {daystring = "Friday";}
	else if(date.getDay()==6) {daystring = "Saturday";}

  return "<div id=\"dateStatement\"><span id=\"month\">" + month + "</span><span id=\"day\"><h1>" + date.getDate() + "</h1></span><span id=\"year\">" + date.getFullYear() + "</span><span id=\"dayString\"><strong>" + daystring + "</strong></span><div id=\"to_stats\"><a href=\"http://www.daileez.com/yesterday/\" title=\""+ alt +"\"><span class=\"dNone\">&laquo;</span></a></div></div>";
}

function loginBoxVisibilityHidden() {
  document.getElementById("loginFlowBox").style.visibility="hidden";
}

function loginBoxVisibility() {
  if (document.getElementById("loginFlowBox").style.visibility == "hidden") {
    document.getElementById("loginFlowBox").style.visibility="visible";
    document.getElementById('loginForm').getElementsByTagName('input')[1].focus();
  } else {
    document.getElementById("loginFlowBox").style.visibility="hidden";
  }
  loginFlowBoxClickPriority = true;
}

function disallowHiddenLoginBox() {
  loginFlowBoxClickPriority = true;
}

function disallowHiddenOptionalPopup() {
  optionalPopupClickPriority = true;
}

/**
 * global HIDDEN
 * 
 */  
function visibilityGlobalHidden() {
  if (!messageWindowPriority) {
    var messageWindow = document.getElementById("messageWindow"); 
    if (messageWindow != null) {
      messageWindow.style.visibility = "hidden"
    }
  }
  
  messageWindowPriority = false;
  
  if (!calendarClickPriority) {
    calendar = document.getElementById("display");
    if (calendar != null) {
      calendar.style.visibility = "hidden";
    }
  }
  
  calendarClickPriority = false;
  
  if (!loginFlowBoxClickPriority) {
    if (document.getElementById("loginFlowBox") != null) {
      if (document.getElementById("loginFlowBox").style.visibility == "visible") {
        document.getElementById("loginFlowBox").style.visibility="hidden";
      }
    }
  }
  
  loginFlowBoxClickPriority = false;
}

function visibilityGlobalHiddenPrefix() {
  
  if (!optionalPopupClickPriority) {
    var optionalSetupWindow = document.getElementById("optionalSetupWindow");
    if (optionalSetupWindow != null) {
      //optionalSetupWindow.style.visibility = "hidden";
    }
    
    var optionalSetupForm = document.getElementById("optionalSetupForm");
    if (optionalSetupForm != null) {
      //optionalSetupForm.style.visibility = "hidden";
    }
  }

  optionalPopupClickPriority = false;
  
  if (!detailStatsBoxPriority) {
    detailstatsbox = document.getElementById("detailStatsBox");
    if (detailstatsbox != null) {
      detailstatsbox.style.visibility = "hidden";
    }
  }

  detailStatsBoxPriority = false;
}

function preloadDetailStatsImages() {

  preloadI1 = new Image(); preloadI1.src = "http://daileez.com/_prefix/html/img/items/itemAVeryBad-0-30.png";
  preloadI2 = new Image(); preloadI2.src = "http://daileez.com/_prefix/html/img/items/itemABad-0-30.png";
  preloadI3 = new Image(); preloadI3.src = "http://daileez.com/_prefix/html/img/items/itemANormal-0-30.png";
  preloadI4 = new Image(); preloadI4.src = "http://daileez.com/_prefix/html/img/items/itemAGood-0-30.png";
  preloadI5 = new Image(); preloadI5.src = "http://daileez.com/_prefix/html/img/items/itemAVeryGood-0-30.png";
  
  preloadI6 = new Image(); preloadI6.src = "http://daileez.com/_prefix/html/img/items/itemBVeryBad-0-30.png";
  preloadI7 = new Image(); preloadI7.src = "http://daileez.com/_prefix/html/img/items/itemBBad-0-30.png";
  preloadI8 = new Image(); preloadI8.src = "http://daileez.com/_prefix/html/img/items/itemBNormal-0-30.png";
  preloadI9 = new Image(); preloadI9.src = "http://daileez.com/_prefix/html/img/items/itemBGood-0-30.png";
  preloadI10 = new Image(); preloadI10.src = "http://daileez.com/_prefix/html/img/items/itemBVeryGood-0-30.png";
  
  preloadI11 = new Image(); preloadI11.src = "http://daileez.com/_prefix/html/img/items/itemCVeryBad-0-30.png";
  preloadI12 = new Image(); preloadI12.src = "http://daileez.com/_prefix/html/img/items/itemCBad-0-30.png";
  preloadI13 = new Image(); preloadI13.src = "http://daileez.com/_prefix/html/img/items/itemCNormal-0-30.png";
  preloadI14 = new Image(); preloadI14.src = "http://daileez.com/_prefix/html/img/items/itemCGood-0-30.png";
  preloadI15 = new Image(); preloadI15.src = "http://daileez.com/_prefix/html/img/items/itemCVeryGood-0-30.png";
  
  preloadI16 = new Image(); preloadI16.src = "http://daileez.com/_prefix/html/img/items/itemDVeryBad-0-30.png";
  preloadI17 = new Image(); preloadI17.src = "http://daileez.com/_prefix/html/img/items/itemDBad-0-30.png";
  preloadI18 = new Image(); preloadI18.src = "http://daileez.com/_prefix/html/img/items/itemDNormal-0-30.png";
  preloadI19 = new Image(); preloadI19.src = "http://daileez.com/_prefix/html/img/items/itemDGood-0-30.png";
  preloadI20 = new Image(); preloadI20.src = "http://daileez.com/_prefix/html/img/items/itemDVeryGood-0-30.png";
  
  preloadI21 = new Image(); preloadI21.src = "http://daileez.com/_prefix/html/img/layout/iphone/bg_boxParamR.png";
  preloadI22 = new Image(); preloadI22.src = "http://daileez.com/_prefix/html/img/layout/iphone/bg_boxParam.png";
  
}

function generateDetailStatsBox(month,type,value1,value2,value3,value4,value5,mob) {
  
  if(month == 1) { lft = 334;
  } else if(month == 2) { lft = 331;
  } else if(month == 3) { lft = 328;
  } else if(month == 4) { lft = 186;
  } else if(month == 5) { lft = 183;
  } else if(month == 6) { lft = 179;
  } else if(month == 7) { lft = 4;
  } else if(month == 8) { lft = 1;
  } else if(month == 9) { lft = -3;
  } else if(month == 10) { lft = -143;
  } else if(month == 11) { lft = -146;
  } else { lft = -150;
  }
  
  urlImg = "http://daileez.com/_prefix/html/img";
  
  img1 = "url(" + urlImg + "/layout/iphone/bg_boxParamR.png)";
  img2 = "url(" + urlImg + "/layout/iphone/bg_boxParam.png)";
  
  obj = document.getElementById("detailStatsBox");
  
  if(mob=="y") {
	  obj.style.left = lft + "px";
	  (month > 3 && month < 7) || (month > 9) ? obj.style.backgroundImage=img1 : obj.style.backgroundImage=img2;
  }
  
  if(type=="weather") {
    document.getElementById("detailIcon1").src = urlImg + "/items/itemAVeryBad-0-30.png";
    document.getElementById("detailIcon2").src = urlImg + "/items/itemABad-0-30.png";
    document.getElementById("detailIcon3").src = urlImg + "/items/itemANormal-0-30.png";
    document.getElementById("detailIcon4").src = urlImg + "/items/itemAGood-0-30.png";
    document.getElementById("detailIcon5").src = urlImg + "/items/itemAVeryGood-0-30.png";
	if(mob=="y") {
		(month < 7) ? obj.style.marginTop = "155px" : obj.style.marginTop = "234px";
	} else {
		obj.style.marginTop = "70px";
  	}
  } else if (type=="happiness") {
    document.getElementById("detailIcon1").src = urlImg + "/items/itemBVeryBad-0-30.png";
    document.getElementById("detailIcon2").src = urlImg + "/items/itemBBad-0-30.png";
    document.getElementById("detailIcon3").src = urlImg + "/items/itemBNormal-0-30.png";
    document.getElementById("detailIcon4").src = urlImg + "/items/itemBGood-0-30.png";
    document.getElementById("detailIcon5").src = urlImg + "/items/itemBVeryGood-0-30.png";
	if(mob=="y") {
		(month < 7) ? obj.style.marginTop = "358px" : obj.style.marginTop = "437px";
	} else {
		obj.style.marginTop = "160px";
	}
  } else if (type=="sleep") {
    document.getElementById("detailIcon1").src = urlImg + "/items/itemCVeryBad-0-30.png";
    document.getElementById("detailIcon2").src = urlImg + "/items/itemCBad-0-30.png";
    document.getElementById("detailIcon3").src = urlImg + "/items/itemCNormal-0-30.png";
    document.getElementById("detailIcon4").src = urlImg + "/items/itemCGood-0-30.png";
    document.getElementById("detailIcon5").src = urlImg + "/items/itemCVeryGood-0-30.png";
	if(mob=="y") {
		(month < 7) ? obj.style.marginTop = "561px" : obj.style.marginTop = "640px";
	} else {
		obj.style.marginTop = "250px";
	}
  } else {
    document.getElementById("detailIcon1").src = urlImg + "/items/itemDVeryBad-0-30.png";
    document.getElementById("detailIcon2").src = urlImg + "/items/itemDBad-0-30.png";
    document.getElementById("detailIcon3").src = urlImg + "/items/itemDNormal-0-30.png";
    document.getElementById("detailIcon4").src = urlImg + "/items/itemDGood-0-30.png";
    document.getElementById("detailIcon5").src = urlImg + "/items/itemDVeryGood-0-30.png";
	if(mob=="y") {
		(month < 7) ? obj.style.marginTop = "764px" : obj.style.marginTop = "843px";
	} else {
		obj.style.marginTop = "340px";
	}
  }
  
  document.getElementById("detailValue1").innerHTML = value1;
  document.getElementById("detailValue2").innerHTML = value2;
  document.getElementById("detailValue3").innerHTML = value3;
  document.getElementById("detailValue4").innerHTML = value4;
  document.getElementById("detailValue5").innerHTML = value5;
  
  var max = 0;
  if (value1 > max) max = value1;
  if (value2 > max) max = value2;
  if (value3 > max) max = value3;
  if (value4 > max) max = value4;
  if (value5 > max) max = value5;
  
  if (max != 0) {
    var q = 60 / max;
  } else {
    var q = 60;
  }
  
  var barrelValue1 = Math.floor(q * value1);
  var barrelValue2 = Math.floor(q * value2);
  var barrelValue3 = Math.floor(q * value3);
  var barrelValue4 = Math.floor(q * value4);
  var barrelValue5 = Math.floor(q * value5);
  
  if (navigator.appName == "Microsoft Internet Explorer") {
    var marginTopExplorer = 8;
  } else {
    var marginTopExplorer = 0;
  }
  
  document.getElementById("barrel1").style.marginTop = (60 - barrelValue1 + marginTopExplorer) + "px";
  document.getElementById("barrel2").style.marginTop = (60 - barrelValue2 + marginTopExplorer) + "px";
  document.getElementById("barrel3").style.marginTop = (60 - barrelValue3 + marginTopExplorer) + "px";
  document.getElementById("barrel4").style.marginTop = (60 - barrelValue4 + marginTopExplorer) + "px";
  document.getElementById("barrel5").style.marginTop = (60 - barrelValue5 + marginTopExplorer) + "px";
  
  if (navigator.appName == "Microsoft Internet Explorer") {
    document.getElementById("barrel1").style.height = (barrelValue1 + 41) + "px";
    document.getElementById("barrel2").style.height = (barrelValue2 + 41) + "px";
    document.getElementById("barrel3").style.height = (barrelValue3 + 41) + "px";
    document.getElementById("barrel4").style.height = (barrelValue4 + 41) + "px";
    document.getElementById("barrel5").style.height = (barrelValue5 + 41) + "px";
  }

  document.getElementById("barrel1image").style.height = barrelValue1 + "px";
  document.getElementById("barrel2image").style.height = barrelValue2 + "px";
  document.getElementById("barrel3image").style.height = barrelValue3 + "px";
  document.getElementById("barrel4image").style.height = barrelValue4 + "px";
  document.getElementById("barrel5image").style.height = barrelValue5 + "px";
  
  obj.style.marginLeft = (90 + month * 55)+"px";
  obj.style.visibility = "visible";

  detailStatsBoxPriority = true;
}

function changePasswordHidden() {
  box = document.getElementById("box_change");
  box.style.height = "1px";
  box.style.visibility = "hidden";
}

function changePassword() {
  box = document.getElementById("box_change");
  box_button = document.getElementById("box_change_button");
  box_button.disabled = "disabled";
  box.style.height = "125px";
  box.style.visibility = "visible";
}

function hidePopupCalendar() {
  calendar = document.getElementById("display");
  calendar.style.visibility = "hidden";
}

function showPopupCalendar() {
  calendar = document.getElementById("display");
  calendar.style.visibility = "visible";
  calendarClickPriority = true;
}

function disallowHiddenCalendar() {
  calendarClickPriority = true;
}

function googleAnalytics() {
  
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  
  if (typeof(_gat) == 'object') {
	  	var pageTracker = _gat._getTracker("UA-11279790-1");
	    pageTracker._setDomainName(".daileez.com");
		pageTracker._setAllowLinker(true);
		pageTracker._setAllowHash(false);
	    pageTracker._trackPageview();
  }
  
  // jeste jedno reseni
  /*var hndl = window.setTimeout(”StartTracking()”, 100);
  function StartTracking(){
    if (typeof(_gat) == ‘object’)
    {
      window.clearTimeout(hndl);
      var pageTracker =_gat._getTracker(” — YOUR GA CODE HERE –”);
      pageTracker._initData();
      pageTracker._trackPageview();
    } else {
      hndl = window.setTimeout(”StartTracking()”, 1000);
    }
  }*/
  
  // puvodni reseni
  /*try {
    var pageTracker = _gat._getTracker("UA-11279790-1");
    pageTracker._setDomainName(".daileez.com");
	pageTracker._setAllowLinker(true);
	pageTracker._setAllowHash(false);
    pageTracker._trackPageview();
  } catch(err) {alert(err);}*/
}

function deactivateDeleteChanger($selection) {
  
  if ($selection == 0) {
    document.getElementById("box_change").style.fontWeight = "bold";
    document.getElementById("box_change1").style.fontWeight = "normal";
  } else {
    document.getElementById("box_change").style.fontWeight = "normal";
    document.getElementById("box_change1").style.fontWeight = "bold";
  }
}

function facebookPublish() {  

	
  if (!document.getElementById("send_summary_facebook").checked && 
	  !document.getElementById("send_weight_facebook").checked && 
	  !document.getElementById("send_facebook").checked) {

    facebookCallback();
  
  } else {
	  
	//facebookCallback();  
	  
	  
	FB.login(function(response) {
	  if (response.authResponse) {
		
		facebookCallback();
	  
	  } else {
		
		facebookCallback();
	  
	  }
	}, {scope:'publish_stream'});

	

    
  }
}


function facebookTweetPublish() {  

	if (document.tweetForm.tweet.value == document.getElementById("textStatus").value) {
		return false;
	}
	
  if (!document.getElementById("send_tweet_facebook").checked) {
  
    facebookTweetCallback();
  
  } else {
  
   
	FB.login(function(response) {
	  if (response.authResponse) {
		
		facebookPublishStatusReport();
	  
	  } else {
		
		facebookTweetCallback();
	  
	  }
	});
	
	
	
    
  }
}


function facebookPublishStatusReport() {


FB.ui(
	   {
		 method: 'stream.publish',
		 message: document.getElementById("tweetTextArea").value, // This field will be ignored on July 12, 2011
		 attachment: {
		   name: document.getElementById("stName").value,
		   href: document.getElementById("stHref").value,
		   caption: document.getElementById("stCaption").value,
		   description: document.getElementById("stDesc").value 
		 },
		 action_links: [
		   {'text':'www.daileez.com', 'href':' http://www.daileez.com'}
		 ],
		 user_message_prompt: document.getElementById("tweetTextArea").value
	   },
	   function(response) {
		 if (response && response.post_id) {
		   facebookTweetCallback();
		 } else {
		   facebookTweetCallback();
		 }
	   }
	 );

}

function facebookCallback() {
  
  document.form.submit();
}

function facebookTweetCallback() {
  
  document.tweetForm.submit();
}

function newAccountSubmit() {
  
  if(
    document.getElementById("user_name").value == '' ||
    document.getElementById("user_email").value == '' ||
    document.getElementById("user_password").value == '' ||
    !document.getElementById("termsAndPrivacy").checked)
  {    
    document.getElementById("messageWindow").style.visibility = "visible";
    messageWindowPriority = true;
  
  } else {
    document.form.submit();
  }
}

function FBnewAccountSubmit() {
  
  if(
    document.getElementById("user_name").value == '' ||
    !document.getElementById("termsAndPrivacy").checked)
  {    
    document.getElementById("messageWindow").style.visibility = "visible";
    messageWindowPriority = true;
  
  } else {
    document.form.submit();
  }
}

function messageWindowHidden() {
  if (document.getElementById("messageWindow") != null) {
    document.getElementById("messageWindow").style.visibility = "hidden";
  }
}

function refreshCaptcha() {

    var http_request = false;

    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (eror) {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    http_request.onreadystatechange = function() { zpracujAjax(http_request); };
    http_request.open('GET', 'http://www.daileez.com/_www/generateCaptcha.php', true);
    http_request.send(null);
}

function zpracujAjax(http_request) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      document.getElementById("captchaImg").src = http_request.responseText;
    }
  }
}

function clearIfDefault(textArea) {
  if (textArea.value == document.getElementById("textSummary").value || textArea.value == document.getElementById("textStatus").value) {
    textArea.value = '';
  }
}

function printMail(start, middle, end) {
	majl = start + "@" + middle + "." + end;
	html = '<a href="mailto:' + majl + '">' + majl + '</a>';
	return document.write(html);
}
/*
function loginUser() {
	window.location = 'http://www.daileez.com/?act=fblogin';	
}*/

function logoutUser(header, text) {
	FB.Connect.logout(function() { window.location = 'http://www.daileez.com/?act=fblogout'; });
	
	/*var odkaz = '' + window.location;
	var ldialog = {
	method: 'fbml.dialog',
	fbml: '<fb:header icon="false" decoration="no_padding"><div style="background-color: #6d84b4; color: #fff; padding: 10px; border-bottom: 1px solid #3b5998;">'+header+'</div></fb:header><div style="padding: 20px 10px;">'+text+'</div><div style="background-color: #f7f7f7; padding: 10px; border-top: 1px solid #cccccc;"><a style="float: right; display: block; width: 58px; height: 20px; background: url('+ odkaz +'/html/img/layout/FBclose.png) no-repeat;" href="' + getWWWaddress() + '/?act=fblogout" target="_parent"><span style="display: none;">close</span></a><div style="clear:both; height: 0; line-height: 0; font-size: 0;"></div></div>',
	width: 450,
	height: 80
	  };
	FB.UIServer.Methods["fbml.dialog"].size = {width:450, height:80};
	FB.ui(ldialog, FBlogout());*/
}

function getWWWaddress() {
	
	var odkaz = '' + window.location.hostname;
	var prtcl = '' + window.location.protocol;
	part = odkaz.split(".");
	return (prtcl + '//www.' + part[1] + '.' + part[2].replace("/", ""));
	
}

function FBlogout() {
	
	/*FB.Connect.logout(function() {
		 //window.location = 'http://www.daileez.com/?act=fblogout'; 
		 setTimeout("window.location = '" + getWWWaddress() + "/?act=fblogout'", 4000);
	});*/
	
	FB.logout(function(response) {
		setTimeout("window.location = '" + getWWWaddress() + "/?act=fblogout'", 4000);
	})
}


function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

function colapseAll(standardHeight) {
	
	divs=document.getElementsByTagName('div');
	
	for ( var i in divs )
	{
		
		if (divs[i].className == "textCommentBox" || divs[i].className == "status") {
			
			var boxObj = divs[i];
			
			if (boxObj.rel != 'expanded') {
				
				var href = divs[i].className == "textCommentBox" ? "sh" : "h";
				var box = divs[i].className == "textCommentBox" ? "sum" : "st";
				var tol = divs[i].className == "textCommentBox" ? 10 : 10;
				
				var actualHeight = boxObj.offsetHeight;
				
				var id = divs[i].id;
				id = id.replace(box, "");
				
				if ((actualHeight - tol) > standardHeight) {
					var hrefObj = document.getElementById(href + id);
					hrefObj.style.display = "block";
					
					boxObj.rel = actualHeight;
					
					for ( var j in boxObj.childNodes ) {
						if (boxObj.childNodes[j].nodeName == 'UL') {
							boxObj.childNodes[j].style.height = (standardHeight-4)+'px';
						}
					}
					
					
				} else if (actualHeight < standardHeight) {
					
					/*for ( var j in boxObj.childNodes ) {
						if (boxObj.childNodes[j].nodeName == 'P') {
							boxObj.childNodes[j].style.background = "none";
						}
					}*/
					
				} 
				boxObj.style.height = standardHeight+'px';
				boxObj.style.overflow = "hidden";
			}
		}
	}
}

function seeMoreExpand(href, box, id, standardHeight) {
	
	var boxObj = document.getElementById(box + id);
	boxObj.style.height = boxObj.rel+'px';
	boxObj.style.marginBottom = "0px";
	
	for ( var j in boxObj.childNodes ) {
		if (boxObj.childNodes[j].nodeName == 'UL') {
			boxObj.childNodes[j].style.height = (boxObj.rel-4)+'px';
		}
	}
	
	boxObj.rel = 'expanded';
	var hrefObj = document.getElementById(href + id);
	hrefObj.style.display = "none";
}

function calculate_time_zone() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}

	return convert(std_time_offset);
}

function convert(value) {
	var hours = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var mins = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var secs = parseInt(value);
	var display_hours = hours;
	// handle GMT case (00:00)
	if (hours == 0) {
		display_hours = "00";
	} else if (hours > 0) {
		// add a plus sign and perhaps an extra 0
		display_hours = (hours < 10) ? "+0"+hours : "+"+hours;
	} else {
		// add an extra 0 if needed 
		display_hours = (hours > -10) ? "-0"+Math.abs(hours) : hours;
	}
	
	mins = (mins < 10) ? "0"+mins : mins;
	return display_hours+mins;
}


