var search_block = false;
var previewHeight = 90;
var previewWidth = 120;

var aktPreviewSize = 2;
var resultViewType = 1;
var is_ie6 = true;
var aktPage = 0;
var pending_resultupdate = false;
var review_choose_article;

function init_search()
{
	$("search_chooser").style.display = "none";
	
	justSelectFirst($('itunesCongress'));
	justSelectFirst($('itunesGroup'));
	justSelectFirst($('itunesSession'));
	
	$('itunesCongress').disabled = false;
	$('itunesGroup').disabled = false;
	$('itunesSession').disabled = false;
	
	if(!(navigator.appName.match(/Microsoft Internet Explorer/) && navigator.appVersion.match(/MSIE 6.0/)))
	{
		is_ie6 = false;
		var top1 = findPosition($('itunesCongress')).top+((150-16)/2);
		var top2 = findPosition($('itunesSession')).top+((150-16)/2);
	
		$('itunesCongressIndicator').style.top = top1+"px";
		$('itunesGroupIndicator').style.top = top1+"px";
		$('itunesSessionIndicator').style.top = top2+"px";
		$('itunesCongressIndicator').style.left = findPosition($('itunesCongress')).left+((itunesCongressWidth-16)/2)+"px";
		$('itunesGroupIndicator').style.left = findPosition($('itunesGroup')).left+((itunesGroupWidth-16)/2)+"px";
		$('itunesSessionIndicator').style.left = findPosition($('itunesSession')).left+((itunesSessionWidth-16)/2)+"px";
	}
}

function justSelectFirst(select)
{
	select.childNodes[0].selected = true;
	for(i=1;i<select.childNodes.length;i++)
	{
		if(select.childNodes[i].selected)
		{
			select.childNodes[i].selected = false;
		}
	}
}

function setItunesElementsAndIndicators(start, value)
{
	setItunesElements(start, value);
	setItunesIndicators(start, value);
}

function setItunesElements(start, value)
{
	switch(start)
	{
		case 3:
			$('itunesCongress').disabled = value;
			$('itunesCongress').style.backgroundColor = value ? '#EEEEEE' : '#FFFFFF';
			start=start-1;
		case 2:
			$('itunesGroup').disabled = value;
			$('itunesGroup').style.backgroundColor = value ? '#EEEEEE' : '#FFFFFF';
			start=start-1;
		case 1:
			$('itunesSession').disabled = value;
			$('itunesSession').style.backgroundColor = value ? '#EEEEEE' : '#FFFFFF';
	}
}

function setItunesIndicators(start, value)
{
	if(!is_ie6)
	{
		switch(start)
		{
			case 3:
				$('itunesCongressIndicator').style.display = value ? 'block' : 'none';
				start=start-1;
			case 2:
				$('itunesGroupIndicator').style.display = value ? 'block' : 'none';
				start=start-1;
			case 1:
				$('itunesSessionIndicator').style.display = value ? 'block' : 'none';
		}
	}
	
	if(value)
	{
		$('resultIndicator').style.visibility = 'visible';
	}
	else
	{
		$('resultIndicator').style.visibility = 'hidden';
	}
}


function changeSearchType(type)
{
	$("search_label_type").innerHTML = type;
	if($('searchfield').value!='')
	{
		processItunesSearch();
	}
}

function switchSearchchooser()
{
	if(search_block)
	{
		return;
	}
	if($("search_chooser").style.display != "none" && !search_block)
	{
		search_block = true;
		$("search_chooser").hide({duration: 0.2});
		setTimeout("search_block=false", 500);
		return;
	}
	search_block = true;
	position = findPosition($("search_choose_img"))
	if(is_ie6)
	{
		$("search_chooser").style.top = position.top-40+"px";
	}
	else
	{
		$("search_chooser").style.top = position.top+14+"px";
	}
	$("search_chooser").style.left = position.left+"px";
	$("search_chooser").style.display = "none";
	$("search_chooser").appear({duration: 0.5});
	setTimeout("search_block=false", 500);
}

function closeAllModalWindows()
{
	if(!search_block)
	{
		if($("search_chooser").style.display != "none")
		{
			$("search_chooser").fade({duration: 0.2});
		}
	}
}

function findPosition(obj)
{
	var curleft = curtop = 0;
	if(obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}while(obj = obj.offsetParent)
		return {left: curleft, top: curtop};
	}
}

