jQuery(document).ready( function() {

  jQuery("body.home div#content .csc-default:first-child").css("border-top", "0px");

  if(jQuery("#webcam1").length > 0)
  {
      //reloadImage();
      new reloadImage(60, 'countdown1', '1');
  }
  
  if(jQuery("#webcam2").length > 0)
  {
      //reloadImage();
      new reloadImage(60, 'countdown2', '2');
  }

  jQuery("#inp-username").focus( function() {
    if(jQuery("#inp-username").val()=='Benutzername') {
      jQuery("#inp-username").val('');
    }
  });
  jQuery("#inp-username").blur( function() {
    if(jQuery("#inp-username").val()=='') {
      jQuery("#inp-username").val('Benutzername');
    }
  });
  jQuery("#inp-password").focus( function() {
    if(jQuery("#inp-password").val()=='Passwort') {
      jQuery("#inp-password").val('');
    }
  });
  jQuery("#inp-password").blur( function() {
    if(jQuery("#inp-password").val()=='') {
      jQuery("#inp-password").val('Passwort');
    }
  });
  jQuery("ul#mainnavi li span a").mouseover( function() {
    jQuery(this).parent("span").animate({backgroundPosition: '0 -1px'},30,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -2px'},30,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -3px'},30,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -4px'},30,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -5px'},30,'linear');
  });
  jQuery("ul#mainnavi li span a").mouseout( function() {
    jQuery(this).parent("span").animate({backgroundPosition: '0 -4px'},50,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -3px'},50,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -2px'},50,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 -1px'},50,'linear');
    jQuery(this).parent("span").animate({backgroundPosition: '0 0'},50,'linear');
  });


	jQuery(".slideshow").cycle({
			timeout: 8000,
			speed:   2000,
			fx:      'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			random:  0
		});


  jQuery("img[alt='banner1']").parent().fancybox({
		'padding'         : 0,
		'margin'          : 0,
		'width' 	        : 1009,
		'height' 	        : 618,
    'autoScale'     	: false,
    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'centerOnScroll'  : false,
		'titleShow'       : false

	});


	jQuery('.single a.jqfancybox').each(function(idx, item)
	{
      var strRemove = location.protocol+'//'+location.host+'/';
		  var imagePath = item.getAttribute('href');

		  imagePath = imagePath.replace(strRemove, "");

		  var imageLink = '<a href="fileadmin/templates/scripts/download_images.php?image=/'+imagePath+'">download</a>';

		  var divSingle = jQuery(item).parents('.single');

		  jQuery(divSingle).children('span').html(imageLink);
	});

});


function hoverRow(element)
{
	var tableColumns = jQuery(element).children();
	for (var i = 0; i <= tableColumns.length; i++)
	{
    jQuery(tableColumns[i]).find("a").css("color", "#F03300");
		jQuery(tableColumns[i]).css("background", "#F5FAFF");
	}

	//console.log(element);
}

function unhoverRow(element)
{
	var tableColumns = jQuery(element).children();

	for (var i = 0; i <= tableColumns.length; i++)
	{
    jQuery(tableColumns[i]).find("a").css("color", "#1555A0");
		jQuery(tableColumns[i]).css("background", "#FFFFFF");
	}
}


Function.prototype.Timer = function (interval, calls, onend)
{
  var count = 0;
  var payloadFunction = this;
  var startTime = new Date();
  var callbackFunction = function () {
    return payloadFunction(startTime, count);
  };

  var endFunction = function () {
    if (onend) {
      onend(startTime, count, calls);
    }
  };

  var timerFunction =  function () {
    count++;
    if (count < calls && callbackFunction() != false) {
      window.setTimeout(timerFunction, interval);
    } else {
      endFunction();
    }
  };
  timerFunction();

};


function leadingzero (number)
{
    return (number < 10) ? '0' + number : number;
}


function reloadDelayedImage(imageId)
{
   var now = new Date();
   jQuery("#webcam"+imageId).attr('src', 'http://webcam.edwm.de/WeWueCam'+imageId+'.jpg?' + now.getTime());
}


function reloadImage(seconds, target, imageId)
{
    var element = document.getElementById(target);

    var calculateAndShow = function ()
    {
      if (seconds >= 0)
      {
        var h = Math.floor(seconds / 3600);
        var m = Math.floor((seconds % 3600) / 60);
        var s = seconds % 60;
        element.innerHTML=
          leadingzero(s);
        seconds--;
      }
      else
      {
        return false;
      }
    };

    var completed = function ()
    {
      var now = new Date();
      jQuery("#webcam"+imageId).attr('src', 'http://webcam.edwm.de/WeWueCam'+imageId+'.jpg?' + now.getTime());
      var imgHeight = jQuery("#webcam"+imageId).height();
      if(imgHeight < 375)
      {
         setTimeout("reloadDelayedImage("+imageId+")", 1000);
      }
      new reloadImage(60, target, imageId);
    };

    calculateAndShow.Timer(1000, Infinity, completed);

    //setTimeout("reloadImage()", 10000);
}

