IE6 = isIE6();

function wait(millis) {
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}

//Rollover Extrainfo
function showInfo(id, todo) {
	var toShow = id + 'Long';
	var allElements = document.getElementsByClassName('listElementTopInfoMore');
	if (allElements.style && allElements.style.display == 'block') {
		allElements.style.display = 'none';
	}
	if (todo == 1) {
		wait(250);
		if (IE6) {
			$(toShow).style.display = 'block';
		} else {
			new Effect.Appear(toShow, { duration:0.3 });
		}
	} else {
		if (IE6) {
			$(toShow).style.display = 'none';
		} else {
			new Effect.Fade(toShow, { duration:0.1 });
		}
	}
}

//general Switcher
function switchit(switchID) {
	ivw('switchit/' + switchID);
	$(switchID).style.display = $(switchID).style.display == 'block'?'none':'block';
}

//allgemeiner Show/Hider, z.b: keywordlisten bei Bildern
function showHide (showID, hideID) {
	ivw('showHide/' + showID + '/' + hideID);
	$(showID).style.display = 'block';
	$(hideID).style.display = 'none';
}

//allgemeiner Hider
function hide (hideID) {
	ivw('hide/' + hideID);
	Element.hide(hideID);
	//$(hideID).style.display = 'none';
}

// ausschalten von 2 IDs -- Provisorium !
function hideBoth (hide1, hide2) {
	ivw('hide/' + hide1 + '&' + hide2);
	$(hide1).style.display = 'none';
	$(hide2).style.display = 'none';
}

function set_item_order(ul_id,li_id_prefix,order_array)
{
	//Position.includeScrollOffsets = true;
	for(i=0; i<order_array.length; i++)
	{
		element = li_id_prefix + order_array[i];
		if($(element))$(ul_id).appendChild($(element));

	}
}
/* the searchBox stuff */
function showSearch() {
	ivw('showSearch');
	document.forms["searchForm"].q.blur();
	$('search').style.display = $('search').style.display == 'block'?'none':'block';
	if ($('search').style.display == 'block') {
		document.forms["searchForm"].q.focus();
	}
}
function checkAllSearch() {
    el = document.forms["searchForm"].elements;
    for (i=0;i < el.length;i++) {
    	if(el[i].type.toLowerCase()=="checkbox")
    	{
	        if($('searchallpict').checked == true) {
	            el[i].checked = true;
			} else {
				el[i].checked = false;
				$('searchpict').checked = true;
			}
		}
    }
}
function setAllCheckbox(checked,checkbox_id,classOfCheckboxes,checked_box) {

	if(!checked) {
		$(checkbox_id).checked = false;
	} else {
		check_all = true;
		el = document.getElementsByClassName(classOfCheckboxes);
    	for (i=0;i < el.length;i++) {
	        if(!el[i].checked) {
	            check_all = false;
			}
	    }
		$(checkbox_id).checked = check_all;
	}
	//we need minimum one to search
	j=0;
	for (i=0;i < el.length;i++) {
        if(!el[i].checked) {
			j++;
		}
    }
	if (j == el.length) {//the last must be checked
		$(checked_box).checked = true;
	}
}


function checkAll(checked,className) {
    el = document.getElementsByClassName(className);
    for (i=0;i < el.length;i++) {
        el[i].checked = checked;
    }
}

//deprecated, use popup() instead if possible
function confirmBox(obj_name,callback_function,parameters)
{

	call = callback_function + '(';
	params = '';
	for(i=2; i< confirmBox.arguments.length; i++)
	{
		params += '"' + confirmBox.arguments[i] + '",';
	}
	call = call + params.substring(0,params.length-1) + ')';

	ivw('confirmBoxAnd-' + callback_function);
	$("lightBoxBackground").show();
	closeFlash();

	url = globalURL+'/mail/0/ajax_show_confirm';
	params = '?ak=' + theAjaxKey;
	params += '&name=' + encodeURIComponent(obj_name);
	new Ajax.Request(url,
		{
			method:'post',
			parameters: params,
			/*parameters: {
				action: 'compose',
				to_name: to_name,
				reply: reply
			},*/
			onSuccess: function(transport){
				$("lightBoxContent").innerHTML = transport.responseText;
				//der ie6 kennt kein position:fixed, also verschieben wir die lightbox, wenn gescrollt wurde
				browser = navigator.userAgent.toLowerCase();
				ganzzahl = parseInt(navigator.appVersion);

				$("lightBoxContent").show();
				if(IE6)
				{
					fixBoxPosition();
				}
				$('confirmBox_callback').value = call;
			},
			onFailure: function() {
				$("lightBoxBackground").hide();
				showFlash();
			}
		}
	);
}

