var picArray;
var picDesc;
var picCounter = 0;
var locationEntry = false;
var noStat = false;

$(function(){
	$('#alert').dialog({
	    autoOpen: false,
	    width: 400,
	    modal: true,
	    resizable: false,
	    buttons: {
	        "Ok": function() {
	            $(this).dialog("close");
	        }
	    }
	});
}); 

$(document).ready(function() {
	var checkAlert = $('#alertText').html();
	if(checkAlert != ''){
	    $('#alert').dialog('open');
	    return false;
	}

  //Event Observer
    $("#formLogin").submit(function(){
		$.post(
		    "index.php",
		    $("#formLogin").serialize() + "&id=Login&event=do_login",
		    function(data){
                if(data.tr_logged == 1){
                    redirect('', 1500);
                }
                else{
                	$('#alertText').html(data.output);
                	$('#alert').dialog('open');
                }
		    }
		    , "json"
		);
        return false;
    });

    if($('form.formDesigner').length > 0){
    	$('form.formDesigner').each(function (index) {
    		var obj = this;
			var f_id = $(this).attr('id');
    		$('#'+f_id).submit(function(){
    			var checks = true;
    			$('#'+f_id).find(".f_required").each(function(index2){
	    			if($(this).attr('type') == 'radio'){
						if($("input[@name="+$(this).attr('name')+"]:checked").length > 0){

						}
						else{
							checks = false;
						}
	    			}
	    			else if($(this).attr('type') == 'checkbox'){
						if(!$(this).is(':checked')){
							checks = false;
						}
	    			}
	    			else{
						if($(this).val().length < 1){
							checks = false;
						}
	    			}
	    		});
	    		if(checks == false){
                	$('#alertText').html('Bitte füllen Sie alle Pflichtfelder aus!');
                	$('#alert').dialog('open');
                	return false;
	    		}
	    		return true;
	    	});
    	});
    	return false;
    }
});

function countKeywords(){
  var text = $('#keywordArea').val();
  $('#countKeywords').html((1001 - text.length));
}

function startUpload(){
  $('#file_upload').uploadifyUpload();
}

function PrevImage()
{
  document.getElementById('imageprev').style.visibility = 'visible';
}

function NextImage()
{
  document.getElementById('imagenext').style.visibility = 'visible';
}

function ClearImage()
{
  document.getElementById('imageprev').style.visibility = 'hidden';
  document.getElementById('imagenext').style.visibility = 'hidden';
}

function subcat(id){
  if(id){
		$.post(
		    "index.php",
        { "event": "inputRequests", "id" : "Register_ho", "mid": id, "todo": "getSubcat" },
		    function(data){
          if(data.text != ""){
            $('#kategorie2').remove();
            $('#cats').append(data.text);
          }
		    }
		    , "json"
		);
  }
}

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

function redirect(url, time){
	if(!time){
		time = 0;
	}
	if(url == ''){
		setTimeout(function(){ location.reload(true); }, time);
	}
	else{
		setTimeout(function(){ location.href = url }, time);
	}
	return;
}

function getDayBefore(before, today){
  var b1 = $('input[name|="'+before+'1"]').val();
  var b2 = $('input[name|="'+before+'2"]').val();
  var b3 = $('input[name|="'+before+'3"]').val();
  var b4 = $('input[name|="'+before+'4"]').val();
  $('input[name|="'+today+'1"]').val(b1);
  $('input[name|="'+today+'2"]').val(b2);
  $('input[name|="'+today+'3"]').val(b3);
  $('input[name|="'+today+'4"]').val(b4);
}

function sendOverview(send){
  if(send){
    $('#formOverview').append('<input type="hidden" name="buttonSave" value="1" />');
    $('#formOverview').submit();
  }
  else{
    $('#formOverview').append('<input type="hidden" name="buttonEdit" value="1" />');
    $('#formOverview').submit();
  }
  return false;
}

//Eintrag anzeigen
function showEntry(id){
	if(locationEntry && locationEntry != id){
		redirect('index.php?entry='+id);
		return;
	}
	$.post(
	    "index.php",
	    { "event": "branchenprofil", "entry": id },
	    function(data){
            if(data.output){
                $('#branchenContent').html(data.output);
                initialize('ajax_map_'+data.entryID);
                if(data.entryLng != "" && data.entryLat != ""){
                    setMarkerWithoutDrag(data.entryLat, data.entryLng);
                }
                document.title = data.newTitle;
                if(!noStat){
                    Statistik();
                }
            }
	    }
	    , "json"
	);
}

function redirect(url, time){
	if(!time){
		time = 0;
	}
	if(url == ''){
		setTimeout(function(){ location.reload(true); }, time);
	}
	else{
		setTimeout(function(){ location.href = url }, time);
	}
	return;
}

function logout(){
	$.post(
	    "index.php",
	    { "id": "Logout", "event": "do_logout" },
	    function(data){
               if(data.tr_logout == 1){
                   redirect('index.php', 0);
               }
	    }
	    , "json"
	);
    return false;
}

function showNextPic(back){
  if(back == true){
    if((picCounter - 1) < 0){
      picCounter = picArray.length - 1;
    }
    else{
      picCounter--;
    }
    var c = picCounter;
    var id = '#picBox_'+c;
    $('.bild img').fadeOut('fast');
    $('#kopftext').html('');
    $(id).fadeIn('slow', function(){ $('#kopftext').html(picDesc[c]); $('#kopftext').fadeIn('slow'); });
  }
  else{
    if((picCounter + 2) <= picArray.length){
      picCounter++;
    }
    else{
      picCounter = 0;
    }
    var c = picCounter;
    var id = '#picBox_'+c;
    $('.bild img').fadeOut('fast');
    $('#kopftext').html('');
    $(id).fadeIn('slow', function(){ $('#kopftext').html(picDesc[c]); $('#kopftext').fadeIn('slow'); });
  }
}


function loadDIV(div,url) {
  $(div).load(url);
}

function loadPanorama(id) {
  $('#panorama').load('/panorama.php?id='+id);
}

function ShopAngebot() {
  $('#hsbox1').load('/einkaufen/shopartikel.php');
}

