function changePic(url,caption) {
 $("p.mainpic img").attr("src",url);
 $("div.caption p").html(caption);
}

$(document).ready(function(){

$.datepicker.setDefaults($.datepicker.regional['de']);


initTabsFBDetails();

  $("a.editBtn").hover(function(){

$(this).parents("div.preview").css("border","1px solid #f00");
},function(){
$(this).parents("div.preview").css("border","1px dotted #f00");
});


$.each( $("div.onAccordionContent"), function(i, n){
	$(n).before($(n).children("h2:first-child").addClass("onAccHead"));
	if ($(n).hasClass('open')) {
	  $(n).prev(".onAccHead").addClass("selected");
	  }
	  else {
	        $(n).addClass("hide");
	  }
});

$("body.bayern img#submitfachbetriebe").attr({"src":"/img/bayern/system/suchen.gif"});
$("body.bayern img#submitfachbetriebe.absenden-detail").attr({"src":"/img/bayern/system/send-bayern.gif"});
$('body.bayern img[src$="suche.gif"],body.bayern input[src$="suche.gif"]').each(function(){
 $(this).attr({"src":"/img/bayern/system/suche.gif"});
});

$(".onAccordion").children("h2:first-child").addClass("first");
	
  $(".onAccHead").click(function() {
    $(this).addClass("current");
    $.each( $(this).parent(".onAccordion").children(".selected:not('.current')"), function(i, n){
      $(n).next("div").animate({ height: 'toggle', opacity: 'toggle'},{duration: 300,
        complete:function(){ 
          $(n).parent(".onAccordion").children(".onAccHead:not('.current')").removeClass("selected");
      } });
    });
    $(this).next("div").animate({ height: 'toggle', opacity: 'toggle'},{duration: 300, 
      complete:function(){
        $(this).prev(".onAccHead").toggleClass("selected");  
        $(this).prev(".onAccHead").removeClass("current");
    } });
  });

if ($('#marquee marquee').length > 0) {
$('#marquee marquee').marquee('pointer').mouseover(function () {
 $(this).trigger('stop');
 }).mouseout(function () {
 $(this).trigger('start');
 }).mousemove(function (event) {
 if ($(this).data('drag') == true) {
 this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
 }
 }).mousedown(function (event) {
 $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
 }).mouseup(function () {
 $(this).data('drag', false);
}); 
}

});


function dateTimeValidate(DateField, label, textCSS, errorCSS, required)
{
	var checkstr = "0123456789";
	var DateValue = "";
	var orgDateValue;
	var DateTemp = "";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	err = 0;
	if (DateField.value)
	{
		DateValue = DateField.value;
	}
	else
	{
		DateValue = DateField.Value;
	}
	if (DateValue == null) //unklar ... workaround
	{
		DateValue = "";
	}
	orgDateValue = DateValue;
	/* Delete all chars except 0..9 */
	for (i = 0; i < DateValue.length; i++) 
	{
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) 
		{
			DateTemp = DateTemp + DateValue.substr(i,1);
		}
	}
	DateValue = DateTemp;
	/* Always change date to 8 digits - string*/
	/* if year is entered as 2-digit / always assume 20xx */
	if (DateValue.length == 6) 
	{
		DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); 
	}
	if (DateValue.length != 8) 
	{
		err = 19;
	}
	/* year is wrong if year = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0) 
	{
		err = 20;
	}
	/* Validation of month*/
	month = DateValue.substr(2,2);
	if ((month < 1) || (month > 12)) 
	{
		err = 21;
	}
	/* Validation of day*/
	day = DateValue.substr(0,2);
	if (day < 1) 
	{
		err = 22;
	}
	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) 
	{
		leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29)) 
	{
		err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28)) 
	{
		err = 24;
	}
	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) 
	{
		err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) 
	{
		err = 26;
	}
	/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
	if (label != null)
	{
		if (err == 0) 
		{
			DateField.value = day + seperator + month + seperator + year;
			label.className=textCSS;
			DateField.IsValid = true;
		}
		else //Fehlerfall 
		{
			//"" nur dann Fehler, wenn req
			if (required || orgDateValue!="")
			{
				label.className=errorCSS;
				DateField.IsValid = false;
			}
			else
			{
  			label.className=textCSS;
				DateField.IsValid = true;
			}
		}
	}
	if (err == 0) 
	{
		DateField.IsValid = true;
	}
	else //Fehlerfall 
	{
			if (required || orgDateValue!="")
			{
				DateField.IsValid = false;
			}
			else 
			{
				DateField.IsValid = true;
			}
	}
}

function ClientValidateDate(source, arguments)
{
	//Das Datum wurde bereits auf xx.xx.xxxx umformatiert. Und isValid am Control gesetzt
	dateTimeValidate(arguments, null, "", "", false);
}

function initTabsFBDetails() {
  if ($('#tab_map').length > 0) {
  $('.generate-ui-tab').tabs({
    show: function (event, ui) {
      if (ui.panel.id == 'tab_map') {
        reInitGoogleMaps();
      }
     else if (ui.panel.id == 'tab_samples') {

    $('#mycarousel').jcarousel({
        scroll: 4});
 }
    }
  });
}
}
