/* Javascript Document Ready */ 
$(document).ready(function(){

	ShowChildElement();
	UR_Start();
	
	/* Sprache 1 auswählen */
	$('#lang').tabs({ selected: 0 });
		
	/* Elemente ausblenden */
	$('[hide="true"]').css('visibility','visible').hide();
	
	/* Formulare validieren Client */
	$('form[validate="true"]').each(function(){ $(this).validate(); });
	
	/* Activate Inactive Elements */
	$('.inactive').focus(function(){
		$(this).removeClass('inactive');
		$(this).addClass('active');
		if ($(this).val()==$(this).attr('default'))
			$(this).attr('value','');	
	});
	
	$('.active').live('focusout',function(){
		if ( ($(this).val()==$(this).attr('default')) || ($(this).val()=='') )
		{
			$(this).removeClass('active');
			$(this).addClass('inactive');
			$(this).attr('value',$(this).attr('default'));	
		}
	});
	
	$('#images a').lightBox({

    imageLoading: 'css/img/lightbox/lightbox-ico-loading.gif',

    imageBtnClose: 'css/img/lightbox/lightbox-btn-close.gif',

    imageBtnNext: 'css/img/lightbox/lightbox-btn-next.gif',

    imageBtnPrev: 'css/img/lightbox/lightbox-btn-prev.gif'

  });
  
  	$('.lightbox').lightBox({

    imageLoading: 'css/img/lightbox/lightbox-ico-loading.gif',

    imageBtnClose: 'css/img/lightbox/lightbox-btn-close.gif',

    imageBtnNext: 'css/img/lightbox/lightbox-btn-next.gif',

    imageBtnPrev: 'css/img/lightbox/lightbox-btn-prev.gif'

  });
	
	$("#newscontainer").cycle({ 
		fx:     'scrollHorz',
		timeout: 6000,
		delay:  -2000 				   
	 });
	
	$('#arrow_left').click(function(){
		$("#newscontainer").cycle('prev');
	});
	
	$('#arrow_right').click(function(){
		$("#newscontainer").cycle('next');
	});
	
	$("#newscontainer").hover(function(){
		$("#newscontainer").cycle('pause');
	},function(){
		$("#newscontainer").cycle('resume');		
	});
	
	/* Star Rating */
	// $('radio.star').rating({cancel: 'Cancel', cancelValue: '0'}); 
	
	$('[datum=true]').live("mouseover",function()
	{
		$(this).datepicker({
			dateFormat: 'dd.mm.yy',
			dayNames: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
			dayNamesMin: ['SO', 'MO', 'DI', 'MI', 'DO', 'FR', 'SA'],
			dayNamesShort: ['Son', 'Mon', 'Die', 'Mit', 'Don', 'Fre', 'Sam'],
			monthNames: ['Jaenner', 'Februar', 'Maerz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
			firstDay: 1
		});
	});
	
	$('.add_rt_row').click(function(){
		
		clone = $('tr.rt_row:last').clone();
		
		clone.find('.input').removeAttr('id');
		clone.insertAfter($('tr.rt_row:last'));
		clone.find('.input').removeClass('hasDatepicker').attr('value','');
		
	});
	
	/* Initialize the Upload Equipment */
 	initUploads();
	$('#moreImages').click(function(){
		if ($(this).parent().children('div').length <= 10)
			$(this).parent().children('div:first').clone().insertBefore($(this));
		else
			$(this).hide();
	});
	
	$('textarea').elastic();
	
	/* Add Field */
	$('.addField').click(function(){
		$(this).hide();
		$(this).next().hide();
		$(this).prev().show();
		$('<input type="text" name="'+$(this).attr('id')+'" style="width:305px;">').insertAfter(this);	
	})
	
	$('.redo').click(function(){
		$(this).hide();
		$(this).next().show();
		$(this).next().next().remove();
		$(this).next().next().show();
	});

	$('#dropdown').click(function(){
	
		if ($(this).hasClass('closed'))
		{
			$('#dropdown_child').show();	
		}
		else
		{
			$('#dropdown_child').hide();
		}
		
		$(this).toggleClass('closed');
		
	});

	/* Hide Adminbuttons */
	$('.adminbuttons').hide();
	
	/* ON Hover Show Adminbuttons */
	$('.adminbuttons').parent('div').hover(function(){$(this).children('.adminbuttons').show()},
								function(){$(this).children('.adminbuttons').hide()});
	
								

	/* Profil Img Select */
	$('.img-select').hover(function(){
	
		$(this).removeClass('ui-state-default');
		$(this).addClass('ui-state-hover');
		
	},function(){
		
		$(this).removeClass('ui-state-hover');
		$(this).addClass('ui-state-default');
		
	});
	$('.img-select').click(function(){
	
		$('.img-select').removeClass('ui-state-active');
		$('.img-select').addClass('ui-state-default');
		
		$(this).removeClass('ui-state-default');
		$(this).addClass('ui-state-active');
		
		$('#image').attr('value',$(this).attr('target'));
		
	});
	
	// Initialize the editor.
	// Callback function can be passed and executed after full instance creation.
	$('.editor').ckeditor({width:500});
	CKFinder.setupCKEditor( null, 'ckfinder/' ) ;
	
	$('[openwith="hover"]').hover(function()
	{
		if ($(this).attr('open')==0)
		{
			$(this).contents('div.child').slideDown(200);
			$(this).contents('img.arrow').attr('src','css/img/buttons/arrow_down_w.gif');
			$(this).attr('open',1);
		}
		else
		{
			$(this).contents('div.child').hide();
			$(this).contents('img.arrow').attr('src','css/img/buttons/arrow_right_w.gif');
			$(this).attr('open',0);
		}
	},function(){
		if ($(this).attr('open')==0)
		{
			$(this).contents('div.child').slideDown(200);
			$(this).contents('img.arrow').attr('src','css/img/buttons/arrow_down_w.gif');
			$(this).attr('open',1);
		}
		else
		{
			$(this).contents('div.child').hide();
			$(this).contents('img.arrow').attr('src','css/img/buttons/arrow_right_w.gif');
			$(this).attr('open',0);
		}
	});
		
	$('.item[openwith="click"][open="0"]').each(function(){
		
		if (!$(this).find('div.childitem[open="1"]').size()>0)
			$(this).find('div.child').hide();
			
	});

	$('.item[openwith="click"]').click(function()
	{
		if ($(this).attr('open')==0)
		{
			$(this).contents('div.child').slideDown(200);
			$(this).contents('img.arrow').attr('src','css/img/buttons/arrow_down.gif');
			$(this).attr('open',1);
		}
		/*else
		{
			$(this).contents('div.child').hide();
			$(this).contents('img.arrow').attr('src','css/img/buttons/arrow_right.gif');
			$(this).attr('open',0);
		}*/
	});
	
	$('[load=hover]').hover(function()
	{
	  	src = $(this).attr("src");
		name = src.substr(0,src.length-4);
		typ = src.substr(src.length-3,3);
		$(this).attr("src",name+'_hover.'+typ);
	},function()
	{
		src = $(this).attr("src");
		name = src.substr(0,src.length-10);
		typ = src.substr(src.length-3,3);
	    $(this).attr("src",name+'.'+typ);
	});
	
	ShowFormElement($('#typ').val());
	
	
});
  
  
function changeOrt()
{
	$('.ortSelect').hide();
	id = $('#bundesland').attr('value');
	
	$('#ort_'+id).show();
	
	if (id!=0)
		$('.ortAll').show();
	else
		$('.ortAll').hide()
}
  
  
function changeArt()

{

  id = $('#art').val();

  art = $('#art option:selected').attr('art');



  $('.subart').hide();

  $('[art="'+id+'"]').show();



  if (art == "wohnung")

  {

    $('#zimmer_row').show();

    $('#wohn_row').show();

    $('#nutz_row').show();

    

    $('.opt').show();

  }

  else if (art == "wohnhaus")

  {

    $('#zimmer_row').show();

    $('#wohn_row').show();

    $('#nutz_row').show();

    

    $('.opt').show();

  }

  else if (art == "grundstück")

  {

    $('#zimmer_row').hide();

    $('#wohn_row').hide();

    $('#nutz_row').hide();

    

    $('.opt').hide();

  }

  else if (art == "gewerbe")

  {

    $('#wohn_row').hide();

    $('#zimmer_row').hide();

    $('#nutz_row').show();

    

    $('.opt').show();

  }

  else if (art == "analgeobjekt")

  {

    $('#zimmer_row').show();

    $('#wohn_row').show();

    $('#nutz_row').show();

    

    $('.opt').show();

  }

  else if (art == "sonderobjekt")

  {

    $('#zimmer_row').show();

    $('#wohn_row').show();

    $('#nutz_row').show();

    

    $('.opt').show();

  }

}



function changeRechtsform()

{

  rf = $('#rechtsform').val();

  

  if (rf == 'miete')

  {

    $('#miete_row').show();

    $('#betriebskosten_row').show();

    $('#kauf_row').hide();

    

  }

  else if (rf == 'kauf')

  {

    $('#kauf_row').show();

    $('#miete_row').hide();

    $('#betriebskosten_row').hide();

  }

  else

  {

     $('#kauf_row').show();

    $('#miete_row').show();

    $('#betriebskosten_row').show();  

  }

}


function ShowFormElement(elem)
{
	$("div.artikelname").hide();
	$("div.artikeltext").hide();
	$("div#artikellink").hide();
	$("div#artikeltarget").hide();
	$("div.artikelnavname").hide();
	$("div#artikelstart").hide()
	$("div#artikeldatum").hide();
	$("div#artikelfrontpage").hide();
	$("div.artikelpretext").hide();
	$("div#artikelsptyp").hide();
	
	switch (parseInt(elem)) {
		case 0: $("div.artikeltext").show();
				$("div#artikelfrontpage").show();
				$("div.artikelnavname").show();
				$("div.artikelname").show();
				$("div#artikeltarget").show();
				$("div#artikelposition").show();
				$("div#artikelsptyp").show();
				break;
		case 1:	$("div.artikeltext").show();
				$("div.artikelpretext").show();
				$("div#artikelfrontpage").show();
				$("div#artikelposition").show();
				$("div.artikelnavname").show();
				$("div.artikelname").show();
				$("div#artikeltarget").show();
				break;
		case 2:	$("div.artikelnavname").show();
				$("div#artikelposition").show();
				break;
		case 3:	break;
		case 4: $("div.artikelnavname").show();
				$("div#artikellink").show();
				$("div#artikeltarget").show();
				$("div#artikelposition").show();
				break;
		case 5:	$("div#artikelfrontpage").show();
				$("div.artikelpretext").show();
				$("div#artikelposition").show();
				$("div.artikelnavname").show();
				$("div.artikelname").show();
				$("div.artikeltext").show();
				$("div#artikeltarget").show();
				break;
		case 6:	$("div#artikelfrontpage").show();
				$("div#artikelposition").show();
				$("div.artikelpretext").show();
				$("div.artikelnavname").show();
				$("div#artikeldatum").show();
				$("div#artikelstart").show();
				$("div.artikelname").show();
				$("div.artikeltext").show();
				$("div#artikeltarget").show();
				break;
		case 7: $("div.artikelnavname").show();
				$("div#artikeltarget").show();
				$("div#artikelposition").show();
				break;
	}
	
}

function ShowChildElement()
{
	$(".pos_select").hide();
	$("#pos_"+$('#position').val()).show();	
}

function OpenMenuElement(elem)
{	
	$('#menu_'+elem+'>child').show();
	$('[parent="'+elem+'"]').parent('div.item').show();
}

function bookmark(url,title)
{
	if (document.all)
	window.external.AddFavorite(url,title)
	else if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
}

function UR_Start() 
{
	UR_Nu = new Date;
	UR_Indhold = showFilled(UR_Nu.getHours()) + ":" + showFilled(UR_Nu.getMinutes()) + ":" + showFilled(UR_Nu.getSeconds());
	$('#clock').html(UR_Indhold);
	setTimeout("UR_Start()",1000);
}
function showFilled(Value) 
{
	return (Value > 9) ? "" + Value : "0" + Value;
}

