
//--- admin control layers
function showAdminControl(controlid) {
	hideAllAdminControls(controlid);
	$('#'+controlid).show();
}

function hideAdminControl(controlid) {
	$('#'+controlid).hide();
}

function hideAllAdminControls(except) {
	$('div.admincontrol').each(function () {
		if (this.id != except) {
			$(this).hide();
		}
	});
	$('div._admincontrol').each(function () {
		if (this.id != except) {
			$(this).hide();
		}
	});
}

$(document).ready(function(){
	//--- click anywhere closes the admin controls
	$(document).click(function () { 
		hideAllAdminControls();
	} );
});


//--- colorbox related stuff
$(document).ready(function(){
	$("a.picturepreview").colorbox({
		current: "Bild {current} von {total}",
		previous: "voriges",
		next: "nächstes",
		close: "schließen"
	});
});



//--- switch buttons overlays
$(document).ready(function(){
	$("#headerswitches > a > img").each(function () { 
		$(this).mouseover( function() {
			$(this).animate({opacity: 0.5}, 'fast');
		} );
		$(this).mouseout( function() {
			$(this).animate({opacity: 1}, 'fast');
		} );
	});

	$("#headerlanguages > a > img").each(function () { 
		$(this).mouseover( function() {
			$(this).animate({opacity: 0.5}, 'fast');
		} );
		$(this).mouseout( function() {
			$(this).animate({opacity: 1}, 'fast');
		} );
	});
});



//--- forms related preparation
$(document).ready(function(){
	var searchfield = $("#searchinput");
	if (searchfield) {
		searchfield.focus(function () { 
			if (this.value == 'Suchbegriff') {
				this.value = '';
			}
		} );
		searchfield.blur(function () { 
			if (this.value == '') {
				this.value = 'Suchbegriff';
			}
		} );
	}
	var logindiv = $("#boxlogindiv");
	if (logindiv) {
		var logindivoverlay = $('#logindivoverlay');
		var loginform = $('#boxlogindiv > form');
		if (loginform) {
			loginform.submit(function () {
				logindivoverlay.width(logindivoverlay.parent().width());
				logindivoverlay.height(logindivoverlay.parent().height());
				$('#logindivoverlay > image').css('margin', ''+parseInt((logindivoverlay.height()-31) / 2)+'px 0px 0px '+parseInt((logindivoverlay.width()-31) / 2)+'px');
				logindivoverlay.fadeIn(function() { 
					$.post("./?action=login", loginform.serializeArray(), function(data) {
						if (data.success) {
							window.location.reload();
						} else {
							logindiv.find("span.general_error").each(function () { $(this).html(data.errors); });
							loginform.find("input").each(function () { this.disabled = false; });
							loginform.find("input").each(function () { 
								if (this.name == 'fdsa') { this.select(); this.focus(); } 
								if (this.name == 'asdf') { this.value = ''; } 
							});
						}
						logindivoverlay.width(logindivoverlay.parent().width());
						logindivoverlay.height(logindivoverlay.parent().height());
						$('#logindivoverlay > image').css('margin', ''+parseInt((logindivoverlay.height()-31) / 2)+'px 0px 0px '+parseInt((logindivoverlay.width()-31) / 2)+'px');
						logindivoverlay.fadeOut();
					}, 'json');

					loginform.find("input").each(function () { this.disabled = true; });
				} );

				return false;
			} );
		}
	}
	
});


//--- projects clickable
$(document).ready(function(){
	$("ul.projects > li > div.inner > div.innerpadded").each(function () { 
		$(this).click(function () { 
			$(this).find('div > a').each(function () { 
				document.location = $(this).attr('href'); 
			});
		} );
	});
	$("ul.projects_archive > li > div.inner > div.innerpadded").each(function () { 
		$(this).click(function () { 
			$(this).find('div > a').each(function () { 
				document.location = $(this).attr('href'); 
			});
		} );
	});
});


//--- sortable lists triggers
$(document).ready(function(){
	$("ul.slideshow[id*='cmssl']").sortable({
		update: function(event, ui) { 
			$.get("./?action=serializeupdate&" + ui.item.parent().sortable('serialize'), function(data) {
				if (data) {
					alert(data);
				}
			});
		}
	});
});