function popup(type,id,callback_function,parameters)
{

	call = callback_function + '(';
	params = '';
	for(i=3; i< popup.arguments.length; i++)
	{
		params += '"' + popup.arguments[i] + '",';
	}
	call = call + params.substring(0,params.length-1) + ')';

	ivw('popupBoxAnd-' + callback_function);
	$("lightBoxBackground").show();
	closeFlash();

	url = globalURL+'/popup/'+id+'/ajax_confirm?mode='+type;
	params = '?ak=' + theAjaxKey;
	//params += '&name=' + encodeURIComponent(obj_name);
	new Ajax.Request(url,
		{
			method:'post',
			parameters: params,
			/*parameters: {
				action: 'compose',
				to_name: to_name,
				reply: reply
			},*/
			onSuccess: function(transport){
				$("lightBoxContent").innerHTML = transport.responseText;
				//der ie6 kennt kein position:fixed, also verschieben wir die lightbox, wenn gescrollt wurde
				browser = navigator.userAgent.toLowerCase();
				ganzzahl = parseInt(navigator.appVersion);

				$("lightBoxContent").show();
				if(IE6)
				{
					fixBoxPosition();
				}
				$('confirmBox_callback').value = call;
			},
			onFailure: function() {
				$("lightBoxBackground").hide();
				showFlash();
			}
		}
	);
}

function alertBox(message,title)
{
	ivw('alertBoxAnd-' + title);
	$("lightBoxBackground").show();
	closeFlash();

	url = globalURL+'/profile/0/ajax_show_alert';
	params = '?ak=' + theAjaxKey;
	params += '&message=' + encodeURIComponent(message);
	params += '&title=' + encodeURIComponent(title);
	new Ajax.Request(url,
		{
			method:'post',
			parameters: params,
			onSuccess: function(transport){
				$("lightBoxContent").innerHTML = transport.responseText;
				if(IE6)
				{
					fixBoxPosition();
				}
				$("lightBoxContent").show();
			},
			onFailure: function() {
				$("lightBoxBackground").hide();
				showFlash();
			}
		}
	);
}

function CheckboxesToValueArray(objects)
{
	output = new Array();
	for(i=0;i<objects.length;i++)
	{
		if(objects[i].checked)
		{
			output.push(objects[i].value);
		}
	}
	return output;
}

function pressedEnter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	return keycode == 13;
}

function call_ajax(url,params,Container4Refill,method)
{
	method = method!='post'?'get':'post';

	params += '&ak=' + theAjaxKey;
	var myAjax = new Ajax.Request(
		url,
			{
				method: method,
				parameters: params,
				onSuccess: function(aResponse) {
					if($(Container4Refill))
					{
						$(Container4Refill).innerHTML = aResponse.responseText;
						initPagerArrows($(Container4Refill));
					}
				}

			}
	);
}

function call_ajax_redirect(url,params,redirect)
{
	params += '&ak=' + theAjaxKey;
	var myAjax = new Ajax.Request(
		url,
			{
				method: 'get',
				parameters: params,
				onSuccess: function(aResponse) {
					//alert(aResponse.responseText);
					if(redirect)
					{
						document.location.href = redirect;
					}
				}

			}
	);
}

//adds trim() to Strings
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function isIE6()
{
	browser = navigator.userAgent.toLowerCase();
	ganzzahl = parseInt(navigator.appVersion);
	explorer = ((browser.indexOf("msie") != -1) && (browser.indexOf("opera") == -1));
	return (explorer && (ganzzahl == 4) && (browser.indexOf("msie 6.0") !=-1));
}

function isIE()
{
	browser = navigator.userAgent.toLowerCase();
	ganzzahl = parseInt(navigator.appVersion);
	explorer = ((browser.indexOf("msie") != -1) && (browser.indexOf("opera") == -1));
	return (explorer && (ganzzahl == 4));
}

function fixBoxPosition()
{
	scrollTop = document.documentElement.scrollTop
	oH = 450;
	if(scrollTop = document.documentElement.scrollTop)
	{
		$("lightBoxContent").style.top = (scrollTop + oH) + 'px';
	}
}

function finalizeSeries(seriesID)
{
	ivw('finalizeSeries');
	url = globalURL + '/series/'+seriesID+'/ajax_finalize';
	call_ajax(url,params,'seriesEditOptions');
	$('locked').show();
	$('p1').src = $('p1').src.replace('down','top');
	$('unlocked').hide();
	moveslider(1,1);
}

function reopenSeries(seriesID)
{
	ivw('reopenSeries');
	url = globalURL + '/series/'+seriesID+'/ajax_reopen';
	call_ajax(url,params,'seriesEditOptions');
	$('unlocked').show();
	$('locked').hide();
	$('p1').src = $('p1').src.replace('top','down');
	moveslider(1,1);
}

function countGalleryVote(contentID, contentType, vote)
{
  ivw('scoutGalleryVote');
	url = globalURL + '/gallery/' + contentID + '/ajax_recordvote';
  params += '&ak=' + theAjaxKey + '&contentType=' + contentType + '&vote=' + vote;

	var myAjax = new Ajax.Request(
		url,
			{
				method: 'get',
				parameters: params,
				onSuccess: function(aResponse) {
				}
			}
	);
}
