var searchRules=new Array();
//searchRules.push ('search:trefwoord|required');

window.onload = setFooterAlignment;

function blink(elementID){	
	// two blinks, 4*2 for four blinks
	var blinkCount = 2*2;
	do {
	   $('#' + elementID)['fade' + ((blinkCount%2 == 0) ? 'Out' : 'In')]('slow');	   
	} while (--blinkCount);
}

function setFFMacWidth(width){	
	if ($.browser.mac() && $.browser.firefox()) {
		return width;		
	} else {
		return 0;
	}
}

function setFooterAlignment(){
	var dHeight = $(document).height() - 30;		
	$('#footer').css('margin-top', dHeight);
	$('#footer').css('display', 'block');
}

function getPopupHTML(){
	return 			'<div id="player">'
					+ '<a href="http://www.adobe.com/go/getflashplayer">'
					+ '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Flash player" border="0">'
					+ '</a></div>';
}

/* logs the visitcard last screen */
function traceFinish(){
	$.get("/trace/visitcard/finished");
}

function traceSpaartool(){
	$.get("/trace/spaartool");
}

$(document).ready(function(){

	// replace standart selects with custom design
	$('.sel').each(function(){
			
			// get width of current select-box
			var w = $(this).width() + 30 + setFFMacWidth(30);			
			
			// get id of current select-box
			var id = $(this).attr('id');

			// hide current standart select-box
			$(this).css('display','none');

			var html_list = '<div class="c_list" style="width:'+ (w-24) +'px;display:none;" id="list_'+id+'" _sel="'+id+'">';

			$('option',this).each(function(){
				html_list += '<div class="c_item" _val="'+$(this).val()+'">'+$(this).text()+'</div>';
			});

			html_list += '</div>';

			var sel_txt = $('option:selected',this).text();

			// create facade of custom select-box
			$(this).after(
					$('<div>')
						.addClass('c_sel_out')
						.css('width',w)
						.append(
							'<div class="c_sel" id="sel_'+id+'" style="width:'+w+'px">'
								+ '<div class="c_sel_left"></div>'
								+ '<div class="c_sel_text" style="width:'+(w-39)+'px">'+sel_txt+'</div>'
								+ '<div class="c_sel_right" _list="'+id+'"></div><br clear="all"/>'
								+ html_list
							+'</div>'
						)
						// bind mouseleve handler which hides custom list when mouse is out
						.bind('mouseleave',function(){
								$('.c_list',this).css('display','none');
								$('.c_list_background',this).css('display','none');
								
							})
						.mouseout(
							function(e) {
								if( ! $(e.target).is('.c_item')) return;
								$(e.target).removeClass('c_item_over') ;
							}
						)
						.mouseover(
							function(e) {
								if( ! $(e.target).is('.c_item')) return;
								$(e.target).addClass('c_item_over')
							}
						)
						.click(function(e) {
								var node = $(e.target);
								if (node.is('.c_sel_right'))
								{
									$('#list_' + id).css('display',$('#list_' + id).css('display')=='none'?'':'none');
									$('#list_' + id + '_2').css('display',$('#list_' + id + '_2').css('display')=='none'?'':'none');
									return;
								}
								if( ! node.is('.c_item')) return;
								$('#list_' + id).css('display','none');
								$('#list_' + id + '_2').css('display','none');
								$('#' + id).val(node.attr('_val'));
								$('.c_sel_text','#sel_' + id).text(node.text());								
								$('#' + id).change();
							}
						)											
				);
				
				var c_list_height = $('.c_list').height();			
				$('.c_list_background').height(c_list_height + 53);				
	});

});

function str_replace(search, replace, subject) {
   var s = subject;
   var ra = r instanceof Array, sa = s instanceof Array;
   var f = [].concat(search);
   var r = [].concat(replace);
   var i = (s = [].concat(s)).length;
   var j = 0;
   while (j = 0, i--) {
       if (s[i]) {
           while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
       }
   }
   return sa ? s : s[0];
}
function parseUrl(data) {
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*(\/)?)([\w\-\.]+\.[^#?\s]+)?(#[\w\-]+)?$/;
    if (data.match(e)) {
        return  {path:RegExp.$4};
    }
    else {
        return  {path:""};
    }
}