//--- basecms forms loading please wait fixup
function formSubmit(f) {
	if (f._submitbutton) {
		if (f._submitbutton.length) {
			for (var i=0; i<f._submitbutton.length; i++) {
				f._submitbutton[i].value=' - - - - - - ';
				f._submitbutton[i].disabled=true;
			}
		} else {
			f._submitbutton.value=' - - - - - - ';
			f._submitbutton.disabled=true;
		}
	}
	if (f._cancelbutton) { 
		f._cancelbutton.value=' - - - - - - '; 
		f._cancelbutton.disabled=true;
	}
}

//--- standard popup stuff.
function popup(url, name, w, h) { 
	window.open(url, name, 'width='+w+',height='+h+',menubar=no,scrollbars=no,toolbars=no');
}

//--- open the flash popup
function pop_flash(theURL,winName,features) {
	window.open(theURL,winName,features);
}




//--- old stuff
var inpreparation=false;
var inpreparation_sub=false;


var isIE = false, isNS = false, isDOM = false, isNS4 = false;
var vis = "", invis = "";
if (document.all) {isIE = true; vis="visible";invis="hidden";}
if (document.layers){isNS = true; isNS4=true; vis="show";invis="hide";}
if (document.getElementById && !isIE) {isDOM=true; invis ="hidden"; vis = "visible"}

function show_div(who) {
  inpreparation = false;
  hide_div();
  if (isDOM) {
    document.getElementById(who).style.visibility=vis; 
  } else {
    if(isIE) { 
      document.getElementById(who).style.visibility=vis; 
    } else { 
      eval('document.layers.'+who+'.visibility=\''+vis+'\'');
    }
  }
}

function hide_div() {
  if (isDOM) {
    for(var i=0; i<document.getElementsByTagName("div").length; i++) {
      var idstr = document.getElementsByTagName("div")[i].getAttribute("id");
      if (idstr != "" && idstr != null && (idstr.indexOf("layer_") >= 0 || idstr.indexOf("layer_") >= 0)) { 
        document.getElementById(idstr).style.visibility=invis; 
      }
    }	
  } else {
    if(isIE) {
      for(var i=0; i<document.getElementsByTagName("div").length; i++) {
        var idstr = document.getElementsByTagName("div")[i].getAttribute("id");
        if (idstr && (idstr != "") && (idstr.indexOf("layer_") >= 0 || idstr.indexOf("layer_") >= 0)) { 
          document.getElementsByTagName("div")[i].style.visibility = invis; 
        }
      }	
    } else {
      for(var i=0; i<document.layers.length; i++) { 
        if (document.layers[i].id.indexOf("new_")==-1) { 
          document.layers[i].visibility = invis; 
        }
      }
    }
  }
}

function cpre() {
  inpreparation = false;
  cpre_sub();
}

function prepared_toclose() {
  if (inpreparation) { hide_div(); }
}

function preparetoclose() {
  inpreparation=true;
  setTimeout(prepared_toclose, 2000);
}


function show_sub(who) {
  inpreparation_sub = false;
  hide_sub();
  if (isDOM) {  
    document.getElementById(who).style.visibility=vis; 
  } else {
    if(isIE) { 
      document.getElementById(who).style.visibility=vis; 
    } else { 
      eval('document.layers.'+who+'.visibility=\''+vis+'\'');
    }
  }
}

function hide_sub() {
  if (isDOM) {
    for(var i=0; i<document.getElementsByTagName("div").length; i++) {
      var idstr = document.getElementsByTagName("div")[i].getAttribute("id");
      if (idstr != "" && idstr != null && (idstr.indexOf("Lsub_") >= 0)) { 
        document.getElementById(idstr).style.visibility=invis; 
      }
    }	
  } else {
    if(isIE) {
      for(var i=0; i<document.getElementsByTagName("div").length; i++) {
        var idstr = document.getElementsByTagName("div")[i].getAttribute("id");
        if (idstr && (idstr != "") && (idstr.indexOf("Lsub_") >= 0)) { 
          document.getElementsByTagName("div")[i].style.visibility = invis; 
        }
      }	
    } else {
      for(var i=0; i<document.layers.length; i++) { 
        if (document.layers[i].id.indexOf("new_")==-1) { 
          document.layers[i].visibility = invis; 
        }
      }
    }
  }
}



if (navigator.appName != "Netscape") {
 document.onmouseup = hide_sub;
} else {
 window.captureEvents(Event.MOUSEUP);
 window.onmouseup = hide_sub;
}

function cpre_sub() {
  inpreparation_sub = false;
}

function prepared_toclose_sub() {
  if (inpreparation_sub) { hide_sub(); }
}

function preparetoclose_sub() {
  inpreparation_sub=true;
  setTimeout(prepared_toclose_sub, 1000);
}