function setPreviewSize(size)
{
	if(size == aktPreviewSize)
	{
		return;
	}
	
	percent = 0;
	
	$('iPreviewSize'+size).src = "/images/browse/browse_size_list_"+size+"_h.png";
	$('iPreviewSize'+aktPreviewSize).src = "/images/browse/browse_size_list_"+aktPreviewSize+".png";
	
	$('iPreviewSize1').style.cursor = 'pointer';
	$('iPreviewSize2').style.cursor = 'pointer';
	$('iPreviewSize3').style.cursor = 'pointer';
	
	$$(".preview_image").each(
		function(item)
		{
			if(resultViewType == 1)
			{	// list
				new Effect.Scale(item, size*43-29, { scaleMode: { originalHeight: previewHeight, originalWidth: previewWidth }, scaleFrom: aktPreviewSize*43-29 });
			}
			else
			{	// grid
				new Effect.Scale(item, size*43+14, { scaleMode: { originalHeight: previewHeight, originalWidth: previewWidth }, scaleFrom: aktPreviewSize*43+14 });
			}
		}
	)
	aktPreviewSize = size;
}

function setResultView(type)
{
	if(type==resultViewType)
	{
		return;
	}
	
	switch(type)
	{
		case 1:
			$('iResultType1').src = "/images/browse/browse_type_list_h.png";
			$('iResultType2').src = "/images/browse/browse_type_icon.png";
			break;
		case 2:
			$('iResultType1').src = "/images/browse/browse_type_list.png";
			$('iResultType2').src = "/images/browse/browse_type_icon_h.png";
			break;
	}
	
	resultViewType = type;
	
	doRequestForResultUpdate();
}

function serializeItunes(page)
{
	if(page==undefined)
	{
		page = 0;
	}
	retval = "previewSize="+aktPreviewSize;
	retval += "&page="+page;
	retval += "&resultViewType="+resultViewType;
	if(!sessionview)
	{
		retval += "&search="+Base64.encode($('searchfield').value);
		retval += "&"+serializeItunesSelect($('itunesGroup'));
		retval += "&"+serializeItunesSelect($('itunesCongress'));
		retval += "&"+serializeItunesSelect($('itunesSession'));
		retval += "&"+Form.serialize($('search_what_form'));
		retval += "&view="+view;
	}
	else
	{
		retval += "&search=AA==&itunesGroup=all&itunesCongress=all&itunesSession="+sessionid;
	}
	
	return retval;
}

function serializeItunesSelect(select)
{
	retval = select.id+"="
	
	if(select.childNodes[0].selected)
	{
		justSelectFirst(select);
		retval += "all";
		return retval;
	}
	
	for(i=0;i<select.childNodes.length;i++)
	{
		if(select.childNodes[i].selected)
		{
			if(!retval.match(/\=$/))
			{
				retval += ","
			}
			retval += select.childNodes[i].value;
		}
	}
	return retval;
}

var lastCallIdentifier;
var pending_request = false;
function processItunesSearch()
{
	setItunesElements(3, true)
	if($('searchfield').value=="")
	{
		$('searchClear').style.visibility = "hidden";
		lastCallIdentifier = Math.random();
		doItunesSearch(lastCallIdentifier);
	}
	else
	{
		$('searchClear').style.visibility = "visible";
		lastCallIdentifier = Math.random();
		setTimeout("doItunesSearch("+lastCallIdentifier+")", 500)
	}
}

function showPlayer()
{
  $('modal_play_link').style.background = '#FFFFFF';
  $('modal_abstract_link').style.background = 'transparent';
  $('modal_abstract').style.display = 'none';
  if(is_ie6)
  { 
    $('modal_player').style.display = 'block';
  }
  else
  {
    $('modal_player').style.height = '605px';
    $('modal_player').style.visibility = 'visible';
  }
}

function showAbstract()
{
  $('modal_play_link').style.background = 'transparent';
  $('modal_abstract_link').style.background = '#FFFFFF';
  if(is_ie6)
  { 
    $('modal_player').style.display = 'none';
  }
  else
  {
    $('modal_player').style.height = '0px';
    $('modal_player').style.visibility = 'hidden';
  }
  $('modal_abstract').style.display = 'block';
}
