$().ready(function() {
	scrollHeaderPhotos()
	window.setInterval("scrollHeaderPhotos()", 10000);
	$(window).scroll(function(){scrollWindow() });

	//add facebook and google+  to first <h1> in main
	var pathname = window.location.href;
	$("#main h1").first().append('<span style="height:35px; float:right; width:130px; overflow:hidden"><iframe src="//www.facebook.com/plugins/like.php?href='+pathname+'&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:350px; height:35px" allowTransparency="true"></iframe></span><span id="socialDiv" style="float:right; width:60px"><g:plusone size="medium"></g:plusone></span>');

});


//google+ stuff
window.___gcfg = {lang: 'de'};
(function() {
  var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  po.src = 'https://apis.google.com/js/plusone.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

function scrollWindow(){

say(scrollWindow.locked)

scrollVal=$(window).scrollTop();
say(scrollVal)
if (scrollVal>285){
	if(!scrollWindow.locked){
		$("#main_nav").css({position: 'fixed', top: '0px', 'margin-left': '50%', left: '-394px'})
		$("#content-wrap").css({top:'20px'})
		$("#sidebar").css({position: 'fixed', top: '60px', 'margin-left': '50%', left: '-405px'})
		scrollWindow.locked = true
	}
}
else if ((scrollVal<280)&&(scrollWindow.locked)){
	$("#main_nav").css({position:'relative',top:'-20px','margin-left': '0px',left:'16px'})
	$("#content-wrap").css({top:'-20px'})
	$("#sidebar").css({position:'absolute',top:'20px','margin-left': '0px',left:'0px'})
	scrollWindow.locked = false
}
}

function scrollHeaderPhotos(){
$("#header-photo-photos").animate({left: '0px'}, 3000, function() {
	var img=$("#header-photo-photos").children().first()
	var imgWidth=parseInt(img.css("width"))
	img.detach()
	$("#header-photoFirst").empty().append(img.clone())
	$("#header-photo-photos").css("left", imgWidth)
	img.appendTo($("#header-photo-photos"))
	// Animation complete.
});
//~ var img=$("#header-photo-photos").children().first()
//~ var imgWidth=parseInt(img.css("width"))
//~ $("#header-photo-photos").animate({left: '-='+imgWidth}, 3000, function() {
	//~ img.detach()
	//~ $("#header-photo-photos").css("left", "0px")
	//~ img.appendTo($("#header-photo-photos"))
	//~ // Animation complete.
//~ });

}

function showMenu(element){
	//~ $(element).css("background","url(fileadmin/templates/nav-hover.jpg)")
	$(element).addClass("activeMenu")
	$(element).children(".submenu").stop(true,true).slideDown(100)
	$(element).children(".submenu").css("z-index", "2")
}
function hideMenu(element){
    $(element).removeClass("activeMenu")
	$(element).children(".submenu").delay(100).slideUp()
	$(element).children(".submenu").css("z-index", "1")
 }

function say(txt){
	//~ document.getElementById("out").innerHTML=txt
}





/**
 * Converts an xs:date or xs:dateTime formatted string into the local timezone
 * and outputs a human-readable form of this date or date/time.
 *
 * @param {string} gCalTime is the xs:date or xs:dateTime formatted string
 * @return {string} is the human-readable date or date/time string
 */
function formatGCalTime(gCalTime) {
  // text for regex matches
  var remtxt = gCalTime;

  function consume(retxt) {
    var match = remtxt.match(new RegExp('^' + retxt));
    if (match) {
      remtxt = remtxt.substring(match[0].length);
      return match[0];
    }
    return '';
  }

  var year = consume('\\d{4}');
  consume('-?');
  var month = consume('\\d{2}');
  consume('-?');
  var dateMonth = consume('\\d{2}');
  dateString = new Array();

  dateString[0] =  dateMonth + "." + month + "." // + year
  var timeOrNot = consume('T');
  // if a DATE-TIME was matched in the regex
  if (timeOrNot == 'T') {
    var hours = consume('\\d{2}');
    consume(':?');
    var mins = consume('\\d{2}');
	dateString[1] = hours+":"+mins
  }
  return dateString;
}

/**
 * Creates an unordered list of events in a human-readable form
 *
 * @param {json} root is the root JSON-formatted content from GData
 * @param {string} divId is the div in which the events are added
 */
function listEvents(root, divId) {
  var feed = root.feed;
  var events = document.getElementById(divId);
  var eventsHTML='<table style="margin:5px; width:135px">';

  // loop through each event in the feed
  for (var i = 0; i < feed.entry.length; i++) {
    var entry = feed.entry[i];
    var title = entry.title.$t;
	var where = entry.gd$where[0].valueString
    var start = formatGCalTime(entry['gd$when'][0].startTime);
    var end = formatGCalTime(entry['gd$when'][0].endTime);

    var dateString = "";
	//event starts and ends at same date
	if (start[0]==end[0]){
		dateString= start[0]+" "+start[1]+"-"+end[1]
	}
	else{
		dateString= start[0]+"-"+end[0]
	}

	eventsHTML += "<tr><td>"+dateString+"</td></tr>"
	eventsHTML += "<tr><td>&nbsp;"+title
	if (where!=""){eventsHTML += "<br>&nbsp;"+where;}
	eventsHTML += "</td></tr>"
  }
  eventsHTML += "</table><br><br>"
  events.innerHTML=eventsHTML;
}

/**
 * Callback function for the Google Calendar GData json-in-script call
 * Inserts the supplied list of events into a div of a pre-defined name
 *
 * @param {json} root is the JSON-formatted content from GData
 */
function insertGoogleAgenda(root) {
  listEvents(root, 'agenda');
}

